2
0

uri.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324
  1. /**
  2. * uri.c: set of generic URI related routines
  3. *
  4. * Reference: RFCs 3986, 2732 and 2373
  5. *
  6. * Copyright (C) 1998-2003 Daniel Veillard. All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * DANIEL VEILLARD BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  22. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Except as contained in this notice, the name of Daniel Veillard shall not
  26. * be used in advertising or otherwise to promote the sale, use or other
  27. * dealings in this Software without prior written authorization from him.
  28. *
  29. * daniel@veillard.com
  30. *
  31. **
  32. *
  33. * Copyright (C) 2007, 2009-2010 Red Hat, Inc.
  34. *
  35. * This library is free software; you can redistribute it and/or
  36. * modify it under the terms of the GNU Lesser General Public
  37. * License as published by the Free Software Foundation; either
  38. * version 2.1 of the License, or (at your option) any later version.
  39. *
  40. * This library is distributed in the hope that it will be useful,
  41. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  43. * Lesser General Public License for more details.
  44. *
  45. * You should have received a copy of the GNU Lesser General Public
  46. * License along with this library; if not, write to the Free Software
  47. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  48. *
  49. * Authors:
  50. * Richard W.M. Jones <rjones@redhat.com>
  51. *
  52. */
  53. #include "qemu/osdep.h"
  54. #include "qemu/cutils.h"
  55. #include "qemu/uri.h"
  56. static void uri_clean(URI *uri);
  57. /*
  58. * Old rule from 2396 used in legacy handling code
  59. * alpha = lowalpha | upalpha
  60. */
  61. #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
  62. /*
  63. * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
  64. * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
  65. * "u" | "v" | "w" | "x" | "y" | "z"
  66. */
  67. #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z'))
  68. /*
  69. * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" |
  70. * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" |
  71. * "U" | "V" | "W" | "X" | "Y" | "Z"
  72. */
  73. #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z'))
  74. #ifdef IS_DIGIT
  75. #undef IS_DIGIT
  76. #endif
  77. /*
  78. * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
  79. */
  80. #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
  81. /*
  82. * alphanum = alpha | digit
  83. */
  84. #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
  85. /*
  86. * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
  87. */
  88. #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
  89. ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
  90. ((x) == '(') || ((x) == ')'))
  91. /*
  92. * unwise = "{" | "}" | "|" | "\" | "^" | "`"
  93. */
  94. #define IS_UNWISE(p) \
  95. (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \
  96. ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \
  97. ((*(p) == ']')) || ((*(p) == '`')))
  98. /*
  99. * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
  100. * "[" | "]"
  101. */
  102. #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
  103. ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
  104. ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \
  105. ((x) == ']'))
  106. /*
  107. * unreserved = alphanum | mark
  108. */
  109. #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
  110. /*
  111. * Skip to next pointer char, handle escaped sequences
  112. */
  113. #define NEXT(p) ((*p == '%') ? p += 3 : p++)
  114. /*
  115. * Productions from the spec.
  116. *
  117. * authority = server | reg_name
  118. * reg_name = 1*( unreserved | escaped | "$" | "," |
  119. * ";" | ":" | "@" | "&" | "=" | "+" )
  120. *
  121. * path = [ abs_path | opaque_part ]
  122. */
  123. /************************************************************************
  124. * *
  125. * RFC 3986 parser *
  126. * *
  127. ************************************************************************/
  128. #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9'))
  129. #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \
  130. ((*(p) >= 'A') && (*(p) <= 'Z')))
  131. #define ISA_HEXDIG(p) \
  132. (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \
  133. ((*(p) >= 'A') && (*(p) <= 'F')))
  134. /*
  135. * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  136. * / "*" / "+" / "," / ";" / "="
  137. */
  138. #define ISA_SUB_DELIM(p) \
  139. (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \
  140. ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \
  141. ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \
  142. ((*(p) == '=')) || ((*(p) == '\'')))
  143. /*
  144. * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
  145. */
  146. #define ISA_GEN_DELIM(p) \
  147. (((*(p) == ':')) || ((*(p) == '/')) || ((*(p) == '?')) || \
  148. ((*(p) == '#')) || ((*(p) == '[')) || ((*(p) == ']')) || \
  149. ((*(p) == '@')))
  150. /*
  151. * reserved = gen-delims / sub-delims
  152. */
  153. #define ISA_RESERVED(p) (ISA_GEN_DELIM(p) || (ISA_SUB_DELIM(p)))
  154. /*
  155. * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  156. */
  157. #define ISA_UNRESERVED(p) \
  158. ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \
  159. ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
  160. /*
  161. * pct-encoded = "%" HEXDIG HEXDIG
  162. */
  163. #define ISA_PCT_ENCODED(p) \
  164. ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2)))
  165. /*
  166. * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  167. */
  168. #define ISA_PCHAR(p) \
  169. (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \
  170. ((*(p) == ':')) || ((*(p) == '@')))
  171. /**
  172. * rfc3986_parse_scheme:
  173. * @uri: pointer to an URI structure
  174. * @str: pointer to the string to analyze
  175. *
  176. * Parse an URI scheme
  177. *
  178. * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  179. *
  180. * Returns 0 or the error code
  181. */
  182. static int rfc3986_parse_scheme(URI *uri, const char **str)
  183. {
  184. const char *cur;
  185. if (str == NULL) {
  186. return -1;
  187. }
  188. cur = *str;
  189. if (!ISA_ALPHA(cur)) {
  190. return 2;
  191. }
  192. cur++;
  193. while (ISA_ALPHA(cur) || ISA_DIGIT(cur) || (*cur == '+') || (*cur == '-') ||
  194. (*cur == '.')) {
  195. cur++;
  196. }
  197. if (uri != NULL) {
  198. g_free(uri->scheme);
  199. uri->scheme = g_strndup(*str, cur - *str);
  200. }
  201. *str = cur;
  202. return 0;
  203. }
  204. /**
  205. * rfc3986_parse_fragment:
  206. * @uri: pointer to an URI structure
  207. * @str: pointer to the string to analyze
  208. *
  209. * Parse the query part of an URI
  210. *
  211. * fragment = *( pchar / "/" / "?" )
  212. * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']'
  213. * in the fragment identifier but this is used very broadly for
  214. * xpointer scheme selection, so we are allowing it here to not break
  215. * for example all the DocBook processing chains.
  216. *
  217. * Returns 0 or the error code
  218. */
  219. static int rfc3986_parse_fragment(URI *uri, const char **str)
  220. {
  221. const char *cur;
  222. if (str == NULL) {
  223. return -1;
  224. }
  225. cur = *str;
  226. while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') ||
  227. (*cur == '[') || (*cur == ']') ||
  228. ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) {
  229. NEXT(cur);
  230. }
  231. if (uri != NULL) {
  232. g_free(uri->fragment);
  233. if (uri->cleanup & 2) {
  234. uri->fragment = g_strndup(*str, cur - *str);
  235. } else {
  236. uri->fragment = uri_string_unescape(*str, cur - *str, NULL);
  237. }
  238. }
  239. *str = cur;
  240. return 0;
  241. }
  242. /**
  243. * rfc3986_parse_query:
  244. * @uri: pointer to an URI structure
  245. * @str: pointer to the string to analyze
  246. *
  247. * Parse the query part of an URI
  248. *
  249. * query = *uric
  250. *
  251. * Returns 0 or the error code
  252. */
  253. static int rfc3986_parse_query(URI *uri, const char **str)
  254. {
  255. const char *cur;
  256. if (str == NULL) {
  257. return -1;
  258. }
  259. cur = *str;
  260. while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') ||
  261. ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) {
  262. NEXT(cur);
  263. }
  264. if (uri != NULL) {
  265. g_free(uri->query);
  266. uri->query = g_strndup(*str, cur - *str);
  267. }
  268. *str = cur;
  269. return 0;
  270. }
  271. /**
  272. * rfc3986_parse_port:
  273. * @uri: pointer to an URI structure
  274. * @str: the string to analyze
  275. *
  276. * Parse a port part and fills in the appropriate fields
  277. * of the @uri structure
  278. *
  279. * port = *DIGIT
  280. *
  281. * Returns 0 or the error code
  282. */
  283. static int rfc3986_parse_port(URI *uri, const char **str)
  284. {
  285. const char *cur = *str;
  286. int port = 0;
  287. if (ISA_DIGIT(cur)) {
  288. while (ISA_DIGIT(cur)) {
  289. port = port * 10 + (*cur - '0');
  290. if (port > 65535) {
  291. return 1;
  292. }
  293. cur++;
  294. }
  295. if (uri) {
  296. uri->port = port;
  297. }
  298. *str = cur;
  299. return 0;
  300. }
  301. return 1;
  302. }
  303. /**
  304. * rfc3986_parse_user_info:
  305. * @uri: pointer to an URI structure
  306. * @str: the string to analyze
  307. *
  308. * Parse a user information part and fill in the appropriate fields
  309. * of the @uri structure
  310. *
  311. * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
  312. *
  313. * Returns 0 or the error code
  314. */
  315. static int rfc3986_parse_user_info(URI *uri, const char **str)
  316. {
  317. const char *cur;
  318. cur = *str;
  319. while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur) ||
  320. (*cur == ':')) {
  321. NEXT(cur);
  322. }
  323. if (*cur == '@') {
  324. if (uri != NULL) {
  325. g_free(uri->user);
  326. if (uri->cleanup & 2) {
  327. uri->user = g_strndup(*str, cur - *str);
  328. } else {
  329. uri->user = uri_string_unescape(*str, cur - *str, NULL);
  330. }
  331. }
  332. *str = cur;
  333. return 0;
  334. }
  335. return 1;
  336. }
  337. /**
  338. * rfc3986_parse_dec_octet:
  339. * @str: the string to analyze
  340. *
  341. * dec-octet = DIGIT ; 0-9
  342. * / %x31-39 DIGIT ; 10-99
  343. * / "1" 2DIGIT ; 100-199
  344. * / "2" %x30-34 DIGIT ; 200-249
  345. * / "25" %x30-35 ; 250-255
  346. *
  347. * Skip a dec-octet.
  348. *
  349. * Returns 0 if found and skipped, 1 otherwise
  350. */
  351. static int rfc3986_parse_dec_octet(const char **str)
  352. {
  353. const char *cur = *str;
  354. if (!(ISA_DIGIT(cur))) {
  355. return 1;
  356. }
  357. if (!ISA_DIGIT(cur + 1)) {
  358. cur++;
  359. } else if ((*cur != '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur + 2))) {
  360. cur += 2;
  361. } else if ((*cur == '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur + 2))) {
  362. cur += 3;
  363. } else if ((*cur == '2') && (*(cur + 1) >= '0') && (*(cur + 1) <= '4') &&
  364. (ISA_DIGIT(cur + 2))) {
  365. cur += 3;
  366. } else if ((*cur == '2') && (*(cur + 1) == '5') && (*(cur + 2) >= '0') &&
  367. (*(cur + 1) <= '5')) {
  368. cur += 3;
  369. } else {
  370. return 1;
  371. }
  372. *str = cur;
  373. return 0;
  374. }
  375. /**
  376. * rfc3986_parse_host:
  377. * @uri: pointer to an URI structure
  378. * @str: the string to analyze
  379. *
  380. * Parse an host part and fills in the appropriate fields
  381. * of the @uri structure
  382. *
  383. * host = IP-literal / IPv4address / reg-name
  384. * IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  385. * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
  386. * reg-name = *( unreserved / pct-encoded / sub-delims )
  387. *
  388. * Returns 0 or the error code
  389. */
  390. static int rfc3986_parse_host(URI *uri, const char **str)
  391. {
  392. const char *cur = *str;
  393. const char *host;
  394. host = cur;
  395. /*
  396. * IPv6 and future addressing scheme are enclosed between brackets
  397. */
  398. if (*cur == '[') {
  399. cur++;
  400. while ((*cur != ']') && (*cur != 0)) {
  401. cur++;
  402. }
  403. if (*cur != ']') {
  404. return 1;
  405. }
  406. cur++;
  407. goto found;
  408. }
  409. /*
  410. * try to parse an IPv4
  411. */
  412. if (ISA_DIGIT(cur)) {
  413. if (rfc3986_parse_dec_octet(&cur) != 0) {
  414. goto not_ipv4;
  415. }
  416. if (*cur != '.') {
  417. goto not_ipv4;
  418. }
  419. cur++;
  420. if (rfc3986_parse_dec_octet(&cur) != 0) {
  421. goto not_ipv4;
  422. }
  423. if (*cur != '.') {
  424. goto not_ipv4;
  425. }
  426. if (rfc3986_parse_dec_octet(&cur) != 0) {
  427. goto not_ipv4;
  428. }
  429. if (*cur != '.') {
  430. goto not_ipv4;
  431. }
  432. if (rfc3986_parse_dec_octet(&cur) != 0) {
  433. goto not_ipv4;
  434. }
  435. goto found;
  436. not_ipv4:
  437. cur = *str;
  438. }
  439. /*
  440. * then this should be a hostname which can be empty
  441. */
  442. while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur)) {
  443. NEXT(cur);
  444. }
  445. found:
  446. if (uri != NULL) {
  447. g_free(uri->authority);
  448. uri->authority = NULL;
  449. g_free(uri->server);
  450. if (cur != host) {
  451. if (uri->cleanup & 2) {
  452. uri->server = g_strndup(host, cur - host);
  453. } else {
  454. uri->server = uri_string_unescape(host, cur - host, NULL);
  455. }
  456. } else {
  457. uri->server = NULL;
  458. }
  459. }
  460. *str = cur;
  461. return 0;
  462. }
  463. /**
  464. * rfc3986_parse_authority:
  465. * @uri: pointer to an URI structure
  466. * @str: the string to analyze
  467. *
  468. * Parse an authority part and fills in the appropriate fields
  469. * of the @uri structure
  470. *
  471. * authority = [ userinfo "@" ] host [ ":" port ]
  472. *
  473. * Returns 0 or the error code
  474. */
  475. static int rfc3986_parse_authority(URI *uri, const char **str)
  476. {
  477. const char *cur;
  478. int ret;
  479. cur = *str;
  480. /*
  481. * try to parse a userinfo and check for the trailing @
  482. */
  483. ret = rfc3986_parse_user_info(uri, &cur);
  484. if ((ret != 0) || (*cur != '@')) {
  485. cur = *str;
  486. } else {
  487. cur++;
  488. }
  489. ret = rfc3986_parse_host(uri, &cur);
  490. if (ret != 0) {
  491. return ret;
  492. }
  493. if (*cur == ':') {
  494. cur++;
  495. ret = rfc3986_parse_port(uri, &cur);
  496. if (ret != 0) {
  497. return ret;
  498. }
  499. }
  500. *str = cur;
  501. return 0;
  502. }
  503. /**
  504. * rfc3986_parse_segment:
  505. * @str: the string to analyze
  506. * @forbid: an optional forbidden character
  507. * @empty: allow an empty segment
  508. *
  509. * Parse a segment and fills in the appropriate fields
  510. * of the @uri structure
  511. *
  512. * segment = *pchar
  513. * segment-nz = 1*pchar
  514. * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
  515. * ; non-zero-length segment without any colon ":"
  516. *
  517. * Returns 0 or the error code
  518. */
  519. static int rfc3986_parse_segment(const char **str, char forbid, int empty)
  520. {
  521. const char *cur;
  522. cur = *str;
  523. if (!ISA_PCHAR(cur)) {
  524. if (empty) {
  525. return 0;
  526. }
  527. return 1;
  528. }
  529. while (ISA_PCHAR(cur) && (*cur != forbid)) {
  530. NEXT(cur);
  531. }
  532. *str = cur;
  533. return 0;
  534. }
  535. /**
  536. * rfc3986_parse_path_ab_empty:
  537. * @uri: pointer to an URI structure
  538. * @str: the string to analyze
  539. *
  540. * Parse an path absolute or empty and fills in the appropriate fields
  541. * of the @uri structure
  542. *
  543. * path-abempty = *( "/" segment )
  544. *
  545. * Returns 0 or the error code
  546. */
  547. static int rfc3986_parse_path_ab_empty(URI *uri, const char **str)
  548. {
  549. const char *cur;
  550. int ret;
  551. cur = *str;
  552. while (*cur == '/') {
  553. cur++;
  554. ret = rfc3986_parse_segment(&cur, 0, 1);
  555. if (ret != 0) {
  556. return ret;
  557. }
  558. }
  559. if (uri != NULL) {
  560. g_free(uri->path);
  561. if (*str != cur) {
  562. if (uri->cleanup & 2) {
  563. uri->path = g_strndup(*str, cur - *str);
  564. } else {
  565. uri->path = uri_string_unescape(*str, cur - *str, NULL);
  566. }
  567. } else {
  568. uri->path = NULL;
  569. }
  570. }
  571. *str = cur;
  572. return 0;
  573. }
  574. /**
  575. * rfc3986_parse_path_absolute:
  576. * @uri: pointer to an URI structure
  577. * @str: the string to analyze
  578. *
  579. * Parse an path absolute and fills in the appropriate fields
  580. * of the @uri structure
  581. *
  582. * path-absolute = "/" [ segment-nz *( "/" segment ) ]
  583. *
  584. * Returns 0 or the error code
  585. */
  586. static int rfc3986_parse_path_absolute(URI *uri, const char **str)
  587. {
  588. const char *cur;
  589. int ret;
  590. cur = *str;
  591. if (*cur != '/') {
  592. return 1;
  593. }
  594. cur++;
  595. ret = rfc3986_parse_segment(&cur, 0, 0);
  596. if (ret == 0) {
  597. while (*cur == '/') {
  598. cur++;
  599. ret = rfc3986_parse_segment(&cur, 0, 1);
  600. if (ret != 0) {
  601. return ret;
  602. }
  603. }
  604. }
  605. if (uri != NULL) {
  606. g_free(uri->path);
  607. if (cur != *str) {
  608. if (uri->cleanup & 2) {
  609. uri->path = g_strndup(*str, cur - *str);
  610. } else {
  611. uri->path = uri_string_unescape(*str, cur - *str, NULL);
  612. }
  613. } else {
  614. uri->path = NULL;
  615. }
  616. }
  617. *str = cur;
  618. return 0;
  619. }
  620. /**
  621. * rfc3986_parse_path_rootless:
  622. * @uri: pointer to an URI structure
  623. * @str: the string to analyze
  624. *
  625. * Parse an path without root and fills in the appropriate fields
  626. * of the @uri structure
  627. *
  628. * path-rootless = segment-nz *( "/" segment )
  629. *
  630. * Returns 0 or the error code
  631. */
  632. static int rfc3986_parse_path_rootless(URI *uri, const char **str)
  633. {
  634. const char *cur;
  635. int ret;
  636. cur = *str;
  637. ret = rfc3986_parse_segment(&cur, 0, 0);
  638. if (ret != 0) {
  639. return ret;
  640. }
  641. while (*cur == '/') {
  642. cur++;
  643. ret = rfc3986_parse_segment(&cur, 0, 1);
  644. if (ret != 0) {
  645. return ret;
  646. }
  647. }
  648. if (uri != NULL) {
  649. g_free(uri->path);
  650. if (cur != *str) {
  651. if (uri->cleanup & 2) {
  652. uri->path = g_strndup(*str, cur - *str);
  653. } else {
  654. uri->path = uri_string_unescape(*str, cur - *str, NULL);
  655. }
  656. } else {
  657. uri->path = NULL;
  658. }
  659. }
  660. *str = cur;
  661. return 0;
  662. }
  663. /**
  664. * rfc3986_parse_path_no_scheme:
  665. * @uri: pointer to an URI structure
  666. * @str: the string to analyze
  667. *
  668. * Parse an path which is not a scheme and fills in the appropriate fields
  669. * of the @uri structure
  670. *
  671. * path-noscheme = segment-nz-nc *( "/" segment )
  672. *
  673. * Returns 0 or the error code
  674. */
  675. static int rfc3986_parse_path_no_scheme(URI *uri, const char **str)
  676. {
  677. const char *cur;
  678. int ret;
  679. cur = *str;
  680. ret = rfc3986_parse_segment(&cur, ':', 0);
  681. if (ret != 0) {
  682. return ret;
  683. }
  684. while (*cur == '/') {
  685. cur++;
  686. ret = rfc3986_parse_segment(&cur, 0, 1);
  687. if (ret != 0) {
  688. return ret;
  689. }
  690. }
  691. if (uri != NULL) {
  692. g_free(uri->path);
  693. if (cur != *str) {
  694. if (uri->cleanup & 2) {
  695. uri->path = g_strndup(*str, cur - *str);
  696. } else {
  697. uri->path = uri_string_unescape(*str, cur - *str, NULL);
  698. }
  699. } else {
  700. uri->path = NULL;
  701. }
  702. }
  703. *str = cur;
  704. return 0;
  705. }
  706. /**
  707. * rfc3986_parse_hier_part:
  708. * @uri: pointer to an URI structure
  709. * @str: the string to analyze
  710. *
  711. * Parse an hierarchical part and fills in the appropriate fields
  712. * of the @uri structure
  713. *
  714. * hier-part = "//" authority path-abempty
  715. * / path-absolute
  716. * / path-rootless
  717. * / path-empty
  718. *
  719. * Returns 0 or the error code
  720. */
  721. static int rfc3986_parse_hier_part(URI *uri, const char **str)
  722. {
  723. const char *cur;
  724. int ret;
  725. cur = *str;
  726. if ((*cur == '/') && (*(cur + 1) == '/')) {
  727. cur += 2;
  728. ret = rfc3986_parse_authority(uri, &cur);
  729. if (ret != 0) {
  730. return ret;
  731. }
  732. ret = rfc3986_parse_path_ab_empty(uri, &cur);
  733. if (ret != 0) {
  734. return ret;
  735. }
  736. *str = cur;
  737. return 0;
  738. } else if (*cur == '/') {
  739. ret = rfc3986_parse_path_absolute(uri, &cur);
  740. if (ret != 0) {
  741. return ret;
  742. }
  743. } else if (ISA_PCHAR(cur)) {
  744. ret = rfc3986_parse_path_rootless(uri, &cur);
  745. if (ret != 0) {
  746. return ret;
  747. }
  748. } else {
  749. /* path-empty is effectively empty */
  750. if (uri != NULL) {
  751. g_free(uri->path);
  752. uri->path = NULL;
  753. }
  754. }
  755. *str = cur;
  756. return 0;
  757. }
  758. /**
  759. * rfc3986_parse_relative_ref:
  760. * @uri: pointer to an URI structure
  761. * @str: the string to analyze
  762. *
  763. * Parse an URI string and fills in the appropriate fields
  764. * of the @uri structure
  765. *
  766. * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
  767. * relative-part = "//" authority path-abempty
  768. * / path-absolute
  769. * / path-noscheme
  770. * / path-empty
  771. *
  772. * Returns 0 or the error code
  773. */
  774. static int rfc3986_parse_relative_ref(URI *uri, const char *str)
  775. {
  776. int ret;
  777. if ((*str == '/') && (*(str + 1) == '/')) {
  778. str += 2;
  779. ret = rfc3986_parse_authority(uri, &str);
  780. if (ret != 0) {
  781. return ret;
  782. }
  783. ret = rfc3986_parse_path_ab_empty(uri, &str);
  784. if (ret != 0) {
  785. return ret;
  786. }
  787. } else if (*str == '/') {
  788. ret = rfc3986_parse_path_absolute(uri, &str);
  789. if (ret != 0) {
  790. return ret;
  791. }
  792. } else if (ISA_PCHAR(str)) {
  793. ret = rfc3986_parse_path_no_scheme(uri, &str);
  794. if (ret != 0) {
  795. return ret;
  796. }
  797. } else {
  798. /* path-empty is effectively empty */
  799. if (uri != NULL) {
  800. g_free(uri->path);
  801. uri->path = NULL;
  802. }
  803. }
  804. if (*str == '?') {
  805. str++;
  806. ret = rfc3986_parse_query(uri, &str);
  807. if (ret != 0) {
  808. return ret;
  809. }
  810. }
  811. if (*str == '#') {
  812. str++;
  813. ret = rfc3986_parse_fragment(uri, &str);
  814. if (ret != 0) {
  815. return ret;
  816. }
  817. }
  818. if (*str != 0) {
  819. uri_clean(uri);
  820. return 1;
  821. }
  822. return 0;
  823. }
  824. /**
  825. * rfc3986_parse:
  826. * @uri: pointer to an URI structure
  827. * @str: the string to analyze
  828. *
  829. * Parse an URI string and fills in the appropriate fields
  830. * of the @uri structure
  831. *
  832. * scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  833. *
  834. * Returns 0 or the error code
  835. */
  836. static int rfc3986_parse(URI *uri, const char *str)
  837. {
  838. int ret;
  839. ret = rfc3986_parse_scheme(uri, &str);
  840. if (ret != 0) {
  841. return ret;
  842. }
  843. if (*str != ':') {
  844. return 1;
  845. }
  846. str++;
  847. ret = rfc3986_parse_hier_part(uri, &str);
  848. if (ret != 0) {
  849. return ret;
  850. }
  851. if (*str == '?') {
  852. str++;
  853. ret = rfc3986_parse_query(uri, &str);
  854. if (ret != 0) {
  855. return ret;
  856. }
  857. }
  858. if (*str == '#') {
  859. str++;
  860. ret = rfc3986_parse_fragment(uri, &str);
  861. if (ret != 0) {
  862. return ret;
  863. }
  864. }
  865. if (*str != 0) {
  866. uri_clean(uri);
  867. return 1;
  868. }
  869. return 0;
  870. }
  871. /**
  872. * rfc3986_parse_uri_reference:
  873. * @uri: pointer to an URI structure
  874. * @str: the string to analyze
  875. *
  876. * Parse an URI reference string and fills in the appropriate fields
  877. * of the @uri structure
  878. *
  879. * URI-reference = URI / relative-ref
  880. *
  881. * Returns 0 or the error code
  882. */
  883. static int rfc3986_parse_uri_reference(URI *uri, const char *str)
  884. {
  885. int ret;
  886. if (str == NULL) {
  887. return -1;
  888. }
  889. uri_clean(uri);
  890. /*
  891. * Try first to parse absolute refs, then fallback to relative if
  892. * it fails.
  893. */
  894. ret = rfc3986_parse(uri, str);
  895. if (ret != 0) {
  896. uri_clean(uri);
  897. ret = rfc3986_parse_relative_ref(uri, str);
  898. if (ret != 0) {
  899. uri_clean(uri);
  900. return ret;
  901. }
  902. }
  903. return 0;
  904. }
  905. /**
  906. * uri_parse:
  907. * @str: the URI string to analyze
  908. *
  909. * Parse an URI based on RFC 3986
  910. *
  911. * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
  912. *
  913. * Returns a newly built URI or NULL in case of error
  914. */
  915. URI *uri_parse(const char *str)
  916. {
  917. URI *uri;
  918. int ret;
  919. if (str == NULL) {
  920. return NULL;
  921. }
  922. uri = uri_new();
  923. ret = rfc3986_parse_uri_reference(uri, str);
  924. if (ret) {
  925. uri_free(uri);
  926. return NULL;
  927. }
  928. return uri;
  929. }
  930. /**
  931. * uri_parse_into:
  932. * @uri: pointer to an URI structure
  933. * @str: the string to analyze
  934. *
  935. * Parse an URI reference string based on RFC 3986 and fills in the
  936. * appropriate fields of the @uri structure
  937. *
  938. * URI-reference = URI / relative-ref
  939. *
  940. * Returns 0 or the error code
  941. */
  942. int uri_parse_into(URI *uri, const char *str)
  943. {
  944. return rfc3986_parse_uri_reference(uri, str);
  945. }
  946. /**
  947. * uri_parse_raw:
  948. * @str: the URI string to analyze
  949. * @raw: if 1 unescaping of URI pieces are disabled
  950. *
  951. * Parse an URI but allows to keep intact the original fragments.
  952. *
  953. * URI-reference = URI / relative-ref
  954. *
  955. * Returns a newly built URI or NULL in case of error
  956. */
  957. URI *uri_parse_raw(const char *str, int raw)
  958. {
  959. URI *uri;
  960. int ret;
  961. if (str == NULL) {
  962. return NULL;
  963. }
  964. uri = uri_new();
  965. if (raw) {
  966. uri->cleanup |= 2;
  967. }
  968. ret = uri_parse_into(uri, str);
  969. if (ret) {
  970. uri_free(uri);
  971. return NULL;
  972. }
  973. return uri;
  974. }
  975. /************************************************************************
  976. * *
  977. * Generic URI structure functions *
  978. * *
  979. ************************************************************************/
  980. /**
  981. * uri_new:
  982. *
  983. * Simply creates an empty URI
  984. *
  985. * Returns the new structure or NULL in case of error
  986. */
  987. URI *uri_new(void)
  988. {
  989. return g_new0(URI, 1);
  990. }
  991. /**
  992. * realloc2n:
  993. *
  994. * Function to handle properly a reallocation when saving an URI
  995. * Also imposes some limit on the length of an URI string output
  996. */
  997. static char *realloc2n(char *ret, int *max)
  998. {
  999. char *temp;
  1000. int tmp;
  1001. tmp = *max * 2;
  1002. temp = g_realloc(ret, (tmp + 1));
  1003. *max = tmp;
  1004. return temp;
  1005. }
  1006. /**
  1007. * uri_to_string:
  1008. * @uri: pointer to an URI
  1009. *
  1010. * Save the URI as an escaped string
  1011. *
  1012. * Returns a new string (to be deallocated by caller)
  1013. */
  1014. char *uri_to_string(URI *uri)
  1015. {
  1016. char *ret = NULL;
  1017. char *temp;
  1018. const char *p;
  1019. int len;
  1020. int max;
  1021. if (uri == NULL) {
  1022. return NULL;
  1023. }
  1024. max = 80;
  1025. ret = g_malloc(max + 1);
  1026. len = 0;
  1027. if (uri->scheme != NULL) {
  1028. p = uri->scheme;
  1029. while (*p != 0) {
  1030. if (len >= max) {
  1031. temp = realloc2n(ret, &max);
  1032. ret = temp;
  1033. }
  1034. ret[len++] = *p++;
  1035. }
  1036. if (len >= max) {
  1037. temp = realloc2n(ret, &max);
  1038. ret = temp;
  1039. }
  1040. ret[len++] = ':';
  1041. }
  1042. if (uri->opaque != NULL) {
  1043. p = uri->opaque;
  1044. while (*p != 0) {
  1045. if (len + 3 >= max) {
  1046. temp = realloc2n(ret, &max);
  1047. ret = temp;
  1048. }
  1049. if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p))) {
  1050. ret[len++] = *p++;
  1051. } else {
  1052. int val = *(unsigned char *)p++;
  1053. int hi = val / 0x10, lo = val % 0x10;
  1054. ret[len++] = '%';
  1055. ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
  1056. ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
  1057. }
  1058. }
  1059. } else {
  1060. if (uri->server != NULL) {
  1061. if (len + 3 >= max) {
  1062. temp = realloc2n(ret, &max);
  1063. ret = temp;
  1064. }
  1065. ret[len++] = '/';
  1066. ret[len++] = '/';
  1067. if (uri->user != NULL) {
  1068. p = uri->user;
  1069. while (*p != 0) {
  1070. if (len + 3 >= max) {
  1071. temp = realloc2n(ret, &max);
  1072. ret = temp;
  1073. }
  1074. if ((IS_UNRESERVED(*(p))) || ((*(p) == ';')) ||
  1075. ((*(p) == ':')) || ((*(p) == '&')) || ((*(p) == '=')) ||
  1076. ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ','))) {
  1077. ret[len++] = *p++;
  1078. } else {
  1079. int val = *(unsigned char *)p++;
  1080. int hi = val / 0x10, lo = val % 0x10;
  1081. ret[len++] = '%';
  1082. ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
  1083. ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
  1084. }
  1085. }
  1086. if (len + 3 >= max) {
  1087. temp = realloc2n(ret, &max);
  1088. ret = temp;
  1089. }
  1090. ret[len++] = '@';
  1091. }
  1092. p = uri->server;
  1093. while (*p != 0) {
  1094. if (len >= max) {
  1095. temp = realloc2n(ret, &max);
  1096. ret = temp;
  1097. }
  1098. ret[len++] = *p++;
  1099. }
  1100. if (uri->port > 0) {
  1101. if (len + 10 >= max) {
  1102. temp = realloc2n(ret, &max);
  1103. ret = temp;
  1104. }
  1105. len += snprintf(&ret[len], max - len, ":%d", uri->port);
  1106. }
  1107. } else if (uri->authority != NULL) {
  1108. if (len + 3 >= max) {
  1109. temp = realloc2n(ret, &max);
  1110. ret = temp;
  1111. }
  1112. ret[len++] = '/';
  1113. ret[len++] = '/';
  1114. p = uri->authority;
  1115. while (*p != 0) {
  1116. if (len + 3 >= max) {
  1117. temp = realloc2n(ret, &max);
  1118. ret = temp;
  1119. }
  1120. if ((IS_UNRESERVED(*(p))) || ((*(p) == '$')) ||
  1121. ((*(p) == ',')) || ((*(p) == ';')) || ((*(p) == ':')) ||
  1122. ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) ||
  1123. ((*(p) == '+'))) {
  1124. ret[len++] = *p++;
  1125. } else {
  1126. int val = *(unsigned char *)p++;
  1127. int hi = val / 0x10, lo = val % 0x10;
  1128. ret[len++] = '%';
  1129. ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
  1130. ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
  1131. }
  1132. }
  1133. } else if (uri->scheme != NULL) {
  1134. if (len + 3 >= max) {
  1135. temp = realloc2n(ret, &max);
  1136. ret = temp;
  1137. }
  1138. ret[len++] = '/';
  1139. ret[len++] = '/';
  1140. }
  1141. if (uri->path != NULL) {
  1142. p = uri->path;
  1143. /*
  1144. * the colon in file:///d: should not be escaped or
  1145. * Windows accesses fail later.
  1146. */
  1147. if ((uri->scheme != NULL) && (p[0] == '/') &&
  1148. (((p[1] >= 'a') && (p[1] <= 'z')) ||
  1149. ((p[1] >= 'A') && (p[1] <= 'Z'))) &&
  1150. (p[2] == ':') && (!strcmp(uri->scheme, "file"))) {
  1151. if (len + 3 >= max) {
  1152. temp = realloc2n(ret, &max);
  1153. ret = temp;
  1154. }
  1155. ret[len++] = *p++;
  1156. ret[len++] = *p++;
  1157. ret[len++] = *p++;
  1158. }
  1159. while (*p != 0) {
  1160. if (len + 3 >= max) {
  1161. temp = realloc2n(ret, &max);
  1162. ret = temp;
  1163. }
  1164. if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) ||
  1165. ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) ||
  1166. ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||
  1167. ((*(p) == ','))) {
  1168. ret[len++] = *p++;
  1169. } else {
  1170. int val = *(unsigned char *)p++;
  1171. int hi = val / 0x10, lo = val % 0x10;
  1172. ret[len++] = '%';
  1173. ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
  1174. ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
  1175. }
  1176. }
  1177. }
  1178. if (uri->query != NULL) {
  1179. if (len + 1 >= max) {
  1180. temp = realloc2n(ret, &max);
  1181. ret = temp;
  1182. }
  1183. ret[len++] = '?';
  1184. p = uri->query;
  1185. while (*p != 0) {
  1186. if (len + 1 >= max) {
  1187. temp = realloc2n(ret, &max);
  1188. ret = temp;
  1189. }
  1190. ret[len++] = *p++;
  1191. }
  1192. }
  1193. }
  1194. if (uri->fragment != NULL) {
  1195. if (len + 3 >= max) {
  1196. temp = realloc2n(ret, &max);
  1197. ret = temp;
  1198. }
  1199. ret[len++] = '#';
  1200. p = uri->fragment;
  1201. while (*p != 0) {
  1202. if (len + 3 >= max) {
  1203. temp = realloc2n(ret, &max);
  1204. ret = temp;
  1205. }
  1206. if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) {
  1207. ret[len++] = *p++;
  1208. } else {
  1209. int val = *(unsigned char *)p++;
  1210. int hi = val / 0x10, lo = val % 0x10;
  1211. ret[len++] = '%';
  1212. ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
  1213. ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
  1214. }
  1215. }
  1216. }
  1217. if (len >= max) {
  1218. temp = realloc2n(ret, &max);
  1219. ret = temp;
  1220. }
  1221. ret[len] = 0;
  1222. return ret;
  1223. }
  1224. /**
  1225. * uri_clean:
  1226. * @uri: pointer to an URI
  1227. *
  1228. * Make sure the URI struct is free of content
  1229. */
  1230. static void uri_clean(URI *uri)
  1231. {
  1232. if (uri == NULL) {
  1233. return;
  1234. }
  1235. g_free(uri->scheme);
  1236. uri->scheme = NULL;
  1237. g_free(uri->server);
  1238. uri->server = NULL;
  1239. g_free(uri->user);
  1240. uri->user = NULL;
  1241. g_free(uri->path);
  1242. uri->path = NULL;
  1243. g_free(uri->fragment);
  1244. uri->fragment = NULL;
  1245. g_free(uri->opaque);
  1246. uri->opaque = NULL;
  1247. g_free(uri->authority);
  1248. uri->authority = NULL;
  1249. g_free(uri->query);
  1250. uri->query = NULL;
  1251. }
  1252. /**
  1253. * uri_free:
  1254. * @uri: pointer to an URI
  1255. *
  1256. * Free up the URI struct
  1257. */
  1258. void uri_free(URI *uri)
  1259. {
  1260. uri_clean(uri);
  1261. g_free(uri);
  1262. }
  1263. /************************************************************************
  1264. * *
  1265. * Helper functions *
  1266. * *
  1267. ************************************************************************/
  1268. /**
  1269. * normalize_uri_path:
  1270. * @path: pointer to the path string
  1271. *
  1272. * Applies the 5 normalization steps to a path string--that is, RFC 2396
  1273. * Section 5.2, steps 6.c through 6.g.
  1274. *
  1275. * Normalization occurs directly on the string, no new allocation is done
  1276. *
  1277. * Returns 0 or an error code
  1278. */
  1279. static int normalize_uri_path(char *path)
  1280. {
  1281. char *cur, *out;
  1282. if (path == NULL) {
  1283. return -1;
  1284. }
  1285. /* Skip all initial "/" chars. We want to get to the beginning of the
  1286. * first non-empty segment.
  1287. */
  1288. cur = path;
  1289. while (cur[0] == '/') {
  1290. ++cur;
  1291. }
  1292. if (cur[0] == '\0') {
  1293. return 0;
  1294. }
  1295. /* Keep everything we've seen so far. */
  1296. out = cur;
  1297. /*
  1298. * Analyze each segment in sequence for cases (c) and (d).
  1299. */
  1300. while (cur[0] != '\0') {
  1301. /*
  1302. * c) All occurrences of "./", where "." is a complete path segment,
  1303. * are removed from the buffer string.
  1304. */
  1305. if ((cur[0] == '.') && (cur[1] == '/')) {
  1306. cur += 2;
  1307. /* '//' normalization should be done at this point too */
  1308. while (cur[0] == '/') {
  1309. cur++;
  1310. }
  1311. continue;
  1312. }
  1313. /*
  1314. * d) If the buffer string ends with "." as a complete path segment,
  1315. * that "." is removed.
  1316. */
  1317. if ((cur[0] == '.') && (cur[1] == '\0')) {
  1318. break;
  1319. }
  1320. /* Otherwise keep the segment. */
  1321. while (cur[0] != '/') {
  1322. if (cur[0] == '\0') {
  1323. goto done_cd;
  1324. }
  1325. (out++)[0] = (cur++)[0];
  1326. }
  1327. /* nomalize // */
  1328. while ((cur[0] == '/') && (cur[1] == '/')) {
  1329. cur++;
  1330. }
  1331. (out++)[0] = (cur++)[0];
  1332. }
  1333. done_cd:
  1334. out[0] = '\0';
  1335. /* Reset to the beginning of the first segment for the next sequence. */
  1336. cur = path;
  1337. while (cur[0] == '/') {
  1338. ++cur;
  1339. }
  1340. if (cur[0] == '\0') {
  1341. return 0;
  1342. }
  1343. /*
  1344. * Analyze each segment in sequence for cases (e) and (f).
  1345. *
  1346. * e) All occurrences of "<segment>/../", where <segment> is a
  1347. * complete path segment not equal to "..", are removed from the
  1348. * buffer string. Removal of these path segments is performed
  1349. * iteratively, removing the leftmost matching pattern on each
  1350. * iteration, until no matching pattern remains.
  1351. *
  1352. * f) If the buffer string ends with "<segment>/..", where <segment>
  1353. * is a complete path segment not equal to "..", that
  1354. * "<segment>/.." is removed.
  1355. *
  1356. * To satisfy the "iterative" clause in (e), we need to collapse the
  1357. * string every time we find something that needs to be removed. Thus,
  1358. * we don't need to keep two pointers into the string: we only need a
  1359. * "current position" pointer.
  1360. */
  1361. while (1) {
  1362. char *segp, *tmp;
  1363. /* At the beginning of each iteration of this loop, "cur" points to
  1364. * the first character of the segment we want to examine.
  1365. */
  1366. /* Find the end of the current segment. */
  1367. segp = cur;
  1368. while ((segp[0] != '/') && (segp[0] != '\0')) {
  1369. ++segp;
  1370. }
  1371. /* If this is the last segment, we're done (we need at least two
  1372. * segments to meet the criteria for the (e) and (f) cases).
  1373. */
  1374. if (segp[0] == '\0') {
  1375. break;
  1376. }
  1377. /* If the first segment is "..", or if the next segment _isn't_ "..",
  1378. * keep this segment and try the next one.
  1379. */
  1380. ++segp;
  1381. if (((cur[0] == '.') && (cur[1] == '.') && (segp == cur + 3)) ||
  1382. ((segp[0] != '.') || (segp[1] != '.') ||
  1383. ((segp[2] != '/') && (segp[2] != '\0')))) {
  1384. cur = segp;
  1385. continue;
  1386. }
  1387. /* If we get here, remove this segment and the next one and back up
  1388. * to the previous segment (if there is one), to implement the
  1389. * "iteratively" clause. It's pretty much impossible to back up
  1390. * while maintaining two pointers into the buffer, so just compact
  1391. * the whole buffer now.
  1392. */
  1393. /* If this is the end of the buffer, we're done. */
  1394. if (segp[2] == '\0') {
  1395. cur[0] = '\0';
  1396. break;
  1397. }
  1398. /* Valgrind complained, strcpy(cur, segp + 3); */
  1399. /* string will overlap, do not use strcpy */
  1400. tmp = cur;
  1401. segp += 3;
  1402. while ((*tmp++ = *segp++) != 0) {
  1403. /* No further work */
  1404. }
  1405. /* If there are no previous segments, then keep going from here. */
  1406. segp = cur;
  1407. while ((segp > path) && ((--segp)[0] == '/')) {
  1408. /* No further work */
  1409. }
  1410. if (segp == path) {
  1411. continue;
  1412. }
  1413. /* "segp" is pointing to the end of a previous segment; find it's
  1414. * start. We need to back up to the previous segment and start
  1415. * over with that to handle things like "foo/bar/../..". If we
  1416. * don't do this, then on the first pass we'll remove the "bar/..",
  1417. * but be pointing at the second ".." so we won't realize we can also
  1418. * remove the "foo/..".
  1419. */
  1420. cur = segp;
  1421. while ((cur > path) && (cur[-1] != '/')) {
  1422. --cur;
  1423. }
  1424. }
  1425. out[0] = '\0';
  1426. /*
  1427. * g) If the resulting buffer string still begins with one or more
  1428. * complete path segments of "..", then the reference is
  1429. * considered to be in error. Implementations may handle this
  1430. * error by retaining these components in the resolved path (i.e.,
  1431. * treating them as part of the final URI), by removing them from
  1432. * the resolved path (i.e., discarding relative levels above the
  1433. * root), or by avoiding traversal of the reference.
  1434. *
  1435. * We discard them from the final path.
  1436. */
  1437. if (path[0] == '/') {
  1438. cur = path;
  1439. while ((cur[0] == '/') && (cur[1] == '.') && (cur[2] == '.') &&
  1440. ((cur[3] == '/') || (cur[3] == '\0'))) {
  1441. cur += 3;
  1442. }
  1443. if (cur != path) {
  1444. out = path;
  1445. while (cur[0] != '\0') {
  1446. (out++)[0] = (cur++)[0];
  1447. }
  1448. out[0] = 0;
  1449. }
  1450. }
  1451. return 0;
  1452. }
  1453. static int is_hex(char c)
  1454. {
  1455. if (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) ||
  1456. ((c >= 'A') && (c <= 'F'))) {
  1457. return 1;
  1458. }
  1459. return 0;
  1460. }
  1461. /**
  1462. * uri_string_unescape:
  1463. * @str: the string to unescape
  1464. * @len: the length in bytes to unescape (or <= 0 to indicate full string)
  1465. * @target: optional destination buffer
  1466. *
  1467. * Unescaping routine, but does not check that the string is an URI. The
  1468. * output is a direct unsigned char translation of %XX values (no encoding)
  1469. * Note that the length of the result can only be smaller or same size as
  1470. * the input string.
  1471. *
  1472. * Returns a copy of the string, but unescaped, will return NULL only in case
  1473. * of error
  1474. */
  1475. char *uri_string_unescape(const char *str, int len, char *target)
  1476. {
  1477. char *ret, *out;
  1478. const char *in;
  1479. if (str == NULL) {
  1480. return NULL;
  1481. }
  1482. if (len <= 0) {
  1483. len = strlen(str);
  1484. }
  1485. if (len < 0) {
  1486. return NULL;
  1487. }
  1488. if (target == NULL) {
  1489. ret = g_malloc(len + 1);
  1490. } else {
  1491. ret = target;
  1492. }
  1493. in = str;
  1494. out = ret;
  1495. while (len > 0) {
  1496. if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) {
  1497. in++;
  1498. if ((*in >= '0') && (*in <= '9')) {
  1499. *out = (*in - '0');
  1500. } else if ((*in >= 'a') && (*in <= 'f')) {
  1501. *out = (*in - 'a') + 10;
  1502. } else if ((*in >= 'A') && (*in <= 'F')) {
  1503. *out = (*in - 'A') + 10;
  1504. }
  1505. in++;
  1506. if ((*in >= '0') && (*in <= '9')) {
  1507. *out = *out * 16 + (*in - '0');
  1508. } else if ((*in >= 'a') && (*in <= 'f')) {
  1509. *out = *out * 16 + (*in - 'a') + 10;
  1510. } else if ((*in >= 'A') && (*in <= 'F')) {
  1511. *out = *out * 16 + (*in - 'A') + 10;
  1512. }
  1513. in++;
  1514. len -= 3;
  1515. out++;
  1516. } else {
  1517. *out++ = *in++;
  1518. len--;
  1519. }
  1520. }
  1521. *out = 0;
  1522. return ret;
  1523. }
  1524. /**
  1525. * uri_string_escape:
  1526. * @str: string to escape
  1527. * @list: exception list string of chars not to escape
  1528. *
  1529. * This routine escapes a string to hex, ignoring reserved characters (a-z)
  1530. * and the characters in the exception list.
  1531. *
  1532. * Returns a new escaped string or NULL in case of error.
  1533. */
  1534. char *uri_string_escape(const char *str, const char *list)
  1535. {
  1536. char *ret, ch;
  1537. char *temp;
  1538. const char *in;
  1539. int len, out;
  1540. if (str == NULL) {
  1541. return NULL;
  1542. }
  1543. if (str[0] == 0) {
  1544. return g_strdup(str);
  1545. }
  1546. len = strlen(str);
  1547. if (!(len > 0)) {
  1548. return NULL;
  1549. }
  1550. len += 20;
  1551. ret = g_malloc(len);
  1552. in = str;
  1553. out = 0;
  1554. while (*in != 0) {
  1555. if (len - out <= 3) {
  1556. temp = realloc2n(ret, &len);
  1557. ret = temp;
  1558. }
  1559. ch = *in;
  1560. if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!strchr(list, ch))) {
  1561. unsigned char val;
  1562. ret[out++] = '%';
  1563. val = ch >> 4;
  1564. if (val <= 9) {
  1565. ret[out++] = '0' + val;
  1566. } else {
  1567. ret[out++] = 'A' + val - 0xA;
  1568. }
  1569. val = ch & 0xF;
  1570. if (val <= 9) {
  1571. ret[out++] = '0' + val;
  1572. } else {
  1573. ret[out++] = 'A' + val - 0xA;
  1574. }
  1575. in++;
  1576. } else {
  1577. ret[out++] = *in++;
  1578. }
  1579. }
  1580. ret[out] = 0;
  1581. return ret;
  1582. }
  1583. /************************************************************************
  1584. * *
  1585. * Public functions *
  1586. * *
  1587. ************************************************************************/
  1588. /**
  1589. * uri_resolve:
  1590. * @URI: the URI instance found in the document
  1591. * @base: the base value
  1592. *
  1593. * Computes he final URI of the reference done by checking that
  1594. * the given URI is valid, and building the final URI using the
  1595. * base URI. This is processed according to section 5.2 of the
  1596. * RFC 2396
  1597. *
  1598. * 5.2. Resolving Relative References to Absolute Form
  1599. *
  1600. * Returns a new URI string (to be freed by the caller) or NULL in case
  1601. * of error.
  1602. */
  1603. char *uri_resolve(const char *uri, const char *base)
  1604. {
  1605. char *val = NULL;
  1606. int ret, len, indx, cur, out;
  1607. URI *ref = NULL;
  1608. URI *bas = NULL;
  1609. URI *res = NULL;
  1610. /*
  1611. * 1) The URI reference is parsed into the potential four components and
  1612. * fragment identifier, as described in Section 4.3.
  1613. *
  1614. * NOTE that a completely empty URI is treated by modern browsers
  1615. * as a reference to "." rather than as a synonym for the current
  1616. * URI. Should we do that here?
  1617. */
  1618. if (uri == NULL) {
  1619. ret = -1;
  1620. } else {
  1621. if (*uri) {
  1622. ref = uri_new();
  1623. ret = uri_parse_into(ref, uri);
  1624. } else {
  1625. ret = 0;
  1626. }
  1627. }
  1628. if (ret != 0) {
  1629. goto done;
  1630. }
  1631. if ((ref != NULL) && (ref->scheme != NULL)) {
  1632. /*
  1633. * The URI is absolute don't modify.
  1634. */
  1635. val = g_strdup(uri);
  1636. goto done;
  1637. }
  1638. if (base == NULL) {
  1639. ret = -1;
  1640. } else {
  1641. bas = uri_new();
  1642. ret = uri_parse_into(bas, base);
  1643. }
  1644. if (ret != 0) {
  1645. if (ref) {
  1646. val = uri_to_string(ref);
  1647. }
  1648. goto done;
  1649. }
  1650. if (ref == NULL) {
  1651. /*
  1652. * the base fragment must be ignored
  1653. */
  1654. g_free(bas->fragment);
  1655. bas->fragment = NULL;
  1656. val = uri_to_string(bas);
  1657. goto done;
  1658. }
  1659. /*
  1660. * 2) If the path component is empty and the scheme, authority, and
  1661. * query components are undefined, then it is a reference to the
  1662. * current document and we are done. Otherwise, the reference URI's
  1663. * query and fragment components are defined as found (or not found)
  1664. * within the URI reference and not inherited from the base URI.
  1665. *
  1666. * NOTE that in modern browsers, the parsing differs from the above
  1667. * in the following aspect: the query component is allowed to be
  1668. * defined while still treating this as a reference to the current
  1669. * document.
  1670. */
  1671. res = uri_new();
  1672. if ((ref->scheme == NULL) && (ref->path == NULL) &&
  1673. ((ref->authority == NULL) && (ref->server == NULL))) {
  1674. res->scheme = g_strdup(bas->scheme);
  1675. if (bas->authority != NULL) {
  1676. res->authority = g_strdup(bas->authority);
  1677. } else if (bas->server != NULL) {
  1678. res->server = g_strdup(bas->server);
  1679. res->user = g_strdup(bas->user);
  1680. res->port = bas->port;
  1681. }
  1682. res->path = g_strdup(bas->path);
  1683. if (ref->query != NULL) {
  1684. res->query = g_strdup(ref->query);
  1685. } else {
  1686. res->query = g_strdup(bas->query);
  1687. }
  1688. res->fragment = g_strdup(ref->fragment);
  1689. goto step_7;
  1690. }
  1691. /*
  1692. * 3) If the scheme component is defined, indicating that the reference
  1693. * starts with a scheme name, then the reference is interpreted as an
  1694. * absolute URI and we are done. Otherwise, the reference URI's
  1695. * scheme is inherited from the base URI's scheme component.
  1696. */
  1697. if (ref->scheme != NULL) {
  1698. val = uri_to_string(ref);
  1699. goto done;
  1700. }
  1701. res->scheme = g_strdup(bas->scheme);
  1702. res->query = g_strdup(ref->query);
  1703. res->fragment = g_strdup(ref->fragment);
  1704. /*
  1705. * 4) If the authority component is defined, then the reference is a
  1706. * network-path and we skip to step 7. Otherwise, the reference
  1707. * URI's authority is inherited from the base URI's authority
  1708. * component, which will also be undefined if the URI scheme does not
  1709. * use an authority component.
  1710. */
  1711. if ((ref->authority != NULL) || (ref->server != NULL)) {
  1712. if (ref->authority != NULL) {
  1713. res->authority = g_strdup(ref->authority);
  1714. } else {
  1715. res->server = g_strdup(ref->server);
  1716. res->user = g_strdup(ref->user);
  1717. res->port = ref->port;
  1718. }
  1719. res->path = g_strdup(ref->path);
  1720. goto step_7;
  1721. }
  1722. if (bas->authority != NULL) {
  1723. res->authority = g_strdup(bas->authority);
  1724. } else if (bas->server != NULL) {
  1725. res->server = g_strdup(bas->server);
  1726. res->user = g_strdup(bas->user);
  1727. res->port = bas->port;
  1728. }
  1729. /*
  1730. * 5) If the path component begins with a slash character ("/"), then
  1731. * the reference is an absolute-path and we skip to step 7.
  1732. */
  1733. if ((ref->path != NULL) && (ref->path[0] == '/')) {
  1734. res->path = g_strdup(ref->path);
  1735. goto step_7;
  1736. }
  1737. /*
  1738. * 6) If this step is reached, then we are resolving a relative-path
  1739. * reference. The relative path needs to be merged with the base
  1740. * URI's path. Although there are many ways to do this, we will
  1741. * describe a simple method using a separate string buffer.
  1742. *
  1743. * Allocate a buffer large enough for the result string.
  1744. */
  1745. len = 2; /* extra / and 0 */
  1746. if (ref->path != NULL) {
  1747. len += strlen(ref->path);
  1748. }
  1749. if (bas->path != NULL) {
  1750. len += strlen(bas->path);
  1751. }
  1752. res->path = g_malloc(len);
  1753. res->path[0] = 0;
  1754. /*
  1755. * a) All but the last segment of the base URI's path component is
  1756. * copied to the buffer. In other words, any characters after the
  1757. * last (right-most) slash character, if any, are excluded.
  1758. */
  1759. cur = 0;
  1760. out = 0;
  1761. if (bas->path != NULL) {
  1762. while (bas->path[cur] != 0) {
  1763. while ((bas->path[cur] != 0) && (bas->path[cur] != '/')) {
  1764. cur++;
  1765. }
  1766. if (bas->path[cur] == 0) {
  1767. break;
  1768. }
  1769. cur++;
  1770. while (out < cur) {
  1771. res->path[out] = bas->path[out];
  1772. out++;
  1773. }
  1774. }
  1775. }
  1776. res->path[out] = 0;
  1777. /*
  1778. * b) The reference's path component is appended to the buffer
  1779. * string.
  1780. */
  1781. if (ref->path != NULL && ref->path[0] != 0) {
  1782. indx = 0;
  1783. /*
  1784. * Ensure the path includes a '/'
  1785. */
  1786. if ((out == 0) && (bas->server != NULL)) {
  1787. res->path[out++] = '/';
  1788. }
  1789. while (ref->path[indx] != 0) {
  1790. res->path[out++] = ref->path[indx++];
  1791. }
  1792. }
  1793. res->path[out] = 0;
  1794. /*
  1795. * Steps c) to h) are really path normalization steps
  1796. */
  1797. normalize_uri_path(res->path);
  1798. step_7:
  1799. /*
  1800. * 7) The resulting URI components, including any inherited from the
  1801. * base URI, are recombined to give the absolute form of the URI
  1802. * reference.
  1803. */
  1804. val = uri_to_string(res);
  1805. done:
  1806. if (ref != NULL) {
  1807. uri_free(ref);
  1808. }
  1809. if (bas != NULL) {
  1810. uri_free(bas);
  1811. }
  1812. if (res != NULL) {
  1813. uri_free(res);
  1814. }
  1815. return val;
  1816. }
  1817. /**
  1818. * uri_resolve_relative:
  1819. * @URI: the URI reference under consideration
  1820. * @base: the base value
  1821. *
  1822. * Expresses the URI of the reference in terms relative to the
  1823. * base. Some examples of this operation include:
  1824. * base = "http://site1.com/docs/book1.html"
  1825. * URI input URI returned
  1826. * docs/pic1.gif pic1.gif
  1827. * docs/img/pic1.gif img/pic1.gif
  1828. * img/pic1.gif ../img/pic1.gif
  1829. * http://site1.com/docs/pic1.gif pic1.gif
  1830. * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif
  1831. *
  1832. * base = "docs/book1.html"
  1833. * URI input URI returned
  1834. * docs/pic1.gif pic1.gif
  1835. * docs/img/pic1.gif img/pic1.gif
  1836. * img/pic1.gif ../img/pic1.gif
  1837. * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif
  1838. *
  1839. *
  1840. * Note: if the URI reference is really weird or complicated, it may be
  1841. * worthwhile to first convert it into a "nice" one by calling
  1842. * uri_resolve (using 'base') before calling this routine,
  1843. * since this routine (for reasonable efficiency) assumes URI has
  1844. * already been through some validation.
  1845. *
  1846. * Returns a new URI string (to be freed by the caller) or NULL in case
  1847. * error.
  1848. */
  1849. char *uri_resolve_relative(const char *uri, const char *base)
  1850. {
  1851. char *val = NULL;
  1852. int ret;
  1853. int ix;
  1854. int pos = 0;
  1855. int nbslash = 0;
  1856. int len;
  1857. URI *ref = NULL;
  1858. URI *bas = NULL;
  1859. char *bptr, *uptr, *vptr;
  1860. int remove_path = 0;
  1861. if ((uri == NULL) || (*uri == 0)) {
  1862. return NULL;
  1863. }
  1864. /*
  1865. * First parse URI into a standard form
  1866. */
  1867. ref = uri_new();
  1868. /* If URI not already in "relative" form */
  1869. if (uri[0] != '.') {
  1870. ret = uri_parse_into(ref, uri);
  1871. if (ret != 0) {
  1872. goto done; /* Error in URI, return NULL */
  1873. }
  1874. } else {
  1875. ref->path = g_strdup(uri);
  1876. }
  1877. /*
  1878. * Next parse base into the same standard form
  1879. */
  1880. if ((base == NULL) || (*base == 0)) {
  1881. val = g_strdup(uri);
  1882. goto done;
  1883. }
  1884. bas = uri_new();
  1885. if (base[0] != '.') {
  1886. ret = uri_parse_into(bas, base);
  1887. if (ret != 0) {
  1888. goto done; /* Error in base, return NULL */
  1889. }
  1890. } else {
  1891. bas->path = g_strdup(base);
  1892. }
  1893. /*
  1894. * If the scheme / server on the URI differs from the base,
  1895. * just return the URI
  1896. */
  1897. if ((ref->scheme != NULL) &&
  1898. ((bas->scheme == NULL) || (strcmp(bas->scheme, ref->scheme)) ||
  1899. (strcmp(bas->server, ref->server)))) {
  1900. val = g_strdup(uri);
  1901. goto done;
  1902. }
  1903. if (bas->path == ref->path ||
  1904. (bas->path && ref->path && !strcmp(bas->path, ref->path))) {
  1905. val = g_strdup("");
  1906. goto done;
  1907. }
  1908. if (bas->path == NULL) {
  1909. val = g_strdup(ref->path);
  1910. goto done;
  1911. }
  1912. if (ref->path == NULL) {
  1913. ref->path = (char *)"/";
  1914. remove_path = 1;
  1915. }
  1916. /*
  1917. * At this point (at last!) we can compare the two paths
  1918. *
  1919. * First we take care of the special case where either of the
  1920. * two path components may be missing (bug 316224)
  1921. */
  1922. if (bas->path == NULL) {
  1923. if (ref->path != NULL) {
  1924. uptr = ref->path;
  1925. if (*uptr == '/') {
  1926. uptr++;
  1927. }
  1928. /* exception characters from uri_to_string */
  1929. val = uri_string_escape(uptr, "/;&=+$,");
  1930. }
  1931. goto done;
  1932. }
  1933. bptr = bas->path;
  1934. if (ref->path == NULL) {
  1935. for (ix = 0; bptr[ix] != 0; ix++) {
  1936. if (bptr[ix] == '/') {
  1937. nbslash++;
  1938. }
  1939. }
  1940. uptr = NULL;
  1941. len = 1; /* this is for a string terminator only */
  1942. } else {
  1943. /*
  1944. * Next we compare the two strings and find where they first differ
  1945. */
  1946. if ((ref->path[pos] == '.') && (ref->path[pos + 1] == '/')) {
  1947. pos += 2;
  1948. }
  1949. if ((*bptr == '.') && (bptr[1] == '/')) {
  1950. bptr += 2;
  1951. } else if ((*bptr == '/') && (ref->path[pos] != '/')) {
  1952. bptr++;
  1953. }
  1954. while ((bptr[pos] == ref->path[pos]) && (bptr[pos] != 0)) {
  1955. pos++;
  1956. }
  1957. if (bptr[pos] == ref->path[pos]) {
  1958. val = g_strdup("");
  1959. goto done; /* (I can't imagine why anyone would do this) */
  1960. }
  1961. /*
  1962. * In URI, "back up" to the last '/' encountered. This will be the
  1963. * beginning of the "unique" suffix of URI
  1964. */
  1965. ix = pos;
  1966. if ((ref->path[ix] == '/') && (ix > 0)) {
  1967. ix--;
  1968. } else if ((ref->path[ix] == 0) && (ix > 1)
  1969. && (ref->path[ix - 1] == '/')) {
  1970. ix -= 2;
  1971. }
  1972. for (; ix > 0; ix--) {
  1973. if (ref->path[ix] == '/') {
  1974. break;
  1975. }
  1976. }
  1977. if (ix == 0) {
  1978. uptr = ref->path;
  1979. } else {
  1980. ix++;
  1981. uptr = &ref->path[ix];
  1982. }
  1983. /*
  1984. * In base, count the number of '/' from the differing point
  1985. */
  1986. if (bptr[pos] != ref->path[pos]) { /* check for trivial URI == base */
  1987. for (; bptr[ix] != 0; ix++) {
  1988. if (bptr[ix] == '/') {
  1989. nbslash++;
  1990. }
  1991. }
  1992. }
  1993. len = strlen(uptr) + 1;
  1994. }
  1995. if (nbslash == 0) {
  1996. if (uptr != NULL) {
  1997. /* exception characters from uri_to_string */
  1998. val = uri_string_escape(uptr, "/;&=+$,");
  1999. }
  2000. goto done;
  2001. }
  2002. /*
  2003. * Allocate just enough space for the returned string -
  2004. * length of the remainder of the URI, plus enough space
  2005. * for the "../" groups, plus one for the terminator
  2006. */
  2007. val = g_malloc(len + 3 * nbslash);
  2008. vptr = val;
  2009. /*
  2010. * Put in as many "../" as needed
  2011. */
  2012. for (; nbslash > 0; nbslash--) {
  2013. *vptr++ = '.';
  2014. *vptr++ = '.';
  2015. *vptr++ = '/';
  2016. }
  2017. /*
  2018. * Finish up with the end of the URI
  2019. */
  2020. if (uptr != NULL) {
  2021. if ((vptr > val) && (len > 0) && (uptr[0] == '/') &&
  2022. (vptr[-1] == '/')) {
  2023. memcpy(vptr, uptr + 1, len - 1);
  2024. vptr[len - 2] = 0;
  2025. } else {
  2026. memcpy(vptr, uptr, len);
  2027. vptr[len - 1] = 0;
  2028. }
  2029. } else {
  2030. vptr[len - 1] = 0;
  2031. }
  2032. /* escape the freshly-built path */
  2033. vptr = val;
  2034. /* exception characters from uri_to_string */
  2035. val = uri_string_escape(vptr, "/;&=+$,");
  2036. g_free(vptr);
  2037. done:
  2038. /*
  2039. * Free the working variables
  2040. */
  2041. if (remove_path != 0) {
  2042. ref->path = NULL;
  2043. }
  2044. if (ref != NULL) {
  2045. uri_free(ref);
  2046. }
  2047. if (bas != NULL) {
  2048. uri_free(bas);
  2049. }
  2050. return val;
  2051. }
  2052. /*
  2053. * Utility functions to help parse and assemble query strings.
  2054. */
  2055. struct QueryParams *query_params_new(int init_alloc)
  2056. {
  2057. struct QueryParams *ps;
  2058. if (init_alloc <= 0) {
  2059. init_alloc = 1;
  2060. }
  2061. ps = g_new(QueryParams, 1);
  2062. ps->n = 0;
  2063. ps->alloc = init_alloc;
  2064. ps->p = g_new(QueryParam, ps->alloc);
  2065. return ps;
  2066. }
  2067. /* Ensure there is space to store at least one more parameter
  2068. * at the end of the set.
  2069. */
  2070. static int query_params_append(struct QueryParams *ps, const char *name,
  2071. const char *value)
  2072. {
  2073. if (ps->n >= ps->alloc) {
  2074. ps->p = g_renew(QueryParam, ps->p, ps->alloc * 2);
  2075. ps->alloc *= 2;
  2076. }
  2077. ps->p[ps->n].name = g_strdup(name);
  2078. ps->p[ps->n].value = g_strdup(value);
  2079. ps->p[ps->n].ignore = 0;
  2080. ps->n++;
  2081. return 0;
  2082. }
  2083. void query_params_free(struct QueryParams *ps)
  2084. {
  2085. int i;
  2086. for (i = 0; i < ps->n; ++i) {
  2087. g_free(ps->p[i].name);
  2088. g_free(ps->p[i].value);
  2089. }
  2090. g_free(ps->p);
  2091. g_free(ps);
  2092. }
  2093. struct QueryParams *query_params_parse(const char *query)
  2094. {
  2095. struct QueryParams *ps;
  2096. const char *end, *eq;
  2097. ps = query_params_new(0);
  2098. if (!query || query[0] == '\0') {
  2099. return ps;
  2100. }
  2101. while (*query) {
  2102. char *name = NULL, *value = NULL;
  2103. /* Find the next separator, or end of the string. */
  2104. end = strchr(query, '&');
  2105. if (!end) {
  2106. end = qemu_strchrnul(query, ';');
  2107. }
  2108. /* Find the first '=' character between here and end. */
  2109. eq = strchr(query, '=');
  2110. if (eq && eq >= end) {
  2111. eq = NULL;
  2112. }
  2113. /* Empty section (eg. "&&"). */
  2114. if (end == query) {
  2115. goto next;
  2116. }
  2117. /* If there is no '=' character, then we have just "name"
  2118. * and consistent with CGI.pm we assume value is "".
  2119. */
  2120. else if (!eq) {
  2121. name = uri_string_unescape(query, end - query, NULL);
  2122. value = NULL;
  2123. }
  2124. /* Or if we have "name=" here (works around annoying
  2125. * problem when calling uri_string_unescape with len = 0).
  2126. */
  2127. else if (eq + 1 == end) {
  2128. name = uri_string_unescape(query, eq - query, NULL);
  2129. value = g_new0(char, 1);
  2130. }
  2131. /* If the '=' character is at the beginning then we have
  2132. * "=value" and consistent with CGI.pm we _ignore_ this.
  2133. */
  2134. else if (query == eq) {
  2135. goto next;
  2136. }
  2137. /* Otherwise it's "name=value". */
  2138. else {
  2139. name = uri_string_unescape(query, eq - query, NULL);
  2140. value = uri_string_unescape(eq + 1, end - (eq + 1), NULL);
  2141. }
  2142. /* Append to the parameter set. */
  2143. query_params_append(ps, name, value);
  2144. g_free(name);
  2145. g_free(value);
  2146. next:
  2147. query = end;
  2148. if (*query) {
  2149. query++; /* skip '&' separator */
  2150. }
  2151. }
  2152. return ps;
  2153. }