Live video on the AT Protocol

api: fixes for https redirect

See merge request aquareum-tv/aquareum!21

+93 -13
+6 -2
Makefile
··· 22 22 node: 23 23 go build -ldflags="-X 'main.Version=$(VERSION)'" -o $(OUT_DIR)/aquareum ./cmd/aquareum 24 24 25 + .PHONY: test 26 + test: 27 + go test ./pkg/... ./cmd/... 28 + 25 29 .PHONY: all 26 - all: version install check app node-all-platforms android 30 + all: version install check app test node-all-platforms android 27 31 28 32 .PHONY: ci 29 - ci: version install check app node-all-platforms ci-upload-node android ci-upload-android 33 + ci: version install check app test node-all-platforms ci-upload-node android ci-upload-android 30 34 31 35 .PHONY: android 32 36 android: app
+8 -4
go.mod
··· 4 4 5 5 require ( 6 6 github.com/adrg/xdg v0.4.0 7 - github.com/fatih/color v1.17.0 8 7 github.com/golang/glog v1.2.0 9 8 github.com/lmittmann/tint v1.0.4 10 9 github.com/orandin/slog-gorm v1.3.2 11 10 github.com/peterbourgon/ff/v3 v3.3.1 11 + github.com/stretchr/testify v1.9.0 12 12 golang.org/x/sync v0.6.0 13 13 gorm.io/driver/sqlite v1.5.5 14 14 ) ··· 16 16 require ( 17 17 github.com/jinzhu/inflection v1.0.0 // indirect 18 18 github.com/jinzhu/now v1.1.5 // indirect 19 - github.com/mattn/go-colorable v0.1.13 // indirect 20 - github.com/mattn/go-isatty v0.0.20 // indirect 19 + github.com/mattn/go-isatty v0.0.20 21 20 golang.org/x/sys v0.18.0 // indirect 22 21 gorm.io/gorm v1.25.9 23 22 ) 24 23 25 - require github.com/mattn/go-sqlite3 v1.14.22 // indirect 24 + require ( 25 + github.com/davecgh/go-spew v1.1.1 // indirect 26 + github.com/mattn/go-sqlite3 v1.14.22 // indirect 27 + github.com/pmezard/go-difflib v1.0.0 // indirect 28 + gopkg.in/yaml.v3 v3.0.1 // indirect 29 + )
+1 -6
go.sum
··· 3 3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 4 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 5 5 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 6 - github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= 7 - github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= 8 6 github.com/golang/glog v1.2.0 h1:uCdmnmatrKCgMBlM4rMuJZWOkPDqdbZPnrMXDY4gI68= 9 7 github.com/golang/glog v1.2.0/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w= 10 8 github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= ··· 15 13 github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 16 14 github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc= 17 15 github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE= 18 - github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 19 - github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 20 - github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 21 16 github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 22 17 github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 23 18 github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= ··· 35 30 golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= 36 31 golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 37 32 golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 38 - golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 39 33 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 40 34 golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= 41 35 golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 36 + gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 42 37 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 43 38 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 44 39 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+4 -1
pkg/api/api.go
··· 32 32 return nil, err 33 33 } 34 34 handleRedirect := func(w http.ResponseWriter, req *http.Request) { 35 - host, _, _ := net.SplitHostPort(req.Host) 35 + host, _, err := net.SplitHostPort(req.Host) 36 + if err != nil { 37 + host = req.Host 38 + } 36 39 u := req.URL 37 40 if tlsPort == "443" { 38 41 u.Host = host
+74
pkg/api/api_test.go
··· 1 + package api 2 + 3 + import ( 4 + "context" 5 + "net/http" 6 + "net/http/httptest" 7 + "testing" 8 + 9 + "aquareum.tv/aquareum/pkg/config" 10 + "aquareum.tv/aquareum/pkg/model" 11 + "github.com/stretchr/testify/assert" 12 + ) 13 + 14 + func TestRedirectHandler(t *testing.T) { 15 + tests := []struct { 16 + name string 17 + httpAddr string 18 + httpsAddr string 19 + requestURL string 20 + expectedURL string 21 + }{ 22 + { 23 + name: "default https port", 24 + httpAddr: "0.0.0.0:80", 25 + httpsAddr: "0.0.0.0:443", 26 + requestURL: "http://example.com/", 27 + expectedURL: "https://example.com/", 28 + }, 29 + { 30 + name: "non-default https port", 31 + httpAddr: "0.0.0.0:80", 32 + httpsAddr: "0.0.0.0:8443", 33 + requestURL: "http://example.com/", 34 + expectedURL: "https://example.com:8443/", 35 + }, 36 + { 37 + name: "non-default http port", 38 + httpAddr: "0.0.0.0:8080", 39 + httpsAddr: "0.0.0.0:443", 40 + requestURL: "http://example.com:8080/", 41 + expectedURL: "https://example.com/", 42 + }, 43 + { 44 + name: "non-default both", 45 + httpAddr: "0.0.0.0:8080", 46 + httpsAddr: "0.0.0.0:8443", 47 + requestURL: "http://example.com:8080/", 48 + expectedURL: "https://example.com:8443/", 49 + }, 50 + } 51 + 52 + for _, tt := range tests { 53 + t.Run(tt.name, func(t *testing.T) { 54 + cli := config.CLI{HttpAddr: tt.httpAddr, HttpsAddr: tt.httpsAddr} 55 + mod := model.DBModel{} 56 + 57 + handler, err := RedirectHandler(context.Background(), cli, &mod) 58 + assert.NoError(t, err, "RedirectHandler should not return an error") 59 + 60 + req := httptest.NewRequest("GET", tt.requestURL, nil) 61 + rr := httptest.NewRecorder() 62 + 63 + handler.ServeHTTP(rr, req) 64 + 65 + result := rr.Result() 66 + assert.Equal(t, http.StatusTemporaryRedirect, result.StatusCode, "handler returned wrong status code") 67 + 68 + redirectURL, err := result.Location() 69 + assert.NoError(t, err, "Failed to get redirect location") 70 + 71 + assert.Equal(t, tt.expectedURL, redirectURL.String(), "handler returned unexpected redirect URL") 72 + }) 73 + } 74 + }