response.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/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.commonHeaders()
  42. r.writer.WriteHeader(http.StatusNotModified)
  43. }
  44. // Cache returns a response with caching headers.
  45. func (r *Response) Cache(mimeType, etag string, content []byte, duration time.Duration) {
  46. r.writer.Header().Set("Content-Type", mimeType)
  47. r.writer.Header().Set("ETag", etag)
  48. r.writer.Header().Set("Cache-Control", "public")
  49. r.writer.Header().Set("Expires", time.Now().Add(duration).Format(time.RFC1123))
  50. if etag == r.request.Header.Get("If-None-Match") {
  51. r.writer.WriteHeader(http.StatusNotModified)
  52. } else {
  53. r.writer.Write(content)
  54. }
  55. }
  56. func (r *Response) commonHeaders() {
  57. r.writer.Header().Set("X-XSS-Protection", "1; mode=block")
  58. r.writer.Header().Set("X-Content-Type-Options", "nosniff")
  59. r.writer.Header().Set("X-Frame-Options", "DENY")
  60. // Even if the directive "frame-src" has been deprecated in Firefox,
  61. // we keep it to stay compatible with other browsers.
  62. r.writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src *; media-src *; frame-src *; child-src *")
  63. }
  64. // NewResponse returns a new Response.
  65. func NewResponse(w http.ResponseWriter, r *http.Request, template *template.Engine) *Response {
  66. return &Response{writer: w, request: r, template: template}
  67. }