Adding support for upload chunked encoding.
This commit is contained in:
parent
9498a5738a
commit
cbbc4376ca
5 changed files with 95 additions and 24 deletions
|
@ -1,6 +1,7 @@
|
|||
'use strict'
|
||||
|
||||
const {EventEmitter} = require('events')
|
||||
const util = require('util')
|
||||
const binding = process.atomBinding('net')
|
||||
const {net, Net} = binding
|
||||
const {URLRequest} = net
|
||||
|
@ -8,6 +9,10 @@ const {URLRequest} = net
|
|||
Object.setPrototypeOf(Net.prototype, EventEmitter.prototype)
|
||||
Object.setPrototypeOf(URLRequest.prototype, EventEmitter.prototype)
|
||||
|
||||
let kSupportedProtocols = new Set();
|
||||
kSupportedProtocols.add('http:');
|
||||
kSupportedProtocols.add('https:');
|
||||
|
||||
class IncomingMessage extends EventEmitter {
|
||||
constructor(url_request) {
|
||||
super();
|
||||
|
@ -57,6 +62,74 @@ class ClientRequest extends EventEmitter {
|
|||
constructor(options, callback) {
|
||||
super();
|
||||
|
||||
if (typeof options === 'string') {
|
||||
options = url.parse(options);
|
||||
} else {
|
||||
options = util._extend({}, options);
|
||||
}
|
||||
|
||||
const method = (options.method || 'GET').toUpperCase();
|
||||
let url_str = options.url;
|
||||
|
||||
if (!url_str) {
|
||||
let url_obj = {};
|
||||
|
||||
const protocol = options.protocol || 'http';
|
||||
if (!kSupportedProtocols.has(protocol)) {
|
||||
throw new Error('Protocol "' + protocol + '" not supported. ');
|
||||
}
|
||||
url_obj.protocol = protocol;
|
||||
|
||||
|
||||
if (options.host) {
|
||||
url_obj.host = options.host;
|
||||
} else {
|
||||
|
||||
if (options.hostname) {
|
||||
url_obj.hostname = options.hostname;
|
||||
} else {
|
||||
url_obj.hostname = 'localhost';
|
||||
}
|
||||
|
||||
if (options.port) {
|
||||
url_obj.port = options.port;
|
||||
}
|
||||
}
|
||||
|
||||
const path = options.path || '/';
|
||||
if (options.path && / /.test(options.path)) {
|
||||
// The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
|
||||
// with an additional rule for ignoring percentage-escaped characters
|
||||
// but that's a) hard to capture in a regular expression that performs
|
||||
// well, and b) possibly too restrictive for real-world usage. That's
|
||||
// why it only scans for spaces because those are guaranteed to create
|
||||
// an invalid request.
|
||||
throw new TypeError('Request path contains unescaped characters.');
|
||||
}
|
||||
url_obj.path = path;
|
||||
|
||||
url_str = url.format(url_obj);
|
||||
}
|
||||
|
||||
const session_name = options.session || '';
|
||||
let url_request = new URLRequest({
|
||||
method: method,
|
||||
url: url_str,
|
||||
session: session_name
|
||||
});
|
||||
|
||||
// Set back and forward links.
|
||||
this._url_request = url_request;
|
||||
url_request._request = this;
|
||||
|
||||
if (options.headers) {
|
||||
let keys = Object.keys(options.headers);
|
||||
for (let i = 0, l = keys.length; i < l; i++) {
|
||||
let key = keys[i];
|
||||
this.setHeader(key, options.headers[key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Flag to prevent writings after end.
|
||||
this._finished = false;
|
||||
|
||||
|
@ -73,19 +146,6 @@ class ClientRequest extends EventEmitter {
|
|||
// after the request starts.
|
||||
this._extra_headers = {};
|
||||
|
||||
let url = options.url;
|
||||
let method = options.method;
|
||||
let session = options.session;
|
||||
|
||||
let url_request = new URLRequest({
|
||||
url: url,
|
||||
method: method,
|
||||
session: session
|
||||
});
|
||||
|
||||
this._url_request = url_request;
|
||||
url_request._request = this;
|
||||
|
||||
url_request.on('response', ()=> {
|
||||
let response = new IncomingMessage(url_request);
|
||||
url_request._response = response;
|
||||
|
@ -97,6 +157,17 @@ class ClientRequest extends EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
get chunkedEncoding() {
|
||||
return this._chunkedEncoding;
|
||||
}
|
||||
|
||||
set chunkedEncoding(value) {
|
||||
if (this._headersSent) {
|
||||
throw new Error('Can\'t set the transfer encoding, headers have been sent.');
|
||||
}
|
||||
this._chunkedEncoding = value;
|
||||
}
|
||||
|
||||
|
||||
setHeader(name, value) {
|
||||
if (typeof name !== 'string')
|
||||
|
@ -121,7 +192,7 @@ class ClientRequest extends EventEmitter {
|
|||
return;
|
||||
}
|
||||
|
||||
var key = name.toLowerCase();
|
||||
let key = name.toLowerCase();
|
||||
return this._extra_headers[key];
|
||||
}
|
||||
|
||||
|
@ -135,7 +206,7 @@ class ClientRequest extends EventEmitter {
|
|||
throw new Error('Can\'t remove headers after they are sent.');
|
||||
}
|
||||
|
||||
var key = name.toLowerCase();
|
||||
let key = name.toLowerCase();
|
||||
delete this._extra_headers[key];
|
||||
this._url_request.removeExtraHeader(name);
|
||||
}
|
||||
|
@ -162,7 +233,7 @@ class ClientRequest extends EventEmitter {
|
|||
// assume that request headers are written after delivering the first
|
||||
// buffer to the network IO thread.
|
||||
if (!this._headersSent) {
|
||||
this._url_request.setChunkedUpload(this._chunkedEncoding);
|
||||
this._url_request.setChunkedUpload(this.chunkedEncoding);
|
||||
this._headersSent = true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue