Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 'use strict' var http = require('http') var https = require('https') var util = require('util') var stream = require('stream') var Redirect = require('./lib/redirect').Redirect function Request ({ uri, headers }) { var self = this stream.Stream.call(self) self.uri = uri self.headers = headers self.readable = true self._redirect = new Redirect(self) self.init() } util.inherits(Request, stream.Stream) Request.prototype.init = function () { // init() contains all the code to setup the request object. // the actual outgoing request is not started until start() is called // this function is called from both the constructor and on redirect. var self = this self.headers = Object.assign({}, self.headers) if (self.uri.path) { self.path = self.uri.path } else { self.path = self.uri.pathname + (self.uri.search || '') } var protocol = self.uri.protocol var defaultModules = {'http:': http, 'https:': https} self.httpModule = defaultModules[protocol] if (!self.httpModule) { return self.emit('error', new Error('Invalid protocol: ' + protocol)) } setImmediate(function () { self.start() }) } Request.prototype.start = function () { // start() is called once we are ready to send the outgoing HTTP request. // this is usually called on the first write(), end() or on nextTick() var self = this if (self._started) { return } self._started = true var reqOptions = { headers: self.headers, host: self.uri.hostname, port: self.uri.port, path: self.path, } try { self.req = self.httpModule.request(reqOptions) } catch (err) { self.emit('error', err) return } self.req.on('response', self.onRequestResponse.bind(self)) self.req.on('error', function (error) { self.emit('error', error) }) self.req.on('drain', function () { self.emit('drain') }) self.req.on('socket', function (socket) { self.emit('socket', socket) }) self.emit('request', self.req) self.req.end() } Request.prototype.onRequestResponse = function (response) { var self = this self.response = response response.request = self if (self._redirect.onResponse(response)) { return // Ignore the rest of the response } else { // Be a good stream and emit end when the response is finished. // Hack to emit end on close because of a core bug that never fires end response.on('close', function () { if (!self._ended) { self.response.emit('end') } }) response.once('end', function () { self._ended = true }) var responseContent = response if (self._paused) { responseContent.pause() } self.responseContent = responseContent self.emit('response', response) responseContent.on('data', function (chunk) { self.emit('data', chunk) }) responseContent.once('end', function (chunk) { self.emit('end', chunk) }) responseContent.on('error', function (error) { self.emit('error', error) }) responseContent.on('close', function () { self.emit('close') }) } } // Stream API Request.prototype.pause = function () { var self = this if (!self.responseContent) { self._paused = true } else { self.responseContent.pause() } } Request.prototype.resume = function () { var self = this if (!self.responseContent) { self._paused = false } else { self.responseContent.resume() } } Request.prototype.destroy = function () { var self = this if (self.response) { self.response.destroy() } } // Exports module.exports = Request |