Index: httpcore/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java
===================================================================
--- httpcore/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java (revision 490694)
+++ httpcore/module-main/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java (working copy)
@@ -101,6 +101,7 @@
setHttpDataReceiver(receiver);
setHttpDataTransmitter(transmitter);
+ setRequestCount(0);
setMaxHeaderCount(params.getIntParameter(HttpConnectionParams.MAX_HEADER_COUNT, -1));
setResponseFactory(new DefaultHttpResponseFactory());
this.open = true;
Index: httpcore/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java
===================================================================
--- httpcore/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java (revision 490694)
+++ httpcore/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java (working copy)
@@ -71,7 +71,8 @@
*/
public abstract class AbstractHttpClientConnection implements HttpClientConnection {
- private int maxHeaderCount = -1;
+ private int maxHeaderCount;
+ private int requestCount;
private final CharArrayBuffer buffer;
private final EntitySerializer entityserializer;
@@ -86,6 +87,8 @@
public AbstractHttpClientConnection() {
super();
+ this.maxHeaderCount = -1;
+ this.requestCount = -1;
this.buffer = new CharArrayBuffer(128);
this.entityserializer = new EntitySerializer(
new StrictContentLengthStrategy());
@@ -98,6 +101,24 @@
protected void setMaxHeaderCount(int maxHeaderCount) {
this.maxHeaderCount = maxHeaderCount;
}
+
+ /**
+ * Sets the request counter.
+ * If the request counter is set to a negative value, request counting
+ * is disabled. If it is set to 0 or a positive value, it will be
+ * automatically increased by {@link #sendRequestHeader sendRequestHeader}.
+ * The default value is negative.
+ *
+ * Derived implementations that want to support request counting
+ * should set the request counter to 0 each time the connection
+ * is openend. They can reset the counter to 0 or -1 on connection
+ * close or shutdown, too.
+ *
+ * @param count the new value for the request counter
+ */
+ protected void setRequestCount(int count) {
+ this.requestCount = count;
+ }
protected void setResponseFactory(final HttpResponseFactory responsefactory) {
if (responsefactory == null) {
@@ -133,6 +154,10 @@
assertOpen();
sendRequestLine(request);
sendRequestHeaders(request);
+
+ // count requests only if the counter has been enabled
+ if (requestCount >= 0)
+ requestCount++;
}
public void sendRequestEntity(final HttpEntityEnclosingRequest request)
@@ -150,6 +175,10 @@
request.getEntity());
}
+ public int getRequestCount() {
+ return requestCount;
+ }
+
protected void doFlush() throws IOException {
this.datatransmitter.flush();
}
Index: httpcore/module-main/src/main/java/org/apache/http/HttpClientConnection.java
===================================================================
--- httpcore/module-main/src/main/java/org/apache/http/HttpClientConnection.java (revision 490694)
+++ httpcore/module-main/src/main/java/org/apache/http/HttpClientConnection.java (working copy)
@@ -102,6 +102,25 @@
*/
void receiveResponseEntity(HttpResponse response)
throws HttpException, IOException;
+
+ /**
+ * Obtains the number of requests sent over this connection.
+ * That is the number of requests since the connection has been
+ * opened. If the connection is not open, the result is undefined
+ * and may be negative.
+ * If a request is currently being sent, it is undefined whether
+ * that request is already counted or not.
+ *
+ * Request counting is an optional operation. Connections that
+ * do not count requests MUST return a negative value while the
+ * connection is open. They SHOULD return a negative value
+ * at any times.
+ *
+ * @return the current request count, or
+ * a negative value if there is none
+ */
+ int getRequestCount()
+ ;
/**
* Writes out all pending buffered data over the open connection.