HTTP server with Ruby

I am trying to make a small HTTP server in Ruby. Its just designed to learn how the material works, nothing special. So what I did was send the server an ajax request. The server is listening on port 2000, so the ajax request is also on port 2000.

The problem I am facing is that the ajax request is returned only with headers, the content is missing. I tried everything I could find, but it doesn't seem to work either ...

I attached the code for you to watch

require 'socket'               # Get sockets from stdlib
server = TCPServer.new(2000)  # Socket to listen on port 2000
loop {                         # Servers run forever
  client = server.accept       # Wait for a client to connect
  headers = "HTTP/1.1 200 OK\r\nDate: Tue, 14 Dec 2010 10:48:45 GMT\r\nServer: Ruby\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n"
  client.puts headers  # Send the time to the client
  client.puts "<html>amit</html>"
  client.close                 # Disconnect from the client
}

The ajax request works when pointed to by a PHP script running on Apache. the only problem is when using this server.

Any help, as always, is greatly appreciated :)

Regards, Amit

+3
3

.

$ telnet localhost 2000
HTTP/1.1 200 OK
Date: Tue, 14 Dec 2010 10:48:45 GMT
Server: Ruby
Content-Type: text/html; charset=iso-8859-1

<html>amit</html>


Connection to host lost.

, AJAX...

+1

Content-Length, , , , curl:

require 'socket'               # Get sockets from stdlib
server = TCPServer.new(2000)   # Socket to listen on port 2000
loop {                         # Servers run forever
  client = server.accept       # Wait for a client to connect
  resp = "<html>amit</html>"
  headers = ["HTTP/1.1 200 OK",
             "Date: Tue, 14 Dec 2010 10:48:45 GMT",
             "Server: Ruby",
             "Content-Type: text/html; charset=iso-8859-1",
             "Content-Length: #{resp.length}\r\n\r\n"].join("\r\n")
  client.puts headers          # Send the time to the client
  client.puts resp
  client.close                 # Disconnect from the client
}
+1

Access-Control-Allow-Origin HTTP. AJAX, XHTTPRequest, , Cross-Origin .

HTTP-:

headers = "HTTP/1.1 200 OK\r\n"
headers += "Access-Control-Allow-Origin: *\r\n"
headers += "Date: Tue, 14 Dec 2010 10:48:45 GMT\r\nServer: Ruby\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n"

, .

+1

Source: https://habr.com/ru/post/1780201/


All Articles