Skip to content

Commit 916b9ca

Browse files
committed
Add Request objects on the HTTP server can be interrupted.
1 parent 825d7a8 commit 916b9ca

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

src/http.cc

+7-10
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ void
8080
HTTPConnection::OnReceive (const void *buf, size_t len)
8181
{
8282
http_parser_execute(&parser_, static_cast<const char*>(buf), len);
83-
84-
if (http_parser_has_error(&parser_))
85-
ForceClose();
83+
if (http_parser_has_error(&parser_)) ForceClose();
8684
}
8785

8886
int
@@ -187,16 +185,15 @@ HTTPConnection::on_headers_complete (http_parser *parser)
187185
);
188186
message_handler->Set(HTTP_VERSION_SYMBOL, String::New(version));
189187

190-
// SHOULD KEEP ALIVE
191-
message_handler->Set( SHOULD_KEEP_ALIVE_SYMBOL
192-
, http_parser_should_keep_alive(&connection->parser_) ? True() : False()
193-
);
194-
188+
message_handler->Set(SHOULD_KEEP_ALIVE_SYMBOL,
189+
http_parser_should_keep_alive(&connection->parser_) ? True() : False());
195190

196-
Local<Value> on_headers_complete_v = message_handler->Get(ON_HEADERS_COMPLETE_SYMBOL);
191+
Local<Value> on_headers_complete_v =
192+
message_handler->Get(ON_HEADERS_COMPLETE_SYMBOL);
197193
if (on_headers_complete_v->IsFunction() == false) return 0;
198194

199-
Handle<Function> on_headers_complete = Handle<Function>::Cast(on_headers_complete_v);
195+
Handle<Function> on_headers_complete =
196+
Handle<Function>::Cast(on_headers_complete_v);
200197

201198
TryCatch try_catch;
202199
Local<Value> ret = on_headers_complete->Call(message_handler, 0, NULL);

src/http.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,24 @@ node.http.Server = function (RequestHandler, options) {
239239
var responses = [];
240240

241241
connection.onMessage = function ( ) {
242+
var interrupted = false;
242243
// filled in ...
243244
var req = { method : null // at onHeadersComplete
244245
, uri : "" // at onURI
245-
, httpVersion : null // at onHeadersComplete
246+
, httpVersion : null // at onHeadersComplete
246247
, headers : [] // at onHeaderField, onHeaderValue
247248
, onBody : null // by user
248249
, onBodyComplete : null // by user
250+
, interrupt : function ( ) { interrupted = true; }
249251
, setBodyEncoding : function (enc) {
250252
connection.setEncoding(enc);
251253
}
252-
}
254+
};
253255
var res = new node.http.ServerResponse(connection, responses);
254256

255257
this.onURI = function (data) {
256258
req.uri += data;
257-
return true
259+
return !interrupted;
258260
};
259261

260262
var last_was_value = false;
@@ -266,7 +268,7 @@ node.http.Server = function (RequestHandler, options) {
266268
else
267269
headers.push([data]);
268270
last_was_value = false;
269-
return true;
271+
return !interrupted;
270272
};
271273

272274
this.onHeaderValue = function (data) {
@@ -276,31 +278,29 @@ node.http.Server = function (RequestHandler, options) {
276278
else
277279
last_pair[1] += data;
278280
last_was_value = true;
279-
return true;
281+
return !interrupted;
280282
};
281283

282284
this.onHeadersComplete = function () {
283285
req.httpVersion = this.httpVersion;
284-
req.method = this.method;
285-
req.uri = node.http.parseUri(req.uri); // TODO parse the URI lazily
286-
286+
req.method = this.method;
287+
// TODO parse the URI lazily?
288+
req.uri = node.http.parseUri(req.uri);
287289
res.should_keep_alive = this.should_keep_alive;
288290

289-
return RequestHandler.apply(server, [req, res]);
291+
RequestHandler.apply(server, [req, res]);
292+
293+
return !interrupted;
290294
};
291295

292296
this.onBody = function (chunk) {
293-
if (req.onBody)
294-
return req.onBody(chunk);
295-
else
296-
return true;
297+
if (req.onBody) req.onBody(chunk);
298+
return !interrupted;
297299
};
298300

299301
this.onMessageComplete = function () {
300-
if (req.onBodyComplete)
301-
return req.onBodyComplete();
302-
else
303-
return true;
302+
if (req.onBodyComplete) req.onBodyComplete();
303+
return !interrupted;
304304
};
305305
};
306306

test/test-http-server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function onLoad() {
4343
c.onReceive = function (chunk) {
4444
server_response += chunk;
4545

46-
if ( requests_sent == 1) {
46+
if (requests_sent == 1) {
4747
c.send("POST /quit HTTP/1.1\r\n\r\n");
4848
c.close();
4949
assertEquals(c.readyState, "readOnly");

website/api.html

+10-2
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ <h3 id="http_server_request"><code>node.http.ServerRequest</code></h3>
622622
<code>"1.1"</code>, <code>"1.0"</code>
623623
</dd>
624624

625-
<dt><code>req.onBody</code></dt>
625+
<dt><code>req.onBody = function (chunk) { }; </code></dt>
626626
<dd>
627627
Callback. Should be set by the user to be informed of when a
628628
piece of the message body is received. Example:
@@ -640,7 +640,7 @@ <h3 id="http_server_request"><code>node.http.ServerRequest</code></h3>
640640
</p>
641641
</dd>
642642

643-
<dt><code>req.onBodyComplete</code></dt>
643+
<dt><code>req.onBodyComplete = function () { };</code></dt>
644644
<dd>
645645
Callback. Made exactly once for each message. No arguments.
646646
After <code>onBodyComplete</code> is executed
@@ -652,6 +652,14 @@ <h3 id="http_server_request"><code>node.http.ServerRequest</code></h3>
652652
Set the encoding for the request body. Either <code>"utf8"</code>
653653
or <code>"raw"</code>. Defaults to raw.
654654
</dd>
655+
656+
<dt><code>req.interrupt()</code></dt>
657+
<dd>
658+
Interrupt the request. You will not receive anymore callbacks.
659+
This is useful if, for example someone is streaming up a file but it
660+
is too large and neesd to be stopped. The connection to the client
661+
will be closed immediately.
662+
</dd>
655663
</dl>
656664

657665
<h3 id="http_server_response"><code>node.http.ServerResponse</code></h3>

0 commit comments

Comments
 (0)