collin (owner)

Revisions

gist: 187018 Download_button fork
public
Public Clone URL: git://gist.github.com/187018.git
Embed All Files: show embed
Diff #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
diff --git a/lib/thin/connection.rb b/lib/thin/connection.rb
index 7ead989..88efa6e 100644
--- a/lib/thin/connection.rb
+++ b/lib/thin/connection.rb
@@ -66,6 +66,7 @@ module Thin
       # It should be noted that connection objects will linger until this
       # callback is no longer referenced, so be tidy!
       @request.async_callback = method(:post_process)
+ @request.async_chunk_callback = method(:chunk_callback)
       
       # When we're under a non-async framework like rails, we can still spawn
       # off async responses using the callback info, so there's little point
@@ -82,6 +83,11 @@ module Thin
       nil # Signal to post_process that the request could not be processed
     end
     
+ # extra callback to send a chunk down the wire without the entire "method(:post_process)"
+ # this allows for some more flexible scenarios where I might not want my headers constructed for me
+ def chunk_callback(chunk)
+ trace { chunk }
+ send_data chunk
+ end
+
     def post_process(result)
       return unless result
       result = result.to_a
diff --git a/lib/thin/request.rb b/lib/thin/request.rb
index 0add8a6..2cf9f47 100644
--- a/lib/thin/request.rb
+++ b/lib/thin/request.rb
@@ -33,6 +33,7 @@ module Thin
     RACK_MULTITHREAD = 'rack.multithread'.freeze
     RACK_MULTIPROCESS = 'rack.multiprocess'.freeze
     RACK_RUN_ONCE = 'rack.run_once'.freeze
+ HTTP_REQUEST_BODY = 'http.request_body
+ ASYNC_CHUNK_CALLBACK = 'async.chunk_callback'.freeze
     ASYNC_CALLBACK = 'async.callback'.freeze
     ASYNC_CLOSE = 'async.close'.freeze
 
@@ -62,7 +63,8 @@ module Thin
 
         RACK_MULTITHREAD => false,
         RACK_MULTIPROCESS => false,
- RACK_RUN_ONCE => false
+ RACK_RUN_ONCE => false,
+ HTTP_REQUEST_BODY => ""
       }
     end
 
@@ -70,6 +72,7 @@ module Thin
     # Raises a +InvalidRequest+ if invalid.
     # Returns +true+ if the parsing is complete.
     def parse(data)
+ # push this data onto the http request body env string
+ env[HTTP_REQUEST_BODY] << data
       if @parser.finished? # Header finished, can only be some more body
         body << data
       else # Parse more header using the super parser
@@ -126,6 +129,10 @@ module Thin
       @env[RACK_MULTITHREAD] = value
     end
+
+ # add the chunk callback to the env
+ def async_chunk_callback=(chunk_callback)
+ @env[ASYNC_CHUNK_CALLBACK] = chunk_callback
+ end
+
     def async_callback=(callback)
       @env[ASYNC_CALLBACK] = callback
       @env[ASYNC_CLOSE] = EventMachine::DefaultDeferrable.new