Skip to content
Browse files

added Parser#error and Parser#error?

* version bump to v0.3.0
* updated gemspec, changelog & readme
  • Loading branch information...
1 parent 3cdca6c commit 64552f6a19908efb4b463f88b6c5f88d1e6db6c2 @deepfryed committed
Showing with 41 additions and 3 deletions.
  1. +2 −1 CHANGELOG
  2. +6 −0 README.md
  3. +13 −0 ext/http-parser/ruby_http_parser.c
  4. +2 −2 http-parser-lite.gemspec
  5. +18 −0 test/test_http_parser.rb
View
3 CHANGELOG
@@ -1,6 +1,7 @@
-== 0.3.0 (2012-06-25)
+== 0.3.0 (2012-06-27)
* Added optional type argument to Parser#initialize and Parser#reset
+* Added Parser#error? and Parser#error
== 0.2.0 (2012-06-25)
View
6 README.md
@@ -73,12 +73,18 @@ HTTP::Parser
#resume
#paused?
+ #error?
+ #error
Constants:
* HTTP::Parser::TYPE_REQUEST
* HTTP::Parser::TYPE_RESPONSE
* HTTP::Parser::TYPE_BOTH
+
+Exceptions:
+
+* HTTP::Parser::Error
```
## License
View
13 ext/http-parser/ruby_http_parser.c
@@ -128,6 +128,17 @@ VALUE rb_parser_http_status(VALUE self) {
return INT2NUM(parser->status_code);
}
+VALUE rb_parser_error_q(VALUE self) {
+ http_parser *parser = rb_http_parser_handle(self);
+ return HTTP_PARSER_ERRNO(parser) != HPE_OK ? Qtrue : Qfalse;
+}
+
+VALUE rb_parser_error(VALUE self) {
+ http_parser *parser = rb_http_parser_handle(self);
+ int errno = HTTP_PARSER_ERRNO(parser);
+ return errno != HPE_OK ? rb_str_new2(http_errno_description(errno)) : Qnil;
+}
+
Init_http_parser() {
mHTTP = rb_define_module("HTTP");
cParser = rb_define_class_under(mHTTP, "Parser", rb_cObject);
@@ -140,6 +151,8 @@ Init_http_parser() {
rb_define_method(cParser, "pause", rb_parser_pause, 0);
rb_define_method(cParser, "resume", rb_parser_resume, 0);
rb_define_method(cParser, "paused?", rb_parser_is_paused, 0);
+ rb_define_method(cParser, "error?", rb_parser_error_q, 0);
+ rb_define_method(cParser, "error", rb_parser_error, 0);
rb_define_method(cParser, "http_method", rb_parser_http_method, 0);
rb_define_method(cParser, "http_version", rb_parser_http_version, 0);
rb_define_method(cParser, "http_status", rb_parser_http_status, 0);
View
4 http-parser-lite.gemspec
@@ -6,11 +6,11 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Bharanee Rathna"]
- s.date = "2012-06-25"
+ s.date = "2012-06-27"
s.description = "A lite ruby wrapper around Joyent http-parser"
s.email = ["deepfryed@gmail.com"]
s.extensions = ["ext/http-parser/extconf.rb"]
- s.files = ["ext/http-parser/ruby_http_parser.c", "ext/http-parser/http_parser.c", "ext/http-parser/http_parser.h", "ext/http-parser/extconf.rb", "test/helper.rb", "test/test_http_parser.rb", "lib/http-parser.rb", "README.md", "CHANGELOG"]
+ s.files = ["ext/http-parser/http_parser.c", "ext/http-parser/ruby_http_parser.c", "ext/http-parser/http_parser.h", "ext/http-parser/extconf.rb", "test/helper.rb", "test/test_http_parser.rb", "lib/http-parser.rb", "README.md", "CHANGELOG"]
s.homepage = "http://github.com/deepfryed/http-parser-lite"
s.require_paths = ["lib", "ext"]
s.rubygems_version = "1.8.24"
View
18 test/test_http_parser.rb
@@ -135,4 +135,22 @@
end
assert parser.reset(0)
end
+
+ it 'should expose parser error status' do
+ parser.reset(HTTP::Parser::TYPE_REQUEST)
+
+ assert parser << "GET / HTTP/1.1\r\n\r\n"
+ assert !parser.error?
+
+ assert_raises(HTTP::Parser::Error) do
+ parser << "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
+ end
+ assert parser.error?
+ assert_match %r{invalid http method}i, parser.error
+
+ # reset should give you a clean slate
+ parser.reset(HTTP::Parser::TYPE_REQUEST)
+ assert !parser.error?
+ assert !parser.error
+ end
end

0 comments on commit 64552f6

Please sign in to comment.
Something went wrong with that request. Please try again.