html.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "html"
  6. "log/slog"
  7. "net/http"
  8. "miniflux.app/v2/internal/http/request"
  9. "miniflux.app/v2/internal/http/response"
  10. )
  11. // OK creates a new HTML response with a 200 status code.
  12. func OK[T []byte | string](w http.ResponseWriter, r *http.Request, body T) {
  13. builder := response.New(w, r)
  14. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  15. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  16. builder.WithBody(body)
  17. builder.Write()
  18. }
  19. // ServerError sends an internal error to the client.
  20. func ServerError(w http.ResponseWriter, r *http.Request, err error) {
  21. slog.Error(http.StatusText(http.StatusInternalServerError),
  22. slog.Any("error", err),
  23. slog.String("client_ip", request.ClientIP(r)),
  24. slog.Group("request",
  25. slog.String("method", r.Method),
  26. slog.String("uri", r.RequestURI),
  27. slog.String("user_agent", r.UserAgent()),
  28. ),
  29. slog.Group("response",
  30. slog.Int("status_code", http.StatusInternalServerError),
  31. ),
  32. )
  33. builder := response.New(w, r)
  34. builder.WithStatus(http.StatusInternalServerError)
  35. builder.WithHeader("Content-Security-Policy", response.ContentSecurityPolicyForUntrustedContent)
  36. builder.WithHeader("Content-Type", "text/plain; charset=utf-8")
  37. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  38. builder.WithBody(html.EscapeString(err.Error()))
  39. builder.Write()
  40. }
  41. // BadRequest sends a bad request error to the client.
  42. func BadRequest(w http.ResponseWriter, r *http.Request, err error) {
  43. slog.Warn(http.StatusText(http.StatusBadRequest),
  44. slog.Any("error", err),
  45. slog.String("client_ip", request.ClientIP(r)),
  46. slog.Group("request",
  47. slog.String("method", r.Method),
  48. slog.String("uri", r.RequestURI),
  49. slog.String("user_agent", r.UserAgent()),
  50. ),
  51. slog.Group("response",
  52. slog.Int("status_code", http.StatusBadRequest),
  53. ),
  54. )
  55. builder := response.New(w, r)
  56. builder.WithStatus(http.StatusBadRequest)
  57. builder.WithHeader("Content-Security-Policy", response.ContentSecurityPolicyForUntrustedContent)
  58. builder.WithHeader("Content-Type", "text/plain; charset=utf-8")
  59. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  60. builder.WithBody(html.EscapeString(err.Error()))
  61. builder.Write()
  62. }
  63. // Forbidden sends a forbidden error to the client.
  64. func Forbidden(w http.ResponseWriter, r *http.Request) {
  65. slog.Warn(http.StatusText(http.StatusForbidden),
  66. slog.String("client_ip", request.ClientIP(r)),
  67. slog.Group("request",
  68. slog.String("method", r.Method),
  69. slog.String("uri", r.RequestURI),
  70. slog.String("user_agent", r.UserAgent()),
  71. ),
  72. slog.Group("response",
  73. slog.Int("status_code", http.StatusForbidden),
  74. ),
  75. )
  76. builder := response.New(w, r)
  77. builder.WithStatus(http.StatusForbidden)
  78. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  79. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  80. builder.WithBody("Access Forbidden")
  81. builder.Write()
  82. }
  83. // NotFound sends a page not found error to the client.
  84. func NotFound(w http.ResponseWriter, r *http.Request) {
  85. slog.Warn(http.StatusText(http.StatusNotFound),
  86. slog.String("client_ip", request.ClientIP(r)),
  87. slog.Group("request",
  88. slog.String("method", r.Method),
  89. slog.String("uri", r.RequestURI),
  90. slog.String("user_agent", r.UserAgent()),
  91. ),
  92. slog.Group("response",
  93. slog.Int("status_code", http.StatusNotFound),
  94. ),
  95. )
  96. builder := response.New(w, r)
  97. builder.WithStatus(http.StatusNotFound)
  98. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  99. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  100. builder.WithBody("Page Not Found")
  101. builder.Write()
  102. }
  103. // Redirect redirects the user to another location.
  104. func Redirect(w http.ResponseWriter, r *http.Request, uri string) {
  105. http.Redirect(w, r, uri, http.StatusFound)
  106. }
  107. // RequestedRangeNotSatisfiable sends a range not satisfiable error to the client.
  108. func RequestedRangeNotSatisfiable(w http.ResponseWriter, r *http.Request, contentRange string) {
  109. slog.Warn(http.StatusText(http.StatusRequestedRangeNotSatisfiable),
  110. slog.String("client_ip", request.ClientIP(r)),
  111. slog.Group("request",
  112. slog.String("method", r.Method),
  113. slog.String("uri", r.RequestURI),
  114. slog.String("user_agent", r.UserAgent()),
  115. ),
  116. slog.Group("response",
  117. slog.Int("status_code", http.StatusRequestedRangeNotSatisfiable),
  118. ),
  119. )
  120. builder := response.New(w, r)
  121. builder.WithStatus(http.StatusRequestedRangeNotSatisfiable)
  122. builder.WithHeader("Content-Type", "text/html; charset=utf-8")
  123. builder.WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
  124. builder.WithHeader("Content-Range", contentRange)
  125. builder.WithBody("Range Not Satisfiable")
  126. builder.Write()
  127. }