uri.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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() any {
  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
  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 ErrorInvalidURI = errors.New("invalid uri")
  230. // Parse initializes URI from the given host and uri.
  231. //
  232. // host may be nil. In this case uri must contain fully qualified uri,
  233. // i.e. with scheme and host. http is assumed if scheme is omitted.
  234. //
  235. // uri may contain e.g. RequestURI without scheme and host if host is non-empty.
  236. func (u *URI) Parse(host, uri []byte) error {
  237. return u.parse(host, uri, false)
  238. }
  239. func (u *URI) parse(host, uri []byte, isTLS bool) error {
  240. u.Reset()
  241. if stringContainsCTLByte(uri) {
  242. return ErrorInvalidURI
  243. }
  244. if len(host) == 0 || bytes.Contains(uri, strColonSlashSlash) {
  245. scheme, newHost, newURI := splitHostURI(host, uri)
  246. u.SetSchemeBytes(scheme)
  247. host = newHost
  248. uri = newURI
  249. }
  250. if isTLS {
  251. u.SetSchemeBytes(strHTTPS)
  252. }
  253. if n := bytes.IndexByte(host, '@'); n >= 0 {
  254. auth := host[:n]
  255. host = host[n+1:]
  256. if n := bytes.IndexByte(auth, ':'); n >= 0 {
  257. u.username = append(u.username[:0], auth[:n]...)
  258. u.password = append(u.password[:0], auth[n+1:]...)
  259. } else {
  260. u.username = append(u.username[:0], auth...)
  261. u.password = u.password[:0]
  262. }
  263. }
  264. u.host = append(u.host, host...)
  265. if parsedHost, err := parseHost(u.host); err != nil {
  266. return err
  267. } else {
  268. u.host = parsedHost
  269. }
  270. lowercaseBytes(u.host)
  271. b := uri
  272. queryIndex := bytes.IndexByte(b, '?')
  273. fragmentIndex := bytes.IndexByte(b, '#')
  274. // Ignore query in fragment part
  275. if fragmentIndex >= 0 && queryIndex > fragmentIndex {
  276. queryIndex = -1
  277. }
  278. if queryIndex < 0 && fragmentIndex < 0 {
  279. u.pathOriginal = append(u.pathOriginal, b...)
  280. u.path = normalizePath(u.path, u.pathOriginal)
  281. return nil
  282. }
  283. if queryIndex >= 0 {
  284. // Path is everything up to the start of the query
  285. u.pathOriginal = append(u.pathOriginal, b[:queryIndex]...)
  286. u.path = normalizePath(u.path, u.pathOriginal)
  287. if fragmentIndex < 0 {
  288. u.queryString = append(u.queryString, b[queryIndex+1:]...)
  289. } else {
  290. u.queryString = append(u.queryString, b[queryIndex+1:fragmentIndex]...)
  291. u.hash = append(u.hash, b[fragmentIndex+1:]...)
  292. }
  293. return nil
  294. }
  295. // fragmentIndex >= 0 && queryIndex < 0
  296. // Path is up to the start of fragment
  297. u.pathOriginal = append(u.pathOriginal, b[:fragmentIndex]...)
  298. u.path = normalizePath(u.path, u.pathOriginal)
  299. u.hash = append(u.hash, b[fragmentIndex+1:]...)
  300. return nil
  301. }
  302. // parseHost parses host as an authority without user
  303. // information. That is, as host[:port].
  304. //
  305. // Based on https://github.com/golang/go/blob/8ac5cbe05d61df0a7a7c9a38ff33305d4dcfea32/src/net/url/url.go#L619
  306. //
  307. // The host is parsed and unescaped in place overwriting the contents of the host parameter.
  308. func parseHost(host []byte) ([]byte, error) {
  309. if len(host) > 0 && host[0] == '[' {
  310. // Parse an IP-Literal in RFC 3986 and RFC 6874.
  311. // E.g., "[fe80::1]", "[fe80::1%25en0]", "[fe80::1]:80".
  312. i := bytes.LastIndexByte(host, ']')
  313. if i < 0 {
  314. return nil, errors.New("missing ']' in host")
  315. }
  316. colonPort := host[i+1:]
  317. if !validOptionalPort(colonPort) {
  318. return nil, fmt.Errorf("invalid port %q after host", colonPort)
  319. }
  320. // RFC 6874 defines that %25 (%-encoded percent) introduces
  321. // the zone identifier, and the zone identifier can use basically
  322. // any %-encoding it likes. That's different from the host, which
  323. // can only %-encode non-ASCII bytes.
  324. // We do impose some restrictions on the zone, to avoid stupidity
  325. // like newlines.
  326. zone := bytes.Index(host[:i], []byte("%25"))
  327. if zone >= 0 {
  328. host1, err := unescape(host[:zone], encodeHost)
  329. if err != nil {
  330. return nil, err
  331. }
  332. host2, err := unescape(host[zone:i], encodeZone)
  333. if err != nil {
  334. return nil, err
  335. }
  336. host3, err := unescape(host[i:], encodeHost)
  337. if err != nil {
  338. return nil, err
  339. }
  340. return append(host1, append(host2, host3...)...), nil
  341. }
  342. } else if i := bytes.LastIndexByte(host, ':'); i != -1 {
  343. colonPort := host[i:]
  344. if !validOptionalPort(colonPort) {
  345. return nil, fmt.Errorf("invalid port %q after host", colonPort)
  346. }
  347. }
  348. var err error
  349. if host, err = unescape(host, encodeHost); err != nil {
  350. return nil, err
  351. }
  352. return host, nil
  353. }
  354. type encoding int
  355. const (
  356. encodeHost encoding = 1 + iota
  357. encodeZone
  358. )
  359. type EscapeError string
  360. func (e EscapeError) Error() string {
  361. return "invalid URL escape " + strconv.Quote(string(e))
  362. }
  363. type InvalidHostError string
  364. func (e InvalidHostError) Error() string {
  365. return "invalid character " + strconv.Quote(string(e)) + " in host name"
  366. }
  367. // unescape unescapes a string; the mode specifies
  368. // which section of the URL string is being unescaped.
  369. //
  370. // Based on https://github.com/golang/go/blob/8ac5cbe05d61df0a7a7c9a38ff33305d4dcfea32/src/net/url/url.go#L199
  371. //
  372. // Unescapes in place overwriting the contents of s and returning it.
  373. func unescape(s []byte, mode encoding) ([]byte, error) {
  374. // Count %, check that they're well-formed.
  375. n := 0
  376. for i := 0; i < len(s); {
  377. switch s[i] {
  378. case '%':
  379. n++
  380. if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
  381. s = s[i:]
  382. if len(s) > 3 {
  383. s = s[:3]
  384. }
  385. return nil, EscapeError(s)
  386. }
  387. // Per https://tools.ietf.org/html/rfc3986#page-21
  388. // in the host component %-encoding can only be used
  389. // for non-ASCII bytes.
  390. // But https://tools.ietf.org/html/rfc6874#section-2
  391. // introduces %25 being allowed to escape a percent sign
  392. // in IPv6 scoped-address literals. Yay.
  393. if mode == encodeHost && unhex(s[i+1]) < 8 && !bytes.Equal(s[i:i+3], []byte("%25")) {
  394. return nil, EscapeError(s[i : i+3])
  395. }
  396. if mode == encodeZone {
  397. // RFC 6874 says basically "anything goes" for zone identifiers
  398. // and that even non-ASCII can be redundantly escaped,
  399. // but it seems prudent to restrict %-escaped bytes here to those
  400. // that are valid host name bytes in their unescaped form.
  401. // That is, you can use escaping in the zone identifier but not
  402. // to introduce bytes you couldn't just write directly.
  403. // But Windows puts spaces here! Yay.
  404. v := unhex(s[i+1])<<4 | unhex(s[i+2])
  405. if !bytes.Equal(s[i:i+3], []byte("%25")) && v != ' ' && shouldEscape(v, encodeHost) {
  406. return nil, EscapeError(s[i : i+3])
  407. }
  408. }
  409. i += 3
  410. default:
  411. if (mode == encodeHost || mode == encodeZone) && s[i] < 0x80 && shouldEscape(s[i], mode) {
  412. return nil, InvalidHostError(s[i : i+1])
  413. }
  414. i++
  415. }
  416. }
  417. if n == 0 {
  418. return s, nil
  419. }
  420. t := s[:0]
  421. for i := 0; i < len(s); i++ {
  422. switch s[i] {
  423. case '%':
  424. t = append(t, unhex(s[i+1])<<4|unhex(s[i+2]))
  425. i += 2
  426. default:
  427. t = append(t, s[i])
  428. }
  429. }
  430. return t, nil
  431. }
  432. // Return true if the specified character should be escaped when
  433. // appearing in a URL string, according to RFC 3986.
  434. //
  435. // Please be informed that for now shouldEscape does not check all
  436. // reserved characters correctly. See https://github.com/golang/go/issues/5684.
  437. //
  438. // Based on https://github.com/golang/go/blob/8ac5cbe05d61df0a7a7c9a38ff33305d4dcfea32/src/net/url/url.go#L100
  439. func shouldEscape(c byte, mode encoding) bool {
  440. // §2.3 Unreserved characters (alphanum)
  441. if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
  442. return false
  443. }
  444. if mode == encodeHost || mode == encodeZone {
  445. // §3.2.2 Host allows
  446. // sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
  447. // as part of reg-name.
  448. // We add : because we include :port as part of host.
  449. // We add [ ] because we include [ipv6]:port as part of host.
  450. // We add < > because they're the only characters left that
  451. // we could possibly allow, and Parse will reject them if we
  452. // escape them (because hosts can't use %-encoding for
  453. // ASCII bytes).
  454. switch c {
  455. case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '[', ']', '<', '>', '"':
  456. return false
  457. }
  458. }
  459. if c == '-' || c == '_' || c == '.' || c == '~' { // §2.3 Unreserved characters (mark)
  460. return false
  461. }
  462. // Everything else must be escaped.
  463. return true
  464. }
  465. func ishex(c byte) bool {
  466. return ('0' <= c && c <= '9') ||
  467. ('a' <= c && c <= 'f') ||
  468. ('A' <= c && c <= 'F')
  469. }
  470. func unhex(c byte) byte {
  471. switch {
  472. case '0' <= c && c <= '9':
  473. return c - '0'
  474. case 'a' <= c && c <= 'f':
  475. return c - 'a' + 10
  476. case 'A' <= c && c <= 'F':
  477. return c - 'A' + 10
  478. }
  479. return 0
  480. }
  481. // validOptionalPort reports whether port is either an empty string
  482. // or matches /^:\d*$/.
  483. func validOptionalPort(port []byte) bool {
  484. if len(port) == 0 {
  485. return true
  486. }
  487. if port[0] != ':' {
  488. return false
  489. }
  490. for _, b := range port[1:] {
  491. if b < '0' || b > '9' {
  492. return false
  493. }
  494. }
  495. return true
  496. }
  497. func normalizePath(dst, src []byte) []byte {
  498. dst = dst[:0]
  499. dst = addLeadingSlash(dst, src)
  500. dst = decodeArgAppendNoPlus(dst, src)
  501. // remove duplicate slashes
  502. b := dst
  503. bSize := len(b)
  504. for {
  505. n := bytes.Index(b, strSlashSlash)
  506. if n < 0 {
  507. break
  508. }
  509. b = b[n:]
  510. copy(b, b[1:])
  511. b = b[:len(b)-1]
  512. bSize--
  513. }
  514. dst = dst[:bSize]
  515. // remove /./ parts
  516. b = dst
  517. for {
  518. n := bytes.Index(b, strSlashDotSlash)
  519. if n < 0 {
  520. break
  521. }
  522. nn := n + len(strSlashDotSlash) - 1
  523. copy(b[n:], b[nn:])
  524. b = b[:len(b)-nn+n]
  525. }
  526. // remove /foo/../ parts
  527. for {
  528. n := bytes.Index(b, strSlashDotDotSlash)
  529. if n < 0 {
  530. break
  531. }
  532. nn := bytes.LastIndexByte(b[:n], '/')
  533. if nn < 0 {
  534. nn = 0
  535. }
  536. n += len(strSlashDotDotSlash) - 1
  537. copy(b[nn:], b[n:])
  538. b = b[:len(b)-n+nn]
  539. }
  540. // remove trailing /foo/..
  541. n := bytes.LastIndex(b, strSlashDotDot)
  542. if n >= 0 && n+len(strSlashDotDot) == len(b) {
  543. nn := bytes.LastIndexByte(b[:n], '/')
  544. if nn < 0 {
  545. return append(dst[:0], strSlash...)
  546. }
  547. b = b[:nn+1]
  548. }
  549. if filepath.Separator == '\\' {
  550. // remove \.\ parts
  551. for {
  552. n := bytes.Index(b, strBackSlashDotBackSlash)
  553. if n < 0 {
  554. break
  555. }
  556. nn := n + len(strSlashDotSlash) - 1
  557. copy(b[n:], b[nn:])
  558. b = b[:len(b)-nn+n]
  559. }
  560. // remove /foo/..\ parts
  561. for {
  562. n := bytes.Index(b, strSlashDotDotBackSlash)
  563. if n < 0 {
  564. break
  565. }
  566. nn := bytes.LastIndexByte(b[:n], '/')
  567. if nn < 0 {
  568. nn = 0
  569. }
  570. nn++
  571. n += len(strSlashDotDotBackSlash)
  572. copy(b[nn:], b[n:])
  573. b = b[:len(b)-n+nn]
  574. }
  575. // remove /foo\..\ parts
  576. for {
  577. n := bytes.Index(b, strBackSlashDotDotBackSlash)
  578. if n < 0 {
  579. break
  580. }
  581. nn := bytes.LastIndexByte(b[:n], '/')
  582. if nn < 0 {
  583. nn = 0
  584. }
  585. n += len(strBackSlashDotDotBackSlash) - 1
  586. copy(b[nn:], b[n:])
  587. b = b[:len(b)-n+nn]
  588. }
  589. // remove trailing \foo\..
  590. n := bytes.LastIndex(b, strBackSlashDotDot)
  591. if n >= 0 && n+len(strSlashDotDot) == len(b) {
  592. nn := bytes.LastIndexByte(b[:n], '/')
  593. if nn < 0 {
  594. return append(dst[:0], strSlash...)
  595. }
  596. b = b[:nn+1]
  597. }
  598. }
  599. return b
  600. }
  601. // RequestURI returns RequestURI - i.e. URI without Scheme and Host.
  602. func (u *URI) RequestURI() []byte {
  603. var dst []byte
  604. if u.DisablePathNormalizing {
  605. dst = u.requestURI[:0]
  606. dst = append(dst, u.PathOriginal()...)
  607. } else {
  608. dst = appendQuotedPath(u.requestURI[:0], u.Path())
  609. }
  610. if u.parsedQueryArgs && u.queryArgs.Len() > 0 {
  611. dst = append(dst, '?')
  612. dst = u.queryArgs.AppendBytes(dst)
  613. } else if len(u.queryString) > 0 {
  614. dst = append(dst, '?')
  615. dst = append(dst, u.queryString...)
  616. }
  617. u.requestURI = dst
  618. return u.requestURI
  619. }
  620. // LastPathSegment returns the last part of uri path after '/'.
  621. //
  622. // Examples:
  623. //
  624. // - For /foo/bar/baz.html path returns baz.html.
  625. // - For /foo/bar/ returns empty byte slice.
  626. // - For /foobar.js returns foobar.js.
  627. //
  628. // The returned bytes are valid until the next URI method call.
  629. func (u *URI) LastPathSegment() []byte {
  630. path := u.Path()
  631. n := bytes.LastIndexByte(path, '/')
  632. if n < 0 {
  633. return path
  634. }
  635. return path[n+1:]
  636. }
  637. // Update updates uri.
  638. //
  639. // The following newURI types are accepted:
  640. //
  641. // - Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
  642. // uri is replaced by newURI.
  643. // - Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
  644. // the original scheme is preserved.
  645. // - Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
  646. // of the original uri is replaced.
  647. // - Relative path, i.e. xx?yy=abc . In this case the original RequestURI
  648. // is updated according to the new relative path.
  649. func (u *URI) Update(newURI string) {
  650. u.UpdateBytes(s2b(newURI))
  651. }
  652. // UpdateBytes updates uri.
  653. //
  654. // The following newURI types are accepted:
  655. //
  656. // - Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
  657. // uri is replaced by newURI.
  658. // - Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
  659. // the original scheme is preserved.
  660. // - Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
  661. // of the original uri is replaced.
  662. // - Relative path, i.e. xx?yy=abc . In this case the original RequestURI
  663. // is updated according to the new relative path.
  664. func (u *URI) UpdateBytes(newURI []byte) {
  665. u.requestURI = u.updateBytes(newURI, u.requestURI)
  666. }
  667. func (u *URI) updateBytes(newURI, buf []byte) []byte {
  668. if len(newURI) == 0 {
  669. return buf
  670. }
  671. n := bytes.Index(newURI, strSlashSlash)
  672. if n >= 0 {
  673. // absolute uri
  674. var b [32]byte
  675. schemeOriginal := b[:0]
  676. if len(u.scheme) > 0 {
  677. schemeOriginal = append([]byte(nil), u.scheme...)
  678. }
  679. if err := u.Parse(nil, newURI); err != nil {
  680. return nil
  681. }
  682. if len(schemeOriginal) > 0 && len(u.scheme) == 0 {
  683. u.scheme = append(u.scheme[:0], schemeOriginal...)
  684. }
  685. return buf
  686. }
  687. if newURI[0] == '/' {
  688. // uri without host
  689. buf = u.appendSchemeHost(buf[:0])
  690. buf = append(buf, newURI...)
  691. if err := u.Parse(nil, buf); err != nil {
  692. return nil
  693. }
  694. return buf
  695. }
  696. // relative path
  697. switch newURI[0] {
  698. case '?':
  699. // query string only update
  700. u.SetQueryStringBytes(newURI[1:])
  701. return append(buf[:0], u.FullURI()...)
  702. case '#':
  703. // update only hash
  704. u.SetHashBytes(newURI[1:])
  705. return append(buf[:0], u.FullURI()...)
  706. default:
  707. // update the last path part after the slash
  708. path := u.Path()
  709. n = bytes.LastIndexByte(path, '/')
  710. if n < 0 {
  711. panic(fmt.Sprintf("BUG: path must contain at least one slash: %q %q", u.Path(), newURI))
  712. }
  713. buf = u.appendSchemeHost(buf[:0])
  714. buf = appendQuotedPath(buf, path[:n+1])
  715. buf = append(buf, newURI...)
  716. if err := u.Parse(nil, buf); err != nil {
  717. return nil
  718. }
  719. return buf
  720. }
  721. }
  722. // FullURI returns full uri in the form {Scheme}://{Host}{RequestURI}#{Hash}.
  723. //
  724. // The returned bytes are valid until the next URI method call.
  725. func (u *URI) FullURI() []byte {
  726. u.fullURI = u.AppendBytes(u.fullURI[:0])
  727. return u.fullURI
  728. }
  729. // AppendBytes appends full uri to dst and returns the extended dst.
  730. func (u *URI) AppendBytes(dst []byte) []byte {
  731. dst = u.appendSchemeHost(dst)
  732. dst = append(dst, u.RequestURI()...)
  733. if len(u.hash) > 0 {
  734. dst = append(dst, '#')
  735. dst = append(dst, u.hash...)
  736. }
  737. return dst
  738. }
  739. func (u *URI) appendSchemeHost(dst []byte) []byte {
  740. dst = append(dst, u.Scheme()...)
  741. dst = append(dst, strColonSlashSlash...)
  742. return append(dst, u.Host()...)
  743. }
  744. // WriteTo writes full uri to w.
  745. //
  746. // WriteTo implements io.WriterTo interface.
  747. func (u *URI) WriteTo(w io.Writer) (int64, error) {
  748. n, err := w.Write(u.FullURI())
  749. return int64(n), err
  750. }
  751. // String returns full uri.
  752. func (u *URI) String() string {
  753. return string(u.FullURI())
  754. }
  755. func splitHostURI(host, uri []byte) ([]byte, []byte, []byte) {
  756. n := bytes.Index(uri, strSlashSlash)
  757. if n < 0 {
  758. return strHTTP, host, uri
  759. }
  760. scheme := uri[:n]
  761. if bytes.IndexByte(scheme, '/') >= 0 {
  762. return strHTTP, host, uri
  763. }
  764. if len(scheme) > 0 && scheme[len(scheme)-1] == ':' {
  765. scheme = scheme[:len(scheme)-1]
  766. }
  767. n += len(strSlashSlash)
  768. uri = uri[n:]
  769. n = bytes.IndexByte(uri, '/')
  770. nq := bytes.IndexByte(uri, '?')
  771. if nq >= 0 && nq < n {
  772. // A hack for urls like foobar.com?a=b/xyz
  773. n = nq
  774. } else if n < 0 {
  775. // A hack for bogus urls like foobar.com?a=b without
  776. // slash after host.
  777. if nq >= 0 {
  778. return scheme, uri[:nq], uri[nq:]
  779. }
  780. return scheme, uri, strSlash
  781. }
  782. return scheme, uri[:n], uri[n:]
  783. }
  784. // QueryArgs returns query args.
  785. //
  786. // The returned args are valid until the next URI method call.
  787. func (u *URI) QueryArgs() *Args {
  788. u.parseQueryArgs()
  789. return &u.queryArgs
  790. }
  791. func (u *URI) parseQueryArgs() {
  792. if u.parsedQueryArgs {
  793. return
  794. }
  795. u.queryArgs.ParseBytes(u.queryString)
  796. u.parsedQueryArgs = true
  797. }
  798. // stringContainsCTLByte reports whether s contains any ASCII control character.
  799. func stringContainsCTLByte(s []byte) bool {
  800. for i := 0; i < len(s); i++ {
  801. b := s[i]
  802. if b < ' ' || b == 0x7f {
  803. return true
  804. }
  805. }
  806. return false
  807. }