2012-02-27 20:09:33 +01:00
|
|
|
# HTTP
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
To use the HTTP server and client one must `require('http')`.
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
The HTTP interfaces in io.js are designed to support many features
|
2010-10-28 14:18:16 +02:00
|
|
|
of the protocol which have been traditionally difficult to use.
|
|
|
|
In particular, large, possibly chunk-encoded, messages. The interface is
|
|
|
|
careful to never buffer entire requests or responses--the
|
|
|
|
user is able to stream data.
|
|
|
|
|
|
|
|
HTTP message headers are represented by an object like this:
|
|
|
|
|
2010-11-18 03:25:14 +01:00
|
|
|
{ 'content-length': '123',
|
|
|
|
'content-type': 'text/plain',
|
|
|
|
'connection': 'keep-alive',
|
2013-08-15 23:12:12 +02:00
|
|
|
'host': 'mysite.com',
|
2010-11-18 03:25:14 +01:00
|
|
|
'accept': '*/*' }
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Keys are lowercased. Values are not modified.
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
In order to support the full spectrum of possible HTTP applications, io.js's
|
2010-10-28 14:18:16 +02:00
|
|
|
HTTP API is very low-level. It deals with stream handling and message
|
|
|
|
parsing only. It parses a message into headers and body but it does not
|
|
|
|
parse the actual headers or the body.
|
|
|
|
|
2013-08-15 23:12:12 +02:00
|
|
|
Defined headers that allow multiple values are concatenated with a `,`
|
|
|
|
character, except for the `set-cookie` and `cookie` headers which are
|
|
|
|
represented as an array of values. Headers such as `content-length`
|
|
|
|
which can only have a single value are parsed accordingly, and only a
|
|
|
|
single value is represented on the parsed object.
|
|
|
|
|
|
|
|
The raw headers as they were received are retained in the `rawHeaders`
|
|
|
|
property, which is an array of `[key, value, key2, value2, ...]`. For
|
|
|
|
example, the previous message header object might have a `rawHeaders`
|
|
|
|
list like the following:
|
|
|
|
|
|
|
|
[ 'ConTent-Length', '123456',
|
|
|
|
'content-LENGTH', '123',
|
|
|
|
'content-type', 'text/plain',
|
|
|
|
'CONNECTION', 'keep-alive',
|
|
|
|
'Host', 'mysite.com',
|
|
|
|
'accepT', '*/*' ]
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-10-28 13:35:33 +01:00
|
|
|
## http.METHODS
|
|
|
|
|
|
|
|
* {Array}
|
|
|
|
|
|
|
|
A list of the HTTP methods that are supported by the parser.
|
|
|
|
|
2012-05-06 04:27:42 +02:00
|
|
|
## http.STATUS_CODES
|
|
|
|
|
|
|
|
* {Object}
|
|
|
|
|
|
|
|
A collection of all the standard HTTP response status codes, and the
|
|
|
|
short description of each. For example, `http.STATUS_CODES[404] === 'Not
|
|
|
|
Found'`.
|
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
## http.createServer([requestListener])
|
|
|
|
|
2013-08-15 01:15:49 +02:00
|
|
|
Returns a new instance of [http.Server](#http_class_http_server).
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
|
|
The `requestListener` is a function which is automatically
|
|
|
|
added to the `'request'` event.
|
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
## http.createClient([port][, host])
|
2012-04-23 06:37:37 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
This function is **deprecated**; please use [http.request()][] instead.
|
|
|
|
Constructs a new HTTP client. `port` and `host` refer to the server to be
|
|
|
|
connected to.
|
2012-04-23 06:37:37 +02:00
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
## Class: http.Server
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
This is an [EventEmitter][] with the following events:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### Event: 'request'
|
|
|
|
|
|
|
|
`function (request, response) { }`
|
|
|
|
|
2011-08-31 15:12:34 +02:00
|
|
|
Emitted each time there is a request. Note that there may be multiple requests
|
2011-03-30 15:06:41 +02:00
|
|
|
per connection (in the case of keep-alive connections).
|
2013-10-16 12:20:25 +02:00
|
|
|
`request` is an instance of [http.IncomingMessage][] and `response` is
|
|
|
|
an instance of [http.ServerResponse][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### Event: 'connection'
|
|
|
|
|
2011-07-16 02:45:43 +02:00
|
|
|
`function (socket) { }`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-15 23:12:12 +02:00
|
|
|
When a new TCP stream is established. `socket` is an object of type
|
|
|
|
`net.Socket`. Usually users will not want to access this event. In
|
|
|
|
particular, the socket will not emit `readable` events because of how
|
|
|
|
the protocol parser attaches to the socket. The `socket` can also be
|
|
|
|
accessed at `request.connection`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### Event: 'close'
|
|
|
|
|
2011-11-02 17:24:53 +01:00
|
|
|
`function () { }`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-15 23:12:12 +02:00
|
|
|
Emitted when the server closes.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
### Event: 'checkContinue'
|
|
|
|
|
2011-06-11 15:09:40 +02:00
|
|
|
`function (request, response) { }`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Emitted each time a request with an http Expect: 100-continue is received.
|
|
|
|
If this event isn't listened for, the server will automatically respond
|
|
|
|
with a 100 Continue as appropriate.
|
|
|
|
|
2013-10-16 12:20:25 +02:00
|
|
|
Handling this event involves calling [response.writeContinue()][] if the client
|
2010-10-28 14:18:16 +02:00
|
|
|
should continue to send the request body, or generating an appropriate HTTP
|
|
|
|
response (e.g., 400 Bad Request) if the client should not continue to send the
|
|
|
|
request body.
|
|
|
|
|
|
|
|
Note that when this event is emitted and handled, the `request` event will
|
|
|
|
not be emitted.
|
|
|
|
|
2012-01-09 03:51:06 +01:00
|
|
|
### Event: 'connect'
|
|
|
|
|
2013-06-13 02:45:30 +02:00
|
|
|
`function (request, socket, head) { }`
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
|
|
Emitted each time a client requests a http CONNECT method. If this event isn't
|
|
|
|
listened for, then clients requesting a CONNECT method will have their
|
|
|
|
connections closed.
|
|
|
|
|
|
|
|
* `request` is the arguments for the http request, as it is in the request
|
|
|
|
event.
|
|
|
|
* `socket` is the network socket between the server and client.
|
2013-06-13 02:45:30 +02:00
|
|
|
* `head` is an instance of Buffer, the first packet of the tunneling stream,
|
|
|
|
this may be empty.
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
|
|
After this event is emitted, the request's socket will not have a `data`
|
|
|
|
event listener, meaning you will need to bind to it in order to handle data
|
|
|
|
sent to the server on that socket.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### Event: 'upgrade'
|
|
|
|
|
2013-06-13 02:45:30 +02:00
|
|
|
`function (request, socket, head) { }`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Emitted each time a client requests a http upgrade. If this event isn't
|
|
|
|
listened for, then clients requesting an upgrade will have their connections
|
|
|
|
closed.
|
|
|
|
|
2012-01-09 03:51:06 +01:00
|
|
|
* `request` is the arguments for the http request, as it is in the request
|
|
|
|
event.
|
2010-10-28 14:18:16 +02:00
|
|
|
* `socket` is the network socket between the server and client.
|
2013-06-13 02:45:30 +02:00
|
|
|
* `head` is an instance of Buffer, the first packet of the upgraded stream,
|
|
|
|
this may be empty.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
After this event is emitted, the request's socket will not have a `data`
|
|
|
|
event listener, meaning you will need to bind to it in order to handle data
|
|
|
|
sent to the server on that socket.
|
|
|
|
|
|
|
|
### Event: 'clientError'
|
|
|
|
|
2012-10-08 01:22:44 +02:00
|
|
|
`function (exception, socket) { }`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-02-15 23:21:48 +01:00
|
|
|
If a client connection emits an 'error' event, it will be forwarded here.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-10-08 01:22:44 +02:00
|
|
|
`socket` is the `net.Socket` object that the error originated from.
|
|
|
|
|
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
### server.listen(port[, hostname][, backlog][, callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-01-19 03:24:11 +01:00
|
|
|
Begin accepting connections on the specified `port` and `hostname`. If the
|
|
|
|
`hostname` is omitted, the server will accept connections on any IPv6 address
|
|
|
|
(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise. A
|
|
|
|
port value of zero will assign a random port.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
To listen to a unix socket, supply a filename instead of port and hostname.
|
|
|
|
|
2012-04-18 15:56:14 +02:00
|
|
|
Backlog is the maximum length of the queue of pending connections.
|
|
|
|
The actual length will be determined by your OS through sysctl settings such as
|
|
|
|
`tcp_max_syn_backlog` and `somaxconn` on linux. The default value of this
|
|
|
|
parameter is 511 (not 512).
|
|
|
|
|
2011-12-16 10:20:27 +01:00
|
|
|
This function is asynchronous. The last parameter `callback` will be added as
|
2012-06-06 21:05:18 +02:00
|
|
|
a listener for the ['listening'][] event. See also [net.Server.listen(port)][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
### server.listen(path[, callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Start a UNIX socket server listening for connections on the given `path`.
|
|
|
|
|
2011-12-16 10:20:27 +01:00
|
|
|
This function is asynchronous. The last parameter `callback` will be added as
|
2012-06-06 21:05:18 +02:00
|
|
|
a listener for the ['listening'][] event. See also [net.Server.listen(path)][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
### server.listen(handle[, callback])
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
|
|
* `handle` {Object}
|
2012-10-08 19:10:29 +02:00
|
|
|
* `callback` {Function}
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
|
|
The `handle` object can be set to either a server or socket (anything
|
|
|
|
with an underlying `_handle` member), or a `{fd: <n>}` object.
|
|
|
|
|
|
|
|
This will cause the server to accept connections on the specified
|
|
|
|
handle, but it is presumed that the file descriptor or handle has
|
|
|
|
already been bound to a port or domain socket.
|
|
|
|
|
|
|
|
Listening on a file descriptor is not supported on Windows.
|
|
|
|
|
|
|
|
This function is asynchronous. The last parameter `callback` will be added as
|
|
|
|
a listener for the ['listening'](net.html#event_listening_) event.
|
2012-10-08 19:10:29 +02:00
|
|
|
See also [net.Server.listen()](net.html#net_server_listen_handle_callback).
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2012-10-08 19:10:29 +02:00
|
|
|
### server.close([callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
Stops the server from accepting new connections. See [net.Server.close()][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
2012-01-15 20:45:31 +01:00
|
|
|
### server.maxHeadersCount
|
|
|
|
|
|
|
|
Limits maximum incoming headers count, equal to 1000 by default. If set to 0 -
|
|
|
|
no limit will be applied.
|
|
|
|
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-04 08:29:22 +01:00
|
|
|
### server.setTimeout(msecs, callback)
|
2012-01-15 20:45:31 +01:00
|
|
|
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-04 08:29:22 +01:00
|
|
|
* `msecs` {Number}
|
|
|
|
* `callback` {Function}
|
|
|
|
|
|
|
|
Sets the timeout value for sockets, and emits a `'timeout'` event on
|
|
|
|
the Server object, passing the socket as an argument, if a timeout
|
|
|
|
occurs.
|
|
|
|
|
|
|
|
If there is a `'timeout'` event listener on the Server object, then it
|
|
|
|
will be called with the timed-out socket as an argument.
|
|
|
|
|
|
|
|
By default, the Server's timeout value is 2 minutes, and sockets are
|
|
|
|
destroyed automatically if they time out. However, if you assign a
|
|
|
|
callback to the Server's `'timeout'` event, then you are responsible
|
|
|
|
for handling socket timeouts.
|
|
|
|
|
|
|
|
### server.timeout
|
|
|
|
|
|
|
|
* {Number} Default = 120000 (2 minutes)
|
|
|
|
|
|
|
|
The number of milliseconds of inactivity before a socket is presumed
|
|
|
|
to have timed out.
|
|
|
|
|
|
|
|
Note that the socket timeout logic is set up on connection, so
|
|
|
|
changing this value only affects *new* connections to the server, not
|
|
|
|
any existing connections.
|
|
|
|
|
|
|
|
Set to 0 to disable any kind of automatic timeout behavior on incoming
|
|
|
|
connections.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
## Class: http.ServerResponse
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
This object is created internally by a HTTP server--not by the user. It is
|
2012-02-05 11:11:54 +01:00
|
|
|
passed as the second parameter to the `'request'` event.
|
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
The response implements the [Writable Stream][] interface. This is an
|
|
|
|
[EventEmitter][] with the following events:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-01-05 12:50:03 +01:00
|
|
|
### Event: 'close'
|
|
|
|
|
|
|
|
`function () { }`
|
|
|
|
|
2013-03-28 15:19:10 +01:00
|
|
|
Indicates that the underlying connection was terminated before
|
2013-10-16 12:20:25 +02:00
|
|
|
[response.end()][] was called or able to flush.
|
2012-01-05 12:50:03 +01:00
|
|
|
|
2013-11-26 21:49:28 +01:00
|
|
|
### Event: 'finish'
|
|
|
|
|
|
|
|
`function () { }`
|
|
|
|
|
|
|
|
Emitted when the response has been sent. More specifically, this event is
|
|
|
|
emitted when the last segment of the response headers and body have been
|
|
|
|
handed off to the operating system for transmission over the network. It
|
|
|
|
does not imply that the client has received anything yet.
|
|
|
|
|
|
|
|
After this event, no more events will be emitted on the response object.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### response.writeContinue()
|
|
|
|
|
|
|
|
Sends a HTTP/1.1 100 Continue message to the client, indicating that
|
2012-06-06 21:05:18 +02:00
|
|
|
the request body should be sent. See the ['checkContinue'][] event on `Server`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
### response.writeHead(statusCode[, statusMessage][, headers])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Sends a response header to the request. The status code is a 3-digit HTTP
|
|
|
|
status code, like `404`. The last argument, `headers`, are the response headers.
|
2013-10-17 02:11:19 +02:00
|
|
|
Optionally one can give a human-readable `statusMessage` as the second
|
2010-10-28 14:18:16 +02:00
|
|
|
argument.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
var body = 'hello world';
|
|
|
|
response.writeHead(200, {
|
|
|
|
'Content-Length': body.length,
|
2010-11-18 03:25:14 +01:00
|
|
|
'Content-Type': 'text/plain' });
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
This method must only be called once on a message and it must
|
2013-10-16 12:20:25 +02:00
|
|
|
be called before [response.end()][] is called.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-10-16 12:20:25 +02:00
|
|
|
If you call [response.write()][] or [response.end()][] before calling this, the
|
2011-02-10 11:18:13 +01:00
|
|
|
implicit/mutable headers will be calculated and call this function for you.
|
|
|
|
|
2013-02-15 23:21:48 +01:00
|
|
|
Note that Content-Length is given in bytes not characters. The above example
|
2011-04-15 00:11:35 +02:00
|
|
|
works because the string `'hello world'` contains only single byte characters.
|
|
|
|
If the body contains higher coded characters then `Buffer.byteLength()`
|
|
|
|
should be used to determine the number of bytes in a given encoding.
|
2015-02-07 11:25:13 +01:00
|
|
|
And io.js does not check whether Content-Length and the length of the body
|
2011-07-29 20:03:58 +02:00
|
|
|
which has been transmitted are equal or not.
|
2011-04-15 00:11:35 +02:00
|
|
|
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-04 08:29:22 +01:00
|
|
|
### response.setTimeout(msecs, callback)
|
|
|
|
|
|
|
|
* `msecs` {Number}
|
|
|
|
* `callback` {Function}
|
|
|
|
|
|
|
|
Sets the Socket's timeout value to `msecs`. If a callback is
|
|
|
|
provided, then it is added as a listener on the `'timeout'` event on
|
|
|
|
the response object.
|
|
|
|
|
|
|
|
If no `'timeout'` listener is added to the request, the response, or
|
|
|
|
the server, then sockets are destroyed when they time out. If you
|
|
|
|
assign a handler on the request, the response, or the server's
|
|
|
|
`'timeout'` events, then it is your responsibility to handle timed out
|
|
|
|
sockets.
|
|
|
|
|
2011-02-10 11:18:13 +01:00
|
|
|
### response.statusCode
|
|
|
|
|
2013-10-16 12:20:25 +02:00
|
|
|
When using implicit headers (not calling [response.writeHead()][] explicitly),
|
|
|
|
this property controls the status code that will be sent to the client when
|
|
|
|
the headers get flushed.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
response.statusCode = 404;
|
|
|
|
|
2011-07-21 17:20:47 +02:00
|
|
|
After response header was sent to the client, this property indicates the
|
|
|
|
status code which was sent out.
|
|
|
|
|
2013-10-17 02:11:19 +02:00
|
|
|
### response.statusMessage
|
|
|
|
|
|
|
|
When using implicit headers (not calling `response.writeHead()` explicitly), this property
|
|
|
|
controls the status message that will be sent to the client when the headers get
|
|
|
|
flushed. If this is left as `undefined` then the standard message for the status
|
|
|
|
code will be used.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
response.statusMessage = 'Not found';
|
|
|
|
|
|
|
|
After response header was sent to the client, this property indicates the
|
|
|
|
status message which was sent out.
|
|
|
|
|
2011-02-10 11:18:13 +01:00
|
|
|
### response.setHeader(name, value)
|
|
|
|
|
|
|
|
Sets a single header value for implicit headers. If this header already exists
|
2011-08-31 15:12:34 +02:00
|
|
|
in the to-be-sent headers, its value will be replaced. Use an array of strings
|
2011-02-10 11:18:13 +01:00
|
|
|
here if you need to send multiple headers with the same name.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
response.setHeader("Content-Type", "text/html");
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
response.setHeader("Set-Cookie", ["type=ninja", "language=javascript"]);
|
|
|
|
|
2012-09-18 18:26:08 +02:00
|
|
|
### response.headersSent
|
|
|
|
|
|
|
|
Boolean (read-only). True if headers were sent, false otherwise.
|
|
|
|
|
2012-02-14 21:38:24 +01:00
|
|
|
### response.sendDate
|
|
|
|
|
2013-06-11 22:56:03 +02:00
|
|
|
When true, the Date header will be automatically generated and sent in
|
2012-02-14 21:38:24 +01:00
|
|
|
the response if it is not already present in the headers. Defaults to true.
|
|
|
|
|
|
|
|
This should only be disabled for testing; HTTP requires the Date header
|
|
|
|
in responses.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
|
|
|
### response.getHeader(name)
|
|
|
|
|
|
|
|
Reads out a header that's already been queued but not sent to the client. Note
|
|
|
|
that the name is case insensitive. This can only be called before headers get
|
|
|
|
implicitly flushed.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
var contentType = response.getHeader('content-type');
|
|
|
|
|
|
|
|
### response.removeHeader(name)
|
|
|
|
|
|
|
|
Removes a header that's queued for implicit sending.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
response.removeHeader("Content-Encoding");
|
|
|
|
|
|
|
|
|
2014-12-02 04:14:57 +01:00
|
|
|
### response.write(chunk[, encoding][, callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-10-16 12:20:25 +02:00
|
|
|
If this method is called and [response.writeHead()][] has not been called,
|
|
|
|
it will switch to implicit header mode and flush the implicit headers.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
|
|
|
This sends a chunk of the response body. This method may
|
2010-10-28 14:18:16 +02:00
|
|
|
be called multiple times to provide successive parts of the body.
|
|
|
|
|
|
|
|
`chunk` can be a string or a buffer. If `chunk` is a string,
|
|
|
|
the second parameter specifies how to encode it into a byte stream.
|
2014-12-02 04:14:57 +01:00
|
|
|
By default the `encoding` is `'utf8'`. The last parameter `callback`
|
|
|
|
will be called when this chunk of data is flushed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
**Note**: This is the raw HTTP body and has nothing to do with
|
|
|
|
higher-level multi-part body encodings that may be used.
|
|
|
|
|
|
|
|
The first time `response.write()` is called, it will send the buffered
|
|
|
|
header information and the first body to the client. The second time
|
2015-02-07 11:25:13 +01:00
|
|
|
`response.write()` is called, io.js assumes you're going to be streaming
|
2010-10-28 14:18:16 +02:00
|
|
|
data, and sends that separately. That is, the response is buffered up to the
|
|
|
|
first chunk of body.
|
|
|
|
|
2012-07-31 06:15:46 +02:00
|
|
|
Returns `true` if the entire data was flushed successfully to the kernel
|
|
|
|
buffer. Returns `false` if all or part of the data was queued in user memory.
|
2013-02-15 23:21:48 +01:00
|
|
|
`'drain'` will be emitted when the buffer is free again.
|
2012-07-31 06:15:46 +02:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### response.addTrailers(headers)
|
|
|
|
|
|
|
|
This method adds HTTP trailing headers (a header but at the end of the
|
2010-11-21 23:22:34 +01:00
|
|
|
message) to the response.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
Trailers will **only** be emitted if chunked encoding is used for the
|
2010-10-28 14:18:16 +02:00
|
|
|
response; if it is not (e.g., if the request was HTTP/1.0), they will
|
|
|
|
be silently discarded.
|
|
|
|
|
|
|
|
Note that HTTP requires the `Trailer` header to be sent if you intend to
|
|
|
|
emit trailers, with a list of the header fields in its value. E.g.,
|
|
|
|
|
|
|
|
response.writeHead(200, { 'Content-Type': 'text/plain',
|
2011-11-14 19:58:58 +01:00
|
|
|
'Trailer': 'Content-MD5' });
|
2010-10-28 14:18:16 +02:00
|
|
|
response.write(fileData);
|
|
|
|
response.addTrailers({'Content-MD5': "7895bf4b8828b55ceaf47747b4bca667"});
|
|
|
|
response.end();
|
|
|
|
|
|
|
|
|
2014-12-02 04:14:57 +01:00
|
|
|
### response.end([data][, encoding][, callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
This method signals to the server that all of the response headers and body
|
2012-08-29 18:52:55 +02:00
|
|
|
have been sent; that server should consider this message complete.
|
2010-10-28 14:18:16 +02:00
|
|
|
The method, `response.end()`, MUST be called on each
|
|
|
|
response.
|
|
|
|
|
2014-12-02 04:14:57 +01:00
|
|
|
If `data` is specified, it is equivalent to calling
|
|
|
|
`response.write(data, encoding)` followed by `response.end(callback)`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-12-18 08:52:42 +01:00
|
|
|
If `callback` is specified, it will be called when the response stream
|
|
|
|
is finished.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## http.request(options[, callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
io.js maintains several connections per server to make HTTP requests.
|
2012-05-16 16:27:34 +02:00
|
|
|
This function allows one to transparently issue requests.
|
|
|
|
|
|
|
|
`options` can be an object or a string. If `options` is a string, it is
|
2012-06-06 21:05:18 +02:00
|
|
|
automatically parsed with [url.parse()][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-21 02:54:59 +01:00
|
|
|
Options:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-21 02:54:59 +01:00
|
|
|
- `host`: A domain name or IP address of the server to issue the request to.
|
2011-10-22 16:40:15 +02:00
|
|
|
Defaults to `'localhost'`.
|
2011-11-26 03:26:11 +01:00
|
|
|
- `hostname`: To support `url.parse()` `hostname` is preferred over `host`
|
2011-10-22 16:40:15 +02:00
|
|
|
- `port`: Port of remote server. Defaults to 80.
|
2012-02-23 16:37:49 +01:00
|
|
|
- `localAddress`: Local interface to bind for network connections.
|
2011-04-26 01:04:07 +02:00
|
|
|
- `socketPath`: Unix Domain Socket (use one of host:port or socketPath)
|
2011-10-22 16:40:15 +02:00
|
|
|
- `method`: A string specifying the HTTP request method. Defaults to `'GET'`.
|
|
|
|
- `path`: Request path. Defaults to `'/'`. Should include query string if any.
|
2013-05-15 22:25:45 +02:00
|
|
|
E.G. `'/index.html?page=12'`. An exception is thrown when the request path
|
|
|
|
contains illegal characters. Currently, only spaces are rejected but that
|
|
|
|
may change in the future.
|
2011-01-21 02:54:59 +01:00
|
|
|
- `headers`: An object containing request headers.
|
2011-10-20 00:06:49 +02:00
|
|
|
- `auth`: Basic authentication i.e. `'user:password'` to compute an
|
|
|
|
Authorization header.
|
2012-06-06 21:05:18 +02:00
|
|
|
- `agent`: Controls [Agent][] behavior. When an Agent is used request will
|
|
|
|
default to `Connection: keep-alive`. Possible values:
|
|
|
|
- `undefined` (default): use [global Agent][] for this host and port.
|
2011-06-28 21:50:42 +02:00
|
|
|
- `Agent` object: explicitly use the passed in `Agent`.
|
2011-10-22 16:40:15 +02:00
|
|
|
- `false`: opts out of connection pooling with an Agent, defaults request to
|
|
|
|
`Connection: close`.
|
2011-01-21 02:54:59 +01:00
|
|
|
|
2013-11-28 21:25:30 +01:00
|
|
|
The optional `callback` parameter will be added as a one time listener for
|
|
|
|
the ['response'][] event.
|
|
|
|
|
2013-10-16 12:20:25 +02:00
|
|
|
`http.request()` returns an instance of the [http.ClientRequest][]
|
2011-01-21 02:54:59 +01:00
|
|
|
class. The `ClientRequest` instance is a writable stream. If one needs to
|
|
|
|
upload a file with a POST request, then write to the `ClientRequest` object.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2014-09-12 11:25:15 +02:00
|
|
|
var postData = querystring.stringify({
|
|
|
|
'msg' : 'Hello World!'
|
|
|
|
});
|
|
|
|
|
2011-01-21 02:54:59 +01:00
|
|
|
var options = {
|
2012-10-29 04:16:40 +01:00
|
|
|
hostname: 'www.google.com',
|
2011-01-21 02:54:59 +01:00
|
|
|
port: 80,
|
|
|
|
path: '/upload',
|
2014-09-12 11:25:15 +02:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'Content-Length': postData.length
|
|
|
|
}
|
2011-01-21 02:54:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var req = http.request(options, function(res) {
|
|
|
|
console.log('STATUS: ' + res.statusCode);
|
|
|
|
console.log('HEADERS: ' + JSON.stringify(res.headers));
|
|
|
|
res.setEncoding('utf8');
|
|
|
|
res.on('data', function (chunk) {
|
2010-10-28 14:18:16 +02:00
|
|
|
console.log('BODY: ' + chunk);
|
|
|
|
});
|
2015-01-15 13:58:31 +01:00
|
|
|
res.on('end', function() {
|
|
|
|
console.log('No more data in response.')
|
|
|
|
})
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
2011-05-06 00:40:45 +02:00
|
|
|
req.on('error', function(e) {
|
|
|
|
console.log('problem with request: ' + e.message);
|
|
|
|
});
|
|
|
|
|
2011-01-21 02:54:59 +01:00
|
|
|
// write data to request body
|
2014-09-12 11:25:15 +02:00
|
|
|
req.write(postData);
|
2011-01-21 02:54:59 +01:00
|
|
|
req.end();
|
|
|
|
|
|
|
|
Note that in the example `req.end()` was called. With `http.request()` one
|
|
|
|
must always call `req.end()` to signify that you're done with the request -
|
|
|
|
even if there is no data being written to the request body.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-21 02:54:59 +01:00
|
|
|
If any error is encountered during the request (be that with DNS resolution,
|
|
|
|
TCP level errors, or actual HTTP parse errors) an `'error'` event is emitted
|
|
|
|
on the returned request object.
|
|
|
|
|
|
|
|
There are a few special headers that should be noted.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
* Sending a 'Connection: keep-alive' will notify io.js that the connection to
|
2010-10-28 14:18:16 +02:00
|
|
|
the server should be persisted until the next request.
|
|
|
|
|
|
|
|
* Sending a 'Content-length' header will disable the default chunked encoding.
|
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
* Sending an 'Expect' header will immediately send the request headers.
|
2010-10-28 14:18:16 +02:00
|
|
|
Usually, when sending 'Expect: 100-continue', you should both set a timeout
|
|
|
|
and listen for the `continue` event. See RFC2616 Section 8.2.3 for more
|
|
|
|
information.
|
|
|
|
|
2011-11-26 03:26:11 +01:00
|
|
|
* Sending an Authorization header will override using the `auth` option
|
2011-10-20 00:06:49 +02:00
|
|
|
to compute basic authentication.
|
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## http.get(options[, callback])
|
2011-01-21 02:54:59 +01:00
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
Since most requests are GET requests without bodies, io.js provides this
|
2012-05-16 16:27:34 +02:00
|
|
|
convenience method. The only difference between this method and `http.request()`
|
|
|
|
is that it sets the method to GET and calls `req.end()` automatically.
|
2011-01-21 02:54:59 +01:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2012-05-16 16:27:34 +02:00
|
|
|
http.get("http://www.google.com/index.html", function(res) {
|
2011-01-21 02:54:59 +01:00
|
|
|
console.log("Got response: " + res.statusCode);
|
|
|
|
}).on('error', function(e) {
|
|
|
|
console.log("Got error: " + e.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
## Class: http.Agent
|
2011-01-21 02:54:59 +01:00
|
|
|
|
2013-08-05 06:30:23 +02:00
|
|
|
The HTTP Agent is used for pooling sockets used in HTTP client
|
|
|
|
requests.
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2013-08-05 06:30:23 +02:00
|
|
|
The HTTP Agent also defaults client requests to using
|
|
|
|
Connection:keep-alive. If no pending HTTP requests are waiting on a
|
2015-02-07 11:25:13 +01:00
|
|
|
socket to become free the socket is closed. This means that io.js's
|
2013-08-05 06:30:23 +02:00
|
|
|
pool has the benefit of keep-alive when under load but still does not
|
|
|
|
require developers to manually close the HTTP clients using
|
|
|
|
KeepAlive.
|
|
|
|
|
|
|
|
If you opt into using HTTP KeepAlive, you can create an Agent object
|
|
|
|
with that flag set to `true`. (See the [constructor
|
|
|
|
options](#http_new_agent_options) below.) Then, the Agent will keep
|
|
|
|
unused sockets in a pool for later use. They will be explicitly
|
2015-02-07 11:25:13 +01:00
|
|
|
marked so as to not keep the io.js process running. However, it is
|
2013-08-05 06:30:23 +02:00
|
|
|
still a good idea to explicitly [`destroy()`](#http_agent_destroy)
|
|
|
|
KeepAlive agents when they are no longer in use, so that the Sockets
|
|
|
|
will be shut down.
|
|
|
|
|
|
|
|
Sockets are removed from the agent's pool when the socket emits either
|
|
|
|
a "close" event or a special "agentRemove" event. This means that if
|
|
|
|
you intend to keep one HTTP request open for a long time and don't
|
|
|
|
want it to stay in the pool you can do something along the lines of:
|
2011-08-12 23:31:17 +02:00
|
|
|
|
|
|
|
http.get(options, function(res) {
|
|
|
|
// Do stuff
|
|
|
|
}).on("socket", function (socket) {
|
|
|
|
socket.emit("agentRemove");
|
|
|
|
});
|
2011-10-20 01:28:44 +02:00
|
|
|
|
2013-08-05 06:30:23 +02:00
|
|
|
Alternatively, you could just opt out of pooling entirely using
|
|
|
|
`agent:false`:
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2013-08-05 06:30:23 +02:00
|
|
|
http.get({
|
|
|
|
hostname: 'localhost',
|
|
|
|
port: 80,
|
|
|
|
path: '/',
|
|
|
|
agent: false // create a new agent just for this one request
|
|
|
|
}, function (res) {
|
|
|
|
// Do stuff with response
|
2011-08-12 23:31:17 +02:00
|
|
|
})
|
|
|
|
|
2013-08-05 06:30:23 +02:00
|
|
|
### new Agent([options])
|
|
|
|
|
|
|
|
* `options` {Object} Set of configurable options to set on the agent.
|
|
|
|
Can have the following fields:
|
|
|
|
* `keepAlive` {Boolean} Keep sockets around in a pool to be used by
|
|
|
|
other requests in the future. Default = `false`
|
|
|
|
* `keepAliveMsecs` {Integer} When using HTTP KeepAlive, how often
|
|
|
|
to send TCP KeepAlive packets over sockets being kept alive.
|
|
|
|
Default = `1000`. Only relevant if `keepAlive` is set to `true`.
|
|
|
|
* `maxSockets` {Number} Maximum number of sockets to allow per
|
|
|
|
host. Default = `Infinity`.
|
|
|
|
* `maxFreeSockets` {Number} Maximum number of sockets to leave open
|
|
|
|
in a free state. Only relevant if `keepAlive` is set to `true`.
|
|
|
|
Default = `256`.
|
|
|
|
|
|
|
|
The default `http.globalAgent` that is used by `http.request` has all
|
|
|
|
of these values set to their respective defaults.
|
|
|
|
|
|
|
|
To configure any of them, you must create your own `Agent` object.
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
var http = require('http');
|
|
|
|
var keepAliveAgent = new http.Agent({ keepAlive: true });
|
2014-03-25 21:02:42 +01:00
|
|
|
options.agent = keepAliveAgent;
|
|
|
|
http.request(options, onResponseCallback);
|
2013-08-05 06:30:23 +02:00
|
|
|
```
|
|
|
|
|
2011-07-26 00:13:24 +02:00
|
|
|
### agent.maxSockets
|
|
|
|
|
2014-02-09 01:45:27 +01:00
|
|
|
By default set to Infinity. Determines how many concurrent sockets the agent
|
|
|
|
can have open per origin. Origin is either a 'host:port' or
|
|
|
|
'host:port:localAddress' combination.
|
2013-08-05 06:30:23 +02:00
|
|
|
|
|
|
|
### agent.maxFreeSockets
|
|
|
|
|
|
|
|
By default set to 256. For Agents supporting HTTP KeepAlive, this
|
|
|
|
sets the maximum number of sockets that will be left open in the free
|
|
|
|
state.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
|
|
### agent.sockets
|
|
|
|
|
2013-08-05 06:30:23 +02:00
|
|
|
An object which contains arrays of sockets currently in use by the
|
|
|
|
Agent. Do not modify.
|
|
|
|
|
|
|
|
### agent.freeSockets
|
|
|
|
|
|
|
|
An object which contains arrays of sockets currently awaiting use by
|
|
|
|
the Agent when HTTP KeepAlive is used. Do not modify.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
|
|
### agent.requests
|
|
|
|
|
2013-06-11 22:56:03 +02:00
|
|
|
An object which contains queues of requests that have not yet been assigned to
|
2011-08-12 23:31:17 +02:00
|
|
|
sockets. Do not modify.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2013-08-05 06:30:23 +02:00
|
|
|
### agent.destroy()
|
|
|
|
|
|
|
|
Destroy any sockets that are currently in use by the agent.
|
|
|
|
|
|
|
|
It is usually not necessary to do this. However, if you are using an
|
|
|
|
agent with KeepAlive enabled, then it is best to explicitly shut down
|
|
|
|
the agent when you know that it will no longer be used. Otherwise,
|
|
|
|
sockets may hang open for quite a long time before the server
|
|
|
|
terminates them.
|
|
|
|
|
|
|
|
### agent.getName(options)
|
|
|
|
|
|
|
|
Get a unique name for a set of request options, to determine whether a
|
|
|
|
connection can be reused. In the http agent, this returns
|
|
|
|
`host:port:localAddress`. In the https agent, the name includes the
|
|
|
|
CA, cert, ciphers, and other HTTPS/TLS-specific options that determine
|
|
|
|
socket reusability.
|
|
|
|
|
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
## http.globalAgent
|
|
|
|
|
|
|
|
Global instance of Agent which is used as the default for all http client
|
|
|
|
requests.
|
|
|
|
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2012-02-27 20:09:33 +01:00
|
|
|
## Class: http.ClientRequest
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
|
|
This object is created internally and returned from `http.request()`. It
|
|
|
|
represents an _in-progress_ request whose header has already been queued. The
|
|
|
|
header is still mutable using the `setHeader(name, value)`, `getHeader(name)`,
|
|
|
|
`removeHeader(name)` API. The actual header will be sent along with the first
|
|
|
|
data chunk or when closing the connection.
|
|
|
|
|
|
|
|
To get the response, add a listener for `'response'` to the request object.
|
|
|
|
`'response'` will be emitted from the request object when the response
|
|
|
|
headers have been received. The `'response'` event is executed with one
|
2013-10-16 12:20:25 +02:00
|
|
|
argument which is an instance of [http.IncomingMessage][].
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
|
|
During the `'response'` event, one can add listeners to the
|
2013-04-05 20:07:53 +02:00
|
|
|
response object; particularly to listen for the `'data'` event.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2013-04-05 20:07:53 +02:00
|
|
|
If no `'response'` handler is added, then the response will be
|
|
|
|
entirely discarded. However, if you add a `'response'` event handler,
|
|
|
|
then you **must** consume the data from the response object, either by
|
|
|
|
calling `response.read()` whenever there is a `'readable'` event, or
|
|
|
|
by adding a `'data'` handler, or by calling the `.resume()` method.
|
2013-06-11 22:56:03 +02:00
|
|
|
Until the data is consumed, the `'end'` event will not fire. Also, until
|
|
|
|
the data is read it will consume memory that can eventually lead to a
|
|
|
|
'process out of memory' error.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
Note: io.js does not check whether Content-Length and the length of the body
|
2011-07-29 20:03:58 +02:00
|
|
|
which has been transmitted are equal or not.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
The request implements the [Writable Stream][] interface. This is an
|
|
|
|
[EventEmitter][] with the following events:
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2013-12-31 01:51:51 +01:00
|
|
|
### Event: 'response'
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
|
|
`function (response) { }`
|
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
Emitted when a response is received to this request. This event is emitted only
|
2013-10-16 12:20:25 +02:00
|
|
|
once. The `response` argument will be an instance of [http.IncomingMessage][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-04-26 01:04:07 +02:00
|
|
|
Options:
|
|
|
|
|
|
|
|
- `host`: A domain name or IP address of the server to issue the request to.
|
|
|
|
- `port`: Port of remote server.
|
|
|
|
- `socketPath`: Unix Domain Socket (use one of host:port or socketPath)
|
|
|
|
|
2011-07-26 00:13:24 +02:00
|
|
|
### Event: 'socket'
|
|
|
|
|
|
|
|
`function (socket) { }`
|
|
|
|
|
|
|
|
Emitted after a socket is assigned to this request.
|
|
|
|
|
2012-01-09 03:51:06 +01:00
|
|
|
### Event: 'connect'
|
|
|
|
|
2013-06-13 02:45:30 +02:00
|
|
|
`function (response, socket, head) { }`
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
|
|
Emitted each time a server responds to a request with a CONNECT method. If this
|
|
|
|
event isn't being listened for, clients receiving a CONNECT method will have
|
|
|
|
their connections closed.
|
|
|
|
|
|
|
|
A client server pair that show you how to listen for the `connect` event.
|
|
|
|
|
|
|
|
var http = require('http');
|
|
|
|
var net = require('net');
|
|
|
|
var url = require('url');
|
|
|
|
|
|
|
|
// Create an HTTP tunneling proxy
|
|
|
|
var proxy = http.createServer(function (req, res) {
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
res.end('okay');
|
|
|
|
});
|
2013-06-13 02:45:30 +02:00
|
|
|
proxy.on('connect', function(req, cltSocket, head) {
|
2012-01-09 03:51:06 +01:00
|
|
|
// connect to an origin server
|
|
|
|
var srvUrl = url.parse('http://' + req.url);
|
|
|
|
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() {
|
|
|
|
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
|
2015-02-07 11:25:13 +01:00
|
|
|
'Proxy-agent: io.js-Proxy\r\n' +
|
2012-01-09 03:51:06 +01:00
|
|
|
'\r\n');
|
2013-06-13 02:45:30 +02:00
|
|
|
srvSocket.write(head);
|
2012-01-09 03:51:06 +01:00
|
|
|
srvSocket.pipe(cltSocket);
|
|
|
|
cltSocket.pipe(srvSocket);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// now that proxy is running
|
|
|
|
proxy.listen(1337, '127.0.0.1', function() {
|
|
|
|
|
|
|
|
// make a request to a tunneling proxy
|
|
|
|
var options = {
|
|
|
|
port: 1337,
|
2012-10-29 04:16:40 +01:00
|
|
|
hostname: '127.0.0.1',
|
2012-01-09 03:51:06 +01:00
|
|
|
method: 'CONNECT',
|
|
|
|
path: 'www.google.com:80'
|
|
|
|
};
|
|
|
|
|
|
|
|
var req = http.request(options);
|
|
|
|
req.end();
|
|
|
|
|
2013-06-13 02:45:30 +02:00
|
|
|
req.on('connect', function(res, socket, head) {
|
2012-01-09 03:51:06 +01:00
|
|
|
console.log('got connected!');
|
|
|
|
|
|
|
|
// make a request over an HTTP tunnel
|
|
|
|
socket.write('GET / HTTP/1.1\r\n' +
|
|
|
|
'Host: www.google.com:80\r\n' +
|
|
|
|
'Connection: close\r\n' +
|
|
|
|
'\r\n');
|
|
|
|
socket.on('data', function(chunk) {
|
|
|
|
console.log(chunk.toString());
|
|
|
|
});
|
|
|
|
socket.on('end', function() {
|
|
|
|
proxy.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### Event: 'upgrade'
|
|
|
|
|
2013-06-13 02:45:30 +02:00
|
|
|
`function (response, socket, head) { }`
|
2011-05-20 01:50:12 +02:00
|
|
|
|
|
|
|
Emitted each time a server responds to a request with an upgrade. If this
|
|
|
|
event isn't being listened for, clients receiving an upgrade header will have
|
|
|
|
their connections closed.
|
|
|
|
|
2012-01-09 03:51:06 +01:00
|
|
|
A client server pair that show you how to listen for the `upgrade` event.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-05-20 01:50:12 +02:00
|
|
|
var http = require('http');
|
|
|
|
|
|
|
|
// Create an HTTP server
|
|
|
|
var srv = http.createServer(function (req, res) {
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
res.end('okay');
|
|
|
|
});
|
2013-06-13 02:45:30 +02:00
|
|
|
srv.on('upgrade', function(req, socket, head) {
|
2011-05-20 01:50:12 +02:00
|
|
|
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
|
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
'Connection: Upgrade\r\n' +
|
2012-01-09 03:51:06 +01:00
|
|
|
'\r\n');
|
2011-05-20 01:50:12 +02:00
|
|
|
|
2012-01-09 03:51:06 +01:00
|
|
|
socket.pipe(socket); // echo back
|
2011-05-20 01:50:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// now that server is running
|
|
|
|
srv.listen(1337, '127.0.0.1', function() {
|
|
|
|
|
|
|
|
// make a request
|
|
|
|
var options = {
|
|
|
|
port: 1337,
|
2012-10-29 04:16:40 +01:00
|
|
|
hostname: '127.0.0.1',
|
2011-05-20 01:50:12 +02:00
|
|
|
headers: {
|
|
|
|
'Connection': 'Upgrade',
|
|
|
|
'Upgrade': 'websocket'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var req = http.request(options);
|
|
|
|
req.end();
|
|
|
|
|
2013-06-13 02:45:30 +02:00
|
|
|
req.on('upgrade', function(res, socket, upgradeHead) {
|
2011-05-20 01:50:12 +02:00
|
|
|
console.log('got upgraded!');
|
|
|
|
socket.end();
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
});
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-06-11 15:09:40 +02:00
|
|
|
### Event: 'continue'
|
|
|
|
|
2011-12-28 21:14:30 +01:00
|
|
|
`function () { }`
|
2011-06-11 15:09:40 +02:00
|
|
|
|
|
|
|
Emitted when the server sends a '100 Continue' HTTP response, usually because
|
|
|
|
the request contained 'Expect: 100-continue'. This is an instruction that
|
|
|
|
the client should send the request body.
|
|
|
|
|
2015-02-25 01:11:11 +01:00
|
|
|
### Event: 'abort'
|
|
|
|
|
|
|
|
`function () { }`
|
|
|
|
|
|
|
|
Emitted when the request has been aborted by the client. This event is only
|
|
|
|
emitted on the first call to `abort()`.
|
|
|
|
|
2015-03-14 08:24:07 +01:00
|
|
|
### request.flushHeaders()
|
2014-04-09 15:02:03 +02:00
|
|
|
|
|
|
|
Flush the request headers.
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
For efficiency reasons, io.js normally buffers the request headers until you
|
2014-04-09 15:02:03 +02:00
|
|
|
call `request.end()` or write the first chunk of request data. It then tries
|
|
|
|
hard to pack the request headers and data into a single TCP packet.
|
|
|
|
|
|
|
|
That's usually what you want (it saves a TCP round-trip) but not when the first
|
2015-03-14 08:24:07 +01:00
|
|
|
data isn't sent until possibly much later. `request.flushHeaders()` lets you bypass
|
2014-04-09 15:02:03 +02:00
|
|
|
the optimization and kickstart the request.
|
|
|
|
|
2014-12-02 04:14:57 +01:00
|
|
|
### request.write(chunk[, encoding][, callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Sends a chunk of the body. By calling this method
|
|
|
|
many times, the user can stream a request body to a
|
|
|
|
server--in that case it is suggested to use the
|
|
|
|
`['Transfer-Encoding', 'chunked']` header line when
|
|
|
|
creating the request.
|
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
The `chunk` argument should be a [Buffer][] or a string.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-12-27 09:43:58 +01:00
|
|
|
The `encoding` argument is optional and only applies when `chunk` is a string.
|
|
|
|
Defaults to `'utf8'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-12-02 04:14:57 +01:00
|
|
|
The `callback` argument is optional and will be called when this chunk of data
|
|
|
|
is flushed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-12-02 04:14:57 +01:00
|
|
|
### request.end([data][, encoding][, callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Finishes sending the request. If any parts of the body are
|
|
|
|
unsent, it will flush them to the stream. If the request is
|
|
|
|
chunked, this will send the terminating `'0\r\n\r\n'`.
|
|
|
|
|
2011-12-27 09:43:58 +01:00
|
|
|
If `data` is specified, it is equivalent to calling
|
2014-12-02 04:14:57 +01:00
|
|
|
`request.write(data, encoding)` followed by `request.end(callback)`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-12-18 08:52:42 +01:00
|
|
|
If `callback` is specified, it will be called when the request stream
|
|
|
|
is finished.
|
|
|
|
|
2011-02-05 00:14:58 +01:00
|
|
|
### request.abort()
|
|
|
|
|
2011-02-05 08:05:25 +01:00
|
|
|
Aborts a request. (New since v0.3.8.)
|
2011-02-05 00:14:58 +01:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
### request.setTimeout(timeout[, callback])
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
Once a socket is assigned to this request and is connected
|
|
|
|
[socket.setTimeout()][] will be called.
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2011-12-27 09:43:58 +01:00
|
|
|
### request.setNoDelay([noDelay])
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
Once a socket is assigned to this request and is connected
|
|
|
|
[socket.setNoDelay()][] will be called.
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
### request.setSocketKeepAlive([enable][, initialDelay])
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
Once a socket is assigned to this request and is connected
|
|
|
|
[socket.setKeepAlive()][] will be called.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
## http.IncomingMessage
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-10-16 12:20:25 +02:00
|
|
|
An `IncomingMessage` object is created by [http.Server][] or
|
|
|
|
[http.ClientRequest][] and passed as the first argument to the `'request'`
|
|
|
|
and `'response'` event respectively. It may be used to access response status,
|
|
|
|
headers and data.
|
2012-02-05 11:11:54 +01:00
|
|
|
|
2013-04-05 20:07:53 +02:00
|
|
|
It implements the [Readable Stream][] interface, as well as the
|
|
|
|
following additional events, methods, and properties.
|
2012-10-12 15:27:47 +02:00
|
|
|
|
2011-06-11 15:09:40 +02:00
|
|
|
### Event: 'close'
|
|
|
|
|
2012-10-12 15:27:47 +02:00
|
|
|
`function () { }`
|
2011-06-11 15:09:40 +02:00
|
|
|
|
2013-02-15 23:21:48 +01:00
|
|
|
Indicates that the underlying connection was closed.
|
2013-10-16 12:32:47 +02:00
|
|
|
Just like `'end'`, this event occurs only once per response.
|
2012-10-12 15:27:47 +02:00
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
### message.httpVersion
|
2011-06-11 15:09:40 +02:00
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
In case of server request, the HTTP version sent by the client. In the case of
|
|
|
|
client response, the HTTP version of the connected-to server.
|
|
|
|
Probably either `'1.1'` or `'1.0'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Also `response.httpVersionMajor` is the first integer and
|
|
|
|
`response.httpVersionMinor` is the second.
|
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
### message.headers
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
The request/response headers object.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
Read only map of header names and values. Header names are lower-cased.
|
|
|
|
Example:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
// Prints something like:
|
|
|
|
//
|
|
|
|
// { 'user-agent': 'curl/7.22.0',
|
|
|
|
// host: '127.0.0.1:8000',
|
|
|
|
// accept: '*/*' }
|
|
|
|
console.log(request.headers);
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-15 23:12:12 +02:00
|
|
|
### message.rawHeaders
|
|
|
|
|
|
|
|
The raw request/response headers list exactly as they were received.
|
|
|
|
|
|
|
|
Note that the keys and values are in the same list. It is *not* a
|
|
|
|
list of tuples. So, the even-numbered offsets are key values, and the
|
|
|
|
odd-numbered offsets are the associated values.
|
|
|
|
|
|
|
|
Header names are not lowercased, and duplicates are not merged.
|
|
|
|
|
|
|
|
// Prints something like:
|
|
|
|
//
|
|
|
|
// [ 'user-agent',
|
|
|
|
// 'this is invalid because there can be only one',
|
|
|
|
// 'User-Agent',
|
|
|
|
// 'curl/7.22.0',
|
|
|
|
// 'Host',
|
|
|
|
// '127.0.0.1:8000',
|
|
|
|
// 'ACCEPT',
|
|
|
|
// '*/*' ]
|
|
|
|
console.log(request.rawHeaders);
|
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
### message.trailers
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-15 23:12:12 +02:00
|
|
|
The request/response trailers object. Only populated at the 'end' event.
|
|
|
|
|
|
|
|
### message.rawTrailers
|
|
|
|
|
|
|
|
The raw request/response trailer keys and values exactly as they were
|
|
|
|
received. Only populated at the 'end' event.
|
2013-01-20 00:49:30 +01:00
|
|
|
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-04 08:29:22 +01:00
|
|
|
### message.setTimeout(msecs, callback)
|
|
|
|
|
|
|
|
* `msecs` {Number}
|
|
|
|
* `callback` {Function}
|
|
|
|
|
|
|
|
Calls `message.connection.setTimeout(msecs, callback)`.
|
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
### message.method
|
|
|
|
|
2013-10-16 12:20:25 +02:00
|
|
|
**Only valid for request obtained from [http.Server][].**
|
2013-01-20 00:49:30 +01:00
|
|
|
|
|
|
|
The request method as a string. Read only. Example:
|
|
|
|
`'GET'`, `'DELETE'`.
|
|
|
|
|
|
|
|
### message.url
|
|
|
|
|
2013-10-16 12:20:25 +02:00
|
|
|
**Only valid for request obtained from [http.Server][].**
|
2013-01-20 00:49:30 +01:00
|
|
|
|
|
|
|
Request URL string. This contains only the URL that is
|
|
|
|
present in the actual HTTP request. If the request is:
|
|
|
|
|
|
|
|
GET /status?name=ryan HTTP/1.1\r\n
|
|
|
|
Accept: text/plain\r\n
|
|
|
|
\r\n
|
|
|
|
|
|
|
|
Then `request.url` will be:
|
|
|
|
|
|
|
|
'/status?name=ryan'
|
|
|
|
|
|
|
|
If you would like to parse the URL into its parts, you can use
|
|
|
|
`require('url').parse(request.url)`. Example:
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
iojs> require('url').parse('/status?name=ryan')
|
2013-01-20 00:49:30 +01:00
|
|
|
{ href: '/status?name=ryan',
|
|
|
|
search: '?name=ryan',
|
|
|
|
query: 'name=ryan',
|
|
|
|
pathname: '/status' }
|
|
|
|
|
|
|
|
If you would like to extract the params from the query string,
|
|
|
|
you can use the `require('querystring').parse` function, or pass
|
|
|
|
`true` as the second argument to `require('url').parse`. Example:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
iojs> require('url').parse('/status?name=ryan', true)
|
2013-01-20 00:49:30 +01:00
|
|
|
{ href: '/status?name=ryan',
|
|
|
|
search: '?name=ryan',
|
|
|
|
query: { name: 'ryan' },
|
|
|
|
pathname: '/status' }
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
### message.statusCode
|
|
|
|
|
|
|
|
**Only valid for response obtained from `http.ClientRequest`.**
|
|
|
|
|
|
|
|
The 3-digit HTTP response status code. E.G. `404`.
|
|
|
|
|
2013-09-22 16:06:58 +02:00
|
|
|
### message.statusMessage
|
|
|
|
|
|
|
|
**Only valid for response obtained from `http.ClientRequest`.**
|
|
|
|
|
|
|
|
The HTTP response status message (reason phrase). E.G. `OK` or `Internal Server Error`.
|
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
### message.socket
|
|
|
|
|
|
|
|
The `net.Socket` object associated with the connection.
|
|
|
|
|
|
|
|
With HTTPS support, use request.connection.verifyPeer() and
|
|
|
|
request.connection.getPeerCertificate() to obtain the client's
|
|
|
|
authentication details.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
|
|
|
|
['checkContinue']: #http_event_checkcontinue
|
2013-10-16 12:20:25 +02:00
|
|
|
['listening']: net.html#net_event_listening
|
2013-11-28 21:25:30 +01:00
|
|
|
['response']: #http_event_response
|
2013-10-16 12:20:25 +02:00
|
|
|
[Agent]: #http_class_http_agent
|
2012-06-06 21:05:18 +02:00
|
|
|
[Buffer]: buffer.html#buffer_buffer
|
|
|
|
[EventEmitter]: events.html#events_class_events_eventemitter
|
2014-04-09 22:50:08 +02:00
|
|
|
[Readable Stream]: stream.html#stream_class_stream_readable
|
|
|
|
[Writable Stream]: stream.html#stream_class_stream_writable
|
2012-06-06 21:05:18 +02:00
|
|
|
[global Agent]: #http_http_globalagent
|
2013-10-16 12:20:25 +02:00
|
|
|
[http.ClientRequest]: #http_class_http_clientrequest
|
|
|
|
[http.IncomingMessage]: #http_http_incomingmessage
|
|
|
|
[http.ServerResponse]: #http_class_http_serverresponse
|
|
|
|
[http.Server]: #http_class_http_server
|
|
|
|
[http.request()]: #http_http_request_options_callback
|
2012-06-06 21:05:18 +02:00
|
|
|
[http.request()]: #http_http_request_options_callback
|
2012-10-08 19:10:29 +02:00
|
|
|
[net.Server.close()]: net.html#net_server_close_callback
|
|
|
|
[net.Server.listen(path)]: net.html#net_server_listen_path_callback
|
|
|
|
[net.Server.listen(port)]: net.html#net_server_listen_port_host_backlog_callback
|
2013-10-16 12:20:25 +02:00
|
|
|
[response.end()]: #http_response_end_data_encoding
|
|
|
|
[response.write()]: #http_response_write_chunk_encoding
|
|
|
|
[response.writeContinue()]: #http_response_writecontinue
|
|
|
|
[response.writeHead()]: #http_response_writehead_statuscode_reasonphrase_headers
|
2012-06-06 21:05:18 +02:00
|
|
|
[socket.setKeepAlive()]: net.html#net_socket_setkeepalive_enable_initialdelay
|
|
|
|
[socket.setNoDelay()]: net.html#net_socket_setnodelay_nodelay
|
|
|
|
[socket.setTimeout()]: net.html#net_socket_settimeout_timeout_callback
|
|
|
|
[stream.setEncoding()]: stream.html#stream_stream_setencoding_encoding
|
|
|
|
[url.parse()]: url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost
|