Skip to content

Support for parsing Content-Type with non-UTF-8 charset directive #95

@chalkpe

Description

@chalkpe

Support plan

  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: 17.9.0
  • module version: 7.0.3
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi application
  • any other relevant information:

What problem are you trying to solve?

Parser doesn't do anything with Content-Type header with charset directive. It only parses payload buffer by utf8, hardcoded. Only way to process non-UTF-8 payload is disable internal parser via route.options.payload.parse: false and decode raw buffer manually, losing all benefits from hapi framework.

import { Server } from '@hapi/hapi'
import { encode } from 'iconv-lite'

async function test(text, charset = 'euc-kr') {
  const server = new Server({})
  server.route({
    method: 'POST',
    path: '/',
    options: { handler: (req) => req.payload }
  })

  await server.start()
  const { payload } = await server.inject({
    method: 'POST',
    url: '/',
    payload: encode(text, charset),
    headers: { 'content-type': `application/json; charset=${charset}` }
  })

  await server.stop()
  return { expected: text, got: payload }
}

test('{"한글":"인코딩"}').then(console.log).catch(console.error)
// { expected: '{"한글":"인코딩"}', got '{"�ѱ�":"���ڵ�"}' }

Do you have a new or modified API suggestion to solve the problem?

  • Add support for payload decoding with charset directive, or create an option for custom payload decoder.
  • Current behaviour of internals.object is ignoring charset part of Content-Type header and stick with stringify payload buffer by utf8 encoding (text, json, form)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions