You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/08/04 14:30:11 UTC
svn commit: r562707 - in /jakarta/httpcomponents/httpcore/trunk:
module-main/src/main/java/org/apache/http/impl/
module-main/src/main/java/org/apache/http/impl/io/
module-nio/src/main/java/org/apache/http/impl/nio/
Author: olegk
Date: Sat Aug 4 05:30:10 2007
New Revision: 562707
URL: http://svn.apache.org/viewvc?view=rev&rev=562707
Log:
HTTPCORE-106: Factored out HTTP message parsing and writing code from blocking connection impls into separate classes making it possible to plug in an alternative implementation of those classes
Added:
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java (with props)
Modified:
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpServerConnection.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java
jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java?view=diff&rev=562707&r1=562706&r2=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java Sat Aug 4 05:30:10 2007
@@ -32,9 +32,7 @@
package org.apache.http.impl;
import java.io.IOException;
-import java.util.Iterator;
-import org.apache.http.Header;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpConnectionMetrics;
import org.apache.http.HttpEntity;
@@ -43,24 +41,17 @@
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseFactory;
-import org.apache.http.NoHttpResponseException;
-import org.apache.http.ProtocolException;
-import org.apache.http.StatusLine;
import org.apache.http.impl.entity.EntityDeserializer;
import org.apache.http.impl.entity.EntitySerializer;
import org.apache.http.impl.entity.LaxContentLengthStrategy;
import org.apache.http.impl.entity.StrictContentLengthStrategy;
-import org.apache.http.impl.io.AbstractMessageParser;
+import org.apache.http.impl.io.HttpRequestWriter;
+import org.apache.http.impl.io.HttpResponseParser;
+import org.apache.http.io.HttpMessageParser;
+import org.apache.http.io.HttpMessageWriter;
import org.apache.http.io.SessionInputBuffer;
import org.apache.http.io.SessionOutputBuffer;
-import org.apache.http.message.BasicHeader;
-import org.apache.http.message.BasicRequestLine;
-import org.apache.http.message.BasicStatusLine;
-import org.apache.http.message.BufferedHeader;
-import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
-import org.apache.http.protocol.HTTP;
-import org.apache.http.util.CharArrayBuffer;
/**
* Abstract client-side HTTP connection capable of transmitting and receiving data
@@ -74,26 +65,20 @@
*/
public abstract class AbstractHttpClientConnection implements HttpClientConnection {
- private final CharArrayBuffer buffer;
private final EntitySerializer entityserializer;
private final EntityDeserializer entitydeserializer;
- private final HttpResponseFactory responsefactory;
private SessionInputBuffer inbuffer = null;
private SessionOutputBuffer outbuffer = null;
-
- private int maxHeaderCount = -1;
- private int maxLineLen = -1;
- private int maxGarbageLines = -1;
+ private HttpMessageParser responseParser = null;
+ private HttpMessageWriter requestWriter = null;
private HttpConnectionMetricsImpl metrics;
public AbstractHttpClientConnection() {
super();
- this.buffer = new CharArrayBuffer(128);
this.entityserializer = createEntitySerializer();
this.entitydeserializer = createEntityDeserializer();
- this.responsefactory = createHttpResponseFactory();
}
protected abstract void assertOpen() throws IllegalStateException;
@@ -110,6 +95,19 @@
return new DefaultHttpResponseFactory();
}
+ protected HttpMessageParser createResponseParser(
+ final SessionInputBuffer buffer,
+ final HttpResponseFactory responseFactory,
+ final HttpParams params) {
+ return new HttpResponseParser(buffer, responseFactory, params);
+ }
+
+ protected HttpMessageWriter createRequestWriter(
+ final SessionOutputBuffer buffer,
+ final HttpParams params) {
+ return new HttpRequestWriter(buffer, params);
+ }
+
protected void init(
final SessionInputBuffer inbuffer,
final SessionOutputBuffer outbuffer,
@@ -122,12 +120,10 @@
}
this.inbuffer = inbuffer;
this.outbuffer = outbuffer;
- this.maxHeaderCount = params.getIntParameter(
- HttpConnectionParams.MAX_HEADER_COUNT, -1);
- this.maxLineLen = params.getIntParameter(
- HttpConnectionParams.MAX_LINE_LENGTH, -1);
- this.maxGarbageLines = params.getIntParameter(
- HttpConnectionParams.MAX_STATUS_LINE_GARBAGE, Integer.MAX_VALUE);
+ this.responseParser = createResponseParser(
+ inbuffer, createHttpResponseFactory(), params);
+ this.requestWriter = createRequestWriter(
+ outbuffer, params);
this.metrics = new HttpConnectionMetricsImpl(
inbuffer.getMetrics(),
outbuffer.getMetrics());
@@ -144,8 +140,7 @@
throw new IllegalArgumentException("HTTP request may not be null");
}
assertOpen();
- sendRequestLine(request);
- sendRequestHeaders(request);
+ this.requestWriter.write(request);
this.metrics.incrementRequestCount();
}
@@ -173,35 +168,10 @@
doFlush();
}
- protected void sendRequestLine(final HttpRequest request)
- throws HttpException, IOException {
- this.buffer.clear();
- BasicRequestLine.format(this.buffer, request.getRequestLine());
- this.outbuffer.writeLine(this.buffer);
- }
-
- protected void sendRequestHeaders(final HttpRequest request)
- throws HttpException, IOException {
- for (Iterator it = request.headerIterator(); it.hasNext(); ) {
- Header header = (Header) it.next();
- if (header instanceof BufferedHeader) {
- // If the header is backed by a buffer, re-use the buffer
- this.outbuffer.writeLine(((BufferedHeader)header).getBuffer());
- } else {
- this.buffer.clear();
- BasicHeader.format(this.buffer, header);
- this.outbuffer.writeLine(this.buffer);
- }
- }
- this.buffer.clear();
- this.outbuffer.writeLine(this.buffer);
- }
-
public HttpResponse receiveResponseHeader()
throws HttpException, IOException {
assertOpen();
- HttpResponse response = readResponseStatusLine();
- readResponseHeaders(response);
+ HttpResponse response = (HttpResponse) this.responseParser.parse();
if (response.getStatusLine().getStatusCode() >= 200) {
this.metrics.incrementResponseCount();
}
@@ -218,63 +188,6 @@
response.setEntity(entity);
}
- /**
- * Tests if the string starts with 'HTTP' signature.
- * @param buffer buffer to test
- * @return <tt>true</tt> if the line starts with 'HTTP'
- * signature, <tt>false</tt> otherwise.
- */
- protected static boolean startsWithHTTP(final CharArrayBuffer buffer) {
- try {
- int i = 0;
- while (HTTP.isWhitespace(buffer.charAt(i))) {
- ++i;
- }
- return buffer.charAt(i) == 'H'
- && buffer.charAt(i + 1) == 'T'
- && buffer.charAt(i + 2) == 'T'
- && buffer.charAt(i + 3) == 'P';
- } catch (IndexOutOfBoundsException e) {
- return false;
- }
- }
-
- protected HttpResponse readResponseStatusLine()
- throws HttpException, IOException {
- // clear the buffer
- this.buffer.clear();
- //read out the HTTP status string
- int count = 0;
- do {
- int i = this.inbuffer.readLine(this.buffer);
- if (i == -1 && count == 0) {
- // The server just dropped connection on us
- throw new NoHttpResponseException("The target server failed to respond");
- }
- if (startsWithHTTP(this.buffer)) {
- // Got one
- break;
- } else if (i == -1 || count >= this.maxGarbageLines) {
- // Giving up
- throw new ProtocolException("The server failed to respond with a " +
- "valid HTTP response");
- }
- count++;
- } while(true);
- //create the status line from the status string
- StatusLine statusline = BasicStatusLine.parse(this.buffer, 0, this.buffer.length());
- return this.responsefactory.newHttpResponse(statusline, null);
- }
-
- protected void readResponseHeaders(final HttpResponse response)
- throws HttpException, IOException {
- Header[] headers = AbstractMessageParser.parseHeaders(
- this.inbuffer,
- this.maxHeaderCount,
- this.maxLineLen);
- response.setHeaders(headers);
- }
-
public boolean isStale() {
assertOpen();
try {
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpServerConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpServerConnection.java?view=diff&rev=562707&r1=562706&r2=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpServerConnection.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpServerConnection.java Sat Aug 4 05:30:10 2007
@@ -32,10 +32,7 @@
package org.apache.http.impl;
import java.io.IOException;
-import java.util.Iterator;
-import org.apache.http.ConnectionClosedException;
-import org.apache.http.Header;
import org.apache.http.HttpConnectionMetrics;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
@@ -44,21 +41,17 @@
import org.apache.http.HttpRequestFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpServerConnection;
-import org.apache.http.RequestLine;
import org.apache.http.impl.entity.EntityDeserializer;
import org.apache.http.impl.entity.EntitySerializer;
import org.apache.http.impl.entity.LaxContentLengthStrategy;
import org.apache.http.impl.entity.StrictContentLengthStrategy;
-import org.apache.http.impl.io.AbstractMessageParser;
+import org.apache.http.impl.io.HttpRequestParser;
+import org.apache.http.impl.io.HttpResponseWriter;
+import org.apache.http.io.HttpMessageParser;
+import org.apache.http.io.HttpMessageWriter;
import org.apache.http.io.SessionInputBuffer;
import org.apache.http.io.SessionOutputBuffer;
-import org.apache.http.message.BasicHeader;
-import org.apache.http.message.BasicRequestLine;
-import org.apache.http.message.BasicStatusLine;
-import org.apache.http.message.BufferedHeader;
-import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
-import org.apache.http.util.CharArrayBuffer;
/**
* Abstract server-side HTTP connection capable of transmitting and receiving data
@@ -72,25 +65,20 @@
*/
public abstract class AbstractHttpServerConnection implements HttpServerConnection {
- private final CharArrayBuffer buffer;
private final EntitySerializer entityserializer;
private final EntityDeserializer entitydeserializer;
- private final HttpRequestFactory requestfactory;
private SessionInputBuffer inbuffer = null;
private SessionOutputBuffer outbuffer = null;
+ private HttpMessageParser requestParser = null;
+ private HttpMessageWriter responseWriter = null;
- private int maxHeaderCount = -1;
- private int maxLineLen = -1;
-
private HttpConnectionMetricsImpl metrics;
public AbstractHttpServerConnection() {
super();
- this.buffer = new CharArrayBuffer(128);
this.entityserializer = createEntitySerializer();
this.entitydeserializer = createEntityDeserializer();
- this.requestfactory = createHttpRequestFactory();
}
protected abstract void assertOpen() throws IllegalStateException;
@@ -107,6 +95,19 @@
return new DefaultHttpRequestFactory();
}
+ protected HttpMessageParser createRequestParser(
+ final SessionInputBuffer buffer,
+ final HttpRequestFactory requestFactory,
+ final HttpParams params) {
+ return new HttpRequestParser(buffer, requestFactory, params);
+ }
+
+ protected HttpMessageWriter createResponseWriter(
+ final SessionOutputBuffer buffer,
+ final HttpParams params) {
+ return new HttpResponseWriter(buffer, params);
+ }
+
protected void init(
final SessionInputBuffer inbuffer,
final SessionOutputBuffer outbuffer,
@@ -119,10 +120,10 @@
}
this.inbuffer = inbuffer;
this.outbuffer = outbuffer;
- this.maxHeaderCount = params.getIntParameter(
- HttpConnectionParams.MAX_HEADER_COUNT, -1);
- this.maxLineLen = params.getIntParameter(
- HttpConnectionParams.MAX_LINE_LENGTH, -1);
+ this.requestParser = createRequestParser(inbuffer,
+ createHttpRequestFactory(), params);
+ this.responseWriter = createResponseWriter(
+ outbuffer, params);
this.metrics = new HttpConnectionMetricsImpl(
inbuffer.getMetrics(),
outbuffer.getMetrics());
@@ -131,8 +132,7 @@
public HttpRequest receiveRequestHeader()
throws HttpException, IOException {
assertOpen();
- HttpRequest request = receiveRequestLine();
- receiveRequestHeaders(request);
+ HttpRequest request = (HttpRequest) this.requestParser.parse();
this.metrics.incrementRequestCount();
return request;
}
@@ -147,26 +147,6 @@
request.setEntity(entity);
}
- protected HttpRequest receiveRequestLine()
- throws HttpException, IOException {
- this.buffer.clear();
- int i = this.inbuffer.readLine(this.buffer);
- if (i == -1) {
- throw new ConnectionClosedException("Client closed connection");
- }
- RequestLine requestline = BasicRequestLine.parse(this.buffer, 0, this.buffer.length());
- return this.requestfactory.newHttpRequest(requestline);
- }
-
- protected void receiveRequestHeaders(final HttpRequest request)
- throws HttpException, IOException {
- Header[] headers = AbstractMessageParser.parseHeaders(
- this.inbuffer,
- this.maxHeaderCount,
- this.maxLineLen);
- request.setHeaders(headers);
- }
-
protected void doFlush() throws IOException {
this.outbuffer.flush();
}
@@ -182,8 +162,7 @@
throw new IllegalArgumentException("HTTP response may not be null");
}
assertOpen();
- sendResponseStatusLine(response);
- sendResponseHeaders(response);
+ this.responseWriter.write(response);
if (response.getStatusLine().getStatusCode() >= 200) {
this.metrics.incrementResponseCount();
}
@@ -200,30 +179,6 @@
response.getEntity());
}
- protected void sendResponseStatusLine(final HttpResponse response)
- throws HttpException, IOException {
- this.buffer.clear();
- BasicStatusLine.format(this.buffer, response.getStatusLine());
- this.outbuffer.writeLine(this.buffer);
- }
-
- protected void sendResponseHeaders(final HttpResponse response)
- throws HttpException, IOException {
- for (Iterator it = response.headerIterator(); it.hasNext(); ) {
- Header header = (Header) it.next();
- if (header instanceof BufferedHeader) {
- // If the header is backed by a buffer, re-use the buffer
- this.outbuffer.writeLine(((BufferedHeader)header).getBuffer());
- } else {
- this.buffer.clear();
- BasicHeader.format(this.buffer, header);
- this.outbuffer.writeLine(this.buffer);
- }
- }
- this.buffer.clear();
- this.outbuffer.writeLine(this.buffer);
- }
-
public boolean isStale() {
assertOpen();
try {
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java?view=diff&rev=562707&r1=562706&r2=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageParser.java Sat Aug 4 05:30:10 2007
@@ -36,23 +36,42 @@
import org.apache.http.Header;
import org.apache.http.HttpException;
+import org.apache.http.HttpMessage;
import org.apache.http.ProtocolException;
import org.apache.http.io.HttpMessageParser;
import org.apache.http.io.SessionInputBuffer;
import org.apache.http.message.BufferedHeader;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
import org.apache.http.util.CharArrayBuffer;
/**
- * A utility class for processing HTTP headers.
+ * Message parser base class.
*
* @author Michael Becke
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*/
public abstract class AbstractMessageParser implements HttpMessageParser {
- /** Disabled default constructor. */
- public AbstractMessageParser() {
+ private final SessionInputBuffer sessionBuffer;
+ private final int maxHeaderCount;
+ private final int maxLineLen;
+
+ public AbstractMessageParser(
+ final SessionInputBuffer buffer,
+ final HttpParams params) {
super();
+ if (buffer == null) {
+ throw new IllegalArgumentException("Session input buffer may not be null");
+ }
+ if (buffer == null) {
+ throw new IllegalArgumentException("HTTP parameters may not be null");
+ }
+ this.sessionBuffer = buffer;
+ this.maxHeaderCount = params.getIntParameter(
+ HttpConnectionParams.MAX_HEADER_COUNT, -1);
+ this.maxLineLen = params.getIntParameter(
+ HttpConnectionParams.MAX_LINE_LENGTH, -1);
}
/**
@@ -137,6 +156,19 @@
public static Header[] parseHeaders(final SessionInputBuffer inbuffer)
throws HttpException, IOException {
return parseHeaders(inbuffer, -1, -1);
+ }
+
+ protected abstract HttpMessage parseHead(SessionInputBuffer sessionBuffer)
+ throws IOException, HttpException;
+
+ public HttpMessage parse() throws IOException, HttpException {
+ HttpMessage message = parseHead(this.sessionBuffer);
+ Header[] headers = AbstractMessageParser.parseHeaders(
+ this.sessionBuffer,
+ this.maxHeaderCount,
+ this.maxLineLen);
+ message.setHeaders(headers);
+ return message;
}
}
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java?view=auto&rev=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java Sat Aug 4 05:30:10 2007
@@ -0,0 +1,86 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.io;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.apache.http.Header;
+import org.apache.http.HttpException;
+import org.apache.http.HttpMessage;
+import org.apache.http.io.HttpMessageWriter;
+import org.apache.http.io.SessionOutputBuffer;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.message.BufferedHeader;
+import org.apache.http.params.HttpParams;
+import org.apache.http.util.CharArrayBuffer;
+
+public abstract class AbstractMessageWriter implements HttpMessageWriter {
+
+ private final SessionOutputBuffer sessionBuffer;
+ private final CharArrayBuffer lineBuf;
+
+ public AbstractMessageWriter(final SessionOutputBuffer buffer, final HttpParams params) {
+ super();
+ if (buffer == null) {
+ throw new IllegalArgumentException("Session input buffer may not be null");
+ }
+ this.sessionBuffer = buffer;
+ this.lineBuf = new CharArrayBuffer(128);
+ }
+
+ protected abstract void writeHeadLine(CharArrayBuffer lineBuffer, HttpMessage message);
+
+ public void write(
+ final HttpMessage message) throws IOException, HttpException {
+ if (message == null) {
+ throw new IllegalArgumentException("HTTP message may not be null");
+ }
+ this.lineBuf.clear();
+ writeHeadLine(this.lineBuf, message);
+ this.sessionBuffer.writeLine(this.lineBuf);
+ for (Iterator it = message.headerIterator(); it.hasNext(); ) {
+ Header header = (Header) it.next();
+ if (header instanceof BufferedHeader) {
+ // If the header is backed by a buffer, re-use the buffer
+ this.sessionBuffer.writeLine(((BufferedHeader)header).getBuffer());
+ } else {
+ this.lineBuf.clear();
+ BasicHeader.format(this.lineBuf, header);
+ this.sessionBuffer.writeLine(this.lineBuf);
+ }
+ }
+ this.lineBuf.clear();
+ this.sessionBuffer.writeLine(this.lineBuf);
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java?view=auto&rev=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java Sat Aug 4 05:30:10 2007
@@ -0,0 +1,74 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.io;
+
+import java.io.IOException;
+
+import org.apache.http.ConnectionClosedException;
+import org.apache.http.HttpException;
+import org.apache.http.HttpMessage;
+import org.apache.http.HttpRequestFactory;
+import org.apache.http.RequestLine;
+import org.apache.http.io.SessionInputBuffer;
+import org.apache.http.message.BasicRequestLine;
+import org.apache.http.params.HttpParams;
+import org.apache.http.util.CharArrayBuffer;
+
+public class HttpRequestParser extends AbstractMessageParser {
+
+ private final HttpRequestFactory requestFactory;
+ private final CharArrayBuffer lineBuf;
+
+ public HttpRequestParser(
+ final SessionInputBuffer buffer,
+ final HttpRequestFactory requestFactory,
+ final HttpParams params) {
+ super(buffer, params);
+ if (requestFactory == null) {
+ throw new IllegalArgumentException("Request factory may not be null");
+ }
+ this.requestFactory = requestFactory;
+ this.lineBuf = new CharArrayBuffer(128);
+ }
+
+ protected HttpMessage parseHead(
+ final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
+ this.lineBuf.clear();
+ int i = sessionBuffer.readLine(this.lineBuf);
+ if (i == -1) {
+ throw new ConnectionClosedException("Client closed connection");
+ }
+ RequestLine requestline = BasicRequestLine.parse(this.lineBuf, 0, this.lineBuf.length());
+ return this.requestFactory.newHttpRequest(requestline);
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestParser.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java?view=auto&rev=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java Sat Aug 4 05:30:10 2007
@@ -0,0 +1,53 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.io;
+
+import org.apache.http.HttpMessage;
+import org.apache.http.HttpRequest;
+import org.apache.http.io.SessionOutputBuffer;
+import org.apache.http.message.BasicRequestLine;
+import org.apache.http.params.HttpParams;
+import org.apache.http.util.CharArrayBuffer;
+
+public class HttpRequestWriter extends AbstractMessageWriter {
+
+ public HttpRequestWriter(final SessionOutputBuffer buffer, final HttpParams params) {
+ super(buffer, params);
+ }
+
+ protected void writeHeadLine(
+ final CharArrayBuffer lineBuffer,
+ final HttpMessage message) {
+ BasicRequestLine.format(lineBuffer, ((HttpRequest) message).getRequestLine());
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java?view=auto&rev=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java Sat Aug 4 05:30:10 2007
@@ -0,0 +1,117 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.io;
+
+import java.io.IOException;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpMessage;
+import org.apache.http.HttpResponseFactory;
+import org.apache.http.NoHttpResponseException;
+import org.apache.http.ProtocolException;
+import org.apache.http.StatusLine;
+import org.apache.http.io.SessionInputBuffer;
+import org.apache.http.message.BasicStatusLine;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.util.CharArrayBuffer;
+
+public class HttpResponseParser extends AbstractMessageParser {
+
+ private final HttpResponseFactory responseFactory;
+ private final CharArrayBuffer lineBuf;
+ private final int maxGarbageLines;
+
+ public HttpResponseParser(
+ final SessionInputBuffer buffer,
+ final HttpResponseFactory responseFactory,
+ final HttpParams params) {
+ super(buffer, params);
+ if (responseFactory == null) {
+ throw new IllegalArgumentException("Response factory may not be null");
+ }
+ this.responseFactory = responseFactory;
+ this.lineBuf = new CharArrayBuffer(128);
+ this.maxGarbageLines = params.getIntParameter(
+ HttpConnectionParams.MAX_STATUS_LINE_GARBAGE, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Tests if the string starts with 'HTTP' signature.
+ * @param buffer buffer to test
+ * @return <tt>true</tt> if the line starts with 'HTTP'
+ * signature, <tt>false</tt> otherwise.
+ */
+ protected static boolean startsWithHTTP(final CharArrayBuffer buffer) {
+ try {
+ int i = 0;
+ while (HTTP.isWhitespace(buffer.charAt(i))) {
+ ++i;
+ }
+ return buffer.charAt(i) == 'H'
+ && buffer.charAt(i + 1) == 'T'
+ && buffer.charAt(i + 2) == 'T'
+ && buffer.charAt(i + 3) == 'P';
+ } catch (IndexOutOfBoundsException e) {
+ return false;
+ }
+ }
+
+ protected HttpMessage parseHead(
+ final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
+ // clear the buffer
+ this.lineBuf.clear();
+ //read out the HTTP status string
+ int count = 0;
+ do {
+ int i = sessionBuffer.readLine(this.lineBuf);
+ if (i == -1 && count == 0) {
+ // The server just dropped connection on us
+ throw new NoHttpResponseException("The target server failed to respond");
+ }
+ if (startsWithHTTP(this.lineBuf)) {
+ // Got one
+ break;
+ } else if (i == -1 || count >= this.maxGarbageLines) {
+ // Giving up
+ throw new ProtocolException("The server failed to respond with a " +
+ "valid HTTP response");
+ }
+ count++;
+ } while(true);
+ //create the status line from the status string
+ StatusLine statusline = BasicStatusLine.parse(this.lineBuf, 0, this.lineBuf.length());
+ return this.responseFactory.newHttpResponse(statusline, null);
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseParser.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java?view=auto&rev=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java Sat Aug 4 05:30:10 2007
@@ -0,0 +1,53 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.io;
+
+import org.apache.http.HttpMessage;
+import org.apache.http.HttpResponse;
+import org.apache.http.io.SessionOutputBuffer;
+import org.apache.http.message.BasicStatusLine;
+import org.apache.http.params.HttpParams;
+import org.apache.http.util.CharArrayBuffer;
+
+public class HttpResponseWriter extends AbstractMessageWriter {
+
+ public HttpResponseWriter(final SessionOutputBuffer buffer, final HttpParams params) {
+ super(buffer, params);
+ }
+
+ protected void writeHeadLine(
+ final CharArrayBuffer lineBuffer,
+ final HttpMessage message) {
+ BasicStatusLine.format(lineBuffer, ((HttpResponse) message).getStatusLine());
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java?view=diff&rev=562707&r1=562706&r2=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java Sat Aug 4 05:30:10 2007
@@ -55,8 +55,8 @@
public class DefaultNHttpClientConnection
extends NHttpConnectionBase implements NHttpClientConnection {
- private NHttpMessageParser responseParser;
- private NHttpMessageWriter requestWriter;
+ private final NHttpMessageParser responseParser;
+ private final NHttpMessageWriter requestWriter;
public DefaultNHttpClientConnection(
final IOSession session,
Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java?view=diff&rev=562707&r1=562706&r2=562707
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java Sat Aug 4 05:30:10 2007
@@ -55,8 +55,8 @@
public class DefaultNHttpServerConnection
extends NHttpConnectionBase implements NHttpServerConnection {
- private NHttpMessageParser requestParser;
- private NHttpMessageWriter responseWriter;
+ private final NHttpMessageParser requestParser;
+ private final NHttpMessageWriter responseWriter;
public DefaultNHttpServerConnection(
final IOSession session,
@@ -78,7 +78,7 @@
return new HttpRequestParser(buffer, requestFactory, params);
}
- protected HttpResponseWriter createResponseWriter(
+ protected NHttpMessageWriter createResponseWriter(
final SessionOutputBuffer buffer,
final HttpParams params) {
return new HttpResponseWriter(buffer, params);