2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
import { notImplemented } from "../util.ts";
|
2020-03-11 10:49:53 -04:00
|
|
|
import { isTypedArray } from "./util.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
import type * as domTypes from "./dom_types.d.ts";
|
2020-06-08 12:08:26 -04:00
|
|
|
import { TextEncoder } from "./text_encoding.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { DenoBlob, bytesSymbol as blobBytesSymbol } from "./blob.ts";
|
2020-03-09 10:18:02 -04:00
|
|
|
import { read } from "../ops/io.ts";
|
2020-03-08 08:09:22 -04:00
|
|
|
import { close } from "../ops/resources.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
import { fetch as opFetch } from "../ops/fetch.ts";
|
|
|
|
import type { FetchResponse } from "../ops/fetch.ts";
|
2020-05-25 12:55:16 -04:00
|
|
|
import * as Body from "./body.ts";
|
|
|
|
import { getHeaderValueParams } from "./util.ts";
|
|
|
|
import { ReadableStreamImpl } from "./streams/readable_stream.ts";
|
2020-06-08 12:08:26 -04:00
|
|
|
import { MultipartBuilder } from "./fetch/multipart.ts";
|
2019-11-04 19:10:54 -05:00
|
|
|
|
2020-06-03 09:43:11 -04:00
|
|
|
const NULL_BODY_STATUS = [101, 204, 205, 304];
|
2020-05-31 19:21:14 -04:00
|
|
|
const REDIRECT_STATUS = [301, 302, 303, 307, 308];
|
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
const responseData = new WeakMap();
|
|
|
|
export class Response extends Body.Body implements domTypes.Response {
|
|
|
|
readonly type: ResponseType;
|
|
|
|
readonly redirected: boolean;
|
|
|
|
readonly url: string;
|
|
|
|
readonly status: number;
|
|
|
|
readonly statusText: string;
|
|
|
|
headers: Headers;
|
2019-11-04 19:10:54 -05:00
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
constructor(body: BodyInit | null = null, init?: domTypes.ResponseInit) {
|
|
|
|
init = init ?? {};
|
2019-11-04 19:10:54 -05:00
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
if (typeof init !== "object") {
|
|
|
|
throw new TypeError(`'init' is not an object`);
|
2019-11-04 19:10:54 -05:00
|
|
|
}
|
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
const extraInit = responseData.get(init) || {};
|
|
|
|
let { type = "default", url = "" } = extraInit;
|
2018-10-26 12:14:06 -04:00
|
|
|
|
2020-07-05 21:37:18 -04:00
|
|
|
let status = init.status === undefined ? 200 : Number(init.status || 0);
|
2020-05-25 12:55:16 -04:00
|
|
|
let statusText = init.statusText ?? "";
|
2020-07-14 15:24:17 -04:00
|
|
|
let headers = init.headers instanceof Headers
|
|
|
|
? init.headers
|
|
|
|
: new Headers(init.headers);
|
2019-11-04 19:10:54 -05:00
|
|
|
|
2020-07-05 21:37:18 -04:00
|
|
|
if (init.status !== undefined && (status < 200 || status > 599)) {
|
2020-05-25 12:55:16 -04:00
|
|
|
throw new RangeError(
|
2020-07-14 15:24:17 -04:00
|
|
|
`The status provided (${init.status}) is outside the range [200, 599]`,
|
2020-05-25 12:55:16 -04:00
|
|
|
);
|
2019-11-04 19:10:54 -05:00
|
|
|
}
|
2019-06-22 10:22:27 -04:00
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
// null body status
|
2020-05-31 19:21:14 -04:00
|
|
|
if (body && NULL_BODY_STATUS.includes(status)) {
|
2020-05-25 12:55:16 -04:00
|
|
|
throw new TypeError("Response with null body status cannot have body");
|
2018-10-26 12:14:06 -04:00
|
|
|
}
|
2019-06-24 09:34:09 -04:00
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
if (!type) {
|
|
|
|
type = "default";
|
2020-02-03 09:54:47 -05:00
|
|
|
} else {
|
2020-05-25 12:55:16 -04:00
|
|
|
if (type == "error") {
|
2020-02-03 09:54:47 -05:00
|
|
|
// spec: https://fetch.spec.whatwg.org/#concept-network-error
|
2020-05-25 12:55:16 -04:00
|
|
|
status = 0;
|
|
|
|
statusText = "";
|
|
|
|
headers = new Headers();
|
|
|
|
body = null;
|
2020-02-03 09:54:47 -05:00
|
|
|
/* spec for other Response types:
|
|
|
|
https://fetch.spec.whatwg.org/#concept-filtered-response-basic
|
|
|
|
Please note that type "basic" is not the same thing as "default".*/
|
2020-05-25 12:55:16 -04:00
|
|
|
} else if (type == "basic") {
|
|
|
|
for (const h of headers) {
|
2020-02-03 09:54:47 -05:00
|
|
|
/* Forbidden Response-Header Names:
|
|
|
|
https://fetch.spec.whatwg.org/#forbidden-response-header-name */
|
|
|
|
if (["set-cookie", "set-cookie2"].includes(h[0].toLowerCase())) {
|
2020-05-25 12:55:16 -04:00
|
|
|
headers.delete(h[0]);
|
2020-02-03 09:54:47 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-25 12:55:16 -04:00
|
|
|
} else if (type == "cors") {
|
2020-02-03 09:54:47 -05:00
|
|
|
/* CORS-safelisted Response-Header Names:
|
|
|
|
https://fetch.spec.whatwg.org/#cors-safelisted-response-header-name */
|
|
|
|
const allowedHeaders = [
|
|
|
|
"Cache-Control",
|
|
|
|
"Content-Language",
|
|
|
|
"Content-Length",
|
|
|
|
"Content-Type",
|
|
|
|
"Expires",
|
|
|
|
"Last-Modified",
|
2020-03-28 13:03:49 -04:00
|
|
|
"Pragma",
|
2020-02-03 09:54:47 -05:00
|
|
|
].map((c: string) => c.toLowerCase());
|
2020-05-25 12:55:16 -04:00
|
|
|
for (const h of headers) {
|
2020-02-03 09:54:47 -05:00
|
|
|
/* Technically this is still not standards compliant because we are
|
|
|
|
supposed to allow headers allowed in the
|
|
|
|
'Access-Control-Expose-Headers' header in the 'internal response'
|
|
|
|
However, this implementation of response doesn't seem to have an
|
|
|
|
easy way to access the internal response, so we ignore that
|
|
|
|
header.
|
|
|
|
TODO(serverhiccups): change how internal responses are handled
|
|
|
|
so we can do this properly. */
|
|
|
|
if (!allowedHeaders.includes(h[0].toLowerCase())) {
|
2020-05-25 12:55:16 -04:00
|
|
|
headers.delete(h[0]);
|
2020-02-03 09:54:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* TODO(serverhiccups): Once I fix the 'internal response' thing,
|
|
|
|
these actually need to treat the internal response differently */
|
2020-05-25 12:55:16 -04:00
|
|
|
} else if (type == "opaque" || type == "opaqueredirect") {
|
|
|
|
url = "";
|
|
|
|
status = 0;
|
|
|
|
statusText = "";
|
|
|
|
headers = new Headers();
|
|
|
|
body = null;
|
2020-02-03 09:54:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
const contentType = headers.get("content-type") || "";
|
2020-07-07 22:25:34 -04:00
|
|
|
const size = Number(headers.get("content-length")) || undefined;
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2020-07-07 22:25:34 -04:00
|
|
|
super(body, { contentType, size });
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
this.url = url;
|
|
|
|
this.statusText = statusText;
|
2020-06-03 09:43:11 -04:00
|
|
|
this.status = extraInit.status || status;
|
2020-05-25 12:55:16 -04:00
|
|
|
this.headers = headers;
|
2020-07-05 21:37:18 -04:00
|
|
|
this.redirected = extraInit.redirected || false;
|
2020-05-25 12:55:16 -04:00
|
|
|
this.type = type;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get ok(): boolean {
|
|
|
|
return 200 <= this.status && this.status < 300;
|
|
|
|
}
|
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
public clone(): domTypes.Response {
|
2018-10-21 17:42:18 -04:00
|
|
|
if (this.bodyUsed) {
|
2020-05-25 12:55:16 -04:00
|
|
|
throw TypeError(Body.BodyUsedError);
|
2018-10-21 17:42:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const iterators = this.headers.entries();
|
|
|
|
const headersList: Array<[string, string]> = [];
|
|
|
|
for (const header of iterators) {
|
|
|
|
headersList.push(header);
|
|
|
|
}
|
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
let resBody = this._bodySource;
|
|
|
|
|
|
|
|
if (this._bodySource instanceof ReadableStreamImpl) {
|
|
|
|
const tees = this._bodySource.tee();
|
|
|
|
this._stream = this._bodySource = tees[0];
|
|
|
|
resBody = tees[1];
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:45:39 -04:00
|
|
|
return new Response(resBody, {
|
2020-05-25 12:55:16 -04:00
|
|
|
status: this.status,
|
|
|
|
statusText: this.statusText,
|
|
|
|
headers: new Headers(headersList),
|
|
|
|
});
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
2020-02-03 09:54:47 -05:00
|
|
|
|
2020-04-10 09:51:17 -04:00
|
|
|
static redirect(url: URL | string, status: number): domTypes.Response {
|
2020-02-03 09:54:47 -05:00
|
|
|
if (![301, 302, 303, 307, 308].includes(status)) {
|
|
|
|
throw new RangeError(
|
2020-07-14 15:24:17 -04:00
|
|
|
"The redirection status must be one of 301, 302, 303, 307 and 308.",
|
2020-02-03 09:54:47 -05:00
|
|
|
);
|
|
|
|
}
|
2020-05-25 12:55:16 -04:00
|
|
|
return new Response(null, {
|
2020-02-03 09:54:47 -05:00
|
|
|
status,
|
2020-05-25 12:55:16 -04:00
|
|
|
statusText: "",
|
|
|
|
headers: [["Location", typeof url === "string" ? url : url.toString()]],
|
|
|
|
});
|
2020-02-03 09:54:47 -05:00
|
|
|
}
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
function sendFetchReq(
|
2019-08-24 16:20:48 -04:00
|
|
|
url: string,
|
|
|
|
method: string | null,
|
2020-04-13 22:46:23 -04:00
|
|
|
headers: Headers | null,
|
2020-07-14 15:24:17 -04:00
|
|
|
body: ArrayBufferView | undefined,
|
2019-08-26 08:50:21 -04:00
|
|
|
): Promise<FetchResponse> {
|
|
|
|
let headerArray: Array<[string, string]> = [];
|
|
|
|
if (headers) {
|
|
|
|
headerArray = Array.from(headers.entries());
|
|
|
|
}
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
method,
|
|
|
|
url,
|
2020-03-28 13:03:49 -04:00
|
|
|
headers: headerArray,
|
2019-08-26 08:50:21 -04:00
|
|
|
};
|
|
|
|
|
2020-03-16 05:22:16 -04:00
|
|
|
return opFetch(args, body);
|
2019-06-24 09:34:09 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
export async function fetch(
|
2020-06-02 00:24:44 -04:00
|
|
|
input: (domTypes.Request & { _bodySource?: unknown }) | URL | string,
|
2020-07-14 15:24:17 -04:00
|
|
|
init?: domTypes.RequestInit,
|
2018-10-26 12:14:06 -04:00
|
|
|
): Promise<Response> {
|
2018-11-14 20:36:34 -05:00
|
|
|
let url: string;
|
|
|
|
let method: string | null = null;
|
2020-04-13 22:46:23 -04:00
|
|
|
let headers: Headers | null = null;
|
2018-11-14 21:19:38 -05:00
|
|
|
let body: ArrayBufferView | undefined;
|
2019-06-24 09:34:09 -04:00
|
|
|
let redirected = false;
|
|
|
|
let remRedirectCount = 20; // TODO: use a better way to handle
|
2018-11-14 20:36:34 -05:00
|
|
|
|
2019-12-14 07:49:30 -05:00
|
|
|
if (typeof input === "string" || input instanceof URL) {
|
|
|
|
url = typeof input === "string" ? (input as string) : (input as URL).href;
|
2018-11-14 20:36:34 -05:00
|
|
|
if (init != null) {
|
|
|
|
method = init.method || null;
|
|
|
|
if (init.headers) {
|
2020-07-14 15:24:17 -04:00
|
|
|
headers = init.headers instanceof Headers
|
|
|
|
? init.headers
|
|
|
|
: new Headers(init.headers);
|
2018-11-14 20:36:34 -05:00
|
|
|
} else {
|
|
|
|
headers = null;
|
|
|
|
}
|
2018-11-14 21:19:38 -05:00
|
|
|
|
2019-01-03 06:41:20 -05:00
|
|
|
// ref: https://fetch.spec.whatwg.org/#body-mixin
|
|
|
|
// Body should have been a mixin
|
|
|
|
// but we are treating it as a separate class
|
2018-11-14 21:19:38 -05:00
|
|
|
if (init.body) {
|
2019-01-03 06:41:20 -05:00
|
|
|
if (!headers) {
|
|
|
|
headers = new Headers();
|
|
|
|
}
|
|
|
|
let contentType = "";
|
2018-11-14 21:19:38 -05:00
|
|
|
if (typeof init.body === "string") {
|
|
|
|
body = new TextEncoder().encode(init.body);
|
2019-01-03 06:41:20 -05:00
|
|
|
contentType = "text/plain;charset=UTF-8";
|
2018-11-14 21:19:38 -05:00
|
|
|
} else if (isTypedArray(init.body)) {
|
|
|
|
body = init.body;
|
2020-05-25 09:26:36 -04:00
|
|
|
} else if (init.body instanceof ArrayBuffer) {
|
|
|
|
body = new Uint8Array(init.body);
|
2019-01-03 06:41:20 -05:00
|
|
|
} else if (init.body instanceof URLSearchParams) {
|
|
|
|
body = new TextEncoder().encode(init.body.toString());
|
|
|
|
contentType = "application/x-www-form-urlencoded;charset=UTF-8";
|
|
|
|
} else if (init.body instanceof DenoBlob) {
|
|
|
|
body = init.body[blobBytesSymbol];
|
|
|
|
contentType = init.body.type;
|
2020-03-17 02:32:43 -04:00
|
|
|
} else if (init.body instanceof FormData) {
|
2020-06-08 12:08:26 -04:00
|
|
|
let boundary;
|
2020-03-17 02:32:43 -04:00
|
|
|
if (headers.has("content-type")) {
|
|
|
|
const params = getHeaderValueParams("content-type");
|
2020-06-08 12:08:26 -04:00
|
|
|
boundary = params.get("boundary")!;
|
2020-03-17 02:32:43 -04:00
|
|
|
}
|
2020-06-08 12:08:26 -04:00
|
|
|
const multipartBuilder = new MultipartBuilder(init.body, boundary);
|
|
|
|
body = multipartBuilder.getBody();
|
|
|
|
contentType = multipartBuilder.getContentType();
|
2018-11-14 21:19:38 -05:00
|
|
|
} else {
|
2020-03-17 02:32:43 -04:00
|
|
|
// TODO: ReadableStream
|
2018-11-14 21:19:38 -05:00
|
|
|
notImplemented();
|
|
|
|
}
|
2019-01-03 06:41:20 -05:00
|
|
|
if (contentType && !headers.has("content-type")) {
|
|
|
|
headers.set("content-type", contentType);
|
|
|
|
}
|
2018-11-14 21:19:38 -05:00
|
|
|
}
|
2018-11-14 20:36:34 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
url = input.url;
|
|
|
|
method = input.method;
|
|
|
|
headers = input.headers;
|
2019-05-31 17:00:37 -04:00
|
|
|
|
|
|
|
if (input._bodySource) {
|
|
|
|
body = new DataView(await input.arrayBuffer());
|
|
|
|
}
|
2018-11-14 20:36:34 -05:00
|
|
|
}
|
2018-09-05 22:13:36 -04:00
|
|
|
|
2020-05-31 19:21:14 -04:00
|
|
|
let responseBody;
|
2020-07-06 21:45:39 -04:00
|
|
|
let responseInit: domTypes.ResponseInit = {};
|
2019-06-24 09:34:09 -04:00
|
|
|
while (remRedirectCount) {
|
2019-08-26 08:50:21 -04:00
|
|
|
const fetchResponse = await sendFetchReq(url, method, headers, body);
|
2019-06-24 09:34:09 -04:00
|
|
|
|
2020-05-31 19:21:14 -04:00
|
|
|
if (
|
|
|
|
NULL_BODY_STATUS.includes(fetchResponse.status) ||
|
|
|
|
REDIRECT_STATUS.includes(fetchResponse.status)
|
|
|
|
) {
|
|
|
|
// We won't use body of received response, so close it now
|
|
|
|
// otherwise it will be kept in resource table.
|
|
|
|
close(fetchResponse.bodyRid);
|
|
|
|
responseBody = null;
|
|
|
|
} else {
|
|
|
|
responseBody = new ReadableStreamImpl({
|
|
|
|
async pull(controller: ReadableStreamDefaultController): Promise<void> {
|
|
|
|
try {
|
|
|
|
const b = new Uint8Array(1024 * 32);
|
|
|
|
const result = await read(fetchResponse.bodyRid, b);
|
|
|
|
if (result === null) {
|
|
|
|
controller.close();
|
|
|
|
return close(fetchResponse.bodyRid);
|
|
|
|
}
|
|
|
|
|
|
|
|
controller.enqueue(b.subarray(0, result));
|
|
|
|
} catch (e) {
|
|
|
|
controller.error(e);
|
2020-05-25 12:55:16 -04:00
|
|
|
controller.close();
|
2020-05-31 19:21:14 -04:00
|
|
|
close(fetchResponse.bodyRid);
|
2020-05-25 12:55:16 -04:00
|
|
|
}
|
2020-05-31 19:21:14 -04:00
|
|
|
},
|
|
|
|
cancel(): void {
|
|
|
|
// When reader.cancel() is called
|
2020-05-25 12:55:16 -04:00
|
|
|
close(fetchResponse.bodyRid);
|
2020-05-31 19:21:14 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-05-25 12:55:16 -04:00
|
|
|
|
2020-05-31 16:13:53 -04:00
|
|
|
responseInit = {
|
2020-06-03 09:43:11 -04:00
|
|
|
status: 200,
|
2020-05-25 12:55:16 -04:00
|
|
|
statusText: fetchResponse.statusText,
|
|
|
|
headers: fetchResponse.headers,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData.set(responseInit, {
|
|
|
|
redirected,
|
|
|
|
rid: fetchResponse.bodyRid,
|
2020-06-03 09:43:11 -04:00
|
|
|
status: fetchResponse.status,
|
2019-08-16 18:20:04 -04:00
|
|
|
url,
|
2020-05-25 12:55:16 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const response = new Response(responseBody, responseInit);
|
|
|
|
|
2020-05-31 19:21:14 -04:00
|
|
|
if (REDIRECT_STATUS.includes(fetchResponse.status)) {
|
2019-06-24 09:34:09 -04:00
|
|
|
// We're in a redirect status
|
|
|
|
switch ((init && init.redirect) || "follow") {
|
|
|
|
case "error":
|
2020-05-25 12:55:16 -04:00
|
|
|
responseInit = {};
|
|
|
|
responseData.set(responseInit, {
|
|
|
|
type: "error",
|
|
|
|
redirected: false,
|
|
|
|
url: "",
|
|
|
|
});
|
|
|
|
return new Response(null, responseInit);
|
2019-06-24 09:34:09 -04:00
|
|
|
case "manual":
|
2020-05-25 12:55:16 -04:00
|
|
|
responseInit = {};
|
|
|
|
responseData.set(responseInit, {
|
|
|
|
type: "opaqueredirect",
|
|
|
|
redirected: false,
|
|
|
|
url: "",
|
|
|
|
});
|
|
|
|
return new Response(null, responseInit);
|
2019-06-24 09:34:09 -04:00
|
|
|
case "follow":
|
|
|
|
default:
|
|
|
|
let redirectUrl = response.headers.get("Location");
|
|
|
|
if (redirectUrl == null) {
|
|
|
|
return response; // Unspecified
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!redirectUrl.startsWith("http://") &&
|
|
|
|
!redirectUrl.startsWith("https://")
|
|
|
|
) {
|
2020-07-13 00:53:36 -04:00
|
|
|
redirectUrl = new URL(redirectUrl, url).href;
|
2019-06-24 09:34:09 -04:00
|
|
|
}
|
|
|
|
url = redirectUrl;
|
|
|
|
redirected = true;
|
|
|
|
remRedirectCount--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 16:13:53 -04:00
|
|
|
|
|
|
|
responseData.set(responseInit, {
|
|
|
|
type: "error",
|
|
|
|
redirected: false,
|
|
|
|
url: "",
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Response(null, responseInit);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|