Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

keep alive.

  • Loading branch information...
commit cd957daf2dbccd07f317213c60a54dccbd13e135 1 parent a77a6e6
mattn authored

Showing 1 changed file with 16 additions and 11 deletions. Show diff stats Hide diff stats

  1. +16 11 example/server.rb
27 example/server.rb
... ... @@ -1,19 +1,24 @@
1 1 h = HTTP::Parser.new()
2 2 s = UV::TCP.new()
3 3 s.bind(UV::ip4_addr('127.0.0.1', 8888))
4   -s.listen(1024) {|x|
  4 +s.listen(1024) do |x|
5 5 return if x != 0
6 6 c = s.accept()
7   - c.read_start {|b|
8   - return unless b
9   - r = h.parse_request(b)
10   - body = "hello #{r.path}"
11   - c.write("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: #{body.size}\r\n\r\n#{body}") {|x|
12   - c.close() if c
13   - c = nil
14   - }
15   - }
16   -}
  7 + c.read_start do |b|
  8 + if b
  9 + r = h.parse_request(b)
  10 + body = "hello #{r.path}"
  11 + if !r.headers.has_key?('Connection') || r.headers['Connection'] != 'Keep-Alive'
  12 + c.write("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: #{body.size}\r\n\r\n#{body}") do |x|
  13 + c.close() if c
  14 + c = nil
  15 + end
  16 + else
  17 + c.write("HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Length: #{body.size}\r\n\r\n#{body}")
  18 + end
  19 + end
  20 + end
  21 +end
17 22
18 23 t = UV::Timer.new
19 24 t.start(3000, 3000) {|x|

0 comments on commit cd957da

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