|
| 1 | +// Copyright 2018 the Deno authors. All rights reserved. MIT license. |
| 2 | + |
| 3 | +import { Closer } from "./io"; |
| 4 | +import * as msg from "gen/msg_generated"; |
| 5 | +import { assert, log } from "./util"; |
| 6 | +import * as dispatch from "./dispatch"; |
| 7 | +import * as flatbuffers from "./flatbuffers"; |
| 8 | +import { close } from "./files"; |
| 9 | + |
| 10 | +// TODO Cannot use Headers due to bug in ts_declaration_builder. |
| 11 | +// import { Headers } from "./headers"; |
| 12 | +// import * as headers from "./headers"; |
| 13 | +// import * as domTypes from "./dom_types"; |
| 14 | + |
| 15 | +type HttpHandler = (req: ServerRequest, res: ServerResponse) => void; |
| 16 | + |
| 17 | +export class HttpServer implements Closer { |
| 18 | + private _closing = false; |
| 19 | + |
| 20 | + constructor(readonly rid: number) { |
| 21 | + assert(rid >= 2); // rid should be after stdout/stderr |
| 22 | + } |
| 23 | + |
| 24 | + async serve(handler: HttpHandler): Promise<void> { |
| 25 | + while (this._closing === false) { |
| 26 | + const [req, res] = await httpAccept(this.rid); |
| 27 | + handler(req, res); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + close(): void { |
| 32 | + this._closing = true; |
| 33 | + close(this.rid); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function deserializeHeaderFields(m: msg.HttpHeader): Array<[string, string]> { |
| 38 | + const out: Array<[string, string]> = []; |
| 39 | + for (let i = 0; i < m.fieldsLength(); i++) { |
| 40 | + const item = m.fields(i)!; |
| 41 | + out.push([item.key()!, item.value()!]); |
| 42 | + } |
| 43 | + return out; |
| 44 | +} |
| 45 | + |
| 46 | +export class ServerRequest { |
| 47 | + // TODO Cannot do this due to ts_declaration_builder bug. |
| 48 | + // headers: domTypes.Headers; |
| 49 | + readonly headers: Array<[string, string]>; |
| 50 | + |
| 51 | + constructor( |
| 52 | + readonly rid: number, |
| 53 | + readonly method: string, |
| 54 | + readonly url: string, |
| 55 | + headersInit: Array<[string, string]> |
| 56 | + ) { |
| 57 | + // TODO cannot use Headers due to ts_declaration_builder bug. |
| 58 | + // this.headers = new Headers(headersInit); |
| 59 | + this.headers = headersInit; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export class ServerResponse { |
| 64 | + headers = new Array<[string, string]>(); // TODO Use Headers |
| 65 | + status = 200; |
| 66 | + |
| 67 | + constructor(readonly rid: number, readonly url: string) {} |
| 68 | + |
| 69 | + writeResponse(body?: ArrayBufferView): void { |
| 70 | + httpWriteResponse(this, body); |
| 71 | + close(this.rid); // TODO Streaming response body. |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +async function httpAccept( |
| 76 | + rid: number |
| 77 | +): Promise<[ServerRequest, ServerResponse]> { |
| 78 | + const builder = flatbuffers.createBuilder(); |
| 79 | + |
| 80 | + msg.HttpAccept.startHttpAccept(builder); |
| 81 | + msg.HttpAccept.addListenerRid(builder, rid); |
| 82 | + const inner = msg.HttpAccept.endHttpAccept(builder); |
| 83 | + |
| 84 | + const baseRes = await dispatch.sendAsync(builder, msg.Any.HttpAccept, inner); |
| 85 | + assert(baseRes != null); |
| 86 | + assert(msg.Any.HttpAcceptRes === baseRes!.innerType()); |
| 87 | + const acceptResMsg = new msg.HttpAcceptRes(); |
| 88 | + assert(baseRes!.inner(acceptResMsg) != null); |
| 89 | + |
| 90 | + const transactionRid = acceptResMsg.transactionRid(); |
| 91 | + const header = acceptResMsg.header()!; |
| 92 | + const fields = deserializeHeaderFields(header); |
| 93 | + const url = header.url()!; |
| 94 | + const method = header.method()!; |
| 95 | + log("http accept:", method, url, fields); |
| 96 | + |
| 97 | + const req = new ServerRequest(transactionRid, method, url, fields); |
| 98 | + const res = new ServerResponse(transactionRid, url); |
| 99 | + return [req, res]; |
| 100 | +} |
| 101 | + |
| 102 | +export function httpListen(address: string): HttpServer { |
| 103 | + const builder = flatbuffers.createBuilder(); |
| 104 | + const address_ = builder.createString(address); |
| 105 | + |
| 106 | + msg.HttpListen.startHttpListen(builder); |
| 107 | + msg.HttpListen.addAddress(builder, address_); |
| 108 | + const inner = msg.HttpListen.endHttpListen(builder); |
| 109 | + |
| 110 | + const baseRes = dispatch.sendSync(builder, msg.Any.HttpListen, inner); |
| 111 | + assert(baseRes != null); |
| 112 | + assert(msg.Any.HttpListenRes === baseRes!.innerType()); |
| 113 | + const res = new msg.HttpListenRes(); |
| 114 | + assert(baseRes!.inner(res) != null); |
| 115 | + return new HttpServer(res.rid()); |
| 116 | +} |
| 117 | + |
| 118 | +export function httpWriteResponse( |
| 119 | + res: ServerResponse, |
| 120 | + body?: ArrayBufferView |
| 121 | +): void { |
| 122 | + const builder = flatbuffers.createBuilder(); |
| 123 | + const fields = msg.HttpHeader.createFieldsVector( |
| 124 | + builder, |
| 125 | + res.headers.map(([key, val]) => { |
| 126 | + const key_ = builder.createString(key); |
| 127 | + const val_ = builder.createString(val); |
| 128 | + msg.KeyValue.startKeyValue(builder); |
| 129 | + msg.KeyValue.addKey(builder, key_); |
| 130 | + msg.KeyValue.addValue(builder, val_); |
| 131 | + return msg.KeyValue.endKeyValue(builder); |
| 132 | + }) |
| 133 | + ); |
| 134 | + msg.HttpHeader.startHttpHeader(builder); |
| 135 | + msg.HttpHeader.addFields(builder, fields); |
| 136 | + msg.HttpHeader.addStatus(builder, res.status); |
| 137 | + msg.HttpHeader.addIsRequest(builder, false); |
| 138 | + |
| 139 | + const header = msg.HttpHeader.endHttpHeader(builder); |
| 140 | + msg.HttpWriteResponse.startHttpWriteResponse(builder); |
| 141 | + msg.HttpWriteResponse.addTransactionRid(builder, res.rid); |
| 142 | + msg.HttpWriteResponse.addHeader(builder, header); |
| 143 | + const inner = msg.HttpWriteResponse.endHttpWriteResponse(builder); |
| 144 | + const r = dispatch.sendSync(builder, msg.Any.HttpWriteResponse, inner, body); |
| 145 | + assert(r == null); |
| 146 | +} |
0 commit comments