Skip to content

Commit

Permalink
Enabling keep-alive by default for HTTP/1.0 protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Mar 1, 2016
1 parent ef42aec commit 99e8f8a
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/HttpParser.java
Expand Up @@ -96,11 +96,19 @@ public void parse(Buf buf, BoolWrap isGet, BoolWrap isKeepAlive, Range body, Ran
buf.scanUntil(SPACE, uri);
buf.scanLn(protocol);

boolean keepAliveByDefault = protocol.isEmpty() || bytes.get(protocol.last()) != '0'; // e.g. HTTP/1.1
IntWrap result = helper.integers[0];
buf.scanLnLn(headers.reset(), result, (byte) 'v', (byte) 'e');

int possibleKeepAliveHeaderPos = result.value;
isKeepAlive.value = possibleKeepAliveHeaderPos >= 0 && isKeepAlive(bytes, headers, helper, possibleKeepAliveHeaderPos);
if (keepAliveByDefault) {
buf.scanLnLn(headers.reset(), result, (byte) 's', (byte) 'e'); // clo[se]
int possibleCloseHeaderPos = result.value;
isKeepAlive.value = possibleCloseHeaderPos < 0 || isConnectionHeader(bytes, headers, helper, possibleCloseHeaderPos);

} else {
buf.scanLnLn(headers.reset(), result, (byte) 'v', (byte) 'e'); // keep-ali[ve]
int possibleKeepAliveHeaderPos = result.value;
isKeepAlive.value = possibleKeepAliveHeaderPos >= 0 && isConnectionHeader(bytes, headers, helper, possibleKeepAliveHeaderPos);
}

BytesUtil.split(bytes, uri, ASTERISK, path, query, false);

Expand All @@ -110,8 +118,8 @@ public void parse(Buf buf, BoolWrap isGet, BoolWrap isKeepAlive, Range body, Ran
}
}

private boolean isKeepAlive(Bytes bytes, Ranges headers, RapidoidHelper helper, int possibleKeepAliveHeaderPos) {
Range maybeConnHdr = headers.get(possibleKeepAliveHeaderPos);
private boolean isConnectionHeader(Bytes bytes, Ranges headers, RapidoidHelper helper, int possibleConnectionHeaderPos) {
Range maybeConnHdr = headers.get(possibleConnectionHeaderPos);

if (BytesUtil.startsWith(bytes, maybeConnHdr, CONNECTION, true)) {
return true;
Expand Down

0 comments on commit 99e8f8a

Please sign in to comment.