How to disconnect http request connection before the http request body has been retrieved?

933 views
Skip to first unread message

teerapap

unread,
Feb 14, 2011, 1:48:53 AM2/14/11
to nodejs
I'm writing a http server with Node.js.

I have a client to upload a large file to this server via HTTP
POST(multipart/data).

I want to accept the only connection which upload the file with valid
filename.(I have some conditions.) The invalid filename connection
should be disconnected before the server retrieves all the data.

I have no idea how to disconnect the http request connection and
return the proper http response.

The http request has only req.pause() method which isn't an answer.
and if I call req.connection.end(), the response.writeHeader()/write()
doesn't send the response.

Jann Horn

unread,
Feb 14, 2011, 11:56:28 AM2/14/11
to nod...@googlegroups.com
Am Sonntag, den 13.02.2011, 22:48 -0800 schrieb teerapap:
> The http request has only req.pause() method which isn't an answer.
> and if I call req.connection.end(), the response.writeHeader()/write()
> doesn't send the response.

First write and then end. Or call end with the data as argument.

req.write(data);
req.end();

is afaik equivalent to

req.end(data);

Jann

signature.asc

teerapap

unread,
Feb 14, 2011, 1:06:48 PM2/14/11
to nodejs
You mean res or req?

I've tried req.end("hello") and found this error

TypeError: Object #<an IncomingMessage> has no method 'end'

but for res.end("hello"), it doesn't stop the request.
>  signature.asc
> < 1KViewDownload

Preston Guillory

unread,
Feb 14, 2011, 1:56:42 PM2/14/11
to nod...@googlegroups.com
Did you try request.socket.destroy()?


--
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.


teerapap

unread,
Feb 15, 2011, 2:14:08 PM2/15/11
to nodejs
Yes, that works. Also request.connection.destroy(). Both cut the
connection.

However, it cuts the connection before the response has already sent
the data back with res.end(). How do I wait on response to be sent?

billywhizz

unread,
Feb 16, 2011, 11:09:03 PM2/16/11
to nodejs
in net.Stream you can call stream.write with a callback that will be
fired when data is flushed to the kernel. it doesn't look like the
callback is exposed in http library. maybe you should raise an issue
for this to be added to the http api?

http://nodejs.org/docs/v0.4.0/api/net.html#socket.write
Reply all
Reply to author
Forward
0 new messages