Skip to content

Commit

Permalink
fix double transfer encoding head problem
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy Johnson <why@ipfs.io>
  • Loading branch information
whyrusleeping committed Mar 14, 2016
1 parent 286a596 commit a1f037c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion commands/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 18 additions & 16 deletions commands/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions commands/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<p></p>" > index/index.html &&
Expand Down
10 changes: 4 additions & 6 deletions test/sharness/t0230-channel-streaming-http-content-type.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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 &&
Expand Down

0 comments on commit a1f037c

Please sign in to comment.