Skip to content

Commit 571ef8d

Browse files
authored
handler: Remove default ports in Location headers (#1226)
* handler: Remove default ports in Location headers * Update nginx.conf * Update nginx.conf
1 parent fb5b620 commit 571ef8d

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

examples/nginx.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ server {
5757
proxy_http_version 1.1;
5858

5959
# Add X-Forwarded-* headers
60-
proxy_set_header X-Forwarded-Host $host;
60+
proxy_set_header X-Forwarded-Host $host:$server_port;
6161
proxy_set_header X-Forwarded-Proto $scheme;
6262

6363
proxy_set_header Upgrade $http_upgrade;

pkg/handler/post_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,45 @@ func TestPost(t *testing.T) {
394394
},
395395
}).Run(handler, t)
396396
})
397+
398+
SubTest(t, "RemoveDefaultPorts", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
399+
ctrl := gomock.NewController(t)
400+
defer ctrl.Finish()
401+
upload := NewMockFullUpload(ctrl)
402+
403+
gomock.InOrder(
404+
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
405+
Size: 300,
406+
MetaData: map[string]string{},
407+
}).Return(upload, nil),
408+
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
409+
ID: "foo",
410+
Size: 300,
411+
MetaData: map[string]string{},
412+
}, nil),
413+
)
414+
415+
handler, _ := NewHandler(Config{
416+
StoreComposer: composer,
417+
BasePath: "/files/",
418+
RespectForwardedHeaders: true,
419+
})
420+
421+
(&httpTest{
422+
Method: "POST",
423+
ReqHeader: map[string]string{
424+
"Tus-Resumable": "1.0.0",
425+
"Upload-Length": "300",
426+
"X-Forwarded-Host": "upload.example.tld:443",
427+
"X-Forwarded-Proto": "https",
428+
},
429+
Code: http.StatusCreated,
430+
ResHeader: map[string]string{
431+
// No :443 in the Location header
432+
"Location": "https://upload.example.tld/files/foo",
433+
},
434+
}).Run(handler, t)
435+
})
397436
})
398437

399438
SubTest(t, "WithUpload", func(t *testing.T, store *MockFullDataStore, _ *StoreComposer) {

pkg/handler/unrouted_handler.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,14 @@ func getHostAndProtocol(r *http.Request, allowForwarded bool) (host, proto strin
13231323
}
13241324
}
13251325

1326+
// Remove default ports
1327+
if proto == "http" {
1328+
host = strings.TrimSuffix(host, ":80")
1329+
}
1330+
if proto == "https" {
1331+
host = strings.TrimSuffix(host, ":443")
1332+
}
1333+
13261334
return
13271335
}
13281336

0 commit comments

Comments
 (0)