[英]Browser sending Empty Request to own Server
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.我最近一直在 Java 中编写自己的 Web 服务器,因为我认为拥有一个服务器很整洁,昨天我偶然发现了一个我仍然没有解决的问题。 My browser (ungoogled Chromium) seems to send some empty requests or something like that to the Server.
我的浏览器(未使用谷歌搜索的 Chromium)似乎向服务器发送了一些空请求或类似的东西。 I have implemented a Request Handler that is supposed to read the GET Request and extract the requested ressource.
我已经实现了一个请求处理程序,它应该读取 GET 请求并提取请求的资源。 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(" ");
它的工作原理是这样的:它接受请求,例如:“GET /index.html HTTP/1.1”并将其放入带有 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.
方法然后数组看起来像这样: ["GET", "/index.html", "HTTP/1.1"] 然后我将数组的第二个值,在本例中为“/index.html”存储在一个变量中,我然后可以用来定位请求的文件并将其提供给用户。 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 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:, 同源 Sec-Fetch- Mode:, 导航 Sec-Fetch-Dest:, 文档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.但是,有时请求只是空的,它打印出来的正是这个: [] 然后它显然会抛出一个 ArrayIndexOutOfBoundsException 。 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.
这很烦人,因为它与 Linux“curl”命令(curl localhost:8080/index.html)完美配合我已经尝试通过使用wireshark 并寻找一些奇怪的东西来解决这个问题,但我没有尝试修复这个问题很糟糕。 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.
我希望我能为任何人提供足够的信息来帮助我解决我的问题,我已经尝试解决这个问题 2 天了,如果有人帮助我,我将非常感激。 Thanks
谢谢
Sorry for the poor text formation, this is my first stackoverflow question抱歉,文本格式不佳,这是我的第一个 stackoverflow 问题
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.首先验证
request
字符串。 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.然后验证 split() 方法的结果,并将您的逻辑放在该块中。 If the condition is not true, avoid processing it.
如果条件不成立,请避免处理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.