Skip to content

Commit

Permalink
Adding websocket|ftp scheme (#302)
Browse files Browse the repository at this point in the history
* adding websocket scheme

* adding ftp
  • Loading branch information
Mzack9999 authored Jan 7, 2024
1 parent d9ae0e4 commit 0fb36f1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
10 changes: 9 additions & 1 deletion url/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,15 @@ func absoluteURLParser(u *URL) (*URL, error) {
return u, nil
}
// Try to parse host related input
if stringsutil.HasPrefixAny(u.Original, HTTP+SchemeSeparator, HTTPS+SchemeSeparator, "//") {
allowedSchemes := []string{
HTTP + SchemeSeparator,
HTTPS + SchemeSeparator,
WEBSOCKET + SchemeSeparator,
WEBSOCKET_SSL + SchemeSeparator,
FTP + SchemeSeparator,
"//",
}
if stringsutil.HasPrefixAny(u.Original, allowedSchemes...) {
u.IsRelative = false
urlparse, parseErr := url.Parse(u.Original)
if parseErr != nil {
Expand Down
7 changes: 7 additions & 0 deletions url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func TestParse(t *testing.T) {
require.Equal(t, "127.0.0.1", U.Hostname(), "different host")
require.Equal(t, "a", U.Fragment, "different fragment")
require.Equal(t, "http://127.0.0.1/#a", U.String(), "different full url")

// websocket
U, err = Parse("wss://127.0.0.1")
require.Nil(t, err, "could not parse url")
require.Equal(t, "wss", U.Scheme, "different scheme")
require.Equal(t, "127.0.0.1", U.Hostname(), "different host")
require.Equal(t, "wss://127.0.0.1", U.String(), "different full url")
}

func TestClone(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions url/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ import (
)

const (
HTTP = "http"
HTTPS = "https"
HTTP = "http"
HTTPS = "https"

// Deny all protocols
// Allow:
// websocket + websocket over ssl
WEBSOCKET = "ws"
WEBSOCKET_SSL = "wss"
FTP = "ftp"

SchemeSeparator = "://"
DefaultHTTPPort = "80"
DefaultHTTPSPort = "443"
Expand Down

0 comments on commit 0fb36f1

Please sign in to comment.