Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exception when webserver returns "500 Internal Server Error" and content-lenght is 0 #1859

Closed
asfimport opened this issue Feb 5, 2007 · 4 comments

Comments

@asfimport
Copy link
Collaborator

Christoph (Bug 41543):
If a server returns a http-response 500 Internal Server Error and the content-
lenght is 0 then JMeter throws a Java Exception instead of returning the error
code 500. This results in 'non HTTP response' messages to be shown.

Severity: normal
OS: Windows XP

@asfimport
Copy link
Collaborator Author

Christoph (migrated from Bugzilla):
We generated a JMeter patch, which results in the expected behaviour.

Created attachment jakarta-jmeter-2.2_HTTPSampler.java.patch: Patch for this bug

jakarta-jmeter-2.2_HTTPSampler.java.patch
--- jmeter-testbed-2.2/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java	2007-01-11 11:13:35.000000000 +0100
+++ jakarta-jmeter-2.2/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java	2006-11-14 20:25:55.000000000 +0100
@@ -55,7 +55,7 @@
     private static final Logger log = LoggingManager.getLoggerForClass();
 
 	private static final int MAX_CONN_RETRIES = 10; // Maximum connection retries
-
+	
 	static {// TODO - document what this is doing and why
 		System.setProperty("java.protocol.handler.pkgs", // $NON-NLS-1$ 
                 JMeterUtils.getPropDefault("ssl.pkgs", // $NON-NLS-1$
@@ -208,6 +208,13 @@
 	protected byte[] readResponse(HttpURLConnection conn, SampleResult res) throws IOException {
 		byte[] readBuffer = getThreadContext().getReadBuffer();
 		BufferedInputStream in;
+		
+		if ((conn.getContentLength() == 0) && JMeterUtils.getPropDefault("httpsampler.obey_contentlength", false)) {
+			log.info("Content-Length: 0, not reading http-body");
+			res.setResponseHeaders(getResponseHeaders(conn));
+			return new byte[0];
+		}
+
 		try {
             // works OK even if ContentEncoding is null
 			if (ENCODING_GZIP.equals(conn.getContentEncoding())) {
@@ -218,17 +225,19 @@
 		} catch (IOException e) {
 			// TODO JDK1.4: if (!e.getCause() instanceof FileNotFoundException)
 			// JDK1.4: {
-            // TODO: what about other 4xx errors? Do we need to log them?
-			if (conn.getResponseCode() != 404) // for JDK1.3
-			{
-				log.error("readResponse: "+e.toString());
-				// JDK1.4: Throwable cause = e.getCause();
-				// JDK1.4: if (cause != null){
-				// JDK1.4: log.error("Cause: "+cause);
-				// JDK1.4: }
-			}
+
 			// Normal InputStream is not available
-			in = new BufferedInputStream(conn.getErrorStream());
+			InputStream errorStream = conn.getErrorStream();
+			if (errorStream == null) {
+				log.info("Error Response Code: "+conn.getResponseCode()+", Server sent no Errorpage");
+				res.setResponseHeaders(getResponseHeaders(conn));
+				return new byte[0];
+			}
+			else {
+				log.info("Error Response Code: "+conn.getResponseCode());
+			}
+			in = new BufferedInputStream(errorStream);
+			
 		} catch (Exception e) {
 			log.error("readResponse: "+e.toString());
 			// JDK1.4: Throwable cause = e.getCause();
@@ -237,6 +246,7 @@
 			// JDK1.4: }
 			in = new BufferedInputStream(conn.getErrorStream());
 		}
+		
 		java.io.ByteArrayOutputStream w = new ByteArrayOutputStream();
 		int x = 0;
 		boolean first = true;
@@ -269,7 +279,8 @@
 		// headerBuf.append(" ");
 		// headerBuf.append(conn.getResponseMessage());
 		headerBuf.append("\n"); //$NON-NLS-1$
-
+		
+		
         String hfk;
 		for (int i = 1; (hfk=conn.getHeaderFieldKey(i)) != null; i++) {
             // TODO - why is this not saved? A: it might be a proxy server specific field.
@@ -280,7 +291,8 @@
                 headerBuf.append(conn.getHeaderField(i));
                 headerBuf.append("\n"); // $NON-NLS-1$
             }
-		}
+		} 
+		
 		return headerBuf.toString();
 	}
 
@@ -431,16 +443,17 @@
             }
 			// Request sent. Now get the response:
 			byte[] responseData = readResponse(conn, res);
-
+				
 			res.sampleEnd();
 			// Done with the sampling proper.
 
 			// Now collect the results into the HTTPSampleResult:
-
 			res.setResponseData(responseData);
-
+			
+			
 			int errorLevel = conn.getResponseCode();
             String respMsg = conn.getResponseMessage();
+            
             if (errorLevel == -1){// Bug 38902 - sometimes -1 seems to be returned unnecessarily
                 try {
                     errorLevel = Integer.parseInt(respMsg.substring(0, 3));
@@ -451,14 +464,16 @@
             }
 			res.setResponseCode(Integer.toString(errorLevel));
 			res.setSuccessful(isSuccessCode(errorLevel));
-
+			
 			res.setResponseMessage(respMsg);
-
+			
 			String ct = conn.getContentType();
 			res.setContentType(ct);// e.g. text/html; charset=ISO-8859-1
             res.setEncodingAndType(ct);
 
 			res.setResponseHeaders(getResponseHeaders(conn));
+			
+					
 			if (res.isRedirect()) {
 				res.setRedirectLocation(conn.getHeaderField(HEADER_LOCATION));
 			}
@@ -476,9 +491,11 @@
 			log.debug("End : sample");
 			return res;
 		} catch (IOException e) {
+			
 			res.sampleEnd();
+			
 			// We don't want to continue using this connection, even if KeepAlive is set
-            conn.disconnect();
+			conn.disconnect();
             conn=null; // Don't process again
 			return errorResult(e, res);
 		} finally {

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
Thanks for the patch; I've applied it.

Does the same problem occur with the HttpClient sampler?

@asfimport
Copy link
Collaborator Author

Christoph (migrated from Bugzilla):
(In reply to comment 2)

Thanks for the patch; I've applied it.
Does the same problem occur with the HttpClient sampler?

i'm sorry but i don't really know if the problem also occurs with the
httpclient... we just use the java httpsampler and we needed to patch this bug

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
OK - no problem.

If you do end up using HttpClient instead and it shows the same problem, please
update the bug (or open a new one) - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant