uri.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. package fasthttp
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "path/filepath"
  8. "strconv"
  9. "sync"
  10. )
  11. // AcquireURI returns an empty URI instance from the pool.
  12. //
  13. // Release the URI with ReleaseURI after the URI is no longer needed.
  14. // This allows reducing GC load.
  15. func AcquireURI() *URI {
  16. return uriPool.Get().(*URI)
  17. }
  18. // ReleaseURI releases the URI acquired via AcquireURI.
  19. //
  20. // The released URI mustn't be used after releasing it, otherwise data races
  21. // may occur.
  22. func ReleaseURI(u *URI) {
  23. u.Reset()
  24. uriPool.Put(u)
  25. }
  26. var uriPool = &sync.Pool{
  27. New: func() interface{} {
  28. return &URI{}
  29. },
  30. }
  31. // URI represents URI :) .
  32. //
  33. // It is forbidden copying URI instances. Create new instance and use CopyTo
  34. // instead.
  35. //
  36. // URI instance MUST NOT be used from concurrently running goroutines.
  37. type URI struct {
  38. noCopy noCopy //nolint:unused,structcheck
  39. pathOriginal []byte
  40. scheme []byte
  41. path []byte
  42. queryString []byte
  43. hash []byte
  44. host []byte
  45. queryArgs Args
  46. parsedQueryArgs bool
  47. // Path values are sent as-is without normalization
  48. //
  49. // Disabled path normalization may be useful for proxying incoming requests
  50. // to servers that are expecting paths to be forwarded as-is.
  51. //
  52. // By default path values are normalized, i.e.
  53. // extra slashes are removed, special characters are encoded.
  54. DisablePathNormalizing bool
  55. fullURI []byte
  56. requestURI []byte
  57. username []byte
  58. password []byte
  59. }
  60. // CopyTo copies uri contents to dst.
  61. func (u *URI) CopyTo(dst *URI) {
  62. dst.Reset()
  63. dst.pathOriginal = append(dst.pathOriginal, u.pathOriginal...)
  64. dst.scheme = append(dst.scheme, u.scheme...)
  65. dst.path = append(dst.path, u.path...)
  66. dst.queryString = append(dst.queryString, u.queryString...)
  67. dst.hash = append(dst.hash, u.hash...)
  68. dst.host = append(dst.host, u.host...)
  69. dst.username = append(dst.username, u.username...)
  70. dst.password = append(dst.password, u.password...)
  71. u.queryArgs.CopyTo(&dst.queryArgs)
  72. dst.parsedQueryArgs = u.parsedQueryArgs
  73. dst.DisablePathNormalizing = u.DisablePathNormalizing
  74. // fullURI and requestURI shouldn't be copied, since they are created
  75. // from scratch on each FullURI() and RequestURI() call.
  76. }
  77. // Hash returns URI hash, i.e. qwe of http://aaa.com/foo/bar?baz=123#qwe .
  78. //
  79. // The returned bytes are valid until the next URI method call.
  80. func (u *URI) Hash() []byte {
  81. return u.hash
  82. }
  83. // SetHash sets URI hash.
  84. func (u *URI) SetHash(hash string) {
  85. u.hash = append(u.hash[:0], hash...)
  86. }
  87. // SetHashBytes sets URI hash.
  88. func (u *URI) SetHashBytes(hash []byte) {
  89. u.hash = append(u.hash[:0], hash...)
  90. }
  91. // Username returns URI username
  92. //
  93. // The returned bytes are valid until the next URI method call.
  94. func (u *URI) Username() []byte {
  95. return u.username
  96. }
  97. // SetUsername sets URI username.
  98. func (u *URI) SetUsername(username string) {
  99. u.username = append(u.username[:0], username...)
  100. }
  101. // SetUsernameBytes sets URI username.
  102. func (u *URI) SetUsernameBytes(username []byte) {
  103. u.username = append(u.username[:0], username...)
  104. }
  105. // Password returns URI password
  106. //
  107. // The returned bytes are valid until the next URI method call.
  108. func (u *URI) Password() []byte {
  109. return u.password
  110. }
  111. // SetPassword sets URI password.
  112. func (u *URI) SetPassword(password string) {
  113. u.password = append(u.password[:0], password...)
  114. }
  115. // SetPasswordBytes sets URI password.
  116. func (u *URI) SetPasswordBytes(password []byte) {
  117. u.password = append(u.password[:0], password...)
  118. }
  119. // QueryString returns URI query string,
  120. // i.e. baz=123 of http://aaa.com/foo/bar?baz=123#qwe .
  121. //
  122. // The returned bytes are valid until the next URI method call.
  123. func (u *URI) QueryString() []byte {
  124. return u.queryString
  125. }
  126. // SetQueryString sets URI query string.
  127. func (u *URI) SetQueryString(queryString string) {
  128. u.queryString = append(u.queryString[:0], queryString...)
  129. u.parsedQueryArgs = false
  130. }
  131. // SetQueryStringBytes sets URI query string.
  132. func (u *URI) SetQueryStringBytes(queryString []byte) {
  133. u.queryString = append(u.queryString[:0], queryString...)
  134. u.parsedQueryArgs = false
  135. }
  136. // Path returns URI path, i.e. /foo/bar of http://aaa.com/foo/bar?baz=123#qwe .
  137. //
  138. // The returned path is always urldecoded and normalized,
  139. // i.e. '//f%20obar/baz/../zzz' becomes '/f obar/zzz'.
  140. //
  141. // The returned bytes are valid until the next URI method call.
  142. func (u *URI) Path() []byte {
  143. path := u.path
  144. if len(path) == 0 {
  145. path = strSlash
  146. }
  147. return path
  148. }
  149. // SetPath sets URI path.
  150. func (u *URI) SetPath(path string) {
  151. u.pathOriginal = append(u.pathOriginal[:0], path...)
  152. u.path = normalizePath(u.path, u.pathOriginal)
  153. }
  154. // SetPathBytes sets URI path.
  155. func (u *URI) SetPathBytes(path []byte) {
  156. u.pathOriginal = append(u.pathOriginal[:0], path...)
  157. u.path = normalizePath(u.path, u.pathOriginal)
  158. }
  159. // PathOriginal returns the original path from requestURI passed to URI.Parse().
  160. //
  161. // The returned bytes are valid until the next URI method call.
  162. func (u *URI) PathOriginal() []byte {
  163. return u.pathOriginal
  164. }
  165. // Scheme returns URI scheme, i.e. http of http://aaa.com/foo/bar?baz=123#qwe .
  166. //
  167. // Returned scheme is always lowercased.
  168. //
  169. // The returned bytes are valid until the next URI method call.
  170. func (u *URI) Scheme() []byte {
  171. scheme := u.scheme
  172. if len(scheme) == 0 {
  173. scheme = strHTTP
  174. }
  175. return scheme
  176. }
  177. // SetScheme sets URI scheme, i.e. http, https, ftp, etc.
  178. func (u *URI) SetScheme(scheme string) {
  179. u.scheme = append(u.scheme[:0], scheme...)
  180. lowercaseBytes(u.scheme)
  181. }
  182. // SetSchemeBytes sets URI scheme, i.e. http, https, ftp, etc.
  183. func (u *URI) SetSchemeBytes(scheme []byte) {
  184. u.scheme = append(u.scheme[:0], scheme...)
  185. lowercaseBytes(u.scheme)
  186. }
  187. func (u *URI) isHttps() bool {
  188. return bytes.Equal(u.scheme, strHTTPS)
  189. }
  190. func (u *URI) isHttp() bool {
  191. return len(u.scheme) == 0 || bytes.Equal(u.scheme, strHTTP)
  192. }
  193. // Reset clears uri.
  194. func (u *URI) Reset() {
  195. u.pathOriginal = u.pathOriginal[:0]
  196. u.scheme = u.scheme[:0]
  197. u.path = u.path[:0]
  198. u.queryString = u.queryString[:0]
  199. u.hash = u.hash[:0]
  200. u.username = u.username[:0]
  201. u.password = u.password[:0]
  202. u.host = u.host[:0]
  203. u.queryArgs.Reset()
  204. u.parsedQueryArgs = false
  205. u.DisablePathNormalizing = false
  206. // There is no need in u.fullURI = u.fullURI[:0], since full uri
  207. // is calculated on each call to FullURI().
  208. // There is no need in u.requestURI = u.requestURI[:0], since requestURI
  209. // is calculated on each call to RequestURI().
  210. }
  211. // Host returns host part, i.e. aaa.com of http://aaa.com/foo/bar?baz=123#qwe .
  212. //
  213. // Host is always lowercased.
  214. //
  215. // The returned bytes are valid until the next URI method call.
  216. func (u *URI) Host() []byte {
  217. return u.host
  218. }
  219. // SetHost sets host for the uri.
  220. func (u *URI) SetHost(host string) {
  221. u.host = append(u.host[:0], host...)
  222. lowercaseBytes(u.host)
  223. }
  224. // SetHostBytes sets host for the uri.
  225. func (u *URI) SetHostBytes(host []byte) {
  226. u.host = append(u.host[:0], host...)
  227. lowercaseBytes(u.host)
  228. }
  229. var (
  230. ErrorInvalidURI = errors.New("invalid uri")
  231. )
  232. // Parse initializes URI from the given host and uri.
  233. //
  234. // host may be nil. In this case uri must contain fully qualified uri,
  235. // i.e. with scheme and host. http is assumed if scheme is omitted.
  236. //
  237. // uri may contain e.g. RequestURI without scheme and host if host is non-empty.
  238. func (u *URI) Parse(host, uri []byte) error {
  239. return u.parse(host, uri, false)
  240. }
  241. func (u *URI) parse(host, uri []byte, isTLS bool) error {
  242. u.Reset()
  243. if stringContainsCTLByte(uri) {
  244. return ErrorInvalidURI
  245. }
  246. if len(host) == 0 || bytes.Contains(uri, strColonSlashSlash) {
  247. scheme, newHost, newURI := splitHostURI(host, uri)
  248. u.SetSchemeBytes(scheme)
  249. host = newHost
  250. uri = newURI
  251. }
  252. if isTLS {
  253. u.SetSchemeBytes(strHTTPS)
  254. }
  255. if n := bytes.IndexByte(host, '@'); n >= 0 {
  256. auth := host[:n]
  257. host = host[n+1:]
  258. if n := bytes.IndexByte(auth, ':'); n >= 0 {
  259. u.username = append(u.username[:0], auth[:n]...)
  260. u.password = append(u.password[:0], auth[n+1:]...)
  261. } else {
  262. u.username = append(u.username[:0], auth...)
  263. u.password = u.password[:0]
  264. }
  265. }
  266. u.host = append(u.host, host...)
  267. if parsedHost, err := parseHost(u.host); err != nil {
  268. return err
  269. } else {
  270. u.host = parsedHost
  271. }
  272. lowercaseBytes(u.host)
  273. b := uri
  274. queryIndex := bytes.IndexByte(b, '?')
  275. fragmentIndex := bytes.IndexByte(b, '#')
  276. // Ignore query in fragment part
  277. if fragmentIndex >= 0 && queryIndex > fragmentIndex {
  278. queryIndex = -1
  279. }
  280. if queryIndex < 0 && fragmentIndex < 0 {
  281. u.pathOriginal = append(u.pathOriginal, b...)
  282. u.path = normalizePath(u.path, u.pathOriginal)
  283. return nil
  284. }
  285. if queryIndex >= 0 {
  286. // Path is everything up to the start of the query
  287. u.pathOriginal = append(u.pathOriginal, b[:queryIndex]...)
  288. u.path = normalizePath(u.path, u.pathOriginal)
  289. if fragmentIndex < 0 {
  290. u.queryString = append(u.queryString, b[queryIndex+1:]...)
  291. } else {
  292. u.queryString = append(u.queryString, b[queryIndex+1:fragmentIndex]...)
  293. u.hash = append(u.hash, b[fragmentIndex+1:]...)
  294. }
  295. return nil
  296. }
  297. // fragmentIndex >= 0 && queryIndex < 0
  298. // Path is up to the start of fragment
  299. u.pathOriginal = append(u.pathOriginal, b[:fragmentIndex]...)
  300. u.path = normalizePath(u.path, u.pathOriginal)
  301. u.hash = append(u.hash, b[fragmentIndex+1:]...)
  302. return nil
  303. }
  304. // parseHost parses host as an authority without user
  305. // information. That is, as host[:port].
  306. //
  307. // Based on https://github.com/golang/go/blob/8ac5cbe05d61df0a7a7c9a38ff33305d4dcfea32/src/net/url/url.go#L619
  308. //
  309. // The host is parsed and unescaped in place overwriting the contents of the host parameter.
  310. func parseHost(host []byte) ([]byte, error) {
  311. if len(host) > 0 && host[0] == '[' {
  312. // Parse an IP-Literal in RFC 3986 and RFC 6874.
  313. // E.g., "[fe80::1]", "[fe80::1%25en0]", "[fe80::1]:80".
  314. i := bytes.LastIndexByte(host, ']')
  315. if i < 0 {
  316. return nil, errors.New("missing ']' in host")
  317. }
  318. colonPort := host[i+1:]
  319. if !validOptionalPort(colonPort) {
  320. return nil, fmt.Errorf("invalid port %q after host", colonPort)
  321. }
  322. // RFC 6874 defines that %25 (%-encoded percent) introduces
  323. // the zone identifier, and the zone identifier can use basically
  324. // any %-encoding it likes. That's different from the host, which
  325. // can only %-encode non-ASCII bytes.
  326. // We do impose some restrictions on the zone, to avoid stupidity
  327. // like newlines.
  328. zone := bytes.Index(host[:i], []byte("%25"))
  329. if zone >= 0 {
  330. host1, err := unescape(host[:zone], encodeHost)
  331. if err != nil {
  332. return nil, err
  333. }
  334. host2, err := unescape(host[zone:i], encodeZone)
  335. if err != nil {
  336. return nil, err
  337. }
  338. host3, err := unescape(host[i:], encodeHost)
  339. if err != nil {
  340. return nil, err
  341. }
  342. return append(host1, append(host2, host3...)...), nil
  343. }
  344. } else if i := bytes.LastIndexByte(host, ':'); i != -1 {
  345. colonPort := host[i:]
  346. if !validOptionalPort(colonPort) {
  347. return nil, fmt.Errorf("invalid port %q after host", colonPort)
  348. }
  349. }
  350. var err error
  351. if host, err = unescape(host, encodeHost); err != nil {
  352. return nil, err
  353. }
  354. return host, nil
  355. }
  356. type encoding int
  357. const (
  358. encodeHost encoding = 1 + iota
  359. encodeZone
  360. )
  361. type EscapeError string
  362. func (e EscapeError) Error() string {
  363. return "invalid URL escape " + strconv.Quote(string(e))
  364. }
  365. type InvalidHostError string
  366. func (e InvalidHostError) Error() string {
  367. return "invalid character " + strconv.Quote(string(e)) + " in host name"
  368. }
  369. // unescape unescapes a string; the mode specifies
  370. // which section of the URL string is being unescaped.
  371. //
  372. // Based on https://github.com/golang/go/blob/8ac5cbe05d61df0a7a7c9a38ff33305d4dcfea32/src/net/url/url.go#L199
  373. //
  374. // Unescapes in place overwriting the contents of s and returning it.
  375. func unescape(s []byte, mode encoding) ([]byte, error) {
  376. // Count %, check that they're well-formed.
  377. n := 0
  378. for i := 0; i < len(s); {
  379. switch s[i] {
  380. case '%':
  381. n++
  382. if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
  383. s = s[i:]
  384. if len(s) > 3 {
  385. s = s[:3]
  386. }
  387. return nil, EscapeError(s)
  388. }
  389. // Per https://tools.ietf.org/html/rfc3986#page-21
  390. // in the host component %-encoding can only be used
  391. // for non-ASCII bytes.
  392. // But https://tools.ietf.org/html/rfc6874#section-2
  393. // introduces %25 being allowed to escape a percent sign
  394. // in IPv6 scoped-address literals. Yay.
  395. if mode == encodeHost && unhex(s[i+1]) < 8 && !bytes.Equal(s[i:i+3], []byte("%25")) {
  396. return nil, EscapeError(s[i : i+3])
  397. }
  398. if mode == encodeZone {
  399. // RFC 6874 says basically "anything goes" for zone identifiers
  400. // and that even non-ASCII can be redundantly escaped,
  401. // but it seems prudent to restrict %-escaped bytes here to those
  402. // that are valid host name bytes in their unescaped form.
  403. // That is, you can use escaping in the zone identifier but not
  404. // to introduce bytes you couldn't just write directly.
  405. // But Windows puts spaces here! Yay.
  406. v := unhex(s[i+1])<<4 | unhex(s[i+2])
  407. if !bytes.Equal(s[i:i+3], []byte("%25")) && v != ' ' && shouldEscape(v, encodeHost) {
  408. return nil, EscapeError(s[i : i+3])
  409. }
  410. }
  411. i += 3
  412. default:
  413. if (mode == encodeHost || mode == encodeZone) && s[i] < 0x80 && shouldEscape(s[i], mode) {
  414. return nil, InvalidHostError(s[i : i+1])
  415. }
  416. i++
  417. }
  418. }
  419. if n == 0 {
  420. return s, nil
  421. }
  422. t := s[:0]
  423. for i := 0; i < len(s); i++ {
  424. switch s[i] {
  425. case '%':
  426. t = append(t, unhex(s[i+1])<<4|unhex(s[i+2]))
  427. i += 2
  428. default:
  429. t = append(t, s[i])
  430. }
  431. }
  432. return t, nil
  433. }
  434. // Return true if the specified character should be escaped when
  435. // appearing in a URL string, according to RFC 3986.
  436. //
  437. // Please be informed that for now shouldEscape does not check all
  438. // reserved characters correctly. See golang.org/issue/5684.
  439. //
  440. // Based on https://github.com/golang/go/blob/8ac5cbe05d61df0a7a7c9a38ff33305d4dcfea32/src/net/url/url.go#L100
  441. func shouldEscape(c byte, mode encoding) bool {
  442. // §2.3 Unreserved characters (alphanum)
  443. if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
  444. return false
  445. }
  446. if mode == encodeHost || mode == encodeZone {
  447. // §3.2.2 Host allows
  448. // sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
  449. // as part of reg-name.
  450. // We add : because we include :port as part of host.
  451. // We add [ ] because we include [ipv6]:port as part of host.
  452. // We add < > because they're the only characters left that
  453. // we could possibly allow, and Parse will reject them if we
  454. // escape them (because hosts can't use %-encoding for
  455. // ASCII bytes).
  456. switch c {
  457. case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '[', ']', '<', '>', '"':
  458. return false
  459. }
  460. }
  461. if c == '-' || c == '_' || c == '.' || c == '~' { // §2.3 Unreserved characters (mark)
  462. return false
  463. }
  464. // Everything else must be escaped.
  465. return true
  466. }
  467. func ishex(c byte) bool {
  468. return ('0' <= c && c <= '9') ||
  469. ('a' <= c && c <= 'f') ||
  470. ('A' <= c && c <= 'F')
  471. }
  472. func unhex(c byte) byte {
  473. switch {
  474. case '0' <= c && c <= '9':
  475. return c - '0'
  476. case 'a' <= c && c <= 'f':
  477. return c - 'a' + 10
  478. case 'A' <= c && c <= 'F':
  479. return c - 'A' + 10
  480. }
  481. return 0
  482. }
  483. // validOptionalPort reports whether port is either an empty string
  484. // or matches /^:\d*$/
  485. func validOptionalPort(port []byte) bool {
  486. if len(port) == 0 {
  487. return true
  488. }
  489. if port[0] != ':' {
  490. return false
  491. }
  492. for _, b := range port[1:] {
  493. if b < '0' || b > '9' {
  494. return false
  495. }
  496. }
  497. return true
  498. }
  499. func normalizePath(dst, src []byte) []byte {
  500. dst = dst[:0]
  501. dst = addLeadingSlash(dst, src)
  502. dst = decodeArgAppendNoPlus(dst, src)
  503. // remove duplicate slashes
  504. b := dst
  505. bSize := len(b)
  506. for {
  507. n := bytes.Index(b, strSlashSlash)
  508. if n < 0 {
  509. break
  510. }
  511. b = b[n:]
  512. copy(b, b[1:])
  513. b = b[:len(b)-1]
  514. bSize--
  515. }
  516. dst = dst[:bSize]
  517. // remove /./ parts
  518. b = dst
  519. for {
  520. n := bytes.Index(b, strSlashDotSlash)
  521. if n < 0 {
  522. break
  523. }
  524. nn := n + len(strSlashDotSlash) - 1
  525. copy(b[n:], b[nn:])
  526. b = b[:len(b)-nn+n]
  527. }
  528. // remove /foo/../ parts
  529. for {
  530. n := bytes.Index(b, strSlashDotDotSlash)
  531. if n < 0 {
  532. break
  533. }
  534. nn := bytes.LastIndexByte(b[:n], '/')
  535. if nn < 0 {
  536. nn = 0
  537. }
  538. n += len(strSlashDotDotSlash) - 1
  539. copy(b[nn:], b[n:])
  540. b = b[:len(b)-n+nn]
  541. }
  542. // remove trailing /foo/..
  543. n := bytes.LastIndex(b, strSlashDotDot)
  544. if n >= 0 && n+len(strSlashDotDot) == len(b) {
  545. nn := bytes.LastIndexByte(b[:n], '/')
  546. if nn < 0 {
  547. return append(dst[:0], strSlash...)
  548. }
  549. b = b[:nn+1]
  550. }
  551. if filepath.Separator == '\\' {
  552. // remove \.\ parts
  553. b = dst
  554. for {
  555. n := bytes.Index(b, strBackSlashDotBackSlash)
  556. if n < 0 {
  557. break
  558. }
  559. nn := n + len(strSlashDotSlash) - 1
  560. copy(b[n:], b[nn:])
  561. b = b[:len(b)-nn+n]
  562. }
  563. // remove /foo/..\ parts
  564. for {
  565. n := bytes.Index(b, strSlashDotDotBackSlash)
  566. if n < 0 {
  567. break
  568. }
  569. nn := bytes.LastIndexByte(b[:n], '/')
  570. if nn < 0 {
  571. nn = 0
  572. }
  573. n += len(strSlashDotDotBackSlash) - 1
  574. copy(b[nn:], b[n:])
  575. b = b[:len(b)-n+nn]
  576. }
  577. // remove /foo\..\ parts
  578. for {
  579. n := bytes.Index(b, strBackSlashDotDotBackSlash)
  580. if n < 0 {
  581. break
  582. }
  583. nn := bytes.LastIndexByte(b[:n], '/')
  584. if nn < 0 {
  585. nn = 0
  586. }
  587. n += len(strBackSlashDotDotBackSlash) - 1
  588. copy(b[nn:], b[n:])
  589. b = b[:len(b)-n+nn]
  590. }
  591. // remove trailing \foo\..
  592. n := bytes.LastIndex(b, strBackSlashDotDot)
  593. if n >= 0 && n+len(strSlashDotDot) == len(b) {
  594. nn := bytes.LastIndexByte(b[:n], '/')
  595. if nn < 0 {
  596. return append(dst[:0], strSlash...)
  597. }
  598. b = b[:nn+1]
  599. }
  600. }
  601. return b
  602. }
  603. // RequestURI returns RequestURI - i.e. URI without Scheme and Host.
  604. func (u *URI) RequestURI() []byte {
  605. var dst []byte
  606. if u.DisablePathNormalizing {
  607. dst = append(u.requestURI[:0], u.PathOriginal()...)
  608. } else {
  609. dst = appendQuotedPath(u.requestURI[:0], u.Path())
  610. }
  611. if u.parsedQueryArgs && u.queryArgs.Len() > 0 {
  612. dst = append(dst, '?')
  613. dst = u.queryArgs.AppendBytes(dst)
  614. } else if len(u.queryString) > 0 {
  615. dst = append(dst, '?')
  616. dst = append(dst, u.queryString...)
  617. }
  618. u.requestURI = dst
  619. return u.requestURI
  620. }
  621. // LastPathSegment returns the last part of uri path after '/'.
  622. //
  623. // Examples:
  624. //
  625. // - For /foo/bar/baz.html path returns baz.html.
  626. // - For /foo/bar/ returns empty byte slice.
  627. // - For /foobar.js returns foobar.js.
  628. //
  629. // The returned bytes are valid until the next URI method call.
  630. func (u *URI) LastPathSegment() []byte {
  631. path := u.Path()
  632. n := bytes.LastIndexByte(path, '/')
  633. if n < 0 {
  634. return path
  635. }
  636. return path[n+1:]
  637. }
  638. // Update updates uri.
  639. //
  640. // The following newURI types are accepted:
  641. //
  642. // - Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
  643. // uri is replaced by newURI.
  644. // - Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
  645. // the original scheme is preserved.
  646. // - Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
  647. // of the original uri is replaced.
  648. // - Relative path, i.e. xx?yy=abc . In this case the original RequestURI
  649. // is updated according to the new relative path.
  650. func (u *URI) Update(newURI string) {
  651. u.UpdateBytes(s2b(newURI))
  652. }
  653. // UpdateBytes updates uri.
  654. //
  655. // The following newURI types are accepted:
  656. //
  657. // - Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
  658. // uri is replaced by newURI.
  659. // - Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
  660. // the original scheme is preserved.
  661. // - Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
  662. // of the original uri is replaced.
  663. // - Relative path, i.e. xx?yy=abc . In this case the original RequestURI
  664. // is updated according to the new relative path.
  665. func (u *URI) UpdateBytes(newURI []byte) {
  666. u.requestURI = u.updateBytes(newURI, u.requestURI)
  667. }
  668. func (u *URI) updateBytes(newURI, buf []byte) []byte {
  669. if len(newURI) == 0 {
  670. return buf
  671. }
  672. n := bytes.Index(newURI, strSlashSlash)
  673. if n >= 0 {
  674. // absolute uri
  675. var b [32]byte
  676. schemeOriginal := b[:0]
  677. if len(u.scheme) > 0 {
  678. schemeOriginal = append([]byte(nil), u.scheme...)
  679. }
  680. if err := u.Parse(nil, newURI); err != nil {
  681. return nil
  682. }
  683. if len(schemeOriginal) > 0 && len(u.scheme) == 0 {
  684. u.scheme = append(u.scheme[:0], schemeOriginal...)
  685. }
  686. return buf
  687. }
  688. if newURI[0] == '/' {
  689. // uri without host
  690. buf = u.appendSchemeHost(buf[:0])
  691. buf = append(buf, newURI...)
  692. if err := u.Parse(nil, buf); err != nil {
  693. return nil
  694. }
  695. return buf
  696. }
  697. // relative path
  698. switch newURI[0] {
  699. case '?':
  700. // query string only update
  701. u.SetQueryStringBytes(newURI[1:])
  702. return append(buf[:0], u.FullURI()...)
  703. case '#':
  704. // update only hash
  705. u.SetHashBytes(newURI[1:])
  706. return append(buf[:0], u.FullURI()...)
  707. default:
  708. // update the last path part after the slash
  709. path := u.Path()
  710. n = bytes.LastIndexByte(path, '/')
  711. if n < 0 {
  712. panic(fmt.Sprintf("BUG: path must contain at least one slash: %q %q", u.Path(), newURI))
  713. }
  714. buf = u.appendSchemeHost(buf[:0])
  715. buf = appendQuotedPath(buf, path[:n+1])
  716. buf = append(buf, newURI...)
  717. if err := u.Parse(nil, buf); err != nil {
  718. return nil
  719. }
  720. return buf
  721. }
  722. }
  723. // FullURI returns full uri in the form {Scheme}://{Host}{RequestURI}#{Hash}.
  724. //
  725. // The returned bytes are valid until the next URI method call.
  726. func (u *URI) FullURI() []byte {
  727. u.fullURI = u.AppendBytes(u.fullURI[:0])
  728. return u.fullURI
  729. }
  730. // AppendBytes appends full uri to dst and returns the extended dst.
  731. func (u *URI) AppendBytes(dst []byte) []byte {
  732. dst = u.appendSchemeHost(dst)
  733. dst = append(dst, u.RequestURI()...)
  734. if len(u.hash) > 0 {
  735. dst = append(dst, '#')
  736. dst = append(dst, u.hash...)
  737. }
  738. return dst
  739. }
  740. func (u *URI) appendSchemeHost(dst []byte) []byte {
  741. dst = append(dst, u.Scheme()...)
  742. dst = append(dst, strColonSlashSlash...)
  743. return append(dst, u.Host()...)
  744. }
  745. // WriteTo writes full uri to w.
  746. //
  747. // WriteTo implements io.WriterTo interface.
  748. func (u *URI) WriteTo(w io.Writer) (int64, error) {
  749. n, err := w.Write(u.FullURI())
  750. return int64(n), err
  751. }
  752. // String returns full uri.
  753. func (u *URI) String() string {
  754. return string(u.FullURI())
  755. }
  756. func splitHostURI(host, uri []byte) ([]byte, []byte, []byte) {
  757. n := bytes.Index(uri, strSlashSlash)
  758. if n < 0 {
  759. return strHTTP, host, uri
  760. }
  761. scheme := uri[:n]
  762. if bytes.IndexByte(scheme, '/') >= 0 {
  763. return strHTTP, host, uri
  764. }
  765. if len(scheme) > 0 && scheme[len(scheme)-1] == ':' {
  766. scheme = scheme[:len(scheme)-1]
  767. }
  768. n += len(strSlashSlash)
  769. uri = uri[n:]
  770. n = bytes.IndexByte(uri, '/')
  771. nq := bytes.IndexByte(uri, '?')
  772. if nq >= 0 && nq < n {
  773. // A hack for urls like foobar.com?a=b/xyz
  774. n = nq
  775. } else if n < 0 {
  776. // A hack for bogus urls like foobar.com?a=b without
  777. // slash after host.
  778. if nq >= 0 {
  779. return scheme, uri[:nq], uri[nq:]
  780. }
  781. return scheme, uri, strSlash
  782. }
  783. return scheme, uri[:n], uri[n:]
  784. }
  785. // QueryArgs returns query args.
  786. //
  787. // The returned args are valid until the next URI method call.
  788. func (u *URI) QueryArgs() *Args {
  789. u.parseQueryArgs()
  790. return &u.queryArgs
  791. }
  792. func (u *URI) parseQueryArgs() {
  793. if u.parsedQueryArgs {
  794. return
  795. }
  796. u.queryArgs.ParseBytes(u.queryString)
  797. u.parsedQueryArgs = true
  798. }
  799. // stringContainsCTLByte reports whether s contains any ASCII control character.
  800. func stringContainsCTLByte(s []byte) bool {
  801. for i := 0; i < len(s); i++ {
  802. b := s[i]
  803. if b < ' ' || b == 0x7f {
  804. return true
  805. }
  806. }
  807. return false
  808. }