http-parser-lite 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.2.0 (2012-06-25)
2
+
3
+ * Added Parser#pause, Parser#resume and Parser#paused?
4
+ * Rename HTTP::ParserError => HTTP::Parser::Error
5
+
1
6
  == 0.1.0 (2012-06-23)
2
7
 
3
8
  * Initial version.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # HTTP Parser Lite
2
2
 
3
- A Lite™ wrapper around the Joyent http-parser goodness for Ruby
3
+ A Lite™ wrapper around the Joyent [http-parser](https://github.com/joyent/http-parser) goodness for Ruby
4
4
 
5
5
  ## Install
6
6
 
@@ -69,9 +69,12 @@ HTTP::Parser
69
69
  #http_status
70
70
  #http_method
71
71
  #http_version
72
- #http_content_length
72
+
73
+ #pause
74
+ #resume
75
+ #paused?
73
76
  ```
74
77
 
75
78
  ## License
76
79
 
77
- [Creative Commons Attribution - CC BY](http://creativecommons.org/licenses/by/3.0)
80
+ MIT
@@ -98,6 +98,23 @@ VALUE rb_parser_reset(VALUE self) {
98
98
  return Qtrue;
99
99
  }
100
100
 
101
+ VALUE rb_parser_pause(VALUE self) {
102
+ http_parser *parser = rb_http_parser_handle(self);
103
+ http_parser_pause(parser, 1);
104
+ return Qtrue;
105
+ }
106
+
107
+ VALUE rb_parser_resume(VALUE self) {
108
+ http_parser *parser = rb_http_parser_handle(self);
109
+ http_parser_pause(parser, 0);
110
+ return Qtrue;
111
+ }
112
+
113
+ VALUE rb_parser_is_paused(VALUE self) {
114
+ http_parser *parser = rb_http_parser_handle(self);
115
+ return HTTP_PARSER_ERRNO(parser) == HPE_PAUSED ? Qtrue : Qfalse;
116
+ }
117
+
101
118
  VALUE rb_parser_http_method(VALUE self) {
102
119
  http_parser *parser = rb_http_parser_handle(self);
103
120
  return rb_str_new2(http_method_str(parser->method));
@@ -117,13 +134,16 @@ VALUE rb_parser_http_status(VALUE self) {
117
134
 
118
135
  Init_http_parser() {
119
136
  mHTTP = rb_define_module("HTTP");
120
- cParser = rb_define_class_under(mHTTP, "Parser", rb_cObject);
121
- eParserError = rb_define_class_under(mHTTP, "ParserError", rb_eStandardError);
137
+ cParser = rb_define_class_under(mHTTP, "Parser", rb_cObject);
138
+ eParserError = rb_define_class_under(cParser, "Error", rb_eStandardError);
122
139
 
123
140
  rb_define_alloc_func(cParser, rb_parser_allocate);
124
141
 
125
142
  rb_define_method(cParser, "<<", rb_parser_parse, 1);
126
143
  rb_define_method(cParser, "parse", rb_parser_parse, 1);
144
+ rb_define_method(cParser, "pause", rb_parser_pause, 0);
145
+ rb_define_method(cParser, "resume", rb_parser_resume, 0);
146
+ rb_define_method(cParser, "paused?", rb_parser_is_paused, 0);
127
147
  rb_define_method(cParser, "reset", rb_parser_reset, 0);
128
148
  rb_define_method(cParser, "http_method", rb_parser_http_method, 0);
129
149
  rb_define_method(cParser, "http_version", rb_parser_http_version, 0);
@@ -32,6 +32,39 @@ describe 'http-parser' do
32
32
  assert_equal 200, parser.http_status
33
33
  end
34
34
 
35
+
36
+ it 'should call callbacks' do
37
+ got = []
38
+ parser.on_message_begin {got << 's'}
39
+ parser.on_message_complete {got << 'e'}
40
+ parser.on_url {got << 'u'}
41
+ parser.on_header_field {got << 'f'}
42
+ parser.on_header_value {got << 'v'}
43
+ parser.on_body {got << 'b'}
44
+
45
+ parser << "POST / HTTP/1.0\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nhello"
46
+ assert_equal %w(s u f v f v b e), got
47
+ end
48
+
49
+ it 'should parse chunked response' do
50
+ got = []
51
+ parser.on_url {|data| got << data}
52
+ parser.on_header_field {|data| got << data}
53
+ parser.on_header_value {|data| got << data}
54
+ parser.on_body {|data| got << data}
55
+ parser << "HTTP/1.1 404 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n"
56
+ parser << "8\r\n"
57
+ parser << "document\r\n"
58
+ parser << "3\r\n"
59
+ parser << "not\r\n"
60
+ parser << "5\r\n"
61
+ parser << "found\r\n"
62
+ parser << "0\r\n"
63
+
64
+ assert_equal 404, parser.http_status
65
+ assert_equal %w(Content-Type text/plain Transfer-Encoding chunked document not found), got
66
+ end
67
+
35
68
  it 'should parse CONNECT' do
36
69
  got = []
37
70
  parser.on_url {|data| got << data}
@@ -45,6 +78,32 @@ describe 'http-parser' do
45
78
  end
46
79
 
47
80
  it 'should raise an error on invalid data' do
48
- assert_raises(HTTP::ParserError) { parser << "GET / foobar\r\n" }
81
+ assert_raises(HTTP::Parser::Error) { parser << "GET / foobar\r\n" }
82
+ end
83
+
84
+ it 'should pause parser' do
85
+ got = []
86
+ parser.on_url {|data| got << data; parser.pause }
87
+
88
+ assert_raises(HTTP::Parser::Error) do
89
+ parser << "GET /hello HTTP/1.0\r\n\r\n"
90
+ end
91
+
92
+ assert_equal %w(/hello), got
93
+ end
94
+
95
+ it 'should resume parser' do
96
+ got = []
97
+ parser.on_url {|data| got << data; parser.pause}
98
+ parser.on_header_field {|data| got << data}
99
+ parser.on_header_value {|data| got << data}
100
+
101
+ parser << "GET /hello "
102
+ assert parser.paused?
103
+ parser.resume
104
+ parser << "HTTP/1.0\r\n"
105
+ parser << "X-Test-Field: 1\r\n\r\n"
106
+
107
+ assert %w(/hello X-Test-Field 1), got
49
108
  end
50
109
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-parser-lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-23 00:00:00.000000000 Z
12
+ date: 2012-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake