segregate 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13ff1b7cc5c95ae45abf27d4edaa5fd80365fa19
4
- data.tar.gz: 59dd6532dd4a5be947c24c7ac0bb9378950f9b88
3
+ metadata.gz: 71d89f3c3e90e41fb84af4faeb638c55b1e57f42
4
+ data.tar.gz: f0de163082142414642801e392f12c7d13448d38
5
5
  SHA512:
6
- metadata.gz: c0efb2a197c27af4005ee120feac38dfcec61d41628f5ffd05c9726ec4aade1fa5b9efcb7cf8690246b2a1462966b79e44616b908de2368242fbb12b459a817b
7
- data.tar.gz: e470e38d363523ea15a042119b85d4835d98d784c15f8a19423c10f146562117f9ba8a051f10667d7345f48d9f189d636e389eeb92e4a66d06df165d0b8caddd
6
+ metadata.gz: 6996697e014269c5a661f51ad4dff60b6b11fbc400a849c18ac62987db430b5cba85b168f489cff1eaa7176c7cd1887ef3b1296bf10ab9cdb5d64797331eb4ce
7
+ data.tar.gz: 2549fd7247b7acbf4c7d3f8823f3679c4eccc4675bfe23b7ddaf120b4d78f464cb912d9bb1afbf9d06d6eedaeb98d9f80e5f7eb4303bbfb1f11241801ddb01a5
data/lib/segregate.rb CHANGED
@@ -22,7 +22,14 @@ class Segregate
22
22
  @uri.respond_to?(meth, include_private) || super
23
23
  end
24
24
 
25
- def initialize callback = nil
25
+ def debug message
26
+ if @debug
27
+ puts "DEBUG: " + message.t_s
28
+ end
29
+ end
30
+
31
+ def initialize callback = nil, *args, **kwargs
32
+ @debug = kwargs[:debug] ? true : false
26
33
  @callback = callback
27
34
  @http_version = [nil, nil]
28
35
 
@@ -35,6 +42,8 @@ class Segregate
35
42
 
36
43
  @stashed_data = ""
37
44
  @stashed_body = ""
45
+
46
+ @header_order = []
38
47
  end
39
48
 
40
49
  def request?
@@ -83,24 +92,46 @@ class Segregate
83
92
 
84
93
  def update_content_length
85
94
  unless @body.empty?
86
- @headers['content-length'] = @body.size.to_s
87
- @headers.delete 'transfer-encoding'
95
+ if @body.length > 99999999999
96
+ @headers['transfer-encoding'] = 'chunked'
97
+ @headers.delete 'content-length'
98
+ else
99
+ @headers['content-length'] = @body.length.to_s
100
+ @headers.delete 'transfer-encoding'
101
+ end
88
102
  end
89
103
  end
90
104
 
91
105
  def raw_data
92
106
  raw_message = ""
93
107
  update_content_length
94
-
108
+
109
+ build_headers raw_message
110
+ build_body raw_message
111
+
112
+ return raw_message
113
+ end
114
+
115
+ def build_headers raw_message
95
116
  request? ? raw_message << request_line + "\r\n" : raw_message << status_line + "\r\n"
96
- ALL_HEADERS.each do |header|
97
- raw_message << "%s: %s\r\n" % [header, headers[header]] if headers[header]
117
+ @header_order.each do |header|
118
+ raw_message << "%s: %s\r\n" % [header, headers[header.downcase]]
98
119
  end
99
-
100
- raw_message << "\r\n" + @body + "\r\n" unless @body.empty?
101
120
  raw_message << "\r\n"
102
121
  end
103
122
 
123
+ def build_body raw_message
124
+ if @headers['content-length']
125
+ raw_message << @body
126
+ elsif @headers['transfer-encoding'] == 'chunked'
127
+ @body.scan(/.{1,65535}/).each do |chunk|
128
+ raw_message << "%s\r\n" % chunk.length.to_s(16)
129
+ raw_message << chunk + "\r\n"
130
+ end
131
+ raw_message << "0\r\n\r\n"
132
+ end
133
+ end
134
+
104
135
  def parse_data data
105
136
  data = StringIO.new(@stashed_data + data)
106
137
  @stashed_data = ""
@@ -109,6 +140,8 @@ class Segregate
109
140
  line, complete_line = get_next_line data
110
141
  complete_line ? parse_line(line) : @stashed_data = line
111
142
  end
143
+
144
+ data.close
112
145
  end
113
146
 
114
147
  def parse_line line
@@ -127,8 +160,17 @@ class Segregate
127
160
  private
128
161
 
129
162
  def get_next_line data
130
- line = data.readline("\r\n")
131
- [line.chomp("\r\n"), line.end_with?("\r\n") || line.length == headers['content-length'].to_i && @state >= :body]
163
+ if @headers['content-length'] && @state >= :body
164
+ line = data.readline("\r\n")
165
+ @inital_line = line
166
+ [line, true]
167
+ else
168
+ line = data.readline("\r\n")
169
+ @inital_line = line
170
+ result = line.end_with?("\r\n")
171
+ line = line[0..-3] if result
172
+ [line, result]
173
+ end
132
174
  end
133
175
 
134
176
  def read_in_first_line line
@@ -139,9 +181,9 @@ class Segregate
139
181
  elsif line =~ STATUS_LINE
140
182
  parse_status_line line
141
183
  elsif line =~ UNKNOWN_REQUEST_LINE
142
- raise "ERROR: Unknown http method: %s" % line[/^\S+/]
184
+ debug "Unknown http method: %s" % line[/^\S+/]
143
185
  else
144
- raise "ERROR: Unknown first line: %s" % line
186
+ debug "Unknown first line: %s" % line
145
187
  end
146
188
 
147
189
  @state.next
@@ -169,8 +211,9 @@ class Segregate
169
211
  if line.empty?
170
212
  @state.next
171
213
  else
172
- key, value = line.split(":")
173
- @headers[key.downcase] = value.strip
214
+ key, value = line.split(": ",2)
215
+ @header_order << key
216
+ @headers[key.downcase] = value
174
217
  end
175
218
 
176
219
  if headers_complete?
@@ -193,7 +236,7 @@ class Segregate
193
236
  line = @stashed_body + line
194
237
  @stashed_body = ""
195
238
 
196
- if line.length == headers['content-length'].to_i
239
+ if line.length >= headers['content-length'].to_i
197
240
  @body = line
198
241
  @callback.on_body @body if @callback.respond_to?(:on_body)
199
242
  @state.next
@@ -226,12 +269,12 @@ class Segregate
226
269
  line = @stashed_body + line
227
270
  @stashed_body = ""
228
271
 
229
- if line.length == @chunk_size
272
+ if line.length >= @chunk_size
230
273
  @body << line
231
274
  @callback.on_body line if @callback.respond_to?(:on_body)
232
275
  @chunked_body_state.next
233
276
  else
234
- @stashed_body = line
277
+ @stashed_body = @inital_line.end_with?("\r\n") ? (line + "\r\n") : line
235
278
  end
236
279
  end
237
280
  end
@@ -1,5 +1,5 @@
1
1
  class Segregate
2
2
  REQUEST_LINE = /^(#{HTTP_METHODS.join("|")})\s(\*|\S+)\sHTTP\/(\d).(\d)$/
3
- STATUS_LINE = /^HTTP\/(\d).(\d)\s(\d{3})\s([\w\s\-]+)$/
3
+ STATUS_LINE = /^HTTP\/(\d).(\d)\s(\d{3})\s([\w\s-]+)$/
4
4
  UNKNOWN_REQUEST_LINE = /^(\w+)\s(\*|\S+)\sHTTP\/(\d).(\d)$/
5
5
  end
@@ -1,4 +1,4 @@
1
1
  class Segregate
2
- VERSION = "0.5.0".freeze
3
- DATE = "2014-02-04".freeze
2
+ VERSION = "0.5.2".freeze
3
+ DATE = "2014-02-10".freeze
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: segregate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Slaughter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-04 00:00:00.000000000 Z
11
+ date: 2014-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -101,13 +101,13 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - README.md
105
104
  - LICENSE
105
+ - README.md
106
+ - lib/segregate.rb
106
107
  - lib/segregate/http_headers.rb
107
108
  - lib/segregate/http_methods.rb
108
109
  - lib/segregate/http_regular_expressions.rb
109
110
  - lib/segregate/version.rb
110
- - lib/segregate.rb
111
111
  - spec/segregate_spec.rb
112
112
  - spec/spec_helper.rb
113
113
  homepage: http://benslaughter.github.io/segregate/
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  version: '0'
131
131
  requirements: []
132
132
  rubyforge_project:
133
- rubygems_version: 2.0.3
133
+ rubygems_version: 2.2.2
134
134
  signing_key:
135
135
  specification_version: 4
136
136
  summary: Segregate http parser