html.go 3.3 KB

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