diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-05-11 23:50:22 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-05-12 17:26:26 +0700 |
| commit | c97c4ff8d531658cbce14310704c5bbb95e95dbd (patch) | |
| tree | d9ff54c583c3c46719960eb79b519905d5bfc29b /lib/http | |
| parent | 25caa3fb9d7c87f08afae5c7f2d742deebb88efc (diff) | |
| download | pakakeh.go-c97c4ff8d531658cbce14310704c5bbb95e95dbd.tar.xz | |
http: prefix the header constants with "Header"
Diffstat (limited to 'lib/http')
| -rw-r--r-- | lib/http/client.go | 12 | ||||
| -rw-r--r-- | lib/http/endpoint.go | 8 | ||||
| -rw-r--r-- | lib/http/http.go | 11 | ||||
| -rw-r--r-- | lib/http/server.go | 13 | ||||
| -rw-r--r-- | lib/http/server_test.go | 8 |
5 files changed, 27 insertions, 25 deletions
diff --git a/lib/http/client.go b/lib/http/client.go index 467ad8be..d3e8e073 100644 --- a/lib/http/client.go +++ b/lib/http/client.go @@ -133,7 +133,7 @@ func (client *Client) PostForm(path string, params url.Values) ( } client.setHeaders(httpReq) - httpReq.Header.Set(ContentType, ContentTypeForm) + httpReq.Header.Set(HeaderContentType, ContentTypeForm) httpRes, err := client.Client.Do(httpReq) if err != nil { @@ -175,7 +175,7 @@ func (client *Client) PostFormData(path string, params map[string][]byte) ( } client.setHeaders(httpReq) - httpReq.Header.Set(ContentType, contentType) + httpReq.Header.Set(HeaderContentType, contentType) httpRes, err := client.Client.Do(httpReq) if err != nil { @@ -216,7 +216,7 @@ func (client *Client) PostJSON(path string, params interface{}) ( } client.setHeaders(httpReq) - httpReq.Header.Set(ContentType, ContentTypeJSON) + httpReq.Header.Set(HeaderContentType, ContentTypeJSON) httpRes, err := client.Client.Do(httpReq) if err != nil { @@ -253,11 +253,11 @@ func (client *Client) setHeaders(req *http.Request) { // setUserAgent set the User-Agent header only if its not defined by user. // func (client *Client) setUserAgent() { - v := client.defHeaders.Get(UserAgent) + v := client.defHeaders.Get(HeaderUserAgent) if len(v) > 0 { return } - client.defHeaders.Set(UserAgent, defUserAgent) + client.defHeaders.Set(HeaderUserAgent, defUserAgent) } // @@ -273,7 +273,7 @@ func (client *Client) uncompress(res *http.Response, body []byte) ( return body, nil } - contentType := res.Header.Get(ContentType) + contentType := res.Header.Get(HeaderContentType) switch { case strings.HasPrefix(contentType, "text/"): case strings.HasPrefix(contentType, ContentTypeJSON): diff --git a/lib/http/endpoint.go b/lib/http/endpoint.go index eb8a31cf..b70d16c4 100644 --- a/lib/http/endpoint.go +++ b/lib/http/endpoint.go @@ -139,11 +139,11 @@ func (ep *Endpoint) call( res.WriteHeader(http.StatusNoContent) return case ResponseTypeBinary: - res.Header().Set(ContentType, ContentTypeBinary) + res.Header().Set(HeaderContentType, ContentTypeBinary) case ResponseTypeJSON: - res.Header().Set(ContentType, ContentTypeJSON) + res.Header().Set(HeaderContentType, ContentTypeJSON) case ResponseTypePlain: - res.Header().Set(ContentType, ContentTypePlain) + res.Header().Set(HeaderContentType, ContentTypePlain) } _, e = res.Write(rspb) @@ -161,7 +161,7 @@ func (ep *Endpoint) error(res http.ResponseWriter, e error) { } res.WriteHeader(se.Code) - res.Header().Set(ContentType, ContentTypeJSON) + res.Header().Set(HeaderContentType, ContentTypeJSON) rsp, err := json.Marshal(se) if err != nil { diff --git a/lib/http/http.go b/lib/http/http.go index d13f3c8b..39139829 100644 --- a/lib/http/http.go +++ b/lib/http/http.go @@ -149,9 +149,6 @@ const ( ContentEncodingGzip = "gzip" ContentEncodingDeflate = "deflate" // Using zlib. - ContentLength = "Content-Length" - - ContentType = "Content-Type" ContentTypeBinary = "application/octet-stream" ContentTypeForm = "application/x-www-form-urlencoded" ContentTypeHTML = "text/html; charset=utf-8" @@ -159,8 +156,12 @@ const ( ContentTypePlain = "text/plain; charset=utf-8" ContentTypeXML = "text/xml; charset=utf-8" - HeaderLocation = "Location" - UserAgent = "User-Agent" + HeaderAllow = "Allow" + HeaderContentLength = "Content-Length" + HeaderContentType = "Content-Type" + HeaderLocation = "Location" + HeaderOrigin = "Origin" + HeaderUserAgent = "User-Agent" ) var ( diff --git a/lib/http/server.go b/lib/http/server.go index 3f6d92ca..e12c20cf 100644 --- a/lib/http/server.go +++ b/lib/http/server.go @@ -373,7 +373,7 @@ func (srv *Server) handleFS( return } - res.Header().Set(ContentType, node.ContentType) + res.Header().Set(HeaderContentType, node.ContentType) if len(node.ContentEncoding) > 0 { res.Header().Set(ContentEncoding, node.ContentEncoding) @@ -397,7 +397,7 @@ func (srv *Server) handleFS( size = int64(len(body)) } - res.Header().Set(ContentLength, strconv.FormatInt(size, 10)) + res.Header().Set(HeaderContentLength, strconv.FormatInt(size, 10)) if method == RequestMethodHead { res.WriteHeader(http.StatusOK) @@ -449,11 +449,11 @@ func (srv *Server) handleHead(res http.ResponseWriter, req *http.Request) { res.WriteHeader(http.StatusNoContent) return case ResponseTypeBinary: - res.Header().Set(ContentType, ContentTypeBinary) + res.Header().Set(HeaderContentType, ContentTypeBinary) case ResponseTypeJSON: - res.Header().Set(ContentType, ContentTypeJSON) + res.Header().Set(HeaderContentType, ContentTypeJSON) case ResponseTypePlain: - res.Header().Set(ContentType, ContentTypePlain) + res.Header().Set(HeaderContentType, ContentTypePlain) } res.WriteHeader(http.StatusOK) @@ -531,7 +531,8 @@ func (srv *Server) handleOptions(res http.ResponseWriter, req *http.Request) { sort.Strings(allows) - res.Header().Set("Allow", strings.Join(allows, ", ")) + res.Header().Set(HeaderAllow, strings.Join(allows, ", ")) + res.WriteHeader(http.StatusOK) } diff --git a/lib/http/server_test.go b/lib/http/server_test.go index 581539c5..7446274f 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -165,7 +165,7 @@ func TestRegisterDelete(t *testing.T) { test.Assert(t, "Body", c.expBody, string(body), true) - gotContentType := res.Header.Get(ContentType) + gotContentType := res.Header.Get(HeaderContentType) test.Assert(t, "Content-Type", c.expContentType, gotContentType, true) } @@ -376,9 +376,9 @@ func TestRegisterHead(t *testing.T) { true) test.Assert(t, "Body", c.expBody, string(body), true) test.Assert(t, "Header.ContentType", c.expContentType, - res.Header[ContentType], true) + res.Header[HeaderContentType], true) test.Assert(t, "Header.ContentLength", c.expContentLength, - res.Header[ContentLength], true) + res.Header[HeaderContentLength], true) } } @@ -495,7 +495,7 @@ k=vv`, t.Fatal(e) } - req.Header.Set(ContentType, ContentTypeForm) + req.Header.Set(HeaderContentType, ContentTypeForm) res, e := client.Do(req) if e != nil { |
