在golang中错误地使用标点符号解析帖子数据

时间:2016-10-15 03:51:48

标签: c post go

我知道如何在golang中解析帖子数据

r.ParseForm()
pid := r.PostFormValue("pid")
code := r.PostFormValue("code")
lang := r.PostFormValue("lang")
author := r.PostFormValue("author")

但是帖子数据是pid=1&code=#include <stdio.h>\x0Aint main()\x0A{\x0A\x09printf(\x223\x5Cn\x22);\x0A\x09return 0;\x0A}&lang=c&author=11(这是从nginx日志中获得的)

因此,当我解析数据时,可能是错误的。 code的解析数据是

#include <stdio.h>
int main()
{
    printf("3\n")

而不是

#include <stdio.h>
int main()
{
    printf("3\n");
    return 0;
}

那么我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您传递的原始代码可能不安全,问题是因为:

https://golang.org/src/net/url/url.go?s=21047:21092#L761

enter image description here

我建议您对日志代码进行base64encode编码,然后在处理程序中对其进行解码。

import "encoding/base64"

...

code, err := base64.RawURLEncoding.DecodeString(r.PostFormValue("code"))
    if err != nil {
        // handle error
    }

然后您的请求应如下所示:

curl --data "pid=1&code=I2luY2x1ZGUgPHN0ZGlvLmg-XHgwQWludCBtYWluKClceDBBe1x4MEFceDA5cHJpbnRmKFx4MjIzXHg1Q25ceDIyKTtceDBBXHgwOXJldHVybiAwO1x4MEF9&lang=c&author=11" http://localhost:8080