fix blocking issue when trying to parse http request in java

1 week ago 5
ARTICLE AD BOX

I'm trying to parse http requests for fun and I'm currently stuck because the method I'm uses blocks until input is received.

byte[] buffer = new byte[1024]; while(!request.isDone()) { int n = input.read(buffer); if(n == -1) { break; } request.parseRequest(buffer, n); } return request;

I'm trying to read into a buffer and parsing the request until it's done. I have a small class which creates a serversocket where I send in a simple curl request such as "curl -v http://localhost:5000/hello". When my buffer is of a much smaller size like 8 bytes, the request is read in chunks and parsed correctly. But when my buffer is much bigger and can read the entire request in one go, only the request line gets parsed. Then, the loop above gets returned to and blocks on the third line where it needs to read. But curl keeps the connection alive and my program is just waiting for input that's never sent. Is there a viable solution to my problem as is other than looking for a non-blocking socket in java?

Read Entire Article