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::CONTENT_LENGTH, Async::HTTP::Protocol::HTTP11::CRLF, Async::HTTP::Protocol::HTTP11::KEEP_ALIVE, Async::HTTP::Protocol::HTTP11::TRANSFER_ENCODING

Instance Method Summary collapse

Methods inherited from HTTP11

#initialize, #multiplex, #read_request, #read_response, #reusable?, #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[:connection] == KEEP_ALIVE
end

#read_body(headers) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/async/http/protocol/http10.rb', line 66

def read_body(headers)
	if content_length = headers[: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
51
52
53
54
55
56
# 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)
		
		unless keep_alive?(request.headers) && keep_alive?(headers)
			@keep_alive = false
			
			break
		end
	end
	
	return false
end

#versionObject



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

def version
	VERSION
end

#write_body(body, chunked = true) ⇒ Object



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

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