render.go 2.8 KB

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