http-parser 1.0.3 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/ext/Rakefile +2 -2
- data/ext/http-parser/http_parser.c +791 -425
- data/ext/http-parser/http_parser.h +164 -33
- data/http-parser.gemspec +7 -5
- data/lib/http-parser.rb +2 -0
- data/lib/http-parser/errors.rb +6 -3
- data/lib/http-parser/ext.rb +2 -0
- data/lib/http-parser/parser.rb +41 -7
- data/lib/http-parser/types.rb +35 -11
- data/lib/http-parser/version.rb +3 -1
- data/spec/error_spec.rb +13 -13
- data/spec/instance_spec.rb +10 -30
- data/spec/parser_spec.rb +26 -26
- metadata +30 -31
data/lib/http-parser/types.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
module HttpParser
|
3
4
|
HTTP_MAX_HEADER_SIZE = (80 * 1024)
|
@@ -14,9 +15,10 @@ module HttpParser
|
|
14
15
|
:CHUNKED => 1 << 2,
|
15
16
|
:CONNECTION_KEEP_ALIVE => 1 << 3,
|
16
17
|
:CONNECTION_CLOSE => 1 << 4,
|
17
|
-
:
|
18
|
-
:
|
19
|
-
:
|
18
|
+
:CONNECTION_UPGRADE => 1 << 5,
|
19
|
+
:TRAILING => 1 << 6,
|
20
|
+
:UPGRADE => 1 << 7,
|
21
|
+
:SKIPBODY => 1 << 8
|
20
22
|
}
|
21
23
|
|
22
24
|
#
|
@@ -41,6 +43,10 @@ module HttpParser
|
|
41
43
|
:PROPPATCH,
|
42
44
|
:SEARCH,
|
43
45
|
:UNLOCK,
|
46
|
+
:BIND,
|
47
|
+
:REBIND,
|
48
|
+
:UNBIND,
|
49
|
+
:ACL,
|
44
50
|
# subversion
|
45
51
|
:REPORT,
|
46
52
|
:MKACTIVITY,
|
@@ -53,7 +59,12 @@ module HttpParser
|
|
53
59
|
:UNSUBSCRIBE,
|
54
60
|
# RFC-5789
|
55
61
|
:PATCH,
|
56
|
-
:PURGE
|
62
|
+
:PURGE,
|
63
|
+
# CalDAV
|
64
|
+
:MKCALENDAR,
|
65
|
+
# RFC-2068, section 19.6.1.2
|
66
|
+
:LINK,
|
67
|
+
:UNLINK
|
57
68
|
]
|
58
69
|
|
59
70
|
|
@@ -79,7 +90,7 @@ module HttpParser
|
|
79
90
|
:index, :uchar,
|
80
91
|
|
81
92
|
:nread, :uint32,
|
82
|
-
:content_length, :
|
93
|
+
:content_length, :uint64,
|
83
94
|
|
84
95
|
# READ-ONLY
|
85
96
|
:http_major, :ushort,
|
@@ -260,6 +271,16 @@ module HttpParser
|
|
260
271
|
::HttpParser.http_should_keep_alive(self) > 0
|
261
272
|
end
|
262
273
|
|
274
|
+
#
|
275
|
+
# Determines if a chunked response has completed
|
276
|
+
#
|
277
|
+
# @return [Boolean]
|
278
|
+
# Specifies whether the chunked response has completed
|
279
|
+
#
|
280
|
+
def final_chunk?
|
281
|
+
::HttpParser.http_body_is_final(self) > 0
|
282
|
+
end
|
283
|
+
|
263
284
|
#
|
264
285
|
# Halts the parser if called in a callback
|
265
286
|
#
|
@@ -294,18 +315,21 @@ module HttpParser
|
|
294
315
|
class Settings < FFI::Struct
|
295
316
|
layout :on_message_begin, :http_cb,
|
296
317
|
:on_url, :http_data_cb,
|
297
|
-
:
|
318
|
+
:on_status, :http_data_cb,
|
298
319
|
:on_header_field, :http_data_cb,
|
299
320
|
:on_header_value, :http_data_cb,
|
300
321
|
:on_headers_complete, :http_cb,
|
301
322
|
:on_body, :http_data_cb,
|
302
|
-
:on_message_complete, :http_cb
|
323
|
+
:on_message_complete, :http_cb,
|
324
|
+
:on_chunk_header, :http_cb,
|
325
|
+
:on_chunk_complete, :http_cb
|
303
326
|
end
|
304
327
|
|
305
328
|
|
306
|
-
attach_function :http_parser_init, [Instance.by_ref, :http_parser_type], :void
|
307
|
-
attach_function :http_parser_execute, [Instance.by_ref, Settings.by_ref, :pointer, :size_t], :size_t
|
329
|
+
attach_function :http_parser_init, [Instance.by_ref, :http_parser_type], :void
|
330
|
+
attach_function :http_parser_execute, [Instance.by_ref, Settings.by_ref, :pointer, :size_t], :size_t
|
331
|
+
attach_function :http_should_keep_alive, [Instance.by_ref], :int
|
308
332
|
|
309
|
-
|
310
|
-
attach_function :
|
333
|
+
# Checks if this is the final chunk of the body
|
334
|
+
attach_function :http_body_is_final, [Instance.by_ref], :int
|
311
335
|
end
|
data/lib/http-parser/version.rb
CHANGED
data/spec/error_spec.rb
CHANGED
@@ -6,25 +6,25 @@ describe HttpParser::Parser, "#initialize" do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it "should return true when error" do
|
9
|
-
subject.parse(@inst, "GETS / HTTP/1.1\r\n").
|
10
|
-
@inst.error
|
9
|
+
expect(subject.parse(@inst, "GETS / HTTP/1.1\r\n")).to eq(true)
|
10
|
+
expect(@inst.error?).to eq(true)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should return false on success" do
|
14
|
-
subject.parse(@inst, "GET / HTTP/1.1\r\n").
|
15
|
-
@inst.error
|
14
|
+
expect(subject.parse(@inst, "GET / HTTP/1.1\r\n")).to eq(false)
|
15
|
+
expect(@inst.error?).to eq(false)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "the error should be inspectable" do
|
19
|
-
subject.parse(@inst, "GETS / HTTP/1.1\r\n").
|
20
|
-
@inst.error.
|
21
|
-
@inst.error
|
19
|
+
expect(subject.parse(@inst, "GETS / HTTP/1.1\r\n")).to eq(true)
|
20
|
+
expect(@inst.error).to be_kind_of(::HttpParser::Error::INVALID_METHOD)
|
21
|
+
expect(@inst.error?).to eq(true)
|
22
22
|
end
|
23
23
|
|
24
24
|
it "raises different error types depending on the error" do
|
25
|
-
subject.parse(@inst, "GET / HTTP/23\r\n").
|
26
|
-
@inst.error.
|
27
|
-
@inst.error
|
25
|
+
expect(subject.parse(@inst, "GET / HTTP/23\r\n")).to eq(true)
|
26
|
+
expect(@inst.error).to be_kind_of(::HttpParser::Error::INVALID_VERSION)
|
27
|
+
expect(@inst.error?).to eq(true)
|
28
28
|
end
|
29
29
|
|
30
30
|
context "callback errors" do
|
@@ -35,10 +35,10 @@ describe HttpParser::Parser, "#initialize" do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should handle unhandled errors gracefully" do
|
38
|
-
subject.parse(@inst, "GET /foo?q=1 HTTP/1.1").
|
38
|
+
expect(subject.parse(@inst, "GET /foo?q=1 HTTP/1.1")).to eq(true)
|
39
39
|
|
40
|
-
@inst.error
|
41
|
-
@inst.error.
|
40
|
+
expect(@inst.error?).to eq(true)
|
41
|
+
expect(@inst.error).to be_kind_of(::HttpParser::Error::CALLBACK)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
data/spec/instance_spec.rb
CHANGED
@@ -1,23 +1,13 @@
|
|
1
1
|
require 'http-parser'
|
2
2
|
|
3
3
|
describe ::HttpParser::Instance, "#initialize" do
|
4
|
-
context "when initialized from a pointer" do
|
5
|
-
it "should not call http_parser_init" do
|
6
|
-
ptr = described_class.new.to_ptr
|
7
|
-
|
8
|
-
::HttpParser.should_not_receive(:http_parser_init)
|
9
|
-
|
10
|
-
described_class.new(ptr)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
4
|
context "when given a block" do
|
15
5
|
it "should yield the new Instance" do
|
16
6
|
expected = nil
|
17
7
|
|
18
8
|
described_class.new { |inst| expected = inst }
|
19
9
|
|
20
|
-
expected.
|
10
|
+
expect(expected).to be_kind_of(described_class)
|
21
11
|
end
|
22
12
|
|
23
13
|
it "should allow changing the parser type" do
|
@@ -25,25 +15,25 @@ describe ::HttpParser::Instance, "#initialize" do
|
|
25
15
|
inst.type = :request
|
26
16
|
end
|
27
17
|
|
28
|
-
inst.type.
|
18
|
+
expect(inst.type).to eq(:request)
|
29
19
|
end
|
30
20
|
end
|
31
21
|
|
32
22
|
describe "#type" do
|
33
23
|
it "should default to :both" do
|
34
|
-
subject.type.
|
24
|
+
expect(subject.type).to eq(:both)
|
35
25
|
end
|
36
26
|
|
37
27
|
it "should convert the type to a Symbol" do
|
38
28
|
subject[:type_flags] = ::HttpParser::TYPES[:request]
|
39
29
|
|
40
|
-
subject.type.
|
30
|
+
expect(subject.type).to eq(:request)
|
41
31
|
end
|
42
32
|
|
43
33
|
it "should extract the type from the type_flags field" do
|
44
34
|
subject[:type_flags] = ((0xff & ~0x3) | ::HttpParser::TYPES[:response])
|
45
35
|
|
46
|
-
subject.type.
|
36
|
+
expect(subject.type).to eq(:response)
|
47
37
|
end
|
48
38
|
end
|
49
39
|
|
@@ -51,7 +41,7 @@ describe ::HttpParser::Instance, "#initialize" do
|
|
51
41
|
it "should set the type" do
|
52
42
|
subject.type = :response
|
53
43
|
|
54
|
-
subject.type.
|
44
|
+
expect(subject.type).to eq(:response)
|
55
45
|
end
|
56
46
|
|
57
47
|
it "should not change flags" do
|
@@ -60,29 +50,19 @@ describe ::HttpParser::Instance, "#initialize" do
|
|
60
50
|
|
61
51
|
subject.type = :request
|
62
52
|
|
63
|
-
subject[:type_flags].
|
53
|
+
expect(subject[:type_flags]).to eq((flags | ::HttpParser::TYPES[:request]))
|
64
54
|
end
|
65
55
|
end
|
66
56
|
|
67
57
|
describe "#stop!" do
|
68
58
|
it "should throw :return, 1" do
|
69
|
-
|
59
|
+
expect { subject.stop! }.to throw_symbol(:return,1)
|
70
60
|
end
|
71
61
|
end
|
72
62
|
|
73
63
|
describe "#error!" do
|
74
64
|
it "should throw :return, -1" do
|
75
|
-
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "#reset!" do
|
80
|
-
it "should call http_parser_init" do
|
81
|
-
inst = described_class.new
|
82
|
-
|
83
|
-
::HttpParser.should_receive(:http_parser_init)
|
84
|
-
|
85
|
-
inst.reset!
|
65
|
+
expect { subject.error! }.to throw_symbol(:return,-1)
|
86
66
|
end
|
87
67
|
end
|
88
68
|
|
@@ -92,7 +72,7 @@ describe ::HttpParser::Instance, "#initialize" do
|
|
92
72
|
end
|
93
73
|
|
94
74
|
inst.reset!
|
95
|
-
inst.type.
|
75
|
+
expect(inst.type).to eq(:request)
|
96
76
|
end
|
97
77
|
end
|
98
78
|
|
data/spec/parser_spec.rb
CHANGED
@@ -15,7 +15,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
15
15
|
|
16
16
|
it "should trigger on a new request" do
|
17
17
|
subject.parse @inst, "GET / HTTP/1.1"
|
18
|
-
@begun.
|
18
|
+
expect(@begun).to eq(true)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -31,11 +31,11 @@ describe HttpParser::Parser, "#initialize" do
|
|
31
31
|
it "should pass the recognized url" do
|
32
32
|
subject.parse @inst, "GET "
|
33
33
|
|
34
|
-
@url.
|
34
|
+
expect(@url).to be_nil
|
35
35
|
|
36
36
|
subject.parse @inst, "#{expected} HTTP/1.1"
|
37
37
|
|
38
|
-
@url.
|
38
|
+
expect(@url).to eq(expected)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -51,11 +51,11 @@ describe HttpParser::Parser, "#initialize" do
|
|
51
51
|
it "should pass the recognized header-name" do
|
52
52
|
subject.parse @inst, "GET /foo HTTP/1.1\r\n"
|
53
53
|
|
54
|
-
@header_field.
|
54
|
+
expect(@header_field).to be_nil
|
55
55
|
|
56
56
|
subject.parse @inst, "#{expected}: example.com\r\n"
|
57
57
|
|
58
|
-
@header_field.
|
58
|
+
expect(@header_field).to eq(expected)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
@@ -71,11 +71,11 @@ describe HttpParser::Parser, "#initialize" do
|
|
71
71
|
it "should pass the recognized header-value" do
|
72
72
|
subject.parse @inst, "GET /foo HTTP/1.1\r\n"
|
73
73
|
|
74
|
-
@header_value.
|
74
|
+
expect(@header_value).to be_nil
|
75
75
|
|
76
76
|
subject.parse @inst, "Host: #{expected}\r\n"
|
77
77
|
|
78
|
-
@header_value.
|
78
|
+
expect(@header_value).to eq(expected)
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
@@ -89,11 +89,11 @@ describe HttpParser::Parser, "#initialize" do
|
|
89
89
|
it "should trigger on the last header" do
|
90
90
|
subject.parse @inst, "GET / HTTP/1.1\r\nHost: example.com\r\n"
|
91
91
|
|
92
|
-
@header_complete.
|
92
|
+
expect(@header_complete).to be_nil
|
93
93
|
|
94
94
|
subject.parse @inst, "\r\n"
|
95
95
|
|
96
|
-
@header_complete.
|
96
|
+
expect(@header_complete).to eq(true)
|
97
97
|
end
|
98
98
|
|
99
99
|
context "when #stop! is called" do
|
@@ -108,7 +108,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
108
108
|
it "should indicate there is no request body to parse" do
|
109
109
|
subject.parse @inst, "GET / HTTP/1.1\r\nHost: example.com\r\n\r\nBody"
|
110
110
|
|
111
|
-
@body.
|
111
|
+
expect(@body).to be_nil
|
112
112
|
end
|
113
113
|
end
|
114
114
|
end
|
@@ -125,11 +125,11 @@ describe HttpParser::Parser, "#initialize" do
|
|
125
125
|
it "should trigger on the body" do
|
126
126
|
subject.parse @inst, "POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n"
|
127
127
|
|
128
|
-
@body.
|
128
|
+
expect(@body).to be_nil
|
129
129
|
|
130
130
|
subject.parse @inst, "#{"%x" % expected.length}\r\n#{expected}"
|
131
131
|
|
132
|
-
@body.
|
132
|
+
expect(@body).to eq(expected)
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
@@ -143,11 +143,11 @@ describe HttpParser::Parser, "#initialize" do
|
|
143
143
|
it "should trigger at the end of the message" do
|
144
144
|
subject.parse @inst, "GET / HTTP/1.1\r\n"
|
145
145
|
|
146
|
-
@message_complete.
|
146
|
+
expect(@message_complete).to be_nil
|
147
147
|
|
148
148
|
subject.parse @inst, "Host: example.com\r\n\r\n"
|
149
149
|
|
150
|
-
@message_complete.
|
150
|
+
expect(@message_complete).to eq(true)
|
151
151
|
end
|
152
152
|
end
|
153
153
|
end
|
@@ -159,7 +159,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
159
159
|
it "should set the http_method field" do
|
160
160
|
subject.parse @inst, "#{expected} / HTTP/1.1\r\n"
|
161
161
|
|
162
|
-
@inst.http_method.
|
162
|
+
expect(@inst.http_method).to eq(expected)
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
@@ -174,7 +174,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
174
174
|
it "should set the http_major field" do
|
175
175
|
subject.parse @inst, "GET / HTTP/#{expected}."
|
176
176
|
|
177
|
-
@inst.http_major.
|
177
|
+
expect(@inst.http_major).to eq(expected)
|
178
178
|
end
|
179
179
|
end
|
180
180
|
|
@@ -186,7 +186,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
186
186
|
it "should set the http_major field" do
|
187
187
|
subject.parse @inst, "HTTP/#{expected}."
|
188
188
|
|
189
|
-
@inst.http_major.
|
189
|
+
expect(@inst.http_major).to eq(expected)
|
190
190
|
end
|
191
191
|
end
|
192
192
|
end
|
@@ -198,7 +198,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
198
198
|
it "should set the http_minor field" do
|
199
199
|
subject.parse @inst, "GET / HTTP/1.#{expected}\r\n"
|
200
200
|
|
201
|
-
@inst.http_minor.
|
201
|
+
expect(@inst.http_minor).to eq(expected)
|
202
202
|
end
|
203
203
|
end
|
204
204
|
|
@@ -210,7 +210,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
210
210
|
it "should set the http_major field" do
|
211
211
|
subject.parse @inst, "HTTP/1.#{expected} "
|
212
212
|
|
213
|
-
@inst.http_minor.
|
213
|
+
expect(@inst.http_minor).to eq(expected)
|
214
214
|
end
|
215
215
|
end
|
216
216
|
end
|
@@ -223,7 +223,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
223
223
|
end
|
224
224
|
|
225
225
|
it "should combine #http_major and #http_minor" do
|
226
|
-
@inst.http_version.
|
226
|
+
expect(@inst.http_version).to eq(expected)
|
227
227
|
end
|
228
228
|
end
|
229
229
|
|
@@ -234,7 +234,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
234
234
|
end
|
235
235
|
|
236
236
|
it "should not be set" do
|
237
|
-
@inst.http_status.
|
237
|
+
expect(@inst.http_status).to be_zero
|
238
238
|
end
|
239
239
|
end
|
240
240
|
|
@@ -248,7 +248,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
248
248
|
end
|
249
249
|
|
250
250
|
it "should set the http_status field" do
|
251
|
-
@inst.http_status.
|
251
|
+
expect(@inst.http_status).to eq(expected)
|
252
252
|
end
|
253
253
|
end
|
254
254
|
end
|
@@ -267,7 +267,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
267
267
|
end
|
268
268
|
|
269
269
|
it "should return true if the Upgrade header was set" do
|
270
|
-
@inst.upgrade
|
270
|
+
expect(@inst.upgrade?).to eq(true)
|
271
271
|
end
|
272
272
|
end
|
273
273
|
|
@@ -281,7 +281,7 @@ describe HttpParser::Parser, "#initialize" do
|
|
281
281
|
|
282
282
|
it "should trigger on a new request" do
|
283
283
|
subject.parse @inst, "GET /demo HTTP/1.1\r\n\r\nGET /demo HTTP/1.1\r\n\r\n"
|
284
|
-
@begun.
|
284
|
+
expect(@begun).to eq(2)
|
285
285
|
end
|
286
286
|
end
|
287
287
|
|
@@ -302,11 +302,11 @@ describe HttpParser::Parser, "#initialize" do
|
|
302
302
|
|
303
303
|
parser.parse @inst, "GET "
|
304
304
|
|
305
|
-
callbacks.url.
|
305
|
+
expect(callbacks.url).to be_nil
|
306
306
|
|
307
307
|
parser.parse @inst, "#{expected} HTTP/1.1"
|
308
308
|
|
309
|
-
callbacks.url.
|
309
|
+
expect(callbacks.url).to eq(expected)
|
310
310
|
end
|
311
311
|
end
|
312
312
|
end
|