File: headers.rb

package info (click to toggle)
ruby-protocol-http1 0.35.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 376 kB
  • sloc: ruby: 2,367; makefile: 4
file content (211 lines) | stat: -rw-r--r-- 5,341 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.

require "protocol/http1/connection"
require "connection_context"

describe Protocol::HTTP1::Connection do
	include_context ConnectionContext
	
	with "#write_response" do
		before do
			server.open!
		end
		
		def validate_headers!(expected_headers = self.headers)
			server.write_empty_body
			client.open!
			
			version, status, reason, headers, body = client.read_response("GET")
			
			expect(headers).to be == headers
		end
		
		with "a content-type header" do
			let(:headers) {{"content-type" => "text/plain"}}
			
			it "can parse the header" do
				server.write_response("HTTP/1.1", 200, headers)
				
				validate_headers!
			end
		end
		
		with "an empty header" do
			let(:headers) {{"nothing" => ""}}
			
			it "can parse the header" do
				server.write_response("HTTP/1.1", 200, headers)
				
				validate_headers!
			end
		end
		
		with "a header that contains tab characters" do
			let(:headers) {{"user-agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) \t\t\tChrome/55.0.2883.95 Safari/537.36"}}
			
			it "can parse the header" do
				server.write_response("HTTP/1.1", 200, headers)
				
				validate_headers!
			end
		end
		
		with "a header that contains obsolete folding whitespace" do
			let(:headers) {{"user-agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko)\n\tChrome/55.0.2883.95 Safari/537.36"}}
			
			it "rejects the response" do
				expect do
					server.write_response("HTTP/1.1", 200, headers)
				end.to raise_exception(Protocol::HTTP1::BadHeader)
			end
		end
		
		with "a header that contains invalid characters" do
			let(:headers) {{"user-agent" => "Mozilla\x00Hacker Browser"}}
			
			it "rejects the response" do
				expect do
					server.write_response("HTTP/1.1", 200, headers)
				end.to raise_exception(Protocol::HTTP1::BadHeader)
			end
		end
		
		with "a header that contains invalid high characters" do
			let(:headers) {{"user-agent" => "Mozilla\x7FHacker Browser"}}
			
			it "allows the response" do
				server.write_response("HTTP/1.1", 200, headers)
				
				validate_headers!
			end
		end
	end
	
	with "#read_request" do
		let(:headers) {Array.new}
		
		before do
			client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\n#{headers.join("\r\n")}\r\n\r\n"
			client.stream.close
		end
		
		with "a header that contains tab characters" do
			let(:headers) {[
				"user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) \t\t\tChrome/55.0.2883.95 Safari/537.36"
			]}
			
			it "can parse the header" do
				authority, method, target, version, headers, body = server.read_request
				
				expect(headers).to have_keys(
					"user-agent" => be == "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) \t\t\tChrome/55.0.2883.95 Safari/537.36"
				)
			end
		end
		
		with "a header that contains obsolete folding whitespace" do
			let(:headers) {[
				"user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko)\n\tChrome/55.0.2883.95 Safari/537.36"
			]}
			
			it "rejects the request" do
				expect do
					server.read_request
				end.to raise_exception(Protocol::HTTP1::BadHeader)
			end
		end
		
		with "a header that contains invalid characters" do
			let(:headers) {[
				"user-agent: Mozilla\x00Hacker Browser"
			]}
			
			it "rejects the request" do
				expect do
					server.read_request
				end.to raise_exception(Protocol::HTTP1::BadHeader)
			end
		end
		
		with "a header that contains invalid high characters" do
			let(:headers) {[
				"user-agent: Mozilla\x7FHacker Browser"
			]}
			
			it "allows the request" do
				authority, method, target, version, headers, body = server.read_request
				
				expect(headers).to have_keys(
					"user-agent" => be == "Mozilla\x7FHacker Browser"
				)
			end
		end
		
		with "a header that contains null character" do
			let(:headers) {[
				"user-agent: Mozilla\x00Hacker Browser"
			]}
			
			it "rejects the request" do
				expect do
					server.read_request
				end.to raise_exception(Protocol::HTTP1::BadHeader)
			end
		end
		
		with "a header that has empty value but includes optional whitespace" do
			let(:headers) {[
				"user-agent: "
			]}
			
			it "can parse the header" do
				authority, method, target, version, headers, body = server.read_request
				
				expect(headers).to have_keys(
					"user-agent" => be == ""
				)
			end
		end
		
		with "a header that has empty value" do
			let(:headers) {[
				"user-agent:"
			]}
			
			it "can parse the header" do
				authority, method, target, version, headers, body = server.read_request
				
				expect(headers).to have_keys(
					"user-agent" => be == ""
				)
			end
		end
		
		with "a header that has invalid name" do
			let(:headers) {[
				"invalid name: value"
			]}
			
			it "rejects the request" do
				expect do
					server.read_request
				end.to raise_exception(Protocol::HTTP1::BadHeader)
			end
		end
		
		with "a header that has empty name" do
			let(:headers) {[
				": value"
			]}
			
			it "rejects the request" do
				expect do
					server.read_request
				end.to raise_exception(Protocol::HTTP1::BadHeader)
			end
		end
	end
end