Skip to content

Commit

Permalink
better workaround for http-kit#70
Browse files Browse the repository at this point in the history
  • Loading branch information
stask committed Jul 18, 2013
1 parent dba6b14 commit f65f592
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/java/org/httpkit/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ public static String camelCase(String key) {
}

public static String getPath(URI uri) {
String path = uri.getRawPath();
return getPath(uri, true);
}

public static String getPath(URI uri, boolean encodePath) {
String path = encodePath ? encodeURI(uri.getPath()) : uri.getRawPath();
String query = uri.getRawQuery();
if ("".equals(path))
path = "/";
Expand Down
10 changes: 7 additions & 3 deletions src/java/org/httpkit/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ private void doWrite(SelectionKey key) {
}

public void exec(String url, RequestConfig cfg, SSLEngine engine, IRespListener cb) {
exec(url, true, cfg, engine, cb);
}

public void exec(String url, boolean encodePath, RequestConfig cfg, SSLEngine engine, IRespListener cb) {
URI uri;
try {
uri = new URI(url);
Expand Down Expand Up @@ -250,7 +254,7 @@ public void exec(String url, RequestConfig cfg, SSLEngine engine, IRespListener

ByteBuffer request[];
try {
request = encode(cfg.method, headers, cfg.body, uri);
request = encode(cfg.method, headers, cfg.body, uri, encodePath);
} catch (IOException e) {
cb.onThrowable(e);
return;
Expand All @@ -270,7 +274,7 @@ public void exec(String url, RequestConfig cfg, SSLEngine engine, IRespListener
}

private ByteBuffer[] encode(HttpMethod method, HeaderMap headers, Object body,
URI uri) throws IOException {
URI uri, boolean encodePath) throws IOException {
ByteBuffer bodyBuffer = HttpUtils.bodyBuffer(body);

if (body != null) {
Expand All @@ -279,7 +283,7 @@ private ByteBuffer[] encode(HttpMethod method, HeaderMap headers, Object body,
headers.put("Content-Length", "0");
}
DynamicBytes bytes = new DynamicBytes(196);
bytes.append(method.toString()).append(SP).append(HttpUtils.getPath(uri));
bytes.append(method.toString()).append(SP).append(HttpUtils.getPath(uri, encodePath));
bytes.append(" HTTP/1.1\r\n");
headers.encodeHeaders(bytes);
ByteBuffer headBuffer = ByteBuffer.wrap(bytes.get(), 0, bytes.length());
Expand Down
6 changes: 3 additions & 3 deletions src/org/httpkit/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@
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 as ] :as opts
[{:keys [client timeout filter worker-pool keepalive as encode-path] :as opts
:or {client @default-client timeout 60000 filter IFilter/ACCEPT_ALL
worker-pool default-pool keepalive 120000 as :auto}}
worker-pool default-pool keepalive 120000 as :auto encode-path true}}
callback]
(let [{:keys [url method headers body sslengine]} (coerce-req opts)
response (promise)
Expand All @@ -150,7 +150,7 @@
;; only the 4 support now
(case as :auto 1 :text 2 :stream 3 :byte-array 4))
cfg (RequestConfig. method headers body timeout keepalive)]
(.exec ^HttpClient client url cfg sslengine listener)
(.exec ^HttpClient client url encode-path cfg sslengine listener)
response))

(defmacro ^:private defreq [method]
Expand Down

0 comments on commit f65f592

Please sign in to comment.