render.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Package render implements functionality related to response rendering.
  2. package render
  3. import (
  4. "bytes"
  5. "encoding/json"
  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, v interface{}) {
  13. JSONStatus(w, 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, v interface{}, status int) {
  21. var b bytes.Buffer
  22. if err := json.NewEncoder(&b).Encode(v); err != nil {
  23. panic(err)
  24. }
  25. setContentTypeUnlessPresent(w, "application/json")
  26. w.WriteHeader(status)
  27. _, _ = b.WriteTo(w)
  28. log.EnabledResponse(w, v)
  29. }
  30. // ProtoJSON is shorthand for ProtoJSONStatus(w, m, http.StatusOK).
  31. func ProtoJSON(w http.ResponseWriter, m proto.Message) {
  32. ProtoJSONStatus(w, m, http.StatusOK)
  33. }
  34. // ProtoJSONStatus writes the given value into the http.ResponseWriter and the
  35. // given status is written as the status code of the response.
  36. func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
  37. b, err := protojson.Marshal(m)
  38. if err != nil {
  39. panic(err)
  40. }
  41. setContentTypeUnlessPresent(w, "application/json")
  42. w.WriteHeader(status)
  43. _, _ = w.Write(b)
  44. }
  45. func setContentTypeUnlessPresent(w http.ResponseWriter, contentType string) {
  46. const header = "Content-Type"
  47. h := w.Header()
  48. if _, ok := h[header]; !ok {
  49. h.Set(header, contentType)
  50. }
  51. }
  52. // RenderableError is the set of errors that implement the basic Render method.
  53. //
  54. // Errors that implement this interface will use their own Render method when
  55. // being rendered into responses.
  56. type RenderableError interface {
  57. error
  58. Render(http.ResponseWriter)
  59. }
  60. // AsRenderableError attempts to return an error type that implements the
  61. // RenderableError interface.
  62. func AsRenderableError(err error) (RenderableError, bool) {
  63. //nolint:errorlint // ignore type assertion warning. casting to interface is hard.
  64. if r, ok := err.(RenderableError); ok {
  65. return r, true
  66. }
  67. return nil, false
  68. }
  69. // Error marshals the JSON representation of err to w. In case err implements
  70. // RenderableError its own Render method will be called instead.
  71. func Error(w http.ResponseWriter, err error) {
  72. log.Error(w, err)
  73. if e, ok := AsRenderableError(err); ok {
  74. e.Render(w)
  75. return
  76. }
  77. JSONStatus(w, err, statusCodeFromError(err))
  78. }
  79. // StatusCodedError is the set of errors that implement the basic StatusCode
  80. // function.
  81. //
  82. // Errors that implement this interface will use the code reported by StatusCode
  83. // as the HTTP response code when being rendered by this package.
  84. type StatusCodedError interface {
  85. error
  86. StatusCode() int
  87. }
  88. // AsStatusCodedError attempts to return an error type that implements the
  89. // StatusCodedError interface.
  90. func AsStatusCodedError(err error) (StatusCodedError, bool) {
  91. //nolint:errorlint // ignore type assertion warning. casting to interface is hard.
  92. if sc, ok := err.(StatusCodedError); ok {
  93. return sc, true
  94. }
  95. return nil, false
  96. }
  97. func statusCodeFromError(err error) (code int) {
  98. code = http.StatusInternalServerError
  99. type causer interface {
  100. Cause() error
  101. }
  102. for err != nil {
  103. if sc, ok := AsStatusCodedError(err); ok {
  104. code = sc.StatusCode()
  105. break
  106. }
  107. //nolint:errorlint // ignore type assertion warning. casting to interface is hard.
  108. cause, ok := err.(causer)
  109. if !ok {
  110. break
  111. }
  112. err = cause.Cause()
  113. }
  114. return
  115. }