chase 0.0.1 → 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.
- checksums.yaml +4 -4
- data/lib/chase/http_parser.rb +9 -8
- data/lib/chase/response.rb +21 -8
- data/lib/chase/server.rb +11 -8
- data/lib/chase/version.rb +1 -1
- data/spec/chase/response_spec.rb +3 -3
- data/spec/chase/server_spec.rb +3 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee9333b640a39435b60f37505f2ece8c5b6ac2b9
|
4
|
+
data.tar.gz: 51b7403cbfa7d7ce518e74e4a8078ad5a7e0761f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6f6dedf96b6ace4439e22b824c87645ceb0c2b39127e965d93266209089ff5d0c8acfa9b30fb8f201117e21e75ddee45f272a3539ca8f64cb6e39be6b807694
|
7
|
+
data.tar.gz: cb26c5997b6f85604c3218bd76f085611bd39af61db8b249bc5a4989802c5b0423353c726e6dd8b9fca5491c3031edeaecfa7fb48e490e8a1e8efc320e47b39c
|
data/lib/chase/http_parser.rb
CHANGED
@@ -14,7 +14,7 @@ module Chase
|
|
14
14
|
parser.on_url do |url|
|
15
15
|
raise HTTP::Parser::Error, 'Invalid method' unless VALID_METHODS.include?(http_method)
|
16
16
|
|
17
|
-
set_env('
|
17
|
+
set_env('REQUEST_METHOD', http_method)
|
18
18
|
set_env('REQUEST_URI', url)
|
19
19
|
matches = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/)
|
20
20
|
matches ||= Hash.new('')
|
@@ -35,13 +35,14 @@ module Chase
|
|
35
35
|
|
36
36
|
parser.on_body { |body| set_env('POST_CONTENT', body) }
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
38
|
+
parser.on_message_begin do
|
39
|
+
set_env('HTTP_COOKIE', '')
|
40
|
+
set_env('POST_CONTENT', '')
|
41
|
+
end
|
42
|
+
|
43
|
+
parser.on_message_complete do
|
44
|
+
request.emit(:created)
|
45
|
+
end
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
data/lib/chase/response.rb
CHANGED
@@ -60,12 +60,16 @@ module Chase
|
|
60
60
|
@content = value.to_s
|
61
61
|
end
|
62
62
|
|
63
|
+
def headers=(value)
|
64
|
+
@headers = value.to_h
|
65
|
+
end
|
66
|
+
|
63
67
|
def flush
|
64
68
|
return if flushed?
|
65
69
|
|
66
|
-
|
67
|
-
|
68
|
-
|
70
|
+
write_headers
|
71
|
+
write("\r\n")
|
72
|
+
write(content)
|
69
73
|
|
70
74
|
emit(:flushed)
|
71
75
|
end
|
@@ -76,13 +80,22 @@ module Chase
|
|
76
80
|
|
77
81
|
private
|
78
82
|
|
79
|
-
def
|
80
|
-
emit(:
|
83
|
+
def write(data)
|
84
|
+
emit(:write, data)
|
85
|
+
end
|
86
|
+
|
87
|
+
def write_headers
|
88
|
+
write "HTTP/1.1 #{STATUS_CODES[status] || '200 OK'}\r\n"
|
89
|
+
headers['Content-Length'] ||= content.bytesize
|
90
|
+
headers.each { |key, value| write_header(key, value) }
|
81
91
|
end
|
82
92
|
|
83
|
-
def
|
84
|
-
|
85
|
-
|
93
|
+
def write_header(key, value)
|
94
|
+
if value.is_a?(Array)
|
95
|
+
value.each { |subvalue| write_header(key, subvalue) }
|
96
|
+
else
|
97
|
+
write("#{key}: #{value}\r\n")
|
98
|
+
end
|
86
99
|
end
|
87
100
|
end
|
88
101
|
end
|
data/lib/chase/server.rb
CHANGED
@@ -9,15 +9,13 @@ module Chase
|
|
9
9
|
include HttpParser
|
10
10
|
|
11
11
|
def receive_data(data)
|
12
|
-
|
13
|
-
prepare_response
|
14
|
-
handle
|
12
|
+
http_parser << data
|
15
13
|
rescue ServerError, HTTP::Parser::Error
|
16
14
|
send_error('400 Bad Request')
|
17
15
|
end
|
18
16
|
|
19
17
|
def request
|
20
|
-
@request ||=
|
18
|
+
@request ||= prepare_request
|
21
19
|
end
|
22
20
|
|
23
21
|
def response
|
@@ -29,14 +27,19 @@ module Chase
|
|
29
27
|
close_connection_after_writing
|
30
28
|
end
|
31
29
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
30
|
+
def prepare_request
|
31
|
+
Request.new.tap do |request|
|
32
|
+
request.on(:created) do
|
33
|
+
raise ServerError unless request.env['REQUEST_METHOD']
|
34
|
+
prepare_response
|
35
|
+
handle
|
36
|
+
end
|
37
|
+
end
|
35
38
|
end
|
36
39
|
|
37
40
|
def prepare_response
|
38
41
|
response.on(:flushed) { close_connection_after_writing }
|
39
|
-
response.on(:
|
42
|
+
response.on(:write) { |data| send_data(data) }
|
40
43
|
end
|
41
44
|
end
|
42
45
|
end
|
data/lib/chase/version.rb
CHANGED
data/spec/chase/response_spec.rb
CHANGED
@@ -15,7 +15,7 @@ RSpec.describe Chase::Response do
|
|
15
15
|
|
16
16
|
it 'emits the headers and content' do
|
17
17
|
output = ''
|
18
|
-
subject.on(:
|
18
|
+
subject.on(:write) { |data| output += data }
|
19
19
|
|
20
20
|
subject.flush
|
21
21
|
|
@@ -33,8 +33,8 @@ RSpec.describe Chase::Response do
|
|
33
33
|
|
34
34
|
it 'does not emit content again' do
|
35
35
|
output = ''
|
36
|
-
subject.on(:
|
37
|
-
|
36
|
+
subject.on(:write) { |data| output += data }
|
37
|
+
|
38
38
|
subject.flush
|
39
39
|
expect(output).to eq('')
|
40
40
|
end
|
data/spec/chase/server_spec.rb
CHANGED
@@ -24,7 +24,7 @@ RSpec.describe Chase::Server do
|
|
24
24
|
context 'valid request' do
|
25
25
|
it 'calls #handle' do
|
26
26
|
expect(subject).to receive(:handle)
|
27
|
-
subject.receive_data(
|
27
|
+
subject.receive_data("GET / HTTP/1.1\r\n\r\n\0")
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'creates a request object' do
|
@@ -49,7 +49,8 @@ RSpec.describe Chase::Server do
|
|
49
49
|
|
50
50
|
Some-Post-Content=Value&Some-Other=abc2
|
51
51
|
eos
|
52
|
-
|
52
|
+
|
53
|
+
expect(subject.request.env['REQUEST_METHOD']).to eq('PATCH')
|
53
54
|
expect(subject.request.env['REQUEST_URI']).to eq('https://www.google.com/chase/request?key=value&other=123')
|
54
55
|
expect(subject.request.env['PROTOCOL']).to eq('https')
|
55
56
|
expect(subject.request.env['PATH_INFO']).to eq('/chase/request')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chase
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Osburn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|