transfer.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2001, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * In order to be useful for every potential user, curl and libcurl are
  11. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  12. *
  13. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  14. * copies of the Software, and permit persons to whom the Software is
  15. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  16. * licenses. You may pick one of these licenses.
  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. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <errno.h>
  33. #include "strequal.h"
  34. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  35. #include <winsock.h>
  36. #include <time.h>
  37. #include <io.h>
  38. #else
  39. #ifdef HAVE_SYS_SOCKET_H
  40. #include <sys/socket.h>
  41. #endif
  42. #include <netinet/in.h>
  43. #include <sys/time.h>
  44. #include <sys/resource.h>
  45. #ifdef HAVE_UNISTD_H
  46. #include <unistd.h>
  47. #endif
  48. #include <netdb.h>
  49. #ifdef HAVE_ARPA_INET_H
  50. #include <arpa/inet.h>
  51. #endif
  52. #ifdef HAVE_NET_IF_H
  53. #include <net/if.h>
  54. #endif
  55. #include <sys/ioctl.h>
  56. #include <signal.h>
  57. #ifdef HAVE_SYS_PARAM_H
  58. #include <sys/param.h>
  59. #endif
  60. #ifdef HAVE_SYS_SELECT_H
  61. #include <sys/select.h>
  62. #endif
  63. #ifndef HAVE_SELECT
  64. #error "We can't compile without select() support!"
  65. #endif
  66. #ifndef HAVE_SOCKET
  67. #error "We can't compile without socket() support!"
  68. #endif
  69. #endif
  70. #include "urldata.h"
  71. #include <curl/curl.h>
  72. #include <curl/types.h>
  73. #include "netrc.h"
  74. #include "hostip.h"
  75. #include "transfer.h"
  76. #include "sendf.h"
  77. #include "speedcheck.h"
  78. #include "getpass.h"
  79. #include "progress.h"
  80. #include "getdate.h"
  81. #include "http.h"
  82. #include "url.h"
  83. #include "getinfo.h"
  84. #include "ssluse.h"
  85. #define _MPRINTF_REPLACE /* use our functions only */
  86. #include <curl/mprintf.h>
  87. /* The last #include file should be: */
  88. #ifdef MALLOCDEBUG
  89. #include "memdebug.h"
  90. #endif
  91. #ifndef min
  92. #define min(a, b) ((a) < (b) ? (a) : (b))
  93. #endif
  94. enum {
  95. KEEP_NONE,
  96. KEEP_READ,
  97. KEEP_WRITE
  98. };
  99. /*
  100. * compareheader()
  101. *
  102. * Returns TRUE if 'headerline' contains the 'header' with given 'content'.
  103. * Pass headers WITH the colon.
  104. */
  105. static bool
  106. compareheader(char *headerline, /* line to check */
  107. const char *header, /* header keyword _with_ colon */
  108. const char *content) /* content string to find */
  109. {
  110. /* RFC2616, section 4.2 says: "Each header field consists of a name followed
  111. * by a colon (":") and the field value. Field names are case-insensitive.
  112. * The field value MAY be preceded by any amount of LWS, though a single SP
  113. * is preferred." */
  114. size_t hlen = strlen(header);
  115. size_t clen;
  116. size_t len;
  117. char *start;
  118. char *end;
  119. if(!strnequal(headerline, header, hlen))
  120. return FALSE; /* doesn't start with header */
  121. /* pass the header */
  122. start = &headerline[hlen];
  123. /* pass all white spaces */
  124. while(*start && isspace((int)*start))
  125. start++;
  126. /* find the end of the header line */
  127. end = strchr(start, '\r'); /* lines end with CRLF */
  128. if(!end) {
  129. /* in case there's a non-standard compliant line here */
  130. end = strchr(start, '\n');
  131. if(!end)
  132. /* hm, there's no line ending here, return false and bail out! */
  133. return FALSE;
  134. }
  135. len = end-start; /* length of the content part of the input line */
  136. clen = strlen(content); /* length of the word to find */
  137. /* find the content string in the rest of the line */
  138. for(;len>=clen;len--, start++) {
  139. if(strnequal(start, content, clen))
  140. return TRUE; /* match! */
  141. }
  142. return FALSE; /* no match */
  143. }
  144. CURLcode Curl_readwrite(struct connectdata *conn,
  145. bool *done)
  146. {
  147. struct Curl_transfer_keeper *k = &conn->keep;
  148. struct SessionHandle *data = conn->data;
  149. int result;
  150. ssize_t nread; /* number of bytes read */
  151. int didwhat=0;
  152. do {
  153. if((k->keepon & KEEP_READ) &&
  154. FD_ISSET(conn->sockfd, &k->readfd)) {
  155. /* read! */
  156. result = Curl_read(conn, conn->sockfd, k->buf,
  157. BUFSIZE -1, &nread);
  158. if(0>result)
  159. break; /* get out of loop */
  160. if(result>0)
  161. return result;
  162. if ((k->bytecount == 0) && (k->writebytecount == 0))
  163. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  164. didwhat |= KEEP_READ;
  165. /* NULL terminate, allowing string ops to be used */
  166. if (0 < nread)
  167. k->buf[nread] = 0;
  168. /* if we receive 0 or less here, the server closed the connection and
  169. we bail out from this! */
  170. else if (0 >= nread) {
  171. k->keepon &= ~KEEP_READ;
  172. FD_ZERO(&k->rkeepfd);
  173. break;
  174. }
  175. /* Default buffer to use when we write the buffer, it may be changed
  176. in the flow below before the actual storing is done. */
  177. k->str = k->buf;
  178. /* Since this is a two-state thing, we check if we are parsing
  179. headers at the moment or not. */
  180. if (k->header) {
  181. /* we are in parse-the-header-mode */
  182. /* header line within buffer loop */
  183. do {
  184. int hbufp_index;
  185. /* str_start is start of line within buf */
  186. k->str_start = k->str;
  187. k->end_ptr = strchr (k->str_start, '\n');
  188. if (!k->end_ptr) {
  189. /* no more complete header lines within buffer */
  190. /* copy what is remaining into headerbuff */
  191. int str_length = (int)strlen(k->str);
  192. /*
  193. * We enlarge the header buffer if it seems to be too
  194. * smallish
  195. */
  196. if (k->hbuflen + (int)str_length >=
  197. data->state.headersize) {
  198. char *newbuff;
  199. long newsize=MAX((k->hbuflen+str_length)*3/2,
  200. data->state.headersize*2);
  201. hbufp_index = k->hbufp - data->state.headerbuff;
  202. newbuff = (char *)realloc(data->state.headerbuff, newsize);
  203. if(!newbuff) {
  204. failf (data, "Failed to alloc memory for big header!");
  205. return CURLE_READ_ERROR;
  206. }
  207. data->state.headersize=newsize;
  208. data->state.headerbuff = newbuff;
  209. k->hbufp = data->state.headerbuff + hbufp_index;
  210. }
  211. strcpy (k->hbufp, k->str);
  212. k->hbufp += strlen (k->str);
  213. k->hbuflen += strlen (k->str);
  214. break; /* read more and try again */
  215. }
  216. k->str = k->end_ptr + 1; /* move past new line */
  217. /*
  218. * We're about to copy a chunk of data to the end of the
  219. * already received header. We make sure that the full string
  220. * fit in the allocated header buffer, or else we enlarge
  221. * it.
  222. */
  223. if (k->hbuflen + (k->str - k->str_start) >=
  224. data->state.headersize) {
  225. char *newbuff;
  226. long newsize=MAX((k->hbuflen+
  227. (k->str-k->str_start))*3/2,
  228. data->state.headersize*2);
  229. hbufp_index = k->hbufp - data->state.headerbuff;
  230. newbuff = (char *)realloc(data->state.headerbuff, newsize);
  231. if(!newbuff) {
  232. failf (data, "Failed to alloc memory for big header!");
  233. return CURLE_READ_ERROR;
  234. }
  235. data->state.headersize= newsize;
  236. data->state.headerbuff = newbuff;
  237. k->hbufp = data->state.headerbuff + hbufp_index;
  238. }
  239. /* copy to end of line */
  240. strncpy (k->hbufp, k->str_start, k->str - k->str_start);
  241. k->hbufp += k->str - k->str_start;
  242. k->hbuflen += k->str - k->str_start;
  243. *k->hbufp = 0;
  244. k->p = data->state.headerbuff;
  245. /****
  246. * We now have a FULL header line that p points to
  247. *****/
  248. if (('\n' == *k->p) || ('\r' == *k->p)) {
  249. /* Zero-length header line means end of headers! */
  250. if ('\r' == *k->p)
  251. k->p++; /* pass the \r byte */
  252. if ('\n' == *k->p)
  253. k->p++; /* pass the \n byte */
  254. if(100 == k->httpcode) {
  255. /*
  256. * we have made a HTTP PUT or POST and this is 1.1-lingo
  257. * that tells us that the server is OK with this and ready
  258. * to receive our stuff.
  259. * However, we'll get more headers now so we must get
  260. * back into the header-parsing state!
  261. */
  262. k->header = TRUE;
  263. k->headerline = 0; /* restart the header line counter */
  264. /* if we did wait for this do enable write now! */
  265. if (k->write_after_100_header) {
  266. k->write_after_100_header = FALSE;
  267. FD_SET (conn->writesockfd, &k->writefd); /* write */
  268. k->keepon |= KEEP_WRITE;
  269. k->wkeepfd = k->writefd;
  270. }
  271. }
  272. else
  273. k->header = FALSE; /* no more header to parse! */
  274. if (417 == k->httpcode) {
  275. /*
  276. * we got: "417 Expectation Failed" this means:
  277. * we have made a HTTP call and our Expect Header
  278. * seems to cause a problem => abort the write operations
  279. * (or prevent them from starting
  280. */
  281. k->write_after_100_header = FALSE;
  282. k->keepon &= ~KEEP_WRITE;
  283. FD_ZERO(&k->wkeepfd);
  284. }
  285. /* now, only output this if the header AND body are requested:
  286. */
  287. k->writetype = CLIENTWRITE_HEADER;
  288. if (data->set.http_include_header)
  289. k->writetype |= CLIENTWRITE_BODY;
  290. result = Curl_client_write(data, k->writetype,
  291. data->state.headerbuff,
  292. k->p - data->state.headerbuff);
  293. if(result)
  294. return result;
  295. data->info.header_size += k->p - data->state.headerbuff;
  296. conn->headerbytecount += k->p - data->state.headerbuff;
  297. if(!k->header) {
  298. /*
  299. * really end-of-headers.
  300. *
  301. * If we requested a "no body", this is a good time to get
  302. * out and return home.
  303. */
  304. bool stop_reading = FALSE;
  305. if(data->set.no_body)
  306. stop_reading = TRUE;
  307. else if(!conn->bits.close) {
  308. /* If this is not the last request before a close, we must
  309. set the maximum download size to the size of the
  310. expected document or else, we won't know when to stop
  311. reading! */
  312. if(-1 != conn->size)
  313. conn->maxdownload = conn->size;
  314. /* If max download size is *zero* (nothing) we already
  315. have nothing and can safely return ok now! */
  316. if(0 == conn->maxdownload)
  317. stop_reading = TRUE;
  318. /* What to do if the size is *not* known? */
  319. }
  320. if(stop_reading) {
  321. /* we make sure that this socket isn't read more now */
  322. k->keepon &= ~KEEP_READ;
  323. FD_ZERO(&k->rkeepfd);
  324. /* for a progress meter/info update before going away */
  325. Curl_pgrsUpdate(conn);
  326. return CURLE_OK;
  327. }
  328. break; /* exit header line loop */
  329. }
  330. /* We continue reading headers, so reset the line-based
  331. header parsing variables hbufp && hbuflen */
  332. k->hbufp = data->state.headerbuff;
  333. k->hbuflen = 0;
  334. continue;
  335. }
  336. /*
  337. * Checks for special headers coming up.
  338. */
  339. if (!k->headerline++) {
  340. /* This is the first header, it MUST be the error code line
  341. or else we consiser this to be the body right away! */
  342. int httpversion_major;
  343. int nc=sscanf (k->p, " HTTP/%d.%d %3d",
  344. &httpversion_major,
  345. &k->httpversion,
  346. &k->httpcode);
  347. if (nc==3) {
  348. k->httpversion += 10 * httpversion_major;
  349. }
  350. else {
  351. /* this is the real world, not a Nirvana
  352. NCSA 1.5.x returns this crap when asked for HTTP/1.1
  353. */
  354. nc=sscanf (k->p, " HTTP %3d", &k->httpcode);
  355. k->httpversion = 10;
  356. }
  357. if (nc) {
  358. data->info.httpcode = k->httpcode;
  359. data->info.httpversion = k->httpversion;
  360. /* 404 -> URL not found! */
  361. if (data->set.http_fail_on_error &&
  362. (k->httpcode >= 400)) {
  363. /* If we have been told to fail hard on HTTP-errors,
  364. here is the check for that: */
  365. /* serious error, go home! */
  366. failf (data, "The requested file was not found");
  367. return CURLE_HTTP_NOT_FOUND;
  368. }
  369. if(k->httpversion == 10)
  370. /* Default action for HTTP/1.0 must be to close, unless
  371. we get one of those fancy headers that tell us the
  372. server keeps it open for us! */
  373. conn->bits.close = TRUE;
  374. switch(k->httpcode) {
  375. case 204:
  376. /* (quote from RFC2616, section 10.2.5): The server has
  377. * fulfilled the request but does not need to return an
  378. * entity-body ... The 204 response MUST NOT include a
  379. * message-body, and thus is always terminated by the first
  380. * empty line after the header fields. */
  381. /* FALLTHROUGH */
  382. case 304:
  383. /* (quote from RFC2616, section 10.3.5): The 304 response MUST
  384. * NOT contain a message-body, and thus is always terminated
  385. * by the first empty line after the header fields. */
  386. conn->size=0;
  387. break;
  388. default:
  389. /* nothing */
  390. break;
  391. }
  392. }
  393. else {
  394. k->header = FALSE; /* this is not a header line */
  395. break;
  396. }
  397. }
  398. /* check for Content-Length: header lines to get size */
  399. if (strnequal("Content-Length:", k->p, 15) &&
  400. sscanf (k->p+15, " %ld", &k->contentlength)) {
  401. conn->size = k->contentlength;
  402. Curl_pgrsSetDownloadSize(data, k->contentlength);
  403. }
  404. /* check for Content-Type: header lines to get the mime-type */
  405. else if (strnequal("Content-Type:", k->p, 13)) {
  406. char *start;
  407. char *end;
  408. int len;
  409. /* Find the first non-space letter */
  410. for(start=k->p+14;
  411. *start && isspace((int)*start);
  412. start++);
  413. /* count all non-space letters following */
  414. for(end=start, len=0;
  415. *end && !isspace((int)*end);
  416. end++, len++);
  417. /* allocate memory of a cloned copy */
  418. data->info.contenttype = malloc(len + 1);
  419. if (NULL == data->info.contenttype)
  420. return CURLE_OUT_OF_MEMORY;
  421. /* copy the content-type string */
  422. memcpy(data->info.contenttype, start, len);
  423. data->info.contenttype[len] = 0; /* zero terminate */
  424. }
  425. else if((k->httpversion == 10) &&
  426. conn->bits.httpproxy &&
  427. compareheader(k->p, "Proxy-Connection:", "keep-alive")) {
  428. /*
  429. * When a HTTP/1.0 reply comes when using a proxy, the
  430. * 'Proxy-Connection: keep-alive' line tells us the
  431. * connection will be kept alive for our pleasure.
  432. * Default action for 1.0 is to close.
  433. */
  434. conn->bits.close = FALSE; /* don't close when done */
  435. infof(data, "HTTP/1.0 proxy connection set to keep alive!\n");
  436. }
  437. else if((k->httpversion == 10) &&
  438. compareheader(k->p, "Connection:", "keep-alive")) {
  439. /*
  440. * A HTTP/1.0 reply with the 'Connection: keep-alive' line
  441. * tells us the connection will be kept alive for our
  442. * pleasure. Default action for 1.0 is to close.
  443. *
  444. * [RFC2068, section 19.7.1] */
  445. conn->bits.close = FALSE; /* don't close when done */
  446. infof(data, "HTTP/1.0 connection set to keep alive!\n");
  447. }
  448. else if (compareheader(k->p, "Connection:", "close")) {
  449. /*
  450. * [RFC 2616, section 8.1.2.1]
  451. * "Connection: close" is HTTP/1.1 language and means that
  452. * the connection will close when this request has been
  453. * served.
  454. */
  455. conn->bits.close = TRUE; /* close when done */
  456. }
  457. else if (compareheader(k->p, "Transfer-Encoding:", "chunked")) {
  458. /*
  459. * [RFC 2616, section 3.6.1] A 'chunked' transfer encoding
  460. * means that the server will send a series of "chunks". Each
  461. * chunk starts with line with info (including size of the
  462. * coming block) (terminated with CRLF), then a block of data
  463. * with the previously mentioned size. There can be any amount
  464. * of chunks, and a chunk-data set to zero signals the
  465. * end-of-chunks. */
  466. conn->bits.chunk = TRUE; /* chunks coming our way */
  467. /* init our chunky engine */
  468. Curl_httpchunk_init(conn);
  469. }
  470. else if (strnequal("Content-Range:", k->p, 14)) {
  471. if (sscanf (k->p+14, " bytes %d-", &k->offset) ||
  472. sscanf (k->p+14, " bytes: %d-", &k->offset)) {
  473. /* This second format was added August 1st 2000 by Igor
  474. Khristophorov since Sun's webserver JavaWebServer/1.1.1
  475. obviously sends the header this way! :-( */
  476. if (conn->resume_from == k->offset) {
  477. /* we asked for a resume and we got it */
  478. k->content_range = TRUE;
  479. }
  480. }
  481. }
  482. else if(data->cookies &&
  483. strnequal("Set-Cookie:", k->p, 11)) {
  484. Curl_cookie_add(data->cookies, TRUE, k->p+12, conn->name);
  485. }
  486. else if(strnequal("Last-Modified:", k->p,
  487. strlen("Last-Modified:")) &&
  488. (data->set.timecondition || data->set.get_filetime) ) {
  489. time_t secs=time(NULL);
  490. k->timeofdoc = curl_getdate(k->p+strlen("Last-Modified:"),
  491. &secs);
  492. if(data->set.get_filetime)
  493. data->info.filetime = k->timeofdoc;
  494. }
  495. else if ((k->httpcode >= 300 && k->httpcode < 400) &&
  496. (data->set.http_follow_location) &&
  497. strnequal("Location:", k->p, 9)) {
  498. /* this is the URL that the server advices us to get instead */
  499. char *ptr;
  500. char *start=k->p;
  501. char backup;
  502. start += 9; /* pass "Location:" */
  503. /* Skip spaces and tabs. We do this to support multiple
  504. white spaces after the "Location:" keyword. */
  505. while(*start && isspace((int)*start ))
  506. start++;
  507. ptr = start; /* start scanning here */
  508. /* scan through the string to find the end */
  509. while(*ptr && !isspace((int)*ptr))
  510. ptr++;
  511. backup = *ptr; /* store the ending letter */
  512. *ptr = '\0'; /* zero terminate */
  513. conn->newurl = strdup(start); /* clone string */
  514. *ptr = backup; /* restore ending letter */
  515. }
  516. /*
  517. * End of header-checks. Write them to the client.
  518. */
  519. k->writetype = CLIENTWRITE_HEADER;
  520. if (data->set.http_include_header)
  521. k->writetype |= CLIENTWRITE_BODY;
  522. result = Curl_client_write(data, k->writetype, k->p,
  523. k->hbuflen);
  524. if(result)
  525. return result;
  526. data->info.header_size += k->hbuflen;
  527. conn->headerbytecount += k->hbuflen;
  528. /* reset hbufp pointer && hbuflen */
  529. k->hbufp = data->state.headerbuff;
  530. k->hbuflen = 0;
  531. }
  532. while (*k->str); /* header line within buffer */
  533. /* We might have reached the end of the header part here, but
  534. there might be a non-header part left in the end of the read
  535. buffer. */
  536. if (!k->header) {
  537. /* the next token and forward is not part of
  538. the header! */
  539. /* we subtract the remaining header size from the buffer */
  540. nread -= (k->str - k->buf);
  541. }
  542. } /* end if header mode */
  543. /* This is not an 'else if' since it may be a rest from the header
  544. parsing, where the beginning of the buffer is headers and the end
  545. is non-headers. */
  546. if (k->str && !k->header && (nread > 0)) {
  547. if(0 == k->bodywrites) {
  548. /* These checks are only made the first time we are about to
  549. write a piece of the body */
  550. if(conn->protocol&PROT_HTTP) {
  551. /* HTTP-only checks */
  552. if (conn->newurl) {
  553. /* abort after the headers if "follow Location" is set */
  554. infof (data, "Follow to new URL: %s\n", conn->newurl);
  555. k->keepon &= ~KEEP_READ;
  556. FD_ZERO(&k->rkeepfd);
  557. return CURLE_OK;
  558. }
  559. else if (conn->resume_from &&
  560. !k->content_range &&
  561. (data->set.httpreq==HTTPREQ_GET)) {
  562. /* we wanted to resume a download, although the server
  563. doesn't seem to support this and we did this with a GET
  564. (if it wasn't a GET we did a POST or PUT resume) */
  565. failf (data, "HTTP server doesn't seem to support "
  566. "byte ranges. Cannot resume.");
  567. return CURLE_HTTP_RANGE_ERROR;
  568. }
  569. else if(data->set.timecondition && !conn->range) {
  570. /* A time condition has been set AND no ranges have been
  571. requested. This seems to be what chapter 13.3.4 of
  572. RFC 2616 defines to be the correct action for a
  573. HTTP/1.1 client */
  574. if((k->timeofdoc > 0) && (data->set.timevalue > 0)) {
  575. switch(data->set.timecondition) {
  576. case TIMECOND_IFMODSINCE:
  577. default:
  578. if(k->timeofdoc < data->set.timevalue) {
  579. infof(data,
  580. "The requested document is not new enough\n");
  581. return CURLE_OK;
  582. }
  583. break;
  584. case TIMECOND_IFUNMODSINCE:
  585. if(k->timeofdoc > data->set.timevalue) {
  586. infof(data,
  587. "The requested document is not old enough\n");
  588. return CURLE_OK;
  589. }
  590. break;
  591. } /* switch */
  592. } /* two valid time strings */
  593. } /* we have a time condition */
  594. } /* this is HTTP */
  595. } /* this is the first time we write a body part */
  596. k->bodywrites++;
  597. if(conn->bits.chunk) {
  598. /*
  599. * Bless me father for I have sinned. Here comes a chunked
  600. * transfer flying and we need to decode this properly. While
  601. * the name says read, this function both reads and writes away
  602. * the data. The returned 'nread' holds the number of actual
  603. * data it wrote to the client. */
  604. CHUNKcode res =
  605. Curl_httpchunk_read(conn, k->str, nread, &nread);
  606. if(CHUNKE_OK < res) {
  607. failf(data, "Receeived problem in the chunky parser");
  608. return CURLE_READ_ERROR;
  609. }
  610. else if(CHUNKE_STOP == res) {
  611. /* we're done reading chunks! */
  612. k->keepon &= ~KEEP_READ; /* read no more */
  613. FD_ZERO(&k->rkeepfd);
  614. /* There are now possibly N number of bytes at the end of the
  615. str buffer that weren't written to the client, but we don't
  616. care about them right now. */
  617. }
  618. /* If it returned OK, we just keep going */
  619. }
  620. if((-1 != conn->maxdownload) &&
  621. (k->bytecount + nread >= conn->maxdownload)) {
  622. nread = conn->maxdownload - k->bytecount;
  623. if(nread < 0 ) /* this should be unusual */
  624. nread = 0;
  625. k->keepon &= ~KEEP_READ; /* we're done reading */
  626. FD_ZERO(&k->rkeepfd);
  627. }
  628. k->bytecount += nread;
  629. Curl_pgrsSetDownloadCounter(data, (double)k->bytecount);
  630. if(!conn->bits.chunk && nread) {
  631. /* If this is chunky transfer, it was already written */
  632. result = Curl_client_write(data, CLIENTWRITE_BODY, k->str,
  633. nread);
  634. if(result)
  635. return result;
  636. }
  637. } /* if (! header and data to read ) */
  638. } /* if( read from socket ) */
  639. if((k->keepon & KEEP_WRITE) &&
  640. FD_ISSET(conn->writesockfd, &k->writefd)) {
  641. /* write */
  642. int i, si;
  643. ssize_t bytes_written;
  644. if ((k->bytecount == 0) && (k->writebytecount == 0))
  645. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  646. didwhat |= KEEP_WRITE;
  647. /* only read more data if there's no upload data already
  648. present in the upload buffer */
  649. if(0 == conn->upload_present) {
  650. /* init the "upload from here" pointer */
  651. conn->upload_fromhere = k->uploadbuf;
  652. nread = data->set.fread(conn->upload_fromhere, 1,
  653. BUFSIZE, data->set.in);
  654. /* the signed int typecase of nread of for systems that has
  655. unsigned size_t */
  656. if (nread<=0) {
  657. /* done */
  658. k->keepon &= ~KEEP_WRITE; /* we're done writing */
  659. FD_ZERO(&k->wkeepfd);
  660. break;
  661. }
  662. /* store number of bytes available for upload */
  663. conn->upload_present = nread;
  664. /* convert LF to CRLF if so asked */
  665. if (data->set.crlf) {
  666. for(i = 0, si = 0; i < nread; i++, si++) {
  667. if (k->buf[i] == 0x0a) {
  668. data->state.scratch[si++] = 0x0d;
  669. data->state.scratch[si] = 0x0a;
  670. }
  671. else {
  672. data->state.scratch[si] = k->uploadbuf[i];
  673. }
  674. }
  675. nread = si;
  676. k->buf = data->state.scratch; /* point to the new buffer */
  677. }
  678. }
  679. else {
  680. /* We have a partial buffer left from a previous "round". Use
  681. that instead of reading more data */
  682. }
  683. /* write to socket */
  684. result = Curl_write(conn,
  685. conn->writesockfd,
  686. conn->upload_fromhere,
  687. conn->upload_present,
  688. &bytes_written);
  689. if(result)
  690. return result;
  691. else if(conn->upload_present != bytes_written) {
  692. /* we only wrote a part of the buffer (if anything), deal with it! */
  693. /* store the amount of bytes left in the buffer to write */
  694. conn->upload_present -= bytes_written;
  695. /* advance the pointer where to find the buffer when the next send
  696. is to happen */
  697. conn->upload_fromhere += bytes_written;
  698. }
  699. else if(data->set.crlf)
  700. k->buf = data->state.buffer; /* put it back on the buffer */
  701. else {
  702. /* we've uploaded that buffer now */
  703. conn->upload_fromhere = k->uploadbuf;
  704. conn->upload_present = 0; /* no more bytes left */
  705. }
  706. k->writebytecount += bytes_written;
  707. Curl_pgrsSetUploadCounter(data, (double)k->writebytecount);
  708. }
  709. } while(0); /* just to break out from! */
  710. if(didwhat) {
  711. /* Update read/write counters */
  712. if(conn->bytecountp)
  713. *conn->bytecountp = k->bytecount; /* read count */
  714. if(conn->writebytecountp)
  715. *conn->writebytecountp = k->writebytecount; /* write count */
  716. }
  717. else {
  718. /* no read no write, this is a timeout? */
  719. if (k->write_after_100_header) {
  720. /* This should allow some time for the header to arrive, but only a
  721. very short time as otherwise it'll be too much wasted times too
  722. often. */
  723. k->write_after_100_header = FALSE;
  724. FD_SET (conn->writesockfd, &k->writefd); /* write socket */
  725. k->keepon |= KEEP_WRITE;
  726. k->wkeepfd = k->writefd;
  727. }
  728. }
  729. k->now = Curl_tvnow();
  730. if(Curl_pgrsUpdate(conn))
  731. result = CURLE_ABORTED_BY_CALLBACK;
  732. else
  733. result = Curl_speedcheck (data, k->now);
  734. if (result)
  735. return result;
  736. if (data->set.timeout &&
  737. ((Curl_tvdiff(k->now, k->start)/1000) >= data->set.timeout)) {
  738. failf (data, "Operation timed out with %d out of %d bytes received",
  739. k->bytecount, conn->size);
  740. return CURLE_OPERATION_TIMEOUTED;
  741. }
  742. if(!k->keepon) {
  743. /*
  744. * The transfer has been performed. Just make some general checks before
  745. * returning.
  746. */
  747. if(!(data->set.no_body) && k->contentlength &&
  748. (k->bytecount != k->contentlength) &&
  749. !conn->newurl) {
  750. failf(data, "transfer closed with %d bytes remaining to read",
  751. k->contentlength-k->bytecount);
  752. return CURLE_PARTIAL_FILE;
  753. }
  754. else if(conn->bits.chunk && conn->proto.http->chunk.datasize) {
  755. failf(data, "transfer closed with at least %d bytes remaining",
  756. conn->proto.http->chunk.datasize);
  757. return CURLE_PARTIAL_FILE;
  758. }
  759. if(Curl_pgrsUpdate(conn))
  760. return CURLE_ABORTED_BY_CALLBACK;
  761. }
  762. /* Now update the "done" boolean we return */
  763. *done = !k->keepon;
  764. return CURLE_OK;
  765. }
  766. CURLcode Curl_readwrite_init(struct connectdata *conn)
  767. {
  768. struct SessionHandle *data = conn->data;
  769. struct Curl_transfer_keeper *k = &conn->keep;
  770. memset(k, 0, sizeof(struct Curl_transfer_keeper));
  771. k->start = Curl_tvnow(); /* start time */
  772. k->now = k->start; /* current time is now */
  773. k->header = TRUE; /* assume header */
  774. k->httpversion = -1; /* unknown at this point */
  775. k->conn = (struct connectdata *)conn; /* store the connection */
  776. data = conn->data; /* there's the root struct */
  777. k->buf = data->state.buffer;
  778. k->uploadbuf = data->state.uploadbuffer;
  779. k->maxfd = (conn->sockfd>conn->writesockfd?
  780. conn->sockfd:conn->writesockfd)+1;
  781. k->hbufp = data->state.headerbuff;
  782. Curl_pgrsTime(data, TIMER_PRETRANSFER);
  783. Curl_speedinit(data);
  784. if (!conn->getheader) {
  785. k->header = FALSE;
  786. if(conn->size > 0)
  787. Curl_pgrsSetDownloadSize(data, conn->size);
  788. }
  789. /* we want header and/or body, if neither then don't do this! */
  790. if(conn->getheader || !data->set.no_body) {
  791. FD_ZERO (&k->readfd); /* clear it */
  792. if(conn->sockfd != -1) {
  793. FD_SET (conn->sockfd, &k->readfd); /* read socket */
  794. k->keepon |= KEEP_READ;
  795. }
  796. FD_ZERO (&k->writefd); /* clear it */
  797. if(conn->writesockfd != -1) {
  798. if (data->set.expect100header)
  799. /* wait with write until we either got 100-continue or a timeout */
  800. k->write_after_100_header = TRUE;
  801. else {
  802. FD_SET (conn->writesockfd, &k->writefd); /* write socket */
  803. k->keepon |= KEEP_WRITE;
  804. }
  805. }
  806. /* get these in backup variables to be able to restore them on each lap in
  807. the select() loop */
  808. k->rkeepfd = k->readfd;
  809. k->wkeepfd = k->writefd;
  810. }
  811. return CURLE_OK;
  812. }
  813. void Curl_single_fdset(struct connectdata *conn,
  814. fd_set *read_fd_set,
  815. fd_set *write_fd_set,
  816. fd_set *exc_fd_set,
  817. int *max_fd)
  818. {
  819. *max_fd = -1; /* init */
  820. if(conn->keep.keepon & KEEP_READ) {
  821. FD_SET(conn->sockfd, read_fd_set);
  822. *max_fd = conn->sockfd;
  823. }
  824. if(conn->keep.keepon & KEEP_WRITE) {
  825. FD_SET(conn->writesockfd, write_fd_set);
  826. if(conn->writesockfd > *max_fd)
  827. *max_fd = conn->writesockfd;
  828. }
  829. /* we don't use exceptions, only touch that one to prevent compiler
  830. warnings! */
  831. *exc_fd_set = *exc_fd_set;
  832. }
  833. /*
  834. * Transfer()
  835. *
  836. * This function is what performs the actual transfer. It is capable of
  837. * doing both ways simultaneously.
  838. * The transfer must already have been setup by a call to Curl_Transfer().
  839. *
  840. * Note that headers are created in a preallocated buffer of a default size.
  841. * That buffer can be enlarged on demand, but it is never shrinken again.
  842. *
  843. * Parts of this function was once written by the friendly Mark Butler
  844. * <[email protected]>.
  845. */
  846. static CURLcode
  847. Transfer(struct connectdata *conn)
  848. {
  849. struct SessionHandle *data = conn->data;
  850. CURLcode result;
  851. struct Curl_transfer_keeper *k = &conn->keep;
  852. bool done=FALSE;
  853. Curl_readwrite_init(conn);
  854. if((conn->sockfd == -1) && (conn->writesockfd == -1))
  855. /* nothing to read, nothing to write, we're already OK! */
  856. return CURLE_OK;
  857. /* we want header and/or body, if neither then don't do this! */
  858. if(!conn->getheader && data->set.no_body)
  859. return CURLE_OK;
  860. while (!done) {
  861. struct timeval interval;
  862. k->readfd = k->rkeepfd; /* set these every lap in the loop */
  863. k->writefd = k->wkeepfd;
  864. interval.tv_sec = 1;
  865. interval.tv_usec = 0;
  866. switch (select (k->maxfd, &k->readfd, &k->writefd, NULL,
  867. &interval)) {
  868. case -1: /* select() error, stop reading */
  869. #ifdef EINTR
  870. /* The EINTR is not serious, and it seems you might get this more
  871. ofen when using the lib in a multi-threaded environment! */
  872. if(errno == EINTR)
  873. ;
  874. else
  875. #endif
  876. done = TRUE; /* no more read or write */
  877. continue;
  878. case 0: /* timeout */
  879. result = Curl_readwrite(conn, &done);
  880. break;
  881. default: /* readable descriptors */
  882. result = Curl_readwrite(conn, &done);
  883. break;
  884. }
  885. if(result)
  886. return result;
  887. /* "done" signals to us if the transfer(s) are ready */
  888. }
  889. return CURLE_OK;
  890. }
  891. CURLcode Curl_pretransfer(struct SessionHandle *data)
  892. {
  893. if(!data->change.url)
  894. /* we can't do anything wihout URL */
  895. return CURLE_URL_MALFORMAT;
  896. #ifdef USE_SSLEAY
  897. /* Init the SSL session ID cache here. We do it here since we want to
  898. do it after the *_setopt() calls (that could change the size) but
  899. before any transfer. */
  900. Curl_SSL_InitSessions(data, data->set.ssl.numsessions);
  901. #endif
  902. data->set.followlocation=0; /* reset the location-follow counter */
  903. data->state.this_is_a_follow = FALSE; /* reset this */
  904. data->state.errorbuf = FALSE; /* no error has occurred */
  905. /* Allow data->set.use_port to set which port to use. This needs to be
  906. * disabled for example when we follow Location: headers to URLs using
  907. * different ports! */
  908. data->state.allow_port = TRUE;
  909. #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
  910. /*************************************************************
  911. * Tell signal handler to ignore SIGPIPE
  912. *************************************************************/
  913. data->state.prev_signal = signal(SIGPIPE, SIG_IGN);
  914. #endif
  915. Curl_initinfo(data); /* reset session-specific information "variables" */
  916. Curl_pgrsStartNow(data);
  917. return CURLE_OK;
  918. }
  919. CURLcode Curl_posttransfer(struct SessionHandle *data)
  920. {
  921. #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
  922. /* restore the signal handler for SIGPIPE before we get back */
  923. signal(SIGPIPE, data->state.prev_signal);
  924. #endif
  925. return CURLE_OK;
  926. }
  927. CURLcode Curl_perform(struct SessionHandle *data)
  928. {
  929. CURLcode res;
  930. CURLcode res2;
  931. struct connectdata *conn=NULL;
  932. char *newurl = NULL; /* possibly a new URL to follow to! */
  933. res = Curl_pretransfer(data);
  934. if(res)
  935. return res;
  936. /*
  937. * It is important that there is NO 'return' from this function any any
  938. * other place than falling down the bottom! This is because we have cleanup
  939. * stuff that must be done before we get back, and that is only performed
  940. * after this do-while loop.
  941. */
  942. do {
  943. Curl_pgrsTime(data, TIMER_STARTSINGLE);
  944. res = Curl_connect(data, &conn);
  945. if(res == CURLE_OK) {
  946. res = Curl_do(&conn);
  947. if(res == CURLE_OK) {
  948. CURLcode res2; /* just a local extra result container */
  949. if(conn->protocol&PROT_FTPS)
  950. /* FTPS, disable ssl while transfering data */
  951. conn->ssl.use = FALSE;
  952. res = Transfer(conn); /* now fetch that URL please */
  953. if(conn->protocol&PROT_FTPS)
  954. /* FTPS, enable ssl again after havving transferred data */
  955. conn->ssl.use = TRUE;
  956. if(res == CURLE_OK)
  957. /*
  958. * We must duplicate the new URL here as the connection data
  959. * may be free()ed in the Curl_done() function.
  960. */
  961. newurl = conn->newurl?strdup(conn->newurl):NULL;
  962. else
  963. /* The transfer phase returned error, we mark the connection to get
  964. * closed to prevent being re-used. This is becasue we can't
  965. * possibly know if the connection is in a good shape or not now. */
  966. conn->bits.close = TRUE;
  967. /* Always run Curl_done(), even if some of the previous calls
  968. failed, but return the previous (original) error code */
  969. res2 = Curl_done(conn);
  970. if(CURLE_OK == res)
  971. res = res2;
  972. }
  973. /*
  974. * Important: 'conn' cannot be used here, since it may have been closed
  975. * in 'Curl_done' or other functions.
  976. */
  977. if((res == CURLE_OK) && newurl) {
  978. /* Location: redirect
  979. This is assumed to happen for HTTP(S) only!
  980. */
  981. char prot[16]; /* URL protocol string storage */
  982. char letter; /* used for a silly sscanf */
  983. if (data->set.maxredirs && (data->set.followlocation >= data->set.maxredirs)) {
  984. failf(data,"Maximum (%d) redirects followed", data->set.maxredirs);
  985. res=CURLE_TOO_MANY_REDIRECTS;
  986. break;
  987. }
  988. /* mark the next request as a followed location: */
  989. data->state.this_is_a_follow = TRUE;
  990. data->set.followlocation++; /* count location-followers */
  991. if(data->set.http_auto_referer) {
  992. /* We are asked to automatically set the previous URL as the
  993. referer when we get the next URL. We pick the ->url field,
  994. which may or may not be 100% correct */
  995. if(data->change.referer_alloc)
  996. /* If we already have an allocated referer, free this first */
  997. free(data->change.referer);
  998. data->change.referer = strdup(data->change.url);
  999. data->change.referer_alloc = TRUE; /* yes, free this later */
  1000. }
  1001. if(2 != sscanf(newurl, "%15[^?&/:]://%c", prot, &letter)) {
  1002. /***
  1003. *DANG* this is an RFC 2068 violation. The URL is supposed
  1004. to be absolute and this doesn't seem to be that!
  1005. ***
  1006. Instead, we have to TRY to append this new path to the old URL
  1007. to the right of the host part. Oh crap, this is doomed to cause
  1008. problems in the future...
  1009. */
  1010. char *protsep;
  1011. char *pathsep;
  1012. char *newest;
  1013. /* we must make our own copy of the URL to play with, as it may
  1014. point to read-only data */
  1015. char *url_clone=strdup(data->change.url);
  1016. if(!url_clone) {
  1017. res = CURLE_OUT_OF_MEMORY;
  1018. break; /* skip out of this loop NOW */
  1019. }
  1020. /* protsep points to the start of the host name */
  1021. protsep=strstr(url_clone, "//");
  1022. if(!protsep)
  1023. protsep=url_clone;
  1024. else
  1025. protsep+=2; /* pass the slashes */
  1026. if('/' != newurl[0]) {
  1027. /* First we need to find out if there's a ?-letter in the URL,
  1028. and cut it and the right-side of that off */
  1029. pathsep = strrchr(protsep, '?');
  1030. if(pathsep)
  1031. *pathsep=0;
  1032. /* we have a relative path to append to the last slash if
  1033. there's one available */
  1034. pathsep = strrchr(protsep, '/');
  1035. if(pathsep)
  1036. *pathsep=0;
  1037. }
  1038. else {
  1039. /* We got a new absolute path for this server, cut off from the
  1040. first slash */
  1041. pathsep = strchr(protsep, '/');
  1042. if(pathsep)
  1043. *pathsep=0;
  1044. }
  1045. newest=(char *)malloc( strlen(url_clone) +
  1046. 1 + /* possible slash */
  1047. strlen(newurl) + 1/* zero byte */);
  1048. if(!newest) {
  1049. res = CURLE_OUT_OF_MEMORY;
  1050. break; /* go go go out from this loop */
  1051. }
  1052. sprintf(newest, "%s%s%s", url_clone, ('/' == newurl[0])?"":"/",
  1053. newurl);
  1054. free(newurl);
  1055. free(url_clone);
  1056. newurl = newest;
  1057. }
  1058. else
  1059. /* This is an absolute URL, don't allow the custom port number */
  1060. data->state.allow_port = FALSE;
  1061. if(data->change.url_alloc)
  1062. free(data->change.url);
  1063. else
  1064. data->change.url_alloc = TRUE; /* the URL is allocated */
  1065. /* TBD: set the URL with curl_setopt() */
  1066. data->change.url = newurl;
  1067. newurl = NULL; /* don't free! */
  1068. infof(data, "Follows Location: to new URL: '%s'\n", data->change.url);
  1069. /*
  1070. * We get here when the HTTP code is 300-399. We need to perform
  1071. * differently based on exactly what return code there was.
  1072. * Discussed on the curl mailing list and posted about on the 26th
  1073. * of January 2001.
  1074. */
  1075. switch(data->info.httpcode) {
  1076. case 300: /* Multiple Choices */
  1077. case 301: /* Moved Permanently */
  1078. case 306: /* Not used */
  1079. case 307: /* Temporary Redirect */
  1080. default: /* for all unknown ones */
  1081. /* These are explicitly mention since I've checked RFC2616 and they
  1082. * seem to be OK to POST to.
  1083. */
  1084. break;
  1085. case 302: /* Found */
  1086. /* (From 10.3.3)
  1087. Note: RFC 1945 and RFC 2068 specify that the client is not allowed
  1088. to change the method on the redirected request. However, most
  1089. existing user agent implementations treat 302 as if it were a 303
  1090. response, performing a GET on the Location field-value regardless
  1091. of the original request method. The status codes 303 and 307 have
  1092. been added for servers that wish to make unambiguously clear which
  1093. kind of reaction is expected of the client.
  1094. (From 10.3.4)
  1095. Note: Many pre-HTTP/1.1 user agents do not understand the 303
  1096. status. When interoperability with such clients is a concern, the
  1097. 302 status code may be used instead, since most user agents react
  1098. to a 302 response as described here for 303.
  1099. */
  1100. case 303: /* See Other */
  1101. /* Disable both types of POSTs, since doing a second POST when
  1102. * following isn't what anyone would want! */
  1103. data->set.httpreq = HTTPREQ_GET; /* enforce GET request */
  1104. infof(data, "Disables POST, goes with GET\n");
  1105. break;
  1106. case 304: /* Not Modified */
  1107. /* 304 means we did a conditional request and it was "Not modified".
  1108. * We shouldn't get any Location: header in this response!
  1109. */
  1110. break;
  1111. case 305: /* Use Proxy */
  1112. /* (quote from RFC2616, section 10.3.6):
  1113. * "The requested resource MUST be accessed through the proxy given
  1114. * by the Location field. The Location field gives the URI of the
  1115. * proxy. The recipient is expected to repeat this single request
  1116. * via the proxy. 305 responses MUST only be generated by origin
  1117. * servers."
  1118. */
  1119. break;
  1120. }
  1121. continue;
  1122. }
  1123. }
  1124. break; /* it only reaches here when this shouldn't loop */
  1125. } while(1); /* loop if Location: */
  1126. if(newurl)
  1127. free(newurl);
  1128. /* run post-transfer uncondionally, but don't clobber the return code if
  1129. we already have an error code recorder */
  1130. res2 = Curl_posttransfer(data);
  1131. if(!res && res2)
  1132. res = res2;
  1133. return res;
  1134. }
  1135. CURLcode
  1136. Curl_Transfer(struct connectdata *c_conn, /* connection data */
  1137. int sockfd, /* socket to read from or -1 */
  1138. int size, /* -1 if unknown at this point */
  1139. bool getheader, /* TRUE if header parsing is wanted */
  1140. long *bytecountp, /* return number of bytes read or NULL */
  1141. int writesockfd, /* socket to write to, it may very well be
  1142. the same we read from. -1 disables */
  1143. long *writebytecountp /* return number of bytes written or
  1144. NULL */
  1145. )
  1146. {
  1147. struct connectdata *conn = (struct connectdata *)c_conn;
  1148. if(!conn)
  1149. return CURLE_BAD_FUNCTION_ARGUMENT;
  1150. /* now copy all input parameters */
  1151. conn->sockfd = sockfd;
  1152. conn->size = size;
  1153. conn->getheader = getheader;
  1154. conn->bytecountp = bytecountp;
  1155. conn->writesockfd = writesockfd;
  1156. conn->writebytecountp = writebytecountp;
  1157. return CURLE_OK;
  1158. }
  1159. /*
  1160. * local variables:
  1161. * eval: (load-file "../curl-mode.el")
  1162. * end:
  1163. * vim600: fdm=marker
  1164. * vim: et sw=2 ts=2 sts=2 tw=78
  1165. */