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 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test.

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

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

export function serveTLS(options: HTTPSOptions): Server {
Copy link
Member

@ry ry Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

certFile: "./http/testdata/tls/localhost.crt",
keyFile: "./http/testdata/tls/localhost.key",
};
console.log(`Simple HTTPS server listening on ${tlsOptions.hostname}:${tlsOptions.port}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ry ry merged commit 0644f9c into denoland:master Nov 4, 2019
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 21, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants