noderb-http 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,108 @@
1
+ require "rack"
2
+ require "stringio"
3
+ require "noderb/modules/http/parser"
4
+
5
+ module NodeRb
6
+ module Modules
7
+ module Http
8
+ class RackParser
9
+
10
+ include Parser
11
+
12
+ def initialize(env)
13
+ setup(:request)
14
+ @env = {
15
+ "rack.input" => StringIO.new,
16
+ "rack.version" => ::Rack::VERSION,
17
+ "rack.errors" => StringIO.new,
18
+ "rack.multithread" => false,
19
+ "rack.multiprocess" => false,
20
+ "rack.run_once" => false,
21
+ "rack.url_scheme" => "http"
22
+ }.merge(env || {})
23
+ end
24
+
25
+ def <<(data)
26
+ self.parse(data)
27
+ return nil unless @_state == :done
28
+ self.dispose
29
+ @env
30
+ end
31
+
32
+ def on_message_begin
33
+ @_state = :field
34
+ @_header_name = ""
35
+ @_header_value = ""
36
+ end
37
+
38
+ def on_method(method)
39
+ @env["REQUEST_METHOD"] = method
40
+ end
41
+
42
+ def on_url(url)
43
+ url = url.split("?", 2)
44
+ if @env["SCRIPT_NAME"]
45
+ url.sub!(@env["SCRIPT_NAME"], "")
46
+ else
47
+ @env["SCRIPT_NAME"] = ""
48
+ end
49
+ @env["PATH_INFO"] = url[0]
50
+ @env["QUERY_STRING"] = url[1] || ""
51
+ end
52
+
53
+ def on_header_field(name)
54
+ if @_state == :value
55
+ header = @_header_name.sub("-", "_").upcase
56
+ case header
57
+ when "CONTENT_TYPE", "CONTENT_LENGTH"
58
+ @env[header] = @_header_value
59
+ when "HOST"
60
+ @env["HTTP_HOST"] = @_header_value
61
+ unless @env["SERVER_NAME"]
62
+ host = @_header_value.split(":")
63
+ @env["SERVER_NAME"] = host[0]
64
+ @env["SERVER_PORT"] = host[1] || 80
65
+ end
66
+ else
67
+ @env["HTTP_#{header}"] = @_header_value
68
+ end
69
+ @_header_name = ""
70
+ @_header_value = ""
71
+ end
72
+ @_state = :field
73
+ @_header_name << name
74
+ end
75
+
76
+ def on_header_value(value)
77
+ @_header_value << value
78
+ @_state = :value
79
+ end
80
+
81
+ def on_headers_complete
82
+ on_header_field("")
83
+ end
84
+
85
+ def on_body(body)
86
+ @env["rack.input"] << body
87
+ end
88
+
89
+ def on_error(name, description)
90
+ raise(RuntimeError, "Http parser error: #{name} (#{description})")
91
+ end
92
+
93
+ def on_keep_alive status
94
+ @env["nrb.keep_alive"] = status
95
+ end
96
+
97
+ def on_upgrade
98
+ @env["nrb.upgrade"] = true
99
+ end
100
+
101
+ def on_message_complete
102
+ @_state = :done
103
+ end
104
+
105
+ end
106
+ end
107
+ end
108
+ end
@@ -2,7 +2,7 @@ module NodeRb
2
2
  module Modules
3
3
  module Http
4
4
 
5
- VERSION = "0.0.4" unless const_defined?(:VERSION)
5
+ VERSION = "0.0.5" unless const_defined?(:VERSION)
6
6
 
7
7
  end
8
8
  end
@@ -1,2 +1,3 @@
1
1
  require "noderb/modules/http/version"
2
- require "noderb/modules/http/parser"
2
+ require "noderb/modules/http/parser"
3
+ require "noderb/modules/http/rack_parser"
data/lib/noderb-http.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  require 'noderb_http_extension'
2
- require 'noderb/modules/http'
2
+ require 'noderb/modules/http'
data/test/init.rb CHANGED
@@ -1,6 +1,6 @@
1
- #$: << File.expand_path("../../ext/noderb_http_extension", __FILE__)
2
- #$: << File.expand_path("../../lib", __FILE__)
3
- require "rubygems"
1
+ $: << File.expand_path("../../ext/noderb_http_extension", __FILE__)
2
+ $: << File.expand_path("../../lib", __FILE__)
3
+
4
4
  require "noderb-http"
5
5
 
6
6
  class TestParser
@@ -61,4 +61,4 @@ CASES.each do |test|
61
61
  parser.dispose
62
62
  end
63
63
 
64
- puts "** All test passed OK" unless failed
64
+ puts "** All test passed OK" unless failed
data/test/test_rack.rb ADDED
@@ -0,0 +1,43 @@
1
+ $: << File.expand_path("../../ext/noderb_http_extension", __FILE__)
2
+ $: << File.expand_path("../../lib", __FILE__)
3
+
4
+ require "rubygems"
5
+
6
+ require "noderb-http"
7
+
8
+ require "eventmachine"
9
+ require "rack/lint"
10
+
11
+ class App
12
+ def self.call(env)
13
+ [200, {"Content-Type" => "text/html"}, "Hello"]
14
+ end
15
+ end
16
+
17
+ class Server < EventMachine::Connection
18
+
19
+ def post_init
20
+ @env = {}
21
+ @parser = ::NodeRb::Modules::Http::RackParser.new(@env)
22
+ @app = ::Rack::Lint.new(App)
23
+ end
24
+
25
+ def receive_data data
26
+ result = (@parser << data)
27
+ if result
28
+ puts result.inspect
29
+ send_data(@app.call(result)[2].inspect)
30
+ close_connection_after_writing
31
+ end
32
+ end
33
+
34
+ def unbind
35
+ @env = nil
36
+ @parser = nil
37
+ end
38
+
39
+ end
40
+
41
+ EventMachine.run do
42
+ EventMachine.start_server "0.0.0.0", 8080, Server
43
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noderb-http
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marek Jelen
@@ -15,9 +15,25 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-01 00:00:00 Z
19
- dependencies: []
20
-
18
+ date: 2011-10-01 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rack
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 1
32
+ - 1
33
+ - 0
34
+ version: 1.1.0
35
+ type: :runtime
36
+ version_requirements: *id001
21
37
  description: Fast and full-featured HTTP parser.
22
38
  email:
23
39
  - marek@jelen.biz
@@ -28,22 +44,25 @@ extensions:
28
44
  extra_rdoc_files: []
29
45
 
30
46
  files:
47
+ - lib/noderb-http.rb
48
+ - lib/noderb/modules/http.rb
31
49
  - lib/noderb/modules/http/parser.rb
50
+ - lib/noderb/modules/http/rack_parser.rb
32
51
  - lib/noderb/modules/http/version.rb
33
- - lib/noderb/modules/http.rb
34
- - lib/noderb-http.rb
35
52
  - ext/noderb_http_extension/extconf.rb
36
53
  - ext/noderb_http_extension/http_parser.c
37
54
  - ext/noderb_http_extension/http_parser.h
38
55
  - ext/noderb_http_extension/noderb_http.c
39
56
  - ext/noderb_http_extension/noderb_http.h
57
+ - test/init.rb
58
+ - test/test_rack.rb
40
59
  - test/cases/curl_get.rb
41
60
  - test/cases/patch_request.rb
42
61
  - test/cases/response.rb
43
62
  - test/cases/upgrade_request.rb
44
- - test/init.rb
45
63
  - LICENSE
46
64
  - README.md
65
+ has_rdoc: true
47
66
  homepage: http://github.com/noderb/noderb-http
48
67
  licenses: []
49
68
 
@@ -75,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
94
  requirements: []
76
95
 
77
96
  rubyforge_project:
78
- rubygems_version: 1.8.8
97
+ rubygems_version: 1.5.2
79
98
  signing_key:
80
99
  specification_version: 3
81
100
  summary: Fast and full-featured HTTP parser