yakischloba-http-parser 0.0.2 → 0.0.3

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/http-parser.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "http-parser"
3
- s.version = "0.0.2"
4
- s.date = "2009-08-07"
3
+ s.version = "0.0.3"
4
+ s.date = "2009-08-08"
5
5
  s.authors = ["Ryan Dahl", "Zed Shaw", "Jake Douglas"]
6
6
  s.email = "jakecdouglas@gmail.com"
7
7
  s.has_rdoc = true
data/lib/http-parser.rb CHANGED
@@ -7,25 +7,27 @@ class HttpParser
7
7
  callback :http_data_cb, [:pointer, :pointer, :size_t], :int
8
8
  callback :http_cb, [:pointer], :int
9
9
 
10
- # Request methods
11
- HTTP_COPY = 0x0001
12
- HTTP_DELETE = 0x0002
13
- HTTP_GET = 0x0004
14
- HTTP_HEAD = 0x0008
15
- HTTP_LOCK = 0x0010
16
- HTTP_MKCOL = 0x0020
17
- HTTP_MOVE = 0x0040
18
- HTTP_OPTIONS = 0x0080
19
- HTTP_POST = 0x0100
20
- HTTP_PROPFIND = 0x0200
21
- HTTP_PROPPATCH = 0x0400
22
- HTTP_PUT = 0x0800
23
- HTTP_TRACE = 0x1000
24
- HTTP_UNLOCK = 0x2000
25
-
26
- # Transfer encodings
27
- HTTP_IDENTITY = 0x01
28
- HTTP_CHUNKED = 0x02
10
+ METHODS = {
11
+ 0x0001 => :COPY,
12
+ 0x0002 => :DELETE,
13
+ 0x0004 => :GET,
14
+ 0x0008 => :HEAD,
15
+ 0x0010 => :LOCK,
16
+ 0x0020 => :MKCOL,
17
+ 0x0040 => :MOVE,
18
+ 0x0080 => :OPTIONS,
19
+ 0x0100 => :POST,
20
+ 0x0200 => :PROPFIND,
21
+ 0x0400 => :PROPPATCH,
22
+ 0x0800 => :PUT,
23
+ 0x1000 => :TRACE,
24
+ 0x2000 => :UNLOCK
25
+ }
26
+
27
+ ENCODINGS = {
28
+ 0x01 => :IDENTITY,
29
+ 0x02 => :CHUNKED
30
+ }
29
31
 
30
32
  enum :http_parser_type, [:HTTP_REQUEST, :HTTP_RESPONSE]
31
33
 
@@ -100,6 +102,38 @@ class HttpParser
100
102
  @abort = true
101
103
  end
102
104
 
105
+ def method
106
+ METHODS[@parser[:method]]
107
+ end
108
+
109
+ def status_code
110
+ @parser[:status_code]
111
+ end
112
+
113
+ def transfer_encoding
114
+ ENCODINGS[@parser[:transfer_encoding]]
115
+ end
116
+
117
+ def version_major
118
+ @parser[:version_major]
119
+ end
120
+
121
+ def version_minor
122
+ @parser[:version_minor]
123
+ end
124
+
125
+ def version
126
+ [version_major, version_minor]
127
+ end
128
+
129
+ def content_length
130
+ @parser[:content_length]
131
+ end
132
+
133
+ def keep_alive?
134
+ @parser[:keep_alive].zero? ? false : true
135
+ end
136
+
103
137
  def on_message_begin(proc=nil, &blk)
104
138
  proc ||= blk
105
139
  @on_message_begin = Proc.new do |ptr|
data/test/request.rb CHANGED
@@ -35,7 +35,15 @@ describe "http-parser request parsing" do
35
35
  @parser.on_header_field {|f| @current_field = f }
36
36
  @parser.on_header_value {|v| @headers[@current_field] = v }
37
37
  @parser.on_headers_complete { @headers_complete = true }
38
- @parser.on_message_complete { @message_complete = true }
38
+ @parser.on_message_complete do
39
+ @message_complete = true
40
+ @content_length = @parser.content_length
41
+ @status_code = @parser.status_code
42
+ @method = @parser.method
43
+ @version = @parser.version
44
+ @keep_alive = @parser.keep_alive?
45
+ @encoding = @parser.transfer_encoding
46
+ end
39
47
  @parser.execute(REQUEST_BUFFER)
40
48
 
41
49
  it "calls back when beginning a message" do
@@ -62,6 +70,21 @@ describe "http-parser request parsing" do
62
70
  @headers_complete.should.equal true
63
71
  end
64
72
 
73
+ it "makes the method/version/keepalive/encoding available after headers are parsed" do
74
+ @method.should.equal :GET
75
+ @version.should.equal [1,1]
76
+ @keep_alive.should.equal true
77
+ @encoding.should.equal :IDENTITY
78
+ end
79
+
80
+ it "has a 0 status code since its a request" do
81
+ @status_code.should.equal 0
82
+ end
83
+
84
+ it "has a 0 content_length since we have no body" do
85
+ @content_length.should.equal 0
86
+ end
87
+
65
88
  it "calls back when parsing of the message is complete" do
66
89
  @message_complete.should.equal true
67
90
  end
data/test/response.rb CHANGED
@@ -36,7 +36,15 @@ describe "http-parser response parsing" do
36
36
  @parser.on_message_begin { @message_begun = true }
37
37
  @parser.on_header_field {|f| @current_field = f }
38
38
  @parser.on_header_value {|v| @headers[@current_field] = v }
39
- @parser.on_headers_complete { @headers_complete = true }
39
+ @parser.on_headers_complete do
40
+ @headers_complete = true
41
+ @method = @parser.method
42
+ @version = @parser.version
43
+ @status_code = @parser.status_code
44
+ @encoding = @parser.transfer_encoding
45
+ @content_length = @parser.content_length
46
+ @keep_alive = @parser.keep_alive?
47
+ end
40
48
  @parser.on_body {|b| @body = b }
41
49
  @parser.on_message_complete { @message_complete = true }
42
50
  @parser.execute(RESPONSE_BUFFER)
@@ -53,6 +61,18 @@ describe "http-parser response parsing" do
53
61
  @headers_complete.should.equal true
54
62
  end
55
63
 
64
+ it "makes the version/status code/encoding/content length/keepalive available after headers are parsed" do
65
+ @version.should.equal [1,1]
66
+ @status_code.should.equal 200
67
+ @encoding.should.equal :IDENTITY
68
+ @content_length.should.equal 17
69
+ @keep_alive.should.equal false
70
+ end
71
+
72
+ it "has a nil method since it's a response" do
73
+ @method.should.equal nil
74
+ end
75
+
56
76
  it "calls back with the body" do
57
77
  @body.should.equal RESPONSE_BODY
58
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yakischloba-http-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Dahl
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-08-07 00:00:00 -07:00
14
+ date: 2009-08-08 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency