doutang1946 2016-10-15 03:51
浏览 78
已采纳

在golang中解析带有标点符号的发布数据不正确

I know how to parse post data in golang

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

But the post data is pid=1&code=#include <stdio.h>\x0Aint main()\x0A{\x0A\x09printf(\x223\x5Cn\x22);\x0A\x09return 0;\x0A}&lang=c&author=11(this is obtained from nginx log)

So when I parse the data, it could be wrong. The parsed data of code is

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

instead of

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

So how can I fix this problem?

  • 写回答

1条回答 默认 最新

  • douhai9043 2016-10-15 05:32
    关注

    You're passing the raw code, which could be unsafe, your problem is because of this:

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

    enter image description here

    I would suggest that you base64encode your log code and then decode it in your handler.

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

    Then your request should look like this:

    curl --data "pid=1&code=I2luY2x1ZGUgPHN0ZGlvLmg-XHgwQWludCBtYWluKClceDBBe1x4MEFceDA5cHJpbnRmKFx4MjIzXHg1Q25ceDIyKTtceDBBXHgwOXJldHVybiAwO1x4MEF9&lang=c&author=11" http://localhost:8080
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用