AWS弹性Beanstalk环境变量的编码和字符集

huangapple go评论70阅读模式
英文:

AWS Elastic Beanstalk environment variable encoding and charset

问题

我在AWS Elastic Beanstalk上部署了一个简单的Java Web应用程序,作为一个自包含的JAR文件。它从环境变量中获取值,将它们显示在网页上作为HTML内容。简化了一下这个应用程序,如果我将环境变量PAGE_TEST = "foo\nbar",然后当我访问页面/test时,我会看到以下内容,因为我的应用程序会将这些行解析为段落:

<p>foo</p>
<p>bar</p>

编码问题

第一个问题是,在Elastic Beanstalk中,将环境变量PAGE_TEST设置为foo\nbar是无效的!在测试页面上,我得到了这个:

<p>foonbar</p>

显然,AWS Elastic Beanstalk正在尝试解释我的反斜杠字符。为什么?如何关闭这个功能?我希望它只使用我提供的确切值!是谁告诉它尝试将这些值作为转义码进行评估的?

我可以通过将反斜杠转义为foo\\nbar来解决这个问题,但我不应该这样做!

字符集问题

但现在出现了一个更严重的问题。我将环境变量PAGE_TEST设置为dan&#231;a,这是一个完全合法的Unicode字符串,葡萄牙语中表示“舞蹈”。不幸的是,Elastic Beanstalk将dan?a传递给我的应用程序。

<p>dan?a</p>

根据经验,我猜测Elastic Beanstalk试图将该值解释为ASCII,并且它正在使用一个字符集解码器,简单地用?替换任何非ASCII字符。或者其他什么原因。但它为什么要这么做呢?现在是2020年了,使用Unicode有什么问题吗?

有人可以帮助我告诉AWS Elastic Beanstalk停止处理我完全有效的环境变量,并且只是接受我提供的值吗?我会非常感谢。

英文:

I have a simple Java web application deployed on AWS Elastic Beanstalk as a self-contained JAR file. It pulls values from environment variables to display them in web pages as HTML. Simplifying the application a bit, if I assign the environment variable PAGE_TEST = &quot;foo\nbar&quot;, then when I go to the page /test then I will see the following, because my application parses out the lines into paragraphs

&lt;p&gt;foo&lt;/p&gt;
&lt;p&gt;bar&lt;/p&gt;

Encoding Problem

The first problem is that in Elastic Beanstalk, setting the environment variable PAGE_TEST to foo\nbar doesn't work! On the test page I get this:

&lt;p&gt;foonbar&lt;/p&gt;

Apparently AWS Elastic Beanstalk is trying to interpret my backslash characters. Why? How can I turn this off? I want it just to use the exact values I give it! Who told it to try to evaluate the values as escape codes?

I can work around this by escaping the backslash as foo\\nbar, but I shouldn't have to!

Charset Problem

But now there is a worse problem. I set the environment variable PAGE_TEST to dan&#231;a, which is a perfectly legitimate Unicode string—the Portuguese word for "dance". Unfortunately Elastic Beanstalk passes dan?a to my application.

&lt;p&gt;dan?a&lt;/p&gt;

From experience I might guess that Elastic Beanstalk is trying to interpret the value as ASCII, and it is using a charset decoder that simply substitutes any non-ASCII character with ?. Or something. By why would it do that? It's 2020—what's wrong with just using Unicode?

Can someone help me tell AWS Elastic Beanstalk to stop mucking around with my perfectly valid environment variables and simply accept them as I provide them? I would be grateful.

答案1

得分: 8

我在Elastic Beanstalk配置文档中找到了一些内容,它表示环境变量只能使用包含字符_ . : / = + \ - @ &#39; &quot;的值。天哪!而且在验证设置时它甚至没有向我警示这个问题,只是漠然地用?替换了它不喜欢的字符。

我仍然不明白为什么它将\字符视为转义字符并将其删除。

无论如何,这彻底破坏了一个在本地正常运行的应用程序。看起来我需要重新设计这个功能。也许我只能将我想要的字符串的UTF-8字节进行Base64编码,然后放入环境变量值中。虽然这看起来好像是不必要的。

英文:

I found some Elastic Beanstalk configuration documentation saying that environment variables can only use values with the characters _ . : / = + \ - @ &#39; &quot;. Sheesh! And it didn't even alert me to the problem during validation of the settings, and instead just blithely replaced characters it didn't like with ?.

I still don't know why it considers the \ character an escape character and removes it.

In any case this completely breaks an application that was working fine locally. It looks like I'll have to redesign the functionality. Maybe I'll have to just Base64-encode the UTF-8 bytes of the string I want into the environment variable value. It seems so needless, though.

huangapple
  • 本文由 发表于 2020年10月25日 07:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64519029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定