使用Indy Tidhttpserver:ServeFile()时如何更改Charset?

发布于 2025-01-31 12:22:47 字数 566 浏览 2 评论 0原文

我的JavaScript,HMTL等文件正在使用charset ISO-8859-1提供服务,但是我需要拥有charset = utf-8。我已经尝试使用aresponseinfo-> charset =“ utf-8”,但似乎没有区别。

我已经完成了此操作,但我宁愿不重复所有可能的文件扩展名测试。

  AResponseInfo->ContentDisposition = "inline";
  if (paths::getExtension(requestedFile, true) == ".JS")
    AResponseInfo->ContentType = "text/javascript; charset=utf-8";

  AResponseInfo->SmartServeFile(AContext, ARequestInfo,uString(requestedFile)); 

(旁注:这可能是因为我正在使用Radstudio 2010年的Indy10版本。升级Indy的最简单方法是什么?)

My Javascript, HMTL etc files are being served using charset ISO-8859-1, but I need then to have charset=utf-8. I've tried using AResponseInfo->CharSet = "utf-8" but it seems to make no difference.

I've done this, which works, but I'd rather not repeat the test for every possible file extension.

  AResponseInfo->ContentDisposition = "inline";
  if (paths::getExtension(requestedFile, true) == ".JS")
    AResponseInfo->ContentType = "text/javascript; charset=utf-8";

  AResponseInfo->SmartServeFile(AContext, ARequestInfo,uString(requestedFile)); 

(side note: This may be because I'm using a version of Indy10 that's a few years old with RadStudio 2010. What's the easiest way to upgrade Indy?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2025-02-07 12:22:47

旁注:这可能是因为我正在使用Radstudio 2010的Indy10版本。

这已经超过了“几年”,这已经超过了十年了。 RS2010于2009年发布。从那时起,已经进行了许多更新tidhttpserver,包括2010年,2012年和2019年与Charset相关的几次更改。

升级印地的最简单方法是什么?

最新的源代码在github上: https://github.com/indysockets/indy/indy/

a href =“ https://web.archive.org/web/20200925081341/http://wwww2.indyproject.org/sockets/sockets/docs/indy10installation.en.aspx” /a>
(注意:链接来自Archive.org,因此是由于 Indyproject.org 的当前问题)

一旦您升级到最新的Indy,aresponseinfo-> charset =“ utf-8”应该按预期工作。

(smart)Servefile()设置aresponseinfo-> contentType属性仅在空白时属性:

  • 如果新的content> content> contentType value指定指定charsetaresponseinfo-> charset设置为该值。

  • 否则,如果新的contentType值不包括charset,而aresponseinfo-&gt; charset是空白的,而新<代码> contentType 值是text /....媒体类型,然后aresponseinfo-&gt; charset设置为默认值,要么us -ascii for XML类型,否则ISO-8859-1


否则,AresponseInfo-&gt; contentTypearesponseinfo-&gt; charset未触及,保留在(smart)servefile()servefile()被称为。

aresponseinfo-&gt; writeheader()被调用,如果aresponseinfo-&gt; contentType仍然空白,并且arpsenseinfo-&gt;代码> aresponseInfo-&gt; contentsTream是分配的数据,然后aresponseinfo-&gt; contentType设置为text/html,以及arespsense> arespsenseinfoinfo--&gt; charset设置为utf-8(rs2009+)或iso-8859-1(pre-rs2009),如果它为空白。

因此,如果要在content-type标题中确保特定的charset,则应该能够明确设置aresponseinfo-&gt; charset无论您想要什么,独立于aresponseinfo-&gt; contentType设置为,tidhttpserver现在将在可能的情况下保留预先存在的charset值。 。

side note: This may be because I'm using a version of Indy10 that's a few years old with RadStudio 2010.

That is more than "a few" years old, that is over a decade old. RS2010 was released in 2009. There have been many updates to TIdHTTPServer since that time, including several charset-related changes in 2010, 2012, and 2019.

What's the easiest way to upgrade Indy?

The latest source code is on GitHub: https://github.com/IndySockets/Indy/

Indy 10 Installation Instructions
(note: link is from archive.org due to current problems with indyproject.org)

Once you have upgraded to the latest Indy, AResponseInfo->CharSet = "utf-8" should work as expected.

(Smart)ServeFile() sets the AResponseInfo->ContentType property only if it is blank:

  • If the new ContentType value specifies a charset, AResponseInfo->CharSet is set to that value.

  • Otherwise, if the new ContentType value does not include a charset, AND AResponseInfo->CharSet is blank, AND the new ContentType value is a text/... media type, then AResponseInfo->CharSet is set to a default value, either us-ascii for XML types, otherwise ISO-8859-1.

Otherwise, AResponseInfo->ContentType and AResponseInfo->CharSet are left untouched, preserving whatever values they were holding before (Smart)ServeFile() was called.

When AResponseInfo->WriteHeader() is called, if AResponseInfo->ContentType is still blank, AND AResponseInfo->ContentText or AResponseInfo->ContentStream is assigned data, then AResponseInfo->ContentType is set to text/html, and AResponseInfo->CharSet is set to utf-8 (RS2009+) or ISO-8859-1 (pre-RS2009) if it is blank.

So, if you want to ensure a particular charset in the Content-Type header, you should be able to explicitly set AResponseInfo->CharSet to whatever you want, independently of what AResponseInfo->ContentType is set to, TIdHTTPServer will now preserve a pre-existing CharSet value whenever possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文