@cryptotaxi247 / kubo / commits / 850602361

fix double transfer encoding head problem

License: MIT Signed-off-by: Jeromy Johnson <why@ipfs.io>

Jeromy Johnson committed Mar 14, 2016 at 16:11 UTC 850602361d8dae4e4b52e8b17b90589d57c47c18
5 files changed +33 -25
commands/http/client.go
+1 -1
@@ -159,7 +159,7 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
159 contentType := httpRes.Header.Get(contentTypeHeader)
160 contentType = strings.Split(contentType, ";")[0]
161
162 - lengthHeader := httpRes.Header.Get(contentLengthHeader)
162 + lengthHeader := httpRes.Header.Get(extraContentLengthHeader)
163 if len(lengthHeader) > 0 {
164 length, err := strconv.ParseUint(lengthHeader, 10, 64)
165 if err != nil {
commands/http/handler.go
+18 -16
@@ -41,20 +41,23 @@ var (
41 )
42
43 const (
44 - StreamErrHeader = "X-Stream-Error"
45 - streamHeader = "X-Stream-Output"
46 - channelHeader = "X-Chunked-Output"
47 - uaHeader = "User-Agent"
48 - contentTypeHeader = "Content-Type"
49 - contentLengthHeader = "Content-Length"
50 - contentDispHeader = "Content-Disposition"
51 - transferEncodingHeader = "Transfer-Encoding"
52 - applicationJson = "application/json"
53 - applicationOctetStream = "application/octet-stream"
54 - plainText = "text/plain"
55 - originHeader = "origin"
44 + StreamErrHeader = "X-Stream-Error"
45 + streamHeader = "X-Stream-Output"
46 + channelHeader = "X-Chunked-Output"
47 + extraContentLengthHeader = "X-Content-Length"
48 + uaHeader = "User-Agent"
49 + contentTypeHeader = "Content-Type"
50 + contentDispHeader = "Content-Disposition"
51 + transferEncodingHeader = "Transfer-Encoding"
52 + applicationJson = "application/json"
53 + applicationOctetStream = "application/octet-stream"
54 + plainText = "text/plain"
55 + originHeader = "origin"
56 )
57
58 +var AllowedExposedHeadersArr = []string{streamHeader, channelHeader, extraContentLengthHeader}
59 +var AllowedExposedHeaders = strings.Join(AllowedExposedHeadersArr, ", ")
60 +
61 const (
62 ACAOrigin = "Access-Control-Allow-Origin"
63 ACAMethods = "Access-Control-Allow-Methods"
@@ -241,7 +244,7 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
244 h.Set("Trailer", StreamErrHeader)
245
246 if res.Length() > 0 {
244 - h.Set(contentLengthHeader, strconv.FormatUint(res.Length(), 10))
247 + h.Set("X-Content-Length", strconv.FormatUint(res.Length(), 10))
248 }
249
250 if _, ok := res.Output().(io.Reader); ok {
@@ -268,12 +271,11 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
271 }
272
273 h.Set(contentTypeHeader, mime)
271 - h.Set(transferEncodingHeader, "chunked")
274
275 // set 'allowed' headers
274 - h.Set("Access-Control-Allow-Headers", "X-Stream-Output, X-Chunked-Output")
276 + h.Set("Access-Control-Allow-Headers", AllowedExposedHeaders)
277 // expose those headers
276 - h.Set("Access-Control-Expose-Headers", "X-Stream-Output, X-Chunked-Output")
278 + h.Set("Access-Control-Expose-Headers", AllowedExposedHeaders)
279
280 if r.Method == "HEAD" { // after all the headers.
281 return
commands/http/handler_test.go
-2
@@ -11,8 +11,6 @@ import (
11 coremock "github.com/ipfs/go-ipfs/core/mock"
12 )
13
14 -const AllowedExposedHeaders = "X-Stream-Output, X-Chunked-Output"
15 -
14 func assertHeaders(t *testing.T, resHeaders http.Header, reqHeaders map[string]string) {
15 for name, value := range reqHeaders {
16 if resHeaders.Get(name) != value {
test/sharness/t0110-gateway.sh
+10
@@ -91,6 +91,16 @@ test_expect_success "log output looks good" '
91 grep "log API client connected" log_out
92 '
93
94 +test_expect_success "GET /api/v0/version succeeds" '
95 + curl -v "http://127.0.0.1:$apiport/api/v0/version" 2> version_out
96 +'
97 +
98 +test_expect_success "output only has one transfer encoding header" '
99 + grep "Transfer-Encoding: chunked" version_out | wc -l > tecount_out &&
100 + echo " 1" > tecount_exp &&
101 + test_cmp tecount_out tecount_exp
102 +'
103 +
104 test_expect_success "setup index hash" '
105 mkdir index &&
106 echo "<p></p>" > index/index.html &&
test/sharness/t0230-channel-streaming-http-content-type.sh
+4 -6
@@ -21,12 +21,11 @@ test_ls_cmd() {
21
22 test_expect_success "Text encoded channel-streaming command output looks good" '
23 printf "HTTP/1.1 200 OK\r\n" >expected_output &&
24 - printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output &&
25 - printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output &&
24 + printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output, X-Content-Length\r\n" >>expected_output &&
25 + printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output, X-Content-Length\r\n" >>expected_output &&
26 printf "Content-Type: text/plain\r\n" >>expected_output &&
27 printf "Server: go-ipfs/%s\r\n" $(ipfs version -n) >>expected_output &&
28 printf "Trailer: X-Stream-Error\r\n" >>expected_output &&
29 - printf "Transfer-Encoding: chunked\r\n" >>expected_output &&
29 printf "X-Chunked-Output: 1\r\n" >>expected_output &&
30 printf "Transfer-Encoding: chunked\r\n" >>expected_output &&
31 printf "\r\n" >>expected_output &&
@@ -44,12 +43,11 @@ test_ls_cmd() {
43
44 test_expect_success "JSON encoded channel-streaming command output looks good" '
45 printf "HTTP/1.1 200 OK\r\n" >expected_output &&
47 - printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output &&
48 - printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output &&
46 + printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output, X-Content-Length\r\n" >>expected_output &&
47 + printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output, X-Content-Length\r\n" >>expected_output &&
48 printf "Content-Type: application/json\r\n" >>expected_output &&
49 printf "Server: go-ipfs/%s\r\n" $(ipfs version -n) >>expected_output &&
50 printf "Trailer: X-Stream-Error\r\n" >>expected_output &&
52 - printf "Transfer-Encoding: chunked\r\n" >>expected_output &&
51 printf "X-Chunked-Output: 1\r\n" >>expected_output &&
52 printf "Transfer-Encoding: chunked\r\n" >>expected_output &&
53 printf "\r\n" >>expected_output &&