response.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 core
  5. import (
  6. "net/http"
  7. "time"
  8. "github.com/miniflux/miniflux2/server/template"
  9. )
  10. // Response handles HTTP responses.
  11. type Response struct {
  12. writer http.ResponseWriter
  13. request *http.Request
  14. template *template.Engine
  15. }
  16. // SetCookie send a cookie to the client.
  17. func (r *Response) SetCookie(cookie *http.Cookie) {
  18. http.SetCookie(r.writer, cookie)
  19. }
  20. // JSON returns a JSONResponse.
  21. func (r *Response) JSON() *JSONResponse {
  22. r.commonHeaders()
  23. return NewJSONResponse(r.writer, r.request)
  24. }
  25. // HTML returns a HTMLResponse.
  26. func (r *Response) HTML() *HTMLResponse {
  27. r.commonHeaders()
  28. return &HTMLResponse{writer: r.writer, request: r.request, template: r.template}
  29. }
  30. // XML returns a XMLResponse.
  31. func (r *Response) XML() *XMLResponse {
  32. r.commonHeaders()
  33. return &XMLResponse{writer: r.writer, request: r.request}
  34. }
  35. // Redirect redirects the user to another location.
  36. func (r *Response) Redirect(path string) {
  37. http.Redirect(r.writer, r.request, path, http.StatusFound)
  38. }
  39. // NotModified sends a response with a 304 status code.
  40. func (r *Response) NotModified() {
  41. r.writer.WriteHeader(http.StatusNotModified)
  42. }
  43. // Cache returns a response with caching headers.
  44. func (r *Response) Cache(mimeType, etag string, content []byte, duration time.Duration) {
  45. r.writer.Header().Set("Content-Type", mimeType)
  46. r.writer.Header().Set("Etag", etag)
  47. r.writer.Header().Set("Cache-Control", "public")
  48. r.writer.Header().Set("Expires", time.Now().Add(duration).Format(time.RFC1123))
  49. if etag == r.request.Header.Get("If-None-Match") {
  50. r.writer.WriteHeader(http.StatusNotModified)
  51. } else {
  52. r.writer.Write(content)
  53. }
  54. }
  55. func (r *Response) commonHeaders() {
  56. r.writer.Header().Set("X-XSS-Protection", "1; mode=block")
  57. r.writer.Header().Set("X-Content-Type-Options", "nosniff")
  58. r.writer.Header().Set("X-Frame-Options", "DENY")
  59. // Even if the directive "frame-src" has been deprecated in Firefox,
  60. // we keep it to stay compatible with other browsers.
  61. r.writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src *; media-src *; frame-src *; child-src *")
  62. }
  63. // NewResponse returns a new Response.
  64. func NewResponse(w http.ResponseWriter, r *http.Request, template *template.Engine) *Response {
  65. return &Response{writer: w, request: r, template: template}
  66. }