[Httpunit-commit] CVS: httpunit/src/com/meterware/pseudoserver HttpRequest.java,1.1,1.2 PseudoServer
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-03-19 03:45:17
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/pseudoserver
In directory sc8-pr-cvs1:/tmp/cvs-serv8325/src/com/meterware/pseudoserver
Modified Files:
HttpRequest.java PseudoServer.java WebResource.java
Log Message:
Continuing work towards persistent connection support
Index: HttpRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/pseudoserver/HttpRequest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HttpRequest.java 18 Mar 2003 19:35:33 -0000 1.1
+++ HttpRequest.java 19 Mar 2003 03:38:06 -0000 1.2
@@ -191,13 +191,14 @@
}
- boolean requestedCloseConnection() {
+ boolean wantsKeepAlive() {
+ return false;
// if ("Keep-alive".equalsIgnoreCase( getConnectionHeader() )) {
-// return false;
+// return true;
// } else if (_protocol.equals( "HTTP/1.1" )) {
-// return "Close".equalsIgnoreCase( getConnectionHeader() );
+// return !"Close".equalsIgnoreCase( getConnectionHeader() );
// } else {
- return true;
+// return false;
// }
}
Index: PseudoServer.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/pseudoserver/PseudoServer.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- PseudoServer.java 18 Mar 2003 19:35:33 -0000 1.6
+++ PseudoServer.java 19 Mar 2003 03:38:06 -0000 1.7
@@ -4,12 +4,12 @@
*
* Copyright (c) 2000-2003, Russell Gold
*
-* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
-* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
-* The above copyright notice and this permission notice shall be included in all copies or substantial portions
+* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
@@ -213,23 +213,29 @@
final BufferedInputStream inputStream = new BufferedInputStream( socket.getInputStream() );
final HttpResponseStream outputStream = new HttpResponseStream( socket.getOutputStream() );
- HttpRequest request;
- do {
- request = new HttpRequest( inputStream );
- respondToRequest( request, outputStream );
- } while (!request.requestedCloseConnection() );
+ while (_active) {
+ HttpRequest request = new HttpRequest( inputStream );
+ boolean keepAlive = respondToRequest( request, outputStream );
+ if (!keepAlive) break;
+ while (_active && 0 == inputStream.available()) {
+ try { Thread.sleep(10); } catch (InterruptedException e) {}
+ }
+ }
outputStream.close();
socket.close();
}
- private void respondToRequest( HttpRequest request, HttpResponseStream response ) {
+ private boolean respondToRequest( HttpRequest request, HttpResponseStream response ) {
+ boolean keepAlive = request.wantsKeepAlive();
try {
+ response.restart();
response.setProtocol( request.getProtocol() );
WebResource resource = getResource( request );
if (resource == null) {
response.setResponse( HttpURLConnection.HTTP_NOT_FOUND, "unable to find " + request.getURI() );
} else {
+ if (resource.closesConnection()) keepAlive = false;
if (resource.getResponseCode() != HttpURLConnection.HTTP_OK) {
response.setResponse( resource.getResponseCode(), "" );
}
@@ -245,6 +251,7 @@
t.printStackTrace();
response.setResponse( HttpURLConnection.HTTP_INTERNAL_ERROR, t.toString() );
}
+ return keepAlive;
}
@@ -313,6 +320,11 @@
final private static String CRLF = "\r\n";
+ void restart() {
+ _headersWritten = false;
+ _headers.clear();
+ }
+
void close() throws IOException {
flushHeaders();
@@ -356,6 +368,7 @@
void write( WebResource resource ) throws IOException {
flushHeaders();
resource.writeTo( _stream );
+ _stream.flush();
}
@@ -371,7 +384,6 @@
for (Enumeration e = _headers.elements(); e.hasMoreElements();) {
sendLine( (String) e.nextElement() );
}
- sendLine( "Connection: close" ); // TODO get rid of this
sendText( CRLF );
_headersWritten = true;
_pw.flush();
Index: WebResource.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/pseudoserver/WebResource.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- WebResource.java 18 Mar 2003 19:35:33 -0000 1.4
+++ WebResource.java 19 Mar 2003 03:38:06 -0000 1.5
@@ -4,12 +4,12 @@
*
* Copyright (c) 2000-2002, Russell Gold
*
-* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
-* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
-* The above copyright notice and this permission notice shall be included in all copies or substantial portions
+* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
@@ -38,6 +38,7 @@
final static String DEFAULT_CONTENT_TYPE = "text/html";
final static String DEFAULT_CHARACTER_SET = "iso-8859-1";
+ private boolean _closesConnection;
public WebResource( String contents ) {
@@ -59,6 +60,8 @@
_headers.addElement( header );
if (header.toLowerCase().startsWith( "content-type" )) _hasExplicitContentTypeHeader = true;
if (header.toLowerCase().startsWith( "content-length" )) _hasExplicitContentLengthHeader = true;
+ if (header.trim().toLowerCase().startsWith( "connection" ) &&
+ header.trim().toLowerCase().endsWith( "close") ) _closesConnection = true;
}
@@ -108,6 +111,11 @@
}
+ boolean closesConnection() {
+ return _closesConnection;
+ }
+
+
void writeTo( OutputStream outputStream ) throws IOException {
if (_stream == null) {
outputStream.write( getContentsAsBytes() );
@@ -163,7 +171,7 @@
public String toString() {
- return "WebResource [code=" + _responseCode + "; type = " + _contentType
+ return "WebResource [code=" + _responseCode + "; type = " + _contentType
+ "; charset = " + _characterSet + "]\n" + getContentsAsString();
}
|