github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/api/client/lib/request.go (about)

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