You are viewing a plain text version of this content. The canonical link for it is here.
Posted to awf-commits@incubator.apache.org by jm...@apache.org on 2011/08/05 23:43:32 UTC
svn commit: r1154415 - in /incubator/deft/sandbox/src:
main/java/org/apache/deft/web/http/client/AsynchronousHttpClient.java
test/java/org/apache/deft/web/http/client/AsynchronousHttpClientTest.java
Author: jmeehan
Date: Fri Aug 5 23:43:31 2011
New Revision: 1154415
URL: http://svn.apache.org/viewvc?rev=1154415&view=rev
Log:
DEFT-131 - Ensure port is within range for AsynchronousHttpClient.
Modified:
incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/client/AsynchronousHttpClient.java
incubator/deft/sandbox/src/test/java/org/apache/deft/web/http/client/AsynchronousHttpClientTest.java
Modified: incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/client/AsynchronousHttpClient.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/client/AsynchronousHttpClient.java?rev=1154415&r1=1154414&r2=1154415&view=diff
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/client/AsynchronousHttpClient.java (original)
+++ incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/client/AsynchronousHttpClient.java Fri Aug 5 23:43:31 2011
@@ -58,6 +58,10 @@ public class AsynchronousHttpClient {
/** The <code>Logger</code>. */
private static final Logger logger = LoggerFactory.getLogger(AsynchronousHttpClient.class);
+ static final int MIN_PORT_NUMBER = 1;
+ static final int MAX_PORT_NUMBER = 65535;
+ static final int DEFAULT_PORT_NUMBER = 80;
+
private static final long TIMEOUT = 15 * 1000; // 15s
private static final AsyncResult<Response> nopAsyncResult = NopAsyncResult.of(Response.class).nopAsyncResult;
@@ -87,8 +91,7 @@ public class AsynchronousHttpClient {
/**
* Create an instance of this type, utilising the given <code>IOLoop</code>.
*
- * @param ioLoop
- * the <code>IOLoop</code> to use.
+ * @param ioLoop the <code>IOLoop</code> to use.
*/
public AsynchronousHttpClient(final IOLoop ioLoop) {
this.ioLoop = ioLoop;
@@ -98,10 +101,8 @@ public class AsynchronousHttpClient {
* Make an asynchronous call as per the given <code>Request</code>, invoking
* the passed callback upon completion.
*
- * @param request
- * the definition of the request to make.
- * @param callback
- * the callback to execute when the response is received.
+ * @param request the definition of the request to make.
+ * @param callback the callback to execute when the response is received.
*/
public void fetch(final Request request, final AsyncResult<Response> callback) {
this.request = request;
@@ -112,11 +113,9 @@ public class AsynchronousHttpClient {
* Make an asynchronous HTTP GET request against the specified URL, and
* invoke the given callback when the response upon completion.
*
- * @param url
- * the URL from which to request, e.g.
+ * @param url the URL from which to request, e.g.
* <em>http://incubator.apache.org/deft/</em>.
- * @param callback
- * the callback to execute when the response is received.
+ * @param callback the callback to execute when the response is received.
*/
public void get(final String url, final AsyncResult<Response> callback) {
request = new Request(url, HttpVerb.GET);
@@ -127,13 +126,10 @@ public class AsynchronousHttpClient {
* Make an asynchronous HTTP POST request against the specified URL, and
* invoke the given callback when the response upon completion.
*
- * @param url
- * the URL from which to request, e.g.
+ * @param url the URL from which to request, e.g.
* <em>http://incubator.apache.org/deft/</em>.
- * @param body
- * the message body to pass.
- * @param callback
- * the callback to execute when the response is received.
+ * @param body the message body to pass.
+ * @param callback the callback to execute when the response is received.
*/
public void post(final String url, final String body, final AsyncResult<Response> callback) {
request = new Request(url, HttpVerb.POST);
@@ -151,13 +147,10 @@ public class AsynchronousHttpClient {
* Make an asynchronous HTTP PUT request against the specified URL, and
* invoke the given callback when the response upon completion.
*
- * @param url
- * the URL from which to request, e.g.
+ * @param url the URL from which to request, e.g.
* <em>http://incubator.apache.org/deft/</em>.
- * @param body
- * the message body to pass.
- * @param callback
- * the callback to execute when the response is received.
+ * @param body the message body to pass.
+ * @param callback the callback to execute when the response is received.
*/
public void put(final String url, final String body, final AsyncResult<Response> callback) {
request = new Request(url, HttpVerb.PUT);
@@ -169,11 +162,9 @@ public class AsynchronousHttpClient {
* Perform the action of making a request against a known URL, before
* invoking the given callback type.
*
- * @param callback
- * the callback to execute when the response is received.
- * @param requestStarted
- * the current time in milliseconds at which the request was
- * begun.
+ * @param callback the callback to execute when the response is received.
+ * @param requestStarted the current time in milliseconds at which the
+ * request was begun.
*/
protected void doFetch(final AsyncResult<Response> callback, final long requestStarted) {
@@ -185,19 +176,30 @@ public class AsynchronousHttpClient {
}
responseCallback = callback;
- int port = request.getURL().getPort();
- port = port == -1 ? 80 : port;
+ int port = getValidPort(request.getURL().getPort());
startTimeout();
- socket.connect(
- request.getURL().getHost(),
- port,
- new AsyncResult<Boolean>() {
- public void onFailure(final Throwable t) { onConnectFailure(t); }
- public void onSuccess(final Boolean result) { onConnect(); }
+ socket.connect(request.getURL().getHost(), port, new AsyncResult<Boolean>() {
+ public void onFailure(final Throwable t) {
+ onConnectFailure(t);
+ }
+
+ public void onSuccess(final Boolean result) {
+ onConnect();
+ }
});
}
+ protected int getValidPort(int port) {
+
+ if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
+ logger.warn("Port [" + port + "] out of range; returning [" + DEFAULT_PORT_NUMBER + "]");
+ return DEFAULT_PORT_NUMBER;
+ }
+
+ return port;
+ }
+
/**
* Close the underlaying {@code AsynchronousSocket}.
*/
@@ -208,10 +210,11 @@ public class AsynchronousHttpClient {
private void startTimeout() {
logger.debug("start timeout...");
- timeout = new Timeout(
- System.currentTimeMillis() + TIMEOUT,
- new AsyncCallback() { public void onCallback() { onTimeout(); }}
- );
+ timeout = new Timeout(System.currentTimeMillis() + TIMEOUT, new AsyncCallback() {
+ public void onCallback() {
+ onTimeout();
+ }
+ });
ioLoop.addTimeout(timeout);
}
@@ -233,9 +236,10 @@ public class AsynchronousHttpClient {
logger.debug("Connected...");
cancelTimeout();
startTimeout();
- socket.write(
- makeRequestLineAndHeaders().getBytes(),
- new AsyncCallback() { public void onCallback() { onWriteComplete(); }
+ socket.write(makeRequestLineAndHeaders().getBytes(), new AsyncCallback() {
+ public void onCallback() {
+ onWriteComplete();
+ }
});
}
@@ -289,14 +293,16 @@ public class AsynchronousHttpClient {
logger.debug("onWriteComplete...");
cancelTimeout();
startTimeout();
- socket.readUntil(
- "\r\n\r\n".getBytes(), /* header delimiter */
- new NaiveAsyncResult() { public void onSuccess(final byte[] headers) { onHeaders(headers); }}
- );
+ socket.readUntil("\r\n\r\n".getBytes(), /* header delimiter */
+ new NaiveAsyncResult() {
+ public void onSuccess(final byte[] headers) {
+ onHeaders(headers);
+ }
+ });
}
private void onHeaders(final byte[] rawResult) {
- String result = new String(rawResult, Charsets.ISO_8859_1);
+ String result = new String(rawResult, Charsets.ISO_8859_1);
logger.debug("headers: {}", result);
cancelTimeout();
response = new Response(requestStarted);
@@ -312,14 +318,16 @@ public class AsynchronousHttpClient {
final String contentLength = response.getHeader("Content-Length");
startTimeout();
if (contentLength != null) {
- socket.readBytes(
- Integer.parseInt(contentLength),
- new NaiveAsyncResult() { public void onSuccess(byte[] body) { onBody(body);}
- });
+ socket.readBytes(Integer.parseInt(contentLength), new NaiveAsyncResult() {
+ public void onSuccess(byte[] body) {
+ onBody(body);
+ }
+ });
} else { // Transfer-Encoding: chunked
- socket.readUntil(
- NEWLINE.getBytes(), /* chunk delimiter */
- new NaiveAsyncResult() { public void onSuccess(byte[] octet) { onChunkOctet(octet);
+ socket.readUntil(NEWLINE.getBytes(), /* chunk delimiter */
+ new NaiveAsyncResult() {
+ public void onSuccess(byte[] octet) {
+ onChunkOctet(octet);
}
});
}
@@ -329,7 +337,7 @@ public class AsynchronousHttpClient {
* JM: TODO, especially noting the redirects we follow....
*/
private void onBody(final byte[] rawBody) {
- final String body = new String(rawBody, Charsets.ISO_8859_1);
+ final String body = new String(rawBody, Charsets.ISO_8859_1);
logger.debug("body size: {}", body.length());
cancelTimeout();
response.setBody(body);
@@ -346,29 +354,33 @@ public class AsynchronousHttpClient {
}
private void onChunk(final byte[] rawChunk) {
- final String chunk = new String(rawChunk, Charsets.ISO_8859_1);
+ final String chunk = new String(rawChunk, Charsets.ISO_8859_1);
logger.debug("chunk size: {}", chunk.length());
cancelTimeout();
response.addChunk(chunk.substring(0, chunk.length() - NEWLINE.length()));
startTimeout();
- socket.readUntil(
- NEWLINE.getBytes(), /* chunk delimiter */
- new NaiveAsyncResult() {public void onSuccess(final byte[] octet) { onChunkOctet(octet);
+ socket.readUntil(NEWLINE.getBytes(), /* chunk delimiter */
+ new NaiveAsyncResult() {
+ public void onSuccess(final byte[] octet) {
+ onChunkOctet(octet);
}
});
}
private void onChunkOctet(final byte[] rawOctet) {
- final String octet = new String(rawOctet, Charsets.ISO_8859_1);
+ final String octet = new String(rawOctet, Charsets.ISO_8859_1);
final int readBytes = Integer.parseInt(octet, 16);
logger.debug("chunk octet: {} (decimal: {})", octet, readBytes);
cancelTimeout();
startTimeout();
if (readBytes != 0) {
- socket.readBytes(
- readBytes + NEWLINE.length(), // chunk delimiter is \r\n
- new NaiveAsyncResult() { public void onSuccess(final byte[] chunk) { onChunk(chunk); }}
- );
+ socket.readBytes(readBytes + NEWLINE.length(), // chunk delimiter is
+ // \r\n
+ new NaiveAsyncResult() {
+ public void onSuccess(final byte[] chunk) {
+ onChunk(chunk);
+ }
+ });
} else {
onBody(response.getBody().getBytes(Charsets.ISO_8859_1));
}
Modified: incubator/deft/sandbox/src/test/java/org/apache/deft/web/http/client/AsynchronousHttpClientTest.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/test/java/org/apache/deft/web/http/client/AsynchronousHttpClientTest.java?rev=1154415&r1=1154414&r2=1154415&view=diff
==============================================================================
--- incubator/deft/sandbox/src/test/java/org/apache/deft/web/http/client/AsynchronousHttpClientTest.java (original)
+++ incubator/deft/sandbox/src/test/java/org/apache/deft/web/http/client/AsynchronousHttpClientTest.java Fri Aug 5 23:43:31 2011
@@ -19,7 +19,10 @@
*/
package org.apache.deft.web.http.client;
+import static org.apache.deft.web.http.client.AsynchronousHttpClient.DEFAULT_PORT_NUMBER;
import static org.apache.deft.web.http.client.AsynchronousHttpClient.HTTP_VERSION;
+import static org.apache.deft.web.http.client.AsynchronousHttpClient.MAX_PORT_NUMBER;
+import static org.apache.deft.web.http.client.AsynchronousHttpClient.MIN_PORT_NUMBER;
import static org.apache.deft.web.http.client.AsynchronousHttpClient.NEWLINE;
import static org.apache.deft.web.http.client.AsynchronousHttpClient.USER_AGENT_HEADER;
import static org.junit.Assert.assertEquals;
@@ -102,4 +105,18 @@ public class AsynchronousHttpClientTest
assertEquals(expected, actual);
}
+
+ @Test
+ public void testGetValidPort() {
+
+ AsynchronousHttpClient client = new AsynchronousHttpClient();
+
+ assertEquals(MIN_PORT_NUMBER, client.getValidPort(MIN_PORT_NUMBER));
+ assertEquals(DEFAULT_PORT_NUMBER, client.getValidPort(MIN_PORT_NUMBER - 1));
+
+ assertEquals(MAX_PORT_NUMBER, client.getValidPort(MAX_PORT_NUMBER));
+ assertEquals(DEFAULT_PORT_NUMBER, client.getValidPort(MAX_PORT_NUMBER + 1));
+
+ assertEquals(42, client.getValidPort(42));
+ }
}