Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std/http: add serveTLS and listenAndServeTLS #3257

Merged
merged 4 commits into from Nov 4, 2019

Conversation

@kevinkassimo
Copy link
Contributor

kevinkassimo commented Nov 3, 2019

Created serveTLS using listenTLS for HTTPS instead of listen for HTTP.

Example:

import { serve, serveTLS } from "./std/http/server.ts";

const tlsOptions = {
  hostname: "localhost",
  port: 443,
  certFile: "./localhost.pem",
  keyFile: "./localhost-key.pem",
};

async function httpServer() {
  console.log(`Simple HTTP server listening on localhost:80`);
  for await (const req of serve("localhost:80")) {
    const headers = new Headers();
    headers.set("Location", "https://localhost");
    req.respond({ status: 307, headers });
  }
}

async function httpsServer() {
  console.log(`Simple HTTPS server listening on ${tlsOptions.hostname}:${tlsOptions.port}`);
  const body = new TextEncoder().encode("Hello HTTPS");
  for await (const req of serveTLS(tlsOptions)) {
    req.respond({ body });
  }
}

httpServer();
httpsServer();

Screen Shot 2019-11-03 at 11 55 10 AM

There seems to be still some issue when plain HTTP payload is submitted to the serveTLS that yields InvalidData error. We need to investigate how to gracefully handle the error in potentially another PR.

)
.catch((_): void => {}); // Ignores the error when closing the process.

await delay(100);

This comment has been minimized.

Copy link
@ry

ry Nov 4, 2019

Collaborator

Is this necessary given you wait for "server listening" on stdout above?

This comment has been minimized.

Copy link
@kevinkassimo

kevinkassimo Nov 4, 2019

Author Contributor

Without this delay we get Connection refused error on client side.

const nread = assertNotEOF(await conn.read(res));
conn.close();
const resStr = new TextDecoder().decode(res.subarray(0, nread));
assert(resStr.includes("Hello HTTPS"));

This comment has been minimized.

Copy link
@ry

ry Nov 4, 2019

Collaborator

Nice test.

@@ -401,6 +401,28 @@ export async function listenAndServe(
}
}

export type HTTPSOptions = Omit<Deno.ListenTLSOptions, "transport">;

export function serveTLS(options: HTTPSOptions): Server {

This comment has been minimized.

Copy link
@ry

ry Nov 4, 2019

Collaborator

Can you add a jsdoc with a little example?

We have a documentation viewer now - so it will be visible at https://deno.land/std/http/server.ts?doc

This comment has been minimized.

Copy link
@kevinkassimo

kevinkassimo Nov 4, 2019

Author Contributor

Added.

@kevinkassimo kevinkassimo force-pushed the kevinkassimo:std/http/https branch from 41ce8e7 to d9389d4 Nov 4, 2019
certFile: "./http/testdata/tls/localhost.crt",
keyFile: "./http/testdata/tls/localhost.key",
};
console.log(`Simple HTTPS server listening on ${tlsOptions.hostname}:${tlsOptions.port}`);

This comment has been minimized.

Copy link
@ry

ry Nov 4, 2019

Collaborator

Can you try printing this after serveTLS is called? That might allow removing the delay

fix
@ry
ry approved these changes Nov 4, 2019
Copy link
Collaborator

ry left a comment

LGTM

@ry ry merged commit 0644f9c into denoland:master Nov 4, 2019
10 checks passed
10 checks passed
test macOS-latest
Details
test_std macOS-latest
Details
test windows-2019
Details
test_std windows-2019
Details
test ubuntu-16.04
Details
test_debug ubuntu-16.04
Details
test_std ubuntu-16.04
Details
bench ubuntu-16.04
Details
lint ubuntu-16.04
Details
license/cla Contributor License Agreement is signed.
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.