httputils.go 2.7 KB

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