request.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package lib
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "github.com/docker/docker/utils"
  12. )
  13. // serverResponse is a wrapper for http API responses.
  14. type serverResponse struct {
  15. body io.ReadCloser
  16. header http.Header
  17. statusCode int
  18. }
  19. // head sends an http request to the docker API using the method HEAD.
  20. func (cli *Client) head(path string, query url.Values, headers map[string][]string) (*serverResponse, error) {
  21. return cli.sendRequest("HEAD", path, query, nil, headers)
  22. }
  23. // get sends an http request to the docker API using the method GET.
  24. func (cli *Client) get(path string, query url.Values, headers map[string][]string) (*serverResponse, error) {
  25. return cli.sendRequest("GET", path, query, nil, headers)
  26. }
  27. // post sends an http request to the docker API using the method POST.
  28. func (cli *Client) post(path string, query url.Values, body interface{}, headers map[string][]string) (*serverResponse, error) {
  29. return cli.sendRequest("POST", path, query, body, headers)
  30. }
  31. // postRaw sends the raw input to the docker API using the method POST.
  32. func (cli *Client) postRaw(path string, query url.Values, body io.Reader, headers map[string][]string) (*serverResponse, error) {
  33. return cli.sendClientRequest("POST", path, query, body, headers)
  34. }
  35. // put sends an http request to the docker API using the method PUT.
  36. func (cli *Client) put(path string, query url.Values, body interface{}, headers map[string][]string) (*serverResponse, error) {
  37. return cli.sendRequest("PUT", path, query, body, headers)
  38. }
  39. // putRaw sends the raw input to the docker API using the method PUT.
  40. func (cli *Client) putRaw(path string, query url.Values, body io.Reader, headers map[string][]string) (*serverResponse, error) {
  41. return cli.sendClientRequest("PUT", path, query, body, headers)
  42. }
  43. // delete sends an http request to the docker API using the method DELETE.
  44. func (cli *Client) delete(path string, query url.Values, headers map[string][]string) (*serverResponse, error) {
  45. return cli.sendRequest("DELETE", path, query, nil, headers)
  46. }
  47. func (cli *Client) sendRequest(method, path string, query url.Values, body interface{}, headers map[string][]string) (*serverResponse, error) {
  48. params, err := encodeData(body)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if body != nil {
  53. if headers == nil {
  54. headers = make(map[string][]string)
  55. }
  56. headers["Content-Type"] = []string{"application/json"}
  57. }
  58. return cli.sendClientRequest(method, path, query, params, headers)
  59. }
  60. func (cli *Client) sendClientRequest(method, path string, query url.Values, body io.Reader, headers map[string][]string) (*serverResponse, error) {
  61. serverResp := &serverResponse{
  62. body: nil,
  63. statusCode: -1,
  64. }
  65. expectedPayload := (method == "POST" || method == "PUT")
  66. if expectedPayload && body == nil {
  67. body = bytes.NewReader([]byte{})
  68. }
  69. req, err := cli.newRequest(method, path, query, body, headers)
  70. req.URL.Host = cli.addr
  71. req.URL.Scheme = cli.scheme
  72. if expectedPayload && req.Header.Get("Content-Type") == "" {
  73. req.Header.Set("Content-Type", "text/plain")
  74. }
  75. resp, err := cli.httpClient.Do(req)
  76. if resp != nil {
  77. serverResp.statusCode = resp.StatusCode
  78. }
  79. if err != nil {
  80. if utils.IsTimeout(err) || strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial unix") {
  81. return serverResp, ErrConnectionFailed
  82. }
  83. if cli.scheme == "http" && strings.Contains(err.Error(), "malformed HTTP response") {
  84. return serverResp, fmt.Errorf("%v.\n* Are you trying to connect to a TLS-enabled daemon without TLS?", err)
  85. }
  86. if cli.scheme == "https" && strings.Contains(err.Error(), "remote error: bad certificate") {
  87. return serverResp, fmt.Errorf("The server probably has client authentication (--tlsverify) enabled. Please check your TLS client certification settings: %v", err)
  88. }
  89. return serverResp, fmt.Errorf("An error occurred trying to connect: %v", err)
  90. }
  91. if serverResp.statusCode < 200 || serverResp.statusCode >= 400 {
  92. body, err := ioutil.ReadAll(resp.Body)
  93. if err != nil {
  94. return serverResp, err
  95. }
  96. if len(body) == 0 {
  97. return serverResp, fmt.Errorf("Error: request returned %s for API route and version %s, check if the server supports the requested API version", http.StatusText(serverResp.statusCode), req.URL)
  98. }
  99. return serverResp, fmt.Errorf("Error response from daemon: %s", bytes.TrimSpace(body))
  100. }
  101. serverResp.body = resp.Body
  102. serverResp.header = resp.Header
  103. return serverResp, nil
  104. }
  105. func (cli *Client) newRequest(method, path string, query url.Values, body io.Reader, headers map[string][]string) (*http.Request, error) {
  106. apiPath := cli.getAPIPath(path, query)
  107. req, err := http.NewRequest(method, apiPath, body)
  108. if err != nil {
  109. return nil, err
  110. }
  111. // Add CLI Config's HTTP Headers BEFORE we set the Docker headers
  112. // then the user can't change OUR headers
  113. for k, v := range cli.customHTTPHeaders {
  114. req.Header.Set(k, v)
  115. }
  116. if headers != nil {
  117. for k, v := range headers {
  118. req.Header[k] = v
  119. }
  120. }
  121. return req, nil
  122. }
  123. func encodeData(data interface{}) (*bytes.Buffer, error) {
  124. params := bytes.NewBuffer(nil)
  125. if data != nil {
  126. if err := json.NewEncoder(params).Encode(data); err != nil {
  127. return nil, err
  128. }
  129. }
  130. return params, nil
  131. }
  132. func ensureReaderClosed(response *serverResponse) {
  133. if response != nil && response.body != nil {
  134. response.body.Close()
  135. }
  136. }