Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "net/url"
  6. )
  7.  
  8. func main() {
  9.  
  10. var Url *url.URL
  11. Url, err := url.Parse("http://www.example.com")
  12. if err != nil {
  13. panic("boom")
  14. }
  15.  
  16. Url.Path += "/some/path/or/other_with_funny_characters?_or_not/"
  17. parameters := url.Values{}
  18. parameters.Add("hello", "42")
  19. parameters.Add("hello", "54")
  20. parameters.Add("vegetable", "potato")
  21. Url.RawQuery = parameters.Encode()
  22.  
  23. fmt.Printf("Encoded URL is %qn", Url.String())
  24. }
  25.  
  26. Encoded URL is "http://www.example.com/some/path/or/other_with_funny_characters%3F_or_not/?vegetable=potato&hello=42&hello=54"
  27.  
  28. function fixedEncodeURIComponent (str) {
  29. return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/*/g, "%2A");
  30. }
  31.  
  32. import "net/url"
  33.  
  34. // UrlEncoded encodes a string like Javascript's encodeURIComponent()
  35. func UrlEncoded(str string) (string, error) {
  36. u, err := url.Parse(str)
  37. if err != nil {
  38. return "", err
  39. }
  40. return u.String(), nil
  41. }
  42.  
  43. template.URLQueryEscaper(path)
  44.  
  45. package main
  46.  
  47.  
  48. import (
  49. "fmt"
  50. "strconv"
  51. )
  52.  
  53.  
  54. const (
  55. encodePath encoding = 1 + iota
  56. encodeHost
  57. encodeUserPassword
  58. encodeQueryComponent
  59. encodeFragment
  60. )
  61.  
  62. type encoding int
  63. type EscapeError string
  64.  
  65. func (e EscapeError) Error() string {
  66. return "invalid URL escape " + strconv.Quote(string(e))
  67. }
  68.  
  69.  
  70. func ishex(c byte) bool {
  71. switch {
  72. case '0' <= c && c <= '9':
  73. return true
  74. case 'a' <= c && c <= 'f':
  75. return true
  76. case 'A' <= c && c <= 'F':
  77. return true
  78. }
  79. return false
  80. }
  81.  
  82. func unhex(c byte) byte {
  83. switch {
  84. case '0' <= c && c <= '9':
  85. return c - '0'
  86. case 'a' <= c && c <= 'f':
  87. return c - 'a' + 10
  88. case 'A' <= c && c <= 'F':
  89. return c - 'A' + 10
  90. }
  91. return 0
  92. }
  93.  
  94.  
  95.  
  96. // Return true if the specified character should be escaped when
  97. // appearing in a URL string, according to RFC 3986.
  98. //
  99. // Please be informed that for now shouldEscape does not check all
  100. // reserved characters correctly. See golang.org/issue/5684.
  101. func shouldEscape(c byte, mode encoding) bool {
  102. // §2.3 Unreserved characters (alphanum)
  103. if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' {
  104. return false
  105. }
  106.  
  107. if mode == encodeHost {
  108. // §3.2.2 Host allows
  109. // sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
  110. // as part of reg-name.
  111. // We add : because we include :port as part of host.
  112. // We add [ ] because we include [ipv6]:port as part of host
  113. switch c {
  114. case '!', '$', '&', ''', '(', ')', '*', '+', ',', ';', '=', ':', '[', ']':
  115. return false
  116. }
  117. }
  118.  
  119. switch c {
  120. case '-', '_', '.', '~': // §2.3 Unreserved characters (mark)
  121. return false
  122.  
  123. case '$', '&', '+', ',', '/', ':', ';', '=', '?', '@': // §2.2 Reserved characters (reserved)
  124. // Different sections of the URL allow a few of
  125. // the reserved characters to appear unescaped.
  126. switch mode {
  127. case encodePath: // §3.3
  128. // The RFC allows : @ & = + $ but saves / ; , for assigning
  129. // meaning to individual path segments. This package
  130. // only manipulates the path as a whole, so we allow those
  131. // last two as well. That leaves only ? to escape.
  132. return c == '?'
  133.  
  134. case encodeUserPassword: // §3.2.1
  135. // The RFC allows ';', ':', '&', '=', '+', '$', and ',' in
  136. // userinfo, so we must escape only '@', '/', and '?'.
  137. // The parsing of userinfo treats ':' as special so we must escape
  138. // that too.
  139. return c == '@' || c == '/' || c == '?' || c == ':'
  140.  
  141. case encodeQueryComponent: // §3.4
  142. // The RFC reserves (so we must escape) everything.
  143. return true
  144.  
  145. case encodeFragment: // §4.1
  146. // The RFC text is silent but the grammar allows
  147. // everything, so escape nothing.
  148. return false
  149. }
  150. }
  151.  
  152. // Everything else must be escaped.
  153. return true
  154. }
  155.  
  156.  
  157.  
  158.  
  159. func escape(s string, mode encoding) string {
  160. spaceCount, hexCount := 0, 0
  161. for i := 0; i < len(s); i++ {
  162. c := s[i]
  163. if shouldEscape(c, mode) {
  164. if c == ' ' && mode == encodeQueryComponent {
  165. spaceCount++
  166. } else {
  167. hexCount++
  168. }
  169. }
  170. }
  171.  
  172. if spaceCount == 0 && hexCount == 0 {
  173. return s
  174. }
  175.  
  176. t := make([]byte, len(s)+2*hexCount)
  177. j := 0
  178. for i := 0; i < len(s); i++ {
  179. switch c := s[i]; {
  180. case c == ' ' && mode == encodeQueryComponent:
  181. t[j] = '+'
  182. j++
  183. case shouldEscape(c, mode):
  184. t[j] = '%'
  185. t[j+1] = "0123456789ABCDEF"[c>>4]
  186. t[j+2] = "0123456789ABCDEF"[c&15]
  187. j += 3
  188. default:
  189. t[j] = s[i]
  190. j++
  191. }
  192. }
  193. return string(t)
  194. }
  195.  
  196.  
  197. // unescape unescapes a string; the mode specifies
  198. // which section of the URL string is being unescaped.
  199. func unescape(s string, mode encoding) (string, error) {
  200. // Count %, check that they're well-formed.
  201. n := 0
  202. hasPlus := false
  203. for i := 0; i < len(s); {
  204. switch s[i] {
  205. case '%':
  206. n++
  207. if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
  208. s = s[i:]
  209. if len(s) > 3 {
  210. s = s[:3]
  211. }
  212. return "", EscapeError(s)
  213. }
  214. i += 3
  215. case '+':
  216. hasPlus = mode == encodeQueryComponent
  217. i++
  218. default:
  219. i++
  220. }
  221. }
  222.  
  223. if n == 0 && !hasPlus {
  224. return s, nil
  225. }
  226.  
  227. t := make([]byte, len(s)-2*n)
  228. j := 0
  229. for i := 0; i < len(s); {
  230. switch s[i] {
  231. case '%':
  232. t[j] = unhex(s[i+1])<<4 | unhex(s[i+2])
  233. j++
  234. i += 3
  235. case '+':
  236. if mode == encodeQueryComponent {
  237. t[j] = ' '
  238. } else {
  239. t[j] = '+'
  240. }
  241. j++
  242. i++
  243. default:
  244. t[j] = s[i]
  245. j++
  246. i++
  247. }
  248. }
  249. return string(t), nil
  250. }
  251.  
  252.  
  253. func EncodeUriComponent(rawString string) string{
  254. return escape(rawString, encodeFragment)
  255. }
  256.  
  257. func DecodeUriCompontent(encoded string) (string, error){
  258. return unescape(encoded, encodeQueryComponent)
  259. }
  260.  
  261.  
  262. // https://golang.org/src/net/url/url.go
  263. // http://remove-line-numbers.ruurtjan.com/
  264. func main() {
  265. // http://www.url-encode-decode.com/
  266. origin := "äöüHel/lo world"
  267. encoded := EncodeUriComponent(origin)
  268. fmt.Println(encoded)
  269.  
  270. s, _ := DecodeUriCompontent(encoded)
  271. fmt.Println(s)
  272. }
  273.  
  274. // -------------------------------------------------------
  275.  
  276. /*
  277. func UrlEncoded(str string) (string, error) {
  278. u, err := url.Parse(str)
  279. if err != nil {
  280. return "", err
  281. }
  282. return u.String(), nil
  283. }
  284.  
  285.  
  286. // http://stackoverflow.com/questions/13820280/encode-decode-urls
  287. // import "net/url"
  288. func old_main() {
  289. a,err := UrlEncoded("hello world")
  290. if err != nil {
  291. fmt.Println(err)
  292. }
  293. fmt.Println(a)
  294.  
  295. // https://gobyexample.com/url-parsing
  296. //s := "postgres://user:pass@host.com:5432/path?k=v#f"
  297. s := "postgres://user:pass@host.com:5432/path?k=vbla%23fooa#f"
  298. u, err := url.Parse(s)
  299. if err != nil {
  300. panic(err)
  301. }
  302.  
  303.  
  304. fmt.Println(u.RawQuery)
  305. fmt.Println(u.Fragment)
  306. fmt.Println(u.String())
  307. m, _ := url.ParseQuery(u.RawQuery)
  308. fmt.Println(m)
  309. fmt.Println(m["k"][0])
  310.  
  311. }
  312. */
  313.  
  314. // -------------------------------------------------------
  315.  
  316. var regexEscapeURIComponent = regexp.MustCompile(`([^%])(+)`)
  317.  
  318. func QueryPercentEncode(str string) string {
  319. str = url.QueryEscape(str)
  320. return regexEscapeURIComponent.ReplaceAllString(str, "$1%20")
  321. }
  322.  
  323. package main
  324.  
  325. import (
  326. "fmt"
  327. "net/url"
  328. "strings"
  329. )
  330.  
  331. func main() {
  332. params := url.Values{
  333. "test_string": {"+!+'( )*-._~0-👿 👿9a-zA-Z 中文测试 test with ❤️ !@#$%^&&*()~<>?/.,;'[][]:{{}|{}|"},
  334. }
  335. urlEncode := params.Encode()
  336. fmt.Println(urlEncode)
  337. urlEncode = ModifyEncodeString(urlEncode)
  338. fmt.Println(urlEncode)
  339. }
  340.  
  341. //This func mimic JS encodeURIComponent, JS is wild and not very strict.
  342. func ModifyEncodeString(str string) string {
  343. resultStr := str
  344. resultStr = strings.Replace(resultStr, "+", "%20", -1)
  345. resultStr = strings.Replace(resultStr, "%21", "!", -1)
  346. resultStr = strings.Replace(resultStr, "%27", "'", -1)
  347. resultStr = strings.Replace(resultStr, "%28", "(", -1)
  348. resultStr = strings.Replace(resultStr, "%29", ")", -1)
  349. resultStr = strings.Replace(resultStr, "%2A", "*", -1)
  350. return resultStr
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement