|
| 1 | +export interface HttpQuery { |
| 2 | + name: string; |
| 3 | + value: string; |
| 4 | +} |
| 5 | + |
| 6 | +export interface HttpHeader { |
| 7 | + [key: string]: string; |
| 8 | +} |
| 9 | + |
| 10 | +export interface HttpResponse { |
| 11 | + status?: number; |
| 12 | + headers?: HttpHeader; |
| 13 | + body?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export interface HttpRequest { |
| 17 | + method: string; |
| 18 | + path: string; |
| 19 | + query: HttpQuery[]; |
| 20 | + headers: HttpHeader; |
| 21 | + body: string; |
| 22 | +} |
| 23 | + |
| 24 | +export class HttpClient { |
| 25 | + buf: Uint8Array; |
| 26 | + res: string; |
| 27 | + conn: Deno.Conn; |
| 28 | + |
| 29 | + constructor(conn: Deno.Conn) { |
| 30 | + this.conn = conn; |
| 31 | + this.buf = new Uint8Array(1); |
| 32 | + this.res = ""; |
| 33 | + } |
| 34 | + |
| 35 | + async readLine() { |
| 36 | + const dec = new TextDecoder(); |
| 37 | + this.res = ""; |
| 38 | + this.buf = new Uint8Array(1); |
| 39 | + |
| 40 | + while (true) { |
| 41 | + if (this.res.indexOf("\n") !== -1) { |
| 42 | + return this.res.slice(0, this.res.length - 2); |
| 43 | + } |
| 44 | + await this.conn.read(this.buf); |
| 45 | + this.res += dec.decode(this.buf); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + async readHead(res: HttpResponse) { |
| 50 | + const line = await this.readLine(); |
| 51 | + res.status = parseInt(line.split(" ")[1]); |
| 52 | + } |
| 53 | + |
| 54 | + async readHeaders(res: HttpResponse) { |
| 55 | + let isEnd = false; |
| 56 | + |
| 57 | + res.headers = {}; |
| 58 | + while (!isEnd) { |
| 59 | + const line = await this.readLine(); |
| 60 | + if (line === "") { |
| 61 | + isEnd = true; |
| 62 | + } else { |
| 63 | + const [name, value] = line.split(":"); |
| 64 | + res.headers[name.trim()] = value.trim(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + async readBody(res: HttpResponse) { |
| 70 | + const dec = new TextDecoder(); |
| 71 | + let finished = false; |
| 72 | + let body = ""; |
| 73 | + const headers = res!.headers; |
| 74 | + |
| 75 | + if (headers!["Transfer-Encoding"] === "chunked") { |
| 76 | + while (!finished) { |
| 77 | + const bufsize = parseInt(await this.readLine(), 16); |
| 78 | + if (bufsize === 0) { |
| 79 | + finished = true; |
| 80 | + } else { |
| 81 | + const buf = new ArrayBuffer(bufsize); |
| 82 | + const arr = new Uint8Array(buf); |
| 83 | + await this.read(arr); |
| 84 | + body += dec.decode(arr); |
| 85 | + await this.readLine(); |
| 86 | + } |
| 87 | + } |
| 88 | + } else { |
| 89 | + const bufsize = parseInt(res?.headers!["Content-Length"], 10); |
| 90 | + const buf = new ArrayBuffer(bufsize); |
| 91 | + const arr = new Uint8Array(buf); |
| 92 | + await this.read(arr); |
| 93 | + body += dec.decode(arr); |
| 94 | + } |
| 95 | + res.body = body; |
| 96 | + } |
| 97 | + |
| 98 | + async read(buf: Uint8Array) { |
| 99 | + return this.conn.read(buf); |
| 100 | + } |
| 101 | + |
| 102 | + async send(data: string) { |
| 103 | + const enc = new TextEncoder(); |
| 104 | + |
| 105 | + return this.conn.write(enc.encode(data)); |
| 106 | + } |
| 107 | + |
| 108 | + buildQueryString(query: HttpQuery[]) { |
| 109 | + return query.map((v) => `${v.name}=${v.value}`).join("&"); |
| 110 | + } |
| 111 | + |
| 112 | + buildHeaders(headers: HttpHeader) { |
| 113 | + return Object.keys(headers).map((v) => `${v}: ${headers[v]}`).join("\r\n"); |
| 114 | + } |
| 115 | + |
| 116 | + async sendRequest(request: HttpRequest) { |
| 117 | + const head = `${request.method} ${request.path}?${this.buildQueryString(request.query) |
| 118 | + } HTTP/1.1\r\n`; |
| 119 | + if (request.body.length > 0) { |
| 120 | + request.headers["Content-length"] = request.body.length.toString(); |
| 121 | + request.headers["Content-type"] = "application/json"; |
| 122 | + } |
| 123 | + const headers = this.buildHeaders(request.headers); |
| 124 | + const reqString = head + headers + "\r\n\r\n" + request.body; |
| 125 | + await this.send(reqString); |
| 126 | + const response: HttpResponse = {}; |
| 127 | + await this.readHead(response); |
| 128 | + await this.readHeaders(response); |
| 129 | + await this.readBody(response); |
| 130 | + await this.conn.close(); |
| 131 | + return response; |
| 132 | + } |
| 133 | +} |
0 commit comments