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

Fix Astro.url.protocol when using the @astrojs/node SSR adapter with HTTPS #5992

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/odd-rats-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/node': patch
---

Fix `Astro.url.protocol` when using the @astrojs/node SSR adapter with HTTPS
7 changes: 6 additions & 1 deletion packages/astro/src/core/app/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import type { SerializedSSRManifest, SSRManifest } from './types';

import * as fs from 'fs';
import { IncomingMessage } from 'http';
import { TLSSocket } from 'tls';
import { deserializeManifest } from './common.js';
import { App, MatchOptions } from './index.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');

function createRequestFromNodeRequest(req: IncomingMessage, body?: Uint8Array): Request {
let url = `http://${req.headers.host}${req.url}`;
const protocol =
req.socket instanceof TLSSocket || req.headers['x-forwarded-proto'] === 'https'
? 'https'
: 'http';
let url = `${protocol}://${req.headers.host}${req.url}`;
let rawHeaders = req.headers as Record<string, any>;
const entries = Object.entries(rawHeaders);
const method = req.method || 'GET';
Expand Down
4 changes: 3 additions & 1 deletion packages/integrations/node/src/standalone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NodeApp } from 'astro/app/node';
import https from 'https';
import path from 'path';
import { fileURLToPath } from 'url';
import { createServer } from './http-server.js';
Expand Down Expand Up @@ -53,8 +54,9 @@ export default function startServer(app: NodeApp, options: Options) {
handler
);

const protocol = server.server instanceof https.Server ? 'https' : 'http';
// eslint-disable-next-line no-console
console.log(`Server listening on http://${host}:${port}`);
console.log(`Server listening on ${protocol}://${host}:${port}`);

return server.closed();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/url-protocol",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
---

<html lang="en">
<head>
<title>url-protocol</title>
</head>
<body>
{Astro.url.protocol}
</body>
</html>
73 changes: 73 additions & 0 deletions packages/integrations/node/test/url-protocol.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { TLSSocket } from 'tls';
import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse } from './test-utils.js';
import { expect } from 'chai';

describe('URL protocol', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/url-protocol/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
});

it('return http when non-secure', async () => {
const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs');
let { req, res, text } = createRequestAndResponse({
url: '/',
});

handler(req, res);
req.send();

const html = await text();
expect(html).to.include('http:');
});

it('return https when secure', async () => {
const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs');
let { req, res, text } = createRequestAndResponse({
socket: new TLSSocket(),
url: '/',
});

handler(req, res);
req.send();

const html = await text();
expect(html).to.include('https:');
});

it('return http when the X-Forwarded-Proto header is set to http', async () => {
const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs');
let { req, res, text } = createRequestAndResponse({
headers: { 'X-Forwarded-Proto': 'http' },
url: '/',
});

handler(req, res);
req.send();

const html = await text();
expect(html).to.include('http:');
});

it('return https when the X-Forwarded-Proto header is set to https', async () => {
const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs');
let { req, res, text } = createRequestAndResponse({
headers: { 'X-Forwarded-Proto': 'https' },
url: '/',
});

handler(req, res);
req.send();

const html = await text();
expect(html).to.include('https:');
});
});
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.