Implementing URLRequest API, getting response body.

This commit is contained in:
ali.ibrahim 2016-09-21 09:23:00 +02:00
parent 81eab9887b
commit 2d9d4af98d
6 changed files with 313 additions and 76 deletions

View file

@ -9,46 +9,65 @@ Object.setPrototypeOf(Net.prototype, EventEmitter.prototype)
Object.setPrototypeOf(URLRequest.prototype, EventEmitter.prototype)
class URLResponse extends EventEmitter {
constructor(request) {
super();
this.request = request;
}
constructor(request) {
super();
this.request = request;
}
get statusCode() {
return this.request.statusCode;
}
get statusCode() {
return this.request.statusCode;
}
get statusMessage() {
return this.request.statusMessage;
}
get statusMessage() {
return this.request.statusMessage;
}
get headers() {
return this.request.responseHeaders;
}
get headers() {
return this.request.rawResponseHeaders;
}
get httpVersion() {
return this.request.responseHttpVersion;
}
get httpVersion() {
return `${this.httpVersionMajor}.${this.httpVersionMinor}`;
}
get httpVersionMajor() {
return this.request.httpVersionMajor;
}
get httpVersionMinor() {
return this.request.httpVersionMinor;
}
get rawHeaders() {
return this.request.rawResponseHeaders;
}
}
Net.prototype.request = function(options, callback) {
let request = new URLRequest(options)
let request = new URLRequest(options)
if (callback) {
request.once('response', callback)
}
if (callback) {
request.once('response', callback)
}
return request
return request
}
URLRequest.prototype._emitResponse = function() {
URLRequest.prototype._emitRequestEvent = function(name) {
if (name === 'response') {
this.response = new URLResponse(this);
this.emit('response', this.response);
this.emit(name, this.response);
} else {
this.emit.apply(this, arguments);
}
}
URLRequest.prototype._emitResponseEvent = function() {
this.response.emit.apply(this.response, arguments);
}
module.exports = net