/** * Invokes {@link HTTPSamplerBase#getArguments()} * * @return the arguments of the associated test element */ protected Arguments getArguments() { return testElement.getArguments(); }
/** * Compute content encoding * @param sampler {@link HTTPSamplerBase} * @param request {@link HttpRequestHdr} * @param pageEncodings Map of page encodings * @param formEncodings Map of form encodings * @throws MalformedURLException when no {@link URL} could be built from * <code>sampler</code> and <code>request</code> */ protected void computeContentEncoding(HTTPSamplerBase sampler, HttpRequestHdr request, Map<String, String> pageEncodings, Map<String, String> formEncodings) throws MalformedURLException { URL pageUrl = null; if(sampler.isProtocolDefaultPort()) { pageUrl = new URL(sampler.getProtocol(), sampler.getDomain(), request.getPath()); } else { pageUrl = new URL(sampler.getProtocol(), sampler.getDomain(), sampler.getPort(), request.getPath()); } String urlWithoutQuery = request.getUrlWithoutQuery(pageUrl); String contentEncoding = computeContentEncoding(request, pageEncodings, formEncodings, urlWithoutQuery); // Set the content encoding if(!StringUtils.isEmpty(contentEncoding)) { sampler.setContentEncoding(contentEncoding); } }
/** * Determine if we should use multipart/form-data or * application/x-www-form-urlencoded for the post * * @return true if multipart/form-data should be used and method is POST */ public boolean getUseMultipartForPost() { // We use multipart if we have been told so, or files are present // and the files should not be send as the post body HTTPFileArg[] files = getHTTPFiles(); return HTTPConstants.POST.equals(getMethod()) && (getDoMultipart() || (files.length > 0 && hasNoMissingFile(files) && !getSendFileAsPostBody())); }
private String generateMatchUrl(HTTPSamplerBase sampler) { StringBuilder buf = new StringBuilder(sampler.getDomain()); buf.append(':'); // $NON-NLS-1$ buf.append(sampler.getPort()); buf.append(sampler.getPath()); if (sampler.getQueryString().length() > 0) { buf.append('?'); // $NON-NLS-1$ buf.append(sampler.getQueryString()); } return buf.toString(); }
log.debug("Check for matches against: "+sampler.toString()); HTTPSamplerBase url = potentialLinks.get(ThreadLocalRandom.current().nextInt(potentialLinks.size())); if (log.isDebugEnabled()) { log.debug("Selected: "+url.toString()); sampler.setDomain(url.getDomain()); sampler.setPath(url.getPath()); if (url.getMethod().equals(HTTPConstants.POST)) { for (JMeterProperty jMeterProperty : sampler.getArguments()) { Argument arg = (Argument) jMeterProperty.getObjectValue(); modifyArgument(arg, url.getArguments()); sampler.setArguments(url.getArguments()); sampler.setProtocol(url.getProtocol()); } else { log.debug("No matches found");
String path = this.getPath(); return new URL(path); String domain = getDomain(); String protocol = getProtocol(); String method = getMethod(); if (PROTOCOL_FILE.equalsIgnoreCase(protocol)) { String queryString = getQueryString(getContentEncoding()); if (queryString.length() > 0) { if (path.contains(QRY_PFX)) {// Already contains a prefix if (isProtocolDefaultPort()) { return new URL(protocol, domain, pathAndQuery.toString()); return new URL(protocol, domain, getPort(), pathAndQuery.toString());
public int replace(String regex, String replaceBy, boolean caseSensitive) throws Exception { int totalReplaced = 0; for (JMeterProperty jMeterProperty : getArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); String value = arg.getValue(); String value = getPath(); if(!StringUtils.isEmpty(value)) { Object[] result = JOrphanUtils.replaceAllWithRegex(value, regex, replaceBy, caseSensitive); if (nbReplaced>0) { String replacedText = (String) result[0]; setPath(replacedText); totalReplaced += nbReplaced; if(!StringUtils.isEmpty(getDomain())) { Object[] result = JOrphanUtils.replaceAllWithRegex(getDomain(), regex, replaceBy, caseSensitive); setDomain(replacedText); totalReplaced += nbReplaced;
String contentEncoding = sampler.getContentEncoding(); urlConfig.parseArguments(postData); sampler.setDoMultipart(true); sampler.setArguments(urlConfig.getArguments()); sampler.setHTTPFiles(urlConfig.getHTTPFileArgs().asArray()); sampler.setDoBrowserCompatibleMultipart(true); // we are parsing browser input here || HTTPConstants.PUT.equals(sampler.getMethod()) || isPotentialXml(postData)) { sampler.addNonEncodedArgument("", postData, ""); } else if (contentType == null || (contentType.startsWith(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED) && sampler.parseArguments(postData.trim(), contentEncoding); //standard name=value postData } else if (postData.length() > 0) { if (isBinaryContent(contentType)) { FileUtils.writeByteArrayToFile(out,request.getRawPostData()); HTTPFileArg [] files = {new HTTPFileArg(out.getPath(),"",contentType)}; sampler.setHTTPFiles(files); } catch (IOException e) { log.warn("Could not create binary file: "+e); sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
query = URLDecoder.decode(newLink.getQueryString(), StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { final Arguments arguments = config.getArguments(); if (!isEqualOrMatches(newLink.getProtocol(), config.getProtocol(), matcher, patternCache)){ return false; final String domain = config.getDomain(); if (domain != null && domain.length() > 0) { if (!isEqualOrMatches(newLink.getDomain(), domain, matcher, patternCache)){ return false; final String path = config.getPath(); if (!newLink.getPath().equals(path) && !matcher.matches(newLink.getPath(), patternCache.getPattern("[/]*" + path, // $NON-NLS-1$ Perl5Compiler.READ_ONLY_MASK))) { return false;
public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws IOException { String contentEncoding = sampler.getContentEncoding(); if(contentEncoding == null || contentEncoding.length() == 0) { contentEncoding = ENCODING; HTTPFileArg[] files = sampler.getHTTPFiles(); if(sampler.getUseMultipart()) { for (JMeterProperty jMeterProperty : sampler.getArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); String parameterName = arg.getName(); writeFormMultipart(bos, parameterName, arg.getValue(), contentEncoding, sampler.getDoBrowserCompatibleMultipart()); if(sampler.getArguments() != null && sampler.getArguments().getArgumentCount() == 0 && sampler.getSendFileAsPostBody()) { if(!sampler.getSendParameterValuesAsPostBody()) { postBody = sampler.getQueryString(contentEncoding); for (JMeterProperty jMeterProperty : sampler.getArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); postBodyBuffer.append(arg.getEncodedValue(contentEncoding));
/** * Create a new Sampler based on an HREF string plus a contextual URL * object. Given that an HREF string might be of three possible forms, some * processing is required. * * @param parsedUrlString * the url from the href * @param context * the context in which the href was found. This is used to * extract url information that might be missing in * <code>parsedUrlString</code> * @return sampler with filled in information about the fully parsed url * @throws MalformedURLException * when the given url (<code>parsedUrlString</code> plus * <code>context</code> is malformed) */ public static HTTPSamplerBase createUrlFromAnchor(String parsedUrlString, URL context) throws MalformedURLException { if (log.isDebugEnabled()) { log.debug("Creating URL from Anchor: " + parsedUrlString + ", base: " + context); } URL url = ConversionUtils.makeRelativeURL(context, parsedUrlString); HTTPSamplerBase sampler =HTTPSamplerFactory.newInstance(); sampler.setDomain(url.getHost()); sampler.setProtocol(url.getProtocol()); sampler.setPort(url.getPort()); sampler.setPath(url.getPath()); sampler.parseArguments(url.getQuery()); return sampler; }
sampler.setHeaderManager(headers); sampler.threadStarted(); // Needed for HTTPSampler2 if (isDebug) { log.debug("{} Execute sample: {} and url {}",port, sampler.getMethod(), sampler.getUrl()); result = sampler.sample(); log.debug("{} Will deliver sample {}", port, sampler.getName()); sampler.threadFinished(); // Needed for HTTPSampler2
HTTPFileArg[] files = sampler.getHTTPFiles(); String contentEncoding = sampler.getContentEncoding(); if(contentEncoding == null || contentEncoding.length() == 0) { contentEncoding = ENCODING; if(sampler.getUseMultipart()) { OutputStream out = connection.getOutputStream(); if(sampler.getArguments() != null && !sampler.hasArguments() && sampler.getSendFileAsPostBody()) { OutputStream out = connection.getOutputStream();
/** * Perform a sample, and return the results * * @return results of the sampling */ public SampleResult sample() { SampleResult res = null; try { res = sample(getUrl(), getMethod(), false, 0); if (res != null) { res.setSampleLabel(getName()); } return res; } catch (Exception e) { return errorResult(e, new HTTPSampleResult()); } }
private void modify(HTTPSamplerBase sampler, String value) { if (isPathExtension()) { String oldPath = sampler.getPath(); int indexOfSessionId = oldPath.indexOf(SEMI_COLON + getArgumentName()); if(oldPath.indexOf(SEMI_COLON + getArgumentName())>=0) { int indexOfQuestionMark = oldPath.indexOf('?'); if(indexOfQuestionMark < 0) { oldPath = oldPath.substring(0, indexOfSessionId); } else { oldPath = oldPath.substring(0, indexOfSessionId)+ oldPath.substring(indexOfQuestionMark); } } if (isPathExtensionNoEquals()) { sampler.setPath(oldPath + SEMI_COLON + getArgumentName() + value); // $NON-NLS-1$ } else { sampler.setPath(oldPath + SEMI_COLON + getArgumentName() + "=" + value); // $NON-NLS-1$ // $NON-NLS-2$ } } else { sampler.getArguments().removeArgument(getArgumentName()); sampler.getArguments().addArgument(new HTTPArgument(getArgumentName(), value, !encode())); } }
@Override public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws IOException { String contentEncoding = sampler.getContentEncoding(); if(contentEncoding == null || contentEncoding.length() == 0) { contentEncoding = ENCODING; boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.length() > 0; HTTPFileArg[] files = sampler.getHTTPFiles(); if(sampler.getArguments() != null && sampler.getArguments().getArgumentCount() == 0 && sampler.getSendFileAsPostBody()) { contentLength = inputFile.length(); else if(sampler.getSendParameterValuesAsPostBody()) { hasPutBody = true; for (JMeterProperty jMeterProperty : sampler.getArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); putBodyBuffer.append(arg.getEncodedValue(contentEncoding));
/** * Set path on sampler * @param sampler {@link HTTPSamplerBase} * @param request {@link HttpRequestHdr} */ protected void computePath(HTTPSamplerBase sampler, HttpRequestHdr request) { if(sampler.getContentEncoding() != null) { sampler.setPath(request.getPath(), sampler.getContentEncoding()); } else { // Although the spec says UTF-8 should be used for encoding URL parameters, // most browser use ISO-8859-1 for default if encoding is not known. // We use null for contentEncoding, then the url parameters will be added // with the value in the URL, and the "encode?" flag set to false sampler.setPath(request.getPath(), null); } if (log.isDebugEnabled()) { log.debug("Proxy: setting path: " + sampler.getPath()); } }
/** * * @see org.apache.jmeter.protocol.http.proxy.SamplerCreator#createSampler(org.apache.jmeter.protocol.http.proxy.HttpRequestHdr, * java.util.Map, java.util.Map) */ @Override public HTTPSamplerBase createSampler(HttpRequestHdr request, Map<String, String> pageEncodings, Map<String, String> formEncodings) { // Instantiate the sampler HTTPSamplerBase sampler = HTTPSamplerFactory.newInstance(request.getHttpSamplerName()); sampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName()); // Defaults sampler.setFollowRedirects(false); sampler.setUseKeepAlive(true); if (log.isDebugEnabled()) { log.debug("getSampler: sampler path = " + sampler.getPath()); } return sampler; }
/** * @see org.apache.jmeter.protocol.http.proxy.SamplerCreator#populateSampler(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase, * org.apache.jmeter.protocol.http.proxy.HttpRequestHdr, java.util.Map, * java.util.Map) */ @Override public final void populateSampler(HTTPSamplerBase sampler, HttpRequestHdr request, Map<String, String> pageEncodings, Map<String, String> formEncodings) throws Exception{ computeFromHeader(sampler, request, pageEncodings, formEncodings); computeFromPostBody(sampler, request); if (log.isDebugEnabled()) { log.debug("sampler path = " + sampler.getPath()); } Arguments arguments = sampler.getArguments(); if(arguments.getArgumentCount() == 1 && arguments.getArgument(0).getName().length()==0) { sampler.setPostBodyRaw(true); } }