开发者

HTTP server with Ruby

开发者 https://www.devze.com 2023-01-30 06:19 出处:网络
I am trying to make a small HTTP server in Ruby. Its just meant to learn how stuff works, nothing big. So what i did is to send the server an ajax request. The server is listening on port 2000, and so

I am trying to make a small HTTP server in Ruby. Its just meant to learn how stuff works, nothing big. So what i did is to send the server an ajax request. The server is listening on port 2000, and so the ajax request is also on port 2000.

The problem i am facing is that the ajax request is returned only with the headers, the content is missing. I tried everything i could find, but it seems to fail too...

I have attached the code, for you to take 开发者_如何学Ca look

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 is working when pointed to a PHP script running on Apache. the only problem seems to occur when using this server.

Any help is as always, deeply appreciated :)

Regards, Amit


Your code works fine.

$ 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.

Now you'll have to find out what's wrong with your AJAX request...


I also included the Content-Length header and that seemed to clear up the errors I was getting with 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
}


You're missing the Access-Control-Allow-Origin HTTP header in your response headers. Since you're trying an AJAX request, and it is basically an XHTTPRequest, you need to pass it in the response to the client, in order to accomplish the Cross-Origin Resource Sharing implementations of your browser.

Just add in your HTTP server:

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"

and then you can try to see if works.

0

精彩评论

暂无评论...
验证码 换一张
取 消