httputils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package httputils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "golang.org/x/net/context"
  9. "github.com/docker/docker/api"
  10. "github.com/docker/docker/pkg/version"
  11. )
  12. // APIVersionKey is the client's requested API version.
  13. const APIVersionKey = "api-version"
  14. // UAStringKey is used as key type for user-agent string in net/context struct
  15. const UAStringKey = "upstream-user-agent"
  16. // APIFunc is an adapter to allow the use of ordinary functions as Docker API endpoints.
  17. // Any function that has the appropriate signature can be registered as a API endpoint (e.g. getVersion).
  18. type APIFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error
  19. // HijackConnection interrupts the http response writer to get the
  20. // underlying connection and operate with it.
  21. func HijackConnection(w http.ResponseWriter) (io.ReadCloser, io.Writer, error) {
  22. conn, _, err := w.(http.Hijacker).Hijack()
  23. if err != nil {
  24. return nil, nil, err
  25. }
  26. // Flush the options to make sure the client sets the raw mode
  27. conn.Write([]byte{})
  28. return conn, conn, nil
  29. }
  30. // CloseStreams ensures that a list for http streams are properly closed.
  31. func CloseStreams(streams ...interface{}) {
  32. for _, stream := range streams {
  33. if tcpc, ok := stream.(interface {
  34. CloseWrite() error
  35. }); ok {
  36. tcpc.CloseWrite()
  37. } else if closer, ok := stream.(io.Closer); ok {
  38. closer.Close()
  39. }
  40. }
  41. }
  42. // CheckForJSON makes sure that the request's Content-Type is application/json.
  43. func CheckForJSON(r *http.Request) error {
  44. ct := r.Header.Get("Content-Type")
  45. // No Content-Type header is ok as long as there's no Body
  46. if ct == "" {
  47. if r.Body == nil || r.ContentLength == 0 {
  48. return nil
  49. }
  50. }
  51. // Otherwise it better be json
  52. if api.MatchesContentType(ct, "application/json") {
  53. return nil
  54. }
  55. return fmt.Errorf("Content-Type specified (%s) must be 'application/json'", ct)
  56. }
  57. // ParseForm ensures the request form is parsed even with invalid content types.
  58. // If we don't do this, POST method without Content-type (even with empty body) will fail.
  59. func ParseForm(r *http.Request) error {
  60. if r == nil {
  61. return nil
  62. }
  63. if err := r.ParseForm(); err != nil && !strings.HasPrefix(err.Error(), "mime:") {
  64. return err
  65. }
  66. return nil
  67. }
  68. // ParseMultipartForm ensures the request form is parsed, even with invalid content types.
  69. func ParseMultipartForm(r *http.Request) error {
  70. if err := r.ParseMultipartForm(4096); err != nil && !strings.HasPrefix(err.Error(), "mime:") {
  71. return err
  72. }
  73. return nil
  74. }
  75. // WriteJSON writes the value v to the http response stream as json with standard json encoding.
  76. func WriteJSON(w http.ResponseWriter, code int, v interface{}) error {
  77. w.Header().Set("Content-Type", "application/json")
  78. w.WriteHeader(code)
  79. return json.NewEncoder(w).Encode(v)
  80. }
  81. // VersionFromContext returns an API version from the context using APIVersionKey.
  82. // It panics if the context value does not have version.Version type.
  83. func VersionFromContext(ctx context.Context) (ver version.Version) {
  84. if ctx == nil {
  85. return
  86. }
  87. val := ctx.Value(APIVersionKey)
  88. if val == nil {
  89. return
  90. }
  91. return val.(version.Version)
  92. }