httputils.go 3.0 KB

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