Skip to content

Commit

Permalink
status is 1xx, 204 or 304, then body is expect to be empty. fix #52
Browse files Browse the repository at this point in the history
  • Loading branch information
shenfeng committed May 15, 2013
1 parent 0feb538 commit 3ca035e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/java/org/httpkit/client/Decoder.java
Expand Up @@ -27,6 +27,8 @@ public class Decoder {
State state = READ_INITIAL;
private final HttpMethod method;

private boolean emptyBodyExpected = false;

public Decoder(IRespListener listener, HttpMethod method) {
this.listener = listener;
this.method = method;
Expand Down Expand Up @@ -56,6 +58,9 @@ private void parseInitialLine(String sb) throws ProtocolException, AbortExceptio
|| (cStart == cEnd && bStart < bEnd)) {
try {
int status = Integer.parseInt(sb.substring(bStart, bEnd));
// status is not 1xx, 204 or 304, then the body is unbounded.
// RFC2616, section 4.4
emptyBodyExpected = status / 100 == 1 || status == 204 || status == 304;
HttpStatus s = HttpStatus.valueOf(status);

HttpVersion version = HTTP_1_1;
Expand Down Expand Up @@ -165,6 +170,8 @@ private void readHeaders(ByteBuffer buffer) throws LineTooLargeException, AbortE
} else {
state = READ_FIXED_LENGTH_CONTENT;
}
} else if (emptyBodyExpected) {
state = ALL_READ;
} else {
state = READ_VARIABLE_LENGTH_CONTENT;
// for readBody min
Expand Down
6 changes: 6 additions & 0 deletions test/org/httpkit/client_test.clj
Expand Up @@ -14,6 +14,7 @@
(defroutes test-routes
(GET "/get" [] "hello world")
(POST "/post" [] "hello world")
(ANY "/204" [] {:status 204})
(PATCH "/patch" [] "hello world")
(ANY "/method" [] (fn [req]
(let [m (:request-method req)]
Expand Down Expand Up @@ -148,6 +149,11 @@
(is (= p1 (:body @(http/get "http://127.0.0.1:4347/params?a=b" query-params))))
(is (= p1 (:body @(http/post "http://127.0.0.1:4347/params?a=b" query-params))))))

(deftest test-jetty-204-decode-properly
;; fix #52
(is (= 204 (:status @(http/get "http://127.0.0.1:14347/204" {:timeout 20}))))
(is (= 204 (:status @(http/post "http://127.0.0.1:14347/204" {:timeout 20})))))

(deftest test-http-client-form-params
(let [url "http://127.0.0.1:4347/params"
value "value"]
Expand Down

0 comments on commit 3ca035e

Please sign in to comment.