diff --git a/netutil/httpheader/header.go b/netutil/httpheader/header.go index 596b26b08..2f02d7a63 100644 --- a/netutil/httpheader/header.go +++ b/netutil/httpheader/header.go @@ -7,7 +7,20 @@ const ( Accept = "Accept" Cookie = "Cookie" + // Upgrade header. check websocket: + // header["Connection"] == "Upgrade" and header["Upgrade"] == "websocket" + Upgrade = "Upgrade" AcceptEnc = "Accept-Encoding" ContentType = "Content-Type" + Connection = "Connection" + + XRealIP = "X-Real-IP" + + XForwardedFor = "X-Forwarded-For" + XForwardedHost = "X-Forwarded-Host" + XForwardedProto = "X-Forwarded-Proto" + + // XRequestedWith header. check ajax: header["X-Requested-With"] == XMLHttpRequest + XRequestedWith = "X-Requested-With" ) diff --git a/netutil/httpreq/util.go b/netutil/httpreq/util.go index 0bcaa0b7c..2b784c4a7 100644 --- a/netutil/httpreq/util.go +++ b/netutil/httpreq/util.go @@ -374,3 +374,20 @@ func ResponseToString(w *http.Response) string { return buf.String() } + +// ParseAccept header to strings. referred from gin framework +func ParseAccept(acceptHeader string) []string { + if acceptHeader == "" { + return []string{} + } + + parts := strings.Split(acceptHeader, ",") + outs := make([]string, 0, len(parts)) + + for _, part := range parts { + if part = strings.TrimSpace(strings.Split(part, ";")[0]); part != "" { + outs = append(outs, part) + } + } + return outs +} diff --git a/netutil/httpreq/util_test.go b/netutil/httpreq/util_test.go index 3bc8c841d..96ab2e999 100644 --- a/netutil/httpreq/util_test.go +++ b/netutil/httpreq/util_test.go @@ -126,3 +126,16 @@ func TestMergeURLValues(t *testing.T) { uv = httpreq.MergeURLValues(nil, url.Values{"key2": []string{"val2"}}) assert.Eq(t, "val2", uv.Get("key2")) } + +func TestParseAccept(t *testing.T) { + // parseAccept + ss := httpreq.ParseAccept("") + assert.Len(t, ss, 0) + + ss = httpreq.ParseAccept(",") + assert.Len(t, ss, 0) + + ss = httpreq.ParseAccept("application/json") + assert.Len(t, ss, 1) + assert.Eq(t, []string{"application/json"}, ss) +}