Skip to content

Commit 8f7f6bc

Browse files
committed
cleanup docs + example api
1 parent 538670d commit 8f7f6bc

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
#!/usr/bin/env ruby
22
$:<< '../lib' << 'lib'
3-
43
require'goliath'
54

6-
classError < Goliath::API
5+
classEarlyAbort < Goliath::API
76
includeGoliath::Validation
87
MAX_SIZE=10
98
TEST_FILE="/tmp/goliath-test-error.log"
10-
9+
1110
defon_headers(env,headers)
1211
env.logger.info'received headers: ' + headers.inspect
1312
env['async-headers']=headers
14-
raiseGoliath::Validation::NotImplementedError.new("Can't handle requests with X-Crash: true.")ifenv['HTTP_X_CRASH'] && env['HTTP_X_CRASH'] == 'true'
13+
14+
ifenv['HTTP_X_CRASH'] && env['HTTP_X_CRASH'] == 'true'
15+
raiseGoliath::Validation::NotImplementedError.new("Can't handle requests with X-Crash: true.")
16+
end
1517
end
1618

1719
defon_body(env,data)
1820
env.logger.info'received data: ' + data
1921
(env['async-body'] ||= '') << data
2022
size=env['async-body'].size
21-
raiseGoliath::Validation::BadRequestError.new("Payload size can't exceed #{MAX_SIZE} bytes. Received #{size.inspect} bytes.")ifsize >= MAX_SIZE
23+
24+
ifsize >= MAX_SIZE
25+
raiseGoliath::Validation::BadRequestError.new("Payload size can't exceed #{MAX_SIZE} bytes. Received #{size.inspect} bytes.")
26+
end
2227
end
2328

2429
defon_close(env)
2530
env.logger.info'closing connection'
2631
end
2732

28-
defresponse(env)
33+
defresponse(env)
2934
File.open(TEST_FILE,"w+"){ |f| f << "response that should not be here"}
3035
[200,{},"OK"]
3136
end
32-
end
37+
end

‎lib/goliath/request.rb‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,10 @@ def server_exception(e)
202202
headers['Content-Length']=body.bytesize.to_s
203203
@env[:terminate_connection]=true
204204
post_process([status,headers,body])
205-
# Pass the request status to succeeded, so that the callback declared in
206-
# #post_process is executed as soon as possible, and not after the whole
207-
# response has been generated. For example, if you wanted to abort the
208-
# request in a #on_headers or #on_body hook (through exception raising),
209-
# the previous behaviour still went through all the hooks and the
210-
# #response method before returning the error. Now the error fires as
211-
# soon as possible. Note that #on_body and #response hooks may still be
212-
# executed if your machine is very fast or the request is very small,
213-
# but at least for long requests you don't get stuck until the full
214-
# request has been received.
205+
206+
# Mark the request as complete to force a flush on the response.
207+
# Note: #on_body and #response hooks may still fire if the data
208+
# is already in the parser buffer.
215209
succeed
216210
end
217211

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
require'spec_helper'
2-
requireFile.join(File.dirname(__FILE__),'../../','examples/error')
2+
requireFile.join(File.dirname(__FILE__),'../../','examples/early_abort')
33

4-
describeErrordo
4+
describeEarlyAbortdo
55
let(:err){Proc.new{fail"API request failed"}}
66

77
afterdo
8-
File.unlink(Error::TEST_FILE)ifFile.exist?(Error::TEST_FILE)
8+
File.unlink(EarlyAbort::TEST_FILE)ifFile.exist?(EarlyAbort::TEST_FILE)
99
end
1010

1111
it"should return OK"do
12-
with_api(Error)do
12+
with_api(EarlyAbort)do
1313
get_request({},err)do |c|
1414
c.response.should == "OK"
1515
end
1616
end
1717
end
18-
18+
1919
# The following two tests are very brittle, since they depend on the speed
2020
# of the machine executing the test and the size of the incoming data
2121
# packets. I hope someone more knowledgeable will be able to refactor these
2222
# ;-)
2323
it'fails without going in the response method if exception is raised in on_header hook'do
24-
with_api(Error)do
24+
with_api(EarlyAbort)do
2525
request_data={
2626
:body=>(["abcd"] * 200_000).join,
2727
:head=>{'X-Crash'=>'true'}
@@ -33,9 +33,9 @@
3333
end
3434
end
3535
end
36-
36+
3737
it'fails without going in the response method if exception is raised in on_body hook'do
38-
with_api(Error)do
38+
with_api(EarlyAbort)do
3939
request_data={
4040
:body=>(["abcd"] * 200_000).join
4141
}

0 commit comments

Comments
(0)