From 850602361d8dae4e4b52e8b17b90589d57c47c18 Mon Sep 17 00:00:00 2001 From: Jeromy Johnson Date: Mon, 14 Mar 2016 16:11:29 -0700 Subject: [PATCH 1/2] fix double transfer encoding head problem License: MIT Signed-off-by: Jeromy Johnson --- commands/http/client.go | 2 +- commands/http/handler.go | 34 ++++++++++--------- commands/http/handler_test.go | 2 -- test/sharness/t0110-gateway.sh | 10 ++++++ ...230-channel-streaming-http-content-type.sh | 10 +++--- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/commands/http/client.go b/commands/http/client.go index eec01a268..5db1ec051 100644 --- a/commands/http/client.go +++ b/commands/http/client.go @@ -159,7 +159,7 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error contentType := httpRes.Header.Get(contentTypeHeader) contentType = strings.Split(contentType, ";")[0] - lengthHeader := httpRes.Header.Get(contentLengthHeader) + lengthHeader := httpRes.Header.Get(extraContentLengthHeader) if len(lengthHeader) > 0 { length, err := strconv.ParseUint(lengthHeader, 10, 64) if err != nil { diff --git a/commands/http/handler.go b/commands/http/handler.go index 9ac6d4a4a..82850764f 100644 --- a/commands/http/handler.go +++ b/commands/http/handler.go @@ -41,20 +41,23 @@ var ( ) const ( - StreamErrHeader = "X-Stream-Error" - streamHeader = "X-Stream-Output" - channelHeader = "X-Chunked-Output" - uaHeader = "User-Agent" - contentTypeHeader = "Content-Type" - contentLengthHeader = "Content-Length" - contentDispHeader = "Content-Disposition" - transferEncodingHeader = "Transfer-Encoding" - applicationJson = "application/json" - applicationOctetStream = "application/octet-stream" - plainText = "text/plain" - originHeader = "origin" + StreamErrHeader = "X-Stream-Error" + streamHeader = "X-Stream-Output" + channelHeader = "X-Chunked-Output" + extraContentLengthHeader = "X-Content-Length" + uaHeader = "User-Agent" + contentTypeHeader = "Content-Type" + contentDispHeader = "Content-Disposition" + transferEncodingHeader = "Transfer-Encoding" + applicationJson = "application/json" + applicationOctetStream = "application/octet-stream" + plainText = "text/plain" + originHeader = "origin" ) +var AllowedExposedHeadersArr = []string{streamHeader, channelHeader, extraContentLengthHeader} +var AllowedExposedHeaders = strings.Join(AllowedExposedHeadersArr, ", ") + const ( ACAOrigin = "Access-Control-Allow-Origin" ACAMethods = "Access-Control-Allow-Methods" @@ -241,7 +244,7 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req h.Set("Trailer", StreamErrHeader) if res.Length() > 0 { - h.Set(contentLengthHeader, strconv.FormatUint(res.Length(), 10)) + h.Set("X-Content-Length", strconv.FormatUint(res.Length(), 10)) } if _, ok := res.Output().(io.Reader); ok { @@ -268,12 +271,11 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req } h.Set(contentTypeHeader, mime) - h.Set(transferEncodingHeader, "chunked") // set 'allowed' headers - h.Set("Access-Control-Allow-Headers", "X-Stream-Output, X-Chunked-Output") + h.Set("Access-Control-Allow-Headers", AllowedExposedHeaders) // expose those headers - h.Set("Access-Control-Expose-Headers", "X-Stream-Output, X-Chunked-Output") + h.Set("Access-Control-Expose-Headers", AllowedExposedHeaders) if r.Method == "HEAD" { // after all the headers. return diff --git a/commands/http/handler_test.go b/commands/http/handler_test.go index e40fb72b5..d31e54e31 100644 --- a/commands/http/handler_test.go +++ b/commands/http/handler_test.go @@ -11,8 +11,6 @@ import ( coremock "github.com/ipfs/go-ipfs/core/mock" ) -const AllowedExposedHeaders = "X-Stream-Output, X-Chunked-Output" - func assertHeaders(t *testing.T, resHeaders http.Header, reqHeaders map[string]string) { for name, value := range reqHeaders { if resHeaders.Get(name) != value { diff --git a/test/sharness/t0110-gateway.sh b/test/sharness/t0110-gateway.sh index dacf4c4b8..46b25b7fa 100755 --- a/test/sharness/t0110-gateway.sh +++ b/test/sharness/t0110-gateway.sh @@ -91,6 +91,16 @@ test_expect_success "log output looks good" ' grep "log API client connected" log_out ' +test_expect_success "GET /api/v0/version succeeds" ' + curl -v "http://127.0.0.1:$apiport/api/v0/version" 2> version_out +' + +test_expect_success "output only has one transfer encoding header" ' + grep "Transfer-Encoding: chunked" version_out | wc -l > tecount_out && + echo " 1" > tecount_exp && + test_cmp tecount_out tecount_exp +' + test_expect_success "setup index hash" ' mkdir index && echo "

" > index/index.html && diff --git a/test/sharness/t0230-channel-streaming-http-content-type.sh b/test/sharness/t0230-channel-streaming-http-content-type.sh index f6e5a6a7f..2a95695e3 100755 --- a/test/sharness/t0230-channel-streaming-http-content-type.sh +++ b/test/sharness/t0230-channel-streaming-http-content-type.sh @@ -21,12 +21,11 @@ test_ls_cmd() { test_expect_success "Text encoded channel-streaming command output looks good" ' printf "HTTP/1.1 200 OK\r\n" >expected_output && - printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output && - printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output && + printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output, X-Content-Length\r\n" >>expected_output && + printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output, X-Content-Length\r\n" >>expected_output && printf "Content-Type: text/plain\r\n" >>expected_output && printf "Server: go-ipfs/%s\r\n" $(ipfs version -n) >>expected_output && printf "Trailer: X-Stream-Error\r\n" >>expected_output && - printf "Transfer-Encoding: chunked\r\n" >>expected_output && printf "X-Chunked-Output: 1\r\n" >>expected_output && printf "Transfer-Encoding: chunked\r\n" >>expected_output && printf "\r\n" >>expected_output && @@ -44,12 +43,11 @@ test_ls_cmd() { test_expect_success "JSON encoded channel-streaming command output looks good" ' printf "HTTP/1.1 200 OK\r\n" >expected_output && - printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output && - printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output && + printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output, X-Content-Length\r\n" >>expected_output && + printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output, X-Content-Length\r\n" >>expected_output && printf "Content-Type: application/json\r\n" >>expected_output && printf "Server: go-ipfs/%s\r\n" $(ipfs version -n) >>expected_output && printf "Trailer: X-Stream-Error\r\n" >>expected_output && - printf "Transfer-Encoding: chunked\r\n" >>expected_output && printf "X-Chunked-Output: 1\r\n" >>expected_output && printf "Transfer-Encoding: chunked\r\n" >>expected_output && printf "\r\n" >>expected_output && From 655804c5ef04855fbb9ea0a6e4f217117791cb07 Mon Sep 17 00:00:00 2001 From: Jeromy Johnson Date: Mon, 14 Mar 2016 16:36:31 -0700 Subject: [PATCH 2/2] fix whitespace trimming License: MIT Signed-off-by: Jeromy Johnson --- test/sharness/t0110-gateway.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/sharness/t0110-gateway.sh b/test/sharness/t0110-gateway.sh index 46b25b7fa..59887c734 100755 --- a/test/sharness/t0110-gateway.sh +++ b/test/sharness/t0110-gateway.sh @@ -96,8 +96,8 @@ test_expect_success "GET /api/v0/version succeeds" ' ' test_expect_success "output only has one transfer encoding header" ' - grep "Transfer-Encoding: chunked" version_out | wc -l > tecount_out && - echo " 1" > tecount_exp && + grep "Transfer-Encoding: chunked" version_out | wc -l | xargs echo > tecount_out && + echo "1" > tecount_exp && test_cmp tecount_out tecount_exp '