nanohttp.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  1. /*
  2. * nanohttp.c: minimalist HTTP GET implementation to fetch external subsets.
  3. * focuses on size, streamability, reentrancy and portability
  4. *
  5. * This is clearly not a general purpose HTTP implementation
  6. * If you look for one, check:
  7. * http://www.w3.org/Library/
  8. *
  9. * See Copyright for the status of this software.
  10. *
  11. * daniel@veillard.com
  12. */
  13. #define IN_LIBXML
  14. #include "libxml.h"
  15. #ifdef LIBXML_HTTP_ENABLED
  16. #include <string.h>
  17. #ifdef HAVE_STDLIB_H
  18. #include <stdlib.h>
  19. #endif
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #ifdef HAVE_SYS_TYPES_H
  24. #include <sys/types.h>
  25. #endif
  26. #ifdef HAVE_SYS_SOCKET_H
  27. #include <sys/socket.h>
  28. #endif
  29. #ifdef HAVE_NETINET_IN_H
  30. #include <netinet/in.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef HAVE_NETDB_H
  36. #include <netdb.h>
  37. #endif
  38. #ifdef HAVE_RESOLV_H
  39. #ifdef HAVE_ARPA_NAMESER_H
  40. #include <arpa/nameser.h>
  41. #endif
  42. #include <resolv.h>
  43. #endif
  44. #ifdef HAVE_FCNTL_H
  45. #include <fcntl.h>
  46. #endif
  47. #ifdef HAVE_ERRNO_H
  48. #include <errno.h>
  49. #endif
  50. #ifdef HAVE_SYS_TIME_H
  51. #include <sys/time.h>
  52. #endif
  53. #ifndef HAVE_POLL_H
  54. #ifdef HAVE_SYS_SELECT_H
  55. #include <sys/select.h>
  56. #endif
  57. #else
  58. #include <poll.h>
  59. #endif
  60. #ifdef HAVE_STRINGS_H
  61. #include <strings.h>
  62. #endif
  63. #ifdef LIBXML_ZLIB_ENABLED
  64. #include <zlib.h>
  65. #endif
  66. #ifdef VMS
  67. #include <stropts>
  68. #define XML_SOCKLEN_T unsigned int
  69. #endif
  70. #if defined(_WIN32) && !defined(__CYGWIN__)
  71. #include <wsockcompat.h>
  72. #endif
  73. #include <libxml/globals.h>
  74. #include <libxml/xmlerror.h>
  75. #include <libxml/xmlmemory.h>
  76. #include <libxml/parser.h> /* for xmlStr(n)casecmp() */
  77. #include <libxml/nanohttp.h>
  78. #include <libxml/globals.h>
  79. #include <libxml/uri.h>
  80. /**
  81. * A couple portability macros
  82. */
  83. #ifndef _WINSOCKAPI_
  84. #if !defined(__BEOS__) || defined(__HAIKU__)
  85. #define closesocket(s) close(s)
  86. #endif
  87. #define SOCKET int
  88. #define INVALID_SOCKET (-1)
  89. #endif
  90. #ifdef __BEOS__
  91. #ifndef PF_INET
  92. #define PF_INET AF_INET
  93. #endif
  94. #endif
  95. #ifndef XML_SOCKLEN_T
  96. #define XML_SOCKLEN_T unsigned int
  97. #endif
  98. #ifdef STANDALONE
  99. #define DEBUG_HTTP
  100. #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
  101. #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
  102. #endif
  103. #define XML_NANO_HTTP_MAX_REDIR 10
  104. #define XML_NANO_HTTP_CHUNK 4096
  105. #define XML_NANO_HTTP_CLOSED 0
  106. #define XML_NANO_HTTP_WRITE 1
  107. #define XML_NANO_HTTP_READ 2
  108. #define XML_NANO_HTTP_NONE 4
  109. typedef struct xmlNanoHTTPCtxt {
  110. char *protocol; /* the protocol name */
  111. char *hostname; /* the host name */
  112. int port; /* the port */
  113. char *path; /* the path within the URL */
  114. char *query; /* the query string */
  115. SOCKET fd; /* the file descriptor for the socket */
  116. int state; /* WRITE / READ / CLOSED */
  117. char *out; /* buffer sent (zero terminated) */
  118. char *outptr; /* index within the buffer sent */
  119. char *in; /* the receiving buffer */
  120. char *content; /* the start of the content */
  121. char *inptr; /* the next byte to read from network */
  122. char *inrptr; /* the next byte to give back to the client */
  123. int inlen; /* len of the input buffer */
  124. int last; /* return code for last operation */
  125. int returnValue; /* the protocol return value */
  126. int version; /* the protocol version */
  127. int ContentLength; /* specified content length from HTTP header */
  128. char *contentType; /* the MIME type for the input */
  129. char *location; /* the new URL in case of redirect */
  130. char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
  131. char *encoding; /* encoding extracted from the contentType */
  132. char *mimeType; /* Mime-Type extracted from the contentType */
  133. #ifdef LIBXML_ZLIB_ENABLED
  134. z_stream *strm; /* Zlib stream object */
  135. int usesGzip; /* "Content-Encoding: gzip" was detected */
  136. #endif
  137. } xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
  138. static int initialized = 0;
  139. static char *proxy = NULL; /* the proxy name if any */
  140. static int proxyPort; /* the proxy port if any */
  141. static unsigned int timeout = 60;/* the select() timeout in seconds */
  142. static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
  143. /**
  144. * xmlHTTPErrMemory:
  145. * @extra: extra information
  146. *
  147. * Handle an out of memory condition
  148. */
  149. static void
  150. xmlHTTPErrMemory(const char *extra)
  151. {
  152. __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
  153. }
  154. /**
  155. * A portability function
  156. */
  157. static int socket_errno(void) {
  158. #ifdef _WINSOCKAPI_
  159. int err = WSAGetLastError();
  160. switch(err) {
  161. case WSAECONNRESET:
  162. return(ECONNRESET);
  163. case WSAEINPROGRESS:
  164. return(EINPROGRESS);
  165. case WSAEINTR:
  166. return(EINTR);
  167. case WSAESHUTDOWN:
  168. return(ESHUTDOWN);
  169. case WSAEWOULDBLOCK:
  170. return(EWOULDBLOCK);
  171. default:
  172. return(err);
  173. }
  174. #else
  175. return(errno);
  176. #endif
  177. }
  178. #ifdef SUPPORT_IP6
  179. static
  180. int have_ipv6(void) {
  181. SOCKET s;
  182. s = socket (AF_INET6, SOCK_STREAM, 0);
  183. if (s != INVALID_SOCKET) {
  184. close (s);
  185. return (1);
  186. }
  187. return (0);
  188. }
  189. #endif
  190. /**
  191. * xmlNanoHTTPInit:
  192. *
  193. * Initialize the HTTP protocol layer.
  194. * Currently it just checks for proxy information
  195. */
  196. void
  197. xmlNanoHTTPInit(void) {
  198. const char *env;
  199. #ifdef _WINSOCKAPI_
  200. WSADATA wsaData;
  201. #endif
  202. if (initialized)
  203. return;
  204. #ifdef _WINSOCKAPI_
  205. if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
  206. return;
  207. #endif
  208. if (proxy == NULL) {
  209. proxyPort = 80;
  210. env = getenv("no_proxy");
  211. if (env && ((env[0] == '*') && (env[1] == 0)))
  212. goto done;
  213. env = getenv("http_proxy");
  214. if (env != NULL) {
  215. xmlNanoHTTPScanProxy(env);
  216. goto done;
  217. }
  218. env = getenv("HTTP_PROXY");
  219. if (env != NULL) {
  220. xmlNanoHTTPScanProxy(env);
  221. goto done;
  222. }
  223. }
  224. done:
  225. initialized = 1;
  226. }
  227. /**
  228. * xmlNanoHTTPCleanup:
  229. *
  230. * Cleanup the HTTP protocol layer.
  231. */
  232. void
  233. xmlNanoHTTPCleanup(void) {
  234. if (proxy != NULL) {
  235. xmlFree(proxy);
  236. proxy = NULL;
  237. }
  238. #ifdef _WINSOCKAPI_
  239. if (initialized)
  240. WSACleanup();
  241. #endif
  242. initialized = 0;
  243. return;
  244. }
  245. /**
  246. * xmlNanoHTTPScanURL:
  247. * @ctxt: an HTTP context
  248. * @URL: The URL used to initialize the context
  249. *
  250. * (Re)Initialize an HTTP context by parsing the URL and finding
  251. * the protocol host port and path it indicates.
  252. */
  253. static void
  254. xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
  255. xmlURIPtr uri;
  256. int len;
  257. /*
  258. * Clear any existing data from the context
  259. */
  260. if (ctxt->protocol != NULL) {
  261. xmlFree(ctxt->protocol);
  262. ctxt->protocol = NULL;
  263. }
  264. if (ctxt->hostname != NULL) {
  265. xmlFree(ctxt->hostname);
  266. ctxt->hostname = NULL;
  267. }
  268. if (ctxt->path != NULL) {
  269. xmlFree(ctxt->path);
  270. ctxt->path = NULL;
  271. }
  272. if (ctxt->query != NULL) {
  273. xmlFree(ctxt->query);
  274. ctxt->query = NULL;
  275. }
  276. if (URL == NULL) return;
  277. uri = xmlParseURIRaw(URL, 1);
  278. if (uri == NULL)
  279. return;
  280. if ((uri->scheme == NULL) || (uri->server == NULL)) {
  281. xmlFreeURI(uri);
  282. return;
  283. }
  284. ctxt->protocol = xmlMemStrdup(uri->scheme);
  285. /* special case of IPv6 addresses, the [] need to be removed */
  286. if ((uri->server != NULL) && (*uri->server == '[')) {
  287. len = strlen(uri->server);
  288. if ((len > 2) && (uri->server[len - 1] == ']')) {
  289. ctxt->hostname = (char *) xmlCharStrndup(uri->server + 1, len -2);
  290. } else
  291. ctxt->hostname = xmlMemStrdup(uri->server);
  292. } else
  293. ctxt->hostname = xmlMemStrdup(uri->server);
  294. if (uri->path != NULL)
  295. ctxt->path = xmlMemStrdup(uri->path);
  296. else
  297. ctxt->path = xmlMemStrdup("/");
  298. if (uri->query != NULL)
  299. ctxt->query = xmlMemStrdup(uri->query);
  300. if (uri->port != 0)
  301. ctxt->port = uri->port;
  302. xmlFreeURI(uri);
  303. }
  304. /**
  305. * xmlNanoHTTPScanProxy:
  306. * @URL: The proxy URL used to initialize the proxy context
  307. *
  308. * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
  309. * the protocol host port it indicates.
  310. * Should be like http://myproxy/ or http://myproxy:3128/
  311. * A NULL URL cleans up proxy information.
  312. */
  313. void
  314. xmlNanoHTTPScanProxy(const char *URL) {
  315. xmlURIPtr uri;
  316. if (proxy != NULL) {
  317. xmlFree(proxy);
  318. proxy = NULL;
  319. }
  320. proxyPort = 0;
  321. #ifdef DEBUG_HTTP
  322. if (URL == NULL)
  323. xmlGenericError(xmlGenericErrorContext,
  324. "Removing HTTP proxy info\n");
  325. else
  326. xmlGenericError(xmlGenericErrorContext,
  327. "Using HTTP proxy %s\n", URL);
  328. #endif
  329. if (URL == NULL) return;
  330. uri = xmlParseURIRaw(URL, 1);
  331. if ((uri == NULL) || (uri->scheme == NULL) ||
  332. (strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
  333. __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
  334. if (uri != NULL)
  335. xmlFreeURI(uri);
  336. return;
  337. }
  338. proxy = xmlMemStrdup(uri->server);
  339. if (uri->port != 0)
  340. proxyPort = uri->port;
  341. xmlFreeURI(uri);
  342. }
  343. /**
  344. * xmlNanoHTTPNewCtxt:
  345. * @URL: The URL used to initialize the context
  346. *
  347. * Allocate and initialize a new HTTP context.
  348. *
  349. * Returns an HTTP context or NULL in case of error.
  350. */
  351. static xmlNanoHTTPCtxtPtr
  352. xmlNanoHTTPNewCtxt(const char *URL) {
  353. xmlNanoHTTPCtxtPtr ret;
  354. ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
  355. if (ret == NULL) {
  356. xmlHTTPErrMemory("allocating context");
  357. return(NULL);
  358. }
  359. memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
  360. ret->port = 80;
  361. ret->returnValue = 0;
  362. ret->fd = INVALID_SOCKET;
  363. ret->ContentLength = -1;
  364. xmlNanoHTTPScanURL(ret, URL);
  365. return(ret);
  366. }
  367. /**
  368. * xmlNanoHTTPFreeCtxt:
  369. * @ctxt: an HTTP context
  370. *
  371. * Frees the context after closing the connection.
  372. */
  373. static void
  374. xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
  375. if (ctxt == NULL) return;
  376. if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
  377. if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
  378. if (ctxt->path != NULL) xmlFree(ctxt->path);
  379. if (ctxt->query != NULL) xmlFree(ctxt->query);
  380. if (ctxt->out != NULL) xmlFree(ctxt->out);
  381. if (ctxt->in != NULL) xmlFree(ctxt->in);
  382. if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
  383. if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
  384. if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
  385. if (ctxt->location != NULL) xmlFree(ctxt->location);
  386. if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
  387. #ifdef LIBXML_ZLIB_ENABLED
  388. if (ctxt->strm != NULL) {
  389. inflateEnd(ctxt->strm);
  390. xmlFree(ctxt->strm);
  391. }
  392. #endif
  393. ctxt->state = XML_NANO_HTTP_NONE;
  394. if (ctxt->fd != INVALID_SOCKET) closesocket(ctxt->fd);
  395. ctxt->fd = INVALID_SOCKET;
  396. xmlFree(ctxt);
  397. }
  398. /**
  399. * xmlNanoHTTPSend:
  400. * @ctxt: an HTTP context
  401. *
  402. * Send the input needed to initiate the processing on the server side
  403. * Returns number of bytes sent or -1 on error.
  404. */
  405. static int
  406. xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char *xmt_ptr, int outlen)
  407. {
  408. int total_sent = 0;
  409. #ifdef HAVE_POLL_H
  410. struct pollfd p;
  411. #else
  412. struct timeval tv;
  413. fd_set wfd;
  414. #endif
  415. if ((ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL)) {
  416. while (total_sent < outlen) {
  417. int nsent = send(ctxt->fd, SEND_ARG2_CAST (xmt_ptr + total_sent),
  418. outlen - total_sent, 0);
  419. if (nsent > 0)
  420. total_sent += nsent;
  421. else if ((nsent == -1) &&
  422. #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
  423. (socket_errno() != EAGAIN) &&
  424. #endif
  425. (socket_errno() != EWOULDBLOCK)) {
  426. __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
  427. if (total_sent == 0)
  428. total_sent = -1;
  429. break;
  430. } else {
  431. /*
  432. * No data sent
  433. * Since non-blocking sockets are used, wait for
  434. * socket to be writable or default timeout prior
  435. * to retrying.
  436. */
  437. #ifndef HAVE_POLL_H
  438. #ifndef _WINSOCKAPI_
  439. if (ctxt->fd > FD_SETSIZE)
  440. return -1;
  441. #endif
  442. tv.tv_sec = timeout;
  443. tv.tv_usec = 0;
  444. FD_ZERO(&wfd);
  445. #ifdef _MSC_VER
  446. #pragma warning(push)
  447. #pragma warning(disable: 4018)
  448. #endif
  449. FD_SET(ctxt->fd, &wfd);
  450. #ifdef _MSC_VER
  451. #pragma warning(pop)
  452. #endif
  453. (void) select(ctxt->fd + 1, NULL, &wfd, NULL, &tv);
  454. #else
  455. p.fd = ctxt->fd;
  456. p.events = POLLOUT;
  457. (void) poll(&p, 1, timeout * 1000);
  458. #endif /* !HAVE_POLL_H */
  459. }
  460. }
  461. }
  462. return total_sent;
  463. }
  464. /**
  465. * xmlNanoHTTPRecv:
  466. * @ctxt: an HTTP context
  467. *
  468. * Read information coming from the HTTP connection.
  469. * This is a blocking call (but it blocks in select(), not read()).
  470. *
  471. * Returns the number of byte read or -1 in case of error.
  472. */
  473. static int
  474. xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt)
  475. {
  476. #ifdef HAVE_POLL_H
  477. struct pollfd p;
  478. #else
  479. fd_set rfd;
  480. struct timeval tv;
  481. #endif
  482. while (ctxt->state & XML_NANO_HTTP_READ) {
  483. if (ctxt->in == NULL) {
  484. ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
  485. if (ctxt->in == NULL) {
  486. xmlHTTPErrMemory("allocating input");
  487. ctxt->last = -1;
  488. return (-1);
  489. }
  490. ctxt->inlen = 65000;
  491. ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
  492. }
  493. if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
  494. int delta = ctxt->inrptr - ctxt->in;
  495. int len = ctxt->inptr - ctxt->inrptr;
  496. memmove(ctxt->in, ctxt->inrptr, len);
  497. ctxt->inrptr -= delta;
  498. ctxt->content -= delta;
  499. ctxt->inptr -= delta;
  500. }
  501. if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
  502. int d_inptr = ctxt->inptr - ctxt->in;
  503. int d_content = ctxt->content - ctxt->in;
  504. int d_inrptr = ctxt->inrptr - ctxt->in;
  505. char *tmp_ptr = ctxt->in;
  506. ctxt->inlen *= 2;
  507. ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
  508. if (ctxt->in == NULL) {
  509. xmlHTTPErrMemory("allocating input buffer");
  510. xmlFree(tmp_ptr);
  511. ctxt->last = -1;
  512. return (-1);
  513. }
  514. ctxt->inptr = ctxt->in + d_inptr;
  515. ctxt->content = ctxt->in + d_content;
  516. ctxt->inrptr = ctxt->in + d_inrptr;
  517. }
  518. ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
  519. if (ctxt->last > 0) {
  520. ctxt->inptr += ctxt->last;
  521. return (ctxt->last);
  522. }
  523. if (ctxt->last == 0) {
  524. return (0);
  525. }
  526. if (ctxt->last == -1) {
  527. switch (socket_errno()) {
  528. case EINPROGRESS:
  529. case EWOULDBLOCK:
  530. #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
  531. case EAGAIN:
  532. #endif
  533. break;
  534. case ECONNRESET:
  535. case ESHUTDOWN:
  536. return (0);
  537. default:
  538. __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
  539. return (-1);
  540. }
  541. }
  542. #ifdef HAVE_POLL_H
  543. p.fd = ctxt->fd;
  544. p.events = POLLIN;
  545. if ((poll(&p, 1, timeout * 1000) < 1)
  546. #if defined(EINTR)
  547. && (errno != EINTR)
  548. #endif
  549. )
  550. return (0);
  551. #else /* !HAVE_POLL_H */
  552. #ifndef _WINSOCKAPI_
  553. if (ctxt->fd > FD_SETSIZE)
  554. return 0;
  555. #endif
  556. tv.tv_sec = timeout;
  557. tv.tv_usec = 0;
  558. FD_ZERO(&rfd);
  559. #ifdef _MSC_VER
  560. #pragma warning(push)
  561. #pragma warning(disable: 4018)
  562. #endif
  563. FD_SET(ctxt->fd, &rfd);
  564. #ifdef _MSC_VER
  565. #pragma warning(pop)
  566. #endif
  567. if ((select(ctxt->fd + 1, &rfd, NULL, NULL, &tv) < 1)
  568. #if defined(EINTR)
  569. && (socket_errno() != EINTR)
  570. #endif
  571. )
  572. return (0);
  573. #endif /* !HAVE_POLL_H */
  574. }
  575. return (0);
  576. }
  577. /**
  578. * xmlNanoHTTPReadLine:
  579. * @ctxt: an HTTP context
  580. *
  581. * Read one line in the HTTP server output, usually for extracting
  582. * the HTTP protocol information from the answer header.
  583. *
  584. * Returns a newly allocated string with a copy of the line, or NULL
  585. * which indicate the end of the input.
  586. */
  587. static char *
  588. xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
  589. char buf[4096];
  590. char *bp = buf;
  591. int rc;
  592. while (bp - buf < 4095) {
  593. if (ctxt->inrptr == ctxt->inptr) {
  594. if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
  595. if (bp == buf)
  596. return(NULL);
  597. else
  598. *bp = 0;
  599. return(xmlMemStrdup(buf));
  600. }
  601. else if ( rc == -1 ) {
  602. return ( NULL );
  603. }
  604. }
  605. *bp = *ctxt->inrptr++;
  606. if (*bp == '\n') {
  607. *bp = 0;
  608. return(xmlMemStrdup(buf));
  609. }
  610. if (*bp != '\r')
  611. bp++;
  612. }
  613. buf[4095] = 0;
  614. return(xmlMemStrdup(buf));
  615. }
  616. /**
  617. * xmlNanoHTTPScanAnswer:
  618. * @ctxt: an HTTP context
  619. * @line: an HTTP header line
  620. *
  621. * Try to extract useful information from the server answer.
  622. * We currently parse and process:
  623. * - The HTTP revision/ return code
  624. * - The Content-Type, Mime-Type and charset used
  625. * - The Location for redirect processing.
  626. *
  627. * Returns -1 in case of failure, the file descriptor number otherwise
  628. */
  629. static void
  630. xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
  631. const char *cur = line;
  632. if (line == NULL) return;
  633. if (!strncmp(line, "HTTP/", 5)) {
  634. int version = 0;
  635. int ret = 0;
  636. cur += 5;
  637. while ((*cur >= '0') && (*cur <= '9')) {
  638. version *= 10;
  639. version += *cur - '0';
  640. cur++;
  641. }
  642. if (*cur == '.') {
  643. cur++;
  644. if ((*cur >= '0') && (*cur <= '9')) {
  645. version *= 10;
  646. version += *cur - '0';
  647. cur++;
  648. }
  649. while ((*cur >= '0') && (*cur <= '9'))
  650. cur++;
  651. } else
  652. version *= 10;
  653. if ((*cur != ' ') && (*cur != '\t')) return;
  654. while ((*cur == ' ') || (*cur == '\t')) cur++;
  655. if ((*cur < '0') || (*cur > '9')) return;
  656. while ((*cur >= '0') && (*cur <= '9')) {
  657. ret *= 10;
  658. ret += *cur - '0';
  659. cur++;
  660. }
  661. if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
  662. ctxt->returnValue = ret;
  663. ctxt->version = version;
  664. } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
  665. const xmlChar *charset, *last, *mime;
  666. cur += 13;
  667. while ((*cur == ' ') || (*cur == '\t')) cur++;
  668. if (ctxt->contentType != NULL)
  669. xmlFree(ctxt->contentType);
  670. ctxt->contentType = xmlMemStrdup(cur);
  671. mime = (const xmlChar *) cur;
  672. last = mime;
  673. while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
  674. (*last != ';') && (*last != ','))
  675. last++;
  676. if (ctxt->mimeType != NULL)
  677. xmlFree(ctxt->mimeType);
  678. ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
  679. charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
  680. if (charset != NULL) {
  681. charset += 8;
  682. last = charset;
  683. while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
  684. (*last != ';') && (*last != ','))
  685. last++;
  686. if (ctxt->encoding != NULL)
  687. xmlFree(ctxt->encoding);
  688. ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
  689. }
  690. } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
  691. const xmlChar *charset, *last, *mime;
  692. cur += 12;
  693. if (ctxt->contentType != NULL) return;
  694. while ((*cur == ' ') || (*cur == '\t')) cur++;
  695. ctxt->contentType = xmlMemStrdup(cur);
  696. mime = (const xmlChar *) cur;
  697. last = mime;
  698. while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
  699. (*last != ';') && (*last != ','))
  700. last++;
  701. if (ctxt->mimeType != NULL)
  702. xmlFree(ctxt->mimeType);
  703. ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
  704. charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
  705. if (charset != NULL) {
  706. charset += 8;
  707. last = charset;
  708. while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
  709. (*last != ';') && (*last != ','))
  710. last++;
  711. if (ctxt->encoding != NULL)
  712. xmlFree(ctxt->encoding);
  713. ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
  714. }
  715. } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
  716. cur += 9;
  717. while ((*cur == ' ') || (*cur == '\t')) cur++;
  718. if (ctxt->location != NULL)
  719. xmlFree(ctxt->location);
  720. if (*cur == '/') {
  721. xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
  722. xmlChar *tmp_loc =
  723. xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
  724. ctxt->location =
  725. (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
  726. } else {
  727. ctxt->location = xmlMemStrdup(cur);
  728. }
  729. } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
  730. cur += 17;
  731. while ((*cur == ' ') || (*cur == '\t')) cur++;
  732. if (ctxt->authHeader != NULL)
  733. xmlFree(ctxt->authHeader);
  734. ctxt->authHeader = xmlMemStrdup(cur);
  735. } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
  736. cur += 19;
  737. while ((*cur == ' ') || (*cur == '\t')) cur++;
  738. if (ctxt->authHeader != NULL)
  739. xmlFree(ctxt->authHeader);
  740. ctxt->authHeader = xmlMemStrdup(cur);
  741. #ifdef LIBXML_ZLIB_ENABLED
  742. } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Encoding:", 17) ) {
  743. cur += 17;
  744. while ((*cur == ' ') || (*cur == '\t')) cur++;
  745. if ( !xmlStrncasecmp( BAD_CAST cur, BAD_CAST"gzip", 4) ) {
  746. ctxt->usesGzip = 1;
  747. ctxt->strm = xmlMalloc(sizeof(z_stream));
  748. if (ctxt->strm != NULL) {
  749. ctxt->strm->zalloc = Z_NULL;
  750. ctxt->strm->zfree = Z_NULL;
  751. ctxt->strm->opaque = Z_NULL;
  752. ctxt->strm->avail_in = 0;
  753. ctxt->strm->next_in = Z_NULL;
  754. inflateInit2( ctxt->strm, 31 );
  755. }
  756. }
  757. #endif
  758. } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
  759. cur += 15;
  760. ctxt->ContentLength = strtol( cur, NULL, 10 );
  761. }
  762. }
  763. /**
  764. * xmlNanoHTTPConnectAttempt:
  765. * @addr: a socket address structure
  766. *
  767. * Attempt a connection to the given IP:port endpoint. It forces
  768. * non-blocking semantic on the socket, and allow 60 seconds for
  769. * the host to answer.
  770. *
  771. * Returns -1 in case of failure, the file descriptor number otherwise
  772. */
  773. static SOCKET
  774. xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
  775. {
  776. #ifndef HAVE_POLL_H
  777. fd_set wfd;
  778. #ifdef _WINSOCKAPI_
  779. fd_set xfd;
  780. #endif
  781. struct timeval tv;
  782. #else /* !HAVE_POLL_H */
  783. struct pollfd p;
  784. #endif /* !HAVE_POLL_H */
  785. int status;
  786. int addrlen;
  787. SOCKET s;
  788. #ifdef SUPPORT_IP6
  789. if (addr->sa_family == AF_INET6) {
  790. s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
  791. addrlen = sizeof(struct sockaddr_in6);
  792. } else
  793. #endif
  794. {
  795. s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  796. addrlen = sizeof(struct sockaddr_in);
  797. }
  798. if (s == INVALID_SOCKET) {
  799. #ifdef DEBUG_HTTP
  800. perror("socket");
  801. #endif
  802. __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
  803. return INVALID_SOCKET;
  804. }
  805. #ifdef _WINSOCKAPI_
  806. {
  807. u_long one = 1;
  808. status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
  809. }
  810. #else /* _WINSOCKAPI_ */
  811. #if defined(VMS)
  812. {
  813. int enable = 1;
  814. status = ioctl(s, FIONBIO, &enable);
  815. }
  816. #else /* VMS */
  817. #if defined(__BEOS__) && !defined(__HAIKU__)
  818. {
  819. bool noblock = true;
  820. status =
  821. setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock,
  822. sizeof(noblock));
  823. }
  824. #else /* __BEOS__ */
  825. if ((status = fcntl(s, F_GETFL, 0)) != -1) {
  826. #ifdef O_NONBLOCK
  827. status |= O_NONBLOCK;
  828. #else /* O_NONBLOCK */
  829. #ifdef F_NDELAY
  830. status |= F_NDELAY;
  831. #endif /* F_NDELAY */
  832. #endif /* !O_NONBLOCK */
  833. status = fcntl(s, F_SETFL, status);
  834. }
  835. if (status < 0) {
  836. #ifdef DEBUG_HTTP
  837. perror("nonblocking");
  838. #endif
  839. __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
  840. closesocket(s);
  841. return INVALID_SOCKET;
  842. }
  843. #endif /* !__BEOS__ */
  844. #endif /* !VMS */
  845. #endif /* !_WINSOCKAPI_ */
  846. if (connect(s, addr, addrlen) == -1) {
  847. switch (socket_errno()) {
  848. case EINPROGRESS:
  849. case EWOULDBLOCK:
  850. break;
  851. default:
  852. __xmlIOErr(XML_FROM_HTTP, 0,
  853. "error connecting to HTTP server");
  854. closesocket(s);
  855. return INVALID_SOCKET;
  856. }
  857. }
  858. #ifndef HAVE_POLL_H
  859. tv.tv_sec = timeout;
  860. tv.tv_usec = 0;
  861. #ifdef _MSC_VER
  862. #pragma warning(push)
  863. #pragma warning(disable: 4018)
  864. #endif
  865. #ifndef _WINSOCKAPI_
  866. if (s > FD_SETSIZE)
  867. return INVALID_SOCKET;
  868. #endif
  869. FD_ZERO(&wfd);
  870. FD_SET(s, &wfd);
  871. #ifdef _WINSOCKAPI_
  872. FD_ZERO(&xfd);
  873. FD_SET(s, &xfd);
  874. switch (select(s + 1, NULL, &wfd, &xfd, &tv))
  875. #else
  876. switch (select(s + 1, NULL, &wfd, NULL, &tv))
  877. #endif
  878. #ifdef _MSC_VER
  879. #pragma warning(pop)
  880. #endif
  881. #else /* !HAVE_POLL_H */
  882. p.fd = s;
  883. p.events = POLLOUT;
  884. switch (poll(&p, 1, timeout * 1000))
  885. #endif /* !HAVE_POLL_H */
  886. {
  887. case 0:
  888. /* Time out */
  889. __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
  890. closesocket(s);
  891. return INVALID_SOCKET;
  892. case -1:
  893. /* Ermm.. ?? */
  894. __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
  895. closesocket(s);
  896. return INVALID_SOCKET;
  897. }
  898. #ifndef HAVE_POLL_H
  899. if (FD_ISSET(s, &wfd)
  900. #ifdef _WINSOCKAPI_
  901. || FD_ISSET(s, &xfd)
  902. #endif
  903. )
  904. #else /* !HAVE_POLL_H */
  905. if (p.revents == POLLOUT)
  906. #endif /* !HAVE_POLL_H */
  907. {
  908. XML_SOCKLEN_T len;
  909. len = sizeof(status);
  910. #ifdef SO_ERROR
  911. if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &status, &len) <
  912. 0) {
  913. /* Solaris error code */
  914. __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
  915. closesocket(s);
  916. return INVALID_SOCKET;
  917. }
  918. #endif
  919. if (status) {
  920. __xmlIOErr(XML_FROM_HTTP, 0,
  921. "Error connecting to remote host");
  922. closesocket(s);
  923. errno = status;
  924. return INVALID_SOCKET;
  925. }
  926. } else {
  927. /* pbm */
  928. __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
  929. closesocket(s);
  930. return INVALID_SOCKET;
  931. }
  932. return (s);
  933. }
  934. /**
  935. * xmlNanoHTTPConnectHost:
  936. * @host: the host name
  937. * @port: the port number
  938. *
  939. * Attempt a connection to the given host:port endpoint. It tries
  940. * the multiple IP provided by the DNS if available.
  941. *
  942. * Returns -1 in case of failure, the file descriptor number otherwise
  943. */
  944. static SOCKET
  945. xmlNanoHTTPConnectHost(const char *host, int port)
  946. {
  947. struct sockaddr *addr = NULL;
  948. struct sockaddr_in sockin;
  949. #ifdef SUPPORT_IP6
  950. struct in6_addr ia6;
  951. struct sockaddr_in6 sockin6;
  952. #endif
  953. SOCKET s;
  954. memset (&sockin, 0, sizeof(sockin));
  955. #ifdef SUPPORT_IP6
  956. memset (&sockin6, 0, sizeof(sockin6));
  957. #endif
  958. #if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
  959. if (have_ipv6 ())
  960. {
  961. if (!(_res.options & RES_INIT))
  962. res_init();
  963. _res.options |= RES_USE_INET6;
  964. }
  965. #endif
  966. #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
  967. if (have_ipv6 ())
  968. #endif
  969. #if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
  970. {
  971. int status;
  972. struct addrinfo hints, *res, *result;
  973. result = NULL;
  974. memset (&hints, 0,sizeof(hints));
  975. hints.ai_socktype = SOCK_STREAM;
  976. status = getaddrinfo (host, NULL, &hints, &result);
  977. if (status) {
  978. __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
  979. return INVALID_SOCKET;
  980. }
  981. for (res = result; res; res = res->ai_next) {
  982. if (res->ai_family == AF_INET) {
  983. if ((size_t)res->ai_addrlen > sizeof(sockin)) {
  984. __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
  985. freeaddrinfo (result);
  986. return INVALID_SOCKET;
  987. }
  988. memcpy (&sockin, res->ai_addr, res->ai_addrlen);
  989. sockin.sin_port = htons (port);
  990. addr = (struct sockaddr *)&sockin;
  991. #ifdef SUPPORT_IP6
  992. } else if (have_ipv6 () && (res->ai_family == AF_INET6)) {
  993. if ((size_t)res->ai_addrlen > sizeof(sockin6)) {
  994. __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
  995. freeaddrinfo (result);
  996. return INVALID_SOCKET;
  997. }
  998. memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
  999. sockin6.sin6_port = htons (port);
  1000. addr = (struct sockaddr *)&sockin6;
  1001. #endif
  1002. } else
  1003. continue; /* for */
  1004. s = xmlNanoHTTPConnectAttempt (addr);
  1005. if (s != INVALID_SOCKET) {
  1006. freeaddrinfo (result);
  1007. return (s);
  1008. }
  1009. }
  1010. if (result)
  1011. freeaddrinfo (result);
  1012. }
  1013. #endif
  1014. #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
  1015. else
  1016. #endif
  1017. #if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
  1018. {
  1019. struct hostent *h;
  1020. struct in_addr ia;
  1021. int i;
  1022. h = gethostbyname (GETHOSTBYNAME_ARG_CAST host);
  1023. if (h == NULL) {
  1024. /*
  1025. * Okay, I got fed up by the non-portability of this error message
  1026. * extraction code. it work on Linux, if it work on your platform
  1027. * and one want to enable it, send me the defined(foobar) needed
  1028. */
  1029. #if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(__linux__)
  1030. const char *h_err_txt = "";
  1031. switch (h_errno) {
  1032. case HOST_NOT_FOUND:
  1033. h_err_txt = "Authoritative host not found";
  1034. break;
  1035. case TRY_AGAIN:
  1036. h_err_txt =
  1037. "Non-authoritative host not found or server failure.";
  1038. break;
  1039. case NO_RECOVERY:
  1040. h_err_txt =
  1041. "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
  1042. break;
  1043. #ifdef NO_ADDRESS
  1044. case NO_ADDRESS:
  1045. h_err_txt =
  1046. "Valid name, no data record of requested type.";
  1047. break;
  1048. #endif
  1049. default:
  1050. h_err_txt = "No error text defined.";
  1051. break;
  1052. }
  1053. __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
  1054. #else
  1055. __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
  1056. #endif
  1057. return INVALID_SOCKET;
  1058. }
  1059. for (i = 0; h->h_addr_list[i]; i++) {
  1060. if (h->h_addrtype == AF_INET) {
  1061. /* A records (IPv4) */
  1062. if ((unsigned int) h->h_length > sizeof(ia)) {
  1063. __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
  1064. return INVALID_SOCKET;
  1065. }
  1066. memcpy (&ia, h->h_addr_list[i], h->h_length);
  1067. sockin.sin_family = h->h_addrtype;
  1068. sockin.sin_addr = ia;
  1069. sockin.sin_port = (unsigned short)htons ((unsigned short)port);
  1070. addr = (struct sockaddr *) &sockin;
  1071. #ifdef SUPPORT_IP6
  1072. } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
  1073. /* AAAA records (IPv6) */
  1074. if ((unsigned int) h->h_length > sizeof(ia6)) {
  1075. __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
  1076. return INVALID_SOCKET;
  1077. }
  1078. memcpy (&ia6, h->h_addr_list[i], h->h_length);
  1079. sockin6.sin6_family = h->h_addrtype;
  1080. sockin6.sin6_addr = ia6;
  1081. sockin6.sin6_port = htons (port);
  1082. addr = (struct sockaddr *) &sockin6;
  1083. #endif
  1084. } else
  1085. break; /* for */
  1086. s = xmlNanoHTTPConnectAttempt (addr);
  1087. if (s != INVALID_SOCKET)
  1088. return (s);
  1089. }
  1090. }
  1091. #endif
  1092. #ifdef DEBUG_HTTP
  1093. xmlGenericError(xmlGenericErrorContext,
  1094. "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
  1095. host);
  1096. #endif
  1097. return INVALID_SOCKET;
  1098. }
  1099. /**
  1100. * xmlNanoHTTPOpen:
  1101. * @URL: The URL to load
  1102. * @contentType: if available the Content-Type information will be
  1103. * returned at that location
  1104. *
  1105. * This function try to open a connection to the indicated resource
  1106. * via HTTP GET.
  1107. *
  1108. * Returns NULL in case of failure, otherwise a request handler.
  1109. * The contentType, if provided must be freed by the caller
  1110. */
  1111. void*
  1112. xmlNanoHTTPOpen(const char *URL, char **contentType) {
  1113. if (contentType != NULL) *contentType = NULL;
  1114. return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
  1115. }
  1116. /**
  1117. * xmlNanoHTTPOpenRedir:
  1118. * @URL: The URL to load
  1119. * @contentType: if available the Content-Type information will be
  1120. * returned at that location
  1121. * @redir: if available the redirected URL will be returned
  1122. *
  1123. * This function try to open a connection to the indicated resource
  1124. * via HTTP GET.
  1125. *
  1126. * Returns NULL in case of failure, otherwise a request handler.
  1127. * The contentType, if provided must be freed by the caller
  1128. */
  1129. void*
  1130. xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
  1131. if (contentType != NULL) *contentType = NULL;
  1132. if (redir != NULL) *redir = NULL;
  1133. return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
  1134. }
  1135. /**
  1136. * xmlNanoHTTPRead:
  1137. * @ctx: the HTTP context
  1138. * @dest: a buffer
  1139. * @len: the buffer length
  1140. *
  1141. * This function tries to read @len bytes from the existing HTTP connection
  1142. * and saves them in @dest. This is a blocking call.
  1143. *
  1144. * Returns the number of byte read. 0 is an indication of an end of connection.
  1145. * -1 indicates a parameter error.
  1146. */
  1147. int
  1148. xmlNanoHTTPRead(void *ctx, void *dest, int len) {
  1149. xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
  1150. #ifdef LIBXML_ZLIB_ENABLED
  1151. int bytes_read = 0;
  1152. int orig_avail_in;
  1153. int z_ret;
  1154. #endif
  1155. if (ctx == NULL) return(-1);
  1156. if (dest == NULL) return(-1);
  1157. if (len <= 0) return(0);
  1158. #ifdef LIBXML_ZLIB_ENABLED
  1159. if (ctxt->usesGzip == 1) {
  1160. if (ctxt->strm == NULL) return(0);
  1161. ctxt->strm->next_out = dest;
  1162. ctxt->strm->avail_out = len;
  1163. ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr;
  1164. while (ctxt->strm->avail_out > 0 &&
  1165. (ctxt->strm->avail_in > 0 || xmlNanoHTTPRecv(ctxt) > 0)) {
  1166. orig_avail_in = ctxt->strm->avail_in =
  1167. ctxt->inptr - ctxt->inrptr - bytes_read;
  1168. ctxt->strm->next_in = BAD_CAST (ctxt->inrptr + bytes_read);
  1169. z_ret = inflate(ctxt->strm, Z_NO_FLUSH);
  1170. bytes_read += orig_avail_in - ctxt->strm->avail_in;
  1171. if (z_ret != Z_OK) break;
  1172. }
  1173. ctxt->inrptr += bytes_read;
  1174. return(len - ctxt->strm->avail_out);
  1175. }
  1176. #endif
  1177. while (ctxt->inptr - ctxt->inrptr < len) {
  1178. if (xmlNanoHTTPRecv(ctxt) <= 0) break;
  1179. }
  1180. if (ctxt->inptr - ctxt->inrptr < len)
  1181. len = ctxt->inptr - ctxt->inrptr;
  1182. memcpy(dest, ctxt->inrptr, len);
  1183. ctxt->inrptr += len;
  1184. return(len);
  1185. }
  1186. /**
  1187. * xmlNanoHTTPClose:
  1188. * @ctx: the HTTP context
  1189. *
  1190. * This function closes an HTTP context, it ends up the connection and
  1191. * free all data related to it.
  1192. */
  1193. void
  1194. xmlNanoHTTPClose(void *ctx) {
  1195. xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
  1196. if (ctx == NULL) return;
  1197. xmlNanoHTTPFreeCtxt(ctxt);
  1198. }
  1199. /**
  1200. * xmlNanoHTTPMethodRedir:
  1201. * @URL: The URL to load
  1202. * @method: the HTTP method to use
  1203. * @input: the input string if any
  1204. * @contentType: the Content-Type information IN and OUT
  1205. * @redir: the redirected URL OUT
  1206. * @headers: the extra headers
  1207. * @ilen: input length
  1208. *
  1209. * This function try to open a connection to the indicated resource
  1210. * via HTTP using the given @method, adding the given extra headers
  1211. * and the input buffer for the request content.
  1212. *
  1213. * Returns NULL in case of failure, otherwise a request handler.
  1214. * The contentType, or redir, if provided must be freed by the caller
  1215. */
  1216. void*
  1217. xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
  1218. char **contentType, char **redir,
  1219. const char *headers, int ilen ) {
  1220. xmlNanoHTTPCtxtPtr ctxt;
  1221. char *bp, *p;
  1222. int blen;
  1223. SOCKET ret;
  1224. int nbRedirects = 0;
  1225. char *redirURL = NULL;
  1226. #ifdef DEBUG_HTTP
  1227. int xmt_bytes;
  1228. #endif
  1229. if (URL == NULL) return(NULL);
  1230. if (method == NULL) method = "GET";
  1231. xmlNanoHTTPInit();
  1232. retry:
  1233. if (redirURL == NULL) {
  1234. ctxt = xmlNanoHTTPNewCtxt(URL);
  1235. if (ctxt == NULL)
  1236. return(NULL);
  1237. } else {
  1238. ctxt = xmlNanoHTTPNewCtxt(redirURL);
  1239. if (ctxt == NULL)
  1240. return(NULL);
  1241. ctxt->location = xmlMemStrdup(redirURL);
  1242. }
  1243. if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
  1244. __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
  1245. xmlNanoHTTPFreeCtxt(ctxt);
  1246. if (redirURL != NULL) xmlFree(redirURL);
  1247. return(NULL);
  1248. }
  1249. if (ctxt->hostname == NULL) {
  1250. __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
  1251. "Failed to identify host in URI");
  1252. xmlNanoHTTPFreeCtxt(ctxt);
  1253. if (redirURL != NULL) xmlFree(redirURL);
  1254. return(NULL);
  1255. }
  1256. if (proxy) {
  1257. blen = strlen(ctxt->hostname) * 2 + 16;
  1258. ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
  1259. }
  1260. else {
  1261. blen = strlen(ctxt->hostname);
  1262. ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
  1263. }
  1264. if (ret == INVALID_SOCKET) {
  1265. xmlNanoHTTPFreeCtxt(ctxt);
  1266. if (redirURL != NULL) xmlFree(redirURL);
  1267. return(NULL);
  1268. }
  1269. ctxt->fd = ret;
  1270. if (input == NULL)
  1271. ilen = 0;
  1272. else
  1273. blen += 36;
  1274. if (headers != NULL)
  1275. blen += strlen(headers) + 2;
  1276. if (contentType && *contentType)
  1277. /* reserve for string plus 'Content-Type: \r\n" */
  1278. blen += strlen(*contentType) + 16;
  1279. if (ctxt->query != NULL)
  1280. /* 1 for '?' */
  1281. blen += strlen(ctxt->query) + 1;
  1282. blen += strlen(method) + strlen(ctxt->path) + 24;
  1283. #ifdef LIBXML_ZLIB_ENABLED
  1284. /* reserve for possible 'Accept-Encoding: gzip' string */
  1285. blen += 23;
  1286. #endif
  1287. if (ctxt->port != 80) {
  1288. /* reserve space for ':xxxxx', incl. potential proxy */
  1289. if (proxy)
  1290. blen += 17;
  1291. else
  1292. blen += 11;
  1293. }
  1294. bp = (char*)xmlMallocAtomic(blen);
  1295. if ( bp == NULL ) {
  1296. xmlNanoHTTPFreeCtxt( ctxt );
  1297. xmlHTTPErrMemory("allocating header buffer");
  1298. return ( NULL );
  1299. }
  1300. p = bp;
  1301. if (proxy) {
  1302. if (ctxt->port != 80) {
  1303. p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
  1304. method, ctxt->hostname,
  1305. ctxt->port, ctxt->path );
  1306. }
  1307. else
  1308. p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
  1309. ctxt->hostname, ctxt->path);
  1310. }
  1311. else
  1312. p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
  1313. if (ctxt->query != NULL)
  1314. p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
  1315. if (ctxt->port == 80) {
  1316. p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
  1317. ctxt->hostname);
  1318. } else {
  1319. p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n",
  1320. ctxt->hostname, ctxt->port);
  1321. }
  1322. #ifdef LIBXML_ZLIB_ENABLED
  1323. p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");
  1324. #endif
  1325. if (contentType != NULL && *contentType)
  1326. p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
  1327. if (headers != NULL)
  1328. p += snprintf( p, blen - (p - bp), "%s", headers );
  1329. if (input != NULL)
  1330. snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
  1331. else
  1332. snprintf(p, blen - (p - bp), "\r\n");
  1333. #ifdef DEBUG_HTTP
  1334. xmlGenericError(xmlGenericErrorContext,
  1335. "-> %s%s", proxy? "(Proxy) " : "", bp);
  1336. if ((blen -= strlen(bp)+1) < 0)
  1337. xmlGenericError(xmlGenericErrorContext,
  1338. "ERROR: overflowed buffer by %d bytes\n", -blen);
  1339. #endif
  1340. ctxt->outptr = ctxt->out = bp;
  1341. ctxt->state = XML_NANO_HTTP_WRITE;
  1342. blen = strlen( ctxt->out );
  1343. #ifdef DEBUG_HTTP
  1344. xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
  1345. if ( xmt_bytes != blen )
  1346. xmlGenericError( xmlGenericErrorContext,
  1347. "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
  1348. xmt_bytes, blen,
  1349. "bytes of HTTP headers sent to host",
  1350. ctxt->hostname );
  1351. #else
  1352. xmlNanoHTTPSend(ctxt, ctxt->out, blen );
  1353. #endif
  1354. if ( input != NULL ) {
  1355. #ifdef DEBUG_HTTP
  1356. xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
  1357. if ( xmt_bytes != ilen )
  1358. xmlGenericError( xmlGenericErrorContext,
  1359. "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
  1360. xmt_bytes, ilen,
  1361. "bytes of HTTP content sent to host",
  1362. ctxt->hostname );
  1363. #else
  1364. xmlNanoHTTPSend( ctxt, input, ilen );
  1365. #endif
  1366. }
  1367. ctxt->state = XML_NANO_HTTP_READ;
  1368. while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
  1369. if (*p == 0) {
  1370. ctxt->content = ctxt->inrptr;
  1371. xmlFree(p);
  1372. break;
  1373. }
  1374. xmlNanoHTTPScanAnswer(ctxt, p);
  1375. #ifdef DEBUG_HTTP
  1376. xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
  1377. #endif
  1378. xmlFree(p);
  1379. }
  1380. if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
  1381. (ctxt->returnValue < 400)) {
  1382. #ifdef DEBUG_HTTP
  1383. xmlGenericError(xmlGenericErrorContext,
  1384. "\nRedirect to: %s\n", ctxt->location);
  1385. #endif
  1386. while ( xmlNanoHTTPRecv(ctxt) > 0 )
  1387. ;
  1388. if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
  1389. nbRedirects++;
  1390. if (redirURL != NULL)
  1391. xmlFree(redirURL);
  1392. redirURL = xmlMemStrdup(ctxt->location);
  1393. xmlNanoHTTPFreeCtxt(ctxt);
  1394. goto retry;
  1395. }
  1396. xmlNanoHTTPFreeCtxt(ctxt);
  1397. if (redirURL != NULL) xmlFree(redirURL);
  1398. #ifdef DEBUG_HTTP
  1399. xmlGenericError(xmlGenericErrorContext,
  1400. "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
  1401. #endif
  1402. return(NULL);
  1403. }
  1404. if (contentType != NULL) {
  1405. if (ctxt->contentType != NULL)
  1406. *contentType = xmlMemStrdup(ctxt->contentType);
  1407. else
  1408. *contentType = NULL;
  1409. }
  1410. if ((redir != NULL) && (redirURL != NULL)) {
  1411. *redir = redirURL;
  1412. } else {
  1413. if (redirURL != NULL)
  1414. xmlFree(redirURL);
  1415. if (redir != NULL)
  1416. *redir = NULL;
  1417. }
  1418. #ifdef DEBUG_HTTP
  1419. if (ctxt->contentType != NULL)
  1420. xmlGenericError(xmlGenericErrorContext,
  1421. "\nCode %d, content-type '%s'\n\n",
  1422. ctxt->returnValue, ctxt->contentType);
  1423. else
  1424. xmlGenericError(xmlGenericErrorContext,
  1425. "\nCode %d, no content-type\n\n",
  1426. ctxt->returnValue);
  1427. #endif
  1428. return((void *) ctxt);
  1429. }
  1430. /**
  1431. * xmlNanoHTTPMethod:
  1432. * @URL: The URL to load
  1433. * @method: the HTTP method to use
  1434. * @input: the input string if any
  1435. * @contentType: the Content-Type information IN and OUT
  1436. * @headers: the extra headers
  1437. * @ilen: input length
  1438. *
  1439. * This function try to open a connection to the indicated resource
  1440. * via HTTP using the given @method, adding the given extra headers
  1441. * and the input buffer for the request content.
  1442. *
  1443. * Returns NULL in case of failure, otherwise a request handler.
  1444. * The contentType, if provided must be freed by the caller
  1445. */
  1446. void*
  1447. xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
  1448. char **contentType, const char *headers, int ilen) {
  1449. return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
  1450. NULL, headers, ilen));
  1451. }
  1452. /**
  1453. * xmlNanoHTTPFetch:
  1454. * @URL: The URL to load
  1455. * @filename: the filename where the content should be saved
  1456. * @contentType: if available the Content-Type information will be
  1457. * returned at that location
  1458. *
  1459. * This function try to fetch the indicated resource via HTTP GET
  1460. * and save it's content in the file.
  1461. *
  1462. * Returns -1 in case of failure, 0 in case of success. The contentType,
  1463. * if provided must be freed by the caller
  1464. */
  1465. int
  1466. xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
  1467. void *ctxt = NULL;
  1468. char *buf = NULL;
  1469. int fd;
  1470. int len;
  1471. int ret = 0;
  1472. if (filename == NULL) return(-1);
  1473. ctxt = xmlNanoHTTPOpen(URL, contentType);
  1474. if (ctxt == NULL) return(-1);
  1475. if (!strcmp(filename, "-"))
  1476. fd = 0;
  1477. else {
  1478. fd = open(filename, O_CREAT | O_WRONLY, 00644);
  1479. if (fd < 0) {
  1480. xmlNanoHTTPClose(ctxt);
  1481. if ((contentType != NULL) && (*contentType != NULL)) {
  1482. xmlFree(*contentType);
  1483. *contentType = NULL;
  1484. }
  1485. return(-1);
  1486. }
  1487. }
  1488. xmlNanoHTTPFetchContent( ctxt, &buf, &len );
  1489. if ( len > 0 ) {
  1490. if (write(fd, buf, len) == -1) {
  1491. ret = -1;
  1492. }
  1493. }
  1494. xmlNanoHTTPClose(ctxt);
  1495. close(fd);
  1496. return(ret);
  1497. }
  1498. #ifdef LIBXML_OUTPUT_ENABLED
  1499. /**
  1500. * xmlNanoHTTPSave:
  1501. * @ctxt: the HTTP context
  1502. * @filename: the filename where the content should be saved
  1503. *
  1504. * This function saves the output of the HTTP transaction to a file
  1505. * It closes and free the context at the end
  1506. *
  1507. * Returns -1 in case of failure, 0 in case of success.
  1508. */
  1509. int
  1510. xmlNanoHTTPSave(void *ctxt, const char *filename) {
  1511. char *buf = NULL;
  1512. int fd;
  1513. int len;
  1514. int ret = 0;
  1515. if ((ctxt == NULL) || (filename == NULL)) return(-1);
  1516. if (!strcmp(filename, "-"))
  1517. fd = 0;
  1518. else {
  1519. fd = open(filename, O_CREAT | O_WRONLY, 0666);
  1520. if (fd < 0) {
  1521. xmlNanoHTTPClose(ctxt);
  1522. return(-1);
  1523. }
  1524. }
  1525. xmlNanoHTTPFetchContent( ctxt, &buf, &len );
  1526. if ( len > 0 ) {
  1527. if (write(fd, buf, len) == -1) {
  1528. ret = -1;
  1529. }
  1530. }
  1531. xmlNanoHTTPClose(ctxt);
  1532. close(fd);
  1533. return(ret);
  1534. }
  1535. #endif /* LIBXML_OUTPUT_ENABLED */
  1536. /**
  1537. * xmlNanoHTTPReturnCode:
  1538. * @ctx: the HTTP context
  1539. *
  1540. * Get the latest HTTP return code received
  1541. *
  1542. * Returns the HTTP return code for the request.
  1543. */
  1544. int
  1545. xmlNanoHTTPReturnCode(void *ctx) {
  1546. xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
  1547. if (ctxt == NULL) return(-1);
  1548. return(ctxt->returnValue);
  1549. }
  1550. /**
  1551. * xmlNanoHTTPAuthHeader:
  1552. * @ctx: the HTTP context
  1553. *
  1554. * Get the authentication header of an HTTP context
  1555. *
  1556. * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
  1557. * header.
  1558. */
  1559. const char *
  1560. xmlNanoHTTPAuthHeader(void *ctx) {
  1561. xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
  1562. if (ctxt == NULL) return(NULL);
  1563. return(ctxt->authHeader);
  1564. }
  1565. /**
  1566. * xmlNanoHTTPContentLength:
  1567. * @ctx: the HTTP context
  1568. *
  1569. * Provides the specified content length from the HTTP header.
  1570. *
  1571. * Return the specified content length from the HTTP header. Note that
  1572. * a value of -1 indicates that the content length element was not included in
  1573. * the response header.
  1574. */
  1575. int
  1576. xmlNanoHTTPContentLength( void * ctx ) {
  1577. xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
  1578. return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
  1579. }
  1580. /**
  1581. * xmlNanoHTTPRedir:
  1582. * @ctx: the HTTP context
  1583. *
  1584. * Provides the specified redirection URL if available from the HTTP header.
  1585. *
  1586. * Return the specified redirection URL or NULL if not redirected.
  1587. */
  1588. const char *
  1589. xmlNanoHTTPRedir( void * ctx ) {
  1590. xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
  1591. return ( ( ctxt == NULL ) ? NULL : ctxt->location );
  1592. }
  1593. /**
  1594. * xmlNanoHTTPEncoding:
  1595. * @ctx: the HTTP context
  1596. *
  1597. * Provides the specified encoding if specified in the HTTP headers.
  1598. *
  1599. * Return the specified encoding or NULL if not available
  1600. */
  1601. const char *
  1602. xmlNanoHTTPEncoding( void * ctx ) {
  1603. xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
  1604. return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
  1605. }
  1606. /**
  1607. * xmlNanoHTTPMimeType:
  1608. * @ctx: the HTTP context
  1609. *
  1610. * Provides the specified Mime-Type if specified in the HTTP headers.
  1611. *
  1612. * Return the specified Mime-Type or NULL if not available
  1613. */
  1614. const char *
  1615. xmlNanoHTTPMimeType( void * ctx ) {
  1616. xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
  1617. return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
  1618. }
  1619. /**
  1620. * xmlNanoHTTPFetchContent:
  1621. * @ctx: the HTTP context
  1622. * @ptr: pointer to set to the content buffer.
  1623. * @len: integer pointer to hold the length of the content
  1624. *
  1625. * Check if all the content was read
  1626. *
  1627. * Returns 0 if all the content was read and available, returns
  1628. * -1 if received content length was less than specified or an error
  1629. * occurred.
  1630. */
  1631. static int
  1632. xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
  1633. xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
  1634. int rc = 0;
  1635. int cur_lgth;
  1636. int rcvd_lgth;
  1637. int dummy_int;
  1638. char * dummy_ptr = NULL;
  1639. /* Dummy up return input parameters if not provided */
  1640. if ( len == NULL )
  1641. len = &dummy_int;
  1642. if ( ptr == NULL )
  1643. ptr = &dummy_ptr;
  1644. /* But can't work without the context pointer */
  1645. if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
  1646. *len = 0;
  1647. *ptr = NULL;
  1648. return ( -1 );
  1649. }
  1650. rcvd_lgth = ctxt->inptr - ctxt->content;
  1651. while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
  1652. rcvd_lgth += cur_lgth;
  1653. if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
  1654. break;
  1655. }
  1656. *ptr = ctxt->content;
  1657. *len = rcvd_lgth;
  1658. if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
  1659. rc = -1;
  1660. else if ( rcvd_lgth == 0 )
  1661. rc = -1;
  1662. return ( rc );
  1663. }
  1664. #ifdef STANDALONE
  1665. int main(int argc, char **argv) {
  1666. char *contentType = NULL;
  1667. if (argv[1] != NULL) {
  1668. if (argv[2] != NULL)
  1669. xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
  1670. else
  1671. xmlNanoHTTPFetch(argv[1], "-", &contentType);
  1672. if (contentType != NULL) xmlFree(contentType);
  1673. } else {
  1674. xmlGenericError(xmlGenericErrorContext,
  1675. "%s: minimal HTTP GET implementation\n", argv[0]);
  1676. xmlGenericError(xmlGenericErrorContext,
  1677. "\tusage %s [ URL [ filename ] ]\n", argv[0]);
  1678. }
  1679. xmlNanoHTTPCleanup();
  1680. xmlMemoryDump();
  1681. return(0);
  1682. }
  1683. #endif /* STANDALONE */
  1684. #else /* !LIBXML_HTTP_ENABLED */
  1685. #ifdef STANDALONE
  1686. #include <stdio.h>
  1687. int main(int argc, char **argv) {
  1688. xmlGenericError(xmlGenericErrorContext,
  1689. "%s : HTTP support not compiled in\n", argv[0]);
  1690. return(0);
  1691. }
  1692. #endif /* STANDALONE */
  1693. #endif /* LIBXML_HTTP_ENABLED */
  1694. #define bottom_nanohttp
  1695. #include "elfgcchack.h"