miniwget.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /* $Id: miniwget.c,v 1.77 2017/05/09 10:04:57 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Website : http://miniupnp.free.fr/
  4. * Author : Thomas Bernard
  5. * Copyright (c) 2005-2017 Thomas Bernard
  6. * This software is subject to the conditions detailed in the
  7. * LICENCE file provided in this distribution. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #ifdef _WIN32
  13. #include <winsock2.h>
  14. #include <ws2tcpip.h>
  15. #include <io.h>
  16. #define MAXHOSTNAMELEN 64
  17. #define snprintf _snprintf
  18. #define socklen_t int
  19. #ifndef strncasecmp
  20. #if defined(_MSC_VER) && (_MSC_VER >= 1400)
  21. #define strncasecmp _memicmp
  22. #else /* defined(_MSC_VER) && (_MSC_VER >= 1400) */
  23. #define strncasecmp memicmp
  24. #endif /* defined(_MSC_VER) && (_MSC_VER >= 1400) */
  25. #endif /* #ifndef strncasecmp */
  26. #else /* #ifdef _WIN32 */
  27. #include <unistd.h>
  28. #include <sys/param.h>
  29. #if defined(__amigaos__) && !defined(__amigaos4__)
  30. #define socklen_t int
  31. #else /* #if defined(__amigaos__) && !defined(__amigaos4__) */
  32. #include <sys/select.h>
  33. #endif /* #else defined(__amigaos__) && !defined(__amigaos4__) */
  34. #include <sys/socket.h>
  35. #include <netinet/in.h>
  36. #include <arpa/inet.h>
  37. #include <net/if.h>
  38. #include <netdb.h>
  39. #define closesocket close
  40. #include <strings.h>
  41. #endif /* #else _WIN32 */
  42. #ifdef __GNU__
  43. #define MAXHOSTNAMELEN 64
  44. #endif /* __GNU__ */
  45. #ifndef MIN
  46. #define MIN(x,y) (((x)<(y))?(x):(y))
  47. #endif /* MIN */
  48. #ifdef _WIN32
  49. #undef OS_STRING
  50. #define OS_STRING "Win32"
  51. #define MINIUPNPC_VERSION_STRING "2.0"
  52. #define UPNP_VERSION_STRING "UPnP/1.1"
  53. #endif
  54. #ifdef __ANDROID__
  55. #undef OS_STRING
  56. #define OS_STRING "Android"
  57. #define MINIUPNPC_VERSION_STRING "2.0"
  58. #define UPNP_VERSION_STRING "UPnP/1.1"
  59. #endif
  60. #include "miniwget.h"
  61. #include "connecthostport.h"
  62. #include "receivedata.h"
  63. #ifndef MAXHOSTNAMELEN
  64. #define MAXHOSTNAMELEN 64
  65. #endif
  66. /*
  67. * Read a HTTP response from a socket.
  68. * Process Content-Length and Transfer-encoding headers.
  69. * return a pointer to the content buffer, which length is saved
  70. * to the length parameter.
  71. */
  72. void *
  73. getHTTPResponse(int s, int * size, int * status_code)
  74. {
  75. char buf[2048];
  76. int n;
  77. int endofheaders = 0;
  78. int chunked = 0;
  79. int content_length = -1;
  80. unsigned int chunksize = 0;
  81. unsigned int bytestocopy = 0;
  82. /* buffers : */
  83. char * header_buf;
  84. unsigned int header_buf_len = 2048;
  85. unsigned int header_buf_used = 0;
  86. char * content_buf;
  87. unsigned int content_buf_len = 2048;
  88. unsigned int content_buf_used = 0;
  89. char chunksize_buf[32];
  90. unsigned int chunksize_buf_index;
  91. #ifdef DEBUG
  92. char * reason_phrase = NULL;
  93. int reason_phrase_len = 0;
  94. #endif
  95. if(status_code) *status_code = -1;
  96. header_buf = malloc(header_buf_len);
  97. if(header_buf == NULL)
  98. {
  99. #ifdef DEBUG
  100. fprintf(stderr, "%s: Memory allocation error\n", "getHTTPResponse");
  101. #endif /* DEBUG */
  102. *size = -1;
  103. return NULL;
  104. }
  105. content_buf = malloc(content_buf_len);
  106. if(content_buf == NULL)
  107. {
  108. free(header_buf);
  109. #ifdef DEBUG
  110. fprintf(stderr, "%s: Memory allocation error\n", "getHTTPResponse");
  111. #endif /* DEBUG */
  112. *size = -1;
  113. return NULL;
  114. }
  115. chunksize_buf[0] = '\0';
  116. chunksize_buf_index = 0;
  117. while((n = receivedata(s, buf, sizeof(buf), 5000, NULL)) > 0)
  118. {
  119. if(endofheaders == 0)
  120. {
  121. int i;
  122. int linestart=0;
  123. int colon=0;
  124. int valuestart=0;
  125. if(header_buf_used + n > header_buf_len) {
  126. char * tmp = realloc(header_buf, header_buf_used + n);
  127. if(tmp == NULL) {
  128. /* memory allocation error */
  129. free(header_buf);
  130. free(content_buf);
  131. *size = -1;
  132. return NULL;
  133. }
  134. header_buf = tmp;
  135. header_buf_len = header_buf_used + n;
  136. }
  137. memcpy(header_buf + header_buf_used, buf, n);
  138. header_buf_used += n;
  139. /* search for CR LF CR LF (end of headers)
  140. * recognize also LF LF */
  141. i = 0;
  142. while(i < ((int)header_buf_used-1) && (endofheaders == 0)) {
  143. if(header_buf[i] == '\r') {
  144. i++;
  145. if(header_buf[i] == '\n') {
  146. i++;
  147. if(i < (int)header_buf_used && header_buf[i] == '\r') {
  148. i++;
  149. if(i < (int)header_buf_used && header_buf[i] == '\n') {
  150. endofheaders = i+1;
  151. }
  152. }
  153. }
  154. } else if(header_buf[i] == '\n') {
  155. i++;
  156. if(header_buf[i] == '\n') {
  157. endofheaders = i+1;
  158. }
  159. }
  160. i++;
  161. }
  162. if(endofheaders == 0)
  163. continue;
  164. /* parse header lines */
  165. for(i = 0; i < endofheaders - 1; i++) {
  166. if(linestart > 0 && colon <= linestart && header_buf[i]==':')
  167. {
  168. colon = i;
  169. while(i < (endofheaders-1)
  170. && (header_buf[i+1] == ' ' || header_buf[i+1] == '\t'))
  171. i++;
  172. valuestart = i + 1;
  173. }
  174. /* detecting end of line */
  175. else if(header_buf[i]=='\r' || header_buf[i]=='\n')
  176. {
  177. if(linestart == 0 && status_code)
  178. {
  179. /* Status line
  180. * HTTP-Version SP Status-Code SP Reason-Phrase CRLF */
  181. int sp;
  182. for(sp = 0; sp < i; sp++)
  183. if(header_buf[sp] == ' ')
  184. {
  185. if(*status_code < 0)
  186. *status_code = atoi(header_buf + sp + 1);
  187. else
  188. {
  189. #ifdef DEBUG
  190. reason_phrase = header_buf + sp + 1;
  191. reason_phrase_len = i - sp - 1;
  192. #endif
  193. break;
  194. }
  195. }
  196. #ifdef DEBUG
  197. printf("HTTP status code = %d, Reason phrase = %.*s\n",
  198. *status_code, reason_phrase_len, reason_phrase);
  199. #endif
  200. }
  201. else if(colon > linestart && valuestart > colon)
  202. {
  203. #ifdef DEBUG
  204. printf("header='%.*s', value='%.*s'\n",
  205. colon-linestart, header_buf+linestart,
  206. i-valuestart, header_buf+valuestart);
  207. #endif
  208. if(0==strncasecmp(header_buf+linestart, "content-length", colon-linestart))
  209. {
  210. content_length = atoi(header_buf+valuestart);
  211. #ifdef DEBUG
  212. printf("Content-Length: %d\n", content_length);
  213. #endif
  214. }
  215. else if(0==strncasecmp(header_buf+linestart, "transfer-encoding", colon-linestart)
  216. && 0==strncasecmp(header_buf+valuestart, "chunked", 7))
  217. {
  218. #ifdef DEBUG
  219. printf("chunked transfer-encoding!\n");
  220. #endif
  221. chunked = 1;
  222. }
  223. }
  224. while((i < (int)header_buf_used) && (header_buf[i]=='\r' || header_buf[i] == '\n'))
  225. i++;
  226. linestart = i;
  227. colon = linestart;
  228. valuestart = 0;
  229. }
  230. }
  231. /* copy the remaining of the received data back to buf */
  232. n = header_buf_used - endofheaders;
  233. memcpy(buf, header_buf + endofheaders, n);
  234. /* if(headers) */
  235. }
  236. if(endofheaders)
  237. {
  238. /* content */
  239. if(chunked)
  240. {
  241. int i = 0;
  242. while(i < n)
  243. {
  244. if(chunksize == 0)
  245. {
  246. /* reading chunk size */
  247. if(chunksize_buf_index == 0) {
  248. /* skipping any leading CR LF */
  249. if(i<n && buf[i] == '\r') i++;
  250. if(i<n && buf[i] == '\n') i++;
  251. }
  252. while(i<n && isxdigit(buf[i])
  253. && chunksize_buf_index < (sizeof(chunksize_buf)-1))
  254. {
  255. chunksize_buf[chunksize_buf_index++] = buf[i];
  256. chunksize_buf[chunksize_buf_index] = '\0';
  257. i++;
  258. }
  259. while(i<n && buf[i] != '\r' && buf[i] != '\n')
  260. i++; /* discarding chunk-extension */
  261. if(i<n && buf[i] == '\r') i++;
  262. if(i<n && buf[i] == '\n') {
  263. unsigned int j;
  264. for(j = 0; j < chunksize_buf_index; j++) {
  265. if(chunksize_buf[j] >= '0'
  266. && chunksize_buf[j] <= '9')
  267. chunksize = (chunksize << 4) + (chunksize_buf[j] - '0');
  268. else
  269. chunksize = (chunksize << 4) + ((chunksize_buf[j] | 32) - 'a' + 10);
  270. }
  271. chunksize_buf[0] = '\0';
  272. chunksize_buf_index = 0;
  273. i++;
  274. } else {
  275. /* not finished to get chunksize */
  276. continue;
  277. }
  278. #ifdef DEBUG
  279. printf("chunksize = %u (%x)\n", chunksize, chunksize);
  280. #endif
  281. if(chunksize == 0)
  282. {
  283. #ifdef DEBUG
  284. printf("end of HTTP content - %d %d\n", i, n);
  285. /*printf("'%.*s'\n", n-i, buf+i);*/
  286. #endif
  287. goto end_of_stream;
  288. }
  289. }
  290. /* it is guaranteed that (n >= i) */
  291. bytestocopy = (chunksize < (unsigned int)(n - i))?chunksize:(unsigned int)(n - i);
  292. if((content_buf_used + bytestocopy) > content_buf_len)
  293. {
  294. char * tmp;
  295. if((content_length >= 0) && ((unsigned int)content_length >= (content_buf_used + bytestocopy))) {
  296. content_buf_len = content_length;
  297. } else {
  298. content_buf_len = content_buf_used + bytestocopy;
  299. }
  300. tmp = realloc(content_buf, content_buf_len);
  301. if(tmp == NULL) {
  302. /* memory allocation error */
  303. free(content_buf);
  304. free(header_buf);
  305. *size = -1;
  306. return NULL;
  307. }
  308. content_buf = tmp;
  309. }
  310. memcpy(content_buf + content_buf_used, buf + i, bytestocopy);
  311. content_buf_used += bytestocopy;
  312. i += bytestocopy;
  313. chunksize -= bytestocopy;
  314. }
  315. }
  316. else
  317. {
  318. /* not chunked */
  319. if(content_length > 0
  320. && (content_buf_used + n) > (unsigned int)content_length) {
  321. /* skipping additional bytes */
  322. n = content_length - content_buf_used;
  323. }
  324. if(content_buf_used + n > content_buf_len)
  325. {
  326. char * tmp;
  327. if(content_length >= 0
  328. && (unsigned int)content_length >= (content_buf_used + n)) {
  329. content_buf_len = content_length;
  330. } else {
  331. content_buf_len = content_buf_used + n;
  332. }
  333. tmp = realloc(content_buf, content_buf_len);
  334. if(tmp == NULL) {
  335. /* memory allocation error */
  336. free(content_buf);
  337. free(header_buf);
  338. *size = -1;
  339. return NULL;
  340. }
  341. content_buf = tmp;
  342. }
  343. memcpy(content_buf + content_buf_used, buf, n);
  344. content_buf_used += n;
  345. }
  346. }
  347. /* use the Content-Length header value if available */
  348. if(content_length > 0 && content_buf_used >= (unsigned int)content_length)
  349. {
  350. #ifdef DEBUG
  351. printf("End of HTTP content\n");
  352. #endif
  353. break;
  354. }
  355. }
  356. end_of_stream:
  357. free(header_buf); header_buf = NULL;
  358. *size = content_buf_used;
  359. if(content_buf_used == 0)
  360. {
  361. free(content_buf);
  362. content_buf = NULL;
  363. }
  364. return content_buf;
  365. }
  366. /* miniwget3() :
  367. * do all the work.
  368. * Return NULL if something failed. */
  369. static void *
  370. miniwget3(const char * host,
  371. unsigned short port, const char * path,
  372. int * size, char * addr_str, int addr_str_len,
  373. const char * httpversion, unsigned int scope_id,
  374. int * status_code)
  375. {
  376. char buf[2048];
  377. int s;
  378. int n;
  379. int len;
  380. int sent;
  381. void * content;
  382. *size = 0;
  383. s = connecthostport(host, port, scope_id);
  384. if(s < 0)
  385. return NULL;
  386. /* get address for caller ! */
  387. if(addr_str)
  388. {
  389. struct sockaddr_storage saddr;
  390. socklen_t saddrlen;
  391. saddrlen = sizeof(saddr);
  392. if(getsockname(s, (struct sockaddr *)&saddr, &saddrlen) < 0)
  393. {
  394. perror("getsockname");
  395. }
  396. else
  397. {
  398. #if defined(__amigaos__) && !defined(__amigaos4__)
  399. /* using INT WINAPI WSAAddressToStringA(LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFOA, LPSTR, LPDWORD);
  400. * But his function make a string with the port : nn.nn.nn.nn:port */
  401. /* if(WSAAddressToStringA((SOCKADDR *)&saddr, sizeof(saddr),
  402. NULL, addr_str, (DWORD *)&addr_str_len))
  403. {
  404. printf("WSAAddressToStringA() failed : %d\n", WSAGetLastError());
  405. }*/
  406. /* the following code is only compatible with ip v4 addresses */
  407. strncpy(addr_str, inet_ntoa(((struct sockaddr_in *)&saddr)->sin_addr), addr_str_len);
  408. #else
  409. #if 0
  410. if(saddr.sa_family == AF_INET6) {
  411. inet_ntop(AF_INET6,
  412. &(((struct sockaddr_in6 *)&saddr)->sin6_addr),
  413. addr_str, addr_str_len);
  414. } else {
  415. inet_ntop(AF_INET,
  416. &(((struct sockaddr_in *)&saddr)->sin_addr),
  417. addr_str, addr_str_len);
  418. }
  419. #endif
  420. /* getnameinfo return ip v6 address with the scope identifier
  421. * such as : 2a01:e35:8b2b:7330::%4281128194 */
  422. n = getnameinfo((const struct sockaddr *)&saddr, saddrlen,
  423. addr_str, addr_str_len,
  424. NULL, 0,
  425. NI_NUMERICHOST | NI_NUMERICSERV);
  426. if(n != 0) {
  427. #ifdef _WIN32
  428. fprintf(stderr, "getnameinfo() failed : %d\n", n);
  429. #else
  430. fprintf(stderr, "getnameinfo() failed : %s\n", gai_strerror(n));
  431. #endif
  432. }
  433. #endif
  434. }
  435. #ifdef DEBUG
  436. printf("address miniwget : %s\n", addr_str);
  437. #endif
  438. }
  439. len = snprintf(buf, sizeof(buf),
  440. "GET %s HTTP/%s\r\n"
  441. "Host: %s:%d\r\n"
  442. "Connection: Close\r\n"
  443. "User-Agent: " OS_STRING ", " UPNP_VERSION_STRING ", MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n"
  444. "\r\n",
  445. path, httpversion, host, port);
  446. if ((unsigned int)len >= sizeof(buf))
  447. {
  448. closesocket(s);
  449. return NULL;
  450. }
  451. sent = 0;
  452. /* sending the HTTP request */
  453. while(sent < len)
  454. {
  455. n = send(s, buf+sent, len-sent, 0);
  456. if(n < 0)
  457. {
  458. perror("send");
  459. closesocket(s);
  460. return NULL;
  461. }
  462. else
  463. {
  464. sent += n;
  465. }
  466. }
  467. content = getHTTPResponse(s, size, status_code);
  468. closesocket(s);
  469. return content;
  470. }
  471. /* miniwget2() :
  472. * Call miniwget3(); retry with HTTP/1.1 if 1.0 fails. */
  473. static void *
  474. miniwget2(const char * host,
  475. unsigned short port, const char * path,
  476. int * size, char * addr_str, int addr_str_len,
  477. unsigned int scope_id, int * status_code)
  478. {
  479. char * respbuffer;
  480. #if 1
  481. respbuffer = miniwget3(host, port, path, size,
  482. addr_str, addr_str_len, "1.1",
  483. scope_id, status_code);
  484. #else
  485. respbuffer = miniwget3(host, port, path, size,
  486. addr_str, addr_str_len, "1.0",
  487. scope_id, status_code);
  488. if (*size == 0)
  489. {
  490. #ifdef DEBUG
  491. printf("Retrying with HTTP/1.1\n");
  492. #endif
  493. free(respbuffer);
  494. respbuffer = miniwget3(host, port, path, size,
  495. addr_str, addr_str_len, "1.1",
  496. scope_id, status_code);
  497. }
  498. #endif
  499. return respbuffer;
  500. }
  501. /* parseURL()
  502. * arguments :
  503. * url : source string not modified
  504. * hostname : hostname destination string (size of MAXHOSTNAMELEN+1)
  505. * port : port (destination)
  506. * path : pointer to the path part of the URL
  507. *
  508. * Return values :
  509. * 0 - Failure
  510. * 1 - Success */
  511. int
  512. parseURL(const char * url,
  513. char * hostname, unsigned short * port,
  514. char * * path, unsigned int * scope_id)
  515. {
  516. char * p1, *p2, *p3;
  517. if(!url)
  518. return 0;
  519. p1 = strstr(url, "://");
  520. if(!p1)
  521. return 0;
  522. p1 += 3;
  523. if( (url[0]!='h') || (url[1]!='t')
  524. ||(url[2]!='t') || (url[3]!='p'))
  525. return 0;
  526. memset(hostname, 0, MAXHOSTNAMELEN + 1);
  527. if(*p1 == '[')
  528. {
  529. /* IP v6 : http://[2a00:1450:8002::6a]/path/abc */
  530. char * scope;
  531. scope = strchr(p1, '%');
  532. p2 = strchr(p1, ']');
  533. if(p2 && scope && scope < p2 && scope_id) {
  534. /* parse scope */
  535. #ifdef IF_NAMESIZE
  536. char tmp[IF_NAMESIZE];
  537. int l;
  538. scope++;
  539. /* "%25" is just '%' in URL encoding */
  540. if(scope[0] == '2' && scope[1] == '5')
  541. scope += 2; /* skip "25" */
  542. l = p2 - scope;
  543. if(l >= IF_NAMESIZE)
  544. l = IF_NAMESIZE - 1;
  545. memcpy(tmp, scope, l);
  546. tmp[l] = '\0';
  547. *scope_id = if_nametoindex(tmp);
  548. if(*scope_id == 0) {
  549. *scope_id = (unsigned int)strtoul(tmp, NULL, 10);
  550. }
  551. #else
  552. /* under windows, scope is numerical */
  553. char tmp[8];
  554. int l;
  555. scope++;
  556. /* "%25" is just '%' in URL encoding */
  557. if(scope[0] == '2' && scope[1] == '5')
  558. scope += 2; /* skip "25" */
  559. l = p2 - scope;
  560. if(l >= sizeof(tmp))
  561. l = sizeof(tmp) - 1;
  562. memcpy(tmp, scope, l);
  563. tmp[l] = '\0';
  564. *scope_id = (unsigned int)strtoul(tmp, NULL, 10);
  565. #endif
  566. }
  567. p3 = strchr(p1, '/');
  568. if(p2 && p3)
  569. {
  570. p2++;
  571. strncpy(hostname, p1, MIN(MAXHOSTNAMELEN, (int)(p2-p1)));
  572. if(*p2 == ':')
  573. {
  574. *port = 0;
  575. p2++;
  576. while( (*p2 >= '0') && (*p2 <= '9'))
  577. {
  578. *port *= 10;
  579. *port += (unsigned short)(*p2 - '0');
  580. p2++;
  581. }
  582. }
  583. else
  584. {
  585. *port = 80;
  586. }
  587. *path = p3;
  588. return 1;
  589. }
  590. }
  591. p2 = strchr(p1, ':');
  592. p3 = strchr(p1, '/');
  593. if(!p3)
  594. return 0;
  595. if(!p2 || (p2>p3))
  596. {
  597. strncpy(hostname, p1, MIN(MAXHOSTNAMELEN, (int)(p3-p1)));
  598. *port = 80;
  599. }
  600. else
  601. {
  602. strncpy(hostname, p1, MIN(MAXHOSTNAMELEN, (int)(p2-p1)));
  603. *port = 0;
  604. p2++;
  605. while( (*p2 >= '0') && (*p2 <= '9'))
  606. {
  607. *port *= 10;
  608. *port += (unsigned short)(*p2 - '0');
  609. p2++;
  610. }
  611. }
  612. *path = p3;
  613. return 1;
  614. }
  615. void *
  616. miniwget(const char * url, int * size,
  617. unsigned int scope_id, int * status_code)
  618. {
  619. unsigned short port;
  620. char * path;
  621. /* protocol://host:port/chemin */
  622. char hostname[MAXHOSTNAMELEN+1];
  623. *size = 0;
  624. if(!parseURL(url, hostname, &port, &path, &scope_id))
  625. return NULL;
  626. #ifdef DEBUG
  627. printf("parsed url : hostname='%s' port=%hu path='%s' scope_id=%u\n",
  628. hostname, port, path, scope_id);
  629. #endif
  630. return miniwget2(hostname, port, path, size, 0, 0, scope_id, status_code);
  631. }
  632. void *
  633. miniwget_getaddr(const char * url, int * size,
  634. char * addr, int addrlen, unsigned int scope_id,
  635. int * status_code)
  636. {
  637. unsigned short port;
  638. char * path;
  639. /* protocol://host:port/path */
  640. char hostname[MAXHOSTNAMELEN+1];
  641. *size = 0;
  642. if(addr)
  643. addr[0] = '\0';
  644. if(!parseURL(url, hostname, &port, &path, &scope_id))
  645. return NULL;
  646. #ifdef DEBUG
  647. printf("parsed url : hostname='%s' port=%hu path='%s' scope_id=%u\n",
  648. hostname, port, path, scope_id);
  649. #endif
  650. return miniwget2(hostname, port, path, size, addr, addrlen, scope_id, status_code);
  651. }