yakischloba-http-parser 0.0.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.
data/test/request.rb ADDED
@@ -0,0 +1,82 @@
1
+ describe "http-parser request parsing" do
2
+
3
+ REQUEST_BUFFER = "GET /favicon.ico?q=search#hey HTTP/1.1\r\n" +
4
+ "Host: 0.0.0.0=5000\r\n" +
5
+ "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0\r\n" +
6
+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
7
+ "Accept-Language: en-us,en;q=0.5\r\n" +
8
+ "Accept-Encoding: gzip,deflate\r\n" +
9
+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
10
+ "Keep-Alive: 300\r\n" +
11
+ "Connection: keep-alive\r\n\r\n"
12
+
13
+ REQUEST_HEADERS = {
14
+ "Host" => "0.0.0.0=5000",
15
+ "User-Agent" => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0",
16
+ "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
17
+ "Accept-Language" => "en-us,en;q=0.5",
18
+ "Accept-Encoding" => "gzip,deflate",
19
+ "Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
20
+ "Keep-Alive" => "300",
21
+ "Connection" => "keep-alive"
22
+ }
23
+
24
+ REQUEST_PATH = "/favicon.ico"
25
+ REQUEST_QUERY = "q=search"
26
+ REQUEST_FRAGMENT = "hey"
27
+
28
+ @headers = {}
29
+
30
+ @parser = HttpParser.new(:HTTP_REQUEST)
31
+ @parser.on_message_begin { @message_begun = true }
32
+ @parser.on_path {|f| @path = f }
33
+ @parser.on_query_string {|q| @query_string = q }
34
+ @parser.on_fragment {|f| @fragment = f }
35
+ @parser.on_header_field {|f| @current_field = f }
36
+ @parser.on_header_value {|v| @headers[@current_field] = v }
37
+ @parser.on_headers_complete { @headers_complete = true }
38
+ @parser.on_message_complete { @message_complete = true }
39
+ @parser.execute(REQUEST_BUFFER)
40
+
41
+ it "calls back when beginning a message" do
42
+ @message_begun.should.equal true
43
+ end
44
+
45
+ it "calls back with the request path" do
46
+ @path.should.equal REQUEST_PATH
47
+ end
48
+
49
+ it "calls back with the query string" do
50
+ @query_string.should.equal REQUEST_QUERY
51
+ end
52
+
53
+ it "calls back with fragments" do
54
+ @fragment.should.equal REQUEST_FRAGMENT
55
+ end
56
+
57
+ it "calls back with header field names and values" do
58
+ @headers.should.equal REQUEST_HEADERS
59
+ end
60
+
61
+ it "calls back when header parsing is complete" do
62
+ @headers_complete.should.equal true
63
+ end
64
+
65
+ it "calls back when parsing of the message is complete" do
66
+ @message_complete.should.equal true
67
+ end
68
+
69
+ it "aborts in the middle of parsing if the user tells it to" do
70
+ @headers = {}
71
+ @path = nil
72
+ @fragment = nil
73
+ @query_string = nil
74
+ @parser.on_query_string {|q| @query_string = q; @parser.abort! }
75
+ @parser.execute(REQUEST_BUFFER)
76
+ @path.should.equal REQUEST_PATH
77
+ @query_string.should.equal REQUEST_QUERY
78
+ @fragment.should.equal nil
79
+ @headers.should == {}
80
+ end
81
+
82
+ end
data/test/response.rb ADDED
@@ -0,0 +1,79 @@
1
+ describe "http-parser response parsing" do
2
+
3
+ RESPONSE_BUFFER = "HTTP/1.1 200 OK\r\nDate: Fri, 07 Aug 2009 05:00:06 GMT\r\n" +
4
+ "Server: Apache\r\n" +
5
+ "X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.4\r\n" +
6
+ "X-Runtime: 0.01179\r\n" +
7
+ "ETag: \"0adbc2039ae89671f7c3511a9ffdbab2\"\r\n" +
8
+ "Cache-Control: private, max-age=0, must-revalidate\r\n" +
9
+ "Set-Cookie: _session_id=45a375a8cd2af6722cac1b46fbc8c5a9; path=/\r\n" +
10
+ "Content-Length: 17\r\n" +
11
+ "Status: 200 OK\r\n" +
12
+ "Vary: Accept-Encoding\r\n" +
13
+ "Connection: close\r\n" +
14
+ "Content-Type: text/html; charset=utf-8\r\n\r\n" +
15
+ "I AM A FAKE BODY\n"
16
+
17
+ RESPONSE_BODY = "I AM A FAKE BODY\n"
18
+ RESPONSE_HEADERS = {
19
+ "Date" => "Fri, 07 Aug 2009 05:00:06 GMT",
20
+ "Server" => "Apache",
21
+ "X-Powered-By" => "Phusion Passenger (mod_rails/mod_rack) 2.2.4",
22
+ "X-Runtime" => "0.01179",
23
+ "ETag" => "\"0adbc2039ae89671f7c3511a9ffdbab2\"",
24
+ "Cache-Control" => "private, max-age=0, must-revalidate",
25
+ "Set-Cookie" => "_session_id=45a375a8cd2af6722cac1b46fbc8c5a9; path=/",
26
+ "Content-Length" => "17",
27
+ "Status" => "200 OK",
28
+ "Vary" => "Accept-Encoding",
29
+ "Connection" => "close",
30
+ "Content-Type" => "text/html; charset=utf-8"
31
+ }
32
+
33
+ @headers = {}
34
+
35
+ @parser = HttpParser.new(:HTTP_RESPONSE)
36
+ @parser.on_message_begin { @message_begun = true }
37
+ @parser.on_header_field {|f| @current_field = f }
38
+ @parser.on_header_value {|v| @headers[@current_field] = v }
39
+ @parser.on_headers_complete { @headers_complete = true }
40
+ @parser.on_body {|b| @body = b }
41
+ @parser.on_message_complete { @message_complete = true }
42
+ @parser.execute(RESPONSE_BUFFER)
43
+
44
+ it "calls back when beginning a message" do
45
+ @message_begun.should.equal true
46
+ end
47
+
48
+ it "calls back with header field names and values" do
49
+ @headers.should.equal RESPONSE_HEADERS
50
+ end
51
+
52
+ it "calls back when header parsing is complete" do
53
+ @headers_complete.should.equal true
54
+ end
55
+
56
+ it "calls back with the body" do
57
+ @body.should.equal RESPONSE_BODY
58
+ end
59
+
60
+ it "calls back when parsing of the message is complete" do
61
+ @message_complete.should.equal true
62
+ end
63
+
64
+ it "aborts in the middle of parsing if the user tells it to" do
65
+ @headers = {}
66
+ @count = 0
67
+ @parser.on_header_value do |v|
68
+ if @count == 2
69
+ @parser.abort!
70
+ else
71
+ @headers[@current_field] = v
72
+ @count += 1
73
+ end
74
+ end
75
+ @parser.execute(RESPONSE_BUFFER)
76
+ @headers.should == {"Date" => "Fri, 07 Aug 2009 05:00:06 GMT", "Server" => "Apache"}
77
+ end
78
+
79
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yakischloba-http-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Dahl
8
+ - Zed Shaw
9
+ - Jake Douglas
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-08-07 00:00:00 -07:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rake
19
+ type: :runtime
20
+ version_requirement:
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: "0"
26
+ version:
27
+ - !ruby/object:Gem::Dependency
28
+ name: ffi
29
+ type: :runtime
30
+ version_requirement:
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ version:
37
+ - !ruby/object:Gem::Dependency
38
+ name: bacon
39
+ type: :runtime
40
+ version_requirement:
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ description: FFI binding to ry's http-parser
48
+ email: jakecdouglas@gmail.com
49
+ executables: []
50
+
51
+ extensions:
52
+ - Rakefile
53
+ extra_rdoc_files: []
54
+
55
+ files:
56
+ - http-parser.gemspec
57
+ - Rakefile
58
+ - lib/http-parser.rb
59
+ - src/http_parser.h
60
+ - src/http_parser.rl
61
+ - src/LICENSE
62
+ - src/Makefile
63
+ - src/README.md
64
+ - src/test.c
65
+ - test/request.rb
66
+ - test/response.rb
67
+ has_rdoc: true
68
+ homepage: http://www.github.com/yakischloba/http-parser-ffi
69
+ licenses:
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.5
91
+ signing_key:
92
+ specification_version: 2
93
+ summary: FFI binding to ry's http-parser
94
+ test_files: []
95
+