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
|
|
|
|
2019-09-02 17:07:11 -04:00
|
|
|
import * as body from "./body.ts";
|
2020-07-08 05:26:39 -04:00
|
|
|
import type * as domTypes from "./dom_types.d.ts";
|
2020-04-22 10:06:51 -04:00
|
|
|
import { ReadableStreamImpl } from "./streams/readable_stream.ts";
|
2019-05-01 23:56:42 -04:00
|
|
|
|
|
|
|
function byteUpperCase(s: string): string {
|
|
|
|
return String(s).replace(/[a-z]/g, function byteUpperCaseReplace(c): string {
|
|
|
|
return c.toUpperCase();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeMethod(m: string): string {
|
|
|
|
const u = byteUpperCase(m);
|
|
|
|
if (
|
|
|
|
u === "DELETE" ||
|
|
|
|
u === "GET" ||
|
|
|
|
u === "HEAD" ||
|
|
|
|
u === "OPTIONS" ||
|
|
|
|
u === "POST" ||
|
|
|
|
u === "PUT"
|
|
|
|
) {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Request extends body.Body implements domTypes.Request {
|
|
|
|
public method: string;
|
|
|
|
public url: string;
|
|
|
|
public credentials?: "omit" | "same-origin" | "include";
|
2020-04-13 22:46:23 -04:00
|
|
|
public headers: Headers;
|
2019-05-01 23:56:42 -04:00
|
|
|
|
|
|
|
constructor(input: domTypes.RequestInfo, init?: domTypes.RequestInit) {
|
|
|
|
if (arguments.length < 1) {
|
|
|
|
throw TypeError("Not enough arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!init) {
|
|
|
|
init = {};
|
|
|
|
}
|
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
let b: BodyInit;
|
2019-05-01 23:56:42 -04:00
|
|
|
|
|
|
|
// prefer body from init
|
|
|
|
if (init.body) {
|
|
|
|
b = init.body;
|
|
|
|
} else if (input instanceof Request && input._bodySource) {
|
|
|
|
if (input.bodyUsed) {
|
|
|
|
throw TypeError(body.BodyUsedError);
|
|
|
|
}
|
|
|
|
b = input._bodySource;
|
|
|
|
} else if (typeof input === "object" && "body" in input && input.body) {
|
|
|
|
if (input.bodyUsed) {
|
|
|
|
throw TypeError(body.BodyUsedError);
|
|
|
|
}
|
|
|
|
b = input.body;
|
|
|
|
} else {
|
|
|
|
b = "";
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:46:23 -04:00
|
|
|
let headers: Headers;
|
2019-05-01 23:56:42 -04:00
|
|
|
|
|
|
|
// prefer headers from init
|
|
|
|
if (init.headers) {
|
|
|
|
headers = new Headers(init.headers);
|
|
|
|
} else if (input instanceof Request) {
|
|
|
|
headers = input.headers;
|
|
|
|
} else {
|
|
|
|
headers = new Headers();
|
|
|
|
}
|
|
|
|
|
|
|
|
const contentType = headers.get("content-type") || "";
|
2020-07-07 22:25:34 -04:00
|
|
|
super(b, { contentType });
|
2019-05-01 23:56:42 -04:00
|
|
|
this.headers = headers;
|
|
|
|
|
|
|
|
// readonly attribute ByteString method;
|
|
|
|
this.method = "GET";
|
|
|
|
|
|
|
|
// readonly attribute USVString url;
|
|
|
|
this.url = "";
|
|
|
|
|
|
|
|
// readonly attribute RequestCredentials credentials;
|
|
|
|
this.credentials = "omit";
|
|
|
|
|
|
|
|
if (input instanceof Request) {
|
|
|
|
if (input.bodyUsed) {
|
|
|
|
throw TypeError(body.BodyUsedError);
|
|
|
|
}
|
|
|
|
this.method = input.method;
|
|
|
|
this.url = input.url;
|
|
|
|
this.headers = new Headers(input.headers);
|
|
|
|
this.credentials = input.credentials;
|
|
|
|
this._stream = input._stream;
|
|
|
|
} else if (typeof input === "string") {
|
|
|
|
this.url = input;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init && "method" in init) {
|
|
|
|
this.method = normalizeMethod(init.method as string);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
init &&
|
|
|
|
"credentials" in init &&
|
|
|
|
init.credentials &&
|
|
|
|
["omit", "same-origin", "include"].indexOf(init.credentials) !== -1
|
|
|
|
) {
|
|
|
|
this.credentials = init.credentials;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public clone(): domTypes.Request {
|
|
|
|
if (this.bodyUsed) {
|
|
|
|
throw TypeError(body.BodyUsedError);
|
|
|
|
}
|
|
|
|
|
|
|
|
const iterators = this.headers.entries();
|
|
|
|
const headersList: Array<[string, string]> = [];
|
|
|
|
for (const header of iterators) {
|
|
|
|
headersList.push(header);
|
|
|
|
}
|
|
|
|
|
2019-10-28 12:41:36 -04:00
|
|
|
let body2 = this._bodySource;
|
|
|
|
|
2020-04-22 10:06:51 -04:00
|
|
|
if (this._bodySource instanceof ReadableStreamImpl) {
|
|
|
|
const tees = this._bodySource.tee();
|
2019-10-28 12:41:36 -04:00
|
|
|
this._stream = this._bodySource = tees[0];
|
|
|
|
body2 = tees[1];
|
|
|
|
}
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-07-06 21:45:39 -04:00
|
|
|
return new Request(this.url, {
|
2019-05-01 23:56:42 -04:00
|
|
|
body: body2,
|
|
|
|
method: this.method,
|
|
|
|
headers: new Headers(headersList),
|
2020-03-28 13:03:49 -04:00
|
|
|
credentials: this.credentials,
|
2019-05-01 23:56:42 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|