示例#1
0
		var req = fn.request(opts, function (res) {
			var statusCode = res.statusCode;

			if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
				res.resume();

				if (++redirectCount > 10) {
					ee.emit('error', new got.MaxRedirectsError(statusCode, opts), null, res);
					return;
				}

				var redirectUrl = urlLib.resolve(urlLib.format(opts), res.headers.location);
				var redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl));

				ee.emit('redirect', res, redirectOpts);

				get(redirectOpts);
				return;
			}

			// do not write ee.bind(...) instead of function - it will break gzip in Node.js 0.10
			setImmediate(function () {
				ee.emit('response', typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res);
			});
		});
示例#2
0
		const req = fn.request(opts, res => {
			const statusCode = res.statusCode;

			if (redirectUrl) {
				res.url = redirectUrl;
			}

			if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
				res.resume();

				if (++redirectCount > 10) {
					ee.emit('error', new got.MaxRedirectsError(statusCode, opts), null, res);
					return;
				}

				redirectUrl = urlLib.resolve(urlLib.format(opts), new Buffer(res.headers.location, 'binary').toString());
				const redirectOpts = Object.assign({}, opts, urlLib.parse(redirectUrl));

				ee.emit('redirect', res, redirectOpts);

				get(redirectOpts);

				return;
			}

			setImmediate(() => {
				ee.emit('response', typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res);
			});
		});
示例#3
0
文件: index.js 项目: bendrucker/got
		var req = fn.request(arg, function (response) {
			var statusCode = response.statusCode;
			var res = response;

			if (proxy) {
				proxy.emit('response', res);
			}

			// auto-redirect only for GET and HEAD methods
			if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
				// discard response
				res.resume();

				if (++redirectCount > 10) {
					cb(new GotError('Redirected 10 times. Aborting.'), undefined, res);
					return;
				}

				delete opts.host;
				delete opts.hostname;
				delete opts.port;
				delete opts.path;

				if (proxy) {
					proxy.emit('redirect', res, opts);
				}

				get(urlLib.resolve(url, res.headers.location), opts, cb);
				return;
			}

			if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
				res = res.pipe(zlib.createUnzip());
			}

			if (statusCode < 200 || statusCode > 299) {
				readAllStream(res, encoding, function (err, data) {
					err = new GotError(opts.method + ' ' + url + ' response code is ' + statusCode + ' (' + statuses[statusCode] + ')', err);
					err.code = statusCode;

					if (data && json) {
						try {
							data = JSON.parse(data);
						} catch (e) {
							err = new GotError('Parsing ' + url + ' response failed', new GotError(e.message, err));
						}
					}

					cb(err, data, response);
				});

				return;
			}

			// pipe the response to the proxy if in proxy mode
			if (proxy) {
				proxy.setReadable(res);
				return;
			}

			readAllStream(res, encoding, function (err, data) {
				if (err) {
					err = new GotError('Reading ' + url + ' response failed', err);
				} else if (json) {
					try {
						data = JSON.parse(data);
					} catch (e) {
						err = new GotError('Parsing ' + url + ' response failed', e);
					}
				}

				cb(err, data, response);
			});
		}).once('error', function (err) {
示例#4
0
        var req = fn.request(opts, function (response) {
            var statusCode = response.statusCode;
            var res = response;

            // auto-redirect only for GET and HEAD methods
            if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
                // discard response
                res.resume();

                if (++redirectCount > 10) {
                    cb(new GotError('Redirected 10 times. Aborting.'), undefined, res);
                    return;
                }

                var redirectUrl = urlLib.resolve(url, res.headers.location);
                var redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl));

                if (opts.agent === infinityAgent.http.globalAgent && redirectOpts.protocol === 'https:' && opts.protocol === 'http:') {
                    redirectOpts.agent = undefined;
                }

                if (proxy) {
                    proxy.emit('redirect', res, redirectOpts);
                }

                get(redirectOpts, cb);
                return;
            }

            if (proxy) {
                proxy.emit('response', res);
            }

            if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
                res = res.pipe(zlib.createUnzip());
            }

            if (statusCode < 200 || statusCode > 299) {
                readAllStream(res, encoding, function (err, data) {
                    err = new GotError(opts.method + ' ' + url + ' response code is ' + statusCode + ' (' + http.STATUS_CODES[statusCode] + ')', err);
                    err.code = statusCode;

                    if (data && json) {
                        try {
                            data = JSON.parse(data);
                        } catch (e) {
                            err = new GotError('Parsing ' + url + ' response failed', new GotError(e.message, err));
                        }
                    }

                    cb(err, data, response);
                });

                return;
            }

            // pipe the response to the proxy if in proxy mode
            if (proxy) {
                proxy.setReadable(res);
                return;
            }

            readAllStream(res, encoding, function (err, data) {
                if (err) {
                    err = new GotError('Reading ' + url + ' response failed', err);
                } else if (json && statusCode !== 204) {
                    // only parse json if the option is enabled, and the response
                    // is not a 204 (empty reponse)
                    try {
                        data = JSON.parse(data);
                    } catch (e) {
                        err = new GotError('Parsing ' + url + ' response failed', e);
                    }
                }

                cb(err, data, response);
            });
        }).once('error', function (err) {