http-parser 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ee24befc2a0a23054f11edb4379b9b6b1f8d21a
4
- data.tar.gz: 816280693464c8c5daa1c7fb20fe4954c3dd1a26
3
+ metadata.gz: c7c15574adec9aefd70f9ad96c542b70ce3b3727
4
+ data.tar.gz: ebcfbcfbe13fa5e1155e4c0a41f242ea96b1e8a7
5
5
  SHA512:
6
- metadata.gz: 1846519857ad32e3f688cf18f28fb3dd2cd33fa4c0a0909d6a2c934218b1ae5df30101b54a8b100cb8caaf28482a964e8e17ef5594abc91fddcb575d67ac5091
7
- data.tar.gz: 82c5dba81e1673ab22497e5e1adda9d6f89ab115795747bc64a0043ced1511880c65d003b379a4f35e89976d92bdf793933bf196b2002876edb431f2c2dcc4c6
6
+ metadata.gz: 0e3a987b169359afe6229340fb4c28e74c411beb302f767bb45f836cc8579598c1d9b89286e9aeb4bf3c114d4fed9b87b5952510c36da676b063e4a60007ee4c
7
+ data.tar.gz: 6a531fd9df9c4f011a7163eb1e87dc260fa0a9ee6c201c9e8156f7b86db0b636f579ca3e094535d16a1bc0541c909f276026811de2ee9f5176177c17cb19f05d
@@ -248,6 +248,7 @@ enum state
248
248
  , s_res_http_minor
249
249
  , s_res_first_status_code
250
250
  , s_res_status_code
251
+ , s_res_status_start
251
252
  , s_res_status
252
253
  , s_res_line_almost_done
253
254
 
@@ -581,6 +582,7 @@ size_t http_parser_execute (http_parser *parser,
581
582
  const char *header_value_mark = 0;
582
583
  const char *url_mark = 0;
583
584
  const char *body_mark = 0;
585
+ const char *status_mark = 0;
584
586
 
585
587
  /* We're in an error state. Don't bother doing anything. */
586
588
  if (HTTP_PARSER_ERRNO(parser) != HPE_OK) {
@@ -627,6 +629,9 @@ size_t http_parser_execute (http_parser *parser,
627
629
  case s_req_fragment:
628
630
  url_mark = data;
629
631
  break;
632
+ case s_res_status:
633
+ status_mark = data;
634
+ break;
630
635
  }
631
636
 
632
637
  for (p=data; p != data + len; p++) {
@@ -833,7 +838,7 @@ size_t http_parser_execute (http_parser *parser,
833
838
  if (!IS_NUM(ch)) {
834
839
  switch (ch) {
835
840
  case ' ':
836
- parser->state = s_res_status;
841
+ parser->state = s_res_status_start;
837
842
  break;
838
843
  case CR:
839
844
  parser->state = s_res_line_almost_done;
@@ -859,24 +864,42 @@ size_t http_parser_execute (http_parser *parser,
859
864
  break;
860
865
  }
861
866
 
867
+ case s_res_status_start:
868
+ {
869
+ if (ch == CR) {
870
+ parser->state = s_res_line_almost_done;
871
+ break;
872
+ }
873
+
874
+ if (ch == LF) {
875
+ parser->state = s_header_field_start;
876
+ break;
877
+ }
878
+
879
+ MARK(status);
880
+ parser->state = s_res_status;
881
+ parser->index = 0;
882
+ break;
883
+ }
884
+
862
885
  case s_res_status:
863
- /* the human readable status. e.g. "NOT FOUND"
864
- * we are not humans so just ignore this */
865
886
  if (ch == CR) {
866
887
  parser->state = s_res_line_almost_done;
888
+ CALLBACK_DATA(status);
867
889
  break;
868
890
  }
869
891
 
870
892
  if (ch == LF) {
871
893
  parser->state = s_header_field_start;
894
+ CALLBACK_DATA(status);
872
895
  break;
873
896
  }
897
+
874
898
  break;
875
899
 
876
900
  case s_res_line_almost_done:
877
901
  STRICT_CHECK(ch != LF);
878
902
  parser->state = s_header_field_start;
879
- CALLBACK_NOTIFY(status_complete);
880
903
  break;
881
904
 
882
905
  case s_start_req:
@@ -1854,12 +1877,14 @@ size_t http_parser_execute (http_parser *parser,
1854
1877
  assert(((header_field_mark ? 1 : 0) +
1855
1878
  (header_value_mark ? 1 : 0) +
1856
1879
  (url_mark ? 1 : 0) +
1857
- (body_mark ? 1 : 0)) <= 1);
1880
+ (body_mark ? 1 : 0) +
1881
+ (status_mark ? 1 : 0)) <= 1);
1858
1882
 
1859
1883
  CALLBACK_DATA_NOADVANCE(header_field);
1860
1884
  CALLBACK_DATA_NOADVANCE(header_value);
1861
1885
  CALLBACK_DATA_NOADVANCE(url);
1862
1886
  CALLBACK_DATA_NOADVANCE(body);
1887
+ CALLBACK_DATA_NOADVANCE(status);
1863
1888
 
1864
1889
  return len;
1865
1890
 
@@ -26,7 +26,7 @@ extern "C" {
26
26
 
27
27
  /* Also update SONAME in the Makefile whenever you change these. */
28
28
  #define HTTP_PARSER_VERSION_MAJOR 2
29
- #define HTTP_PARSER_VERSION_MINOR 1
29
+ #define HTTP_PARSER_VERSION_MINOR 2
30
30
  #define HTTP_PARSER_VERSION_PATCH 0
31
31
 
32
32
  #include <sys/types.h>
@@ -143,13 +143,13 @@ enum flags
143
143
  \
144
144
  /* Callback-related errors */ \
145
145
  XX(CB_message_begin, "the on_message_begin callback failed") \
146
- XX(CB_status_complete, "the on_status_complete callback failed") \
147
146
  XX(CB_url, "the on_url callback failed") \
148
147
  XX(CB_header_field, "the on_header_field callback failed") \
149
148
  XX(CB_header_value, "the on_header_value callback failed") \
150
149
  XX(CB_headers_complete, "the on_headers_complete callback failed") \
151
150
  XX(CB_body, "the on_body callback failed") \
152
151
  XX(CB_message_complete, "the on_message_complete callback failed") \
152
+ XX(CB_status, "the on_status callback failed") \
153
153
  \
154
154
  /* Parsing-related errors */ \
155
155
  XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
@@ -224,7 +224,7 @@ struct http_parser {
224
224
  struct http_parser_settings {
225
225
  http_cb on_message_begin;
226
226
  http_data_cb on_url;
227
- http_cb on_status_complete;
227
+ http_data_cb on_status;
228
228
  http_data_cb on_header_field;
229
229
  http_data_cb on_header_value;
230
230
  http_cb on_headers_complete;
@@ -36,13 +36,13 @@ module HttpParser
36
36
  :OK => Error::OK,
37
37
 
38
38
  :CB_message_begin => Error::CALLBACK,
39
- :CB_status_complete => Error::CALLBACK,
40
39
  :CB_url => Error::CALLBACK,
41
40
  :CB_header_field => Error::CALLBACK,
42
41
  :CB_header_value => Error::CALLBACK,
43
42
  :CB_headers_complete => Error::CALLBACK,
44
43
  :CB_body => Error::CALLBACK,
45
44
  :CB_message_complete => Error::CALLBACK,
45
+ :CB_status => Error::CALLBACK,
46
46
 
47
47
  :INVALID_EOF_STATE => Error::INVALID_EOF_STATE,
48
48
  :HEADER_OVERFLOW => Error::HEADER_OVERFLOW,
@@ -1,7 +1,7 @@
1
1
 
2
2
  module HttpParser
3
3
  class Parser
4
- CALLBACKS = [:on_message_begin, :on_url, :on_status_complete, :on_header_field, :on_header_value, :on_headers_complete, :on_body, :on_message_complete]
4
+ CALLBACKS = [:on_message_begin, :on_url, :on_status, :on_header_field, :on_header_value, :on_headers_complete, :on_body, :on_message_complete]
5
5
 
6
6
  #
7
7
  # Returns a new request/response instance variable
@@ -72,10 +72,10 @@ module HttpParser
72
72
  # @yieldparam [HttpParser::Instance] instance
73
73
  # The state so far of the request / response being processed.
74
74
  #
75
- def on_status_complete(&block)
76
- cb = Callback.new(&block)
77
- @callbacks[:on_status_complete] = cb
78
- @settings[:on_status_complete] = cb
75
+ def on_status(&block)
76
+ cb = DataCallback.new(&block)
77
+ @callbacks[:on_status] = cb
78
+ @settings[:on_status] = cb
79
79
  end
80
80
 
81
81
  #
@@ -294,7 +294,7 @@ module HttpParser
294
294
  class Settings < FFI::Struct
295
295
  layout :on_message_begin, :http_cb,
296
296
  :on_url, :http_data_cb,
297
- :on_status_complete, :http_cb,
297
+ :on_status, :http_data_cb,
298
298
  :on_header_field, :http_data_cb,
299
299
  :on_header_value, :http_data_cb,
300
300
  :on_headers_complete, :http_cb,
@@ -1,3 +1,3 @@
1
1
  module HttpParser
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
@@ -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").should be_true
10
- @inst.error?.should be_true
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").should be_false
15
- @inst.error?.should be_false
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").should be_true
20
- @inst.error.should be_kind_of(::HttpParser::Error::INVALID_METHOD)
21
- @inst.error?.should be_true
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").should be_true
26
- @inst.error.should be_kind_of(::HttpParser::Error::INVALID_VERSION)
27
- @inst.error?.should be_true
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").should be_true
38
+ expect(subject.parse(@inst, "GET /foo?q=1 HTTP/1.1")).to eq(true)
39
39
 
40
- @inst.error?.should be_true
41
- @inst.error.should be_kind_of(::HttpParser::Error::CALLBACK)
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
@@ -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.should be_kind_of(described_class)
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.should == :request
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.should == :both
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.should == :request
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.should == :response
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.should == :response
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].should == (flags | ::HttpParser::TYPES[:request])
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
- lambda { subject.stop! }.should throw_symbol(:return,1)
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
- lambda { subject.error! }.should throw_symbol(:return,-1)
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.should == :request
75
+ expect(inst.type).to eq(:request)
96
76
  end
97
77
  end
98
78
 
@@ -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.should be_true
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.should be_nil
34
+ expect(@url).to be_nil
35
35
 
36
36
  subject.parse @inst, "#{expected} HTTP/1.1"
37
37
 
38
- @url.should == expected
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.should be_nil
54
+ expect(@header_field).to be_nil
55
55
 
56
56
  subject.parse @inst, "#{expected}: example.com\r\n"
57
57
 
58
- @header_field.should == expected
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.should be_nil
74
+ expect(@header_value).to be_nil
75
75
 
76
76
  subject.parse @inst, "Host: #{expected}\r\n"
77
77
 
78
- @header_value.should == expected
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.should be_nil
92
+ expect(@header_complete).to be_nil
93
93
 
94
94
  subject.parse @inst, "\r\n"
95
95
 
96
- @header_complete.should be_true
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.should be_nil
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.should be_nil
128
+ expect(@body).to be_nil
129
129
 
130
130
  subject.parse @inst, "#{"%x" % expected.length}\r\n#{expected}"
131
131
 
132
- @body.should == expected
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.should be_nil
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.should be_true
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.should == expected
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.should == expected
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.should == expected
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.should == expected
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.should == expected
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.should == expected
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.should be_zero
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.should == expected
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?.should be_true
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.should == 2
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.should be_nil
305
+ expect(callbacks.url).to be_nil
306
306
 
307
307
  parser.parse @inst, "#{expected} HTTP/1.1"
308
308
 
309
- callbacks.url.should == expected
309
+ expect(callbacks.url).to eq(expected)
310
310
  end
311
311
  end
312
312
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-25 00:00:00.000000000 Z
11
+ date: 2014-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi-compiler