You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2004/09/14 17:50:41 UTC
cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server RequestLine.java SimpleHttpServerConnection.java SimpleResponse.java
olegk 2004/09/14 08:50:41
Modified: httpclient/src/test/org/apache/commons/httpclient
TestEffectiveHttpVersion.java TestMethodAbort.java
httpclient/src/test/org/apache/commons/httpclient/server
RequestLine.java SimpleHttpServerConnection.java
SimpleResponse.java
Log:
Enhancements to the SimpleHttpServer testing framework
Contributed by Oleg Kalnichevski
Revision Changes Path
1.2 +6 -6 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestEffectiveHttpVersion.java
Index: TestEffectiveHttpVersion.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestEffectiveHttpVersion.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestEffectiveHttpVersion.java 9 May 2004 12:16:12 -0000 1.1
+++ TestEffectiveHttpVersion.java 14 Sep 2004 15:50:40 -0000 1.2
@@ -75,8 +75,8 @@
public boolean process(final SimpleRequest request, final SimpleResponse response)
throws IOException
{
- String protocol = request.getRequestLine().getProtocol();
- response.setStatusLine(protocol + " 200 OK");
+ HttpVersion httpversion = request.getRequestLine().getHttpVersion();
+ response.setStatusLine(httpversion, HttpStatus.SC_OK);
response.setBodyString(request.getBodyString());
return true;
}
1.2 +6 -6 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodAbort.java
Index: TestMethodAbort.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodAbort.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestMethodAbort.java 11 May 2004 20:43:55 -0000 1.1
+++ TestMethodAbort.java 14 Sep 2004 15:50:40 -0000 1.2
@@ -82,9 +82,9 @@
final String garbage = "garbage!\r\n";
final long count = 1000000000;
- String protocol = request.getRequestLine().getProtocol();
+ HttpVersion httpversion = request.getRequestLine().getHttpVersion();
ResponseWriter out = conn.getWriter();
- out.println(protocol + " 200 OK");
+ out.println(httpversion + " 200 OK");
out.println("Content-Type: text/plain");
out.println("Content-Length: " + count * garbage.length()) ;
out.println("Connection: close");
1.4 +42 -24 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/RequestLine.java
Index: RequestLine.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/RequestLine.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- RequestLine.java 22 Feb 2004 18:08:52 -0000 1.3
+++ RequestLine.java 14 Sep 2004 15:50:41 -0000 1.4
@@ -34,16 +34,25 @@
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpVersion;
+import org.apache.commons.httpclient.ProtocolException;
+
/**
* Defines a HTTP request-line, consisting of method name, URI and protocol.
* Instances of this class are immutable.
*
* @author Christian Kohlschuetter
+ * @author Oleg Kalnichevski
*/
public class RequestLine {
- private String method, uri, protocol;
- public static RequestLine parseLine(String l) {
+ private HttpVersion httpversion = null;
+ private String method = null;
+ private String uri= null;
+
+ public static RequestLine parseLine(final String l)
+ throws HttpException {
String method = null;
String uri = null;
String protocol = null;
@@ -53,42 +62,51 @@
uri = st.nextToken();
protocol = st.nextToken();
} catch (NoSuchElementException e) {
+ throw new ProtocolException("Invalid request line: " + l);
}
-
return new RequestLine(method, uri, protocol);
}
- public RequestLine(String method, String uri, String protocol) {
- this.method = method;
+ public RequestLine(final String method, final String uri, final HttpVersion httpversion) {
+ super();
+ if (method == null) {
+ throw new IllegalArgumentException("Method may not be null");
+ }
+ if (uri == null) {
+ throw new IllegalArgumentException("URI may not be null");
+ }
+ if (httpversion == null) {
+ throw new IllegalArgumentException("HTTP version may not be null");
+ }
+ this.method = method;
this.uri = uri;
- this.protocol = protocol;
+ this.httpversion = httpversion;
+ }
+
+ public RequestLine(final String method, final String uri, final String httpversion)
+ throws ProtocolException {
+ this(method, uri, HttpVersion.parse(httpversion));
}
public String getMethod() {
- return method;
+ return this.method;
}
- public String getProtocol() {
- return protocol;
+ public HttpVersion getHttpVersion() {
+ return this.httpversion;
}
public String getUri() {
- return uri;
+ return this.uri;
}
public String toString() {
StringBuffer sb = new StringBuffer();
- if(method != null) {
- sb.append(method);
- if(uri != null) {
- sb.append(" ");
- sb.append(uri);
- if(protocol != null) {
- sb.append(" ");
- sb.append(protocol);
- }
- }
- }
+ sb.append(this.method);
+ sb.append(" ");
+ sb.append(this.uri);
+ sb.append(" ");
+ sb.append(this.httpversion);
return sb.toString();
}
}
1.10 +9 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java
Index: SimpleHttpServerConnection.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- SimpleHttpServerConnection.java 13 Jun 2004 20:22:20 -0000 1.9
+++ SimpleHttpServerConnection.java 14 Sep 2004 15:50:41 -0000 1.10
@@ -37,6 +37,7 @@
import java.io.UnsupportedEncodingException;
import java.net.Socket;
+import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpParser;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
@@ -155,11 +156,15 @@
RequestLine.parseLine(line),
HttpParser.parseHeaders(in, HTTP_ELEMENT_CHARSET),
null);
- } catch (IOException e) {
+ } catch (HttpException e) {
connectionClose();
SimpleResponse response = ErrorResponse.getInstance().
getResponse(HttpStatus.SC_BAD_REQUEST);
server.writeResponse(this, response);
+ return;
+ } catch (IOException e) {
+ connectionClose();
+ LOG.error("I/O error processing request", e);
return;
}
server.processRequest(this, request);
1.2 +22 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleResponse.java
Index: SimpleResponse.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleResponse.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SimpleResponse.java 27 Feb 2004 19:04:32 -0000 1.1
+++ SimpleResponse.java 14 Sep 2004 15:50:41 -0000 1.2
@@ -35,6 +35,8 @@
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HeaderGroup;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.HttpVersion;
/**
* A generic HTTP response.
@@ -78,8 +80,24 @@
return this.statusLine;
}
- public void setStatusLine(String string) {
+ public void setStatusLine(final String string) {
this.statusLine = string;
+ }
+
+ public void setStatusLine(final HttpVersion version, int statuscode) {
+ if (version == null) {
+ throw new IllegalArgumentException("HTTP version may not be null");
+ }
+ StringBuffer buffer = new StringBuffer();
+ buffer.append(version);
+ buffer.append(' ');
+ buffer.append(statuscode);
+ String statustext = HttpStatus.getStatusText(statuscode);
+ if (statustext != null) {
+ buffer.append(' ');
+ buffer.append(statustext);
+ }
+ this.statusLine = buffer.toString();
}
public boolean containsHeader(final String name) {
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org