Class: Async::HTTP::Protocol::HTTP10

Inherits:
HTTP11
  • Object
show all
Defined in:
lib/async/http/protocol/http10.rb

Overview

Implements basic HTTP/1.1 request/response.

Constant Summary collapse

VERSION =
"HTTP/1.0".freeze

Constants inherited from HTTP11

Async::HTTP::Protocol::HTTP11::CLOSE, Async::HTTP::Protocol::HTTP11::CRLF, Async::HTTP::Protocol::HTTP11::HTTP_CONNECTION, Async::HTTP::Protocol::HTTP11::HTTP_CONTENT_LENGTH, Async::HTTP::Protocol::HTTP11::HTTP_TRANSFER_ENCODING, Async::HTTP::Protocol::HTTP11::KEEP_ALIVE

Instance Method Summary collapse

Methods inherited from HTTP11

#initialize, #read_request, #read_response, #send_request, #write_request, #write_response

Constructor Details

This class inherits a constructor from Async::HTTP::Protocol::HTTP11

Instance Method Details

#keep_alive?(headers) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/async/http/protocol/http10.rb', line 37

def keep_alive?(headers)
  headers[HTTP_CONNECTION] == KEEP_ALIVE
end

#read_body(headers) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/async/http/protocol/http10.rb', line 60

def read_body(headers)
  if content_length = headers[HTTP_CONTENT_LENGTH]
    return @stream.read(Integer(content_length))
  # elsif !keep_alive?(headers)
  #  return @stream.read
  end
end

#receive_requestsObject

Server loop.



42
43
44
45
46
47
48
49
50
# File 'lib/async/http/protocol/http10.rb', line 42

def receive_requests
  while request = Request.new(*self.read_request)
    status, headers, body = yield request
    
    write_response(request.version, status, headers, body)
    
    break unless keep_alive?(request.headers) && keep_alive?(headers)
  end
end

#versionObject



33
34
35
# File 'lib/async/http/protocol/http10.rb', line 33

def version
  VERSION
end

#write_body(body, chunked = true) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/async/http/protocol/http10.rb', line 52

def write_body(body, chunked = true)
  buffer = String.new
  body.each{|chunk| buffer << chunk}
    
  @stream.write("Content-Length: #{buffer.bytesize}\r\n\r\n")
  @stream.write(buffer)
end