I have been coding my own Webserver in Java recently because I thought it was neat having one and yesterday I stumbled upon a problem that I still havent fixed. My browser (ungoogled Chromium) seems to send some empty requests or something like that to the Server. I have implemented a Request Handler that is supposed to read the GET Request and extract the requested ressource. It works like this : it takes the request for example : "GET /index.html HTTP/1.1" and puts it in an Array with the String.split(" "); method the Array then looks like this : ["GET", "/index.html", "HTTP/1.1"] Then I store the second value of the array, in this case "/index.html" in a variable that I can then use to locate the requested file and serve it to the User. For debugging purposes I also print out the full requests sent by the user, that looks like this
GET, /index.html, HTTP/1.1 Host:, localhost:8080 Connection:, keep-alive Cache-Control:, max-age=0 Upgrade-Insecure-Requests:, 1 DNT:, 1 User-Agent:, Mozilla/5.0, (X11;, Linux, x86_64), AppleWebKit/537.36, (KHTML,, like, Gecko), Chrome/86.0.4240.111, Safari/537.36 Accept:, text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9 Sec-Fetch-Site:, same-origin Sec-Fetch-Mode:, navigate Sec-Fetch-Dest:, document Referer:, http://localhost:8080/index.html Accept-Encoding:, gzip,, deflate,, br Accept-Language:, en-US,en;q=0.9
However, sometimes the Request is just empty, It prints exactly this : [] It then throws an ArrayIndexOutOfBoundsException obviously. This is pretty annoying because it works perfectly fine with the Linux "curl" command (curl localhost:8080/index.html) I have already tried to solve the issue by using wireshark and looking for some weird stuff but none of my attempts to fix this issue have worked sadly. I hope I could provide enough information for anyone to help me with my Issue, I've been trying to solve this for 2 days now and i'd be super grateful if someone helps me. Thanks
Sorry for the poor text formation, this is my first stackoverflow question
if (!request.trim().isEmpty())
{
String[] requestArray= request.split(" ");
if (requestArray.length > 1)
{
//your logic as usual
}
else
{
log.error("The request has an incorrect format: "+request);
//...
}
}
/*else
{
log.error("The request is empty");
//...
} uncomment this only if needed, as you could get spammed by empty requests */
Validate first the request
String. If it's empty, avoid processing it, no need to split. Trim will remove leading/trailing spaces.
Validate then split() method's result, and place your logic in that block. If the condition is not true, avoid processing it.