html.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2018 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 html // import "miniflux.app/http/response/html"
  5. import (
  6. "net/http"
  7. "miniflux.app/http/response"
  8. "miniflux.app/logger"
  9. )
  10. // OK creates a new HTML response with a 200 status code.
  11. func OK(w http.ResponseWriter, r *http.Request, body interface{}) {
  12. builder := response.New(w, r)
  13. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  14. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  15. builder.WithBody(body)
  16. builder.Write()
  17. }
  18. // ServerError sends an internal error to the client.
  19. func ServerError(w http.ResponseWriter, r *http.Request, err error) {
  20. logger.Error("[HTTP:Internal Server Error] %s => %v", r.URL, err)
  21. builder := response.New(w, r)
  22. builder.WithStatus(http.StatusInternalServerError)
  23. builder.WithHeader("Content-Security-Policy", `default-src 'self'`)
  24. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  25. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  26. builder.WithBody(err)
  27. builder.Write()
  28. }
  29. // BadRequest sends a bad request error to the client.
  30. func BadRequest(w http.ResponseWriter, r *http.Request, err error) {
  31. logger.Error("[HTTP:Bad Request] %s => %v", r.URL, err)
  32. builder := response.New(w, r)
  33. builder.WithStatus(http.StatusBadRequest)
  34. builder.WithHeader("Content-Security-Policy", `default-src 'self'`)
  35. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  36. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  37. builder.WithBody(err)
  38. builder.Write()
  39. }
  40. // Forbidden sends a forbidden error to the client.
  41. func Forbidden(w http.ResponseWriter, r *http.Request) {
  42. logger.Error("[HTTP:Forbidden] %s", r.URL)
  43. builder := response.New(w, r)
  44. builder.WithStatus(http.StatusForbidden)
  45. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  46. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  47. builder.WithBody("Access Forbidden")
  48. builder.Write()
  49. }
  50. // NotFound sends a page not found error to the client.
  51. func NotFound(w http.ResponseWriter, r *http.Request) {
  52. logger.Error("[HTTP:Not Found] %s", r.URL)
  53. builder := response.New(w, r)
  54. builder.WithStatus(http.StatusNotFound)
  55. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  56. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  57. builder.WithBody("Page Not Found")
  58. builder.Write()
  59. }
  60. // Redirect redirects the user to another location.
  61. func Redirect(w http.ResponseWriter, r *http.Request, uri string) {
  62. http.Redirect(w, r, uri, http.StatusFound)
  63. }
  64. // RequestedRangeNotSatisfiable sends a range not satisfiable error to the client.
  65. func RequestedRangeNotSatisfiable(w http.ResponseWriter, r *http.Request, contentRange string) {
  66. logger.Error("[HTTP:Range Not Satisfiable] %s", r.URL)
  67. builder := response.New(w, r)
  68. builder.WithStatus(http.StatusRequestedRangeNotSatisfiable)
  69. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  70. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  71. builder.WithHeader("Content-Range", contentRange)
  72. builder.WithBody("Range Not Satisfiable")
  73. builder.Write()
  74. }