transfer.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. /* -- WIN32 approved -- */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_SYS_STAT_H
  34. #include <sys/stat.h>
  35. #endif
  36. #include <errno.h>
  37. #include "strtoofft.h"
  38. #include "strequal.h"
  39. #ifdef WIN32
  40. #include <time.h>
  41. #include <io.h>
  42. #else
  43. #ifdef HAVE_SYS_SOCKET_H
  44. #include <sys/socket.h>
  45. #endif
  46. #ifdef HAVE_NETINET_IN_H
  47. #include <netinet/in.h>
  48. #endif
  49. #ifdef HAVE_SYS_TIME_H
  50. #include <sys/time.h>
  51. #endif
  52. #ifdef HAVE_UNISTD_H
  53. #include <unistd.h>
  54. #endif
  55. #include <netdb.h>
  56. #ifdef HAVE_ARPA_INET_H
  57. #include <arpa/inet.h>
  58. #endif
  59. #ifdef HAVE_NET_IF_H
  60. #include <net/if.h>
  61. #endif
  62. #ifdef HAVE_SYS_IOCTL_H
  63. #include <sys/ioctl.h>
  64. #endif
  65. #include <signal.h>
  66. #ifdef HAVE_SYS_PARAM_H
  67. #include <sys/param.h>
  68. #endif
  69. #ifdef HAVE_SYS_SELECT_H
  70. #include <sys/select.h>
  71. #endif
  72. #ifndef HAVE_SOCKET
  73. #error "We can't compile without socket() support!"
  74. #endif
  75. #endif
  76. #include "urldata.h"
  77. #include <curl/curl.h>
  78. #include "netrc.h"
  79. #include "content_encoding.h"
  80. #include "hostip.h"
  81. #include "transfer.h"
  82. #include "sendf.h"
  83. #include "speedcheck.h"
  84. #include "progress.h"
  85. #include "http.h"
  86. #include "url.h"
  87. #include "getinfo.h"
  88. #include "sslgen.h"
  89. #include "http_digest.h"
  90. #include "http_ntlm.h"
  91. #include "http_negotiate.h"
  92. #include "share.h"
  93. #include "memory.h"
  94. #include "select.h"
  95. #include "multiif.h"
  96. #include "easyif.h" /* for Curl_convert_to_network prototype */
  97. #define _MPRINTF_REPLACE /* use our functions only */
  98. #include <curl/mprintf.h>
  99. /* The last #include file should be: */
  100. #include "memdebug.h"
  101. #define CURL_TIMEOUT_EXPECT_100 1000 /* counting ms here */
  102. /*
  103. * This function will call the read callback to fill our buffer with data
  104. * to upload.
  105. */
  106. CURLcode Curl_fillreadbuffer(struct connectdata *conn, int bytes, int *nreadp)
  107. {
  108. struct SessionHandle *data = conn->data;
  109. size_t buffersize = (size_t)bytes;
  110. int nread;
  111. if(conn->bits.upload_chunky) {
  112. /* if chunked Transfer-Encoding */
  113. buffersize -= (8 + 2 + 2); /* 32bit hex + CRLF + CRLF */
  114. data->reqdata.upload_fromhere += 10; /* 32bit hex + CRLF */
  115. }
  116. /* this function returns a size_t, so we typecast to int to prevent warnings
  117. with picky compilers */
  118. nread = (int)conn->fread(data->reqdata.upload_fromhere, 1,
  119. buffersize, conn->fread_in);
  120. if(nread == CURL_READFUNC_ABORT) {
  121. failf(data, "operation aborted by callback\n");
  122. return CURLE_ABORTED_BY_CALLBACK;
  123. }
  124. if(!conn->bits.forbidchunk && conn->bits.upload_chunky) {
  125. /* if chunked Transfer-Encoding */
  126. char hexbuffer[11];
  127. int hexlen = snprintf(hexbuffer, sizeof(hexbuffer),
  128. "%x\r\n", nread);
  129. /* move buffer pointer */
  130. data->reqdata.upload_fromhere -= hexlen;
  131. nread += hexlen;
  132. /* copy the prefix to the buffer */
  133. memcpy(data->reqdata.upload_fromhere, hexbuffer, hexlen);
  134. /* always append CRLF to the data */
  135. memcpy(data->reqdata.upload_fromhere + nread, "\r\n", 2);
  136. if((nread - hexlen) == 0) {
  137. /* mark this as done once this chunk is transfered */
  138. data->reqdata.keep.upload_done = TRUE;
  139. }
  140. nread+=2; /* for the added CRLF */
  141. }
  142. *nreadp = nread;
  143. #ifdef CURL_DOES_CONVERSIONS
  144. if(data->set.prefer_ascii) {
  145. CURLcode res;
  146. res = Curl_convert_to_network(data, data->reqdata.upload_fromhere, nread);
  147. /* Curl_convert_to_network calls failf if unsuccessful */
  148. if(res != CURLE_OK) {
  149. return(res);
  150. }
  151. }
  152. #endif /* CURL_DOES_CONVERSIONS */
  153. return CURLE_OK;
  154. }
  155. /*
  156. * checkhttpprefix()
  157. *
  158. * Returns TRUE if member of the list matches prefix of string
  159. */
  160. static bool
  161. checkhttpprefix(struct SessionHandle *data,
  162. const char *s)
  163. {
  164. struct curl_slist *head = data->set.http200aliases;
  165. bool rc = FALSE;
  166. #ifdef CURL_DOES_CONVERSIONS
  167. /* convert from the network encoding using a scratch area */
  168. char *scratch = calloc(1, strlen(s)+1);
  169. if (NULL == scratch) {
  170. failf (data, "Failed to calloc memory for conversion!");
  171. return FALSE; /* can't return CURLE_OUT_OF_MEMORY so return FALSE */
  172. }
  173. strcpy(scratch, s);
  174. if (CURLE_OK != Curl_convert_from_network(data, scratch, strlen(s)+1)) {
  175. /* Curl_convert_from_network calls failf if unsuccessful */
  176. free(scratch);
  177. return FALSE; /* can't return CURLE_foobar so return FALSE */
  178. }
  179. s = scratch;
  180. #endif /* CURL_DOES_CONVERSIONS */
  181. while (head) {
  182. if (checkprefix(head->data, s)) {
  183. rc = TRUE;
  184. break;
  185. }
  186. head = head->next;
  187. }
  188. if ((rc != TRUE) && (checkprefix("HTTP/", s))) {
  189. rc = TRUE;
  190. }
  191. #ifdef CURL_DOES_CONVERSIONS
  192. free(scratch);
  193. #endif /* CURL_DOES_CONVERSIONS */
  194. return rc;
  195. }
  196. /*
  197. * Curl_readrewind() rewinds the read stream. This typically (so far) only
  198. * used for HTTP POST/PUT with multi-pass authentication when a sending was
  199. * denied and a resend is necessary.
  200. */
  201. CURLcode Curl_readrewind(struct connectdata *conn)
  202. {
  203. struct SessionHandle *data = conn->data;
  204. conn->bits.rewindaftersend = FALSE; /* we rewind now */
  205. /* We have sent away data. If not using CURLOPT_POSTFIELDS or
  206. CURLOPT_HTTPPOST, call app to rewind
  207. */
  208. if(data->set.postfields ||
  209. (data->set.httpreq == HTTPREQ_POST_FORM))
  210. ; /* do nothing */
  211. else {
  212. if(data->set.ioctl) {
  213. curlioerr err;
  214. err = (data->set.ioctl) (data, CURLIOCMD_RESTARTREAD,
  215. data->set.ioctl_client);
  216. infof(data, "the ioctl callback returned %d\n", (int)err);
  217. if(err) {
  218. /* FIXME: convert to a human readable error message */
  219. failf(data, "ioctl callback returned error %d\n", (int)err);
  220. return CURLE_SEND_FAIL_REWIND;
  221. }
  222. }
  223. else {
  224. /* If no CURLOPT_READFUNCTION is used, we know that we operate on a
  225. given FILE * stream and we can actually attempt to rewind that
  226. ourself with fseek() */
  227. if(data->set.fread == (curl_read_callback)fread) {
  228. if(-1 != fseek(data->set.in, 0, SEEK_SET))
  229. /* successful rewind */
  230. return CURLE_OK;
  231. }
  232. /* no callback set or failure aboe, makes us fail at once */
  233. failf(data, "necessary data rewind wasn't possible\n");
  234. return CURLE_SEND_FAIL_REWIND;
  235. }
  236. }
  237. return CURLE_OK;
  238. }
  239. static int data_pending(struct connectdata *conn)
  240. {
  241. return Curl_ssl_data_pending(conn, FIRSTSOCKET);
  242. }
  243. #ifndef MIN
  244. #define MIN(a,b) (a < b ? a : b)
  245. #endif
  246. static void read_rewind(struct connectdata *conn,
  247. size_t thismuch)
  248. {
  249. conn->read_pos -= thismuch;
  250. conn->bits.stream_was_rewound = TRUE;
  251. #ifdef CURLDEBUG
  252. {
  253. char buf[512 + 1];
  254. size_t show;
  255. show = MIN(conn->buf_len - conn->read_pos, sizeof(buf)-1);
  256. memcpy(buf, conn->master_buffer + conn->read_pos, show);
  257. buf[show] = '\0';
  258. DEBUGF(infof(conn->data,
  259. "Buffer after stream rewind (read_pos = %d): [%s]",
  260. conn->read_pos, buf));
  261. }
  262. #endif
  263. }
  264. /*
  265. * Curl_readwrite() is the low-level function to be called when data is to
  266. * be read and written to/from the connection.
  267. */
  268. CURLcode Curl_readwrite(struct connectdata *conn,
  269. bool *done)
  270. {
  271. struct SessionHandle *data = conn->data;
  272. struct Curl_transfer_keeper *k = &data->reqdata.keep;
  273. CURLcode result;
  274. ssize_t nread; /* number of bytes read */
  275. int didwhat=0;
  276. curl_socket_t fd_read;
  277. curl_socket_t fd_write;
  278. int select_res;
  279. curl_off_t contentlength;
  280. /* only use the proper socket if the *_HOLD bit is not set simultaneously as
  281. then we are in rate limiting state in that transfer direction */
  282. if((k->keepon & (KEEP_READ|KEEP_READ_HOLD)) == KEEP_READ)
  283. fd_read = conn->sockfd;
  284. else
  285. fd_read = CURL_SOCKET_BAD;
  286. if((k->keepon & (KEEP_WRITE|KEEP_WRITE_HOLD)) == KEEP_WRITE)
  287. fd_write = conn->writesockfd;
  288. else
  289. fd_write = CURL_SOCKET_BAD;
  290. select_res = Curl_select(fd_read, fd_write, 0);
  291. if(select_res == CSELECT_ERR) {
  292. failf(data, "select/poll returned error");
  293. return CURLE_SEND_ERROR;
  294. }
  295. do {
  296. /* We go ahead and do a read if we have a readable socket or if
  297. the stream was rewound (in which case we have data in a
  298. buffer) */
  299. if((k->keepon & KEEP_READ) &&
  300. ((select_res & CSELECT_IN) || conn->bits.stream_was_rewound)) {
  301. /* read */
  302. bool is_empty_data = FALSE;
  303. /* This is where we loop until we have read everything there is to
  304. read or we get a EWOULDBLOCK */
  305. do {
  306. size_t buffersize = data->set.buffer_size?
  307. data->set.buffer_size : BUFSIZE;
  308. size_t bytestoread = buffersize;
  309. int readrc;
  310. if (k->size != -1 && !k->header) {
  311. /* make sure we don't read "too much" if we can help it since we
  312. might be pipelining and then someone else might want to read what
  313. follows! */
  314. curl_off_t totalleft = k->size - k->bytecount;
  315. if(totalleft < bytestoread)
  316. bytestoread = (size_t)totalleft;
  317. }
  318. /* receive data from the network! */
  319. readrc = Curl_read(conn, conn->sockfd, k->buf, bytestoread, &nread);
  320. /* subzero, this would've blocked */
  321. if(0 > readrc)
  322. break; /* get out of loop */
  323. /* get the CURLcode from the int */
  324. result = (CURLcode)readrc;
  325. if(result>0)
  326. return result;
  327. if ((k->bytecount == 0) && (k->writebytecount == 0)) {
  328. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  329. if(k->wait100_after_headers)
  330. /* set time stamp to compare with when waiting for the 100 */
  331. k->start100 = Curl_tvnow();
  332. }
  333. didwhat |= KEEP_READ;
  334. /* indicates data of zero size, i.e. empty file */
  335. is_empty_data = (bool)((nread == 0) && (k->bodywrites == 0));
  336. /* NULL terminate, allowing string ops to be used */
  337. if (0 < nread || is_empty_data) {
  338. k->buf[nread] = 0;
  339. }
  340. else if (0 >= nread) {
  341. /* if we receive 0 or less here, the server closed the connection
  342. and we bail out from this! */
  343. k->keepon &= ~KEEP_READ;
  344. break;
  345. }
  346. /* Default buffer to use when we write the buffer, it may be changed
  347. in the flow below before the actual storing is done. */
  348. k->str = k->buf;
  349. /* Since this is a two-state thing, we check if we are parsing
  350. headers at the moment or not. */
  351. if (k->header) {
  352. /* we are in parse-the-header-mode */
  353. bool stop_reading = FALSE;
  354. /* header line within buffer loop */
  355. do {
  356. size_t hbufp_index;
  357. size_t rest_length;
  358. size_t full_length;
  359. int writetype;
  360. /* str_start is start of line within buf */
  361. k->str_start = k->str;
  362. /* data is in network encoding so use 0x0a instead of '\n' */
  363. k->end_ptr = memchr(k->str_start, 0x0a, nread);
  364. if (!k->end_ptr) {
  365. /* Not a complete header line within buffer, append the data to
  366. the end of the headerbuff. */
  367. if (k->hbuflen + nread >= data->state.headersize) {
  368. /* We enlarge the header buffer as it is too small */
  369. char *newbuff;
  370. size_t newsize=CURLMAX((k->hbuflen+nread)*3/2,
  371. data->state.headersize*2);
  372. hbufp_index = k->hbufp - data->state.headerbuff;
  373. newbuff = (char *)realloc(data->state.headerbuff, newsize);
  374. if(!newbuff) {
  375. failf (data, "Failed to alloc memory for big header!");
  376. return CURLE_OUT_OF_MEMORY;
  377. }
  378. data->state.headersize=newsize;
  379. data->state.headerbuff = newbuff;
  380. k->hbufp = data->state.headerbuff + hbufp_index;
  381. }
  382. memcpy(k->hbufp, k->str, nread);
  383. k->hbufp += nread;
  384. k->hbuflen += nread;
  385. if (!k->headerline && (k->hbuflen>5)) {
  386. /* make a first check that this looks like a HTTP header */
  387. if(!checkhttpprefix(data, data->state.headerbuff)) {
  388. /* this is not the beginning of a HTTP first header line */
  389. k->header = FALSE;
  390. k->badheader = HEADER_ALLBAD;
  391. break;
  392. }
  393. }
  394. break; /* read more and try again */
  395. }
  396. /* decrease the size of the remaining (supposed) header line */
  397. rest_length = (k->end_ptr - k->str)+1;
  398. nread -= (ssize_t)rest_length;
  399. k->str = k->end_ptr + 1; /* move past new line */
  400. full_length = k->str - k->str_start;
  401. /*
  402. * We're about to copy a chunk of data to the end of the
  403. * already received header. We make sure that the full string
  404. * fit in the allocated header buffer, or else we enlarge
  405. * it.
  406. */
  407. if (k->hbuflen + full_length >=
  408. data->state.headersize) {
  409. char *newbuff;
  410. size_t newsize=CURLMAX((k->hbuflen+full_length)*3/2,
  411. data->state.headersize*2);
  412. hbufp_index = k->hbufp - data->state.headerbuff;
  413. newbuff = (char *)realloc(data->state.headerbuff, newsize);
  414. if(!newbuff) {
  415. failf (data, "Failed to alloc memory for big header!");
  416. return CURLE_OUT_OF_MEMORY;
  417. }
  418. data->state.headersize= newsize;
  419. data->state.headerbuff = newbuff;
  420. k->hbufp = data->state.headerbuff + hbufp_index;
  421. }
  422. /* copy to end of line */
  423. memcpy(k->hbufp, k->str_start, full_length);
  424. k->hbufp += full_length;
  425. k->hbuflen += full_length;
  426. *k->hbufp = 0;
  427. k->end_ptr = k->hbufp;
  428. k->p = data->state.headerbuff;
  429. /****
  430. * We now have a FULL header line that p points to
  431. *****/
  432. if(!k->headerline) {
  433. /* the first read header */
  434. if((k->hbuflen>5) &&
  435. !checkhttpprefix(data, data->state.headerbuff)) {
  436. /* this is not the beginning of a HTTP first header line */
  437. k->header = FALSE;
  438. if(nread)
  439. /* since there's more, this is a partial bad header */
  440. k->badheader = HEADER_PARTHEADER;
  441. else {
  442. /* this was all we read so its all a bad header */
  443. k->badheader = HEADER_ALLBAD;
  444. nread = (ssize_t)rest_length;
  445. }
  446. break;
  447. }
  448. }
  449. /* headers are in network encoding so
  450. use 0x0a and 0x0d instead of '\n' and '\r' */
  451. if ((0x0a == *k->p) || (0x0d == *k->p)) {
  452. size_t headerlen;
  453. /* Zero-length header line means end of headers! */
  454. #ifdef CURL_DOES_CONVERSIONS
  455. if (0x0d == *k->p) {
  456. *k->p = '\r'; /* replace with CR in host encoding */
  457. k->p++; /* pass the CR byte */
  458. }
  459. if (0x0a == *k->p) {
  460. *k->p = '\n'; /* replace with LF in host encoding */
  461. k->p++; /* pass the LF byte */
  462. }
  463. #else
  464. if ('\r' == *k->p)
  465. k->p++; /* pass the \r byte */
  466. if ('\n' == *k->p)
  467. k->p++; /* pass the \n byte */
  468. #endif /* CURL_DOES_CONVERSIONS */
  469. if(100 == k->httpcode) {
  470. /*
  471. * We have made a HTTP PUT or POST and this is 1.1-lingo
  472. * that tells us that the server is OK with this and ready
  473. * to receive the data.
  474. * However, we'll get more headers now so we must get
  475. * back into the header-parsing state!
  476. */
  477. k->header = TRUE;
  478. k->headerline = 0; /* restart the header line counter */
  479. /* if we did wait for this do enable write now! */
  480. if (k->write_after_100_header) {
  481. k->write_after_100_header = FALSE;
  482. k->keepon |= KEEP_WRITE;
  483. }
  484. }
  485. else {
  486. k->header = FALSE; /* no more header to parse! */
  487. if((k->size == -1) && !conn->bits.chunk && !conn->bits.close)
  488. /* When connection is not to get closed, but no
  489. Content-Length nor Content-Encoding chunked have been
  490. received, there is no body in this response. We don't set
  491. stop_reading TRUE since that would also prevent necessary
  492. authentication actions to take place. */
  493. conn->bits.no_body = TRUE;
  494. }
  495. if (417 == k->httpcode) {
  496. /*
  497. * we got: "417 Expectation Failed" this means:
  498. * we have made a HTTP call and our Expect Header
  499. * seems to cause a problem => abort the write operations
  500. * (or prevent them from starting).
  501. */
  502. k->write_after_100_header = FALSE;
  503. k->keepon &= ~KEEP_WRITE;
  504. }
  505. #ifndef CURL_DISABLE_HTTP
  506. /*
  507. * When all the headers have been parsed, see if we should give
  508. * up and return an error.
  509. */
  510. if (Curl_http_should_fail(conn)) {
  511. failf (data, "The requested URL returned error: %d",
  512. k->httpcode);
  513. return CURLE_HTTP_RETURNED_ERROR;
  514. }
  515. #endif /* CURL_DISABLE_HTTP */
  516. /* now, only output this if the header AND body are requested:
  517. */
  518. writetype = CLIENTWRITE_HEADER;
  519. if (data->set.include_header)
  520. writetype |= CLIENTWRITE_BODY;
  521. headerlen = k->p - data->state.headerbuff;
  522. result = Curl_client_write(conn, writetype,
  523. data->state.headerbuff,
  524. headerlen);
  525. if(result)
  526. return result;
  527. data->info.header_size += (long)headerlen;
  528. conn->headerbytecount += (long)headerlen;
  529. conn->deductheadercount =
  530. (100 == k->httpcode)?conn->headerbytecount:0;
  531. if (data->reqdata.resume_from &&
  532. (data->set.httpreq==HTTPREQ_GET) &&
  533. (k->httpcode == 416)) {
  534. /* "Requested Range Not Satisfiable" */
  535. stop_reading = TRUE;
  536. }
  537. #ifndef CURL_DISABLE_HTTP
  538. if(!stop_reading) {
  539. /* Curl_http_auth_act() checks what authentication methods
  540. * that are available and decides which one (if any) to
  541. * use. It will set 'newurl' if an auth metod was picked. */
  542. result = Curl_http_auth_act(conn);
  543. if(result)
  544. return result;
  545. if(conn->bits.rewindaftersend) {
  546. /* We rewind after a complete send, so thus we continue
  547. sending now */
  548. infof(data, "Keep sending data to get tossed away!\n");
  549. k->keepon |= KEEP_WRITE;
  550. }
  551. }
  552. #endif /* CURL_DISABLE_HTTP */
  553. if(!k->header) {
  554. /*
  555. * really end-of-headers.
  556. *
  557. * If we requested a "no body", this is a good time to get
  558. * out and return home.
  559. */
  560. if(conn->bits.no_body)
  561. stop_reading = TRUE;
  562. else {
  563. /* If we know the expected size of this document, we set the
  564. maximum download size to the size of the expected
  565. document or else, we won't know when to stop reading!
  566. Note that we set the download maximum even if we read a
  567. "Connection: close" header, to make sure that
  568. "Content-Length: 0" still prevents us from attempting to
  569. read the (missing) response-body.
  570. */
  571. /* According to RFC2616 section 4.4, we MUST ignore
  572. Content-Length: headers if we are now receiving data
  573. using chunked Transfer-Encoding.
  574. */
  575. if(conn->bits.chunk)
  576. k->size=-1;
  577. }
  578. if(-1 != k->size) {
  579. /* We do this operation even if no_body is true, since this
  580. data might be retrieved later with curl_easy_getinfo()
  581. and its CURLINFO_CONTENT_LENGTH_DOWNLOAD option. */
  582. Curl_pgrsSetDownloadSize(data, k->size);
  583. k->maxdownload = k->size;
  584. }
  585. /* If max download size is *zero* (nothing) we already
  586. have nothing and can safely return ok now! */
  587. if(0 == k->maxdownload)
  588. stop_reading = TRUE;
  589. if(stop_reading) {
  590. /* we make sure that this socket isn't read more now */
  591. k->keepon &= ~KEEP_READ;
  592. }
  593. break; /* exit header line loop */
  594. }
  595. /* We continue reading headers, so reset the line-based
  596. header parsing variables hbufp && hbuflen */
  597. k->hbufp = data->state.headerbuff;
  598. k->hbuflen = 0;
  599. continue;
  600. }
  601. /*
  602. * Checks for special headers coming up.
  603. */
  604. if (!k->headerline++) {
  605. /* This is the first header, it MUST be the error code line
  606. or else we consider this to be the body right away! */
  607. int httpversion_major;
  608. int nc;
  609. #ifdef CURL_DOES_CONVERSIONS
  610. #define HEADER1 scratch
  611. #define SCRATCHSIZE 21
  612. CURLcode res;
  613. char scratch[SCRATCHSIZE+1]; /* "HTTP/major.minor 123" */
  614. /* We can't really convert this yet because we
  615. don't know if it's the 1st header line or the body.
  616. So we do a partial conversion into a scratch area,
  617. leaving the data at k->p as-is.
  618. */
  619. strncpy(&scratch[0], k->p, SCRATCHSIZE);
  620. scratch[SCRATCHSIZE] = 0; /* null terminate */
  621. res = Curl_convert_from_network(data,
  622. &scratch[0],
  623. SCRATCHSIZE);
  624. if (CURLE_OK != res) {
  625. /* Curl_convert_from_network calls failf if unsuccessful */
  626. return res;
  627. }
  628. #else
  629. #define HEADER1 k->p /* no conversion needed, just use k->p */
  630. #endif /* CURL_DOES_CONVERSIONS */
  631. nc = sscanf(HEADER1,
  632. " HTTP/%d.%d %3d",
  633. &httpversion_major,
  634. &k->httpversion,
  635. &k->httpcode);
  636. if (nc==3) {
  637. k->httpversion += 10 * httpversion_major;
  638. }
  639. else {
  640. /* this is the real world, not a Nirvana
  641. NCSA 1.5.x returns this crap when asked for HTTP/1.1
  642. */
  643. nc=sscanf(HEADER1, " HTTP %3d", &k->httpcode);
  644. k->httpversion = 10;
  645. /* If user has set option HTTP200ALIASES,
  646. compare header line against list of aliases
  647. */
  648. if (!nc) {
  649. if (checkhttpprefix(data, k->p)) {
  650. nc = 1;
  651. k->httpcode = 200;
  652. k->httpversion =
  653. (data->set.httpversion==CURL_HTTP_VERSION_1_0)? 10 : 11;
  654. }
  655. }
  656. }
  657. if (nc) {
  658. data->info.httpcode = k->httpcode;
  659. data->info.httpversion = k->httpversion;
  660. /*
  661. * This code executes as part of processing the header. As a
  662. * result, it's not totally clear how to interpret the
  663. * response code yet as that depends on what other headers may
  664. * be present. 401 and 407 may be errors, but may be OK
  665. * depending on how authentication is working. Other codes
  666. * are definitely errors, so give up here.
  667. */
  668. if (data->set.http_fail_on_error && (k->httpcode >= 400) &&
  669. ((k->httpcode != 401) || !data->set.userpwd) &&
  670. ((k->httpcode != 407) || !data->set.proxyuserpwd) ) {
  671. if (data->reqdata.resume_from &&
  672. (data->set.httpreq==HTTPREQ_GET) &&
  673. (k->httpcode == 416)) {
  674. /* "Requested Range Not Satisfiable", just proceed and
  675. pretend this is no error */
  676. }
  677. else {
  678. /* serious error, go home! */
  679. failf (data, "The requested URL returned error: %d",
  680. k->httpcode);
  681. return CURLE_HTTP_RETURNED_ERROR;
  682. }
  683. }
  684. if(k->httpversion == 10)
  685. /* Default action for HTTP/1.0 must be to close, unless
  686. we get one of those fancy headers that tell us the
  687. server keeps it open for us! */
  688. conn->bits.close = TRUE;
  689. switch(k->httpcode) {
  690. case 204:
  691. /* (quote from RFC2616, section 10.2.5): The server has
  692. * fulfilled the request but does not need to return an
  693. * entity-body ... The 204 response MUST NOT include a
  694. * message-body, and thus is always terminated by the first
  695. * empty line after the header fields. */
  696. /* FALLTHROUGH */
  697. case 416: /* Requested Range Not Satisfiable, it has the
  698. Content-Length: set as the "real" document but no
  699. actual response is sent. */
  700. case 304:
  701. /* (quote from RFC2616, section 10.3.5): The 304 response
  702. * MUST NOT contain a message-body, and thus is always
  703. * terminated by the first empty line after the header
  704. * fields. */
  705. k->size=0;
  706. k->maxdownload=0;
  707. k->ignorecl = TRUE; /* ignore Content-Length headers */
  708. break;
  709. default:
  710. /* nothing */
  711. break;
  712. }
  713. }
  714. else {
  715. k->header = FALSE; /* this is not a header line */
  716. break;
  717. }
  718. }
  719. #ifdef CURL_DOES_CONVERSIONS
  720. /* convert from the network encoding */
  721. result = Curl_convert_from_network(data, k->p, strlen(k->p));
  722. if (CURLE_OK != result) {
  723. return(result);
  724. }
  725. /* Curl_convert_from_network calls failf if unsuccessful */
  726. #endif /* CURL_DOES_CONVERSIONS */
  727. /* Check for Content-Length: header lines to get size. Ignore
  728. the header completely if we get a 416 response as then we're
  729. resuming a document that we don't get, and this header contains
  730. info about the true size of the document we didn't get now. */
  731. if (!k->ignorecl && !data->set.ignorecl &&
  732. checkprefix("Content-Length:", k->p)) {
  733. contentlength = curlx_strtoofft(k->p+15, NULL, 10);
  734. if (data->set.max_filesize &&
  735. contentlength > data->set.max_filesize) {
  736. failf(data, "Maximum file size exceeded");
  737. return CURLE_FILESIZE_EXCEEDED;
  738. }
  739. if(contentlength >= 0) {
  740. k->size = contentlength;
  741. k->maxdownload = k->size;
  742. }
  743. else {
  744. /* Negative Content-Length is really odd, and we know it
  745. happens for example when older Apache servers send large
  746. files */
  747. conn->bits.close = TRUE;
  748. infof(data, "Negative content-length: %" FORMAT_OFF_T
  749. ", closing after transfer\n", contentlength);
  750. }
  751. }
  752. /* check for Content-Type: header lines to get the mime-type */
  753. else if (checkprefix("Content-Type:", k->p)) {
  754. char *start;
  755. char *end;
  756. size_t len;
  757. /* Find the first non-space letter */
  758. for(start=k->p+13;
  759. *start && ISSPACE(*start);
  760. start++)
  761. ; /* empty loop */
  762. /* data is now in the host encoding so
  763. use '\r' and '\n' instead of 0x0d and 0x0a */
  764. end = strchr(start, '\r');
  765. if(!end)
  766. end = strchr(start, '\n');
  767. if(end) {
  768. /* skip all trailing space letters */
  769. for(; ISSPACE(*end) && (end > start); end--)
  770. ; /* empty loop */
  771. /* get length of the type */
  772. len = end-start+1;
  773. /* allocate memory of a cloned copy */
  774. Curl_safefree(data->info.contenttype);
  775. data->info.contenttype = malloc(len + 1);
  776. if (NULL == data->info.contenttype)
  777. return CURLE_OUT_OF_MEMORY;
  778. /* copy the content-type string */
  779. memcpy(data->info.contenttype, start, len);
  780. data->info.contenttype[len] = 0; /* zero terminate */
  781. }
  782. }
  783. #ifndef CURL_DISABLE_HTTP
  784. else if((k->httpversion == 10) &&
  785. conn->bits.httpproxy &&
  786. Curl_compareheader(k->p,
  787. "Proxy-Connection:", "keep-alive")) {
  788. /*
  789. * When a HTTP/1.0 reply comes when using a proxy, the
  790. * 'Proxy-Connection: keep-alive' line tells us the
  791. * connection will be kept alive for our pleasure.
  792. * Default action for 1.0 is to close.
  793. */
  794. conn->bits.close = FALSE; /* don't close when done */
  795. infof(data, "HTTP/1.0 proxy connection set to keep alive!\n");
  796. }
  797. else if((k->httpversion == 11) &&
  798. conn->bits.httpproxy &&
  799. Curl_compareheader(k->p,
  800. "Proxy-Connection:", "close")) {
  801. /*
  802. * We get a HTTP/1.1 response from a proxy and it says it'll
  803. * close down after this transfer.
  804. */
  805. conn->bits.close = TRUE; /* close when done */
  806. infof(data, "HTTP/1.1 proxy connection set close!\n");
  807. }
  808. else if((k->httpversion == 10) &&
  809. Curl_compareheader(k->p, "Connection:", "keep-alive")) {
  810. /*
  811. * A HTTP/1.0 reply with the 'Connection: keep-alive' line
  812. * tells us the connection will be kept alive for our
  813. * pleasure. Default action for 1.0 is to close.
  814. *
  815. * [RFC2068, section 19.7.1] */
  816. conn->bits.close = FALSE; /* don't close when done */
  817. infof(data, "HTTP/1.0 connection set to keep alive!\n");
  818. }
  819. else if (Curl_compareheader(k->p, "Connection:", "close")) {
  820. /*
  821. * [RFC 2616, section 8.1.2.1]
  822. * "Connection: close" is HTTP/1.1 language and means that
  823. * the connection will close when this request has been
  824. * served.
  825. */
  826. conn->bits.close = TRUE; /* close when done */
  827. }
  828. else if (Curl_compareheader(k->p,
  829. "Transfer-Encoding:", "chunked")) {
  830. /*
  831. * [RFC 2616, section 3.6.1] A 'chunked' transfer encoding
  832. * means that the server will send a series of "chunks". Each
  833. * chunk starts with line with info (including size of the
  834. * coming block) (terminated with CRLF), then a block of data
  835. * with the previously mentioned size. There can be any amount
  836. * of chunks, and a chunk-data set to zero signals the
  837. * end-of-chunks. */
  838. conn->bits.chunk = TRUE; /* chunks coming our way */
  839. /* init our chunky engine */
  840. Curl_httpchunk_init(conn);
  841. }
  842. else if (checkprefix("Trailer:", k->p) ||
  843. checkprefix("Trailers:", k->p)) {
  844. /*
  845. * This test helps Curl_httpchunk_read() to determine to look
  846. * for well formed trailers after the zero chunksize record. In
  847. * this case a CRLF is required after the zero chunksize record
  848. * when no trailers are sent, or after the last trailer record.
  849. *
  850. * It seems both Trailer: and Trailers: occur in the wild.
  851. */
  852. conn->bits.trailerHdrPresent = TRUE;
  853. }
  854. else if (checkprefix("Content-Encoding:", k->p) &&
  855. data->set.encoding) {
  856. /*
  857. * Process Content-Encoding. Look for the values: identity,
  858. * gzip, deflate, compress, x-gzip and x-compress. x-gzip and
  859. * x-compress are the same as gzip and compress. (Sec 3.5 RFC
  860. * 2616). zlib cannot handle compress. However, errors are
  861. * handled further down when the response body is processed
  862. */
  863. char *start;
  864. /* Find the first non-space letter */
  865. for(start=k->p+17;
  866. *start && ISSPACE(*start);
  867. start++)
  868. ; /* empty loop */
  869. /* Record the content-encoding for later use */
  870. if (checkprefix("identity", start))
  871. k->content_encoding = IDENTITY;
  872. else if (checkprefix("deflate", start))
  873. k->content_encoding = DEFLATE;
  874. else if (checkprefix("gzip", start)
  875. || checkprefix("x-gzip", start))
  876. k->content_encoding = GZIP;
  877. else if (checkprefix("compress", start)
  878. || checkprefix("x-compress", start))
  879. k->content_encoding = COMPRESS;
  880. }
  881. else if (checkprefix("Content-Range:", k->p)) {
  882. /* Content-Range: bytes [num]-
  883. Content-Range: bytes: [num]-
  884. Content-Range: [num]-
  885. The second format was added since Sun's webserver
  886. JavaWebServer/1.1.1 obviously sends the header this way!
  887. The third added since some servers use that!
  888. */
  889. char *ptr = k->p + 14;
  890. /* Move forward until first digit */
  891. while(*ptr && !ISDIGIT(*ptr))
  892. ptr++;
  893. k->offset = curlx_strtoofft(ptr, NULL, 10);
  894. if (data->reqdata.resume_from == k->offset)
  895. /* we asked for a resume and we got it */
  896. k->content_range = TRUE;
  897. }
  898. #if !defined(CURL_DISABLE_COOKIES)
  899. else if(data->cookies &&
  900. checkprefix("Set-Cookie:", k->p)) {
  901. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE,
  902. CURL_LOCK_ACCESS_SINGLE);
  903. Curl_cookie_add(data,
  904. data->cookies, TRUE, k->p+11,
  905. /* If there is a custom-set Host: name, use it
  906. here, or else use real peer host name. */
  907. conn->allocptr.cookiehost?
  908. conn->allocptr.cookiehost:conn->host.name,
  909. data->reqdata.path);
  910. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  911. }
  912. #endif
  913. else if(checkprefix("Last-Modified:", k->p) &&
  914. (data->set.timecondition || data->set.get_filetime) ) {
  915. time_t secs=time(NULL);
  916. k->timeofdoc = curl_getdate(k->p+strlen("Last-Modified:"),
  917. &secs);
  918. if(data->set.get_filetime)
  919. data->info.filetime = (long)k->timeofdoc;
  920. }
  921. else if((checkprefix("WWW-Authenticate:", k->p) &&
  922. (401 == k->httpcode)) ||
  923. (checkprefix("Proxy-authenticate:", k->p) &&
  924. (407 == k->httpcode))) {
  925. result = Curl_http_input_auth(conn, k->httpcode, k->p);
  926. if(result)
  927. return result;
  928. }
  929. else if ((k->httpcode >= 300 && k->httpcode < 400) &&
  930. checkprefix("Location:", k->p)) {
  931. if(data->set.http_follow_location) {
  932. /* this is the URL that the server advices us to get instead */
  933. char *ptr;
  934. char *start=k->p;
  935. char backup;
  936. start += 9; /* pass "Location:" */
  937. /* Skip spaces and tabs. We do this to support multiple
  938. white spaces after the "Location:" keyword. */
  939. while(*start && ISSPACE(*start ))
  940. start++;
  941. /* Scan through the string from the end to find the last
  942. non-space. k->end_ptr points to the actual terminating zero
  943. letter, move pointer one letter back and start from
  944. there. This logic strips off trailing whitespace, but keeps
  945. any embedded whitespace. */
  946. ptr = k->end_ptr-1;
  947. while((ptr>=start) && ISSPACE(*ptr))
  948. ptr--;
  949. ptr++;
  950. backup = *ptr; /* store the ending letter */
  951. if(ptr != start) {
  952. *ptr = '\0'; /* zero terminate */
  953. data->reqdata.newurl = strdup(start); /* clone string */
  954. *ptr = backup; /* restore ending letter */
  955. if(!data->reqdata.newurl)
  956. return CURLE_OUT_OF_MEMORY;
  957. }
  958. }
  959. }
  960. #endif /* CURL_DISABLE_HTTP */
  961. /*
  962. * End of header-checks. Write them to the client.
  963. */
  964. writetype = CLIENTWRITE_HEADER;
  965. if (data->set.include_header)
  966. writetype |= CLIENTWRITE_BODY;
  967. if(data->set.verbose)
  968. Curl_debug(data, CURLINFO_HEADER_IN,
  969. k->p, (size_t)k->hbuflen, conn);
  970. result = Curl_client_write(conn, writetype, k->p, k->hbuflen);
  971. if(result)
  972. return result;
  973. data->info.header_size += (long)k->hbuflen;
  974. conn->headerbytecount += (long)k->hbuflen;
  975. /* reset hbufp pointer && hbuflen */
  976. k->hbufp = data->state.headerbuff;
  977. k->hbuflen = 0;
  978. }
  979. while (!stop_reading && *k->str); /* header line within buffer */
  980. if(stop_reading)
  981. /* We've stopped dealing with input, get out of the do-while loop */
  982. break;
  983. /* We might have reached the end of the header part here, but
  984. there might be a non-header part left in the end of the read
  985. buffer. */
  986. } /* end if header mode */
  987. /* This is not an 'else if' since it may be a rest from the header
  988. parsing, where the beginning of the buffer is headers and the end
  989. is non-headers. */
  990. if (k->str && !k->header && (nread > 0 || is_empty_data)) {
  991. if(0 == k->bodywrites && !is_empty_data) {
  992. /* These checks are only made the first time we are about to
  993. write a piece of the body */
  994. if(conn->protocol&PROT_HTTP) {
  995. /* HTTP-only checks */
  996. if (data->reqdata.newurl) {
  997. if(conn->bits.close) {
  998. /* Abort after the headers if "follow Location" is set
  999. and we're set to close anyway. */
  1000. k->keepon &= ~KEEP_READ;
  1001. *done = TRUE;
  1002. return CURLE_OK;
  1003. }
  1004. /* We have a new url to load, but since we want to be able
  1005. to re-use this connection properly, we read the full
  1006. response in "ignore more" */
  1007. k->ignorebody = TRUE;
  1008. infof(data, "Ignoring the response-body\n");
  1009. }
  1010. if (data->reqdata.resume_from && !k->content_range &&
  1011. (data->set.httpreq==HTTPREQ_GET) &&
  1012. !k->ignorebody) {
  1013. /* we wanted to resume a download, although the server doesn't
  1014. * seem to support this and we did this with a GET (if it
  1015. * wasn't a GET we did a POST or PUT resume) */
  1016. failf(data, "HTTP server doesn't seem to support "
  1017. "byte ranges. Cannot resume.");
  1018. return CURLE_HTTP_RANGE_ERROR;
  1019. }
  1020. if(data->set.timecondition && !data->reqdata.range) {
  1021. /* A time condition has been set AND no ranges have been
  1022. requested. This seems to be what chapter 13.3.4 of
  1023. RFC 2616 defines to be the correct action for a
  1024. HTTP/1.1 client */
  1025. if((k->timeofdoc > 0) && (data->set.timevalue > 0)) {
  1026. switch(data->set.timecondition) {
  1027. case CURL_TIMECOND_IFMODSINCE:
  1028. default:
  1029. if(k->timeofdoc < data->set.timevalue) {
  1030. infof(data,
  1031. "The requested document is not new enough\n");
  1032. *done = TRUE;
  1033. return CURLE_OK;
  1034. }
  1035. break;
  1036. case CURL_TIMECOND_IFUNMODSINCE:
  1037. if(k->timeofdoc > data->set.timevalue) {
  1038. infof(data,
  1039. "The requested document is not old enough\n");
  1040. *done = TRUE;
  1041. return CURLE_OK;
  1042. }
  1043. break;
  1044. } /* switch */
  1045. } /* two valid time strings */
  1046. } /* we have a time condition */
  1047. } /* this is HTTP */
  1048. } /* this is the first time we write a body part */
  1049. k->bodywrites++;
  1050. /* pass data to the debug function before it gets "dechunked" */
  1051. if(data->set.verbose) {
  1052. if(k->badheader) {
  1053. Curl_debug(data, CURLINFO_DATA_IN, data->state.headerbuff,
  1054. (size_t)k->hbuflen, conn);
  1055. if(k->badheader == HEADER_PARTHEADER)
  1056. Curl_debug(data, CURLINFO_DATA_IN,
  1057. k->str, (size_t)nread, conn);
  1058. }
  1059. else
  1060. Curl_debug(data, CURLINFO_DATA_IN,
  1061. k->str, (size_t)nread, conn);
  1062. }
  1063. #ifndef CURL_DISABLE_HTTP
  1064. if(conn->bits.chunk) {
  1065. /*
  1066. * Bless me father for I have sinned. Here comes a chunked
  1067. * transfer flying and we need to decode this properly. While
  1068. * the name says read, this function both reads and writes away
  1069. * the data. The returned 'nread' holds the number of actual
  1070. * data it wrote to the client. */
  1071. CHUNKcode res =
  1072. Curl_httpchunk_read(conn, k->str, nread, &nread);
  1073. if(CHUNKE_OK < res) {
  1074. if(CHUNKE_WRITE_ERROR == res) {
  1075. failf(data, "Failed writing data");
  1076. return CURLE_WRITE_ERROR;
  1077. }
  1078. failf(data, "Received problem %d in the chunky parser", res);
  1079. return CURLE_RECV_ERROR;
  1080. }
  1081. else if(CHUNKE_STOP == res) {
  1082. /* we're done reading chunks! */
  1083. k->keepon &= ~KEEP_READ; /* read no more */
  1084. /* There are now possibly N number of bytes at the end of the
  1085. str buffer that weren't written to the client, but we don't
  1086. care about them right now. */
  1087. }
  1088. /* If it returned OK, we just keep going */
  1089. }
  1090. #endif /* CURL_DISABLE_HTTP */
  1091. if((-1 != k->maxdownload) &&
  1092. (k->bytecount + nread >= k->maxdownload)) {
  1093. /* The 'excess' amount below can't be more than BUFSIZE which
  1094. always will fit in a size_t */
  1095. size_t excess = k->bytecount + nread - k->maxdownload;
  1096. if (excess > 0 && !k->ignorebody) {
  1097. infof(data,
  1098. "Rewinding stream by : %d"
  1099. " bytes on url %s (size = %" FORMAT_OFF_T
  1100. ", maxdownload = %" FORMAT_OFF_T
  1101. ", bytecount = %" FORMAT_OFF_T ", nread = %d)\n",
  1102. excess, conn->data->reqdata.path,
  1103. k->size, k->maxdownload, k->bytecount, nread);
  1104. read_rewind(conn, excess);
  1105. }
  1106. nread = (ssize_t) (k->maxdownload - k->bytecount);
  1107. if(nread < 0 ) /* this should be unusual */
  1108. nread = 0;
  1109. k->keepon &= ~KEEP_READ; /* we're done reading */
  1110. }
  1111. k->bytecount += nread;
  1112. Curl_pgrsSetDownloadCounter(data, k->bytecount);
  1113. if(!conn->bits.chunk && (nread || k->badheader || is_empty_data)) {
  1114. /* If this is chunky transfer, it was already written */
  1115. if(k->badheader && !k->ignorebody) {
  1116. /* we parsed a piece of data wrongly assuming it was a header
  1117. and now we output it as body instead */
  1118. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  1119. data->state.headerbuff,
  1120. k->hbuflen);
  1121. if(result)
  1122. return result;
  1123. }
  1124. if(k->badheader < HEADER_ALLBAD) {
  1125. /* This switch handles various content encodings. If there's an
  1126. error here, be sure to check over the almost identical code
  1127. in http_chunks.c.
  1128. Make sure that ALL_CONTENT_ENCODINGS contains all the
  1129. encodings handled here. */
  1130. #ifdef HAVE_LIBZ
  1131. switch (k->content_encoding) {
  1132. case IDENTITY:
  1133. #endif
  1134. /* This is the default when the server sends no
  1135. Content-Encoding header. See Curl_readwrite_init; the
  1136. memset() call initializes k->content_encoding to zero. */
  1137. if(!k->ignorebody)
  1138. result = Curl_client_write(conn, CLIENTWRITE_BODY, k->str,
  1139. nread);
  1140. #ifdef HAVE_LIBZ
  1141. break;
  1142. case DEFLATE:
  1143. /* Assume CLIENTWRITE_BODY; headers are not encoded. */
  1144. if(!k->ignorebody)
  1145. result = Curl_unencode_deflate_write(conn, k, nread);
  1146. break;
  1147. case GZIP:
  1148. /* Assume CLIENTWRITE_BODY; headers are not encoded. */
  1149. if(!k->ignorebody)
  1150. result = Curl_unencode_gzip_write(conn, k, nread);
  1151. break;
  1152. case COMPRESS:
  1153. default:
  1154. failf (data, "Unrecognized content encoding type. "
  1155. "libcurl understands `identity', `deflate' and `gzip' "
  1156. "content encodings.");
  1157. result = CURLE_BAD_CONTENT_ENCODING;
  1158. break;
  1159. }
  1160. #endif
  1161. }
  1162. k->badheader = HEADER_NORMAL; /* taken care of now */
  1163. if(result)
  1164. return result;
  1165. }
  1166. } /* if (! header and data to read ) */
  1167. if (is_empty_data) {
  1168. /* if we received nothing, the server closed the connection and we
  1169. are done */
  1170. k->keepon &= ~KEEP_READ;
  1171. }
  1172. } while(data_pending(conn));
  1173. } /* if( read from socket ) */
  1174. /* If we still have writing to do, we check if we have a writable
  1175. socket. */
  1176. if((k->keepon & KEEP_WRITE) && (select_res & CSELECT_OUT)) {
  1177. /* write */
  1178. int i, si;
  1179. ssize_t bytes_written;
  1180. bool writedone=TRUE;
  1181. if ((k->bytecount == 0) && (k->writebytecount == 0))
  1182. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  1183. didwhat |= KEEP_WRITE;
  1184. /*
  1185. * We loop here to do the READ and SEND loop until we run out of
  1186. * data to send or until we get EWOULDBLOCK back
  1187. */
  1188. do {
  1189. /* only read more data if there's no upload data already
  1190. present in the upload buffer */
  1191. if(0 == data->reqdata.upload_present) {
  1192. /* init the "upload from here" pointer */
  1193. data->reqdata.upload_fromhere = k->uploadbuf;
  1194. if(!k->upload_done) {
  1195. /* HTTP pollution, this should be written nicer to become more
  1196. protocol agnostic. */
  1197. int fillcount;
  1198. if(k->wait100_after_headers &&
  1199. (data->reqdata.proto.http->sending == HTTPSEND_BODY)) {
  1200. /* If this call is to send body data, we must take some action:
  1201. We have sent off the full HTTP 1.1 request, and we shall now
  1202. go into the Expect: 100 state and await such a header */
  1203. k->wait100_after_headers = FALSE; /* headers sent */
  1204. k->write_after_100_header = TRUE; /* wait for the header */
  1205. k->keepon &= ~KEEP_WRITE; /* disable writing */
  1206. k->start100 = Curl_tvnow(); /* timeout count starts now */
  1207. didwhat &= ~KEEP_WRITE; /* we didn't write anything actually */
  1208. break;
  1209. }
  1210. result = Curl_fillreadbuffer(conn, BUFSIZE, &fillcount);
  1211. if(result)
  1212. return result;
  1213. nread = (ssize_t)fillcount;
  1214. }
  1215. else
  1216. nread = 0; /* we're done uploading/reading */
  1217. /* the signed int typecase of nread of for systems that has
  1218. unsigned size_t */
  1219. if (nread<=0) {
  1220. /* done */
  1221. k->keepon &= ~KEEP_WRITE; /* we're done writing */
  1222. writedone = TRUE;
  1223. if(conn->bits.rewindaftersend) {
  1224. result = Curl_readrewind(conn);
  1225. if(result)
  1226. return result;
  1227. }
  1228. break;
  1229. }
  1230. /* store number of bytes available for upload */
  1231. data->reqdata.upload_present = nread;
  1232. /* convert LF to CRLF if so asked */
  1233. #ifdef CURL_DO_LINEEND_CONV
  1234. /* always convert if we're FTPing in ASCII mode */
  1235. if ((data->set.crlf) || (data->set.prefer_ascii)) {
  1236. #else
  1237. if (data->set.crlf) {
  1238. #endif /* CURL_DO_LINEEND_CONV */
  1239. if(data->state.scratch == NULL)
  1240. data->state.scratch = malloc(2*BUFSIZE);
  1241. if(data->state.scratch == NULL) {
  1242. failf (data, "Failed to alloc scratch buffer!");
  1243. return CURLE_OUT_OF_MEMORY;
  1244. }
  1245. /*
  1246. * ASCII/EBCDIC Note: This is presumably a text (not binary)
  1247. * transfer so the data should already be in ASCII.
  1248. * That means the hex values for ASCII CR (0x0d) & LF (0x0a)
  1249. * must be used instead of the escape sequences \r & \n.
  1250. */
  1251. for(i = 0, si = 0; i < nread; i++, si++) {
  1252. if (data->reqdata.upload_fromhere[i] == 0x0a) {
  1253. data->state.scratch[si++] = 0x0d;
  1254. data->state.scratch[si] = 0x0a;
  1255. if (!data->set.crlf) {
  1256. /* we're here only because FTP is in ASCII mode...
  1257. bump infilesize for the LF we just added */
  1258. data->set.infilesize++;
  1259. }
  1260. }
  1261. else
  1262. data->state.scratch[si] = data->reqdata.upload_fromhere[i];
  1263. }
  1264. if(si != nread) {
  1265. /* only perform the special operation if we really did replace
  1266. anything */
  1267. nread = si;
  1268. /* upload from the new (replaced) buffer instead */
  1269. data->reqdata.upload_fromhere = data->state.scratch;
  1270. /* set the new amount too */
  1271. data->reqdata.upload_present = nread;
  1272. }
  1273. }
  1274. }
  1275. else {
  1276. /* We have a partial buffer left from a previous "round". Use
  1277. that instead of reading more data */
  1278. }
  1279. /* write to socket (send away data) */
  1280. result = Curl_write(conn,
  1281. conn->writesockfd, /* socket to send to */
  1282. data->reqdata.upload_fromhere, /* buffer pointer */
  1283. data->reqdata.upload_present, /* buffer size */
  1284. &bytes_written); /* actually send away */
  1285. if(result)
  1286. return result;
  1287. if(data->set.verbose)
  1288. /* show the data before we change the pointer upload_fromhere */
  1289. Curl_debug(data, CURLINFO_DATA_OUT, data->reqdata.upload_fromhere,
  1290. (size_t)bytes_written, conn);
  1291. if(data->reqdata.upload_present != bytes_written) {
  1292. /* we only wrote a part of the buffer (if anything), deal with it! */
  1293. /* store the amount of bytes left in the buffer to write */
  1294. data->reqdata.upload_present -= bytes_written;
  1295. /* advance the pointer where to find the buffer when the next send
  1296. is to happen */
  1297. data->reqdata.upload_fromhere += bytes_written;
  1298. writedone = TRUE; /* we are done, stop the loop */
  1299. }
  1300. else {
  1301. /* we've uploaded that buffer now */
  1302. data->reqdata.upload_fromhere = k->uploadbuf;
  1303. data->reqdata.upload_present = 0; /* no more bytes left */
  1304. if(k->upload_done) {
  1305. /* switch off writing, we're done! */
  1306. k->keepon &= ~KEEP_WRITE; /* we're done writing */
  1307. writedone = TRUE;
  1308. }
  1309. }
  1310. k->writebytecount += bytes_written;
  1311. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  1312. } while(!writedone); /* loop until we're done writing! */
  1313. }
  1314. } while(0); /* just to break out from! */
  1315. k->now = Curl_tvnow();
  1316. if(didwhat) {
  1317. /* Update read/write counters */
  1318. if(k->bytecountp)
  1319. *k->bytecountp = k->bytecount; /* read count */
  1320. if(k->writebytecountp)
  1321. *k->writebytecountp = k->writebytecount; /* write count */
  1322. }
  1323. else {
  1324. /* no read no write, this is a timeout? */
  1325. if (k->write_after_100_header) {
  1326. /* This should allow some time for the header to arrive, but only a
  1327. very short time as otherwise it'll be too much wasted times too
  1328. often. */
  1329. /* Quoting RFC2616, section "8.2.3 Use of the 100 (Continue) Status":
  1330. Therefore, when a client sends this header field to an origin server
  1331. (possibly via a proxy) from which it has never seen a 100 (Continue)
  1332. status, the client SHOULD NOT wait for an indefinite period before
  1333. sending the request body.
  1334. */
  1335. long ms = Curl_tvdiff(k->now, k->start100);
  1336. if(ms > CURL_TIMEOUT_EXPECT_100) {
  1337. /* we've waited long enough, continue anyway */
  1338. k->write_after_100_header = FALSE;
  1339. k->keepon |= KEEP_WRITE;
  1340. }
  1341. }
  1342. }
  1343. if(Curl_pgrsUpdate(conn))
  1344. result = CURLE_ABORTED_BY_CALLBACK;
  1345. else
  1346. result = Curl_speedcheck(data, k->now);
  1347. if (result)
  1348. return result;
  1349. if (data->set.timeout &&
  1350. ((Curl_tvdiff(k->now, k->start)/1000) >= data->set.timeout)) {
  1351. if (k->size != -1) {
  1352. failf(data, "Operation timed out after %d seconds with %"
  1353. FORMAT_OFF_T " out of %" FORMAT_OFF_T " bytes received",
  1354. data->set.timeout, k->bytecount, k->size);
  1355. } else {
  1356. failf(data, "Operation timed out after %d seconds with %"
  1357. FORMAT_OFF_T " bytes received",
  1358. data->set.timeout, k->bytecount);
  1359. }
  1360. return CURLE_OPERATION_TIMEOUTED;
  1361. }
  1362. if(!k->keepon) {
  1363. /*
  1364. * The transfer has been performed. Just make some general checks before
  1365. * returning.
  1366. */
  1367. if(!(conn->bits.no_body) && (k->size != -1) &&
  1368. (k->bytecount != k->size) &&
  1369. #ifdef CURL_DO_LINEEND_CONV
  1370. /* Most FTP servers don't adjust their file SIZE response for CRLFs,
  1371. so we'll check to see if the discrepancy can be explained
  1372. by the number of CRLFs we've changed to LFs.
  1373. */
  1374. (k->bytecount != (k->size + data->state.crlf_conversions)) &&
  1375. #endif /* CURL_DO_LINEEND_CONV */
  1376. !data->reqdata.newurl) {
  1377. failf(data, "transfer closed with %" FORMAT_OFF_T
  1378. " bytes remaining to read",
  1379. k->size - k->bytecount);
  1380. return CURLE_PARTIAL_FILE;
  1381. }
  1382. else if(!(conn->bits.no_body) &&
  1383. conn->bits.chunk &&
  1384. (data->reqdata.proto.http->chunk.state != CHUNK_STOP)) {
  1385. /*
  1386. * In chunked mode, return an error if the connection is closed prior to
  1387. * the empty (terminiating) chunk is read.
  1388. *
  1389. * The condition above used to check for
  1390. * conn->proto.http->chunk.datasize != 0 which is true after reading
  1391. * *any* chunk, not just the empty chunk.
  1392. *
  1393. */
  1394. failf(data, "transfer closed with outstanding read data remaining");
  1395. return CURLE_PARTIAL_FILE;
  1396. }
  1397. if(Curl_pgrsUpdate(conn))
  1398. return CURLE_ABORTED_BY_CALLBACK;
  1399. }
  1400. /* Now update the "done" boolean we return */
  1401. *done = (bool)(0 == (k->keepon&(KEEP_READ|KEEP_WRITE)));
  1402. return CURLE_OK;
  1403. }
  1404. /*
  1405. * Curl_readwrite_init() inits the readwrite session. This is inited each time for a
  1406. * transfer, sometimes multiple times on the same SessionHandle
  1407. */
  1408. CURLcode Curl_readwrite_init(struct connectdata *conn)
  1409. {
  1410. struct SessionHandle *data = conn->data;
  1411. struct Curl_transfer_keeper *k = &data->reqdata.keep;
  1412. /* NB: the content encoding software depends on this initialization of
  1413. Curl_transfer_keeper.*/
  1414. memset(k, 0, sizeof(struct Curl_transfer_keeper));
  1415. k->start = Curl_tvnow(); /* start time */
  1416. k->now = k->start; /* current time is now */
  1417. k->header = TRUE; /* assume header */
  1418. k->httpversion = -1; /* unknown at this point */
  1419. k->size = data->reqdata.size;
  1420. k->maxdownload = data->reqdata.maxdownload;
  1421. k->bytecountp = data->reqdata.bytecountp;
  1422. k->writebytecountp = data->reqdata.writebytecountp;
  1423. k->bytecount = 0;
  1424. k->buf = data->state.buffer;
  1425. k->uploadbuf = data->state.uploadbuffer;
  1426. k->maxfd = (conn->sockfd>conn->writesockfd?
  1427. conn->sockfd:conn->writesockfd)+1;
  1428. k->hbufp = data->state.headerbuff;
  1429. k->ignorebody=FALSE;
  1430. Curl_pgrsTime(data, TIMER_PRETRANSFER);
  1431. Curl_speedinit(data);
  1432. Curl_pgrsSetUploadCounter(data, 0);
  1433. Curl_pgrsSetDownloadCounter(data, 0);
  1434. if (!conn->bits.getheader) {
  1435. k->header = FALSE;
  1436. if(k->size > 0)
  1437. Curl_pgrsSetDownloadSize(data, k->size);
  1438. }
  1439. /* we want header and/or body, if neither then don't do this! */
  1440. if(conn->bits.getheader || !conn->bits.no_body) {
  1441. if(conn->sockfd != CURL_SOCKET_BAD) {
  1442. k->keepon |= KEEP_READ;
  1443. }
  1444. if(conn->writesockfd != CURL_SOCKET_BAD) {
  1445. /* HTTP 1.1 magic:
  1446. Even if we require a 100-return code before uploading data, we might
  1447. need to write data before that since the REQUEST may not have been
  1448. finished sent off just yet.
  1449. Thus, we must check if the request has been sent before we set the
  1450. state info where we wait for the 100-return code
  1451. */
  1452. if (data->state.expect100header &&
  1453. (data->reqdata.proto.http->sending == HTTPSEND_BODY)) {
  1454. /* wait with write until we either got 100-continue or a timeout */
  1455. k->write_after_100_header = TRUE;
  1456. k->start100 = k->start;
  1457. }
  1458. else {
  1459. if(data->state.expect100header)
  1460. /* when we've sent off the rest of the headers, we must await a
  1461. 100-continue */
  1462. k->wait100_after_headers = TRUE;
  1463. k->keepon |= KEEP_WRITE;
  1464. }
  1465. }
  1466. }
  1467. return CURLE_OK;
  1468. }
  1469. /*
  1470. * Curl_single_getsock() gets called by the multi interface code when the app
  1471. * has requested to get the sockets for the current connection. This function
  1472. * will then be called once for every connection that the multi interface
  1473. * keeps track of. This function will only be called for connections that are
  1474. * in the proper state to have this information available.
  1475. */
  1476. int Curl_single_getsock(struct connectdata *conn,
  1477. curl_socket_t *sock, /* points to numsocks number
  1478. of sockets */
  1479. int numsocks)
  1480. {
  1481. struct SessionHandle *data = conn->data;
  1482. int bitmap = GETSOCK_BLANK;
  1483. int index = 0;
  1484. if(numsocks < 2)
  1485. /* simple check but we might need two slots */
  1486. return GETSOCK_BLANK;
  1487. if(data->reqdata.keep.keepon & KEEP_READ) {
  1488. bitmap |= GETSOCK_READSOCK(index);
  1489. sock[index] = conn->sockfd;
  1490. }
  1491. if(data->reqdata.keep.keepon & KEEP_WRITE) {
  1492. if((conn->sockfd != conn->writesockfd) ||
  1493. !(data->reqdata.keep.keepon & KEEP_READ)) {
  1494. /* only if they are not the same socket or we didn't have a readable
  1495. one, we increase index */
  1496. if(data->reqdata.keep.keepon & KEEP_READ)
  1497. index++; /* increase index if we need two entries */
  1498. sock[index] = conn->writesockfd;
  1499. }
  1500. bitmap |= GETSOCK_WRITESOCK(index);
  1501. }
  1502. return bitmap;
  1503. }
  1504. /*
  1505. * Transfer()
  1506. *
  1507. * This function is what performs the actual transfer. It is capable of
  1508. * doing both ways simultaneously.
  1509. * The transfer must already have been setup by a call to Curl_setup_transfer().
  1510. *
  1511. * Note that headers are created in a preallocated buffer of a default size.
  1512. * That buffer can be enlarged on demand, but it is never shrunken again.
  1513. *
  1514. * Parts of this function was once written by the friendly Mark Butler
  1515. * <butlerm@xmission.com>.
  1516. */
  1517. static CURLcode
  1518. Transfer(struct connectdata *conn)
  1519. {
  1520. CURLcode result;
  1521. struct SessionHandle *data = conn->data;
  1522. struct Curl_transfer_keeper *k = &data->reqdata.keep;
  1523. bool done=FALSE;
  1524. if(!(conn->protocol & PROT_FILE))
  1525. /* Only do this if we are not transferring FILE:, since the file: treatment
  1526. is different*/
  1527. Curl_readwrite_init(conn);
  1528. if((conn->sockfd == CURL_SOCKET_BAD) &&
  1529. (conn->writesockfd == CURL_SOCKET_BAD))
  1530. /* nothing to read, nothing to write, we're already OK! */
  1531. return CURLE_OK;
  1532. /* we want header and/or body, if neither then don't do this! */
  1533. if(!conn->bits.getheader && conn->bits.no_body)
  1534. return CURLE_OK;
  1535. while (!done) {
  1536. curl_socket_t fd_read;
  1537. curl_socket_t fd_write;
  1538. /* limit-rate logic: if speed exceeds threshold, then do not include fd in
  1539. select set. The current speed is recalculated in each Curl_readwrite()
  1540. call */
  1541. if ((k->keepon & KEEP_WRITE) &&
  1542. (!data->set.max_send_speed ||
  1543. (data->progress.ulspeed < data->set.max_send_speed) )) {
  1544. fd_write = conn->writesockfd;
  1545. k->keepon &= ~KEEP_WRITE_HOLD;
  1546. }
  1547. else {
  1548. fd_write = CURL_SOCKET_BAD;
  1549. if(k->keepon & KEEP_WRITE)
  1550. k->keepon |= KEEP_WRITE_HOLD; /* hold it */
  1551. }
  1552. if ((k->keepon & KEEP_READ) &&
  1553. (!data->set.max_recv_speed ||
  1554. (data->progress.dlspeed < data->set.max_recv_speed)) ) {
  1555. fd_read = conn->sockfd;
  1556. k->keepon &= ~KEEP_READ_HOLD;
  1557. }
  1558. else {
  1559. fd_read = CURL_SOCKET_BAD;
  1560. if(k->keepon & KEEP_READ)
  1561. k->keepon |= KEEP_READ_HOLD; /* hold it */
  1562. }
  1563. /* The *_HOLD logic is necessary since even though there might be no
  1564. traffic during the select interval, we still call Curl_readwrite() for
  1565. the timeout case and if we limit transfer speed we must make sure that
  1566. this function doesn't transfer anything while in HOLD status. */
  1567. switch (Curl_select(fd_read, fd_write, 1000)) {
  1568. case -1: /* select() error, stop reading */
  1569. #ifdef EINTR
  1570. /* The EINTR is not serious, and it seems you might get this more
  1571. ofen when using the lib in a multi-threaded environment! */
  1572. if(errno == EINTR)
  1573. ;
  1574. else
  1575. #endif
  1576. done = TRUE; /* no more read or write */
  1577. continue;
  1578. case 0: /* timeout */
  1579. default: /* readable descriptors */
  1580. result = Curl_readwrite(conn, &done);
  1581. break;
  1582. }
  1583. if(result)
  1584. return result;
  1585. /* "done" signals to us if the transfer(s) are ready */
  1586. }
  1587. return CURLE_OK;
  1588. }
  1589. /*
  1590. * Curl_pretransfer() is called immediately before a transfer starts.
  1591. */
  1592. CURLcode Curl_pretransfer(struct SessionHandle *data)
  1593. {
  1594. CURLcode res;
  1595. if(!data->change.url) {
  1596. /* we can't do anything wihout URL */
  1597. failf(data, "No URL set!\n");
  1598. return CURLE_URL_MALFORMAT;
  1599. }
  1600. /* Init the SSL session ID cache here. We do it here since we want to do it
  1601. after the *_setopt() calls (that could change the size of the cache) but
  1602. before any transfer takes place. */
  1603. res = Curl_ssl_initsessions(data, data->set.ssl.numsessions);
  1604. if(res)
  1605. return res;
  1606. data->set.followlocation=0; /* reset the location-follow counter */
  1607. data->state.this_is_a_follow = FALSE; /* reset this */
  1608. data->state.errorbuf = FALSE; /* no error has occurred */
  1609. data->state.authproblem = FALSE;
  1610. data->state.authhost.want = data->set.httpauth;
  1611. data->state.authproxy.want = data->set.proxyauth;
  1612. /* If there is a list of cookie files to read, do it now! */
  1613. if(data->change.cookielist) {
  1614. Curl_cookie_loadfiles(data);
  1615. }
  1616. /* Allow data->set.use_port to set which port to use. This needs to be
  1617. * disabled for example when we follow Location: headers to URLs using
  1618. * different ports! */
  1619. data->state.allow_port = TRUE;
  1620. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  1621. /*************************************************************
  1622. * Tell signal handler to ignore SIGPIPE
  1623. *************************************************************/
  1624. if(!data->set.no_signal)
  1625. data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
  1626. #endif
  1627. Curl_initinfo(data); /* reset session-specific information "variables" */
  1628. Curl_pgrsStartNow(data);
  1629. return CURLE_OK;
  1630. }
  1631. /*
  1632. * Curl_posttransfer() is called immediately after a transfer ends
  1633. */
  1634. CURLcode Curl_posttransfer(struct SessionHandle *data)
  1635. {
  1636. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  1637. /* restore the signal handler for SIGPIPE before we get back */
  1638. if(!data->set.no_signal)
  1639. signal(SIGPIPE, data->state.prev_signal);
  1640. #else
  1641. (void)data; /* unused parameter */
  1642. #endif
  1643. if(!(data->progress.flags & PGRS_HIDE) &&
  1644. !data->progress.callback)
  1645. /* only output if we don't use a progress callback and we're not hidden */
  1646. fprintf(data->set.err, "\n");
  1647. return CURLE_OK;
  1648. }
  1649. /*
  1650. * strlen_url() returns the length of the given URL if the spaces within the
  1651. * URL were properly URL encoded.
  1652. */
  1653. static int strlen_url(char *url)
  1654. {
  1655. char *ptr;
  1656. int newlen=0;
  1657. bool left=TRUE; /* left side of the ? */
  1658. for(ptr=url; *ptr; ptr++) {
  1659. switch(*ptr) {
  1660. case '?':
  1661. left=FALSE;
  1662. default:
  1663. newlen++;
  1664. break;
  1665. case ' ':
  1666. if(left)
  1667. newlen+=3;
  1668. else
  1669. newlen++;
  1670. break;
  1671. }
  1672. }
  1673. return newlen;
  1674. }
  1675. /* strcpy_url() copies a url to a output buffer and URL-encodes the spaces in
  1676. * the source URL accordingly.
  1677. */
  1678. static void strcpy_url(char *output, char *url)
  1679. {
  1680. /* we must add this with whitespace-replacing */
  1681. bool left=TRUE;
  1682. char *iptr;
  1683. char *optr = output;
  1684. for(iptr = url; /* read from here */
  1685. *iptr; /* until zero byte */
  1686. iptr++) {
  1687. switch(*iptr) {
  1688. case '?':
  1689. left=FALSE;
  1690. default:
  1691. *optr++=*iptr;
  1692. break;
  1693. case ' ':
  1694. if(left) {
  1695. *optr++='%'; /* add a '%' */
  1696. *optr++='2'; /* add a '2' */
  1697. *optr++='0'; /* add a '0' */
  1698. }
  1699. else
  1700. *optr++='+'; /* add a '+' here */
  1701. break;
  1702. }
  1703. }
  1704. *optr=0; /* zero terminate output buffer */
  1705. }
  1706. /*
  1707. * Curl_follow() handles the URL redirect magic. Pass in the 'newurl' string
  1708. * as given by the remote server and set up the new URL to request.
  1709. */
  1710. CURLcode Curl_follow(struct SessionHandle *data,
  1711. char *newurl, /* this 'newurl' is the Location: string,
  1712. and it must be malloc()ed before passed
  1713. here */
  1714. bool retry) /* set TRUE if this is a request retry as
  1715. opposed to a real redirect following */
  1716. {
  1717. /* Location: redirect */
  1718. char prot[16]; /* URL protocol string storage */
  1719. char letter; /* used for a silly sscanf */
  1720. size_t newlen;
  1721. char *newest;
  1722. if(!retry) {
  1723. if ((data->set.maxredirs != -1) &&
  1724. (data->set.followlocation >= data->set.maxredirs)) {
  1725. failf(data,"Maximum (%d) redirects followed", data->set.maxredirs);
  1726. return CURLE_TOO_MANY_REDIRECTS;
  1727. }
  1728. /* mark the next request as a followed location: */
  1729. data->state.this_is_a_follow = TRUE;
  1730. data->set.followlocation++; /* count location-followers */
  1731. }
  1732. if(data->set.http_auto_referer) {
  1733. /* We are asked to automatically set the previous URL as the
  1734. referer when we get the next URL. We pick the ->url field,
  1735. which may or may not be 100% correct */
  1736. if(data->change.referer_alloc)
  1737. /* If we already have an allocated referer, free this first */
  1738. free(data->change.referer);
  1739. data->change.referer = strdup(data->change.url);
  1740. data->change.referer_alloc = TRUE; /* yes, free this later */
  1741. }
  1742. if(2 != sscanf(newurl, "%15[^?&/:]://%c", prot, &letter)) {
  1743. /***
  1744. *DANG* this is an RFC 2068 violation. The URL is supposed
  1745. to be absolute and this doesn't seem to be that!
  1746. ***
  1747. Instead, we have to TRY to append this new path to the old URL
  1748. to the right of the host part. Oh crap, this is doomed to cause
  1749. problems in the future...
  1750. */
  1751. char *protsep;
  1752. char *pathsep;
  1753. char *useurl = newurl;
  1754. size_t urllen;
  1755. /* we must make our own copy of the URL to play with, as it may
  1756. point to read-only data */
  1757. char *url_clone=strdup(data->change.url);
  1758. if(!url_clone)
  1759. return CURLE_OUT_OF_MEMORY; /* skip out of this NOW */
  1760. /* protsep points to the start of the host name */
  1761. protsep=strstr(url_clone, "//");
  1762. if(!protsep)
  1763. protsep=url_clone;
  1764. else
  1765. protsep+=2; /* pass the slashes */
  1766. if('/' != newurl[0]) {
  1767. int level=0;
  1768. /* First we need to find out if there's a ?-letter in the URL,
  1769. and cut it and the right-side of that off */
  1770. pathsep = strchr(protsep, '?');
  1771. if(pathsep)
  1772. *pathsep=0;
  1773. /* we have a relative path to append to the last slash if
  1774. there's one available */
  1775. pathsep = strrchr(protsep, '/');
  1776. if(pathsep)
  1777. *pathsep=0;
  1778. /* Check if there's any slash after the host name, and if so,
  1779. remember that position instead */
  1780. pathsep = strchr(protsep, '/');
  1781. if(pathsep)
  1782. protsep = pathsep+1;
  1783. else
  1784. protsep = NULL;
  1785. /* now deal with one "./" or any amount of "../" in the newurl
  1786. and act accordingly */
  1787. if((useurl[0] == '.') && (useurl[1] == '/'))
  1788. useurl+=2; /* just skip the "./" */
  1789. while((useurl[0] == '.') &&
  1790. (useurl[1] == '.') &&
  1791. (useurl[2] == '/')) {
  1792. level++;
  1793. useurl+=3; /* pass the "../" */
  1794. }
  1795. if(protsep) {
  1796. while(level--) {
  1797. /* cut off one more level from the right of the original URL */
  1798. pathsep = strrchr(protsep, '/');
  1799. if(pathsep)
  1800. *pathsep=0;
  1801. else {
  1802. *protsep=0;
  1803. break;
  1804. }
  1805. }
  1806. }
  1807. }
  1808. else {
  1809. /* We got a new absolute path for this server, cut off from the
  1810. first slash */
  1811. pathsep = strchr(protsep, '/');
  1812. if(pathsep) {
  1813. /* When people use badly formatted URLs, such as
  1814. "http://www.url.com?dir=/home/daniel" we must not use the first
  1815. slash, if there's a ?-letter before it! */
  1816. char *sep = strchr(protsep, '?');
  1817. if(sep && (sep < pathsep))
  1818. pathsep = sep;
  1819. *pathsep=0;
  1820. }
  1821. else {
  1822. /* There was no slash. Now, since we might be operating on a badly
  1823. formatted URL, such as "http://www.url.com?id=2380" which doesn't
  1824. use a slash separator as it is supposed to, we need to check for a
  1825. ?-letter as well! */
  1826. pathsep = strchr(protsep, '?');
  1827. if(pathsep)
  1828. *pathsep=0;
  1829. }
  1830. }
  1831. /* If the new part contains a space, this is a mighty stupid redirect
  1832. but we still make an effort to do "right". To the left of a '?'
  1833. letter we replace each space with %20 while it is replaced with '+'
  1834. on the right side of the '?' letter.
  1835. */
  1836. newlen = strlen_url(useurl);
  1837. urllen = strlen(url_clone);
  1838. newest=(char *)malloc( urllen + 1 + /* possible slash */
  1839. newlen + 1 /* zero byte */);
  1840. if(!newest) {
  1841. free(url_clone); /* don't leak this */
  1842. return CURLE_OUT_OF_MEMORY; /* go out from this */
  1843. }
  1844. /* copy over the root url part */
  1845. memcpy(newest, url_clone, urllen);
  1846. /* check if we need to append a slash */
  1847. if(('/' == useurl[0]) || (protsep && !*protsep))
  1848. ;
  1849. else
  1850. newest[urllen++]='/';
  1851. /* then append the new piece on the right side */
  1852. strcpy_url(&newest[urllen], useurl);
  1853. free(newurl); /* newurl is the allocated pointer */
  1854. free(url_clone);
  1855. newurl = newest;
  1856. }
  1857. else {
  1858. /* This is an absolute URL, don't allow the custom port number */
  1859. data->state.allow_port = FALSE;
  1860. if(strchr(newurl, ' ')) {
  1861. /* This new URL contains at least one space, this is a mighty stupid
  1862. redirect but we still make an effort to do "right". */
  1863. newlen = strlen_url(newurl);
  1864. newest = malloc(newlen+1); /* get memory for this */
  1865. if(newest) {
  1866. strcpy_url(newest, newurl); /* create a space-free URL */
  1867. free(newurl); /* that was no good */
  1868. newurl = newest; /* use this instead now */
  1869. }
  1870. }
  1871. }
  1872. if(data->change.url_alloc)
  1873. free(data->change.url);
  1874. else
  1875. data->change.url_alloc = TRUE; /* the URL is allocated */
  1876. data->change.url = newurl;
  1877. newurl = NULL; /* don't free! */
  1878. infof(data, "Issue another request to this URL: '%s'\n", data->change.url);
  1879. /*
  1880. * We get here when the HTTP code is 300-399 (and 401). We need to perform
  1881. * differently based on exactly what return code there was.
  1882. *
  1883. * News from 7.10.6: we can also get here on a 401 or 407, in case we act on
  1884. * a HTTP (proxy-) authentication scheme other than Basic.
  1885. */
  1886. switch(data->info.httpcode) {
  1887. /* 401 - Act on a www-authentication, we keep on moving and do the
  1888. Authorization: XXXX header in the HTTP request code snippet */
  1889. /* 407 - Act on a proxy-authentication, we keep on moving and do the
  1890. Proxy-Authorization: XXXX header in the HTTP request code snippet */
  1891. /* 300 - Multiple Choices */
  1892. /* 306 - Not used */
  1893. /* 307 - Temporary Redirect */
  1894. default: /* for all above (and the unknown ones) */
  1895. /* Some codes are explicitly mentioned since I've checked RFC2616 and they
  1896. * seem to be OK to POST to.
  1897. */
  1898. break;
  1899. case 301: /* Moved Permanently */
  1900. /* (quote from RFC2616, section 10.3.2):
  1901. *
  1902. * Note: When automatically redirecting a POST request after receiving a
  1903. * 301 status code, some existing HTTP/1.0 user agents will erroneously
  1904. * change it into a GET request.
  1905. *
  1906. * ----
  1907. *
  1908. * Warning: Because most of importants user agents do this obvious RFC2616
  1909. * violation, many webservers expect this misbehavior. So these servers
  1910. * often answers to a POST request with an error page. To be sure that
  1911. * libcurl gets the page that most user agents would get, libcurl has to
  1912. * force GET:
  1913. */
  1914. if( data->set.httpreq == HTTPREQ_POST
  1915. || data->set.httpreq == HTTPREQ_POST_FORM) {
  1916. infof(data,
  1917. "Violate RFC 2616/10.3.2 and switch from POST to GET\n");
  1918. data->set.httpreq = HTTPREQ_GET;
  1919. }
  1920. break;
  1921. case 302: /* Found */
  1922. /* (From 10.3.3)
  1923. Note: RFC 1945 and RFC 2068 specify that the client is not allowed
  1924. to change the method on the redirected request. However, most
  1925. existing user agent implementations treat 302 as if it were a 303
  1926. response, performing a GET on the Location field-value regardless
  1927. of the original request method. The status codes 303 and 307 have
  1928. been added for servers that wish to make unambiguously clear which
  1929. kind of reaction is expected of the client.
  1930. (From 10.3.4)
  1931. Note: Many pre-HTTP/1.1 user agents do not understand the 303
  1932. status. When interoperability with such clients is a concern, the
  1933. 302 status code may be used instead, since most user agents react
  1934. to a 302 response as described here for 303.
  1935. */
  1936. case 303: /* See Other */
  1937. /* Disable both types of POSTs, since doing a second POST when
  1938. * following isn't what anyone would want! */
  1939. if(data->set.httpreq != HTTPREQ_GET) {
  1940. data->set.httpreq = HTTPREQ_GET; /* enforce GET request */
  1941. infof(data, "Disables POST, goes with %s\n",
  1942. data->set.opt_no_body?"HEAD":"GET");
  1943. }
  1944. break;
  1945. case 304: /* Not Modified */
  1946. /* 304 means we did a conditional request and it was "Not modified".
  1947. * We shouldn't get any Location: header in this response!
  1948. */
  1949. break;
  1950. case 305: /* Use Proxy */
  1951. /* (quote from RFC2616, section 10.3.6):
  1952. * "The requested resource MUST be accessed through the proxy given
  1953. * by the Location field. The Location field gives the URI of the
  1954. * proxy. The recipient is expected to repeat this single request
  1955. * via the proxy. 305 responses MUST only be generated by origin
  1956. * servers."
  1957. */
  1958. break;
  1959. }
  1960. Curl_pgrsTime(data, TIMER_REDIRECT);
  1961. Curl_pgrsResetTimes(data);
  1962. return CURLE_OK;
  1963. }
  1964. static CURLcode
  1965. Curl_connect_host(struct SessionHandle *data,
  1966. struct connectdata **conn)
  1967. {
  1968. CURLcode res = CURLE_OK;
  1969. int urlchanged = FALSE;
  1970. do {
  1971. bool async;
  1972. bool protocol_done=TRUE; /* will be TRUE always since this is only used
  1973. within the easy interface */
  1974. Curl_pgrsTime(data, TIMER_STARTSINGLE);
  1975. data->change.url_changed = FALSE;
  1976. res = Curl_connect(data, conn, &async, &protocol_done);
  1977. if((CURLE_OK == res) && async) {
  1978. /* Now, if async is TRUE here, we need to wait for the name
  1979. to resolve */
  1980. res = Curl_wait_for_resolv(*conn, NULL);
  1981. if(CURLE_OK == res)
  1982. /* Resolved, continue with the connection */
  1983. res = Curl_async_resolved(*conn, &protocol_done);
  1984. else
  1985. /* if we can't resolve, we kill this "connection" now */
  1986. (void)Curl_disconnect(*conn);
  1987. }
  1988. if(res)
  1989. break;
  1990. /* If a callback (or something) has altered the URL we should use within
  1991. the Curl_connect(), we detect it here and act as if we are redirected
  1992. to the new URL */
  1993. urlchanged = data->change.url_changed;
  1994. if ((CURLE_OK == res) && urlchanged) {
  1995. res = Curl_done(conn, res, FALSE);
  1996. if(CURLE_OK == res) {
  1997. char *gotourl = strdup(data->change.url);
  1998. res = Curl_follow(data, gotourl, FALSE);
  1999. if(res)
  2000. free(gotourl);
  2001. }
  2002. }
  2003. } while (urlchanged && res == CURLE_OK);
  2004. return res;
  2005. }
  2006. /* Returns TRUE and sets '*url' if a request retry is wanted */
  2007. bool Curl_retry_request(struct connectdata *conn,
  2008. char **url)
  2009. {
  2010. bool retry = FALSE;
  2011. struct SessionHandle *data = conn->data;
  2012. if((data->reqdata.keep.bytecount+conn->headerbytecount == 0) &&
  2013. conn->bits.reuse &&
  2014. !conn->bits.no_body) {
  2015. /* We got no data, we attempted to re-use a connection and yet we want a
  2016. "body". This might happen if the connection was left alive when we were
  2017. done using it before, but that was closed when we wanted to read from
  2018. it again. Bad luck. Retry the same request on a fresh connect! */
  2019. infof(conn->data, "Connection died, retrying a fresh connect\n");
  2020. *url = strdup(conn->data->change.url);
  2021. conn->bits.close = TRUE; /* close this connection */
  2022. conn->bits.retry = TRUE; /* mark this as a connection we're about
  2023. to retry. Marking it this way should
  2024. prevent i.e HTTP transfers to return
  2025. error just because nothing has been
  2026. transfered! */
  2027. retry = TRUE;
  2028. }
  2029. return retry;
  2030. }
  2031. /*
  2032. * Curl_perform() is the internal high-level function that gets called by the
  2033. * external curl_easy_perform() function. It inits, performs and cleans up a
  2034. * single file transfer.
  2035. */
  2036. CURLcode Curl_perform(struct SessionHandle *data)
  2037. {
  2038. CURLcode res;
  2039. CURLcode res2;
  2040. struct connectdata *conn=NULL;
  2041. char *newurl = NULL; /* possibly a new URL to follow to! */
  2042. bool retry = FALSE;
  2043. data->state.used_interface = Curl_if_easy;
  2044. res = Curl_pretransfer(data);
  2045. if(res)
  2046. return res;
  2047. /*
  2048. * It is important that there is NO 'return' from this function at any other
  2049. * place than falling down to the end of the function! This is because we
  2050. * have cleanup stuff that must be done before we get back, and that is only
  2051. * performed after this do-while loop.
  2052. */
  2053. do {
  2054. res = Curl_connect_host(data, &conn); /* primary connection */
  2055. if(res == CURLE_OK) {
  2056. bool do_done;
  2057. if(data->set.connect_only) {
  2058. /* keep connection open for application to use the socket */
  2059. conn->bits.close = FALSE;
  2060. res = Curl_done(&conn, CURLE_OK, FALSE);
  2061. break;
  2062. }
  2063. res = Curl_do(&conn, &do_done);
  2064. if(res == CURLE_OK) {
  2065. res = Transfer(conn); /* now fetch that URL please */
  2066. if(res == CURLE_OK) {
  2067. retry = Curl_retry_request(conn, &newurl);
  2068. if(!retry)
  2069. /*
  2070. * We must duplicate the new URL here as the connection data may
  2071. * be free()ed in the Curl_done() function.
  2072. */
  2073. newurl = data->reqdata.newurl?strdup(data->reqdata.newurl):NULL;
  2074. }
  2075. else {
  2076. /* The transfer phase returned error, we mark the connection to get
  2077. * closed to prevent being re-used. This is becasue we can't
  2078. * possibly know if the connection is in a good shape or not now. */
  2079. conn->bits.close = TRUE;
  2080. if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET]) {
  2081. /* if we failed anywhere, we must clean up the secondary socket if
  2082. it was used */
  2083. sclose(conn->sock[SECONDARYSOCKET]);
  2084. conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
  2085. }
  2086. }
  2087. /* Always run Curl_done(), even if some of the previous calls
  2088. failed, but return the previous (original) error code */
  2089. res2 = Curl_done(&conn, res, FALSE);
  2090. if(CURLE_OK == res)
  2091. res = res2;
  2092. }
  2093. else
  2094. /* Curl_do() failed, clean up left-overs in the done-call */
  2095. res2 = Curl_done(&conn, res, FALSE);
  2096. /*
  2097. * Important: 'conn' cannot be used here, since it may have been closed
  2098. * in 'Curl_done' or other functions.
  2099. */
  2100. if((res == CURLE_OK) && newurl) {
  2101. res = Curl_follow(data, newurl, retry);
  2102. if(CURLE_OK == res) {
  2103. newurl = NULL;
  2104. continue;
  2105. }
  2106. }
  2107. }
  2108. break; /* it only reaches here when this shouldn't loop */
  2109. } while(1); /* loop if Location: */
  2110. if(newurl)
  2111. free(newurl);
  2112. if(res && !data->state.errorbuf) {
  2113. /*
  2114. * As an extra precaution: if no error string has been set and there was
  2115. * an error, use the strerror() string or if things are so bad that not
  2116. * even that is good, set a bad string that mentions the error code.
  2117. */
  2118. const char *str = curl_easy_strerror(res);
  2119. if(!str)
  2120. failf(data, "unspecified error %d", (int)res);
  2121. else
  2122. failf(data, "%s", str);
  2123. }
  2124. /* run post-transfer uncondionally, but don't clobber the return code if
  2125. we already have an error code recorder */
  2126. res2 = Curl_posttransfer(data);
  2127. if(!res && res2)
  2128. res = res2;
  2129. return res;
  2130. }
  2131. /*
  2132. * Curl_setup_transfer() is called to setup some basic properties for the
  2133. * upcoming transfer.
  2134. */
  2135. CURLcode
  2136. Curl_setup_transfer(
  2137. struct connectdata *c_conn, /* connection data */
  2138. int sockindex, /* socket index to read from or -1 */
  2139. curl_off_t size, /* -1 if unknown at this point */
  2140. bool getheader, /* TRUE if header parsing is wanted */
  2141. curl_off_t *bytecountp, /* return number of bytes read or NULL */
  2142. int writesockindex, /* socket index to write to, it may very
  2143. well be the same we read from. -1
  2144. disables */
  2145. curl_off_t *writecountp /* return number of bytes written or
  2146. NULL */
  2147. )
  2148. {
  2149. struct connectdata *conn = (struct connectdata *)c_conn;
  2150. struct SessionHandle *data = conn->data;
  2151. if(!conn)
  2152. return CURLE_BAD_FUNCTION_ARGUMENT;
  2153. curlassert((sockindex <= 1) && (sockindex >= -1));
  2154. /* now copy all input parameters */
  2155. conn->sockfd = sockindex == -1 ?
  2156. CURL_SOCKET_BAD : conn->sock[sockindex];
  2157. conn->writesockfd = writesockindex == -1 ?
  2158. CURL_SOCKET_BAD:conn->sock[writesockindex];
  2159. conn->bits.getheader = getheader;
  2160. data->reqdata.size = size;
  2161. data->reqdata.bytecountp = bytecountp;
  2162. data->reqdata.writebytecountp = writecountp;
  2163. return CURLE_OK;
  2164. }