Skip to content

Commit 515955a

Browse files
committed
HTTPRequestProcessor: Allow max header size to be set and optimize writes
This allows the max header size to be set (since it looks like it was desired to be configurable). It also optimizes `runProcessData()` to avoid needing to create the new `tmp` buffer
1 parent 838cd67 commit 515955a

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

protocol/src/main/java/org/threadly/litesockets/protocols/http/request/HTTPRequestProcessor.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.threadly.litesockets.protocols.http.shared.HTTPParsingException;
1212
import org.threadly.litesockets.protocols.websocket.WSFrame;
1313
import org.threadly.litesockets.protocols.websocket.WSUtils;
14+
import org.threadly.util.ArgumentVerifier;
1415

1516
/**
1617
* This processes byte data and turns it into HTTPrequests. It does this through callbacks to a {@link HTTPRequestCallback} interface.
@@ -26,7 +27,6 @@ public class HTTPRequestProcessor {
2627
private final ReuseableMergedByteBuffers pendingBuffers = new ReuseableMergedByteBuffers();
2728
private final ListenerHelper<HTTPRequestCallback> listeners = new ListenerHelper<>(HTTPRequestCallback.class);
2829
private int maxHeaderLength = MAX_HEADER_LENGTH;
29-
private int maxRowLength = MAX_HEADER_ROW_LENGTH;
3030
private HTTPRequest request;
3131
private int currentBodySize = 0;
3232
private long bodySize = 0;
@@ -39,6 +39,17 @@ public class HTTPRequestProcessor {
3939
* Constructs an httpRequestProcessor.
4040
*/
4141
public HTTPRequestProcessor() {}
42+
43+
/**
44+
* Set the maximum sized headers this processor is willing to accept.
45+
*
46+
* @param maxHeaderLength The size in bytes that the header in total can not exceed
47+
*/
48+
public void setMaxHeaderLength(int maxHeaderLength) {
49+
ArgumentVerifier.assertGreaterThanZero(maxHeaderLength, "maxHeaderLength");
50+
51+
this.maxHeaderLength = maxHeaderLength;
52+
}
4253

4354
/**
4455
* Adds an {@link HTTPRequestCallback} to the processor. More the one can be added.
@@ -103,10 +114,8 @@ public void connectionClosed() {
103114
}
104115
}
105116

106-
107117
/**
108118
* Resets the processor and any pending buffers left in it.
109-
*
110119
*/
111120
public void clearBuffer() {
112121
reset();
@@ -122,17 +131,16 @@ private void runProcessData() {
122131
return;
123132
}
124133
if(pos > -1) {
125-
MergedByteBuffers tmp = new ReuseableMergedByteBuffers();
126-
tmp.add(pendingBuffers.pullBuffer(pos+2));
127-
pendingBuffers.discard(2);
128134
try{
129-
String reqh = tmp.getAsString(tmp.indexOf(HTTPConstants.HTTP_NEWLINE_DELIMINATOR));
130-
if(reqh.length() > this.maxRowLength) {
135+
int reqDelim = pendingBuffers.indexOf(HTTPConstants.HTTP_NEWLINE_DELIMINATOR);
136+
if(reqDelim >= MAX_HEADER_ROW_LENGTH) {
131137
reset(new HTTPParsingException("Request Header is to big!"));
132138
return;
133139
}
140+
String reqh = pendingBuffers.getAsString(reqDelim);
134141
HTTPRequestHeader hrh = new HTTPRequestHeader(reqh);
135-
HTTPHeaders hh = new HTTPHeaders(tmp.getAsString(tmp.remaining()));
142+
HTTPHeaders hh = new HTTPHeaders(pendingBuffers.getAsString((pos + 2) - reqh.length()));
143+
pendingBuffers.discard(2); // discard final newline
136144
request = new HTTPRequest(hrh, hh);
137145
listeners.call().headersFinished(request);
138146
bodySize = hh.getContentLength();
@@ -243,7 +251,7 @@ private boolean parseChunkData() {
243251
} else {
244252
if(currentBodySize == bodySize && pendingBuffers.remaining() >=2) {
245253
chunkedBB.flip();
246-
sendDuplicateBBtoListeners(chunkedBB.duplicate());
254+
sendDuplicateBBtoListeners(chunkedBB);
247255
chunkedBB = null;
248256
pendingBuffers.discard(2);
249257
bodySize = -1;

0 commit comments

Comments
 (0)