File: bad.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 (163 lines) | stat: -rw-r--r-- 3,162 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
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023-2024, by Samuel Williams.

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

describe Protocol::HTTP1::Connection do
	include_context ConnectionContext
	
	before do
		# We use a thread here, as writing to the stream may block, e.g. if the input is big enough.
		@writer = Thread.new do
			client.stream.write(input)
			client.stream.close
		end
	end
	
	after do
		@writer.join
	end
	
	with "invalid hexadecimal content-length" do
		def input
			<<~HTTP.gsub("\n", "\r\n")
			POST / HTTP/1.1
			Host: a.com
			Content-Length: 0x10
			Connection: close
			
			0123456789abcdef
			HTTP
		end
		
		it "should fail to parse the request body" do
			expect do
				server.read_request
			end.to raise_exception(Protocol::HTTP1::BadRequest)
		end
	end
	
	with "invalid +integer content-length" do
		def input
			<<~HTTP.gsub("\n", "\r\n")
			POST / HTTP/1.1
			Host: a.com
			Content-Length: +16
			Connection: close
			
			0123456789abcdef
			HTTP
		end
		
		it "should fail to parse the request body" do
			expect do
				server.read_request
			end.to raise_exception(Protocol::HTTP1::BadRequest)
		end
	end
	
	with "invalid -integer content-length" do
		def input
			<<~HTTP.gsub("\n", "\r\n")
			POST / HTTP/1.1
			Host: a.com
			Content-Length: -16
			Connection: close
			
			0123456789abcdef
			HTTP
		end
		
		it "should fail to parse the request body" do
			expect do
				server.read_request
			end.to raise_exception(Protocol::HTTP1::BadRequest)
		end
	end
	
	with "invalid hexidecimal chunk size" do
		def input
			<<~HTTP.gsub("\n", "\r\n")
			POST / HTTP/1.1
			Host: a.com
			Transfer-Encoding: chunked
			Connection: close
			
			0x10
			0123456789abcdef
			0
			HTTP
		end
		
		it "should fail to parse the request body" do
			authority, method, target, version, headers, body = server.read_request
			
			expect(body).to be_a(Protocol::HTTP1::Body::Chunked)
			
			expect do
				body.read
			end.to raise_exception(Protocol::HTTP1::BadRequest)
		end
	end
	
	with "invalid +integer chunk size" do
		def input
			<<~HTTP.gsub("\n", "\r\n")
			POST / HTTP/1.1
			Host: a.com
			Transfer-Encoding: chunked
			Connection: close
			
			+10
			0123456789abcdef
			0
			HTTP
		end
		
		it "should fail to parse the request body" do
			authority, method, target, version, headers, body = server.read_request
			
			expect(body).to be_a(Protocol::HTTP1::Body::Chunked)
			
			expect do
				body.read
			end.to raise_exception(Protocol::HTTP1::BadRequest)
		end
	end
	
	with "line length exceeding the limit" do
		def input
			<<~HTTP.gsub("\n", "\r\n")
			POST / HTTP/1.1
			Host: a.com
			Connection: close
			Long-Header: #{'a' * 8192}
			
			HTTP
		end
		
		it "should fail to parse the request" do
			expect do
				server.read_request
			end.to raise_exception(Protocol::HTTP1::LineLengthError)
		end
	end
	
	with "incomplete headers" do
		def input
			<<~HTTP.gsub("\n", "\r\n")
			POST / HTTP/1.1
			Host: a.com
			HTTP
		end
		
		it "should fail to parse the request" do
			expect do
				server.read_request
			end.to raise_exception(EOFError)
		end
	end
end