response.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package handler
  5. import (
  6. "net/http"
  7. "time"
  8. "github.com/miniflux/miniflux/config"
  9. "github.com/miniflux/miniflux/template"
  10. )
  11. // Response handles HTTP responses.
  12. type Response struct {
  13. cfg *config.Config
  14. writer http.ResponseWriter
  15. request *http.Request
  16. template *template.Engine
  17. }
  18. // SetCookie send a cookie to the client.
  19. func (r *Response) SetCookie(cookie *http.Cookie) {
  20. http.SetCookie(r.writer, cookie)
  21. }
  22. // JSON returns a JSONResponse.
  23. func (r *Response) JSON() *JSONResponse {
  24. r.commonHeaders()
  25. return NewJSONResponse(r.writer, r.request)
  26. }
  27. // HTML returns a HTMLResponse.
  28. func (r *Response) HTML() *HTMLResponse {
  29. r.commonHeaders()
  30. return &HTMLResponse{writer: r.writer, request: r.request, template: r.template}
  31. }
  32. // XML returns a XMLResponse.
  33. func (r *Response) XML() *XMLResponse {
  34. r.commonHeaders()
  35. return &XMLResponse{writer: r.writer, request: r.request}
  36. }
  37. // Redirect redirects the user to another location.
  38. func (r *Response) Redirect(path string) {
  39. http.Redirect(r.writer, r.request, path, http.StatusFound)
  40. }
  41. // NotModified sends a response with a 304 status code.
  42. func (r *Response) NotModified() {
  43. r.commonHeaders()
  44. r.writer.WriteHeader(http.StatusNotModified)
  45. }
  46. // Cache returns a response with caching headers.
  47. func (r *Response) Cache(mimeType, etag string, content []byte, duration time.Duration) {
  48. r.writer.Header().Set("Content-Type", mimeType)
  49. r.writer.Header().Set("ETag", etag)
  50. r.writer.Header().Set("Cache-Control", "public")
  51. r.writer.Header().Set("Expires", time.Now().Add(duration).Format(time.RFC1123))
  52. if etag == r.request.Header.Get("If-None-Match") {
  53. r.writer.WriteHeader(http.StatusNotModified)
  54. } else {
  55. r.writer.Write(content)
  56. }
  57. }
  58. func (r *Response) commonHeaders() {
  59. r.writer.Header().Set("X-XSS-Protection", "1; mode=block")
  60. r.writer.Header().Set("X-Content-Type-Options", "nosniff")
  61. r.writer.Header().Set("X-Frame-Options", "DENY")
  62. // Even if the directive "frame-src" has been deprecated in Firefox,
  63. // we keep it to stay compatible with other browsers.
  64. r.writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src *; media-src *; frame-src *; child-src *")
  65. if r.cfg.IsHTTPS && r.cfg.HasHSTS() {
  66. r.writer.Header().Set("Strict-Transport-Security", "max-age=31536000")
  67. }
  68. }
  69. // NewResponse returns a new Response.
  70. func NewResponse(cfg *config.Config, w http.ResponseWriter, r *http.Request, template *template.Engine) *Response {
  71. return &Response{cfg: cfg, writer: w, request: r, template: template}
  72. }