Windows: Security warning based on server OS

Signed-off-by: John Howard <jhoward@microsoft.com>
This commit is contained in:
John Howard 2015-06-04 06:30:14 -07:00
parent f5527956fb
commit b4f6e2c8b7

View file

@ -1,8 +1,11 @@
package httputils
import (
"errors"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/docker/docker/pkg/jsonmessage"
)
@ -25,3 +28,31 @@ func NewHTTPRequestError(msg string, res *http.Response) error {
Code: res.StatusCode,
}
}
type ServerHeader struct {
App string // docker
Ver string // 1.8.0-dev
OS string // windows or linux
}
// parseServerHeader extracts pieces from am HTTP server header
// which is in the format "docker/version (os)" eg docker/1.8.0-dev (windows)
func ParseServerHeader(hdr string) (*ServerHeader, error) {
re := regexp.MustCompile(`.*\((.+)\).*$`)
r := &ServerHeader{}
if matches := re.FindStringSubmatch(hdr); matches != nil {
r.OS = matches[1]
parts := strings.Split(hdr, "/")
if len(parts) != 2 {
return nil, errors.New("Bad header: '/' missing")
}
r.App = parts[0]
v := strings.Split(parts[1], " ")
if len(v) != 2 {
return nil, errors.New("Bad header: Expected single space")
}
r.Ver = v[0]
return r, nil
}
return nil, errors.New("Bad header: Failed regex match")
}