nanohttp.c 46 KB

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