html.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package response // import "miniflux.app/v2/internal/http/response"
  4. import (
  5. "fmt"
  6. "html"
  7. "log/slog"
  8. "net/http"
  9. "miniflux.app/v2/internal/http/request"
  10. "miniflux.app/v2/internal/urllib"
  11. )
  12. // HTMLServerError sends an internal error to the client.
  13. func HTMLServerError(w http.ResponseWriter, r *http.Request, err error) {
  14. slog.Error(http.StatusText(http.StatusInternalServerError),
  15. slog.Any("error", err),
  16. slog.String("client_ip", request.ClientIP(r)),
  17. slog.Group("request",
  18. slog.String("method", r.Method),
  19. slog.String("uri", r.RequestURI),
  20. slog.String("user_agent", r.UserAgent()),
  21. ),
  22. slog.Group("response",
  23. slog.Int("status_code", http.StatusInternalServerError),
  24. ),
  25. )
  26. NewBuilder(w, r).
  27. WithStatus(http.StatusInternalServerError).
  28. WithHeader("Content-Security-Policy", ContentSecurityPolicyForUntrustedContent).
  29. WithHeader("Content-Type", "text/plain; charset=utf-8").
  30. WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store").
  31. WithBodyAsString(html.EscapeString(err.Error())).
  32. Write()
  33. }
  34. // HTMLBadRequest sends a bad request error to the client.
  35. func HTMLBadRequest(w http.ResponseWriter, r *http.Request, err error) {
  36. slog.Warn(http.StatusText(http.StatusBadRequest),
  37. slog.Any("error", err),
  38. slog.String("client_ip", request.ClientIP(r)),
  39. slog.Group("request",
  40. slog.String("method", r.Method),
  41. slog.String("uri", r.RequestURI),
  42. slog.String("user_agent", r.UserAgent()),
  43. ),
  44. slog.Group("response",
  45. slog.Int("status_code", http.StatusBadRequest),
  46. ),
  47. )
  48. NewBuilder(w, r).
  49. WithStatus(http.StatusBadRequest).
  50. WithHeader("Content-Security-Policy", ContentSecurityPolicyForUntrustedContent).
  51. WithHeader("Content-Type", "text/plain; charset=utf-8").
  52. WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store").
  53. WithBodyAsString(html.EscapeString(err.Error())).
  54. Write()
  55. }
  56. // HTMLForbidden sends a forbidden error to the client.
  57. func HTMLForbidden(w http.ResponseWriter, r *http.Request) {
  58. slog.Warn(http.StatusText(http.StatusForbidden),
  59. slog.String("client_ip", request.ClientIP(r)),
  60. slog.Group("request",
  61. slog.String("method", r.Method),
  62. slog.String("uri", r.RequestURI),
  63. slog.String("user_agent", r.UserAgent()),
  64. ),
  65. slog.Group("response",
  66. slog.Int("status_code", http.StatusForbidden),
  67. ),
  68. )
  69. NewBuilder(w, r).
  70. WithStatus(http.StatusForbidden).
  71. WithHeader("Content-Type", "text/html; charset=utf-8").
  72. WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store").
  73. WithBodyAsString("Access Forbidden").
  74. Write()
  75. }
  76. // HTMLNotFound sends a page not found error to the client.
  77. func HTMLNotFound(w http.ResponseWriter, r *http.Request) {
  78. slog.Warn(http.StatusText(http.StatusNotFound),
  79. slog.String("client_ip", request.ClientIP(r)),
  80. slog.Group("request",
  81. slog.String("method", r.Method),
  82. slog.String("uri", r.RequestURI),
  83. slog.String("user_agent", r.UserAgent()),
  84. ),
  85. slog.Group("response",
  86. slog.Int("status_code", http.StatusNotFound),
  87. ),
  88. )
  89. NewBuilder(w, r).
  90. WithStatus(http.StatusNotFound).
  91. WithHeader("Content-Type", "text/html; charset=utf-8").
  92. WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store").
  93. WithBodyAsString("Page Not Found").
  94. Write()
  95. }
  96. // HTMLNotAcceptable sends a not acceptable error to the client.
  97. func HTMLNotAcceptable(w http.ResponseWriter, r *http.Request) {
  98. slog.Warn(http.StatusText(http.StatusNotAcceptable),
  99. slog.String("client_ip", request.ClientIP(r)),
  100. slog.Group("request",
  101. slog.String("method", r.Method),
  102. slog.String("uri", r.RequestURI),
  103. slog.String("user_agent", r.UserAgent()),
  104. slog.String("accept_encoding", r.Header.Get("Accept-Encoding")),
  105. ),
  106. slog.Group("response",
  107. slog.Int("status_code", http.StatusNotAcceptable),
  108. ),
  109. )
  110. NewBuilder(w, r).
  111. WithStatus(http.StatusNotAcceptable).
  112. WithHeader("Content-Type", "text/html; charset=utf-8").
  113. WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store").
  114. WithBodyAsString("Unknown Accept-Encoding").
  115. Write()
  116. }
  117. // HTMLRedirect redirects the user to a relative path or an absolute http(s) URL.
  118. func HTMLRedirect(w http.ResponseWriter, r *http.Request, uri string) {
  119. if !urllib.IsRelativePath(uri) && !urllib.IsAbsoluteURL(uri) {
  120. HTMLBadRequest(w, r, fmt.Errorf("invalid redirect URL: %q", uri))
  121. return
  122. }
  123. http.Redirect(w, r, uri, http.StatusFound)
  124. }
  125. // HTMLRequestedRangeNotSatisfiable sends a range not satisfiable error to the client.
  126. func HTMLRequestedRangeNotSatisfiable(w http.ResponseWriter, r *http.Request, contentRange string) {
  127. slog.Warn(http.StatusText(http.StatusRequestedRangeNotSatisfiable),
  128. slog.String("client_ip", request.ClientIP(r)),
  129. slog.Group("request",
  130. slog.String("method", r.Method),
  131. slog.String("uri", r.RequestURI),
  132. slog.String("user_agent", r.UserAgent()),
  133. ),
  134. slog.Group("response",
  135. slog.Int("status_code", http.StatusRequestedRangeNotSatisfiable),
  136. ),
  137. )
  138. NewBuilder(w, r).
  139. WithStatus(http.StatusRequestedRangeNotSatisfiable).
  140. WithHeader("Content-Type", "text/html; charset=utf-8").
  141. WithHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store").
  142. WithHeader("Content-Range", contentRange).
  143. WithBodyAsString("Range Not Satisfiable").
  144. Write()
  145. }