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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72eefdf46b326be759f69345fb5234e1a873137c
4
- data.tar.gz: a603c8ce78dba15cfe203aeb18b84cfc7c72a436
3
+ metadata.gz: ee9333b640a39435b60f37505f2ece8c5b6ac2b9
4
+ data.tar.gz: 51b7403cbfa7d7ce518e74e4a8078ad5a7e0761f
5
5
  SHA512:
6
- metadata.gz: f3e270e13da9d72c0f22a7ae199499295455befd9b035c6ee3f625516a9942ea90262b9a7ee22cdc592a436167fad2bf4dafefe7acb7e5b3452a6e7a88b81b11
7
- data.tar.gz: 92856fd255a979f89b91a664e651ab91d456c8263e0feac5858a823d118490b8c19ebd557e13daf24a0ae7eac7a3947e6531b1dbaf7cb19350c34940efcc08c1
6
+ metadata.gz: a6f6dedf96b6ace4439e22b824c87645ceb0c2b39127e965d93266209089ff5d0c8acfa9b30fb8f201117e21e75ddee45f272a3539ca8f64cb6e39be6b807694
7
+ data.tar.gz: cb26c5997b6f85604c3218bd76f085611bd39af61db8b249bc5a4989802c5b0423353c726e6dd8b9fca5491c3031edeaecfa7fb48e490e8a1e8efc320e47b39c
@@ -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('HTTP_METHOD', http_method)
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
- # parser.on_message_begin do
39
- # puts "message begin"
40
- # end
41
- #
42
- # parser.on_message_complete do
43
- # puts "message complete"
44
- # end
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
 
@@ -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
- send_headers
67
- send("\r\n")
68
- send(content)
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 send(data)
80
- emit(:send, data)
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 send_headers
84
- send "HTTP/1.1 #{STATUS_CODES[status] || '200 OK'}\r\n"
85
- headers.each { |key, value| send("#{key}: #{value}\r\n") }
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
- parse_request data
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 ||= Request.new
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 parse_request(data)
33
- http_parser << data
34
- raise ServerError unless request.env['HTTP_METHOD']
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(:send) { |data| send_data(data) }
42
+ response.on(:write) { |data| send_data(data) }
40
43
  end
41
44
  end
42
45
  end
data/lib/chase/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Chase
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
@@ -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(:send) { |data| output += data }
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(:send) { |data| output += data }
37
-
36
+ subject.on(:write) { |data| output += data }
37
+
38
38
  subject.flush
39
39
  expect(output).to eq('')
40
40
  end
@@ -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('GET / HTTP/1.1')
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
- expect(subject.request.env['HTTP_METHOD']).to eq('PATCH')
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.1
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-17 00:00:00.000000000 Z
11
+ date: 2017-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine