Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
chibat committed Jul 15, 2020
1 parent b4e6bf3 commit c4f6179
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
43 changes: 29 additions & 14 deletions src/http_client.ts
Expand Up @@ -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";
Expand All @@ -25,13 +25,19 @@ export type Response = {
headers: Map<string, HeaderValue>;
};

export async function exchange(request: Request, proxy?: Proxy): Promise<Response> {

const endpointUrl = request.url instanceof URL ? request.url : new URL(request.url);
export async function exchange(
request: Request,
proxy?: Proxy,
): Promise<Response> {
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
Expand All @@ -40,7 +46,9 @@ export async function exchange(request: Request, proxy?: Proxy): Promise<Respons
const connectParam = { hostname: connectHostname, port: connectPort };

let conn =
await (connectTls ? Deno.connectTls(connectParam) : Deno.connect(connectParam));
await (connectTls
? Deno.connectTls(connectParam)
: Deno.connect(connectParam));

let reader = new BufReader(conn);

Expand All @@ -59,15 +67,19 @@ export async function exchange(request: Request, proxy?: Proxy): Promise<Respons
}

// for TLS
async function connectProxy(endpointUrl: URL, conn: Deno.Conn, reader: BufReader, proxy: Proxy): Promise<Deno.Conn> {

async function connectProxy(
endpointUrl: URL,
conn: Deno.Conn,
reader: BufReader,
proxy: Proxy,
): Promise<Deno.Conn> {
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);
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions src/http_client_test.ts
Expand Up @@ -8,13 +8,14 @@ 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);

// deno run -A --unstable http_client_test.ts

// $ /etc/init.d/squid restart
// /etc/squid/squid.conf


0 comments on commit c4f6179

Please sign in to comment.