github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/pkg/httputils/httputils.go (about) 1 package httputils 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "regexp" 8 "strings" 9 10 "github.com/docker/docker/pkg/jsonmessage" 11 ) 12 13 // Download requests a given URL and returns an io.Reader. 14 func Download(url string) (resp *http.Response, err error) { 15 if resp, err = http.Get(url); err != nil { 16 return nil, err 17 } 18 if resp.StatusCode >= 400 { 19 return nil, fmt.Errorf("Got HTTP status code >= 400: %s", resp.Status) 20 } 21 return resp, nil 22 } 23 24 // NewHTTPRequestError returns a JSON response error. 25 func NewHTTPRequestError(msg string, res *http.Response) error { 26 return &jsonmessage.JSONError{ 27 Message: msg, 28 Code: res.StatusCode, 29 } 30 } 31 32 // ServerHeader contains the server information. 33 type ServerHeader struct { 34 App string // docker 35 Ver string // 1.8.0-dev 36 OS string // windows or linux 37 } 38 39 // ParseServerHeader extracts pieces from an HTTP server header 40 // which is in the format "docker/version (os)" eg docker/1.8.0-dev (windows). 41 func ParseServerHeader(hdr string) (*ServerHeader, error) { 42 re := regexp.MustCompile(`.*\((.+)\).*$`) 43 r := &ServerHeader{} 44 if matches := re.FindStringSubmatch(hdr); matches != nil { 45 r.OS = matches[1] 46 parts := strings.Split(hdr, "/") 47 if len(parts) != 2 { 48 return nil, errors.New("Bad header: '/' missing") 49 } 50 r.App = parts[0] 51 v := strings.Split(parts[1], " ") 52 if len(v) != 2 { 53 return nil, errors.New("Bad header: Expected single space") 54 } 55 r.Ver = v[0] 56 return r, nil 57 } 58 return nil, errors.New("Bad header: Failed regex match") 59 }