uri.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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. queryArgs Args
  40. pathOriginal []byte
  41. scheme []byte
  42. path []byte
  43. queryString []byte
  44. hash []byte
  45. host []byte
  46. fullURI []byte
  47. requestURI []byte
  48. username []byte
  49. password []byte
  50. parsedQueryArgs bool
  51. // Path values are sent as-is without normalization.
  52. //
  53. // Disabled path normalization may be useful for proxying incoming requests
  54. // to servers that are expecting paths to be forwarded as-is.
  55. //
  56. // By default path values are normalized, i.e.
  57. // extra slashes are removed, special characters are encoded.
  58. DisablePathNormalizing bool
  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. parsedHost, err := parseHost(u.host)
  266. if err != nil {
  267. return err
  268. }
  269. u.host = parsedHost
  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 hex2intTable[c] < 16
  467. }
  468. func unhex(c byte) byte {
  469. return hex2intTable[c] & 15
  470. }
  471. // validOptionalPort reports whether port is either an empty string
  472. // or matches /^:\d*$/.
  473. func validOptionalPort(port []byte) bool {
  474. if len(port) == 0 {
  475. return true
  476. }
  477. if port[0] != ':' {
  478. return false
  479. }
  480. for _, b := range port[1:] {
  481. if b < '0' || b > '9' {
  482. return false
  483. }
  484. }
  485. return true
  486. }
  487. func normalizePath(dst, src []byte) []byte {
  488. dst = dst[:0]
  489. dst = addLeadingSlash(dst, src)
  490. dst = decodeArgAppendNoPlus(dst, src)
  491. // remove duplicate slashes
  492. b := dst
  493. bSize := len(b)
  494. for {
  495. n := bytes.Index(b, strSlashSlash)
  496. if n < 0 {
  497. break
  498. }
  499. b = b[n:]
  500. copy(b, b[1:])
  501. b = b[:len(b)-1]
  502. bSize--
  503. }
  504. dst = dst[:bSize]
  505. // remove /./ parts
  506. b = dst
  507. for {
  508. n := bytes.Index(b, strSlashDotSlash)
  509. if n < 0 {
  510. break
  511. }
  512. nn := n + len(strSlashDotSlash) - 1
  513. copy(b[n:], b[nn:])
  514. b = b[:len(b)-nn+n]
  515. }
  516. // remove /foo/../ parts
  517. for {
  518. n := bytes.Index(b, strSlashDotDotSlash)
  519. if n < 0 {
  520. break
  521. }
  522. nn := bytes.LastIndexByte(b[:n], '/')
  523. if nn < 0 {
  524. nn = 0
  525. }
  526. n += len(strSlashDotDotSlash) - 1
  527. copy(b[nn:], b[n:])
  528. b = b[:len(b)-n+nn]
  529. }
  530. // remove trailing /foo/..
  531. n := bytes.LastIndex(b, strSlashDotDot)
  532. if n >= 0 && n+len(strSlashDotDot) == len(b) {
  533. nn := bytes.LastIndexByte(b[:n], '/')
  534. if nn < 0 {
  535. return append(dst[:0], strSlash...)
  536. }
  537. b = b[:nn+1]
  538. }
  539. if filepath.Separator == '\\' {
  540. // remove \.\ parts
  541. for {
  542. n := bytes.Index(b, strBackSlashDotBackSlash)
  543. if n < 0 {
  544. break
  545. }
  546. nn := n + len(strSlashDotSlash) - 1
  547. copy(b[n:], b[nn:])
  548. b = b[:len(b)-nn+n]
  549. }
  550. // remove /foo/..\ parts
  551. for {
  552. n := bytes.Index(b, strSlashDotDotBackSlash)
  553. if n < 0 {
  554. break
  555. }
  556. nn := bytes.LastIndexByte(b[:n], '/')
  557. if nn < 0 {
  558. nn = 0
  559. }
  560. nn++
  561. n += len(strSlashDotDotBackSlash)
  562. copy(b[nn:], b[n:])
  563. b = b[:len(b)-n+nn]
  564. }
  565. // remove /foo\..\ parts
  566. for {
  567. n := bytes.Index(b, strBackSlashDotDotBackSlash)
  568. if n < 0 {
  569. break
  570. }
  571. nn := bytes.LastIndexByte(b[:n], '/')
  572. if nn < 0 {
  573. nn = 0
  574. }
  575. n += len(strBackSlashDotDotBackSlash) - 1
  576. copy(b[nn:], b[n:])
  577. b = b[:len(b)-n+nn]
  578. }
  579. // remove trailing \foo\..
  580. n := bytes.LastIndex(b, strBackSlashDotDot)
  581. if n >= 0 && n+len(strSlashDotDot) == len(b) {
  582. nn := bytes.LastIndexByte(b[:n], '/')
  583. if nn < 0 {
  584. return append(dst[:0], strSlash...)
  585. }
  586. b = b[:nn+1]
  587. }
  588. }
  589. return b
  590. }
  591. // RequestURI returns RequestURI - i.e. URI without Scheme and Host.
  592. func (u *URI) RequestURI() []byte {
  593. var dst []byte
  594. if u.DisablePathNormalizing {
  595. dst = u.requestURI[:0]
  596. dst = append(dst, u.PathOriginal()...)
  597. } else {
  598. dst = appendQuotedPath(u.requestURI[:0], u.Path())
  599. }
  600. if u.parsedQueryArgs && u.queryArgs.Len() > 0 {
  601. dst = append(dst, '?')
  602. dst = u.queryArgs.AppendBytes(dst)
  603. } else if len(u.queryString) > 0 {
  604. dst = append(dst, '?')
  605. dst = append(dst, u.queryString...)
  606. }
  607. u.requestURI = dst
  608. return u.requestURI
  609. }
  610. // LastPathSegment returns the last part of uri path after '/'.
  611. //
  612. // Examples:
  613. //
  614. // - For /foo/bar/baz.html path returns baz.html.
  615. // - For /foo/bar/ returns empty byte slice.
  616. // - For /foobar.js returns foobar.js.
  617. //
  618. // The returned bytes are valid until the next URI method call.
  619. func (u *URI) LastPathSegment() []byte {
  620. path := u.Path()
  621. n := bytes.LastIndexByte(path, '/')
  622. if n < 0 {
  623. return path
  624. }
  625. return path[n+1:]
  626. }
  627. // Update updates uri.
  628. //
  629. // The following newURI types are accepted:
  630. //
  631. // - Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
  632. // uri is replaced by newURI.
  633. // - Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
  634. // the original scheme is preserved.
  635. // - Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
  636. // of the original uri is replaced.
  637. // - Relative path, i.e. xx?yy=abc . In this case the original RequestURI
  638. // is updated according to the new relative path.
  639. func (u *URI) Update(newURI string) {
  640. u.UpdateBytes(s2b(newURI))
  641. }
  642. // UpdateBytes updates uri.
  643. //
  644. // The following newURI types are accepted:
  645. //
  646. // - Absolute, i.e. http://foobar.com/aaa/bb?cc . In this case the original
  647. // uri is replaced by newURI.
  648. // - Absolute without scheme, i.e. //foobar.com/aaa/bb?cc. In this case
  649. // the original scheme is preserved.
  650. // - Missing host, i.e. /aaa/bb?cc . In this case only RequestURI part
  651. // of the original uri is replaced.
  652. // - Relative path, i.e. xx?yy=abc . In this case the original RequestURI
  653. // is updated according to the new relative path.
  654. func (u *URI) UpdateBytes(newURI []byte) {
  655. u.requestURI = u.updateBytes(newURI, u.requestURI)
  656. }
  657. func (u *URI) updateBytes(newURI, buf []byte) []byte {
  658. if len(newURI) == 0 {
  659. return buf
  660. }
  661. n := bytes.Index(newURI, strSlashSlash)
  662. if n >= 0 {
  663. // absolute uri
  664. var b [32]byte
  665. schemeOriginal := b[:0]
  666. if len(u.scheme) > 0 {
  667. schemeOriginal = append([]byte(nil), u.scheme...)
  668. }
  669. if err := u.Parse(nil, newURI); err != nil {
  670. return nil
  671. }
  672. if len(schemeOriginal) > 0 && len(u.scheme) == 0 {
  673. u.scheme = append(u.scheme[:0], schemeOriginal...)
  674. }
  675. return buf
  676. }
  677. if newURI[0] == '/' {
  678. // uri without host
  679. buf = u.appendSchemeHost(buf[:0])
  680. buf = append(buf, newURI...)
  681. if err := u.Parse(nil, buf); err != nil {
  682. return nil
  683. }
  684. return buf
  685. }
  686. // relative path
  687. switch newURI[0] {
  688. case '?':
  689. // query string only update
  690. u.SetQueryStringBytes(newURI[1:])
  691. return append(buf[:0], u.FullURI()...)
  692. case '#':
  693. // update only hash
  694. u.SetHashBytes(newURI[1:])
  695. return append(buf[:0], u.FullURI()...)
  696. default:
  697. // update the last path part after the slash
  698. path := u.Path()
  699. n = bytes.LastIndexByte(path, '/')
  700. if n < 0 {
  701. panic(fmt.Sprintf("BUG: path must contain at least one slash: %q %q", u.Path(), newURI))
  702. }
  703. buf = u.appendSchemeHost(buf[:0])
  704. buf = appendQuotedPath(buf, path[:n+1])
  705. buf = append(buf, newURI...)
  706. if err := u.Parse(nil, buf); err != nil {
  707. return nil
  708. }
  709. return buf
  710. }
  711. }
  712. // FullURI returns full uri in the form {Scheme}://{Host}{RequestURI}#{Hash}.
  713. //
  714. // The returned bytes are valid until the next URI method call.
  715. func (u *URI) FullURI() []byte {
  716. u.fullURI = u.AppendBytes(u.fullURI[:0])
  717. return u.fullURI
  718. }
  719. // AppendBytes appends full uri to dst and returns the extended dst.
  720. func (u *URI) AppendBytes(dst []byte) []byte {
  721. dst = u.appendSchemeHost(dst)
  722. dst = append(dst, u.RequestURI()...)
  723. if len(u.hash) > 0 {
  724. dst = append(dst, '#')
  725. dst = append(dst, u.hash...)
  726. }
  727. return dst
  728. }
  729. func (u *URI) appendSchemeHost(dst []byte) []byte {
  730. dst = append(dst, u.Scheme()...)
  731. dst = append(dst, strColonSlashSlash...)
  732. return append(dst, u.Host()...)
  733. }
  734. // WriteTo writes full uri to w.
  735. //
  736. // WriteTo implements io.WriterTo interface.
  737. func (u *URI) WriteTo(w io.Writer) (int64, error) {
  738. n, err := w.Write(u.FullURI())
  739. return int64(n), err
  740. }
  741. // String returns full uri.
  742. func (u *URI) String() string {
  743. return string(u.FullURI())
  744. }
  745. func splitHostURI(host, uri []byte) ([]byte, []byte, []byte) {
  746. n := bytes.Index(uri, strSlashSlash)
  747. if n < 0 {
  748. return strHTTP, host, uri
  749. }
  750. scheme := uri[:n]
  751. if bytes.IndexByte(scheme, '/') >= 0 {
  752. return strHTTP, host, uri
  753. }
  754. if len(scheme) > 0 && scheme[len(scheme)-1] == ':' {
  755. scheme = scheme[:len(scheme)-1]
  756. }
  757. n += len(strSlashSlash)
  758. uri = uri[n:]
  759. n = bytes.IndexByte(uri, '/')
  760. nq := bytes.IndexByte(uri, '?')
  761. if nq >= 0 && (n < 0 || nq < n) {
  762. // A hack for urls like foobar.com?a=b/xyz
  763. n = nq
  764. }
  765. nh := bytes.IndexByte(uri, '#')
  766. if nh >= 0 && (n < 0 || nh < n) {
  767. // A hack for urls like foobar.com#abc.com
  768. n = nh
  769. }
  770. if n < 0 {
  771. return scheme, uri, strSlash
  772. }
  773. return scheme, uri[:n], uri[n:]
  774. }
  775. // QueryArgs returns query args.
  776. //
  777. // The returned args are valid until the next URI method call.
  778. func (u *URI) QueryArgs() *Args {
  779. u.parseQueryArgs()
  780. return &u.queryArgs
  781. }
  782. func (u *URI) parseQueryArgs() {
  783. if u.parsedQueryArgs {
  784. return
  785. }
  786. u.queryArgs.ParseBytes(u.queryString)
  787. u.parsedQueryArgs = true
  788. }
  789. // stringContainsCTLByte reports whether s contains any ASCII control character.
  790. func stringContainsCTLByte(s []byte) bool {
  791. for i := 0; i < len(s); i++ {
  792. b := s[i]
  793. if b < ' ' || b == 0x7f {
  794. return true
  795. }
  796. }
  797. return false
  798. }