Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/milky/network/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MilkyHttpHandler {
// Access token middleware for API routes
if (this.config.accessToken) {
this.app.use(`${this.config.prefix}/api`, (req, res, next) => {
if (req.headers['content-type'] !== 'application/json') {
if (!req.headers['content-type']?.includes('application/json')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: 建议让 Content-Type 的检查对大小写不敏感,并且能正确处理数组形式的值。

req.headers['content-type'] 可能是 string | string[] | undefined,并且头名/头值的大小写都可能不同。先统一大小写并将其拍平成字符串,可以避免一些细微的不匹配,例如:

const contentType = req.headers['content-type'];
const contentTypeStr = Array.isArray(contentType) ? contentType.join(',') : contentType || '';

if (!contentTypeStr.toLowerCase().includes('application/json')) {
  // ...
}

这样可以正确处理诸如 application/json; charset=utf-8、大小写混合以及多值 header 等情况。

Original comment in English

suggestion: Consider making the Content-Type check case-insensitive and robust to array values.

req.headers['content-type'] can be string | string[] | undefined, and header names/values may vary in case. Normalizing and flattening first avoids subtle mismatches, e.g.:

const contentType = req.headers['content-type'];
const contentTypeStr = Array.isArray(contentType) ? contentType.join(',') : contentType || '';

if (!contentTypeStr.toLowerCase().includes('application/json')) {
  // ...
}

This will handle values like application/json; charset=utf-8, mixed casing, and multi-valued headers correctly.

this.ctx.logger.warn(
'MilkyHttp',
`${req.ip} -> ${req.path} (Content-Type not application/json)`
Expand Down