| 1 | package util |
| 2 | |
| 3 | import "net/url" |
| 4 | |
| 5 | // URLUserPassword creates a url.Userinfo from user and password strings. |
| 6 | // |
| 7 | // Unlike url.UserPassword, this function treats empty strings as "not specified": |
| 8 | // - If password is non-empty, returns url.UserPassword(user, password). |
| 9 | // - If password is empty but user is non-empty, returns url.User(user). |
| 10 | // - If both are empty, returns nil (which serializes to "" in url.URL). |
| 11 | // |
| 12 | // Parameters: |
| 13 | // - user: The username, or empty string if no authentication. |
| 14 | // - password: The password, or empty string if no password. |
| 15 | // |
| 16 | // Returns: |
| 17 | // - A *url.Userinfo with the appropriate authentication info, or nil if neither user nor password is provided. |
| 18 | func URLUserPassword(user, password string) *url.Userinfo { |
| 19 | if password != "" { |
| 20 | return url.UserPassword(user, password) |
| 21 | } else if user != "" { |
| 22 | return url.User(user) |
| 23 | } |
| 24 | |
| 25 | return nil |
| 26 | } |