Skip to content

Commit

Permalink
output coercion: :as option, accepted :auto :text :stream :byte-array
Browse files Browse the repository at this point in the history
  • Loading branch information
shenfeng committed May 1, 2013
1 parent 4a721e9 commit a15fde2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
30 changes: 15 additions & 15 deletions src/java/org/httpkit/client/RespListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,23 @@ class Handler implements Runnable {
private final int status;
private final Map<String, String> headers;
private final Object body;
private final Throwable e;
private final IResponseHandler handler;

public Handler(IResponseHandler handler, int status, Map<String, String> headers,
Object body, Throwable e) {
Object body) {
this.handler = handler;
this.status = status;
this.headers = headers;
this.body = body;
this.e = e;
this.handler = handler;
}

public Handler(IResponseHandler handler, Throwable e) {
this(handler, 0, null, null, e);
}

public Handler(IResponseHandler handler, int status, Map<String, String> headers,
Object body) {
this(handler, status, headers, body, null);
this(handler, 0, null, e);
}

public void run() {
if (e != null) {
handler.onThrowable(e);
if (body instanceof Throwable) {
handler.onThrowable((Throwable) body);
} else {
handler.onSuccess(status, headers, body);
}
Expand Down Expand Up @@ -106,11 +99,13 @@ private DynamicBytes unzipBody() throws IOException {
private final IResponseHandler handler;
private final IFilter filter;
private final ExecutorService pool;
final int coercion;

public RespListener(IResponseHandler handler, IFilter filter, ExecutorService pool) {
public RespListener(IResponseHandler handler, IFilter filter, ExecutorService pool, int coercion) {
body = new DynamicBytes(1024 * 16);
this.filter = filter;
this.handler = handler;
this.coercion = coercion;
this.pool = pool;
}

Expand All @@ -128,13 +123,18 @@ public void onCompleted() {
}
try {
DynamicBytes bytes = unzipBody();
if (isText()) {
// 1=> auto, 2=>text, 3=>stream, 4=>byte-array
if (coercion == 2 || (coercion == 1 && isText())) {
Charset charset = HttpUtils.detectCharset(headers, bytes);
String html = new String(bytes.get(), 0, bytes.length(), charset);
pool.submit(new Handler(handler, status.getCode(), headers, html));
} else {
BytesInputStream is = new BytesInputStream(bytes.get(), bytes.length());
pool.submit(new Handler(handler, status.getCode(), headers, is));
if (coercion == 4) { // byte-array
pool.submit(new Handler(handler, status.getCode(), headers, is.bytes()));
} else {
pool.submit(new Handler(handler, status.getCode(), headers, is));
}
}
} catch (IOException e) {
handler.onThrowable(e);
Expand Down
3 changes: 1 addition & 2 deletions src/java/org/httpkit/server/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public HttpServer(String ip, int port, IHandler handler, int maxBody, int maxLin
this.selector = Selector.open();
this.serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
InetSocketAddress addr = new InetSocketAddress(ip, port);
serverChannel.socket().bind(addr);
serverChannel.socket().bind(new InetSocketAddress(ip, port));
serverChannel.register(selector, OP_ACCEPT);
}

Expand Down
23 changes: 18 additions & 5 deletions src/org/httpkit/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,22 @@
(println \"resp1's status: \" (:status @resp1))
(println \"resp2's status: \" (:status @resp2)))
Request options:
:url :method :headers :timeout :query-params :form-params
Output coercion:
;; Return the body as a byte stream
(request {:url \"http://site.com/favicon.ico\" :as :stream})
;; Coerce as a byte-array
(request {:url \"http://site.com/favicon.ico\" :as :byte-array})
;; return the body as a string body
(request {:url \"http://site.com/string.txt\" :as :text})
;; Try to automatically coerce the output based on the content-type header, currently supports :text :stream, (with automatic charset detection)
(request {:url \"http://site.com/string.txt\" :as :auto})
Request options:
:url :method :headers :timeout :query-params :form-params :as
:client :body :basic-auth :user-agent :filter :worker-pool"
[{:keys [client timeout filter worker-pool keepalive sslengine] :as opts
:or {client @default-client timeout 60000 filter IFilter/ACCEPT_ALL worker-pool default-pool keepalive 120000}}
[{:keys [client timeout filter worker-pool keepalive sslengine as] :as opts
:or {client @default-client timeout 60000 filter IFilter/ACCEPT_ALL
worker-pool default-pool keepalive 120000 as :auto}}
callback]
(let [{:keys [url method headers body]} (coerce-req opts)
response (promise)
Expand All @@ -118,7 +129,9 @@
:status status}))
(onThrowable [this t]
(deliver-resp {:opts opts :error t})))
listener (RespListener. handler filter worker-pool)
listener (RespListener. handler filter worker-pool
;; only the 4 support now
(case as :auto 1 :text 2 :stream 3 :byte-array 4))
cfg (RequestConfig. method timeout keepalive sslengine)]
(.exec ^HttpClient client url headers body cfg listener)
response))
Expand Down
14 changes: 14 additions & 0 deletions test/org/httpkit/client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@
(is (= url (:u params)))
(is (= "false" (:try params)))))

(deftest test-output-coercion
(let [url "http://localhost:4347/length?length=1024"]
(let [body (:body @(http/get url {:as :text}))]
(is (string? body))
(is (= 1024 (count body))))
(let [body (:body @(http/get url))] ; auto
(is (string? body)))
(let [body (:body @(http/get url {:as :auto}))] ; auto
(is (string? body)))
(let [body (:body @(http/get url {:as :stream}))]
(is (instance? java.io.InputStream body)))
(let [body (:body @(http/get url {:as :byte-array}))]
(is (= 1024 (alength body))))))

(deftest test-https
(let [get-url (fn [length] (str "https://localhost:9898/length?length=" length))]
(doseq [i (range 0 2)]
Expand Down

0 comments on commit a15fde2

Please sign in to comment.