⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httprequesthdr.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    private boolean isMultipart(String contentType) {
        if (contentType != null && contentType.startsWith(HTTPConstants.MULTIPART_FORM_DATA)) {
            return true;
        } else {
            return false;
        }
    }

    private MultipartUrlConfig getMultipartConfig(String contentType) {
        if(isMultipart(contentType)) {
            // Get the boundary string for the multiparts from the content type
            String boundaryString = contentType.substring(contentType.toLowerCase().indexOf("boundary=") + "boundary=".length());
            return new MultipartUrlConfig(boundaryString);
        }
        else {
            return null;
        }
    }

    private void populateSampler(Map pageEncodings, Map formEncodings) 
            throws MalformedURLException, UnsupportedEncodingException {        
        sampler.setDomain(serverName());
        if (log.isDebugEnabled())
    		log.debug("Proxy: setting server: " + sampler.getDomain());
		sampler.setMethod(method);
		log.debug("Proxy: setting method: " + sampler.getMethod());
		sampler.setPort(serverPort());
        if (log.isDebugEnabled())
            log.debug("Proxy: setting port: " + sampler.getPort());
		if (url.indexOf("//") > -1) {
			String protocol = url.substring(0, url.indexOf(":"));
            if (log.isDebugEnabled())
    			log.debug("Proxy: setting protocol to : " + protocol);
			sampler.setProtocol(protocol);
		} else if (sampler.getPort() == HTTPConstants.DEFAULT_HTTPS_PORT) {
			sampler.setProtocol(HTTPS);
            if (log.isDebugEnabled())
    			log.debug("Proxy: setting protocol to https");
		} else {
            if (log.isDebugEnabled())
    			log.debug("Proxy setting default protocol to: http");
			sampler.setProtocol(HTTP);
		}
        
        URL pageUrl = null;
        if(sampler.isProtocolDefaultPort()) {            
            pageUrl = new URL(sampler.getProtocol(), sampler.getDomain(), getPath());
        }
        else {
            pageUrl = new URL(sampler.getProtocol(), sampler.getDomain(), sampler.getPort(), getPath());
        }
        String urlWithoutQuery = getUrlWithoutQuery(pageUrl);
        

        // Check if the request itself tells us what the encoding is
        String contentEncoding = null;
        String requestContentEncoding = getContentEncoding();
        if(requestContentEncoding != null) {
            contentEncoding = requestContentEncoding;
        }
        else {        
            // Check if we know the encoding of the page
            if (pageEncodings != null) {
                synchronized (pageEncodings) {
                    contentEncoding = (String) pageEncodings.get(urlWithoutQuery);
                }
            }
            // Check if we know the encoding of the form
            if (formEncodings != null) {
                synchronized (formEncodings) {
                    String formEncoding = (String) formEncodings.get(urlWithoutQuery);
                    // Form encoding has priority over page encoding
                    if (formEncoding != null) {
                        contentEncoding = formEncoding;
                    }
                }
            }
        }

        // Get the post data using the content encoding of the request
        String postData = null;
        if (log.isDebugEnabled()) {
            if(contentEncoding != null) {
                log.debug("Using encoding " + contentEncoding + " for request body");
            }
            else {
                log.debug("No encoding found, using JRE default encoding for request body");
            }
        }
        if (contentEncoding != null) {
            postData = new String(rawPostData, contentEncoding);
        } else {
            // Use default encoding
            postData = new String(rawPostData);
        }

        if(contentEncoding != null) {
            sampler.setPath(getPath(), contentEncoding);
        }
        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(getPath(), null);
        }
        if (log.isDebugEnabled())
            log.debug("Proxy: setting path: " + sampler.getPath());
        if (numberRequests) {
            requestNumber++;
            sampler.setName(requestNumber + " " + sampler.getPath());
        } else {
            sampler.setName(sampler.getPath());
        }
        
        // Set the content encoding
        if(contentEncoding != null) {
            sampler.setContentEncoding(contentEncoding);
        }
        
        // If it was a HTTP GET request, then all parameters in the URL
        // has been handled by the sampler.setPath above, so we just need
        // to do parse the rest of the request if it is not a GET request
        if(!HTTPConstants.GET.equals(method)) {
            // Check if it was a multipart http post request
            final String contentType = getContentType();
            MultipartUrlConfig urlConfig = getMultipartConfig(contentType);
            if (urlConfig != null) {
                urlConfig.parseArguments(postData);
                // Tell the sampler to do a multipart post
                sampler.setDoMultipartPost(true);
                // Remove the header for content-type and content-length, since
                // those values will most likely be incorrect when the sampler
                // performs the multipart request, because the boundary string
                // will change
                getHeaderManager().removeHeaderNamed(CONTENT_TYPE);
                getHeaderManager().removeHeaderNamed(CONTENT_LENGTH);
            
                // Set the form data
                sampler.setArguments(urlConfig.getArguments());
                // Set the file uploads
                sampler.setFileField(urlConfig.getFileFieldName());
                sampler.setFilename(urlConfig.getFilename());
                sampler.setMimetype(urlConfig.getMimeType());
            } else if (postData != null && postData.trim().startsWith("<?")) {
                // Not sure if this is needed anymore. I assume these requests
                // do not have HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED as content type,
                // and they would therefore be catched by the last else if of these if else if tests
                sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
            } else if (contentType == null || contentType.startsWith(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED) ){
                // It is the most common post request, with parameter name and values
                // We also assume this if no content type is present, to be most backwards compatible,
                // but maybe we should only parse arguments if the content type is as expected
                sampler.parseArguments(postData.trim(), contentEncoding); //standard name=value postData
            } else if (postData != null && postData.length() > 0) {
                // Just put the whole postbody as the value of a parameter
                sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
            }
        }
        if (log.isDebugEnabled())
    		log.debug("sampler path = " + sampler.getPath());
	}

	//
	// Parsing Methods
	//

	/**
	 * Find the //server.name from an url.
	 * 
	 * @return server's internet name
	 */
	private String serverName() {
		// chop to "server.name:x/thing"
		String str = url;
		int i = str.indexOf("//"); // $NON-NLS-1$
		if (i > 0) {
			str = str.substring(i + 2);
		}
		// chop to server.name:xx
		i = str.indexOf("/"); // $NON-NLS-1$
		if (0 < i) {
			str = str.substring(0, i);
		}
		// chop to server.name
		i = str.indexOf(":"); // $NON-NLS-1$
		if (0 < i) {
			str = str.substring(0, i);
		}
		return str;
	}

	// TODO replace repeated substr() above and below with more efficient method.
	
	/**
	 * Find the :PORT from http://server.ect:PORT/some/file.xxx
	 * 
	 * @return server's port (or UNSPECIFIED if not found)
	 */
	private int serverPort() {
		String str = url;
		// chop to "server.name:x/thing"
		int i = str.indexOf("//");
		if (i > 0) {
			str = str.substring(i + 2);
		}
		// chop to server.name:xx
		i = str.indexOf("/");
		if (0 < i) {
			str = str.substring(0, i);
		}
		// chop XX
		i = str.indexOf(":");
		if (0 < i) {
			return Integer.parseInt(str.substring(i + 1).trim());
		}
		return HTTPSamplerBase.UNSPECIFIED_PORT;
	}

	/**
	 * Find the /some/file.xxxx from http://server.ect:PORT/some/file.xxx
	 * 
	 * @return the path
	 */
	private String getPath() {
		String str = url;
		int i = str.indexOf("//");
		if (i > 0) {
			str = str.substring(i + 2);
		}
		i = str.indexOf("/");
		if (i < 0) {
			return "";
		}
		return str.substring(i);
	}

	/**
	 * Returns the url string extracted from the first line of the client request.
	 * 
	 * @return the url
	 */
	public String getUrl(){
		return url;
	}
	
	/**
	 * Returns the next token in a string.
	 * 
	 * @param tk
	 *            String that is partially tokenized.
	 * @return The remainder
	 */
	private String getToken(StringTokenizer tk) {
		if (tk.hasMoreTokens()) {
			return tk.nextToken();
		}
		return "";// $NON-NLS-1$
	}

	/**
	 * Returns the remainder of a tokenized string.
	 * 
	 * @param tk
	 *            String that is partially tokenized.
	 * @return The remainder
	 */
	private String getRemainder(StringTokenizer tk) {
		StringBuffer strBuff = new StringBuffer();
		if (tk.hasMoreTokens()) {
			strBuff.append(tk.nextToken());
		}
		while (tk.hasMoreTokens()) {
            strBuff.append(" "); // $NON-NLS-1$
            strBuff.append(tk.nextToken());
		}
		return strBuff.toString();
	}

    private String getUrlWithoutQuery(URL _url) {
        String fullUrl = _url.toString();
        String urlWithoutQuery = fullUrl;
        String query = _url.getQuery();
        if(query != null) {
            // Get rid of the query and the ?
            urlWithoutQuery = urlWithoutQuery.substring(0, urlWithoutQuery.length() - query.length() - 1);
        }
        return urlWithoutQuery;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -