aboutsummaryrefslogtreecommitdiff
path: root/uri.go
diff options
context:
space:
mode:
authorGravatar Erik Dubbelboer <erik@dubbelboer.com> 2019-08-18 11:23:33 +0200
committerGravatar GitHub <noreply@github.com> 2019-08-18 11:23:33 +0200
commit2edabf3b76473af8d82b4a746ae8f3f6fe31dca8 (patch)
treed029744a9046e707b26b881e7cae3c98014058bc /uri.go
parentCorrectly handle `NoDefaultContentType` without setting an `Content-Type` val... (diff)
downloadfasthttp-2edabf3b76473af8d82b4a746ae8f3f6fe31dca8.tar.gz
fasthttp-2edabf3b76473af8d82b4a746ae8f3f6fe31dca8.tar.bz2
fasthttp-2edabf3b76473af8d82b4a746ae8f3f6fe31dca8.zip
Add support for user:pass in URLs (#614)
Fixes #609
Diffstat (limited to 'uri.go')
-rw-r--r--uri.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/uri.go b/uri.go
index d536f59..e9cd4b1 100644
--- a/uri.go
+++ b/uri.go
@@ -51,6 +51,9 @@ type URI struct {
fullURI []byte
requestURI []byte
+ username []byte
+ password []byte
+
h *RequestHeader
}
@@ -63,6 +66,8 @@ func (u *URI) CopyTo(dst *URI) {
dst.queryString = append(dst.queryString[:0], u.queryString...)
dst.hash = append(dst.hash[:0], u.hash...)
dst.host = append(dst.host[:0], u.host...)
+ dst.username = append(dst.username[:0], u.username...)
+ dst.password = append(dst.password[:0], u.password...)
u.queryArgs.CopyTo(&dst.queryArgs)
dst.parsedQueryArgs = u.parsedQueryArgs
@@ -89,6 +94,36 @@ func (u *URI) SetHashBytes(hash []byte) {
u.hash = append(u.hash[:0], hash...)
}
+// Username returns URI username
+func (u *URI) Username() []byte {
+ return u.username
+}
+
+// SetUsername sets URI username.
+func (u *URI) SetUsername(username string) {
+ u.username = append(u.username[:0], username...)
+}
+
+// SetUsernameBytes sets URI username.
+func (u *URI) SetUsernameBytes(username []byte) {
+ u.username = append(u.username[:0], username...)
+}
+
+// Password returns URI password
+func (u *URI) Password() []byte {
+ return u.password
+}
+
+// SetPassword sets URI password.
+func (u *URI) SetPassword(password string) {
+ u.password = append(u.password[:0], password...)
+}
+
+// SetPasswordBytes sets URI password.
+func (u *URI) SetPasswordBytes(password []byte) {
+ u.password = append(u.password[:0], password...)
+}
+
// QueryString returns URI query string,
// i.e. baz=123 of http://aaa.com/foo/bar?baz=123#qwe .
//
@@ -174,6 +209,8 @@ func (u *URI) Reset() {
u.path = u.path[:0]
u.queryString = u.queryString[:0]
u.hash = u.hash[:0]
+ u.username = u.username[:0]
+ u.password = u.password[:0]
u.host = u.host[:0]
u.queryArgs.Reset()
@@ -236,6 +273,20 @@ func (u *URI) parse(host, uri []byte, h *RequestHeader) {
scheme, host, uri := splitHostURI(host, uri)
u.scheme = append(u.scheme, scheme...)
lowercaseBytes(u.scheme)
+
+ if n := bytes.Index(host, strAt); n >= 0 {
+ auth := host[:n]
+ host = host[n+1:]
+
+ if n := bytes.Index(auth, strColon); n >= 0 {
+ u.username = auth[:n]
+ u.password = auth[n+1:]
+ } else {
+ u.username = auth
+ u.password = auth[:0] // Make sure it's not nil
+ }
+ }
+
u.host = append(u.host, host...)
lowercaseBytes(u.host)