You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Purell
  2. Purell is a tiny Go library to normalize URLs. It returns a pure URL. Pure-ell. Sanitizer and all. Yeah, I know...
  3. Based on the [wikipedia paper][wiki] and the [RFC 3986 document][rfc].
  4. [![build status](https://travis-ci.org/PuerkitoBio/purell.svg?branch=master)](http://travis-ci.org/PuerkitoBio/purell)
  5. ## Install
  6. `go get github.com/PuerkitoBio/purell`
  7. ## Changelog
  8. * **v1.1.1** : Fix failing test due to Go1.12 changes (thanks to @ianlancetaylor).
  9. * **2016-11-14 (v1.1.0)** : IDN: Conform to RFC 5895: Fold character width (thanks to @beeker1121).
  10. * **2016-07-27 (v1.0.0)** : Normalize IDN to ASCII (thanks to @zenovich).
  11. * **2015-02-08** : Add fix for relative paths issue ([PR #5][pr5]) and add fix for unnecessary encoding of reserved characters ([see issue #7][iss7]).
  12. * **v0.2.0** : Add benchmarks, Attempt IDN support.
  13. * **v0.1.0** : Initial release.
  14. ## Examples
  15. From `example_test.go` (note that in your code, you would import "github.com/PuerkitoBio/purell", and would prefix references to its methods and constants with "purell."):
  16. ```go
  17. package purell
  18. import (
  19. "fmt"
  20. "net/url"
  21. )
  22. func ExampleNormalizeURLString() {
  23. if normalized, err := NormalizeURLString("hTTp://someWEBsite.com:80/Amazing%3f/url/",
  24. FlagLowercaseScheme|FlagLowercaseHost|FlagUppercaseEscapes); err != nil {
  25. panic(err)
  26. } else {
  27. fmt.Print(normalized)
  28. }
  29. // Output: http://somewebsite.com:80/Amazing%3F/url/
  30. }
  31. func ExampleMustNormalizeURLString() {
  32. normalized := MustNormalizeURLString("hTTpS://someWEBsite.com:443/Amazing%fa/url/",
  33. FlagsUnsafeGreedy)
  34. fmt.Print(normalized)
  35. // Output: http://somewebsite.com/Amazing%FA/url
  36. }
  37. func ExampleNormalizeURL() {
  38. if u, err := url.Parse("Http://SomeUrl.com:8080/a/b/.././c///g?c=3&a=1&b=9&c=0#target"); err != nil {
  39. panic(err)
  40. } else {
  41. normalized := NormalizeURL(u, FlagsUsuallySafeGreedy|FlagRemoveDuplicateSlashes|FlagRemoveFragment)
  42. fmt.Print(normalized)
  43. }
  44. // Output: http://someurl.com:8080/a/c/g?c=3&a=1&b=9&c=0
  45. }
  46. ```
  47. ## API
  48. As seen in the examples above, purell offers three methods, `NormalizeURLString(string, NormalizationFlags) (string, error)`, `MustNormalizeURLString(string, NormalizationFlags) (string)` and `NormalizeURL(*url.URL, NormalizationFlags) (string)`. They all normalize the provided URL based on the specified flags. Here are the available flags:
  49. ```go
  50. const (
  51. // Safe normalizations
  52. FlagLowercaseScheme NormalizationFlags = 1 << iota // HTTP://host -> http://host, applied by default in Go1.1
  53. FlagLowercaseHost // http://HOST -> http://host
  54. FlagUppercaseEscapes // http://host/t%ef -> http://host/t%EF
  55. FlagDecodeUnnecessaryEscapes // http://host/t%41 -> http://host/tA
  56. FlagEncodeNecessaryEscapes // http://host/!"#$ -> http://host/%21%22#$
  57. FlagRemoveDefaultPort // http://host:80 -> http://host
  58. FlagRemoveEmptyQuerySeparator // http://host/path? -> http://host/path
  59. // Usually safe normalizations
  60. FlagRemoveTrailingSlash // http://host/path/ -> http://host/path
  61. FlagAddTrailingSlash // http://host/path -> http://host/path/ (should choose only one of these add/remove trailing slash flags)
  62. FlagRemoveDotSegments // http://host/path/./a/b/../c -> http://host/path/a/c
  63. // Unsafe normalizations
  64. FlagRemoveDirectoryIndex // http://host/path/index.html -> http://host/path/
  65. FlagRemoveFragment // http://host/path#fragment -> http://host/path
  66. FlagForceHTTP // https://host -> http://host
  67. FlagRemoveDuplicateSlashes // http://host/path//a///b -> http://host/path/a/b
  68. FlagRemoveWWW // http://www.host/ -> http://host/
  69. FlagAddWWW // http://host/ -> http://www.host/ (should choose only one of these add/remove WWW flags)
  70. FlagSortQuery // http://host/path?c=3&b=2&a=1&b=1 -> http://host/path?a=1&b=1&b=2&c=3
  71. // Normalizations not in the wikipedia article, required to cover tests cases
  72. // submitted by jehiah
  73. FlagDecodeDWORDHost // http://1113982867 -> http://66.102.7.147
  74. FlagDecodeOctalHost // http://0102.0146.07.0223 -> http://66.102.7.147
  75. FlagDecodeHexHost // http://0x42660793 -> http://66.102.7.147
  76. FlagRemoveUnnecessaryHostDots // http://.host../path -> http://host/path
  77. FlagRemoveEmptyPortSeparator // http://host:/path -> http://host/path
  78. // Convenience set of safe normalizations
  79. FlagsSafe NormalizationFlags = FlagLowercaseHost | FlagLowercaseScheme | FlagUppercaseEscapes | FlagDecodeUnnecessaryEscapes | FlagEncodeNecessaryEscapes | FlagRemoveDefaultPort | FlagRemoveEmptyQuerySeparator
  80. // For convenience sets, "greedy" uses the "remove trailing slash" and "remove www. prefix" flags,
  81. // while "non-greedy" uses the "add (or keep) the trailing slash" and "add www. prefix".
  82. // Convenience set of usually safe normalizations (includes FlagsSafe)
  83. FlagsUsuallySafeGreedy NormalizationFlags = FlagsSafe | FlagRemoveTrailingSlash | FlagRemoveDotSegments
  84. FlagsUsuallySafeNonGreedy NormalizationFlags = FlagsSafe | FlagAddTrailingSlash | FlagRemoveDotSegments
  85. // Convenience set of unsafe normalizations (includes FlagsUsuallySafe)
  86. FlagsUnsafeGreedy NormalizationFlags = FlagsUsuallySafeGreedy | FlagRemoveDirectoryIndex | FlagRemoveFragment | FlagForceHTTP | FlagRemoveDuplicateSlashes | FlagRemoveWWW | FlagSortQuery
  87. FlagsUnsafeNonGreedy NormalizationFlags = FlagsUsuallySafeNonGreedy | FlagRemoveDirectoryIndex | FlagRemoveFragment | FlagForceHTTP | FlagRemoveDuplicateSlashes | FlagAddWWW | FlagSortQuery
  88. // Convenience set of all available flags
  89. FlagsAllGreedy = FlagsUnsafeGreedy | FlagDecodeDWORDHost | FlagDecodeOctalHost | FlagDecodeHexHost | FlagRemoveUnnecessaryHostDots | FlagRemoveEmptyPortSeparator
  90. FlagsAllNonGreedy = FlagsUnsafeNonGreedy | FlagDecodeDWORDHost | FlagDecodeOctalHost | FlagDecodeHexHost | FlagRemoveUnnecessaryHostDots | FlagRemoveEmptyPortSeparator
  91. )
  92. ```
  93. For convenience, the set of flags `FlagsSafe`, `FlagsUsuallySafe[Greedy|NonGreedy]`, `FlagsUnsafe[Greedy|NonGreedy]` and `FlagsAll[Greedy|NonGreedy]` are provided for the similarly grouped normalizations on [wikipedia's URL normalization page][wiki]. You can add (using the bitwise OR `|` operator) or remove (using the bitwise AND NOT `&^` operator) individual flags from the sets if required, to build your own custom set.
  94. The [full godoc reference is available on gopkgdoc][godoc].
  95. Some things to note:
  96. * `FlagDecodeUnnecessaryEscapes`, `FlagEncodeNecessaryEscapes`, `FlagUppercaseEscapes` and `FlagRemoveEmptyQuerySeparator` are always implicitly set, because internally, the URL string is parsed as an URL object, which automatically decodes unnecessary escapes, uppercases and encodes necessary ones, and removes empty query separators (an unnecessary `?` at the end of the url). So this operation cannot **not** be done. For this reason, `FlagRemoveEmptyQuerySeparator` (as well as the other three) has been included in the `FlagsSafe` convenience set, instead of `FlagsUnsafe`, where Wikipedia puts it.
  97. * The `FlagDecodeUnnecessaryEscapes` decodes the following escapes (*from -> to*):
  98. - %24 -> $
  99. - %26 -> &
  100. - %2B-%3B -> +,-./0123456789:;
  101. - %3D -> =
  102. - %40-%5A -> @ABCDEFGHIJKLMNOPQRSTUVWXYZ
  103. - %5F -> _
  104. - %61-%7A -> abcdefghijklmnopqrstuvwxyz
  105. - %7E -> ~
  106. * When the `NormalizeURL` function is used (passing an URL object), this source URL object is modified (that is, after the call, the URL object will be modified to reflect the normalization).
  107. * The *replace IP with domain name* normalization (`http://208.77.188.166/ → http://www.example.com/`) is obviously not possible for a library without making some network requests. This is not implemented in purell.
  108. * The *remove unused query string parameters* and *remove default query parameters* are also not implemented, since this is a very case-specific normalization, and it is quite trivial to do with an URL object.
  109. ### Safe vs Usually Safe vs Unsafe
  110. Purell allows you to control the level of risk you take while normalizing an URL. You can aggressively normalize, play it totally safe, or anything in between.
  111. Consider the following URL:
  112. `HTTPS://www.RooT.com/toto/t%45%1f///a/./b/../c/?z=3&w=2&a=4&w=1#invalid`
  113. Normalizing with the `FlagsSafe` gives:
  114. `https://www.root.com/toto/tE%1F///a/./b/../c/?z=3&w=2&a=4&w=1#invalid`
  115. With the `FlagsUsuallySafeGreedy`:
  116. `https://www.root.com/toto/tE%1F///a/c?z=3&w=2&a=4&w=1#invalid`
  117. And with `FlagsUnsafeGreedy`:
  118. `http://root.com/toto/tE%1F/a/c?a=4&w=1&w=2&z=3`
  119. ## TODOs
  120. * Add a class/default instance to allow specifying custom directory index names? At the moment, removing directory index removes `(^|/)((?:default|index)\.\w{1,4})$`.
  121. ## Thanks / Contributions
  122. @rogpeppe
  123. @jehiah
  124. @opennota
  125. @pchristopher1275
  126. @zenovich
  127. @beeker1121
  128. ## License
  129. The [BSD 3-Clause license][bsd].
  130. [bsd]: http://opensource.org/licenses/BSD-3-Clause
  131. [wiki]: http://en.wikipedia.org/wiki/URL_normalization
  132. [rfc]: http://tools.ietf.org/html/rfc3986#section-6
  133. [godoc]: http://go.pkgdoc.org/github.com/PuerkitoBio/purell
  134. [pr5]: https://github.com/PuerkitoBio/purell/pull/5
  135. [iss7]: https://github.com/PuerkitoBio/purell/issues/7