nsURLParsers.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include <string.h>
  6. #include "mozilla/RangedPtr.h"
  7. #include "nsURLParsers.h"
  8. #include "nsURLHelper.h"
  9. #include "nsString.h"
  10. #include "nsCRT.h"
  11. using namespace mozilla;
  12. //----------------------------------------------------------------------------
  13. static uint32_t
  14. CountConsecutiveSlashes(const char *str, int32_t len)
  15. {
  16. RangedPtr<const char> p(str, len);
  17. uint32_t count = 0;
  18. while (len-- && *p++ == '/') ++count;
  19. return count;
  20. }
  21. //----------------------------------------------------------------------------
  22. // nsBaseURLParser implementation
  23. //----------------------------------------------------------------------------
  24. NS_IMPL_ISUPPORTS(nsAuthURLParser, nsIURLParser)
  25. NS_IMPL_ISUPPORTS(nsNoAuthURLParser, nsIURLParser)
  26. #define SET_RESULT(component, pos, len) \
  27. PR_BEGIN_MACRO \
  28. if (component ## Pos) \
  29. *component ## Pos = uint32_t(pos); \
  30. if (component ## Len) \
  31. *component ## Len = int32_t(len); \
  32. PR_END_MACRO
  33. #define OFFSET_RESULT(component, offset) \
  34. PR_BEGIN_MACRO \
  35. if (component ## Pos) \
  36. *component ## Pos += offset; \
  37. PR_END_MACRO
  38. NS_IMETHODIMP
  39. nsBaseURLParser::ParseURL(const char *spec, int32_t specLen,
  40. uint32_t *schemePos, int32_t *schemeLen,
  41. uint32_t *authorityPos, int32_t *authorityLen,
  42. uint32_t *pathPos, int32_t *pathLen)
  43. {
  44. if (NS_WARN_IF(!spec)) {
  45. return NS_ERROR_INVALID_POINTER;
  46. }
  47. if (specLen < 0)
  48. specLen = strlen(spec);
  49. const char *stop = nullptr;
  50. const char *colon = nullptr;
  51. const char *slash = nullptr;
  52. const char *p = spec;
  53. uint32_t offset = 0;
  54. int32_t len = specLen;
  55. // skip leading whitespace
  56. while (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') {
  57. spec++;
  58. specLen--;
  59. offset++;
  60. p++;
  61. len--;
  62. }
  63. for (; len && *p && !colon && !slash; ++p, --len) {
  64. switch (*p) {
  65. case ':':
  66. if (!colon)
  67. colon = p;
  68. break;
  69. case '/': // start of filepath
  70. case '?': // start of query
  71. case '#': // start of ref
  72. if (!slash)
  73. slash = p;
  74. break;
  75. case '@': // username@hostname
  76. case '[': // start of IPv6 address literal
  77. if (!stop)
  78. stop = p;
  79. break;
  80. }
  81. }
  82. // disregard the first colon if it follows an '@' or a '['
  83. if (colon && stop && colon > stop)
  84. colon = nullptr;
  85. // if the spec only contained whitespace ...
  86. if (specLen == 0) {
  87. SET_RESULT(scheme, 0, -1);
  88. SET_RESULT(authority, 0, 0);
  89. SET_RESULT(path, 0, 0);
  90. return NS_OK;
  91. }
  92. // ignore trailing whitespace and control characters
  93. for (p = spec + specLen - 1; ((unsigned char) *p <= ' ') && (p != spec); --p)
  94. ;
  95. specLen = p - spec + 1;
  96. if (colon && (colon < slash || !slash)) {
  97. //
  98. // spec = <scheme>:/<the-rest>
  99. //
  100. // or
  101. //
  102. // spec = <scheme>:<authority>
  103. // spec = <scheme>:<path-no-slashes>
  104. //
  105. if (!net_IsValidScheme(spec, colon - spec) || (*(colon+1) == ':')) {
  106. return NS_ERROR_MALFORMED_URI;
  107. }
  108. SET_RESULT(scheme, offset, colon - spec);
  109. if (authorityLen || pathLen) {
  110. uint32_t schemeLen = colon + 1 - spec;
  111. offset += schemeLen;
  112. ParseAfterScheme(colon + 1, specLen - schemeLen,
  113. authorityPos, authorityLen,
  114. pathPos, pathLen);
  115. OFFSET_RESULT(authority, offset);
  116. OFFSET_RESULT(path, offset);
  117. }
  118. }
  119. else {
  120. //
  121. // spec = <authority-no-port-or-password>/<path>
  122. // spec = <path>
  123. //
  124. // or
  125. //
  126. // spec = <authority-no-port-or-password>/<path-with-colon>
  127. // spec = <path-with-colon>
  128. //
  129. // or
  130. //
  131. // spec = <authority-no-port-or-password>
  132. // spec = <path-no-slashes-or-colon>
  133. //
  134. SET_RESULT(scheme, 0, -1);
  135. if (authorityLen || pathLen) {
  136. ParseAfterScheme(spec, specLen,
  137. authorityPos, authorityLen,
  138. pathPos, pathLen);
  139. OFFSET_RESULT(authority, offset);
  140. OFFSET_RESULT(path, offset);
  141. }
  142. }
  143. return NS_OK;
  144. }
  145. NS_IMETHODIMP
  146. nsBaseURLParser::ParseAuthority(const char *auth, int32_t authLen,
  147. uint32_t *usernamePos, int32_t *usernameLen,
  148. uint32_t *passwordPos, int32_t *passwordLen,
  149. uint32_t *hostnamePos, int32_t *hostnameLen,
  150. int32_t *port)
  151. {
  152. if (NS_WARN_IF(!auth)) {
  153. return NS_ERROR_INVALID_POINTER;
  154. }
  155. if (authLen < 0)
  156. authLen = strlen(auth);
  157. SET_RESULT(username, 0, -1);
  158. SET_RESULT(password, 0, -1);
  159. SET_RESULT(hostname, 0, authLen);
  160. if (port)
  161. *port = -1;
  162. return NS_OK;
  163. }
  164. NS_IMETHODIMP
  165. nsBaseURLParser::ParseUserInfo(const char *userinfo, int32_t userinfoLen,
  166. uint32_t *usernamePos, int32_t *usernameLen,
  167. uint32_t *passwordPos, int32_t *passwordLen)
  168. {
  169. SET_RESULT(username, 0, -1);
  170. SET_RESULT(password, 0, -1);
  171. return NS_OK;
  172. }
  173. NS_IMETHODIMP
  174. nsBaseURLParser::ParseServerInfo(const char *serverinfo, int32_t serverinfoLen,
  175. uint32_t *hostnamePos, int32_t *hostnameLen,
  176. int32_t *port)
  177. {
  178. SET_RESULT(hostname, 0, -1);
  179. if (port)
  180. *port = -1;
  181. return NS_OK;
  182. }
  183. NS_IMETHODIMP
  184. nsBaseURLParser::ParsePath(const char *path, int32_t pathLen,
  185. uint32_t *filepathPos, int32_t *filepathLen,
  186. uint32_t *queryPos, int32_t *queryLen,
  187. uint32_t *refPos, int32_t *refLen)
  188. {
  189. if (NS_WARN_IF(!path)) {
  190. return NS_ERROR_INVALID_POINTER;
  191. }
  192. if (pathLen < 0)
  193. pathLen = strlen(path);
  194. // path = [/]<segment1>/<segment2>/<...>/<segmentN>?<query>#<ref>
  195. // XXX PL_strnpbrk would be nice, but it's buggy
  196. // search for first occurrence of either ? or #
  197. const char *query_beg = 0, *query_end = 0;
  198. const char *ref_beg = 0;
  199. const char *p = 0;
  200. for (p = path; p < path + pathLen; ++p) {
  201. // only match the query string if it precedes the reference fragment
  202. if (!ref_beg && !query_beg && *p == '?')
  203. query_beg = p + 1;
  204. else if (*p == '#') {
  205. ref_beg = p + 1;
  206. if (query_beg)
  207. query_end = p;
  208. break;
  209. }
  210. }
  211. if (query_beg) {
  212. if (query_end)
  213. SET_RESULT(query, query_beg - path, query_end - query_beg);
  214. else
  215. SET_RESULT(query, query_beg - path, pathLen - (query_beg - path));
  216. }
  217. else
  218. SET_RESULT(query, 0, -1);
  219. if (ref_beg)
  220. SET_RESULT(ref, ref_beg - path, pathLen - (ref_beg - path));
  221. else
  222. SET_RESULT(ref, 0, -1);
  223. const char *end;
  224. if (query_beg)
  225. end = query_beg - 1;
  226. else if (ref_beg)
  227. end = ref_beg - 1;
  228. else
  229. end = path + pathLen;
  230. // an empty file path is no file path
  231. if (end != path)
  232. SET_RESULT(filepath, 0, end - path);
  233. else
  234. SET_RESULT(filepath, 0, -1);
  235. return NS_OK;
  236. }
  237. NS_IMETHODIMP
  238. nsBaseURLParser::ParseFilePath(const char *filepath, int32_t filepathLen,
  239. uint32_t *directoryPos, int32_t *directoryLen,
  240. uint32_t *basenamePos, int32_t *basenameLen,
  241. uint32_t *extensionPos, int32_t *extensionLen)
  242. {
  243. if (NS_WARN_IF(!filepath)) {
  244. return NS_ERROR_INVALID_POINTER;
  245. }
  246. if (filepathLen < 0)
  247. filepathLen = strlen(filepath);
  248. if (filepathLen == 0) {
  249. SET_RESULT(directory, 0, -1);
  250. SET_RESULT(basename, 0, 0); // assume a zero length file basename
  251. SET_RESULT(extension, 0, -1);
  252. return NS_OK;
  253. }
  254. const char *p;
  255. const char *end = filepath + filepathLen;
  256. // search backwards for filename
  257. for (p = end - 1; *p != '/' && p > filepath; --p)
  258. ;
  259. if (*p == '/') {
  260. // catch /.. and /.
  261. if ((p+1 < end && *(p+1) == '.') &&
  262. (p+2 == end || (*(p+2) == '.' && p+3 == end)))
  263. p = end - 1;
  264. // filepath = <directory><filename>.<extension>
  265. SET_RESULT(directory, 0, p - filepath + 1);
  266. ParseFileName(p + 1, end - (p + 1),
  267. basenamePos, basenameLen,
  268. extensionPos, extensionLen);
  269. OFFSET_RESULT(basename, p + 1 - filepath);
  270. OFFSET_RESULT(extension, p + 1 - filepath);
  271. }
  272. else {
  273. // filepath = <filename>.<extension>
  274. SET_RESULT(directory, 0, -1);
  275. ParseFileName(filepath, filepathLen,
  276. basenamePos, basenameLen,
  277. extensionPos, extensionLen);
  278. }
  279. return NS_OK;
  280. }
  281. nsresult
  282. nsBaseURLParser::ParseFileName(const char *filename, int32_t filenameLen,
  283. uint32_t *basenamePos, int32_t *basenameLen,
  284. uint32_t *extensionPos, int32_t *extensionLen)
  285. {
  286. if (NS_WARN_IF(!filename)) {
  287. return NS_ERROR_INVALID_POINTER;
  288. }
  289. if (filenameLen < 0)
  290. filenameLen = strlen(filename);
  291. // no extension if filename ends with a '.'
  292. if (filename[filenameLen-1] != '.') {
  293. // ignore '.' at the beginning
  294. for (const char *p = filename + filenameLen - 1; p > filename; --p) {
  295. if (*p == '.') {
  296. // filename = <basename.extension>
  297. SET_RESULT(basename, 0, p - filename);
  298. SET_RESULT(extension, p + 1 - filename, filenameLen - (p - filename + 1));
  299. return NS_OK;
  300. }
  301. }
  302. }
  303. // filename = <basename>
  304. SET_RESULT(basename, 0, filenameLen);
  305. SET_RESULT(extension, 0, -1);
  306. return NS_OK;
  307. }
  308. //----------------------------------------------------------------------------
  309. // nsNoAuthURLParser implementation
  310. //----------------------------------------------------------------------------
  311. NS_IMETHODIMP
  312. nsNoAuthURLParser::ParseAuthority(const char *auth, int32_t authLen,
  313. uint32_t *usernamePos, int32_t *usernameLen,
  314. uint32_t *passwordPos, int32_t *passwordLen,
  315. uint32_t *hostnamePos, int32_t *hostnameLen,
  316. int32_t *port)
  317. {
  318. NS_NOTREACHED("Shouldn't parse auth in a NoAuthURL!");
  319. return NS_ERROR_UNEXPECTED;
  320. }
  321. void
  322. nsNoAuthURLParser::ParseAfterScheme(const char *spec, int32_t specLen,
  323. uint32_t *authPos, int32_t *authLen,
  324. uint32_t *pathPos, int32_t *pathLen)
  325. {
  326. NS_PRECONDITION(specLen >= 0, "unexpected");
  327. // everything is the path
  328. uint32_t pos = 0;
  329. switch (CountConsecutiveSlashes(spec, specLen)) {
  330. case 0:
  331. case 1:
  332. break;
  333. case 2:
  334. {
  335. const char *p = nullptr;
  336. if (specLen > 2) {
  337. // looks like there is an authority section
  338. #if defined(XP_WIN)
  339. // if the authority looks like a drive number then we
  340. // really want to treat it as part of the path
  341. // [a-zA-Z][:|]{/\}
  342. // i.e one of: c: c:\foo c:/foo c| c|\foo c|/foo
  343. if ((specLen > 3) && (spec[3] == ':' || spec[3] == '|') &&
  344. nsCRT::IsAsciiAlpha(spec[2]) &&
  345. ((specLen == 4) || (spec[4] == '/') || (spec[4] == '\\'))) {
  346. pos = 1;
  347. break;
  348. }
  349. #endif
  350. // Ignore apparent authority; path is everything after it
  351. for (p = spec + 2; p < spec + specLen; ++p) {
  352. if (*p == '/' || *p == '?' || *p == '#')
  353. break;
  354. }
  355. }
  356. SET_RESULT(auth, 0, -1);
  357. if (p && p != spec+specLen)
  358. SET_RESULT(path, p - spec, specLen - (p - spec));
  359. else
  360. SET_RESULT(path, 0, -1);
  361. return;
  362. }
  363. default:
  364. pos = 2;
  365. break;
  366. }
  367. SET_RESULT(auth, pos, 0);
  368. SET_RESULT(path, pos, specLen - pos);
  369. }
  370. #if defined(XP_WIN)
  371. NS_IMETHODIMP
  372. nsNoAuthURLParser::ParseFilePath(const char *filepath, int32_t filepathLen,
  373. uint32_t *directoryPos, int32_t *directoryLen,
  374. uint32_t *basenamePos, int32_t *basenameLen,
  375. uint32_t *extensionPos, int32_t *extensionLen)
  376. {
  377. if (NS_WARN_IF(!filepath)) {
  378. return NS_ERROR_INVALID_POINTER;
  379. }
  380. if (filepathLen < 0)
  381. filepathLen = strlen(filepath);
  382. // look for a filepath consisting of only a drive number, which may or
  383. // may not have a leading slash.
  384. if (filepathLen > 1 && filepathLen < 4) {
  385. const char *end = filepath + filepathLen;
  386. const char *p = filepath;
  387. if (*p == '/')
  388. p++;
  389. if ((end-p == 2) && (p[1]==':' || p[1]=='|') && nsCRT::IsAsciiAlpha(*p)) {
  390. // filepath = <drive-number>:
  391. SET_RESULT(directory, 0, filepathLen);
  392. SET_RESULT(basename, 0, -1);
  393. SET_RESULT(extension, 0, -1);
  394. return NS_OK;
  395. }
  396. }
  397. // otherwise fallback on common implementation
  398. return nsBaseURLParser::ParseFilePath(filepath, filepathLen,
  399. directoryPos, directoryLen,
  400. basenamePos, basenameLen,
  401. extensionPos, extensionLen);
  402. }
  403. #endif
  404. //----------------------------------------------------------------------------
  405. // nsAuthURLParser implementation
  406. //----------------------------------------------------------------------------
  407. NS_IMETHODIMP
  408. nsAuthURLParser::ParseAuthority(const char *auth, int32_t authLen,
  409. uint32_t *usernamePos, int32_t *usernameLen,
  410. uint32_t *passwordPos, int32_t *passwordLen,
  411. uint32_t *hostnamePos, int32_t *hostnameLen,
  412. int32_t *port)
  413. {
  414. nsresult rv;
  415. if (NS_WARN_IF(!auth)) {
  416. return NS_ERROR_INVALID_POINTER;
  417. }
  418. if (authLen < 0)
  419. authLen = strlen(auth);
  420. if (authLen == 0) {
  421. SET_RESULT(username, 0, -1);
  422. SET_RESULT(password, 0, -1);
  423. SET_RESULT(hostname, 0, 0);
  424. if (port)
  425. *port = -1;
  426. return NS_OK;
  427. }
  428. // search backwards for @
  429. const char *p = auth + authLen - 1;
  430. for (; (*p != '@') && (p > auth); --p) {
  431. continue;
  432. }
  433. if ( *p == '@' ) {
  434. // auth = <user-info@server-info>
  435. rv = ParseUserInfo(auth, p - auth,
  436. usernamePos, usernameLen,
  437. passwordPos, passwordLen);
  438. if (NS_FAILED(rv)) return rv;
  439. rv = ParseServerInfo(p + 1, authLen - (p - auth + 1),
  440. hostnamePos, hostnameLen,
  441. port);
  442. if (NS_FAILED(rv)) return rv;
  443. OFFSET_RESULT(hostname, p + 1 - auth);
  444. // malformed if has a username or password
  445. // but no host info, such as: http://u:p@/
  446. if ((usernamePos || passwordPos) && (!hostnamePos || !*hostnameLen)) {
  447. return NS_ERROR_MALFORMED_URI;
  448. }
  449. }
  450. else {
  451. // auth = <server-info>
  452. SET_RESULT(username, 0, -1);
  453. SET_RESULT(password, 0, -1);
  454. rv = ParseServerInfo(auth, authLen,
  455. hostnamePos, hostnameLen,
  456. port);
  457. if (NS_FAILED(rv)) return rv;
  458. }
  459. return NS_OK;
  460. }
  461. NS_IMETHODIMP
  462. nsAuthURLParser::ParseUserInfo(const char *userinfo, int32_t userinfoLen,
  463. uint32_t *usernamePos, int32_t *usernameLen,
  464. uint32_t *passwordPos, int32_t *passwordLen)
  465. {
  466. if (NS_WARN_IF(!userinfo)) {
  467. return NS_ERROR_INVALID_POINTER;
  468. }
  469. if (userinfoLen < 0)
  470. userinfoLen = strlen(userinfo);
  471. if (userinfoLen == 0) {
  472. SET_RESULT(username, 0, -1);
  473. SET_RESULT(password, 0, -1);
  474. return NS_OK;
  475. }
  476. const char *p = (const char *) memchr(userinfo, ':', userinfoLen);
  477. if (p) {
  478. // userinfo = <username:password>
  479. if (p == userinfo) {
  480. // must have a username!
  481. return NS_ERROR_MALFORMED_URI;
  482. }
  483. SET_RESULT(username, 0, p - userinfo);
  484. SET_RESULT(password, p - userinfo + 1, userinfoLen - (p - userinfo + 1));
  485. }
  486. else {
  487. // userinfo = <username>
  488. SET_RESULT(username, 0, userinfoLen);
  489. SET_RESULT(password, 0, -1);
  490. }
  491. return NS_OK;
  492. }
  493. NS_IMETHODIMP
  494. nsAuthURLParser::ParseServerInfo(const char *serverinfo, int32_t serverinfoLen,
  495. uint32_t *hostnamePos, int32_t *hostnameLen,
  496. int32_t *port)
  497. {
  498. if (NS_WARN_IF(!serverinfo)) {
  499. return NS_ERROR_INVALID_POINTER;
  500. }
  501. if (serverinfoLen < 0)
  502. serverinfoLen = strlen(serverinfo);
  503. if (serverinfoLen == 0) {
  504. SET_RESULT(hostname, 0, 0);
  505. if (port)
  506. *port = -1;
  507. return NS_OK;
  508. }
  509. // search backwards for a ':' but stop on ']' (IPv6 address literal
  510. // delimiter). check for illegal characters in the hostname.
  511. const char *p = serverinfo + serverinfoLen - 1;
  512. const char *colon = nullptr, *bracket = nullptr;
  513. for (; p > serverinfo; --p) {
  514. switch (*p) {
  515. case ']':
  516. bracket = p;
  517. break;
  518. case ':':
  519. if (bracket == nullptr)
  520. colon = p;
  521. break;
  522. case ' ':
  523. // hostname must not contain a space
  524. return NS_ERROR_MALFORMED_URI;
  525. }
  526. }
  527. if (colon) {
  528. // serverinfo = <hostname:port>
  529. SET_RESULT(hostname, 0, colon - serverinfo);
  530. if (port) {
  531. // XXX unfortunately ToInteger is not defined for substrings
  532. nsAutoCString buf(colon+1, serverinfoLen - (colon + 1 - serverinfo));
  533. if (buf.Length() == 0) {
  534. *port = -1;
  535. }
  536. else {
  537. const char* nondigit = NS_strspnp("0123456789", buf.get());
  538. if (nondigit && *nondigit)
  539. return NS_ERROR_MALFORMED_URI;
  540. nsresult err;
  541. *port = buf.ToInteger(&err);
  542. if (NS_FAILED(err) || *port < 0 || *port > std::numeric_limits<uint16_t>::max())
  543. return NS_ERROR_MALFORMED_URI;
  544. }
  545. }
  546. }
  547. else {
  548. // serverinfo = <hostname>
  549. SET_RESULT(hostname, 0, serverinfoLen);
  550. if (port)
  551. *port = -1;
  552. }
  553. // In case of IPv6 address check its validity
  554. if (*hostnameLen > 1 && *(serverinfo + *hostnamePos) == '[' &&
  555. *(serverinfo + *hostnamePos + *hostnameLen - 1) == ']' &&
  556. !net_IsValidIPv6Addr(serverinfo + *hostnamePos + 1, *hostnameLen - 2))
  557. return NS_ERROR_MALFORMED_URI;
  558. return NS_OK;
  559. }
  560. void
  561. nsAuthURLParser::ParseAfterScheme(const char *spec, int32_t specLen,
  562. uint32_t *authPos, int32_t *authLen,
  563. uint32_t *pathPos, int32_t *pathLen)
  564. {
  565. NS_PRECONDITION(specLen >= 0, "unexpected");
  566. uint32_t nslash = CountConsecutiveSlashes(spec, specLen);
  567. // search for the end of the authority section
  568. const char *end = spec + specLen;
  569. const char *p;
  570. for (p = spec + nslash; p < end; ++p) {
  571. if (*p == '/' || *p == '?' || *p == '#')
  572. break;
  573. }
  574. if (p < end) {
  575. // spec = [/]<auth><path>
  576. SET_RESULT(auth, nslash, p - (spec + nslash));
  577. SET_RESULT(path, p - spec, specLen - (p - spec));
  578. }
  579. else {
  580. // spec = [/]<auth>
  581. SET_RESULT(auth, nslash, specLen - nslash);
  582. SET_RESULT(path, 0, -1);
  583. }
  584. }
  585. //----------------------------------------------------------------------------
  586. // nsStdURLParser implementation
  587. //----------------------------------------------------------------------------
  588. void
  589. nsStdURLParser::ParseAfterScheme(const char *spec, int32_t specLen,
  590. uint32_t *authPos, int32_t *authLen,
  591. uint32_t *pathPos, int32_t *pathLen)
  592. {
  593. NS_PRECONDITION(specLen >= 0, "unexpected");
  594. uint32_t nslash = CountConsecutiveSlashes(spec, specLen);
  595. // search for the end of the authority section
  596. const char *end = spec + specLen;
  597. const char *p;
  598. for (p = spec + nslash; p < end; ++p) {
  599. if (strchr("/?#;", *p))
  600. break;
  601. }
  602. switch (nslash) {
  603. case 0:
  604. case 2:
  605. if (p < end) {
  606. // spec = (//)<auth><path>
  607. SET_RESULT(auth, nslash, p - (spec + nslash));
  608. SET_RESULT(path, p - spec, specLen - (p - spec));
  609. }
  610. else {
  611. // spec = (//)<auth>
  612. SET_RESULT(auth, nslash, specLen - nslash);
  613. SET_RESULT(path, 0, -1);
  614. }
  615. break;
  616. case 1:
  617. // spec = /<path>
  618. SET_RESULT(auth, 0, -1);
  619. SET_RESULT(path, 0, specLen);
  620. break;
  621. default:
  622. // spec = ///[/]<path>
  623. SET_RESULT(auth, 2, 0);
  624. SET_RESULT(path, 2, specLen - 2);
  625. }
  626. }