Skip to content

Commit

Permalink
handler: Remove default ports in Location headers (#1226)
Browse files Browse the repository at this point in the history
* handler: Remove default ports in Location headers

* Update nginx.conf

* Update nginx.conf
  • Loading branch information
Acconut authored Dec 20, 2024
1 parent fb5b620 commit 571ef8d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ server {
proxy_http_version 1.1;

# Add X-Forwarded-* headers
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Proto $scheme;

proxy_set_header Upgrade $http_upgrade;
Expand Down
39 changes: 39 additions & 0 deletions pkg/handler/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,45 @@ func TestPost(t *testing.T) {
},
}).Run(handler, t)
})

SubTest(t, "RemoveDefaultPorts", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)

gomock.InOrder(
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return(upload, nil),
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{},
}, nil),
)

handler, _ := NewHandler(Config{
StoreComposer: composer,
BasePath: "/files/",
RespectForwardedHeaders: true,
})

(&httpTest{
Method: "POST",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Upload-Length": "300",
"X-Forwarded-Host": "upload.example.tld:443",
"X-Forwarded-Proto": "https",
},
Code: http.StatusCreated,
ResHeader: map[string]string{
// No :443 in the Location header
"Location": "https://upload.example.tld/files/foo",
},
}).Run(handler, t)
})
})

SubTest(t, "WithUpload", func(t *testing.T, store *MockFullDataStore, _ *StoreComposer) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/handler/unrouted_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,14 @@ func getHostAndProtocol(r *http.Request, allowForwarded bool) (host, proto strin
}
}

// Remove default ports
if proto == "http" {
host = strings.TrimSuffix(host, ":80")
}
if proto == "https" {
host = strings.TrimSuffix(host, ":443")
}

return
}

Expand Down

0 comments on commit 571ef8d

Please sign in to comment.