简体   繁体   English

使用Ruby的HTTP服务器

[英]HTTP server with Ruby

I am trying to make a small HTTP server in Ruby. 我想在Ruby中创建一个小型HTTP服务器。 Its just meant to learn how stuff works, nothing big. 它只是为了学习东西是如何工作的,没什么大不了的。 So what i did is to send the server an ajax request. 所以我做的是向服务器发送ajax请求。 The server is listening on port 2000, and so the ajax request is also on port 2000. 服务器正在侦听端口2000,因此ajax请求也在端口2000上。

The problem i am facing is that the ajax request is returned only with the headers, the content is missing. 我面临的问题是只有标题返回ajax请求,内容丢失。 I tried everything i could find, but it seems to fail too... 我尝试了我能找到的一切,但它似乎也失败了......

I have attached the code, for you to take a 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. 当指向在Apache上运行的PHP脚本时,ajax请求正在工作。 the only problem seems to occur when using this server. 使用此服务器时似乎唯一的问题。

Any help is as always, deeply appreciated :) 任何帮助都一如既往,深深感激:)

Regards, Amit 此致,阿米特

You're missing the Access-Control-Allow-Origin HTTP header in your response headers. 您在响应标头中缺少Access-Control-Allow-Origin HTTP标头。 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. 由于您正在尝试AJAX请求,并且它基本上是XHTTPRequest,因此您需要在响应中将其传递给客户端,以便完成浏览器的跨源资源共享实现。

Just add in your HTTP server: 只需添加您的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"

and then you can try to see if works. 然后你可以尝试看看是否有效。

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... 现在你必须找出你的AJAX请求有什么问题......

I also included the Content-Length header and that seemed to clear up the errors I was getting with curl: 我还包括了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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM