4
0

render.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Package render implements functionality related to response rendering.
  2. package render
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "net/http"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. "google.golang.org/protobuf/proto"
  9. "github.com/smallstep/certificates/api/log"
  10. )
  11. // JSON is shorthand for JSONStatus(w, v, http.StatusOK).
  12. func JSON(w http.ResponseWriter, r *http.Request, v any) {
  13. JSONStatus(w, r, v, http.StatusOK)
  14. }
  15. // JSONStatus marshals v into w. It additionally sets the status code of
  16. // w to the given one.
  17. //
  18. // JSONStatus sets the Content-Type of w to application/json unless one is
  19. // specified.
  20. func JSONStatus(w http.ResponseWriter, r *http.Request, v any, status int) {
  21. setContentTypeUnlessPresent(w, "application/json")
  22. w.WriteHeader(status)
  23. if err := json.NewEncoder(w).Encode(v); err != nil {
  24. if _, ok := errors.AsType[*json.UnsupportedTypeError](err); ok {
  25. panic(err)
  26. }
  27. if _, ok := errors.AsType[*json.UnsupportedValueError](err); ok {
  28. panic(err)
  29. }
  30. if _, ok := errors.AsType[*json.MarshalerError](err); ok {
  31. panic(err)
  32. }
  33. }
  34. log.EnabledResponse(w, r, v)
  35. }
  36. // ProtoJSON is shorthand for ProtoJSONStatus(w, m, http.StatusOK).
  37. func ProtoJSON(w http.ResponseWriter, m proto.Message) {
  38. ProtoJSONStatus(w, m, http.StatusOK)
  39. }
  40. // ProtoJSONStatus writes the given value into the http.ResponseWriter and the
  41. // given status is written as the status code of the response.
  42. func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
  43. b, err := protojson.Marshal(m)
  44. if err != nil {
  45. panic(err)
  46. }
  47. setContentTypeUnlessPresent(w, "application/json")
  48. w.WriteHeader(status)
  49. _, _ = w.Write(b)
  50. }
  51. func setContentTypeUnlessPresent(w http.ResponseWriter, contentType string) {
  52. const header = "Content-Type"
  53. h := w.Header()
  54. if _, ok := h[header]; !ok {
  55. h.Set(header, contentType)
  56. }
  57. }
  58. // RenderableError is the set of errors that implement the basic Render method.
  59. //
  60. // Errors that implement this interface will use their own Render method when
  61. // being rendered into responses.
  62. type RenderableError interface {
  63. error
  64. Render(http.ResponseWriter, *http.Request)
  65. }
  66. // Error marshals the JSON representation of err to w. In case err implements
  67. // RenderableError its own Render method will be called instead.
  68. func Error(rw http.ResponseWriter, r *http.Request, err error) {
  69. log.Error(rw, r, err)
  70. if re, ok := errors.AsType[RenderableError](err); ok {
  71. re.Render(rw, r)
  72. return
  73. }
  74. JSONStatus(rw, r, err, statusCodeFromError(err))
  75. }
  76. // StatusCodedError is the set of errors that implement the basic StatusCode
  77. // function.
  78. //
  79. // Errors that implement this interface will use the code reported by StatusCode
  80. // as the HTTP response code when being rendered by this package.
  81. type StatusCodedError interface {
  82. error
  83. StatusCode() int
  84. }
  85. func statusCodeFromError(err error) (code int) {
  86. code = http.StatusInternalServerError
  87. type causer interface {
  88. Cause() error
  89. }
  90. for err != nil {
  91. if sc, ok := errors.AsType[StatusCodedError](err); ok {
  92. code = sc.StatusCode()
  93. break
  94. }
  95. var c causer
  96. if !errors.As(err, &c) {
  97. break
  98. }
  99. err = c.Cause()
  100. }
  101. return
  102. }