Skip to content

Commit

Permalink
fix: ignore 'content-type' header capitalization (sveltejs#1463)
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed May 30, 2021
1 parent 1bb44ca commit 2ee331d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pretty-cycles-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow non-lowercase 'content-type' header in ssr fetch requests
4 changes: 3 additions & 1 deletion packages/kit/src/runtime/server/parse_body/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { lowercase_keys } from '../utils.js';
import { read_only_form_data } from './read_only_form_data.js';

/** @param {import('types/hooks').Incoming} req */
export function parse_body(req) {
const raw = req.rawBody;
if (!raw) return raw;

const [type, ...directives] = req.headers['content-type'].split(/;\s*/);
const headers = lowercase_keys(req.headers);
const [type, ...directives] = headers['content-type'].split(/;\s*/);

if (typeof raw === 'string') {
switch (type) {
Expand Down
21 changes: 21 additions & 0 deletions packages/kit/src/runtime/server/parse_body/parse_body.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { parse_body } from './index.js';

test('parses a body with a lowercase content-type', () => {
const msg = {
rawBody: JSON.stringify('the body'),
headers: { 'content-type': 'application/json' }
};
assert.equal(parse_body(msg), 'the body');
});

test('parses a body with a non-lowercase content-type', () => {
const msg = {
rawBody: JSON.stringify('the body'),
headers: { 'Content-Type': 'application/json' }
};
assert.equal(parse_body(msg), 'the body');
});

test.run();

0 comments on commit 2ee331d

Please sign in to comment.