Skip to content
Browse files

keep alive.

  • Loading branch information...
1 parent a77a6e6 commit cd957daf2dbccd07f317213c60a54dccbd13e135 @mattn committed Dec 26, 2012
Showing with 16 additions and 11 deletions.
  1. +16 −11 example/server.rb
View
27 example/server.rb
@@ -1,19 +1,24 @@
h = HTTP::Parser.new()
s = UV::TCP.new()
s.bind(UV::ip4_addr('127.0.0.1', 8888))
-s.listen(1024) {|x|
+s.listen(1024) do |x|
return if x != 0
c = s.accept()
- c.read_start {|b|
- return unless b
- r = h.parse_request(b)
- body = "hello #{r.path}"
- c.write("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: #{body.size}\r\n\r\n#{body}") {|x|
- c.close() if c
- c = nil
- }
- }
-}
+ c.read_start do |b|
+ if b
+ r = h.parse_request(b)
+ body = "hello #{r.path}"
+ if !r.headers.has_key?('Connection') || r.headers['Connection'] != 'Keep-Alive'
+ c.write("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: #{body.size}\r\n\r\n#{body}") do |x|
+ c.close() if c
+ c = nil
+ end
+ else
+ c.write("HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Length: #{body.size}\r\n\r\n#{body}")
+ end
+ end
+ end
+end
t = UV::Timer.new
t.start(3000, 3000) {|x|

0 comments on commit cd957da

Please sign in to comment.
Something went wrong with that request. Please try again.