1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# -*- frozen-string-literal: true -*-
module Plum
# HTTP/1.x client session.
class LegacyClientSession
# Creates a new HTTP/1.1 client session
def initialize(socket, config)
require "http/parser"
@socket = socket
@config = config
@parser = setup_parser
@requests = []
@response = nil
@headers_callback = nil
end
def succ
@parser << @socket.readpartial(1024)
rescue => e # including HTTP::Parser::Error
fail(e)
end
def empty?
!@response
end
def close
@closed = true
@response._fail if @response
end
def request(headers, body = nil, &headers_cb)
response = Response.new
@requests << [response, headers, body, headers_cb]
consume_queue
response
end
private
def fail(exception)
close
raise exception
end
def consume_queue
return if @response
response, headers, body, cb = @requests.shift
headers["host"] = headers[":authority"] || headers["host"] || @config[:hostname]
@response = response
@headers_callback = cb
@socket << "%s %s HTTP/1.1\r\n" % [headers[":method"], headers[":path"]]
headers.each { |key, value|
next if key.start_with?(":") # HTTP/2 psuedo headers
@socket << "%s: %s\r\n" % [key, value]
}
@socket << "\r\n"
if body
@socket << body
end
end
def setup_parser
parser = HTTP::Parser.new
parser.on_headers_complete = proc {
resp_headers = parser.headers.map { |key, value| [key.downcase, value] }.to_h
@response._headers({ ":status" => parser.status_code }.merge(resp_headers))
@headers_callback.call(@response) if @headers_callback
}
parser.on_body = proc { |chunk|
@response._chunk(chunk)
}
parser.on_message_complete = proc { |env|
@response._finish
@response = nil
@headers_callback = nil
close unless parser.keep_alive?
consume_queue
}
parser
end
end
end
|