Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
Why is my Javascript socket connecting to a web server but not to localhost?

I am creating an plugin for Adobe FrameMaker using ExtendScript (which is basically Javascript with a bunch of FM-specific objects and methods). As there is no browser I cannot use any of the web-based libraries to connect to a server. I have been using a Socket and it works fine when I connect to one of my hosted domains. For testing and demo purposes I now want to connect to my localhost, which has the XAMPP package running on it.

Connecting to the localhost from a web browser works fine, so I know the page I am requesting exists and the local Apache is working. But when I ask for the same page from my script I get a Bad Request error. Checking the Apache access log I find that the line sent to the socket is misinterpreted: the first \r\n after the protocol shows up as \n following the HTTP/1.1 string. Here are two lines from the Apache access log, the first one for a request from my local web browser, the second from the socket:

::1 - - [23/Sep/2021:09:19:32 +0100] "GET /index.html HTTP/1.1" 304 - "-" "Mozilla [etc]"
::1 - - [23/Sep/2021:09:20:51 +0100] "GET /index.html HTTP/1.1\n" 400 331 "-" "-"

The javascript I am using to connect to a server is here:

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 );

Running this code gives a 400 Bad Request response. When I change the host to one of my web domains without any further changes, I get the 200 response code plus the requested page. Of course, putting 'localhost/index.html' in my browser gives the page without any issues.

This might be an Apache access settings issue but I cannot figure out where the problem might be. I have a fresh XAMPP installation and did not change anything to the default settings. It seems to be there is something in the handling of incoming requests that is handled differently between the browser and the socket, which causes the \n to remain attached to the protocol identifier and that of course throws off the Apache http server. But how do I get the protocol string to be correctly parsed? And why do I need to jump through those hoops in the browser when I am following the protocol and it works for a server on the internet?

Thanks for pointers that help me solve this and be able to host pages (and a MySQL database locally instead of having to put them on some server in the internet) - please do not refer to web sockets or any web-oriented libraries as I cannot use them in my application. The Socket object is the only tool I have available.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!