You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

96 lines
2.0 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package httputil
  7. import (
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "strings"
  13. "sync"
  14. )
  15. type busterWriter struct {
  16. headerMap http.Header
  17. status int
  18. io.Writer
  19. }
  20. func (bw *busterWriter) Header() http.Header {
  21. return bw.headerMap
  22. }
  23. func (bw *busterWriter) WriteHeader(status int) {
  24. bw.status = status
  25. }
  26. // CacheBusters maintains a cache of cache busting tokens for static resources served by Handler.
  27. type CacheBusters struct {
  28. Handler http.Handler
  29. mu sync.Mutex
  30. tokens map[string]string
  31. }
  32. func sanitizeTokenRune(r rune) rune {
  33. if r <= ' ' || r >= 127 {
  34. return -1
  35. }
  36. // Convert percent encoding reserved characters to '-'.
  37. if strings.ContainsRune("!#$&'()*+,/:;=?@[]", r) {
  38. return '-'
  39. }
  40. return r
  41. }
  42. // Get returns the cache busting token for path. If the token is not already
  43. // cached, Get issues a HEAD request on handler and uses the response ETag and
  44. // Last-Modified headers to compute a token.
  45. func (cb *CacheBusters) Get(path string) string {
  46. cb.mu.Lock()
  47. if cb.tokens == nil {
  48. cb.tokens = make(map[string]string)
  49. }
  50. token, ok := cb.tokens[path]
  51. cb.mu.Unlock()
  52. if ok {
  53. return token
  54. }
  55. w := busterWriter{
  56. Writer: ioutil.Discard,
  57. headerMap: make(http.Header),
  58. }
  59. r := &http.Request{URL: &url.URL{Path: path}, Method: "HEAD"}
  60. cb.Handler.ServeHTTP(&w, r)
  61. if w.status == 200 {
  62. token = w.headerMap.Get("Etag")
  63. if token == "" {
  64. token = w.headerMap.Get("Last-Modified")
  65. }
  66. token = strings.Trim(token, `" `)
  67. token = strings.Map(sanitizeTokenRune, token)
  68. }
  69. cb.mu.Lock()
  70. cb.tokens[path] = token
  71. cb.mu.Unlock()
  72. return token
  73. }
  74. // AppendQueryParam appends the token as a query parameter to path.
  75. func (cb *CacheBusters) AppendQueryParam(path string, name string) string {
  76. token := cb.Get(path)
  77. if token == "" {
  78. return path
  79. }
  80. return path + "?" + name + "=" + token
  81. }