mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-26 15:42:21 +08:00
Merge pull request #2465 from ipfs/fix/double-encoding-header
fix double transfer encoding header problem
This commit is contained in:
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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 | xargs echo > 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 &&
|
||||
|
@ -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 &&
|
||||
|
Reference in New Issue
Block a user