forgejo-federation/vendor/github.com/golang/gddo/httputil/buster.go
PhilippHomann 684b7a999f
Dump: add output format tar and output to stdout (#10376)
* Dump: Use mholt/archive/v3 to support tar including many compressions

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: Allow dump output to stdout

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: Fixed bug present since #6677 where SessionConfig.Provider is never "file"

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: never pack RepoRootPath, LFS.ContentPath and LogRootPath when they are below AppDataPath

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: also dump LFS (fixes #10058)

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Dump: never dump CustomPath if CustomPath is a subdir of or equal to AppDataPath (fixes #10365)

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* Use log.Info instead of fmt.Fprintf

Signed-off-by: Philipp Homann <homann.philipp@googlemail.com>

* import ordering

* make fmt

Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Matti R <matti@mdranta.net>
2020-06-05 16:47:39 -04:00

95 lines
2 KiB
Go

// Copyright 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.
package httputil
import (
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
)
type busterWriter struct {
headerMap http.Header
status int
io.Writer
}
func (bw *busterWriter) Header() http.Header {
return bw.headerMap
}
func (bw *busterWriter) WriteHeader(status int) {
bw.status = status
}
// CacheBusters maintains a cache of cache busting tokens for static resources served by Handler.
type CacheBusters struct {
Handler http.Handler
mu sync.Mutex
tokens map[string]string
}
func sanitizeTokenRune(r rune) rune {
if r <= ' ' || r >= 127 {
return -1
}
// Convert percent encoding reserved characters to '-'.
if strings.ContainsRune("!#$&'()*+,/:;=?@[]", r) {
return '-'
}
return r
}
// Get returns the cache busting token for path. If the token is not already
// cached, Get issues a HEAD request on handler and uses the response ETag and
// Last-Modified headers to compute a token.
func (cb *CacheBusters) Get(path string) string {
cb.mu.Lock()
if cb.tokens == nil {
cb.tokens = make(map[string]string)
}
token, ok := cb.tokens[path]
cb.mu.Unlock()
if ok {
return token
}
w := busterWriter{
Writer: ioutil.Discard,
headerMap: make(http.Header),
}
r := &http.Request{URL: &url.URL{Path: path}, Method: "HEAD"}
cb.Handler.ServeHTTP(&w, r)
if w.status == 200 {
token = w.headerMap.Get("Etag")
if token == "" {
token = w.headerMap.Get("Last-Modified")
}
token = strings.Trim(token, `" `)
token = strings.Map(sanitizeTokenRune, token)
}
cb.mu.Lock()
cb.tokens[path] = token
cb.mu.Unlock()
return token
}
// AppendQueryParam appends the token as a query parameter to path.
func (cb *CacheBusters) AppendQueryParam(path string, name string) string {
token := cb.Get(path)
if token == "" {
return path
}
return path + "?" + name + "=" + token
}