em-http-server 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +1 -1
- data/em-http-server.gemspec +1 -1
- data/lib/em-http-server/response.rb +1 -0
- data/lib/em-http-server/server.rb +17 -7
- data/test/test_app.rb +42 -16
- data/test/test_delegated.rb +1 -10
- data/test/test_response.rb +1 -10
- metadata +9 -14
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 530fe32e11627dd6b6889f1dfe18ff14f09b27fc
|
4
|
+
data.tar.gz: e7b4dc4ff5fb5391a6c1378bb28278f75b4cd593
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7cc141f2eb7be20f0c24b89391927406fef09abd02557150bb8757e973e713611bf13f8dce0c760afadaf36f45085299f54bed6ca421ceadaebcb74f670bc829
|
7
|
+
data.tar.gz: 90d4f5ae859931153636e4bd00fd157a1526b5a1046a625eac48b32f407879862b027a8fd24fd9a8e8eed7abded55cf1449bea41ae407370d7a7da152aa7babd
|
data/README.md
CHANGED
data/em-http-server.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
12
|
gem.name = "em-http-server"
|
13
13
|
gem.require_paths = ["lib"]
|
14
|
-
gem.version = "0.1.
|
14
|
+
gem.version = "0.1.7"
|
15
15
|
|
16
16
|
gem.add_runtime_dependency "eventmachine"
|
17
17
|
end
|
@@ -41,11 +41,11 @@ module EventMachine
|
|
41
41
|
def parse_first_header(line)
|
42
42
|
|
43
43
|
# split the line into: METHOD URI PROTOCOL
|
44
|
-
# eg: GET /
|
44
|
+
# eg: GET / HTTP/1.1
|
45
45
|
parsed = line.split(' ')
|
46
46
|
|
47
47
|
# a correct request has three parts
|
48
|
-
|
48
|
+
return bad_parsing unless parsed.size == 3
|
49
49
|
|
50
50
|
@http_request_method, uri, @http_protocol = parsed
|
51
51
|
|
@@ -53,16 +53,26 @@ module EventMachine
|
|
53
53
|
@http_request_uri, @http_query_string = uri.split('?')
|
54
54
|
end
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
def bad_parsing
|
57
|
+
code = 400
|
58
|
+
desc = "Bad request"
|
59
|
+
string = respond_to?(:http_error_string) ? http_error_string(code, desc) : default_error_string(code, desc)
|
60
|
+
send_error string
|
61
|
+
raise("#{code} #{desc}")
|
62
|
+
end
|
63
|
+
|
64
|
+
def default_error_string(code, desc)
|
65
|
+
string = "HTTP/1.1 #{code} #{desc}\r\n"
|
59
66
|
string << "Connection: close\r\n"
|
60
67
|
string << "Content-type: text/plain\r\n"
|
61
68
|
string << "\r\n"
|
62
69
|
string << "Detected error: HTTP code #{code}"
|
70
|
+
end
|
71
|
+
|
72
|
+
# send back to the client an HTTP error
|
73
|
+
def send_error(string)
|
63
74
|
send_data string
|
64
75
|
close_connection_after_writing
|
65
|
-
raise("server error #{code}")
|
66
76
|
end
|
67
77
|
|
68
78
|
end
|
@@ -72,7 +82,7 @@ end
|
|
72
82
|
|
73
83
|
if __FILE__ == $0
|
74
84
|
|
75
|
-
class HTTPHandler < EM::
|
85
|
+
class HTTPHandler < EM::HttpServer::Server
|
76
86
|
|
77
87
|
def process_http_request
|
78
88
|
puts @http_request_method
|
data/test/test_app.rb
CHANGED
@@ -1,22 +1,13 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'em-http-server'
|
3
3
|
|
4
|
-
|
5
|
-
once = false
|
6
|
-
require 'eventmachine'
|
7
|
-
rescue LoadError => e
|
8
|
-
raise e if once
|
9
|
-
once = true
|
10
|
-
require 'rubygems'
|
11
|
-
retry
|
12
|
-
end
|
13
|
-
|
4
|
+
require 'eventmachine'
|
14
5
|
|
15
6
|
|
16
7
|
#--------------------------------------
|
17
8
|
|
18
9
|
module EventMachine
|
19
|
-
class
|
10
|
+
class HttpHandler < EM::HttpServer::Server
|
20
11
|
def process_http_request
|
21
12
|
send_data generate_response()
|
22
13
|
close_connection_after_writing
|
@@ -47,7 +38,7 @@ EORESP
|
|
47
38
|
def test_simple_get
|
48
39
|
received_response = nil
|
49
40
|
|
50
|
-
EventMachine::
|
41
|
+
EventMachine::HttpHandler.class_eval do
|
51
42
|
def generate_response
|
52
43
|
TestResponse_1
|
53
44
|
end
|
@@ -55,7 +46,7 @@ EORESP
|
|
55
46
|
|
56
47
|
|
57
48
|
EventMachine.run do
|
58
|
-
EventMachine.start_server TestHost, TestPort, EventMachine::
|
49
|
+
EventMachine.start_server TestHost, TestPort, EventMachine::HttpHandler
|
59
50
|
EventMachine.add_timer(1) {raise "timed out"} # make sure the test completes
|
60
51
|
|
61
52
|
cb = proc do
|
@@ -75,7 +66,7 @@ EORESP
|
|
75
66
|
|
76
67
|
# This frowsy-looking protocol handler allows the test harness to make some
|
77
68
|
# its local variables visible, so we can set them here and they can be asserted later.
|
78
|
-
class MyTestServer < EventMachine::
|
69
|
+
class MyTestServer < EventMachine::HttpHandler
|
79
70
|
|
80
71
|
def initialize *args
|
81
72
|
super
|
@@ -251,7 +242,42 @@ EORESP
|
|
251
242
|
cb = proc do
|
252
243
|
tcp = TCPSocket.new TestHost, TestPort
|
253
244
|
data = [
|
254
|
-
"GET
|
245
|
+
"GET HTTP/1.1\r\n",
|
246
|
+
"\r\n"
|
247
|
+
].join
|
248
|
+
tcp.write data
|
249
|
+
received_response = tcp.read
|
250
|
+
end
|
251
|
+
eb = proc { EventMachine.stop }
|
252
|
+
EventMachine.defer cb, eb
|
253
|
+
|
254
|
+
EventMachine.add_timer(1) {raise "timed out"} # make sure the test completes
|
255
|
+
end
|
256
|
+
|
257
|
+
assert_equal( "HTTP/1.1 400 Bad request\r\nConnection: close\r\nContent-type: text/plain\r\n\r\nDetected error: HTTP code 400", received_response )
|
258
|
+
end
|
259
|
+
|
260
|
+
def test_invalid_custom
|
261
|
+
received_response = nil
|
262
|
+
|
263
|
+
MyTestServer.class_eval do
|
264
|
+
def http_error_string(code, desc)
|
265
|
+
return 'custom'
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
EventMachine.run do
|
270
|
+
EventMachine.start_server(TestHost, TestPort, MyTestServer) do |conn|
|
271
|
+
conn.instance_eval do
|
272
|
+
@assertions = proc do
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
cb = proc do
|
278
|
+
tcp = TCPSocket.new TestHost, TestPort
|
279
|
+
data = [
|
280
|
+
"GET HTTP/1.1\r\n",
|
255
281
|
"\r\n"
|
256
282
|
].join
|
257
283
|
tcp.write data
|
@@ -263,7 +289,7 @@ EORESP
|
|
263
289
|
EventMachine.add_timer(1) {raise "timed out"} # make sure the test completes
|
264
290
|
end
|
265
291
|
|
266
|
-
assert_equal( "
|
292
|
+
assert_equal( "custom", received_response )
|
267
293
|
end
|
268
294
|
|
269
295
|
|
data/test/test_delegated.rb
CHANGED
@@ -1,16 +1,7 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'em-http-server'
|
3
3
|
|
4
|
-
|
5
|
-
once = false
|
6
|
-
require 'eventmachine'
|
7
|
-
rescue LoadError => e
|
8
|
-
raise e if once
|
9
|
-
once = true
|
10
|
-
require 'rubygems'
|
11
|
-
retry
|
12
|
-
end
|
13
|
-
|
4
|
+
require 'eventmachine'
|
14
5
|
|
15
6
|
#--------------------------------------
|
16
7
|
|
data/test/test_response.rb
CHANGED
@@ -1,16 +1,7 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'em-http-server'
|
3
3
|
|
4
|
-
|
5
|
-
once = false
|
6
|
-
require 'eventmachine'
|
7
|
-
rescue LoadError => e
|
8
|
-
raise e if once
|
9
|
-
once = true
|
10
|
-
require 'rubygems'
|
11
|
-
retry
|
12
|
-
end
|
13
|
-
|
4
|
+
require 'eventmachine'
|
14
5
|
|
15
6
|
#--------------------------------------
|
16
7
|
|
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-http-server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.7
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- alor
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-11-26 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: eventmachine
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
description: Simple http server for eventmachine
|
@@ -49,30 +46,28 @@ files:
|
|
49
46
|
- test/test_response.rb
|
50
47
|
homepage: https://github.com/alor/em-http-server
|
51
48
|
licenses: []
|
49
|
+
metadata: {}
|
52
50
|
post_install_message:
|
53
51
|
rdoc_options: []
|
54
52
|
require_paths:
|
55
53
|
- lib
|
56
54
|
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
55
|
requirements:
|
59
|
-
- -
|
56
|
+
- - '>='
|
60
57
|
- !ruby/object:Gem::Version
|
61
58
|
version: '0'
|
62
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
60
|
requirements:
|
65
|
-
- -
|
61
|
+
- - '>='
|
66
62
|
- !ruby/object:Gem::Version
|
67
63
|
version: '0'
|
68
64
|
requirements: []
|
69
65
|
rubyforge_project:
|
70
|
-
rubygems_version:
|
66
|
+
rubygems_version: 2.0.6
|
71
67
|
signing_key:
|
72
|
-
specification_version:
|
68
|
+
specification_version: 4
|
73
69
|
summary: Simple http server for eventmachine with the same interface as evma_httpserver
|
74
70
|
test_files:
|
75
71
|
- test/test_app.rb
|
76
72
|
- test/test_delegated.rb
|
77
73
|
- test/test_response.rb
|
78
|
-
has_rdoc:
|