writer.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // headerwriter writes response HTTP headers
  2. package headerwriter
  3. import (
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go.withmatt.com/httpheaders"
  10. )
  11. const (
  12. // Content-Disposition header format
  13. contentDispositionFmt = "%s; filename=\"%s%s\""
  14. )
  15. // Writer is a struct that builds HTTP response headers.
  16. type Writer struct {
  17. config *Config
  18. originalResponseHeaders http.Header // Original response headers
  19. res http.Header // Headers to be written to the response
  20. maxAge int // Current max age for Cache-Control header
  21. url string // URL of the request, used for canonical header
  22. }
  23. // newWriter creates a new HeaderBuilder instance with the provided origin headers and URL
  24. func newWriter(config *Config, originalResponseHeaders http.Header, url string) *Writer {
  25. return &Writer{
  26. config: config,
  27. originalResponseHeaders: originalResponseHeaders,
  28. url: url,
  29. res: make(http.Header),
  30. maxAge: -1,
  31. }
  32. }
  33. // SetMaxAge sets the max-age for the Cache-Control header.
  34. // Overrides any existing max-age value.
  35. func (w *Writer) SetMaxAge(maxAge int) {
  36. if maxAge > 0 {
  37. w.maxAge = maxAge
  38. }
  39. }
  40. // SetIsFallbackImage sets the Fallback-Image header to
  41. // indicate that the fallback image was used.
  42. func (w *Writer) SetIsFallbackImage() {
  43. w.res.Set("Fallback-Image", "1")
  44. }
  45. // SetMaxAgeTime sets the max-age for the Cache-Control header based
  46. // on the time provided. If time provided is in the past compared
  47. // to the current maxAge value, it will correct maxAge.
  48. func (w *Writer) SetMaxAgeFromExpires(expires *time.Time) {
  49. if expires == nil {
  50. return
  51. }
  52. // Convert current maxAge to time
  53. currentMaxAgeTime := time.Now().Add(time.Duration(w.maxAge) * time.Second)
  54. // If the expires time is in the past compared to the current maxAge time,
  55. // or if maxAge is not set, we will use the expires time to set the maxAge.
  56. if w.maxAge < 0 || expires.Before(currentMaxAgeTime) {
  57. // Get the TTL from the expires time (must not be in the past)
  58. expiresTTL := max(0, int(time.Until(*expires).Seconds()))
  59. if expiresTTL > 0 {
  60. w.maxAge = expiresTTL
  61. }
  62. }
  63. }
  64. // SetLastModified sets the Last-Modified header from request
  65. func (w *Writer) SetLastModified() {
  66. if !w.config.LastModifiedEnabled {
  67. return
  68. }
  69. val := w.originalResponseHeaders.Get(httpheaders.LastModified)
  70. if val == "" {
  71. return
  72. }
  73. w.res.Set(httpheaders.LastModified, val)
  74. }
  75. // SetVary sets the Vary header
  76. func (w *Writer) SetVary() {
  77. vary := make([]string, 0)
  78. if w.config.SetVaryAccept {
  79. vary = append(vary, "Accept")
  80. }
  81. if w.config.EnableClientHints {
  82. vary = append(vary, "Sec-CH-DPR", "DPR", "Sec-CH-Width", "Width")
  83. }
  84. varyValue := strings.Join(vary, ", ")
  85. if varyValue != "" {
  86. w.res.Set(httpheaders.Vary, varyValue)
  87. }
  88. }
  89. // Copy copies specified headers from the original response headers to the response headers.
  90. func (w *Writer) Copy(only []string) {
  91. for _, key := range only {
  92. values := w.originalResponseHeaders.Values(key)
  93. for _, value := range values {
  94. w.res.Add(key, value)
  95. }
  96. }
  97. }
  98. // CopyFrom copies specified headers from the headers object. Please note that
  99. // all the past operations may overwrite those values.
  100. func (w *Writer) CopyFrom(headers http.Header, only []string) {
  101. for _, key := range only {
  102. values := headers.Values(key)
  103. for _, value := range values {
  104. w.res.Add(key, value)
  105. }
  106. }
  107. }
  108. // SetContentLength sets the Content-Length header
  109. func (w *Writer) SetContentLength(contentLength int) {
  110. if contentLength > 0 {
  111. w.res.Set(httpheaders.ContentLength, strconv.Itoa(contentLength))
  112. }
  113. }
  114. // SetContentDisposition sets the Content-Disposition header
  115. func (w *Writer) SetContentDisposition(filename, ext string, returnAttachment bool) {
  116. disposition := "inline"
  117. if returnAttachment {
  118. disposition = "attachment"
  119. }
  120. value := fmt.Sprintf(contentDispositionFmt, disposition, strings.ReplaceAll(filename, `"`, "%22"), ext)
  121. w.res.Set(httpheaders.ContentDisposition, value)
  122. }
  123. func (w *Writer) SetContentType(mime string) {
  124. w.res.Set(httpheaders.ContentType, mime)
  125. }
  126. // writeCanonical sets the Link header with the canonical URL.
  127. // It is mandatory for any response if enabled in the configuration.
  128. func (b *Writer) SetCanonical() {
  129. if !b.config.SetCanonicalHeader {
  130. return
  131. }
  132. if strings.HasPrefix(b.url, "https://") || strings.HasPrefix(b.url, "http://") {
  133. value := fmt.Sprintf(`<%s>; rel="canonical"`, b.url)
  134. b.res.Set(httpheaders.Link, value)
  135. }
  136. }
  137. // setCacheControlNoCache sets the Cache-Control header to no-cache (default).
  138. func (w *Writer) setCacheControlNoCache() {
  139. w.res.Set(httpheaders.CacheControl, "no-cache")
  140. }
  141. // setCacheControlMaxAge sets the Cache-Control header with max-age.
  142. func (w *Writer) setCacheControlMaxAge() {
  143. maxAge := w.maxAge
  144. if maxAge <= 0 {
  145. maxAge = w.config.DefaultTTL
  146. }
  147. if maxAge > 0 {
  148. w.res.Set(httpheaders.CacheControl, fmt.Sprintf("max-age=%d, public", maxAge))
  149. }
  150. }
  151. // setCacheControlPassthrough sets the Cache-Control header from the request
  152. // if passthrough is enabled in the configuration.
  153. func (w *Writer) setCacheControlPassthrough() bool {
  154. if !w.config.CacheControlPassthrough || w.maxAge > 0 {
  155. return false
  156. }
  157. if val := w.originalResponseHeaders.Get(httpheaders.CacheControl); val != "" {
  158. w.res.Set(httpheaders.CacheControl, val)
  159. return true
  160. }
  161. if val := w.originalResponseHeaders.Get(httpheaders.Expires); val != "" {
  162. if t, err := time.Parse(http.TimeFormat, val); err == nil {
  163. w.maxAge = max(0, int(time.Until(t).Seconds()))
  164. }
  165. }
  166. return false
  167. }
  168. // setCSP sets the Content-Security-Policy header to prevent script execution.
  169. func (w *Writer) setCSP() {
  170. w.res.Set("Content-Security-Policy", "script-src 'none'")
  171. }
  172. // Write writes the headers to the response writer
  173. func (w *Writer) Write(rw http.ResponseWriter) {
  174. w.setCacheControlNoCache()
  175. if !w.setCacheControlPassthrough() {
  176. w.setCacheControlMaxAge()
  177. }
  178. w.setCSP()
  179. for key, values := range w.res {
  180. for _, value := range values {
  181. rw.Header().Add(key, value)
  182. }
  183. }
  184. }
  185. // NOTE: WIP
  186. // func (w *HeaderBuilder) SetDebugHeaders() {
  187. // if config.EnableDebugHeaders {
  188. // rw.Header().Set("X-Origin-Content-Length", strconv.Itoa(len(originData.Data)))
  189. // rw.Header().Set("X-Origin-Width", resultData.Headers["X-Origin-Width"])
  190. // rw.Header().Set("X-Origin-Height", resultData.Headers["X-Origin-Height"])
  191. // rw.Header().Set("X-Result-Width", resultData.Headers["X-Result-Width"])
  192. // rw.Header().Set("X-Result-Height", resultData.Headers["X-Result-Height"])
  193. // }
  194. // }