Skip to content

Commit

Permalink
chore: add location header
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyjoygh committed Aug 18, 2024
1 parent 80548aa commit a3943f5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"math/big"
"net/mail"
"net/url"
"strconv"
"strings"
)
Expand Down Expand Up @@ -55,3 +56,9 @@ func RandomString(n int) (string, error) {
}
return sb.String(), nil
}

// ValidateURI validates the URI.
func ValidateURI(uri string) bool {
u, err := url.Parse(uri)
return err == nil && u.Scheme != "" && u.Host != ""
}
30 changes: 30 additions & 0 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,33 @@ func TestValidateEmail(t *testing.T) {
}
}
}

func TestValidateURI(t *testing.T) {
tests := []struct {
uri string
want bool
}{
{
uri: "https://localhsot:3000",
want: true,
},
{
uri: "https://yourselfhosted.com",
want: true,
},
{
uri: "google.com",
want: false,
},
{
uri: "i don't know",
want: false,
},
}
for _, test := range tests {
result := ValidateURI(test.uri)
if result != test.want {
t.Errorf("Validate URI %s: got result %v, want %v.", test.uri, result, test.want)
}
}
}
6 changes: 5 additions & 1 deletion server/route/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ func (s *FrontendService) registerRoutes(e *echo.Echo) {
}

metric.Enqueue("shortcut view")
// Only set the `Location` header if the link is a valid URI.
if util.ValidateURI(shortcut.Link) {
c.Response().Header().Set("Location", shortcut.Link)
}
// Inject shortcut metadata into `index.html`.
indexHTML := strings.ReplaceAll(rawIndexHTML, headerMetadataPlaceholder, generateShortcutMetadata(shortcut).String())
return c.HTML(http.StatusOK, indexHTML)
return c.HTML(http.StatusSeeOther, indexHTML)
})

e.GET("/c/:collectionName", func(c echo.Context) error {
Expand Down

0 comments on commit a3943f5

Please sign in to comment.