duanqiao8925 2015-03-25 07:37
浏览 405
已采纳

Go URL中的多余转义字符

I have the following snippet of code :

    u := *baseURL
    u.User = nil 
    if q := strings.Index(path, "?"); q > 0 { 
            u.Path = path[:q]
            u.RawQuery = path[q+1:]
    } else {
            u.Path = path
    }   

    log.Printf(" url %v, u.String())

I see that when the baseurl is set to something like this http://localhost:9000/buckets/test%?bucket_uuid=7864b0dcdf0a578bd0012c70aef58aca the url package seems to add an extra escape character near the % sign. For e.g. the output of the above print statement is the following :

2015/03/25 12:02:49  url http://localhost:9000/pools/default/buckets/test%2525?bucket_uuid=7864b0dcdf0a578bd0012c70aef58aca

This seems to only happen when the RawQuery field of the URL is set. Any idea why this is happening ? I'm using go version 1.3.3

Cheers, Manik

  • 写回答

1条回答 默认 最新

  • dongpin2969 2015-03-25 09:27
    关注

    URLs may only contain characters of the ASCII character set, but it is often intended to include/transfer characters outside of this ASCII set. In such cases the URL has to be converted into a valid ASCII format.

    If the raw URL contains characters outside of the allowed set, they are escaped: they are replaced with a '%' followed by two hexadecimal digits. Therefore the character '%' is special and also has to be escaped (and its escaped form will start with '%' as well, and its hexadecimal code is 25).

    Since your raw URL contains the character '%', it will be replaced by "%25".

    Back to your example: in the printed form you see "%2525". You could ask why not just "%25"?

    This is because your original url contains a '%' in its escaped form which means its raw form contains the escape sequence "%25". If you use/interpret this as raw input, the '%' will be replaced by "%25" which will be followed by the "25" from the input hence resulting in "%2525".

    See: HTML URL Encoding Reference

    Also: RFC 1738 - Uniform Resource Locators (URL)

    And also: RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改