render.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // Error marshals the JSON representation of err to w. In case err implements
  61. // RenderableError its own Render method will be called instead.
  62. func Error(w http.ResponseWriter, err error) {
  63. log.Error(w, err)
  64. if e, ok := err.(RenderableError); ok {
  65. e.Render(w)
  66. return
  67. }
  68. JSONStatus(w, err, statusCodeFromError(err))
  69. }
  70. // StatusCodedError is the set of errors that implement the basic StatusCode
  71. // function.
  72. //
  73. // Errors that implement this interface will use the code reported by StatusCode
  74. // as the HTTP response code when being rendered by this package.
  75. type StatusCodedError interface {
  76. error
  77. StatusCode() int
  78. }
  79. func statusCodeFromError(err error) (code int) {
  80. code = http.StatusInternalServerError
  81. type causer interface {
  82. Cause() error
  83. }
  84. for err != nil {
  85. if sc, ok := err.(StatusCodedError); ok {
  86. code = sc.StatusCode()
  87. break
  88. }
  89. cause, ok := err.(causer)
  90. if !ok {
  91. break
  92. }
  93. err = cause.Cause()
  94. }
  95. return
  96. }