Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use net/url .String() #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Based on the [wikipedia paper][wiki] and the [RFC 3986 document][rfc].

## Changelog

* **v1.2.0** : Switch to `net/url` url escaping.
* **v1.1.1** : Fix failing test due to Go1.12 changes (thanks to @ianlancetaylor).
* **2016-11-14 (v1.1.0)** : IDN: Conform to RFC 5895: Fold character width (thanks to @beeker1121).
* **2016-07-27 (v1.0.0)** : Normalize IDN to ASCII (thanks to @zenovich).
Expand Down
18 changes: 12 additions & 6 deletions purell.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ var rxHostInteriorDots = regexp.MustCompile(`\.+`)
var rxEmptyPort = regexp.MustCompile(`:+$`)

// Map of flags to implementation function.
// FlagDecodeUnnecessaryEscapes has no action, since it is done automatically
// by parsing the string as an URL. Same for FlagUppercaseEscapes and FlagRemoveEmptyQuerySeparator.
// FlagDecodeUnnecessaryEscapes, FlagEncodeNecessaryEscapes, and FlagUppercaseEscapes
// have no action, since they are done automatically by parsing the string as an URL.

// Since maps have undefined traversing order, make a slice of ordered keys
var flagsOrder = []NormalizationFlags{
Expand All @@ -105,6 +105,7 @@ var flagsOrder = []NormalizationFlags{
FlagRemoveDuplicateSlashes,
FlagRemoveWWW,
FlagAddWWW,
FlagRemoveEmptyQuerySeparator,
FlagSortQuery,
FlagDecodeDWORDHost,
FlagDecodeOctalHost,
Expand All @@ -127,6 +128,7 @@ var flags = map[NormalizationFlags]func(*url.URL){
FlagRemoveDuplicateSlashes: removeDuplicateSlashes,
FlagRemoveWWW: removeWWW,
FlagAddWWW: addWWW,
FlagRemoveEmptyQuerySeparator: removeEmptyQuerySeparator,
FlagSortQuery: sortQuery,
FlagDecodeDWORDHost: decodeDWORDHost,
FlagDecodeOctalHost: decodeOctalHost,
Expand Down Expand Up @@ -175,12 +177,14 @@ func NormalizeURLString(u string, f NormalizationFlags) (string, error) {
// NormalizeURL returns the normalized string.
// It takes a parsed URL object as input, as well as the normalization flags.
func NormalizeURL(u *url.URL, f NormalizationFlags) string {
u.RawPath = ""
for _, k := range flagsOrder {
if f&k == k {
flags[k](u)
}
}
return escapeURL(u)

return u.String()
}

func lowercaseScheme(u *url.URL) {
Expand Down Expand Up @@ -225,9 +229,7 @@ func addTrailingSlash(u *url.URL) {
u.Path += "/"
}
} else if l = len(u.Host); l > 0 {
if !strings.HasSuffix(u.Host, "/") {
u.Host += "/"
}
u.Path = "/"
}
}

Expand Down Expand Up @@ -293,6 +295,10 @@ func addWWW(u *url.URL) {
}
}

func removeEmptyQuerySeparator(u *url.URL) {
u.ForceQuery = false
}

func sortQuery(u *url.URL) {
q := u.Query()

Expand Down
8 changes: 6 additions & 2 deletions purell_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package purell

import (
"fmt"
"net/url"
"testing"
"unicode"
)

type testCase struct {
Expand Down Expand Up @@ -748,6 +746,11 @@ func runCase(tc *testCase, t *testing.T) {
}
}

/*
* NOTE: these tests as written do not test URL path escaping, since they include chars
* read as query strings &c. We recommend using the default net/url escaping code; if you
* need something more permissive, you'll need to rely on urlesc.

func TestDecodeUnnecessaryEscapesAll(t *testing.T) {
var url = "http://host/"

Expand Down Expand Up @@ -787,3 +790,4 @@ func TestEncodeNecessaryEscapesAll(t *testing.T) {
t.Errorf("EncodeNecessaryEscapesAll:\nwant\n%s\ngot\n%s", want, s)
}
}
*/
174 changes: 0 additions & 174 deletions urlesc.go

This file was deleted.

Loading