1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-03 17:08:35 -05:00
denoland-deno/http.ts

213 lines
5.2 KiB
TypeScript
Raw Normal View History

import { listen, Conn, toAsyncIterator, Reader, copy } from "deno";
2018-11-09 17:23:01 -05:00
import { BufReader, BufState, BufWriter } from "./bufio.ts";
2018-11-07 13:16:07 -05:00
import { TextProtoReader } from "./textproto.ts";
2018-11-09 17:23:01 -05:00
import { STATUS_TEXT } from "./http_status";
import { assert } from "./util";
2018-11-07 13:16:07 -05:00
interface Deferred {
promise: Promise<{}>;
resolve: () => void;
reject: () => void;
}
function deferred(): Deferred {
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
resolve,
reject
};
}
interface ServeEnv {
reqQueue: ServerRequest[];
serveDeferred: Deferred;
}
// Continuously read more requests from conn until EOF
// Mutually calling with maybeHandleReq
// TODO: make them async function after this change is done
// https://github.com/tc39/ecma262/pull/1250
// See https://v8.dev/blog/fast-async
export function serveConn(env: ServeEnv, conn: Conn) {
readRequest(conn).then(maybeHandleReq.bind(null, env, conn));
}
function maybeHandleReq(env: ServeEnv, conn: Conn, maybeReq: any) {
const [req, _err] = maybeReq;
if (_err) {
conn.close(); // assume EOF for now...
return;
2018-11-07 13:16:07 -05:00
}
env.reqQueue.push(req); // push req to queue
env.serveDeferred.resolve(); // signal while loop to process it
// TODO: protection against client req flooding
serveConn(env, conn); // try read more (reusing connection)
2018-11-08 15:03:45 -05:00
}
2018-11-07 13:16:07 -05:00
export async function* serve(addr: string) {
const listener = listen("tcp", addr);
const env: ServeEnv = {
reqQueue: [], // in case multiple promises are ready
serveDeferred: deferred()
};
// Routine that keeps calling accept
const acceptRoutine = () => {
const handleConn = (conn: Conn) => {
serveConn(env, conn); // don't block
scheduleAccept(); // schedule next accept
};
const scheduleAccept = () => {
listener.accept().then(handleConn);
};
scheduleAccept();
};
acceptRoutine();
// Loop hack to allow yield (yield won't work in callbacks)
while (true) {
await env.serveDeferred.promise;
env.serveDeferred = deferred(); // use a new deferred
let queueToProcess = env.reqQueue;
env.reqQueue = [];
for (const result of queueToProcess) {
yield result;
2018-11-08 15:03:45 -05:00
}
2018-11-07 13:16:07 -05:00
}
listener.close();
2018-11-07 13:16:07 -05:00
}
export async function listenAndServe(
addr: string,
handler: (req: ServerRequest) => void
) {
2018-12-09 15:35:26 -05:00
const server = serve(addr);
for await (const request of server) {
await handler(request);
}
}
export interface Response {
2018-11-08 15:03:45 -05:00
status?: number;
2018-11-09 17:23:01 -05:00
headers?: Headers;
body?: Uint8Array | Reader;
2018-11-09 17:23:01 -05:00
}
export function setContentLength(r: Response): void {
if (!r.headers) {
r.headers = new Headers();
}
if (r.body) {
if (!r.headers.has("content-length")) {
if (r.body instanceof Uint8Array) {
const bodyLength = r.body.byteLength;
r.headers.append("Content-Length", bodyLength.toString());
} else {
r.headers.append("Transfer-Encoding", "chunked");
}
}
2018-11-09 17:23:01 -05:00
}
2018-11-08 15:03:45 -05:00
}
2018-11-07 13:16:07 -05:00
export class ServerRequest {
2018-11-08 15:03:45 -05:00
url: string;
method: string;
proto: string;
2018-11-08 18:15:26 -05:00
headers: Headers;
2018-11-09 17:23:01 -05:00
w: BufWriter;
private async _streamBody(body: Reader, bodyLength: number) {
const n = await copy(this.w, body);
assert(n == bodyLength);
}
private async _streamChunkedBody(body: Reader) {
const encoder = new TextEncoder();
for await (const chunk of toAsyncIterator(body)) {
const start = encoder.encode(`${chunk.byteLength.toString(16)}\r\n`);
const end = encoder.encode("\r\n");
await this.w.write(start);
await this.w.write(chunk);
await this.w.write(end);
}
const endChunk = encoder.encode("0\r\n\r\n");
await this.w.write(endChunk);
}
2018-11-09 17:23:01 -05:00
async respond(r: Response): Promise<void> {
const protoMajor = 1;
const protoMinor = 1;
const statusCode = r.status || 200;
const statusText = STATUS_TEXT.get(statusCode);
if (!statusText) {
throw Error("bad status code");
}
2018-11-07 13:16:07 -05:00
2018-12-04 20:34:35 -05:00
let out = `HTTP/${protoMajor}.${protoMinor} ${statusCode} ${statusText}\r\n`;
2018-11-09 17:23:01 -05:00
setContentLength(r);
if (r.headers) {
for (const [key, value] of r.headers) {
2018-11-09 17:23:01 -05:00
out += `${key}: ${value}\r\n`;
}
}
out += "\r\n";
const header = new TextEncoder().encode(out);
let n = await this.w.write(header);
assert(header.byteLength == n);
2018-11-09 17:23:01 -05:00
if (r.body) {
if (r.body instanceof Uint8Array) {
n = await this.w.write(r.body);
assert(r.body.byteLength == n);
} else {
if (r.headers.has("content-length")) {
await this._streamBody(
r.body,
parseInt(r.headers.get("content-length"))
);
} else {
await this._streamChunkedBody(r.body);
}
}
2018-11-09 17:23:01 -05:00
}
await this.w.flush();
2018-11-07 01:08:51 -05:00
}
2018-11-07 13:16:07 -05:00
}
2018-11-07 01:08:51 -05:00
async function readRequest(c: Conn): Promise<[ServerRequest, BufState]> {
const bufr = new BufReader(c);
const bufw = new BufWriter(c);
2018-11-07 13:16:07 -05:00
const req = new ServerRequest();
req.w = bufw;
const tp = new TextProtoReader(bufr);
2018-11-07 01:08:51 -05:00
2018-11-08 15:03:45 -05:00
let s: string;
let err: BufState;
2018-11-08 18:15:26 -05:00
// First line: GET /index.html HTTP/1.0
2018-11-08 15:03:45 -05:00
[s, err] = await tp.readLine();
2018-11-09 17:23:01 -05:00
if (err) {
return [null, err];
}
2018-11-08 18:15:26 -05:00
[req.method, req.url, req.proto] = s.split(" ", 3);
2018-11-07 13:16:07 -05:00
2018-11-08 18:15:26 -05:00
[req.headers, err] = await tp.readMIMEHeader();
2018-11-08 15:03:45 -05:00
// TODO: handle body
2018-11-09 17:23:01 -05:00
return [req, err];
2018-11-07 01:08:51 -05:00
}