UNPKG

3.19 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3import { __extends } from "tslib";
4import { URLBuilder } from "../url";
5import { BaseRequestPolicy, } from "./requestPolicy";
6export var DefaultRedirectOptions = {
7 handleRedirects: true,
8 maxRetries: 20,
9};
10export function redirectPolicy(maximumRetries) {
11 if (maximumRetries === void 0) { maximumRetries = 20; }
12 return {
13 create: function (nextPolicy, options) {
14 return new RedirectPolicy(nextPolicy, options, maximumRetries);
15 },
16 };
17}
18var RedirectPolicy = /** @class */ (function (_super) {
19 __extends(RedirectPolicy, _super);
20 function RedirectPolicy(nextPolicy, options, maxRetries) {
21 if (maxRetries === void 0) { maxRetries = 20; }
22 var _this = _super.call(this, nextPolicy, options) || this;
23 _this.maxRetries = maxRetries;
24 return _this;
25 }
26 RedirectPolicy.prototype.sendRequest = function (request) {
27 var _this = this;
28 return this._nextPolicy
29 .sendRequest(request)
30 .then(function (response) { return handleRedirect(_this, response, 0); });
31 };
32 return RedirectPolicy;
33}(BaseRequestPolicy));
34export { RedirectPolicy };
35function handleRedirect(policy, response, currentRetries) {
36 var request = response.request, status = response.status;
37 var locationHeader = response.headers.get("location");
38 if (locationHeader &&
39 (status === 300 ||
40 (status === 301 && ["GET", "HEAD"].includes(request.method)) ||
41 (status === 302 && ["GET", "POST", "HEAD"].includes(request.method)) ||
42 (status === 303 && "POST" === request.method) ||
43 status === 307) &&
44 ((request.redirectLimit !== undefined && currentRetries < request.redirectLimit) ||
45 (request.redirectLimit === undefined && currentRetries < policy.maxRetries))) {
46 var builder = URLBuilder.parse(request.url);
47 builder.setPath(locationHeader);
48 request.url = builder.toString();
49 // POST request with Status code 302 and 303 should be converted into a
50 // redirected GET request if the redirect url is present in the location header
51 // reference: https://tools.ietf.org/html/rfc7231#page-57 && https://fetch.spec.whatwg.org/#http-redirect-fetch
52 if ((status === 302 || status === 303) && request.method === "POST") {
53 request.method = "GET";
54 delete request.body;
55 }
56 return policy._nextPolicy
57 .sendRequest(request)
58 .then(function (res) { return handleRedirect(policy, res, currentRetries + 1); })
59 .then(function (res) { return recordRedirect(res, request.url); });
60 }
61 return Promise.resolve(response);
62}
63function recordRedirect(response, redirect) {
64 // This is called as the recursive calls to handleRedirect() unwind,
65 // only record the deepest/last redirect
66 if (!response.redirected) {
67 response.redirected = true;
68 response.url = redirect;
69 }
70 return response;
71}
72//# sourceMappingURL=redirectPolicy.js.map
\No newline at end of file