zoukankan      html  css  js  c++  java
  • http.StripPrefix 的参数含义

    看下面两行代码:

    http.Handle("/file/", http.StripPrefix("/file", http.FileServer(http.Dir("./output/"))))
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./output/"))))
    http.Handle("/file1", http.StripPrefix("/file1", http.FileServer(http.Dir("./output/"))))
    http.Handle("/file2", http.StripPrefix("/file2/", http.FileServer(http.Dir("./output/"))))

    注意看下图结果:

    • 1、2 行链接地址是带对应前缀的。
    • 3行链接地址不带
    • 4直接 404

     

    image

    image

     

    image

    image

     

    TrimPrefix

    TrimPrefix returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.

    StripPrefix

    // StripPrefix returns a handler that serves HTTP requests
    // by removing the given prefix from the request URL's Path
    // and invoking the handler h. StripPrefix handles a
    // request for a path that doesn't begin with prefix by
    // replying with an HTTP 404 not found error.
    func StripPrefix(prefix string, h Handler) Handler {
        if prefix == "" {
            return h
        }
        return HandlerFunc(func(w ResponseWriter, r *Request) {
            if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {
                r.URL.Path = p
                h.ServeHTTP(w, r)
            } else {
                NotFound(w, r)
            }
        })
    }
    如上图代码所示,
    404 是触发了上面的 else 条件。
  • 相关阅读:
    BZOJ1430小猴打架——prufer序列
    [集训队作业2018]蜀道难——TopTree+贪心+树链剖分+链分治+树形DP
    BZOJ5063旅游——非旋转treap
    bzoj 4570 妖怪
    Luogu 1452 Beauty Contest
    bzoj 1337 最小圆覆盖
    bzoj 1007 水平可见直线
    Luogu 4724 三维凸包
    bzoj 4827 礼物
    hdu 4348 To the moon
  • 原文地址:https://www.cnblogs.com/ghj1976/p/5102556.html
Copyright © 2011-2022 走看看