response.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 &JSONResponse{writer: r.writer, request: 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. }
  60. // NewResponse returns a new Response.
  61. func NewResponse(w http.ResponseWriter, r *http.Request, template *template.Engine) *Response {
  62. return &Response{writer: w, request: r, template: template}
  63. }