Adding support for POST requests.
This commit is contained in:
parent
2b3b41d5f9
commit
f7525d7877
5 changed files with 230 additions and 97 deletions
|
@ -56,10 +56,16 @@ Net.prototype.request = function(options, callback) {
|
|||
}
|
||||
|
||||
URLRequest.prototype._init = function() {
|
||||
this._finished = false;
|
||||
this._hasBody = true;
|
||||
// Flag to prevent writings after end.
|
||||
this._finished = false;
|
||||
|
||||
// Set when the request uses chuned encoding. Can be switched
|
||||
// to true only once and never set back to false.
|
||||
this._chunkedEncoding = false;
|
||||
this._headersSent = false;
|
||||
|
||||
// Flag to prevent request's headers modifications after
|
||||
// headers flush.
|
||||
this._headersSent = false;
|
||||
}
|
||||
|
||||
|
||||
|
@ -111,80 +117,73 @@ URLRequest.prototype._emitResponseEvent = function() {
|
|||
}
|
||||
|
||||
|
||||
URLRequest.prototype.write = function(chunk, encoding, callback) {
|
||||
|
||||
if (this.finished) {
|
||||
var err = new Error('write after end');
|
||||
process.nextTick(writeAfterEndNT, this, err, callback);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* TODO
|
||||
if (!this._header) {
|
||||
this._implicitHeader();
|
||||
}
|
||||
*/
|
||||
|
||||
if (!this._hasBody) {
|
||||
//debug('This type of response MUST NOT have a body. ' +
|
||||
// 'Ignoring write() calls.');
|
||||
return true;
|
||||
}
|
||||
URLRequest.prototype._write = function(chunk, encoding, callback, is_last) {
|
||||
|
||||
|
||||
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
|
||||
throw new TypeError('First argument must be a string or Buffer');
|
||||
let chunk_is_string = typeof chunk === 'string';
|
||||
let chunk_is_buffer = chunk instanceof Buffer;
|
||||
if (!chunk_is_string && !chunk_is_buffer) {
|
||||
throw new TypeError('First argument must be a string or Buffer.');
|
||||
}
|
||||
|
||||
|
||||
// If we get an empty string or buffer, then just do nothing, and
|
||||
// signal the user to keep writing.
|
||||
if (chunk.length === 0) return true;
|
||||
|
||||
var len, ret;
|
||||
if (this.chunkedEncoding) {
|
||||
if (typeof chunk === 'string' &&
|
||||
encoding !== 'hex' &&
|
||||
encoding !== 'base64' &&
|
||||
encoding !== 'binary') {
|
||||
len = Buffer.byteLength(chunk, encoding);
|
||||
chunk = len.toString(16) + CRLF + chunk + CRLF;
|
||||
ret = this._send(chunk, encoding, callback);
|
||||
} else {
|
||||
// buffer, or a non-toString-friendly encoding
|
||||
if (typeof chunk === 'string')
|
||||
len = Buffer.byteLength(chunk, encoding);
|
||||
else
|
||||
len = chunk.length;
|
||||
|
||||
if (this.connection && !this.connection.corked) {
|
||||
this.connection.cork();
|
||||
process.nextTick(connectionCorkNT, this.connection);
|
||||
}
|
||||
this._send(len.toString(16), 'binary', null);
|
||||
this._send(crlf_buf, null, null);
|
||||
this._send(chunk, encoding, null);
|
||||
ret = this._send(crlf_buf, null, callback);
|
||||
}
|
||||
} else {
|
||||
ret = this._send(chunk, encoding, callback);
|
||||
if (chunk_is_string) {
|
||||
// We convert all strings into binary buffers.
|
||||
chunk = Buffer.from(chunk, encoding);
|
||||
}
|
||||
|
||||
//debug('write ret = ' + ret);
|
||||
return ret;
|
||||
// Headers are assumed to be sent on first call to _writeBuffer,
|
||||
// i.e. after the first call to write or end.
|
||||
let result = this._writeBuffer(chunk, is_last);
|
||||
|
||||
// Since writing to the network is asynchronous, we conservatively
|
||||
// assume that request headers are written after delivering the first
|
||||
// buffer to the network IO thread.
|
||||
this._headersSent = true;
|
||||
|
||||
// The write callback is fired asynchronously to mimic Node.js.
|
||||
if (callback) {
|
||||
process.nextTick(callback);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
URLRequest.prototype.write = function(data, encoding, callback) {
|
||||
|
||||
if (this._finished) {
|
||||
let error = new Error('Write after end.');
|
||||
process.nextTick(writeAfterEndNT, this, error, callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
return this._write(data, encoding, callback, false);
|
||||
};
|
||||
|
||||
|
||||
URLRequest.prototype.end = function(data, encoding, callback) {
|
||||
if (this._finished) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this._finished = true;
|
||||
|
||||
URLRequest.prototype.end = function() {
|
||||
this._end();
|
||||
if (typeof data === 'function') {
|
||||
callback = data;
|
||||
encoding = null;
|
||||
data = null;
|
||||
} else if (typeof encoding === 'function') {
|
||||
callback = encoding;
|
||||
encoding = null;
|
||||
}
|
||||
|
||||
data = data || '';
|
||||
|
||||
return this._write(data, encoding, callback, true);
|
||||
};
|
||||
|
||||
|
||||
function writeAfterEndNT(self, err, callback) {
|
||||
self.emit('error', err);
|
||||
if (callback) callback(err);
|
||||
function writeAfterEndNT(self, error, callback) {
|
||||
self.emit('error', error);
|
||||
if (callback) callback(error);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue