http.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <[email protected]>, 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. #ifndef CURL_DISABLE_HTTP
  25. /* -- WIN32 approved -- */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <errno.h>
  34. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  35. #include <time.h>
  36. #include <io.h>
  37. #else
  38. #ifdef HAVE_SYS_SOCKET_H
  39. #include <sys/socket.h>
  40. #endif
  41. #ifdef HAVE_NETINET_IN_H
  42. #include <netinet/in.h>
  43. #endif
  44. #include <sys/time.h>
  45. #ifdef HAVE_TIME_H
  46. #ifdef TIME_WITH_SYS_TIME
  47. #include <time.h>
  48. #endif
  49. #endif
  50. #ifdef HAVE_UNISTD_H
  51. #include <unistd.h>
  52. #endif
  53. #include <netdb.h>
  54. #ifdef HAVE_ARPA_INET_H
  55. #include <arpa/inet.h>
  56. #endif
  57. #ifdef HAVE_NET_IF_H
  58. #include <net/if.h>
  59. #endif
  60. #include <sys/ioctl.h>
  61. #include <signal.h>
  62. #ifdef HAVE_SYS_PARAM_H
  63. #include <sys/param.h>
  64. #endif
  65. #ifdef HAVE_SYS_SELECT_H
  66. #include <sys/select.h>
  67. #endif
  68. #endif
  69. #include "urldata.h"
  70. #include <curl/curl.h>
  71. #include "transfer.h"
  72. #include "sendf.h"
  73. #include "formdata.h"
  74. #include "progress.h"
  75. #include "base64.h"
  76. #include "cookie.h"
  77. #include "strequal.h"
  78. #include "ssluse.h"
  79. #include "http_digest.h"
  80. #include "http_ntlm.h"
  81. #include "http_negotiate.h"
  82. #include "url.h"
  83. #include "share.h"
  84. #include "hostip.h"
  85. #include "http.h"
  86. #include "curl_memory.h"
  87. #define _MPRINTF_REPLACE /* use our functions only */
  88. #include <curl/mprintf.h>
  89. /* The last #include file should be: */
  90. #include "memdebug.h"
  91. /*
  92. * checkheaders() checks the linked list of custom HTTP headers for a
  93. * particular header (prefix).
  94. *
  95. * Returns a pointer to the first matching header or NULL if none matched.
  96. */
  97. static char *checkheaders(struct SessionHandle *data, const char *thisheader)
  98. {
  99. struct curl_slist *head;
  100. size_t thislen = strlen(thisheader);
  101. for(head = data->set.headers; head; head=head->next) {
  102. if(strnequal(head->data, thisheader, thislen))
  103. return head->data;
  104. }
  105. return NULL;
  106. }
  107. /*
  108. * Curl_output_basic() sets up an Authorization: header (or the proxy version)
  109. * for HTTP Basic authentication.
  110. *
  111. * Returns CURLcode.
  112. */
  113. static CURLcode Curl_output_basic(struct connectdata *conn, bool proxy)
  114. {
  115. char *authorization;
  116. struct SessionHandle *data=conn->data;
  117. char **userp;
  118. char *user;
  119. char *pwd;
  120. if(proxy) {
  121. userp = &conn->allocptr.proxyuserpwd;
  122. user = conn->proxyuser;
  123. pwd = conn->proxypasswd;
  124. }
  125. else {
  126. userp = &conn->allocptr.userpwd;
  127. user = conn->user;
  128. pwd = conn->passwd;
  129. }
  130. snprintf(data->state.buffer, sizeof(data->state.buffer), "%s:%s", user, pwd);
  131. if(Curl_base64_encode(data->state.buffer,
  132. strlen(data->state.buffer),
  133. &authorization) > 0) {
  134. if(*userp)
  135. free(*userp);
  136. *userp = aprintf( "%sAuthorization: Basic %s\015\012",
  137. proxy?"Proxy-":"",
  138. authorization);
  139. free(authorization);
  140. }
  141. else
  142. return CURLE_OUT_OF_MEMORY;
  143. return CURLE_OK;
  144. }
  145. /* pickoneauth() selects the most favourable authentication method from the
  146. * ones available and the ones we want.
  147. *
  148. * return TRUE if one was picked
  149. */
  150. static bool pickoneauth(struct auth *pick)
  151. {
  152. bool picked;
  153. /* only deal with authentication we want */
  154. long avail = pick->avail & pick->want;
  155. picked = TRUE;
  156. /* The order of these checks is highly relevant, as this will be the order
  157. of preference in case of the existance of multiple accepted types. */
  158. if(avail & CURLAUTH_GSSNEGOTIATE)
  159. pick->picked = CURLAUTH_GSSNEGOTIATE;
  160. else if(avail & CURLAUTH_DIGEST)
  161. pick->picked = CURLAUTH_DIGEST;
  162. else if(avail & CURLAUTH_NTLM)
  163. pick->picked = CURLAUTH_NTLM;
  164. else if(avail & CURLAUTH_BASIC)
  165. pick->picked = CURLAUTH_BASIC;
  166. else {
  167. pick->picked = CURLAUTH_PICKNONE; /* we select to use nothing */
  168. picked = FALSE;
  169. }
  170. pick->avail = CURLAUTH_NONE; /* clear it here */
  171. return picked;
  172. }
  173. /*
  174. * Curl_http_auth_act() gets called when a all HTTP headers have been received
  175. * and it checks what authentication methods that are available and decides
  176. * which one (if any) to use. It will set 'newurl' if an auth metod was
  177. * picked.
  178. */
  179. CURLcode Curl_http_auth_act(struct connectdata *conn)
  180. {
  181. struct SessionHandle *data = conn->data;
  182. bool pickhost = FALSE;
  183. bool pickproxy = FALSE;
  184. CURLcode code = CURLE_OK;
  185. if(data->state.authproblem)
  186. return data->set.http_fail_on_error?CURLE_HTTP_RETURNED_ERROR:CURLE_OK;
  187. if(conn->bits.user_passwd &&
  188. ((conn->keep.httpcode == 401) ||
  189. (conn->bits.authprobe && conn->keep.httpcode < 300))) {
  190. pickhost = pickoneauth(&data->state.authhost);
  191. if(!pickhost)
  192. data->state.authproblem = TRUE;
  193. }
  194. if(conn->bits.proxy_user_passwd &&
  195. ((conn->keep.httpcode == 407) ||
  196. (conn->bits.authprobe && conn->keep.httpcode < 300))) {
  197. pickproxy = pickoneauth(&data->state.authproxy);
  198. if(!pickproxy)
  199. data->state.authproblem = TRUE;
  200. }
  201. if(pickhost || pickproxy)
  202. conn->newurl = strdup(data->change.url); /* clone URL */
  203. else if((conn->keep.httpcode < 300) &&
  204. (!data->state.authhost.done) &&
  205. conn->bits.authprobe) {
  206. /* no (known) authentication available,
  207. authentication is not "done" yet and
  208. no authentication seems to be required and
  209. we didn't try HEAD or GET */
  210. if((data->set.httpreq != HTTPREQ_GET) &&
  211. (data->set.httpreq != HTTPREQ_HEAD)) {
  212. conn->newurl = strdup(data->change.url); /* clone URL */
  213. data->state.authhost.done = TRUE;
  214. }
  215. }
  216. if (Curl_http_should_fail(conn)) {
  217. failf (data, "The requested URL returned error: %d",
  218. conn->keep.httpcode);
  219. code = CURLE_HTTP_RETURNED_ERROR;
  220. }
  221. return code;
  222. }
  223. /**
  224. * Curl_http_output_auth() setups the authentication headers for the
  225. * host/proxy and the correct authentication
  226. * method. conn->data->state.authdone is set to TRUE when authentication is
  227. * done.
  228. *
  229. * @param conn all information about the current connection
  230. * @param request pointer to the request keyword
  231. * @param path pointer to the requested path
  232. * @param proxytunnel boolean if this is the request setting up a "proxy
  233. * tunnel"
  234. *
  235. * @returns CURLcode
  236. */
  237. static CURLcode
  238. Curl_http_output_auth(struct connectdata *conn,
  239. char *request,
  240. char *path,
  241. bool proxytunnel) /* TRUE if this is the request setting
  242. up the proxy tunnel */
  243. {
  244. CURLcode result = CURLE_OK;
  245. struct SessionHandle *data = conn->data;
  246. char *auth=NULL;
  247. curlassert(data);
  248. if((conn->bits.httpproxy && conn->bits.proxy_user_passwd) ||
  249. conn->bits.user_passwd)
  250. /* continue please */ ;
  251. else {
  252. data->state.authhost.done = TRUE;
  253. data->state.authproxy.done = TRUE;
  254. return CURLE_OK; /* no authentication with no user or password */
  255. }
  256. if(data->state.authhost.want && !data->state.authhost.picked)
  257. /* The app has selected one or more methods, but none has been picked
  258. so far by a server round-trip. Then we set the picked one to the
  259. want one, and if this is one single bit it'll be used instantly. */
  260. data->state.authhost.picked = data->state.authhost.want;
  261. if(data->state.authproxy.want && !data->state.authproxy.picked)
  262. /* The app has selected one or more methods, but none has been picked so
  263. far by a proxy round-trip. Then we set the picked one to the want one,
  264. and if this is one single bit it'll be used instantly. */
  265. data->state.authproxy.picked = data->state.authproxy.want;
  266. /* To prevent the user+password to get sent to other than the original
  267. host due to a location-follow, we do some weirdo checks here */
  268. if(!data->state.this_is_a_follow ||
  269. !data->state.auth_host ||
  270. curl_strequal(data->state.auth_host, conn->host.name) ||
  271. data->set.http_disable_hostname_check_before_authentication) {
  272. /* Send proxy authentication header if needed */
  273. if (conn->bits.httpproxy &&
  274. (conn->bits.tunnel_proxy == proxytunnel)) {
  275. #ifdef USE_SSLEAY
  276. if(data->state.authproxy.want == CURLAUTH_NTLM) {
  277. auth=(char *)"NTLM";
  278. result = Curl_output_ntlm(conn, TRUE);
  279. if(result)
  280. return result;
  281. }
  282. else
  283. #endif
  284. if(data->state.authproxy.want == CURLAUTH_BASIC) {
  285. /* Basic */
  286. if(conn->bits.proxy_user_passwd &&
  287. !checkheaders(data, "Proxy-authorization:")) {
  288. auth=(char *)"Basic";
  289. result = Curl_output_basic(conn, TRUE);
  290. if(result)
  291. return result;
  292. }
  293. data->state.authproxy.done = TRUE;
  294. }
  295. else if(data->state.authproxy.want == CURLAUTH_DIGEST) {
  296. auth=(char *)"Digest";
  297. result = Curl_output_digest(conn,
  298. TRUE, /* proxy */
  299. (unsigned char *)request,
  300. (unsigned char *)path);
  301. if(result)
  302. return result;
  303. }
  304. infof(data, "Proxy auth using %s with user '%s'\n",
  305. auth, conn->proxyuser?conn->proxyuser:"");
  306. }
  307. else
  308. /* we have no proxy so let's pretend we're done authenticating
  309. with it */
  310. data->state.authproxy.done = TRUE;
  311. /* Send web authentication header if needed */
  312. {
  313. auth = NULL;
  314. #ifdef HAVE_GSSAPI
  315. if((data->state.authhost.want == CURLAUTH_GSSNEGOTIATE) &&
  316. data->state.negotiate.context &&
  317. !GSS_ERROR(data->state.negotiate.status)) {
  318. auth=(char *)"GSS-Negotiate";
  319. result = Curl_output_negotiate(conn);
  320. if (result)
  321. return result;
  322. data->state.authhost.done = TRUE;
  323. }
  324. else
  325. #endif
  326. #ifdef USE_SSLEAY
  327. if(data->state.authhost.picked == CURLAUTH_NTLM) {
  328. auth=(char *)"NTLM";
  329. result = Curl_output_ntlm(conn, FALSE);
  330. if(result)
  331. return result;
  332. }
  333. else
  334. #endif
  335. {
  336. if(data->state.authhost.picked == CURLAUTH_DIGEST) {
  337. auth=(char *)"Digest";
  338. result = Curl_output_digest(conn,
  339. FALSE, /* not a proxy */
  340. (unsigned char *)request,
  341. (unsigned char *)path);
  342. if(result)
  343. return result;
  344. }
  345. else if(data->state.authhost.picked == CURLAUTH_BASIC) {
  346. if(conn->bits.user_passwd &&
  347. !checkheaders(data, "Authorization:")) {
  348. auth=(char *)"Basic";
  349. result = Curl_output_basic(conn, FALSE);
  350. if(result)
  351. return result;
  352. }
  353. /* basic is always ready */
  354. data->state.authhost.done = TRUE;
  355. }
  356. }
  357. if(auth)
  358. infof(data, "Server auth using %s with user '%s'\n",
  359. auth, conn->user);
  360. }
  361. }
  362. else
  363. data->state.authhost.done = TRUE;
  364. return result;
  365. }
  366. /*
  367. * Curl_http_input_auth() deals with Proxy-Authenticate: and WWW-Authenticate:
  368. * headers. They are dealt with both in the transfer.c main loop and in the
  369. * proxy CONNECT loop.
  370. */
  371. CURLcode Curl_http_input_auth(struct connectdata *conn,
  372. int httpcode,
  373. char *header) /* the first non-space */
  374. {
  375. /*
  376. * This resource requires authentication
  377. */
  378. struct SessionHandle *data = conn->data;
  379. long *availp;
  380. char *start;
  381. struct auth *authp;
  382. if (httpcode == 407) {
  383. start = header+strlen("Proxy-authenticate:");
  384. availp = &data->info.proxyauthavail;
  385. authp = &data->state.authproxy;
  386. }
  387. else {
  388. start = header+strlen("WWW-Authenticate:");
  389. availp = &data->info.httpauthavail;
  390. authp = &data->state.authhost;
  391. }
  392. /* pass all white spaces */
  393. while(*start && isspace((int)*start))
  394. start++;
  395. /*
  396. * Here we check if we want the specific single authentiction (using ==) and
  397. * if we do, we initiate usage of it.
  398. *
  399. * If the provided authentication is wanted as one out of several accepted
  400. * types (using &), we OR this authenticaion type to the authavail
  401. * variable.
  402. */
  403. #ifdef HAVE_GSSAPI
  404. if (checkprefix("GSS-Negotiate", start) ||
  405. checkprefix("Negotiate", start)) {
  406. *availp |= CURLAUTH_GSSNEGOTIATE;
  407. authp->avail |= CURLAUTH_GSSNEGOTIATE;
  408. if(authp->picked == CURLAUTH_GSSNEGOTIATE) {
  409. /* if exactly this is wanted, go */
  410. int neg = Curl_input_negotiate(conn, start);
  411. if (neg == 0) {
  412. conn->newurl = strdup(data->change.url);
  413. data->state.authproblem = (conn->newurl == NULL);
  414. }
  415. else {
  416. infof(data, "Authentication problem. Ignoring this.\n");
  417. data->state.authproblem = TRUE;
  418. }
  419. }
  420. }
  421. else
  422. #endif
  423. #ifdef USE_SSLEAY
  424. /* NTLM support requires the SSL crypto libs */
  425. if(checkprefix("NTLM", start)) {
  426. *availp |= CURLAUTH_NTLM;
  427. authp->avail |= CURLAUTH_NTLM;
  428. if(authp->picked == CURLAUTH_NTLM) {
  429. /* NTLM authentication is picked and activated */
  430. CURLntlm ntlm =
  431. Curl_input_ntlm(conn, (bool)(httpcode == 407), start);
  432. if(CURLNTLM_BAD != ntlm)
  433. data->state.authproblem = FALSE;
  434. else {
  435. infof(data, "Authentication problem. Ignoring this.\n");
  436. data->state.authproblem = TRUE;
  437. }
  438. }
  439. }
  440. else
  441. #endif
  442. if(checkprefix("Digest", start)) {
  443. CURLdigest dig;
  444. *availp |= CURLAUTH_DIGEST;
  445. authp->avail |= CURLAUTH_DIGEST;
  446. /* We call this function on input Digest headers even if Digest
  447. * authentication isn't activated yet, as we need to store the
  448. * incoming data from this header in case we are gonna use Digest. */
  449. dig = Curl_input_digest(conn, (bool)(httpcode == 407), start);
  450. if(CURLDIGEST_FINE != dig) {
  451. infof(data, "Authentication problem. Ignoring this.\n");
  452. data->state.authproblem = TRUE;
  453. }
  454. }
  455. else if(checkprefix("Basic", start)) {
  456. *availp |= CURLAUTH_BASIC;
  457. authp->avail |= CURLAUTH_BASIC;
  458. if(authp->picked == CURLAUTH_BASIC) {
  459. /* We asked for Basic authentication but got a 40X back
  460. anyway, which basicly means our name+password isn't
  461. valid. */
  462. authp->avail = CURLAUTH_NONE;
  463. infof(data, "Authentication problem. Ignoring this.\n");
  464. data->state.authproblem = TRUE;
  465. }
  466. }
  467. return CURLE_OK;
  468. }
  469. /**
  470. * Curl_http_should_fail() determines whether an HTTP response has gotten us
  471. * into an error state or not.
  472. *
  473. * @param conn all information about the current connection
  474. *
  475. * @retval 0 communications should continue
  476. *
  477. * @retval 1 communications should not continue
  478. */
  479. int Curl_http_should_fail(struct connectdata *conn)
  480. {
  481. struct SessionHandle *data;
  482. struct Curl_transfer_keeper *k;
  483. curlassert(conn);
  484. data = conn->data;
  485. curlassert(data);
  486. /*
  487. ** For readability
  488. */
  489. k = &conn->keep;
  490. /*
  491. ** If we haven't been asked to fail on error,
  492. ** don't fail.
  493. */
  494. if (!data->set.http_fail_on_error)
  495. return 0;
  496. /*
  497. ** Any code < 400 is never terminal.
  498. */
  499. if (k->httpcode < 400)
  500. return 0;
  501. /*
  502. ** Any code >= 400 that's not 401 or 407 is always
  503. ** a terminal error
  504. */
  505. if ((k->httpcode != 401) &&
  506. (k->httpcode != 407))
  507. return 1;
  508. /*
  509. ** All we have left to deal with is 401 and 407
  510. */
  511. curlassert((k->httpcode == 401) || (k->httpcode == 407));
  512. /*
  513. ** Examine the current authentication state to see if this
  514. ** is an error. The idea is for this function to get
  515. ** called after processing all the headers in a response
  516. ** message. So, if we've been to asked to authenticate a
  517. ** particular stage, and we've done it, we're OK. But, if
  518. ** we're already completely authenticated, it's not OK to
  519. ** get another 401 or 407.
  520. **
  521. ** It is possible for authentication to go stale such that
  522. ** the client needs to reauthenticate. Once that info is
  523. ** available, use it here.
  524. */
  525. #if 0 /* set to 1 when debugging this functionality */
  526. infof(data,"%s: authstage = %d\n",__FUNCTION__,data->state.authstage);
  527. infof(data,"%s: authwant = 0x%08x\n",__FUNCTION__,data->state.authwant);
  528. infof(data,"%s: authavail = 0x%08x\n",__FUNCTION__,data->state.authavail);
  529. infof(data,"%s: httpcode = %d\n",__FUNCTION__,k->httpcode);
  530. infof(data,"%s: authdone = %d\n",__FUNCTION__,data->state.authdone);
  531. infof(data,"%s: newurl = %s\n",__FUNCTION__,conn->newurl ? conn->newurl : "(null)");
  532. infof(data,"%s: authproblem = %d\n",__FUNCTION__,data->state.authproblem);
  533. #endif
  534. /*
  535. ** Either we're not authenticating, or we're supposed to
  536. ** be authenticating something else. This is an error.
  537. */
  538. if((k->httpcode == 401) && !conn->bits.user_passwd)
  539. return TRUE;
  540. if((k->httpcode == 407) && !conn->bits.proxy_user_passwd)
  541. return TRUE;
  542. return data->state.authproblem;
  543. }
  544. /*
  545. * readmoredata() is a "fread() emulation" to provide POST and/or request
  546. * data. It is used when a huge POST is to be made and the entire chunk wasn't
  547. * sent in the first send(). This function will then be called from the
  548. * transfer.c loop when more data is to be sent to the peer.
  549. *
  550. * Returns the amount of bytes it filled the buffer with.
  551. */
  552. static size_t readmoredata(char *buffer,
  553. size_t size,
  554. size_t nitems,
  555. void *userp)
  556. {
  557. struct connectdata *conn = (struct connectdata *)userp;
  558. struct HTTP *http = conn->proto.http;
  559. size_t fullsize = size * nitems;
  560. if(0 == http->postsize)
  561. /* nothing to return */
  562. return 0;
  563. /* make sure that a HTTP request is never sent away chunked! */
  564. conn->bits.forbidchunk= (http->sending == HTTPSEND_REQUEST)?TRUE:FALSE;
  565. if(http->postsize <= (curl_off_t)fullsize) {
  566. memcpy(buffer, http->postdata, (size_t)http->postsize);
  567. fullsize = (size_t)http->postsize;
  568. if(http->backup.postsize) {
  569. /* move backup data into focus and continue on that */
  570. http->postdata = http->backup.postdata;
  571. http->postsize = http->backup.postsize;
  572. conn->fread = http->backup.fread;
  573. conn->fread_in = http->backup.fread_in;
  574. http->sending++; /* move one step up */
  575. http->backup.postsize=0;
  576. }
  577. else
  578. http->postsize = 0;
  579. return fullsize;
  580. }
  581. memcpy(buffer, http->postdata, fullsize);
  582. http->postdata += fullsize;
  583. http->postsize -= fullsize;
  584. return fullsize;
  585. }
  586. /* ------------------------------------------------------------------------- */
  587. /*
  588. * The add_buffer series of functions are used to build one large memory chunk
  589. * from repeated function invokes. Used so that the entire HTTP request can
  590. * be sent in one go.
  591. */
  592. struct send_buffer {
  593. char *buffer;
  594. size_t size_max;
  595. size_t size_used;
  596. };
  597. typedef struct send_buffer send_buffer;
  598. static CURLcode
  599. add_buffer(send_buffer *in, const void *inptr, size_t size);
  600. /*
  601. * add_buffer_init() sets up and returns a fine buffer struct
  602. */
  603. static
  604. send_buffer *add_buffer_init(void)
  605. {
  606. send_buffer *blonk;
  607. blonk=(send_buffer *)malloc(sizeof(send_buffer));
  608. if(blonk) {
  609. memset(blonk, 0, sizeof(send_buffer));
  610. return blonk;
  611. }
  612. return NULL; /* failed, go home */
  613. }
  614. /*
  615. * add_buffer_send() sends a buffer and frees all associated memory.
  616. *
  617. * Returns CURLcode
  618. */
  619. static
  620. CURLcode add_buffer_send(send_buffer *in,
  621. struct connectdata *conn,
  622. long *bytes_written) /* add the number of sent
  623. bytes to this counter */
  624. {
  625. ssize_t amount;
  626. CURLcode res;
  627. char *ptr;
  628. size_t size;
  629. struct HTTP *http = conn->proto.http;
  630. size_t sendsize;
  631. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  632. /* The looping below is required since we use non-blocking sockets, but due
  633. to the circumstances we will just loop and try again and again etc */
  634. ptr = in->buffer;
  635. size = in->size_used;
  636. if(conn->protocol & PROT_HTTPS) {
  637. /* We never send more than CURL_MAX_WRITE_SIZE bytes in one single chunk
  638. when we speak HTTPS, as if only a fraction of it is sent now, this data
  639. needs to fit into the normal read-callback buffer later on and that
  640. buffer is using this size.
  641. */
  642. sendsize= (size > CURL_MAX_WRITE_SIZE)?CURL_MAX_WRITE_SIZE:size;
  643. /* OpenSSL is very picky and we must send the SAME buffer pointer to the
  644. library when we attempt to re-send this buffer. Sending the same data
  645. is not enough, we must use the exact same address. For this reason, we
  646. must copy the data to the uploadbuffer first, since that is the buffer
  647. we will be using if this send is retried later.
  648. */
  649. memcpy(conn->data->state.uploadbuffer, ptr, sendsize);
  650. ptr = conn->data->state.uploadbuffer;
  651. }
  652. else
  653. sendsize = size;
  654. res = Curl_write(conn, sockfd, ptr, sendsize, &amount);
  655. if(CURLE_OK == res) {
  656. if(conn->data->set.verbose)
  657. /* this data _may_ contain binary stuff */
  658. Curl_debug(conn->data, CURLINFO_HEADER_OUT, ptr, amount,
  659. conn->host.dispname);
  660. *bytes_written += amount;
  661. if((size_t)amount != size) {
  662. /* The whole request could not be sent in one system call. We must queue
  663. it up and send it later when we get the chance. We must not loop here
  664. and wait until it might work again. */
  665. size -= amount;
  666. ptr = in->buffer + amount;
  667. /* backup the currently set pointers */
  668. http->backup.fread = conn->fread;
  669. http->backup.fread_in = conn->fread_in;
  670. http->backup.postdata = http->postdata;
  671. http->backup.postsize = http->postsize;
  672. /* set the new pointers for the request-sending */
  673. conn->fread = (curl_read_callback)readmoredata;
  674. conn->fread_in = (void *)conn;
  675. http->postdata = ptr;
  676. http->postsize = (curl_off_t)size;
  677. http->send_buffer = in;
  678. http->sending = HTTPSEND_REQUEST;
  679. return CURLE_OK;
  680. }
  681. http->sending = HTTPSEND_BODY;
  682. /* the full buffer was sent, clean up and return */
  683. }
  684. if(in->buffer)
  685. free(in->buffer);
  686. free(in);
  687. return res;
  688. }
  689. /*
  690. * add_bufferf() add the formatted input to the buffer.
  691. */
  692. static
  693. CURLcode add_bufferf(send_buffer *in, const char *fmt, ...)
  694. {
  695. char *s;
  696. va_list ap;
  697. va_start(ap, fmt);
  698. s = vaprintf(fmt, ap); /* this allocs a new string to append */
  699. va_end(ap);
  700. if(s) {
  701. CURLcode result = add_buffer(in, s, strlen(s));
  702. free(s);
  703. if(CURLE_OK == result)
  704. return CURLE_OK;
  705. }
  706. /* If we failed, we cleanup the whole buffer and return error */
  707. if(in->buffer)
  708. free(in->buffer);
  709. free(in);
  710. return CURLE_OUT_OF_MEMORY;
  711. }
  712. /*
  713. * add_buffer() appends a memory chunk to the existing buffer
  714. */
  715. static
  716. CURLcode add_buffer(send_buffer *in, const void *inptr, size_t size)
  717. {
  718. char *new_rb;
  719. size_t new_size;
  720. if(!in->buffer ||
  721. ((in->size_used + size) > (in->size_max - 1))) {
  722. new_size = (in->size_used+size)*2;
  723. if(in->buffer)
  724. /* we have a buffer, enlarge the existing one */
  725. new_rb = (char *)realloc(in->buffer, new_size);
  726. else
  727. /* create a new buffer */
  728. new_rb = (char *)malloc(new_size);
  729. if(!new_rb)
  730. return CURLE_OUT_OF_MEMORY;
  731. in->buffer = new_rb;
  732. in->size_max = new_size;
  733. }
  734. memcpy(&in->buffer[in->size_used], inptr, size);
  735. in->size_used += size;
  736. return CURLE_OK;
  737. }
  738. /* end of the add_buffer functions */
  739. /* ------------------------------------------------------------------------- */
  740. /*
  741. * Curl_compareheader()
  742. *
  743. * Returns TRUE if 'headerline' contains the 'header' with given 'content'.
  744. * Pass headers WITH the colon.
  745. */
  746. bool
  747. Curl_compareheader(char *headerline, /* line to check */
  748. const char *header, /* header keyword _with_ colon */
  749. const char *content) /* content string to find */
  750. {
  751. /* RFC2616, section 4.2 says: "Each header field consists of a name followed
  752. * by a colon (":") and the field value. Field names are case-insensitive.
  753. * The field value MAY be preceded by any amount of LWS, though a single SP
  754. * is preferred." */
  755. size_t hlen = strlen(header);
  756. size_t clen;
  757. size_t len;
  758. char *start;
  759. char *end;
  760. if(!strnequal(headerline, header, hlen))
  761. return FALSE; /* doesn't start with header */
  762. /* pass the header */
  763. start = &headerline[hlen];
  764. /* pass all white spaces */
  765. while(*start && isspace((int)*start))
  766. start++;
  767. /* find the end of the header line */
  768. end = strchr(start, '\r'); /* lines end with CRLF */
  769. if(!end) {
  770. /* in case there's a non-standard compliant line here */
  771. end = strchr(start, '\n');
  772. if(!end)
  773. /* hm, there's no line ending here, use the zero byte! */
  774. end = strchr(start, '\0');
  775. }
  776. len = end-start; /* length of the content part of the input line */
  777. clen = strlen(content); /* length of the word to find */
  778. /* find the content string in the rest of the line */
  779. for(;len>=clen;len--, start++) {
  780. if(strnequal(start, content, clen))
  781. return TRUE; /* match! */
  782. }
  783. return FALSE; /* no match */
  784. }
  785. /*
  786. * ConnectHTTPProxyTunnel() requires that we're connected to a HTTP proxy. This
  787. * function will issue the necessary commands to get a seamless tunnel through
  788. * this proxy. After that, the socket can be used just as a normal socket.
  789. */
  790. CURLcode Curl_ConnectHTTPProxyTunnel(struct connectdata *conn,
  791. int sockindex,
  792. char *hostname,
  793. int remote_port)
  794. {
  795. int subversion=0;
  796. struct SessionHandle *data=conn->data;
  797. struct Curl_transfer_keeper *k = &conn->keep;
  798. CURLcode result;
  799. int res;
  800. size_t nread; /* total size read */
  801. int perline; /* count bytes per line */
  802. bool keepon=TRUE;
  803. ssize_t gotbytes;
  804. char *ptr;
  805. long timeout; /* default timeout in seconds */
  806. struct timeval interval;
  807. fd_set rkeepfd;
  808. fd_set readfd;
  809. char *line_start;
  810. char *host_port;
  811. curl_socket_t tunnelsocket = conn->sock[sockindex];
  812. #define SELECT_OK 0
  813. #define SELECT_ERROR 1
  814. #define SELECT_TIMEOUT 2
  815. int error = SELECT_OK;
  816. infof(data, "Establish HTTP proxy tunnel to %s:%d\n", hostname, remote_port);
  817. do {
  818. if(conn->newurl) {
  819. /* This only happens if we've looped here due to authentication reasons,
  820. and we don't really use the newly cloned URL here then. Just free()
  821. it. */
  822. free(conn->newurl);
  823. conn->newurl = NULL;
  824. }
  825. host_port = aprintf("%s:%d", hostname, remote_port);
  826. if(!host_port)
  827. return CURLE_OUT_OF_MEMORY;
  828. /* Setup the proxy-authorization header, if any */
  829. result = Curl_http_output_auth(conn, (char *)"CONNECT", host_port, TRUE);
  830. if(CURLE_OK == result) {
  831. /* OK, now send the connect request to the proxy */
  832. result =
  833. Curl_sendf(tunnelsocket, conn,
  834. "CONNECT %s:%d HTTP/1.0\015\012"
  835. "%s"
  836. "%s"
  837. "\r\n",
  838. hostname, remote_port,
  839. conn->bits.proxy_user_passwd?
  840. conn->allocptr.proxyuserpwd:"",
  841. data->set.useragent?conn->allocptr.uagent:""
  842. );
  843. if(result)
  844. failf(data, "Failed sending CONNECT to proxy");
  845. }
  846. free(host_port);
  847. if(result)
  848. return result;
  849. FD_ZERO (&readfd); /* clear it */
  850. FD_SET (tunnelsocket, &readfd); /* read socket */
  851. /* get this in a backup variable to be able to restore it on each lap in
  852. the select() loop */
  853. rkeepfd = readfd;
  854. ptr=data->state.buffer;
  855. line_start = ptr;
  856. nread=0;
  857. perline=0;
  858. while((nread<BUFSIZE) && (keepon && !error)) {
  859. readfd = rkeepfd; /* set every lap */
  860. interval.tv_sec = 1; /* timeout each second and check the timeout */
  861. interval.tv_usec = 0;
  862. if(data->set.timeout) {
  863. /* if timeout is requested, find out how much remaining time we have */
  864. timeout = data->set.timeout - /* timeout time */
  865. Curl_tvdiff(Curl_tvnow(), conn->now)/1000; /* spent time */
  866. if(timeout <=0 ) {
  867. failf(data, "Proxy connection aborted due to timeout");
  868. error = SELECT_TIMEOUT; /* already too little time */
  869. break;
  870. }
  871. }
  872. switch (select (tunnelsocket+1, &readfd, NULL, NULL, &interval)) {
  873. case -1: /* select() error, stop reading */
  874. error = SELECT_ERROR;
  875. failf(data, "Proxy CONNECT aborted due to select() error");
  876. break;
  877. case 0: /* timeout */
  878. break;
  879. default:
  880. /*
  881. * This code previously didn't use the kerberos sec_read() code
  882. * to read, but when we use Curl_read() it may do so. Do confirm
  883. * that this is still ok and then remove this comment!
  884. */
  885. res= Curl_read(conn, tunnelsocket, ptr, BUFSIZE-nread, &gotbytes);
  886. if(res< 0)
  887. /* EWOULDBLOCK */
  888. continue; /* go loop yourself */
  889. else if(res)
  890. keepon = FALSE;
  891. else if(gotbytes <= 0) {
  892. keepon = FALSE;
  893. error = SELECT_ERROR;
  894. failf(data, "Proxy CONNECT aborted");
  895. }
  896. else {
  897. /*
  898. * We got a whole chunk of data, which can be anything from one byte
  899. * to a set of lines and possibly just a piece of the last line.
  900. *
  901. * TODO: To make this code work less error-prone, we need to make
  902. * sure that we read and create full lines before we compare them,
  903. * as there is really nothing that stops the proxy from delivering
  904. * the response lines in multiple parts, each part consisting of
  905. * only a little piece of the line(s). */
  906. int i;
  907. nread += gotbytes;
  908. for(i = 0; i < gotbytes; ptr++, i++) {
  909. perline++; /* amount of bytes in this line so far */
  910. if(*ptr=='\n') {
  911. char letter;
  912. int writetype;
  913. /* output debug output if that is requested */
  914. if(data->set.verbose)
  915. Curl_debug(data, CURLINFO_HEADER_IN, line_start, perline,
  916. conn->host.dispname);
  917. /* send the header to the callback */
  918. writetype = CLIENTWRITE_HEADER;
  919. if(data->set.include_header)
  920. writetype |= CLIENTWRITE_BODY;
  921. result = Curl_client_write(data, writetype, line_start, perline);
  922. if(result)
  923. return result;
  924. /* Newlines are CRLF, so the CR is ignored as the line isn't
  925. really terminated until the LF comes. Treat a following CR
  926. as end-of-headers as well.*/
  927. if(('\r' == line_start[0]) ||
  928. ('\n' == line_start[0])) {
  929. /* end of response-headers from the proxy */
  930. keepon=FALSE;
  931. break; /* breaks out of for-loop, not switch() */
  932. }
  933. /* keep a backup of the position we are about to blank */
  934. letter = line_start[perline];
  935. line_start[perline]=0; /* zero terminate the buffer */
  936. if((checkprefix("WWW-Authenticate:", line_start) &&
  937. (401 == k->httpcode)) ||
  938. (checkprefix("Proxy-authenticate:", line_start) &&
  939. (407 == k->httpcode))) {
  940. result = Curl_http_input_auth(conn, k->httpcode, line_start);
  941. if(result)
  942. return result;
  943. }
  944. else if(2 == sscanf(line_start, "HTTP/1.%d %d",
  945. &subversion,
  946. &k->httpcode)) {
  947. /* store the HTTP code from the proxy */
  948. data->info.httpproxycode = k->httpcode;
  949. }
  950. /* put back the letter we blanked out before */
  951. line_start[perline]= letter;
  952. perline=0; /* line starts over here */
  953. line_start = ptr+1; /* this skips the zero byte we wrote */
  954. }
  955. }
  956. }
  957. break;
  958. } /* switch */
  959. } /* while there's buffer left and loop is requested */
  960. if(error)
  961. return CURLE_RECV_ERROR;
  962. if(data->info.httpproxycode != 200)
  963. /* Deal with the possibly already received authenticate
  964. headers. 'newurl' is set to a new URL if we must loop. */
  965. Curl_http_auth_act(conn);
  966. } while(conn->newurl);
  967. if(200 != k->httpcode) {
  968. failf(data, "Received HTTP code %d from proxy after CONNECT",
  969. k->httpcode);
  970. return CURLE_RECV_ERROR;
  971. }
  972. /* If a proxy-authorization header was used for the proxy, then we should
  973. make sure that it isn't accidentally used for the document request
  974. after we've connected. So let's free and clear it here. */
  975. Curl_safefree(conn->allocptr.proxyuserpwd);
  976. conn->allocptr.proxyuserpwd = NULL;
  977. data->state.authproxy.done = TRUE;
  978. infof (data, "Proxy replied OK to CONNECT request\n");
  979. return CURLE_OK;
  980. }
  981. /*
  982. * Curl_http_connect() performs HTTP stuff to do at connect-time, called from
  983. * the generic Curl_connect().
  984. */
  985. CURLcode Curl_http_connect(struct connectdata *conn)
  986. {
  987. struct SessionHandle *data;
  988. CURLcode result;
  989. data=conn->data;
  990. /* If we are not using a proxy and we want a secure connection, perform SSL
  991. * initialization & connection now. If using a proxy with https, then we
  992. * must tell the proxy to CONNECT to the host we want to talk to. Only
  993. * after the connect has occured, can we start talking SSL
  994. */
  995. if(conn->bits.tunnel_proxy) {
  996. /* either SSL over proxy, or explicitly asked for */
  997. result = Curl_ConnectHTTPProxyTunnel(conn, FIRSTSOCKET,
  998. conn->host.name,
  999. conn->remote_port);
  1000. if(CURLE_OK != result)
  1001. return result;
  1002. }
  1003. if(conn->protocol & PROT_HTTPS) {
  1004. /* now, perform the SSL initialization for this socket */
  1005. result = Curl_SSLConnect(conn, FIRSTSOCKET);
  1006. if(result)
  1007. return result;
  1008. }
  1009. if(conn->bits.user_passwd && !data->state.this_is_a_follow) {
  1010. /* Authorization: is requested, this is not a followed location, get the
  1011. original host name */
  1012. if (data->state.auth_host)
  1013. /* Free to avoid leaking memory on multiple requests*/
  1014. free(data->state.auth_host);
  1015. data->state.auth_host = strdup(conn->host.name);
  1016. }
  1017. return CURLE_OK;
  1018. }
  1019. /*
  1020. * Curl_http_done() gets called from Curl_done() after a single HTTP request
  1021. * has been performed.
  1022. */
  1023. CURLcode Curl_http_done(struct connectdata *conn,
  1024. CURLcode status)
  1025. {
  1026. struct SessionHandle *data;
  1027. struct HTTP *http;
  1028. (void)status; /* no use for us */
  1029. data=conn->data;
  1030. http=conn->proto.http;
  1031. /* set the proper values (possibly modified on POST) */
  1032. conn->fread = data->set.fread; /* restore */
  1033. conn->fread_in = data->set.in; /* restore */
  1034. if (http == NULL)
  1035. return CURLE_OK;
  1036. if(http->send_buffer) {
  1037. send_buffer *buff = http->send_buffer;
  1038. free(buff->buffer);
  1039. free(buff);
  1040. http->send_buffer = NULL; /* cleaer the pointer */
  1041. }
  1042. if(HTTPREQ_POST_FORM == data->set.httpreq) {
  1043. conn->bytecount = http->readbytecount + http->writebytecount;
  1044. Curl_formclean(http->sendit); /* Now free that whole lot */
  1045. }
  1046. else if(HTTPREQ_PUT == data->set.httpreq)
  1047. conn->bytecount = http->readbytecount + http->writebytecount;
  1048. if(!conn->bits.retry &&
  1049. ((http->readbytecount +
  1050. conn->headerbytecount -
  1051. conn->deductheadercount)) <= 0) {
  1052. /* If this connection isn't simply closed to be retried, AND nothing was
  1053. read from the HTTP server (that counts), this can't be right so we
  1054. return an error here */
  1055. failf(data, "Empty reply from server");
  1056. return CURLE_GOT_NOTHING;
  1057. }
  1058. return CURLE_OK;
  1059. }
  1060. /*
  1061. * Curl_http() gets called from the generic Curl_do() function when a HTTP
  1062. * request is to be performed. This creates and sends a properly constructed
  1063. * HTTP request.
  1064. */
  1065. CURLcode Curl_http(struct connectdata *conn)
  1066. {
  1067. struct SessionHandle *data=conn->data;
  1068. char *buf = data->state.buffer; /* this is a short cut to the buffer */
  1069. CURLcode result;
  1070. struct HTTP *http;
  1071. char *ppath = conn->path;
  1072. char *host = conn->host.name;
  1073. const char *te = ""; /* tranfer-encoding */
  1074. char *ptr;
  1075. char *request;
  1076. Curl_HttpReq httpreq = data->set.httpreq;
  1077. char *addcookies = NULL;
  1078. if(!conn->proto.http) {
  1079. /* Only allocate this struct if we don't already have it! */
  1080. http = (struct HTTP *)malloc(sizeof(struct HTTP));
  1081. if(!http)
  1082. return CURLE_OUT_OF_MEMORY;
  1083. memset(http, 0, sizeof(struct HTTP));
  1084. conn->proto.http = http;
  1085. }
  1086. else
  1087. http = conn->proto.http;
  1088. /* We default to persistant connections */
  1089. conn->bits.close = FALSE;
  1090. if ( (conn->protocol&(PROT_HTTP|PROT_FTP)) &&
  1091. data->set.upload) {
  1092. httpreq = HTTPREQ_PUT;
  1093. }
  1094. /* Now set the 'request' pointer to the proper request string */
  1095. if(data->set.customrequest)
  1096. request = data->set.customrequest;
  1097. else {
  1098. if(conn->bits.no_body)
  1099. request = (char *)"HEAD";
  1100. else {
  1101. curlassert((httpreq > HTTPREQ_NONE) && (httpreq < HTTPREQ_LAST));
  1102. switch(httpreq) {
  1103. case HTTPREQ_POST:
  1104. case HTTPREQ_POST_FORM:
  1105. request = (char *)"POST";
  1106. break;
  1107. case HTTPREQ_PUT:
  1108. request = (char *)"PUT";
  1109. break;
  1110. default: /* this should never happen */
  1111. case HTTPREQ_GET:
  1112. request = (char *)"GET";
  1113. break;
  1114. case HTTPREQ_HEAD:
  1115. request = (char *)"HEAD";
  1116. break;
  1117. }
  1118. }
  1119. }
  1120. /* The User-Agent string might have been allocated in url.c already, because
  1121. it might have been used in the proxy connect, but if we have got a header
  1122. with the user-agent string specified, we erase the previously made string
  1123. here. */
  1124. if(checkheaders(data, "User-Agent:") && conn->allocptr.uagent) {
  1125. free(conn->allocptr.uagent);
  1126. conn->allocptr.uagent=NULL;
  1127. }
  1128. /* setup the authentication headers */
  1129. result = Curl_http_output_auth(conn, request, ppath, FALSE);
  1130. if(result)
  1131. return result;
  1132. if((!data->state.authhost.done || !data->state.authproxy.done ) &&
  1133. (httpreq != HTTPREQ_GET)) {
  1134. /* Until we are authenticated, we switch over to HEAD. Unless its a GET
  1135. we want to do. The explanation for this is rather long and boring, but
  1136. the point is that it can't be done otherwise without risking having to
  1137. send the POST or PUT data multiple times. */
  1138. httpreq = HTTPREQ_HEAD;
  1139. request = (char *)"HEAD";
  1140. conn->bits.no_body = TRUE;
  1141. conn->bits.authprobe = TRUE; /* this is a request done to probe for
  1142. authentication methods */
  1143. }
  1144. else
  1145. conn->bits.authprobe = FALSE;
  1146. Curl_safefree(conn->allocptr.ref);
  1147. if(data->change.referer && !checkheaders(data, "Referer:"))
  1148. conn->allocptr.ref = aprintf("Referer: %s\015\012", data->change.referer);
  1149. else
  1150. conn->allocptr.ref = NULL;
  1151. if(data->set.cookie && !checkheaders(data, "Cookie:"))
  1152. addcookies = data->set.cookie;
  1153. if(!conn->bits.upload_chunky && (httpreq != HTTPREQ_GET)) {
  1154. /* not a chunky transfer yet, but data is to be sent */
  1155. ptr = checkheaders(data, "Transfer-Encoding:");
  1156. if(ptr) {
  1157. /* Some kind of TE is requested, check if 'chunked' is chosen */
  1158. conn->bits.upload_chunky =
  1159. Curl_compareheader(ptr, "Transfer-Encoding:", "chunked");
  1160. te = "";
  1161. }
  1162. }
  1163. else if(conn->bits.upload_chunky) {
  1164. /* RFC2616 section 4.4:
  1165. Messages MUST NOT include both a Content-Length header field and a
  1166. non-identity transfer-coding. If the message does include a non-
  1167. identity transfer-coding, the Content-Length MUST be ignored. */
  1168. if(!checkheaders(data, "Transfer-Encoding:")) {
  1169. te = "Transfer-Encoding: chunked\r\n";
  1170. }
  1171. else {
  1172. te = "";
  1173. conn->bits.upload_chunky = FALSE; /* transfer-encoding was disabled,
  1174. so don't chunkify this! */
  1175. }
  1176. }
  1177. Curl_safefree(conn->allocptr.host);
  1178. ptr = checkheaders(data, "Host:");
  1179. if(ptr && !data->state.this_is_a_follow) {
  1180. /* If we have a given custom Host: header, we extract the host name in
  1181. order to possibly use it for cookie reasons later on. We only allow the
  1182. custom Host: header if this is NOT a redirect, as setting Host: in the
  1183. redirected request is being out on thin ice. */
  1184. char *start = ptr+strlen("Host:");
  1185. while(*start && isspace((int)*start ))
  1186. start++;
  1187. ptr = start; /* start host-scanning here */
  1188. /* scan through the string to find the end (space or colon) */
  1189. while(*ptr && !isspace((int)*ptr) && !(':'==*ptr))
  1190. ptr++;
  1191. if(ptr != start) {
  1192. size_t len=ptr-start;
  1193. conn->allocptr.cookiehost = malloc(len+1);
  1194. if(!conn->allocptr.cookiehost)
  1195. return CURLE_OUT_OF_MEMORY;
  1196. memcpy(conn->allocptr.cookiehost, start, len);
  1197. conn->allocptr.cookiehost[len]=0;
  1198. }
  1199. conn->allocptr.host = NULL;
  1200. }
  1201. else {
  1202. /* When building Host: headers, we must put the host name within
  1203. [brackets] if the host name is a plain IPv6-address. RFC2732-style. */
  1204. if(((conn->protocol&PROT_HTTPS) && (conn->remote_port == PORT_HTTPS)) ||
  1205. (!(conn->protocol&PROT_HTTPS) && (conn->remote_port == PORT_HTTP)) )
  1206. /* If (HTTPS on port 443) OR (non-HTTPS on port 80) then don't include
  1207. the port number in the host string */
  1208. conn->allocptr.host = aprintf("Host: %s%s%s\r\n",
  1209. conn->bits.ipv6_ip?"[":"",
  1210. host,
  1211. conn->bits.ipv6_ip?"]":"");
  1212. else
  1213. conn->allocptr.host = aprintf("Host: %s%s%s:%d\r\n",
  1214. conn->bits.ipv6_ip?"[":"",
  1215. host,
  1216. conn->bits.ipv6_ip?"]":"",
  1217. conn->remote_port);
  1218. if(!conn->allocptr.host)
  1219. /* without Host: we can't make a nice request */
  1220. return CURLE_OUT_OF_MEMORY;
  1221. }
  1222. if (conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
  1223. /* Using a proxy but does not tunnel through it */
  1224. /* The path sent to the proxy is in fact the entire URL. But if the remote
  1225. host is a IDN-name, we must make sure that the request we produce only
  1226. uses the encoded host name! */
  1227. if(conn->host.dispname != conn->host.name) {
  1228. char *url = data->change.url;
  1229. char *iPtr = strstr(url, conn->host.dispname);
  1230. if(iPtr) {
  1231. /* This is where the display name starts in the URL, now replace this
  1232. part with the encoded name. TODO: This method of replacing the host
  1233. name is rather crude as I believe there's a slight risk that the
  1234. user has entered a user name or password that contain the host name
  1235. string. */
  1236. size_t currlen = strlen(conn->host.dispname);
  1237. size_t newlen = strlen(conn->host.name);
  1238. size_t urllen = strlen(url);
  1239. char *newurl;
  1240. newurl = malloc(urllen + newlen - currlen + 1);
  1241. if(newurl) {
  1242. /* copy the part before the host name */
  1243. memcpy(newurl, url, iPtr - url);
  1244. /* append the new host name instead of the old */
  1245. memcpy(newurl + (iPtr - url), conn->host.name, newlen);
  1246. /* append the piece after the host name */
  1247. memcpy(newurl + newlen + (iPtr - url),
  1248. iPtr + currlen, /* copy the trailing zero byte too */
  1249. urllen - (iPtr-url) - currlen + 1);
  1250. if(data->change.url_alloc)
  1251. free(data->change.url);
  1252. data->change.url = newurl;
  1253. data->change.url_alloc = TRUE;
  1254. }
  1255. else
  1256. return CURLE_OUT_OF_MEMORY;
  1257. }
  1258. }
  1259. ppath = data->change.url;
  1260. }
  1261. if(HTTPREQ_POST_FORM == httpreq) {
  1262. /* we must build the whole darned post sequence first, so that we have
  1263. a size of the whole shebang before we start to send it */
  1264. result = Curl_getFormData(&http->sendit, data->set.httppost,
  1265. &http->postsize);
  1266. if(CURLE_OK != result) {
  1267. /* Curl_getFormData() doesn't use failf() */
  1268. failf(data, "failed creating formpost data");
  1269. return result;
  1270. }
  1271. }
  1272. if(!checkheaders(data, "Pragma:"))
  1273. http->p_pragma = "Pragma: no-cache\r\n";
  1274. if(!checkheaders(data, "Accept:"))
  1275. http->p_accept = "Accept: */*\r\n";
  1276. if(( (HTTPREQ_POST == httpreq) ||
  1277. (HTTPREQ_POST_FORM == httpreq) ||
  1278. (HTTPREQ_PUT == httpreq) ) &&
  1279. conn->resume_from) {
  1280. /**********************************************************************
  1281. * Resuming upload in HTTP means that we PUT or POST and that we have
  1282. * got a resume_from value set. The resume value has already created
  1283. * a Range: header that will be passed along. We need to "fast forward"
  1284. * the file the given number of bytes and decrease the assume upload
  1285. * file size before we continue this venture in the dark lands of HTTP.
  1286. *********************************************************************/
  1287. if(conn->resume_from < 0 ) {
  1288. /*
  1289. * This is meant to get the size of the present remote-file by itself.
  1290. * We don't support this now. Bail out!
  1291. */
  1292. conn->resume_from = 0;
  1293. }
  1294. if(conn->resume_from) {
  1295. /* do we still game? */
  1296. curl_off_t passed=0;
  1297. /* Now, let's read off the proper amount of bytes from the
  1298. input. If we knew it was a proper file we could've just
  1299. fseek()ed but we only have a stream here */
  1300. do {
  1301. size_t readthisamountnow = (size_t)(conn->resume_from - passed);
  1302. size_t actuallyread;
  1303. if(readthisamountnow > BUFSIZE)
  1304. readthisamountnow = BUFSIZE;
  1305. actuallyread =
  1306. data->set.fread(data->state.buffer, 1, (size_t)readthisamountnow,
  1307. data->set.in);
  1308. passed += actuallyread;
  1309. if(actuallyread != readthisamountnow) {
  1310. failf(data, "Could only read %" FORMAT_OFF_T
  1311. " bytes from the input",
  1312. passed);
  1313. return CURLE_READ_ERROR;
  1314. }
  1315. } while(passed != conn->resume_from); /* loop until done */
  1316. /* now, decrease the size of the read */
  1317. if(data->set.infilesize>0) {
  1318. data->set.infilesize -= conn->resume_from;
  1319. if(data->set.infilesize <= 0) {
  1320. failf(data, "File already completely uploaded");
  1321. return CURLE_PARTIAL_FILE;
  1322. }
  1323. }
  1324. /* we've passed, proceed as normal */
  1325. }
  1326. }
  1327. if(conn->bits.use_range) {
  1328. /*
  1329. * A range is selected. We use different headers whether we're downloading
  1330. * or uploading and we always let customized headers override our internal
  1331. * ones if any such are specified.
  1332. */
  1333. if((httpreq == HTTPREQ_GET) &&
  1334. !checkheaders(data, "Range:")) {
  1335. /* if a line like this was already allocated, free the previous one */
  1336. if(conn->allocptr.rangeline)
  1337. free(conn->allocptr.rangeline);
  1338. conn->allocptr.rangeline = aprintf("Range: bytes=%s\r\n", conn->range);
  1339. }
  1340. else if((httpreq != HTTPREQ_GET) &&
  1341. !checkheaders(data, "Content-Range:")) {
  1342. if(conn->resume_from) {
  1343. /* This is because "resume" was selected */
  1344. curl_off_t total_expected_size=
  1345. conn->resume_from + data->set.infilesize;
  1346. conn->allocptr.rangeline =
  1347. aprintf("Content-Range: bytes %s%" FORMAT_OFF_T
  1348. "/%" FORMAT_OFF_T "\r\n",
  1349. conn->range, total_expected_size-1,
  1350. total_expected_size);
  1351. }
  1352. else {
  1353. /* Range was selected and then we just pass the incoming range and
  1354. append total size */
  1355. conn->allocptr.rangeline =
  1356. aprintf("Content-Range: bytes %s/%" FORMAT_OFF_T "\r\n",
  1357. conn->range, data->set.infilesize);
  1358. }
  1359. }
  1360. }
  1361. {
  1362. /* Use 1.1 unless the use specificly asked for 1.0 */
  1363. const char *httpstring=
  1364. data->set.httpversion==CURL_HTTP_VERSION_1_0?"1.0":"1.1";
  1365. send_buffer *req_buffer;
  1366. struct curl_slist *headers=data->set.headers;
  1367. curl_off_t postsize; /* off_t type to be able to hold a large file size */
  1368. /* initialize a dynamic send-buffer */
  1369. req_buffer = add_buffer_init();
  1370. if(!req_buffer)
  1371. return CURLE_OUT_OF_MEMORY;
  1372. /* add the main request stuff */
  1373. result =
  1374. add_bufferf(req_buffer,
  1375. "%s " /* GET/HEAD/POST/PUT */
  1376. "%s HTTP/%s\r\n" /* path + HTTP version */
  1377. "%s" /* proxyuserpwd */
  1378. "%s" /* userpwd */
  1379. "%s" /* range */
  1380. "%s" /* user agent */
  1381. "%s" /* host */
  1382. "%s" /* pragma */
  1383. "%s" /* accept */
  1384. "%s" /* accept-encoding */
  1385. "%s" /* referer */
  1386. "%s",/* transfer-encoding */
  1387. request,
  1388. ppath,
  1389. httpstring,
  1390. conn->allocptr.proxyuserpwd?
  1391. conn->allocptr.proxyuserpwd:"",
  1392. conn->allocptr.userpwd?conn->allocptr.userpwd:"",
  1393. (conn->bits.use_range && conn->allocptr.rangeline)?
  1394. conn->allocptr.rangeline:"",
  1395. (data->set.useragent && *data->set.useragent && conn->allocptr.uagent)?
  1396. conn->allocptr.uagent:"",
  1397. (conn->allocptr.host?conn->allocptr.host:""), /* Host: host */
  1398. http->p_pragma?http->p_pragma:"",
  1399. http->p_accept?http->p_accept:"",
  1400. (data->set.encoding && *data->set.encoding && conn->allocptr.accept_encoding)?
  1401. conn->allocptr.accept_encoding:"",
  1402. (data->change.referer && conn->allocptr.ref)?conn->allocptr.ref:"" /* Referer: <data> */,
  1403. te
  1404. );
  1405. if(result)
  1406. return result;
  1407. if(data->cookies || addcookies) {
  1408. struct Cookie *co=NULL; /* no cookies from start */
  1409. int count=0;
  1410. if(data->cookies) {
  1411. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  1412. co = Curl_cookie_getlist(data->cookies,
  1413. conn->allocptr.cookiehost?
  1414. conn->allocptr.cookiehost:host, ppath,
  1415. (bool)(conn->protocol&PROT_HTTPS?TRUE:FALSE));
  1416. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  1417. }
  1418. if(co) {
  1419. struct Cookie *store=co;
  1420. /* now loop through all cookies that matched */
  1421. while(co) {
  1422. if(co->value) {
  1423. if(0 == count) {
  1424. result = add_bufferf(req_buffer, "Cookie: ");
  1425. if(result)
  1426. break;
  1427. }
  1428. result = add_bufferf(req_buffer,
  1429. "%s%s=%s", count?"; ":"",
  1430. co->name, co->value);
  1431. if(result)
  1432. break;
  1433. count++;
  1434. }
  1435. co = co->next; /* next cookie please */
  1436. }
  1437. Curl_cookie_freelist(store); /* free the cookie list */
  1438. }
  1439. if(addcookies && (CURLE_OK == result)) {
  1440. if(!count)
  1441. result = add_bufferf(req_buffer, "Cookie: ");
  1442. if(CURLE_OK == result) {
  1443. result = add_bufferf(req_buffer, "%s%s",
  1444. count?"; ":"",
  1445. addcookies);
  1446. count++;
  1447. }
  1448. }
  1449. if(count && (CURLE_OK == result))
  1450. result = add_buffer(req_buffer, "\r\n", 2);
  1451. if(result)
  1452. return result;
  1453. }
  1454. if(data->set.timecondition) {
  1455. struct tm *thistime;
  1456. /* Phil Karn (Fri, 13 Apr 2001) pointed out that the If-Modified-Since
  1457. * header family should have their times set in GMT as RFC2616 defines:
  1458. * "All HTTP date/time stamps MUST be represented in Greenwich Mean Time
  1459. * (GMT), without exception. For the purposes of HTTP, GMT is exactly
  1460. * equal to UTC (Coordinated Universal Time)." (see page 20 of RFC2616).
  1461. */
  1462. #ifdef HAVE_GMTIME_R
  1463. /* thread-safe version */
  1464. struct tm keeptime;
  1465. thistime = (struct tm *)gmtime_r(&data->set.timevalue, &keeptime);
  1466. #else
  1467. thistime = gmtime(&data->set.timevalue);
  1468. #endif
  1469. #ifdef HAVE_STRFTIME
  1470. /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
  1471. strftime(buf, BUFSIZE-1, "%a, %d %b %Y %H:%M:%S GMT", thistime);
  1472. #else
  1473. /* TODO: Right, we *could* write a replacement here */
  1474. strcpy(buf, "no strftime() support");
  1475. #endif
  1476. switch(data->set.timecondition) {
  1477. case CURL_TIMECOND_IFMODSINCE:
  1478. default:
  1479. result = add_bufferf(req_buffer,
  1480. "If-Modified-Since: %s\r\n", buf);
  1481. break;
  1482. case CURL_TIMECOND_IFUNMODSINCE:
  1483. result = add_bufferf(req_buffer,
  1484. "If-Unmodified-Since: %s\r\n", buf);
  1485. break;
  1486. case CURL_TIMECOND_LASTMOD:
  1487. result = add_bufferf(req_buffer,
  1488. "Last-Modified: %s\r\n", buf);
  1489. break;
  1490. }
  1491. if(result)
  1492. return result;
  1493. }
  1494. while(headers) {
  1495. ptr = strchr(headers->data, ':');
  1496. if(ptr) {
  1497. /* we require a colon for this to be a true header */
  1498. ptr++; /* pass the colon */
  1499. while(*ptr && isspace((int)*ptr))
  1500. ptr++;
  1501. if(*ptr) {
  1502. /* only send this if the contents was non-blank */
  1503. result = add_bufferf(req_buffer, "%s\r\n", headers->data);
  1504. if(result)
  1505. return result;
  1506. }
  1507. }
  1508. headers = headers->next;
  1509. }
  1510. http->postdata = NULL; /* nothing to post at this point */
  1511. Curl_pgrsSetUploadSize(data, 0); /* upload size is 0 atm */
  1512. /* If 'authdone' is FALSE, we must not set the write socket index to the
  1513. Curl_transfer() call below, as we're not ready to actually upload any
  1514. data yet. */
  1515. switch(httpreq) {
  1516. case HTTPREQ_POST_FORM:
  1517. if(Curl_FormInit(&http->form, http->sendit)) {
  1518. failf(data, "Internal HTTP POST error!");
  1519. return CURLE_HTTP_POST_ERROR;
  1520. }
  1521. /* set the read function to read from the generated form data */
  1522. conn->fread = (curl_read_callback)Curl_FormReader;
  1523. conn->fread_in = &http->form;
  1524. http->sending = HTTPSEND_BODY;
  1525. if(!conn->bits.upload_chunky) {
  1526. /* only add Content-Length if not uploading chunked */
  1527. result = add_bufferf(req_buffer,
  1528. "Content-Length: %" FORMAT_OFF_T "\r\n",
  1529. http->postsize);
  1530. if(result)
  1531. return result;
  1532. }
  1533. if(!checkheaders(data, "Expect:")) {
  1534. /* if not disabled explicitly we add a Expect: 100-continue
  1535. to the headers which actually speeds up post operations (as
  1536. there is one packet coming back from the web server) */
  1537. result = add_bufferf(req_buffer,
  1538. "Expect: 100-continue\r\n");
  1539. if(result)
  1540. return result;
  1541. data->set.expect100header = TRUE;
  1542. }
  1543. if(!checkheaders(data, "Content-Type:")) {
  1544. /* Get Content-Type: line from Curl_formpostheader.
  1545. The Content-Type header line also contains the MIME boundary
  1546. string etc why disabling this header is likely to not make things
  1547. work, but we support disabling it anyway.
  1548. */
  1549. char *contentType;
  1550. size_t linelength=0;
  1551. contentType = Curl_formpostheader((void *)&http->form,
  1552. &linelength);
  1553. if(!contentType) {
  1554. failf(data, "Could not get Content-Type header line!");
  1555. return CURLE_HTTP_POST_ERROR;
  1556. }
  1557. result = add_buffer(req_buffer, contentType, linelength);
  1558. if(result)
  1559. return result;
  1560. }
  1561. /* make the request end in a true CRLF */
  1562. result = add_buffer(req_buffer, "\r\n", 2);
  1563. if(result)
  1564. return result;
  1565. /* set upload size to the progress meter */
  1566. Curl_pgrsSetUploadSize(data, http->postsize);
  1567. /* fire away the whole request to the server */
  1568. result = add_buffer_send(req_buffer, conn,
  1569. &data->info.request_size);
  1570. if(result)
  1571. failf(data, "Failed sending POST request");
  1572. else
  1573. /* setup variables for the upcoming transfer */
  1574. result = Curl_Transfer(conn, FIRSTSOCKET, -1, TRUE,
  1575. &http->readbytecount,
  1576. FIRSTSOCKET,
  1577. &http->writebytecount);
  1578. if(result) {
  1579. Curl_formclean(http->sendit); /* free that whole lot */
  1580. return result;
  1581. }
  1582. break;
  1583. case HTTPREQ_PUT: /* Let's PUT the data to the server! */
  1584. if((data->set.infilesize>0) && !conn->bits.upload_chunky) {
  1585. /* only add Content-Length if not uploading chunked */
  1586. result = add_bufferf(req_buffer,
  1587. "Content-Length: %" FORMAT_OFF_T "\r\n", /* size */
  1588. data->set.infilesize );
  1589. if(result)
  1590. return result;
  1591. }
  1592. if(!checkheaders(data, "Expect:")) {
  1593. /* if not disabled explicitly we add a Expect: 100-continue
  1594. to the headers which actually speeds up post operations (as
  1595. there is one packet coming back from the web server) */
  1596. result = add_bufferf(req_buffer,
  1597. "Expect: 100-continue\r\n");
  1598. if(result)
  1599. return result;
  1600. data->set.expect100header = TRUE;
  1601. }
  1602. result = add_buffer(req_buffer, "\r\n", 2); /* end of headers */
  1603. if(result)
  1604. return result;
  1605. /* set the upload size to the progress meter */
  1606. Curl_pgrsSetUploadSize(data, data->set.infilesize);
  1607. /* this sends the buffer and frees all the buffer resources */
  1608. result = add_buffer_send(req_buffer, conn,
  1609. &data->info.request_size);
  1610. if(result)
  1611. failf(data, "Failed sending POST request");
  1612. else
  1613. /* prepare for transfer */
  1614. result = Curl_Transfer(conn, FIRSTSOCKET, -1, TRUE,
  1615. &http->readbytecount,
  1616. FIRSTSOCKET,
  1617. &http->writebytecount);
  1618. if(result)
  1619. return result;
  1620. break;
  1621. case HTTPREQ_POST:
  1622. /* this is the simple POST, using x-www-form-urlencoded style */
  1623. /* store the size of the postfields */
  1624. postsize = data->set.postfieldsize?
  1625. data->set.postfieldsize:
  1626. (data->set.postfields?(curl_off_t)strlen(data->set.postfields):0);
  1627. if(!conn->bits.upload_chunky) {
  1628. /* We only set Content-Length and allow a custom Content-Length if
  1629. we don't upload data chunked, as RFC2616 forbids us to set both
  1630. kinds of headers (Transfer-Encoding: chunked and Content-Length) */
  1631. if(!checkheaders(data, "Content-Length:")) {
  1632. /* we allow replacing this header, although it isn't very wise to
  1633. actually set your own */
  1634. result = add_bufferf(req_buffer,
  1635. "Content-Length: %" FORMAT_OFF_T"\r\n",
  1636. postsize);
  1637. if(result)
  1638. return result;
  1639. }
  1640. }
  1641. if(!checkheaders(data, "Content-Type:")) {
  1642. result = add_bufferf(req_buffer,
  1643. "Content-Type: application/x-www-form-urlencoded\r\n");
  1644. if(result)
  1645. return result;
  1646. }
  1647. if(data->set.postfields) {
  1648. if((data->state.authhost.done || data->state.authproxy.done )
  1649. && (postsize < (100*1024))) {
  1650. /* If we're not done with the authentication phase, we don't expect
  1651. to actually send off any data yet. Hence, we delay the sending of
  1652. the body until we receive that friendly 100-continue response */
  1653. /* The post data is less than 100K, then append it to the header.
  1654. This limit is no magic limit but only set to prevent really huge
  1655. POSTs to get the data duplicated with malloc() and family. */
  1656. result = add_buffer(req_buffer, "\r\n", 2); /* end of headers! */
  1657. if(result)
  1658. return result;
  1659. if(!conn->bits.upload_chunky) {
  1660. /* We're not sending it 'chunked', append it to the request
  1661. already now to reduce the number if send() calls */
  1662. result = add_buffer(req_buffer, data->set.postfields,
  1663. (size_t)postsize);
  1664. }
  1665. else {
  1666. /* Append the POST data chunky-style */
  1667. result = add_bufferf(req_buffer, "%x\r\n", (int)postsize);
  1668. if(CURLE_OK == result)
  1669. result = add_buffer(req_buffer, data->set.postfields,
  1670. (size_t)postsize);
  1671. if(CURLE_OK == result)
  1672. result = add_buffer(req_buffer,
  1673. "\r\n0\r\n\r\n", 7); /* end of a chunked
  1674. transfer stream */
  1675. }
  1676. if(result)
  1677. return result;
  1678. }
  1679. else {
  1680. /* A huge POST coming up, do data separate from the request */
  1681. http->postsize = postsize;
  1682. http->postdata = data->set.postfields;
  1683. http->sending = HTTPSEND_BODY;
  1684. conn->fread = (curl_read_callback)readmoredata;
  1685. conn->fread_in = (void *)conn;
  1686. /* set the upload size to the progress meter */
  1687. Curl_pgrsSetUploadSize(data, http->postsize);
  1688. if(!checkheaders(data, "Expect:")) {
  1689. /* if not disabled explicitly we add a Expect: 100-continue to the
  1690. headers which actually speeds up post operations (as there is
  1691. one packet coming back from the web server) */
  1692. add_bufferf(req_buffer,
  1693. "Expect: 100-continue\r\n");
  1694. data->set.expect100header = TRUE;
  1695. }
  1696. add_buffer(req_buffer, "\r\n", 2); /* end of headers! */
  1697. }
  1698. }
  1699. else {
  1700. add_buffer(req_buffer, "\r\n", 2); /* end of headers! */
  1701. /* set the upload size to the progress meter */
  1702. Curl_pgrsSetUploadSize(data, data->set.infilesize);
  1703. /* set the pointer to mark that we will send the post body using
  1704. the read callback */
  1705. http->postdata = (char *)&http->postdata;
  1706. }
  1707. /* issue the request */
  1708. result = add_buffer_send(req_buffer, conn,
  1709. &data->info.request_size);
  1710. if(result)
  1711. failf(data, "Failed sending HTTP POST request");
  1712. else
  1713. result =
  1714. Curl_Transfer(conn, FIRSTSOCKET, -1, TRUE,
  1715. &http->readbytecount,
  1716. http->postdata?FIRSTSOCKET:-1,
  1717. http->postdata?&http->writebytecount:NULL);
  1718. break;
  1719. default:
  1720. add_buffer(req_buffer, "\r\n", 2);
  1721. /* issue the request */
  1722. result = add_buffer_send(req_buffer, conn,
  1723. &data->info.request_size);
  1724. if(result)
  1725. failf(data, "Failed sending HTTP request");
  1726. else
  1727. /* HTTP GET/HEAD download: */
  1728. result = Curl_Transfer(conn, FIRSTSOCKET, -1, TRUE,
  1729. &http->readbytecount,
  1730. http->postdata?FIRSTSOCKET:-1,
  1731. http->postdata?&http->writebytecount:NULL);
  1732. }
  1733. if(result)
  1734. return result;
  1735. }
  1736. return CURLE_OK;
  1737. }
  1738. #endif