how to abort a http request?

1,926 views
Skip to first unread message

henrik

unread,
Aug 25, 2010, 9:00:01 AM8/25/10
to nodejs
hello,

I want to abort a http client request inside the http.createServer
callback function.

function validateUploadSize(headers) {
if (headers['content-length']) {
var bytes = parseInt(headers['content-length'], 10);
if(bytes > 1000000) {
return false;
} else {
return true;
}
} else {
return false;
}
}

var server = http.createServer(function(req, res) {
if (req.url == '/image') {
if(validateUploadSize(req.headers) == false) {
res.writeHead(413, { 'content-type': 'text/plain;
charset=utf-8' });
res.end('413');
} else {
// ...
}
} else {
res.writeHead(404, { 'content-type': 'text/plain;
charset=utf-8' });
res.end('404');
}
});

Now my question is how to I respond as fast as possible in case of an
"error"? Lets say someone wants to upload a 5MB jpeg. The content-
length will be available at the beginning and I don't want the user
upload the complete file and then responding that the file is too big.
In nginx there was the "client_max_body_size" option. But I could not
find out how to abort and respond with node.

Marco Rogers

unread,
Aug 25, 2010, 10:58:50 AM8/25/10
to nodejs
You can read req.headers['content-length'] and if it's too big, just
end the request right there.

// pseudo-code
if( parseInt(req.headers['content-length'], 10) > 5000) {
req.writeHead(500, {});
req.end('Your upload is too big');

chase sechrist

unread,
Aug 25, 2010, 4:24:30 PM8/25/10
to nodejs
For use cases other than the original post, is there an exposed way to
close connections abruptly, other than close()ing the fd?
Message has been deleted

bluesman

unread,
Aug 25, 2010, 4:36:50 PM8/25/10
to nodejs
i had done it like this:

req.connection.destroy();
var body = "Forbidden";
res.writeHead(403, {
'Content-Type': 'text/html',
'Content-Length':
Buffer.byteLength(body)
});
res.end(body);

Matt Ranney

unread,
Aug 25, 2010, 5:53:14 PM8/25/10
to nod...@googlegroups.com
On Wed, Aug 25, 2010 at 1:30 PM, bluesman <tomwa...@gmail.com> wrote:
                   req.connection.destroy();

I think this is the only required part.  After you destroy the socket, there's no place for a response to go.

Sam Tingleff

unread,
Aug 25, 2010, 6:19:45 PM8/25/10
to nodejs
Is there a counterpart to destroy() in earlier versions of node? We
have an app currently on pre-0.1.100 and without
req.connection.destroy() I do not believe it is properly closing
client connections.

On Aug 25, 2:53 pm, Matt Ranney <m...@ranney.com> wrote:

Terry Riegel

unread,
Aug 25, 2010, 10:31:04 PM8/25/10
to nod...@googlegroups.com
403 Forbidden would be an incorrect response for the op's situation...

From...
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

413 Request Entity Too Large

The server is refusing to process a request because the request entity is larger than the server is willing or able to process. The server MAY close the connection to prevent the client from continuing the request.


Would be more appropriate.

Terry

Sent from my iPhone

On Aug 25, 2010, at 4:30 PM, bluesman <tomwa...@gmail.com> wrote:

I had done it like this:


                   req.connection.destroy();
                   var body = "Forbidden";
                   res.writeHead(403, {
                           'Content-Type': 'text/html',
                               'Content-Length':
Buffer.byteLength(body)
                               });
                   res.end(body);


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Dean Landolt

unread,
Aug 26, 2010, 11:06:56 AM8/26/10
to nod...@googlegroups.com
On Wed, Aug 25, 2010 at 10:31 PM, Terry Riegel <rie...@clearimageonline.com> wrote:
403 Forbidden would be an incorrect response for the op's situation...

From...
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

413 Request Entity Too Large

That's what she said! Everyone drinks.

Micheil Smith

unread,
Aug 26, 2010, 11:14:39 AM8/26/10
to nod...@googlegroups.com
It could've also been:

411 Length Required

Just sayin'

– Micheil

Dean Landolt

unread,
Aug 26, 2010, 11:17:33 AM8/26/10
to nod...@googlegroups.com
On Thu, Aug 26, 2010 at 11:14 AM, Micheil Smith <mic...@brandedcode.com> wrote:
It could've also been:

       411 Length Required

Just sayin'


Heh. That's actually "Take a looooong drink." Hey, I didn't make the rules [1]

[1] http://statuscodedrinkinggame.com/

henrik

unread,
Sep 1, 2010, 7:28:30 AM9/1/10
to nodejs
hello,

thanks for your replies but none of your suggestions work the way
needed :-(
marcos suggestion does not cancel the upload and will respond after
the upload is complete.
bluesmans solution will abort the upload, but the response will never
reach the client. this could work when you don't care about the reason
the upload was aborted but if you want to abort on various criteria
(too big, bad mime type, ...) you can't do it within one request :-(

Isaac Schlueter

unread,
Sep 1, 2010, 1:27:16 PM9/1/10
to nod...@googlegroups.com
How do you need them to work? Generally, if it can be done in http or
tcp, it can be done by node.

Why won't bluesman's suggestion reach the client?

--i

henrik

unread,
Sep 4, 2010, 12:38:39 PM9/4/10
to nodejs
hi,

I simply want to return different http status codes:

200 if everything is ok, 413 if upload is too big and so on...

the response in bluesmans suggestion can't reach the client because
the connection is destroyed. request and response should share the
same connection.

Isaac Schlueter

unread,
Sep 4, 2010, 1:38:54 PM9/4/10
to nod...@googlegroups.com
So, what's wrong with just sending your response, and then removing
any listeners you have attached?

--i

Reply all
Reply to author
Forward
0 new messages