spider-gazelle 0.1.1 → 0.1.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: f6835ff8fc79a090958589cc6c64474d27446b98
4
- data.tar.gz: be7c36e4a3a219ee14ee0c35d4099f534b2fa9ee
3
+ metadata.gz: 389b76b304d2c434532df00b18f0e772b7bca616
4
+ data.tar.gz: de17af1941a3ce3cf24a65ec069a1e33bf5490f6
5
5
  SHA512:
6
- metadata.gz: 52c7dabc77dbe369030abff53cf86b1c120601de0fc88a03f5e84ce888c7513c99bbe7c2281597d525cb80eac92137e3ebb78e18fd2b0298f2a0b1f0ee47454d
7
- data.tar.gz: 4b97ce2d2131da24f716d438c0d82c1e76ed7c94c3b5acdf9263c9175a0b7c8152737e7113ead316116c4fcfae7752ddb2b9548aef6ece7a6336ce22bc06d4d9
6
+ metadata.gz: d3751fe5f50137772b2b434cf8ab2bbbeb8c597e343977ca99fd43de1cc5592f7897d756ae04e1ba4d81adbe60de712ab6f8169a42eb3a62bc9b91b6ccd60d90
7
+ data.tar.gz: 1bd6d81f822682a1c52b0c1f843e0f3323f69cc80e7effd755c092874fd6019692f8a705d92d87509393654034ac3a0d885b4c43ad88660f2247fc6834df5734
data/bin/sg CHANGED
@@ -82,7 +82,6 @@ ENV['SG_MODE'] = 'no_ipc' if ::FFI::Platform.windows?
82
82
  puts "#{e.message}\n#{e.backtrace.join("\n") unless e.backtrace.nil?}\n"
83
83
  }).finally do
84
84
  # This will execute if the TCP binding is lost
85
- p 'server failed to load - shutting down'
86
85
  server.shutdown
87
86
  end
88
87
 
@@ -5,6 +5,8 @@ module SpiderGazelle
5
5
  class Connection
6
6
  Hijack = Struct.new(:socket, :env)
7
7
 
8
+ REQUEST_METHOD = 'REQUEST_METHOD'.freeze # NOTE:: duplicate in gazelle.rb
9
+ HEAD = 'HEAD'.freeze
8
10
 
9
11
  RACK = 'rack'.freeze # used for filtering headers
10
12
  CLOSE = 'close'.freeze
@@ -15,6 +17,10 @@ module SpiderGazelle
15
17
  COLON_SPACE = ': '.freeze
16
18
  EOF = "0\r\n\r\n".freeze
17
19
  CRLF = "\r\n".freeze
20
+ ZERO = '0'.freeze
21
+
22
+ HTTP_STATUS_CODES = ::Rack::Utils::HTTP_STATUS_CODES
23
+ HTTP_STATUS_DEFAULT = proc { 'CUSTOM' }
18
24
 
19
25
  HTTP_11_400 = "HTTP/1.1 400 Bad Request\r\n\r\n".freeze
20
26
 
@@ -187,6 +193,7 @@ module SpiderGazelle
187
193
  @deferred_responses = nil if @deferred_responses
188
194
 
189
195
  status, headers, body = result
196
+ send_body = @request.env[REQUEST_METHOD] != HEAD
190
197
 
191
198
  # If a file, stream the body in a non-blocking fashion
192
199
  if body.respond_to? :to_path
@@ -201,19 +208,38 @@ module SpiderGazelle
201
208
 
202
209
  write_headers(status, headers)
203
210
 
204
- file = @loop.file(body.to_path, File::RDONLY)
205
- file.progress do # File is open and available for reading
206
- file.send_file(@socket, type).finally do
207
- file.close
208
- if @request.keep_alive == false
209
- @socket.shutdown
211
+ if send_body
212
+ file = @loop.file(body.to_path, File::RDONLY)
213
+ file.progress do # File is open and available for reading
214
+ file.send_file(@socket, type).finally do
215
+ file.close
216
+ if @request.keep_alive == false
217
+ @socket.shutdown
218
+ end
210
219
  end
211
220
  end
221
+ return file
212
222
  end
213
-
214
- return file
215
223
  else
216
- write_response(status, headers, body)
224
+ # Optimize the response
225
+ begin
226
+ body_size = body.size
227
+ if body_size < 2
228
+ if body_size == 1
229
+ headers[CONTENT_LENGTH] = body[0].bytesize
230
+ else
231
+ headers[CONTENT_LENGTH] = ZERO
232
+ end
233
+ end
234
+ rescue # just in case
235
+ end
236
+
237
+ if send_body
238
+ write_response(status, headers, body)
239
+ else
240
+ write_headers(status, headers)
241
+ @socket.shutdown if @request.keep_alive == false
242
+ end
217
243
  end
218
244
  end
219
245
  end
@@ -256,7 +282,7 @@ module SpiderGazelle
256
282
  end
257
283
 
258
284
  def write_headers(status, headers)
259
- header = "HTTP/1.1 #{status}\r\n"
285
+ header = "HTTP/1.1 #{status} #{fetch_code(status)}\r\n"
260
286
  headers.each do |key, value|
261
287
  next if key.start_with? RACK
262
288
 
@@ -274,6 +300,10 @@ module SpiderGazelle
274
300
  @socket.write chunk
275
301
  end
276
302
 
303
+ def fetch_code(status)
304
+ HTTP_STATUS_CODES.fetch(status, &HTTP_STATUS_DEFAULT)
305
+ end
306
+
277
307
 
278
308
  # ------------------------
279
309
  # Async response functions:
@@ -352,12 +352,15 @@ module SpiderGazelle
352
352
 
353
353
  def log(*args)
354
354
  msg = ''
355
- if args[0].respond_to? :backtrace
356
- msg << "unhandled exception: #{args[0]}\n #{args[0].backtrace}"
355
+ err = args[-1]
356
+ if err && err.respond_to?(:backtrace)
357
+ msg << "unhandled exception: #{err.message} (#{args[0..-2]})"
358
+ msg << "\n#{err.backtrace.join("\n")}" if err.respond_to?(:backtrace) && err.backtrace
357
359
  else
358
360
  msg << "unhandled exception: #{args}"
359
361
  end
360
362
  @logger.error msg
363
+ ::Libuv::Q.reject(@web, msg)
361
364
  end
362
365
  end
363
366
  end
@@ -1,3 +1,3 @@
1
1
  module SpiderGazelle
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spider-gazelle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-08 00:00:00.000000000 Z
11
+ date: 2014-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake