Golang中*http.Request.RequestURI和*http.Request.URL.Path区别和使用

665 阅读2分钟

这是net/http包中Request类型的两个成员。
先说结论:

  1. *http.Request.RequestURI是请求时发送的请求行的URI,也就是raw URL(你发送请求时候写的是什么样子就是什么样子),并且会包含查询参数
  2. http.Request.URL.Path是对RequestURI经过还原转义(unescape)后的形式(URL的setPath方法处理,传给setPath的参数p就是RequestURI去掉?及后面的查询参数后的部分),http.Request.URL.Path不会包含查询参数: 根本原因是Go通过unescape函数自动处理了URL里已经转义的字符,例如将URL里的查询参数中的+号unescape成空格,路径或者参数中的%XX也unescape成对应的字符
    Go automatically processes escaped characters in a URL, converting + to a space and %XX to the corresponding character.
    源码片段如下:
// unescape unescapes a string; the mode specifies
// which section of the URL string is being unescaped.
func unescape(s string, mode encoding) (string, error) {
    // Count %, check that they're well-formed.
    n := 0
    hasPlus := false
    for i := 0; i < len(s); {
        switch s[i] {
            case '%':
            n++
            if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
                s = s[i:]
                if len(s) > 3 {
                    s = s[:3]
                }
                return "", EscapeError(s)
            }
            // Per https://tools.ietf.org/html/rfc3986#page-21
            // in the host component %-encoding can only be used
            // for non-ASCII bytes.
            // But https://tools.ietf.org/html/rfc6874#section-2
            // introduces %25 being allowed to escape a percent sign
            // in IPv6 scoped-address literals. Yay.
            if mode == encodeHost && unhex(s[i+1]) < 8 && s[i:i+3] != "%25" {
                return "", EscapeError(s[i : i+3])
            }
            if mode == encodeZone {
                // RFC 6874 says basically "anything goes" for zone identifiers
                // and that even non-ASCII can be redundantly escaped,
                // but it seems prudent to restrict %-escaped bytes here to those
                // that are valid host name bytes in their unescaped form.
                // That is, you can use escaping in the zone identifier but not
                // to introduce bytes you couldn't just write directly.
                // But Windows puts spaces here! Yay.
                v := unhex(s[i+1])<<4 | unhex(s[i+2])
                if s[i:i+3] != "%25" && v != ' ' && shouldEscape(v, encodeHost) {
                    return "", EscapeError(s[i : i+3])
                }
            }
            i += 3
            case '+':
            hasPlus = mode == encodeQueryComponent
            i++
            default:
            if (mode == encodeHost || mode == encodeZone) && s[i] < 0x80 && shouldEscape(s[i], mode) {
                return "", InvalidHostError(s[i : i+1])
            }
            i++
        }
    }
    if n == 0 && !hasPlus {
        return s, nil
    }
    var t strings.Builder
    t.Grow(len(s) - 2*n)
    for i := 0; i < len(s); i++ {
        switch s[i] {
            case '%':
            t.WriteByte(unhex(s[i+1])<<4 | unhex(s[i+2]))
            i += 2
            case '+':
            if mode == encodeQueryComponent {
                t.WriteByte(' ')
            } else {
                t.WriteByte('+')
            }
            default:
            t.WriteByte(s[i])
        }
    }
    return t.String(), nil
} 

// setPath sets the Path and RawPath fields of the URL based on the provided
// escaped path p. It maintains the invariant that RawPath is only specified
// when it differs from the default encoding of the path.
// For example:
// - setPath("/foo/bar")   will set Path="/foo/bar" and RawPath=""
// - setPath("/foo%2fbar") will set Path="/foo/bar" and RawPath="/foo%2fbar"
// setPath will return an error only if the provided path contains an invalid
// escaping.
func (u *URL) setPath(p string) error {
    path, err := unescape(p, encodePath)
    if err != nil {
        return err
    }
    u.Path = path
    if escp := escape(path, encodePath); p == escp {
        // Default encoding is fine.
        u.RawPath = ""
    } else {
        u.RawPath = p
    }
    return nil
}

测试:

package main

import (
    "fmt"
    "net/http"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Println("RequestURI:", r.RequestURI)
    fmt.Println("Path:", r.URL.Path)
    fmt.Println("RawQuery:", r.URL.RawQuery)
    fmt.Println("Query:", r.URL.Query())
    w.Write([]byte("Hello Mapmost\n"))
}

type apiHandler struct{}

func (ah apiHandler) ServeHTTP(w http.ResponseWriter, r http.Request) {
    w.Write([]byte("Hello Mapmost API\n"))
}

func main() {
    http.HandleFunc("/", indexHandler)
    h := &apiHandler{}
    http.Handle("/api", h)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        fmt.Println(err)
    }
}