Apache mangles HTTP requests, causing failure to load pages
Posted: 23. September 2021 16:04
I am building an application which connects to my local web server using a Javascript socket. Note that this is not a browser, so I cannot use any web-based or even web-oriented libraries. I have been able to connect to a web server on the internet using my socket code. When I try to connect o my local Apache server (part of the XAMPP package for Windows, without any changes to the settings), every request I send causes a Bad Request response. When I look into the access log, I can see that the first newline following the HTTP/1.1 protocol identifier is not recognised and probably causes the Bad Request response.
The essential part of the code I am using:
When I use this code with sHost = 'www.jang.nl' (one of my domains, hosted by One.com), I get the contents of the requested page (preceded by the usual headers). But running it with sHost set to 'localhost' gives the Bad Request response. Asking for localhost/index.html page in a local web browser gives the page contents without any problems.
Checking the access log, I see the following lines - first line for the request by the local web browser, second line when using the socket:
::1 - - [23/Sep/2021:09:19:32 +0100] "GET /index.html HTTP/1.1" 304 - "-" "Mozilla (etc)"
::1 - - [23/Sep/2021:09:19:52 +0100] "GET /index.html HTTP/1.1\n" 400 331 "-" "-"
My question: is there any setting for any of the Apache config files that may be causing this ? To me it seems obvious that the \n should not be there in the protocol identifier string, but why is that string not stripped as defined by the protocol ?
The essential part of the code I am using:
- Code: Select all
var sHost = 'localhost';
var oSocket = new Socket;
oSocket.open( sHost + ':80', 'UTF-8' );
oSocket.write( 'GET /index.html HTTP/1.1\r\nHost: ' + sHost + '\r\n\r\n' );
var sReply = oSocket.read( );
alert( sReply );
When I use this code with sHost = 'www.jang.nl' (one of my domains, hosted by One.com), I get the contents of the requested page (preceded by the usual headers). But running it with sHost set to 'localhost' gives the Bad Request response. Asking for localhost/index.html page in a local web browser gives the page contents without any problems.
Checking the access log, I see the following lines - first line for the request by the local web browser, second line when using the socket:
::1 - - [23/Sep/2021:09:19:32 +0100] "GET /index.html HTTP/1.1" 304 - "-" "Mozilla (etc)"
::1 - - [23/Sep/2021:09:19:52 +0100] "GET /index.html HTTP/1.1\n" 400 331 "-" "-"
My question: is there any setting for any of the Apache config files that may be causing this ? To me it seems obvious that the \n should not be there in the protocol identifier string, but why is that string not stripped as defined by the protocol ?