📄 proxy.java
字号:
writeErrorToClient(HttpReplyHdr.formServerNotFound());
result = generateErrorResult(result, uhe); // Generate result (if nec.) and populate it
} catch (IllegalArgumentException e) {
log.error("Not implemented (probably used https)", e);
writeErrorToClient(HttpReplyHdr.formNotImplemented());
result = generateErrorResult(result, e); // Generate result (if nec.) and populate it
} catch (Exception e) {
log.error("Exception when processing sample", e);
writeErrorToClient(HttpReplyHdr.formTimeout());
result = generateErrorResult(result, e); // Generate result (if nec.) and populate it
} finally {
if (log.isDebugEnabled()) {
log.debug("Will deliver sample " + sampler.getName());
}
/*
* We don't want to store any cookies in the generated test plan
*/
if (headers != null) {
headers.removeHeaderNamed("cookie");// Always remove cookies // $NON-NLS-1$
headers.removeHeaderNamed("Authorization");// Always remove authorization // $NON-NLS-1$
// Remove additional headers
for(int i=0; i < headersToRemove.length; i++){
headers.removeHeaderNamed(headersToRemove[i]);
}
}
target.deliverSampler(sampler, new TestElement[] { captureHttpHeaders ? headers : null }, result);
try {
clientSocket.close();
} catch (Exception e) {
log.error("", e);
}
sampler.threadFinished(); // Needed for HTTPSampler2
}
}
private SampleResult generateErrorResult(SampleResult result, Exception e) {
if (result == null) {
result = new SampleResult();
result.setSampleLabel("Sample failed");
}
result.setResponseMessage(e.getMessage());
return result;
}
/**
* Write output to the output stream, then flush and close the stream.
*
* @param inBytes
* the bytes to write
* @param out
* the output stream to write to
* @throws IOException
* if an IOException occurs while writing
*/
private void writeToClient(SampleResult res, OutputStream out) throws IOException {
try {
String responseHeaders = massageResponseHeaders(res);
out.write(responseHeaders.getBytes());
out.write('\n'); // $NON-NLS-1$
out.write(res.getResponseData());
out.flush();
log.debug("Done writing to client");
} catch (IOException e) {
log.error("", e);
throw e;
} finally {
try {
out.close();
} catch (Exception ex) {
log.warn("Error while closing socket", ex);
}
}
}
/**
* In the event the content was gzipped and unpacked, the content-encoding
* header must be removed and the content-length header should be corrected.
*
* The Transfer-Encoding header is also removed.
*
* @param res - response
*
* @return updated headers to be sent to client
*/
private String massageResponseHeaders(SampleResult res) {
String headers = res.getResponseHeaders();
String [] headerLines=headers.split(NEW_LINE, 0); // drop empty trailing content
int contentLengthIndex=-1;
boolean fixContentLength = false;
for (int i=0;i<headerLines.length;i++){
String line=headerLines[i];
String[] parts=line.split(":\\s+",2); // $NON-NLS-1$
if (parts.length==2){
if (HTTPConstants.TRANSFER_ENCODING.equalsIgnoreCase(parts[0])){
headerLines[i]=null; // We don't want this passed on to browser
continue;
}
if (HTTPConstants.HEADER_CONTENT_ENCODING.equalsIgnoreCase(parts[0])
&&
HTTPConstants.ENCODING_GZIP.equalsIgnoreCase(parts[1])
){
headerLines[i]=null; // We don't want this passed on to browser
fixContentLength = true;
continue;
}
if (HTTPConstants.HEADER_CONTENT_LENGTH.equalsIgnoreCase(parts[0])){
contentLengthIndex=i;
continue;
}
}
}
if (fixContentLength && contentLengthIndex>=0){// Fix the content length
headerLines[contentLengthIndex]=HTTPConstants.HEADER_CONTENT_LENGTH+": "+res.getResponseData().length;
}
StringBuffer sb = new StringBuffer(headers.length());
for (int i=0;i<headerLines.length;i++){
String line=headerLines[i];
if (line != null){
sb.append(line).append(NEW_LINE);
}
}
return sb.toString();
}
/**
* Write an error message to the client. The message should be the full HTTP
* response.
*
* @param message
* the message to write
*/
private void writeErrorToClient(String message) {
try {
OutputStream sockOut = clientSocket.getOutputStream();
DataOutputStream out = new DataOutputStream(sockOut);
out.writeBytes(message);
out.flush();
} catch (Exception e) {
log.warn("Exception while writing error", e);
}
}
/**
* Add the page encoding of the sample result to the Map with page encodings
*
* @param result the sample result to check
* @return the page encoding found for the sample result, or null
*/
private String addPageEncoding(SampleResult result) {
String pageEncoding = getContentEncoding(result);
if(pageEncoding != null) {
String urlWithoutQuery = getUrlWithoutQuery(result.getURL());
synchronized(pageEncodings) {
pageEncodings.put(urlWithoutQuery, pageEncoding);
}
}
return pageEncoding;
}
/**
* Add the form encodings for all forms in the sample result
*
* @param result the sample result to check
* @param pageEncoding the encoding used for the sample result page
*/
private void addFormEncodings(SampleResult result, String pageEncoding) {
FormCharSetFinder finder = new FormCharSetFinder();
if (!result.getContentType().startsWith("text/")){ // TODO perhaps make more specific than this?
return; // no point parsing anything else, e.g. GIF ...
}
try {
finder.addFormActionsAndCharSet(result.getResponseDataAsString(), formEncodings, pageEncoding);
}
catch (HTMLParseException parseException) {
log.debug("Unable to parse response, could not find any form character set encodings");
}
}
/**
* Get the value of the charset of the content-type header of the sample result
*
* @param res the sample result to find the charset for
* @return the charset found, or null
*/
private String getContentEncoding(SampleResult res) {
String contentTypeHeader = res.getContentType();
String charSet = null;
if (contentTypeHeader != null) {
int charSetStartPos = contentTypeHeader.toLowerCase().indexOf("charset=");
if (charSetStartPos > 0) {
charSet = contentTypeHeader.substring(charSetStartPos + "charset=".length());
if (charSet != null) {
if (charSet.trim().length() > 0) {
charSet = charSet.trim();
} else {
charSet = null;
}
}
}
}
return charSet;
}
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 + -