duanchi0883649 2015-08-30 21:46
浏览 330
已采纳

在golang HTTP FileServer的Content-Type标头上设置'charset'属性

I've got an issue when testing a golang web app. In the deployed version, nginx fronts the application and explicitly sets charset utf8; so that all textual content types are appended with a charset declaration.

In testing, I'm hitting the golang application directly, and here the content type does not have a charset set. This is causing problems when trying to serve libraries like d3 which has lines like:

var ε = 1e-6, ε2 = ε * ε, π = Math.PI, τ = 2 * π, τε = τ - ε, halfπ = π / 2, d3_radians = π / 180, d3_degrees = 180 / π;

Because golang doesn't specify the charset, these are rendered in chrome as:

var ε = 1e-6, ε2 = ε * ε....

What's the best way of getting the golang http server to output the charset=utf8 declaration on the HTTP headers?

  • 写回答

1条回答 默认 最新

  • dongren7374 2015-08-31 04:19
    关注

    FileServer calls mime.TypeByExtension to get the content type. If no type is registered for the extension, then FileServer calls http.DetectContentType to guess the type from the contents of the file.

    If a text/ content type does not specify a charset parameter, then the mime package automatically adds the 'charset=utf-8' parameter to the content type.

    To debug this problem, check to see if the system mime.types file specifies a content type for the file extension and that the charset parameter is'charset=utf-8' or the charset parameter is not set.

    If the system mime.types entry is missing or incorrect, then call mime.AddExtensionType to override the system setting. This can be called once before any content is served.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助