diff --git a/src/http_client.ts b/src/http_client.ts index af59e4c..abafbfa 100644 --- a/src/http_client.ts +++ b/src/http_client.ts @@ -6,11 +6,11 @@ const DELIMITER = "\r\n"; export type HeaderValue = string | number; type Proxy = { - hostname: string, - port: number, - username?: string, - password?: string -} + hostname: string; + port: number; + username?: string; + password?: string; +}; export type Request = { method: "GET" | "POST" | "PUT" | "DELETE"; @@ -25,13 +25,19 @@ export type Response = { headers: Map; }; -export async function exchange(request: Request, proxy?: Proxy): Promise { - - const endpointUrl = request.url instanceof URL ? request.url : new URL(request.url); +export async function exchange( + request: Request, + proxy?: Proxy, +): Promise { + const endpointUrl = request.url instanceof URL + ? request.url + : new URL(request.url); const connectTls = !proxy && endpointUrl.protocol === "https:"; const connectHostname = proxy ? proxy.hostname : endpointUrl.hostname; - const connectPort = proxy ? proxy.port : endpointUrl.port + const connectPort = proxy + ? proxy.port + : endpointUrl.port ? Number.parseInt(endpointUrl.port) : (endpointUrl.protocol === "https:") ? 443 @@ -40,7 +46,9 @@ export async function exchange(request: Request, proxy?: Proxy): Promise { - +async function connectProxy( + endpointUrl: URL, + conn: Deno.Conn, + reader: BufReader, + proxy: Proxy, +): Promise { const port = endpointUrl.port ? endpointUrl.port : 443; const auth = encode(proxy.username + ":" + proxy.password); const requestLine = `CONNECT ${endpointUrl.hostname}:${port} HTTP/1.1${DELIMITER}` + `Host: ${endpointUrl.hostname}:${port}${DELIMITER}` + - (proxy.username ? `Proxy-Authorization: Basic ${auth}${DELIMITER}` : "") + + (proxy.username ? `Proxy-Authorization: Basic ${auth}${DELIMITER}` : "") + `Proxy-Connection: Keep-Alive${DELIMITER}${DELIMITER}`; console.debug(requestLine); @@ -85,7 +97,10 @@ async function connectProxy(endpointUrl: URL, conn: Deno.Conn, reader: BufReader } // TODO if 200 response, start TLS - return (Deno as any).startTls(conn, {hostname: endpointUrl.hostname, port: port}); // TODO unstable + return (Deno as any).startTls( + conn, + { hostname: endpointUrl.hostname, port: port }, + ); // TODO unstable } export class Header { diff --git a/src/http_client_test.ts b/src/http_client_test.ts index 3ad4b7f..d51ba35 100644 --- a/src/http_client_test.ts +++ b/src/http_client_test.ts @@ -8,7 +8,10 @@ const url = // const url = "https://github.com/"; const request: Request = { method: "GET", url: url }; -const res = await exchange(request, {hostname: "localhost",port: 3128, username: "user1", password: "test"}); +const res = await exchange( + request, + { hostname: "localhost", port: 3128, username: "user1", password: "test" }, +); console.log("Status: " + res.status); console.log("Body: " + res.body); @@ -16,5 +19,3 @@ console.log("Body: " + res.body); // $ /etc/init.d/squid restart // /etc/squid/squid.conf - -