Debian Codegraph @

deps/curl/lib/http.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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 https://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 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifndef CURL_DISABLE_HTTP
26
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30
31 #ifdef HAVE_NETDB_H
32 #include <netdb.h>
33 #endif
34 #ifdef HAVE_ARPA_INET_H
35 #include <arpa/inet.h>
36 #endif
37 #ifdef HAVE_NET_IF_H
38 #include <net/if.h>
39 #endif
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
42 #endif
43
44 #ifdef HAVE_SYS_PARAM_H
45 #include <sys/param.h>
46 #endif
47
48 #include "urldata.h"
49 #include <curl/curl.h>
50 #include "transfer.h"
51 #include "sendf.h"
52 #include "formdata.h"
53 #include "mime.h"
54 #include "progress.h"
55 #include "curl_base64.h"
56 #include "cookie.h"
57 #include "vauth/vauth.h"
58 #include "vtls/vtls.h"
59 #include "http_digest.h"
60 #include "http_ntlm.h"
61 #include "curl_ntlm_wb.h"
62 #include "http_negotiate.h"
63 #include "url.h"
64 #include "share.h"
65 #include "hostip.h"
66 #include "http.h"
67 #include "select.h"
68 #include "parsedate.h" /* for the week day and month names */
69 #include "strtoofft.h"
70 #include "multiif.h"
71 #include "strcase.h"
72 #include "content_encoding.h"
73 #include "http_proxy.h"
74 #include "warnless.h"
75 #include "non-ascii.h"
76 #include "http2.h"
77 #include "connect.h"
78 #include "strdup.h"
79 #include "altsvc.h"
80
81 /* The last 3 #include files should be in this order */
82 #include "curl_printf.h"
83 #include "curl_memory.h"
84 #include "memdebug.h"
85
86 /*
87 * Forward declarations.
88 */
89
90 static int http_getsock_do(struct connectdata *conn,
91 curl_socket_t *socks);
92 static int http_should_fail(struct connectdata *conn);
93
94 #ifndef CURL_DISABLE_PROXY
95 static CURLcode add_haproxy_protocol_header(struct connectdata *conn);
96 #endif
97
98 #ifdef USE_SSL
99 static CURLcode https_connecting(struct connectdata *conn, bool *done);
100 static int https_getsock(struct connectdata *conn,
101 curl_socket_t *socks);
102 #else
103 #define https_connecting(x,y) CURLE_COULDNT_CONNECT
104 #endif
105 static CURLcode http_setup_conn(struct connectdata *conn);
106
107 /*
108 * HTTP handler interface.
109 */
110 const struct Curl_handler Curl_handler_http = {
111 "HTTP", /* scheme */
112 http_setup_conn, /* setup_connection */
113 Curl_http, /* do_it */
114 Curl_http_done, /* done */
115 ZERO_NULL, /* do_more */
116 Curl_http_connect, /* connect_it */
117 ZERO_NULL, /* connecting */
118 ZERO_NULL, /* doing */
119 ZERO_NULL, /* proto_getsock */
120 http_getsock_do, /* doing_getsock */
121 ZERO_NULL, /* domore_getsock */
122 ZERO_NULL, /* perform_getsock */
123 ZERO_NULL, /* disconnect */
124 ZERO_NULL, /* readwrite */
125 ZERO_NULL, /* connection_check */
126 PORT_HTTP, /* defport */
127 CURLPROTO_HTTP, /* protocol */
128 PROTOPT_CREDSPERREQUEST /* flags */
129 };
130
131 #ifdef USE_SSL
132 /*
133 * HTTPS handler interface.
134 */
135 const struct Curl_handler Curl_handler_https = {
136 "HTTPS", /* scheme */
137 http_setup_conn, /* setup_connection */
138 Curl_http, /* do_it */
139 Curl_http_done, /* done */
140 ZERO_NULL, /* do_more */
141 Curl_http_connect, /* connect_it */
142 https_connecting, /* connecting */
143 ZERO_NULL, /* doing */
144 https_getsock, /* proto_getsock */
145 http_getsock_do, /* doing_getsock */
146 ZERO_NULL, /* domore_getsock */
147 ZERO_NULL, /* perform_getsock */
148 ZERO_NULL, /* disconnect */
149 ZERO_NULL, /* readwrite */
150 ZERO_NULL, /* connection_check */
151 PORT_HTTPS, /* defport */
152 CURLPROTO_HTTPS, /* protocol */
153 PROTOPT_SSL | PROTOPT_CREDSPERREQUEST | PROTOPT_ALPN_NPN /* flags */
154 };
155 #endif
156
157 static CURLcode http_setup_conn(struct connectdata *conn)
158 {
159 /* allocate the HTTP-specific struct for the Curl_easy, only to survive
160 during this request */
161 struct HTTP *http;
162 struct Curl_easy *data = conn->data;
163 DEBUGASSERT(data->req.protop == NULL);
164
165 http = calloc(1, sizeof(struct HTTP));
166 if(!http)
167 return CURLE_OUT_OF_MEMORY;
168
169 Curl_mime_initpart(&http->form, conn->data);
170 data->req.protop = http;
171
172 if(data->set.httpversion == CURL_HTTP_VERSION_3) {
173 if(conn->handler->flags & PROTOPT_SSL)
174 /* Only go HTTP/3 directly on HTTPS URLs. It needs a UDP socket and does
175 the QUIC dance. */
176 conn->transport = TRNSPRT_QUIC;
177 else {
178 failf(data, "HTTP/3 requested for non-HTTPS URL");
179 return CURLE_URL_MALFORMAT;
180 }
181 }
182 else {
183 if(!CONN_INUSE(conn))
184 /* if not already multi-using, setup connection details */
185 Curl_http2_setup_conn(conn);
186 Curl_http2_setup_req(data);
187 }
188 return CURLE_OK;
189 }
190
191 #ifndef CURL_DISABLE_PROXY
192 /*
193 * checkProxyHeaders() checks the linked list of custom proxy headers
194 * if proxy headers are not available, then it will lookup into http header
195 * link list
196 *
197 * It takes a connectdata struct as input instead of the Curl_easy simply to
198 * know if this is a proxy request or not, as it then might check a different
199 * header list. Provide the header prefix without colon!.
200 */
201 char *Curl_checkProxyheaders(const struct connectdata *conn,
202 const char *thisheader)
203 {
204 struct curl_slist *head;
205 size_t thislen = strlen(thisheader);
206 struct Curl_easy *data = conn->data;
207
208 for(head = (conn->bits.proxy && data->set.sep_headers) ?
209 data->set.proxyheaders : data->set.headers;
210 head; head = head->next) {
211 if(strncasecompare(head->data, thisheader, thislen) &&
212 Curl_headersep(head->data[thislen]))
213 return head->data;
214 }
215
216 return NULL;
217 }
218 #else
219 /* disabled */
220 #define Curl_checkProxyheaders(x,y) NULL
221 #endif
222
223 /*
224 * Strip off leading and trailing whitespace from the value in the
225 * given HTTP header line and return a strdupped copy. Returns NULL in
226 * case of allocation failure. Returns an empty string if the header value
227 * consists entirely of whitespace.
228 */
229 char *Curl_copy_header_value(const char *header)
230 {
231 const char *start;
232 const char *end;
233 char *value;
234 size_t len;
235
236 /* Find the end of the header name */
237 while(*header && (*header != ':'))
238 ++header;
239
240 if(*header)
241 /* Skip over colon */
242 ++header;
243
244 /* Find the first non-space letter */
245 start = header;
246 while(*start && ISSPACE(*start))
247 start++;
248
249 /* data is in the host encoding so
250 use '\r' and '\n' instead of 0x0d and 0x0a */
251 end = strchr(start, '\r');
252 if(!end)
253 end = strchr(start, '\n');
254 if(!end)
255 end = strchr(start, '\0');
256 if(!end)
257 return NULL;
258
259 /* skip all trailing space letters */
260 while((end > start) && ISSPACE(*end))
261 end--;
262
263 /* get length of the type */
264 len = end - start + 1;
265
266 value = malloc(len + 1);
267 if(!value)
268 return NULL;
269
270 memcpy(value, start, len);
271 value[len] = 0; /* zero terminate */
272
273 return value;
274 }
275
276 #ifndef CURL_DISABLE_HTTP_AUTH
277 /*
278 * http_output_basic() sets up an Authorization: header (or the proxy version)
279 * for HTTP Basic authentication.
280 *
281 * Returns CURLcode.
282 */
283 static CURLcode http_output_basic(struct connectdata *conn, bool proxy)
284 {
285 size_t size = 0;
286 char *authorization = NULL;
287 struct Curl_easy *data = conn->data;
288 char **userp;
289 const char *user;
290 const char *pwd;
291 CURLcode result;
292 char *out;
293
294 if(proxy) {
295 userp = &conn->allocptr.proxyuserpwd;
296 user = conn->http_proxy.user;
297 pwd = conn->http_proxy.passwd;
298 }
299 else {
300 userp = &conn->allocptr.userpwd;
301 user = conn->user;
302 pwd = conn->passwd;
303 }
304
305 out = aprintf("%s:%s", user, pwd);
306 if(!out)
307 return CURLE_OUT_OF_MEMORY;
308
309 result = Curl_base64_encode(data, out, strlen(out), &authorization, &size);
310 if(result)
311 goto fail;
312
313 if(!authorization) {
314 result = CURLE_REMOTE_ACCESS_DENIED;
315 goto fail;
316 }
317
318 free(*userp);
319 *userp = aprintf("%sAuthorization: Basic %s\r\n",
320 proxy ? "Proxy-" : "",
321 authorization);
322 free(authorization);
323 if(!*userp) {
324 result = CURLE_OUT_OF_MEMORY;
325 goto fail;
326 }
327
328 fail:
329 free(out);
330 return result;
331 }
332
333 /*
334 * http_output_bearer() sets up an Authorization: header
335 * for HTTP Bearer authentication.
336 *
337 * Returns CURLcode.
338 */
339 static CURLcode http_output_bearer(struct connectdata *conn)
340 {
341 char **userp;
342 CURLcode result = CURLE_OK;
343
344 userp = &conn->allocptr.userpwd;
345 free(*userp);
346 *userp = aprintf("Authorization: Bearer %s\r\n",
347 conn->data->set.str[STRING_BEARER]);
348
349 if(!*userp) {
350 result = CURLE_OUT_OF_MEMORY;
351 goto fail;
352 }
353
354 fail:
355 return result;
356 }
357
358 #endif
359
360 /* pickoneauth() selects the most favourable authentication method from the
361 * ones available and the ones we want.
362 *
363 * return TRUE if one was picked
364 */
365 static bool pickoneauth(struct auth *pick, unsigned long mask)
366 {
367 bool picked;
368 /* only deal with authentication we want */
369 unsigned long avail = pick->avail & pick->want & mask;
370 picked = TRUE;
371
372 /* The order of these checks is highly relevant, as this will be the order
373 of preference in case of the existence of multiple accepted types. */
374 if(avail & CURLAUTH_NEGOTIATE)
375 pick->picked = CURLAUTH_NEGOTIATE;
376 else if(avail & CURLAUTH_BEARER)
377 pick->picked = CURLAUTH_BEARER;
378 else if(avail & CURLAUTH_DIGEST)
379 pick->picked = CURLAUTH_DIGEST;
380 else if(avail & CURLAUTH_NTLM)
381 pick->picked = CURLAUTH_NTLM;
382 else if(avail & CURLAUTH_NTLM_WB)
383 pick->picked = CURLAUTH_NTLM_WB;
384 else if(avail & CURLAUTH_BASIC)
385 pick->picked = CURLAUTH_BASIC;
386 else {
387 pick->picked = CURLAUTH_PICKNONE; /* we select to use nothing */
388 picked = FALSE;
389 }
390 pick->avail = CURLAUTH_NONE; /* clear it here */
391
392 return picked;
393 }
394
395 /*
396 * http_perhapsrewind()
397 *
398 * If we are doing POST or PUT {
399 * If we have more data to send {
400 * If we are doing NTLM {
401 * Keep sending since we must not disconnect
402 * }
403 * else {
404 * If there is more than just a little data left to send, close
405 * the current connection by force.
406 * }
407 * }
408 * If we have sent any data {
409 * If we don't have track of all the data {
410 * call app to tell it to rewind
411 * }
412 * else {
413 * rewind internally so that the operation can restart fine
414 * }
415 * }
416 * }
417 */
418 static CURLcode http_perhapsrewind(struct connectdata *conn)
419 {
420 struct Curl_easy *data = conn->data;
421 struct HTTP *http = data->req.protop;
422 curl_off_t bytessent;
423 curl_off_t expectsend = -1; /* default is unknown */
424
425 if(!http)
426 /* If this is still NULL, we have not reach very far and we can safely
427 skip this rewinding stuff */
428 return CURLE_OK;
429
430 switch(data->set.httpreq) {
431 case HTTPREQ_GET:
432 case HTTPREQ_HEAD:
433 return CURLE_OK;
434 default:
435 break;
436 }
437
438 bytessent = data->req.writebytecount;
439
440 if(conn->bits.authneg) {
441 /* This is a state where we are known to be negotiating and we don't send
442 any data then. */
443 expectsend = 0;
444 }
445 else if(!conn->bits.protoconnstart) {
446 /* HTTP CONNECT in progress: there is no body */
447 expectsend = 0;
448 }
449 else {
450 /* figure out how much data we are expected to send */
451 switch(data->set.httpreq) {
452 case HTTPREQ_POST:
453 case HTTPREQ_PUT:
454 if(data->state.infilesize != -1)
455 expectsend = data->state.infilesize;
456 break;
457 case HTTPREQ_POST_FORM:
458 case HTTPREQ_POST_MIME:
459 expectsend = http->postsize;
460 break;
461 default:
462 break;
463 }
464 }
465
466 conn->bits.rewindaftersend = FALSE; /* default */
467
468 if((expectsend == -1) || (expectsend > bytessent)) {
469 #if defined(USE_NTLM)
470 /* There is still data left to send */
471 if((data->state.authproxy.picked == CURLAUTH_NTLM) ||
472 (data->state.authhost.picked == CURLAUTH_NTLM) ||
473 (data->state.authproxy.picked == CURLAUTH_NTLM_WB) ||
474 (data->state.authhost.picked == CURLAUTH_NTLM_WB)) {
475 if(((expectsend - bytessent) < 2000) ||
476 (conn->http_ntlm_state != NTLMSTATE_NONE) ||
477 (conn->proxy_ntlm_state != NTLMSTATE_NONE)) {
478 /* The NTLM-negotiation has started *OR* there is just a little (<2K)
479 data left to send, keep on sending. */
480
481 /* rewind data when completely done sending! */
482 if(!conn->bits.authneg && (conn->writesockfd != CURL_SOCKET_BAD)) {
483 conn->bits.rewindaftersend = TRUE;
484 infof(data, "Rewind stream after send\n");
485 }
486
487 return CURLE_OK;
488 }
489
490 if(conn->bits.close)
491 /* this is already marked to get closed */
492 return CURLE_OK;
493
494 infof(data, "NTLM send, close instead of sending %"
495 CURL_FORMAT_CURL_OFF_T " bytes\n",
496 (curl_off_t)(expectsend - bytessent));
497 }
498 #endif
499 #if defined(USE_SPNEGO)
500 /* There is still data left to send */
501 if((data->state.authproxy.picked == CURLAUTH_NEGOTIATE) ||
502 (data->state.authhost.picked == CURLAUTH_NEGOTIATE)) {
503 if(((expectsend - bytessent) < 2000) ||
504 (conn->http_negotiate_state != GSS_AUTHNONE) ||
505 (conn->proxy_negotiate_state != GSS_AUTHNONE)) {
506 /* The NEGOTIATE-negotiation has started *OR*
507 there is just a little (<2K) data left to send, keep on sending. */
508
509 /* rewind data when completely done sending! */
510 if(!conn->bits.authneg && (conn->writesockfd != CURL_SOCKET_BAD)) {
511 conn->bits.rewindaftersend = TRUE;
512 infof(data, "Rewind stream after send\n");
513 }
514
515 return CURLE_OK;
516 }
517
518 if(conn->bits.close)
519 /* this is already marked to get closed */
520 return CURLE_OK;
521
522 infof(data, "NEGOTIATE send, close instead of sending %"
523 CURL_FORMAT_CURL_OFF_T " bytes\n",
524 (curl_off_t)(expectsend - bytessent));
525 }
526 #endif
527
528 /* This is not NEGOTIATE/NTLM or many bytes left to send: close */
529 streamclose(conn, "Mid-auth HTTP and much data left to send");
530 data->req.size = 0; /* don't download any more than 0 bytes */
531
532 /* There still is data left to send, but this connection is marked for
533 closure so we can safely do the rewind right now */
534 }
535
536 if(bytessent)
537 /* we rewind now at once since if we already sent something */
538 return Curl_readrewind(conn);
539
540 return CURLE_OK;
541 }
542
543 /*
544 * Curl_http_auth_act() gets called when all HTTP headers have been received
545 * and it checks what authentication methods that are available and decides
546 * which one (if any) to use. It will set 'newurl' if an auth method was
547 * picked.
548 */
549
550 CURLcode Curl_http_auth_act(struct connectdata *conn)
551 {
552 struct Curl_easy *data = conn->data;
553 bool pickhost = FALSE;
554 bool pickproxy = FALSE;
555 CURLcode result = CURLE_OK;
556 unsigned long authmask = ~0ul;
557
558 if(!data->set.str[STRING_BEARER])
559 authmask &= (unsigned long)~CURLAUTH_BEARER;
560
561 if(100 <= data->req.httpcode && 199 >= data->req.httpcode)
562 /* this is a transient response code, ignore */
563 return CURLE_OK;
564
565 if(data->state.authproblem)
566 return data->set.http_fail_on_error?CURLE_HTTP_RETURNED_ERROR:CURLE_OK;
567
568 if((conn->bits.user_passwd || data->set.str[STRING_BEARER]) &&
569 ((data->req.httpcode == 401) ||
570 (conn->bits.authneg && data->req.httpcode < 300))) {
571 pickhost = pickoneauth(&data->state.authhost, authmask);
572 if(!pickhost)
573 data->state.authproblem = TRUE;
574 if(data->state.authhost.picked == CURLAUTH_NTLM &&
575 conn->httpversion > 11) {
576 infof(data, "Forcing HTTP/1.1 for NTLM");
577 connclose(conn, "Force HTTP/1.1 connection");
578 conn->data->set.httpversion = CURL_HTTP_VERSION_1_1;
579 }
580 }
581 if(conn->bits.proxy_user_passwd &&
582 ((data->req.httpcode == 407) ||
583 (conn->bits.authneg && data->req.httpcode < 300))) {
584 pickproxy = pickoneauth(&data->state.authproxy,
585 authmask & ~CURLAUTH_BEARER);
586 if(!pickproxy)
587 data->state.authproblem = TRUE;
588 }
589
590 if(pickhost || pickproxy) {
591 if((data->set.httpreq != HTTPREQ_GET) &&
592 (data->set.httpreq != HTTPREQ_HEAD) &&
593 !conn->bits.rewindaftersend) {
594 result = http_perhapsrewind(conn);
595 if(result)
596 return result;
597 }
598 /* In case this is GSS auth, the newurl field is already allocated so
599 we must make sure to free it before allocating a new one. As figured
600 out in bug #2284386 */
601 Curl_safefree(data->req.newurl);
602 data->req.newurl = strdup(data->change.url); /* clone URL */
603 if(!data->req.newurl)
604 return CURLE_OUT_OF_MEMORY;
605 }
606 else if((data->req.httpcode < 300) &&
607 (!data->state.authhost.done) &&
608 conn->bits.authneg) {
609 /* no (known) authentication available,
610 authentication is not "done" yet and
611 no authentication seems to be required and
612 we didn't try HEAD or GET */
613 if((data->set.httpreq != HTTPREQ_GET) &&
614 (data->set.httpreq != HTTPREQ_HEAD)) {
615 data->req.newurl = strdup(data->change.url); /* clone URL */
616 if(!data->req.newurl)
617 return CURLE_OUT_OF_MEMORY;
618 data->state.authhost.done = TRUE;
619 }
620 }
621 if(http_should_fail(conn)) {
622 failf(data, "The requested URL returned error: %d",
623 data->req.httpcode);
624 result = CURLE_HTTP_RETURNED_ERROR;
625 }
626
627 return result;
628 }
629
630 #ifndef CURL_DISABLE_HTTP_AUTH
631 /*
632 * Output the correct authentication header depending on the auth type
633 * and whether or not it is to a proxy.
634 */
635 static CURLcode
636 output_auth_headers(struct connectdata *conn,
637 struct auth *authstatus,
638 const char *request,
639 const char *path,
640 bool proxy)
641 {
642 const char *auth = NULL;
643 CURLcode result = CURLE_OK;
644 struct Curl_easy *data = conn->data;
645
646 #ifdef CURL_DISABLE_CRYPTO_AUTH
647 (void)request;
648 (void)path;
649 #endif
650
651 #ifdef USE_SPNEGO
652 if(authstatus->picked == CURLAUTH_NEGOTIATE) {
653 auth = "Negotiate";
654 result = Curl_output_negotiate(conn, proxy);
655 if(result)
656 return result;
657 }
658 else
659 #endif
660 #ifdef USE_NTLM
661 if(authstatus->picked == CURLAUTH_NTLM) {
662 auth = "NTLM";
663 result = Curl_output_ntlm(conn, proxy);
664 if(result)
665 return result;
666 }
667 else
668 #endif
669 #if defined(USE_NTLM) && defined(NTLM_WB_ENABLED)
670 if(authstatus->picked == CURLAUTH_NTLM_WB) {
671 auth = "NTLM_WB";
672 result = Curl_output_ntlm_wb(conn, proxy);
673 if(result)
674 return result;
675 }
676 else
677 #endif
678 #ifndef CURL_DISABLE_CRYPTO_AUTH
679 if(authstatus->picked == CURLAUTH_DIGEST) {
680 auth = "Digest";
681 result = Curl_output_digest(conn,
682 proxy,
683 (const unsigned char *)request,
684 (const unsigned char *)path);
685 if(result)
686 return result;
687 }
688 else
689 #endif
690 if(authstatus->picked == CURLAUTH_BASIC) {
691 /* Basic */
692 if((proxy && conn->bits.proxy_user_passwd &&
693 !Curl_checkProxyheaders(conn, "Proxy-authorization")) ||
694 (!proxy && conn->bits.user_passwd &&
695 !Curl_checkheaders(conn, "Authorization"))) {
696 auth = "Basic";
697 result = http_output_basic(conn, proxy);
698 if(result)
699 return result;
700 }
701
702 /* NOTE: this function should set 'done' TRUE, as the other auth
703 functions work that way */
704 authstatus->done = TRUE;
705 }
706 if(authstatus->picked == CURLAUTH_BEARER) {
707 /* Bearer */
708 if((!proxy && data->set.str[STRING_BEARER] &&
709 !Curl_checkheaders(conn, "Authorization:"))) {
710 auth = "Bearer";
711 result = http_output_bearer(conn);
712 if(result)
713 return result;
714 }
715
716 /* NOTE: this function should set 'done' TRUE, as the other auth
717 functions work that way */
718 authstatus->done = TRUE;
719 }
720
721 if(auth) {
722 infof(data, "%s auth using %s with user '%s'\n",
723 proxy ? "Proxy" : "Server", auth,
724 proxy ? (conn->http_proxy.user ? conn->http_proxy.user : "") :
725 (conn->user ? conn->user : ""));
726 authstatus->multipass = (!authstatus->done) ? TRUE : FALSE;
727 }
728 else
729 authstatus->multipass = FALSE;
730
731 return CURLE_OK;
732 }
733
734 /**
735 * Curl_http_output_auth() setups the authentication headers for the
736 * host/proxy and the correct authentication
737 * method. conn->data->state.authdone is set to TRUE when authentication is
738 * done.
739 *
740 * @param conn all information about the current connection
741 * @param request pointer to the request keyword
742 * @param path pointer to the requested path; should include query part
743 * @param proxytunnel boolean if this is the request setting up a "proxy
744 * tunnel"
745 *
746 * @returns CURLcode
747 */
748 CURLcode
749 Curl_http_output_auth(struct connectdata *conn,
750 const char *request,
751 const char *path,
752 bool proxytunnel) /* TRUE if this is the request setting
753 up the proxy tunnel */
754 {
755 CURLcode result = CURLE_OK;
756 struct Curl_easy *data = conn->data;
757 struct auth *authhost;
758 struct auth *authproxy;
759
760 DEBUGASSERT(data);
761
762 authhost = &data->state.authhost;
763 authproxy = &data->state.authproxy;
764
765 if((conn->bits.httpproxy && conn->bits.proxy_user_passwd) ||
766 conn->bits.user_passwd || data->set.str[STRING_BEARER])
767 /* continue please */;
768 else {
769 authhost->done = TRUE;
770 authproxy->done = TRUE;
771 return CURLE_OK; /* no authentication with no user or password */
772 }
773
774 if(authhost->want && !authhost->picked)
775 /* The app has selected one or more methods, but none has been picked
776 so far by a server round-trip. Then we set the picked one to the
777 want one, and if this is one single bit it'll be used instantly. */
778 authhost->picked = authhost->want;
779
780 if(authproxy->want && !authproxy->picked)
781 /* The app has selected one or more methods, but none has been picked so
782 far by a proxy round-trip. Then we set the picked one to the want one,
783 and if this is one single bit it'll be used instantly. */
784 authproxy->picked = authproxy->want;
785
786 #ifndef CURL_DISABLE_PROXY
787 /* Send proxy authentication header if needed */
788 if(conn->bits.httpproxy &&
789 (conn->bits.tunnel_proxy == (bit)proxytunnel)) {
790 result = output_auth_headers(conn, authproxy, request, path, TRUE);
791 if(result)
792 return result;
793 }
794 else
795 #else
796 (void)proxytunnel;
797 #endif /* CURL_DISABLE_PROXY */
798 /* we have no proxy so let's pretend we're done authenticating
799 with it */
800 authproxy->done = TRUE;
801
802 /* To prevent the user+password to get sent to other than the original
803 host due to a location-follow, we do some weirdo checks here */
804 if(!data->state.this_is_a_follow ||
805 conn->bits.netrc ||
806 !data->state.first_host ||
807 data->set.allow_auth_to_other_hosts ||
808 strcasecompare(data->state.first_host, conn->host.name)) {
809 result = output_auth_headers(conn, authhost, request, path, FALSE);
810 }
811 else
812 authhost->done = TRUE;
813
814 return result;
815 }
816
817 #else
818 /* when disabled */
819 CURLcode
820 Curl_http_output_auth(struct connectdata *conn,
821 const char *request,
822 const char *path,
823 bool proxytunnel)
824 {
825 (void)conn;
826 (void)request;
827 (void)path;
828 (void)proxytunnel;
829 return CURLE_OK;
830 }
831 #endif
832
833 /*
834 * Curl_http_input_auth() deals with Proxy-Authenticate: and WWW-Authenticate:
835 * headers. They are dealt with both in the transfer.c main loop and in the
836 * proxy CONNECT loop.
837 */
838
839 CURLcode Curl_http_input_auth(struct connectdata *conn, bool proxy,
840 const char *auth) /* the first non-space */
841 {
842 /*
843 * This resource requires authentication
844 */
845 struct Curl_easy *data = conn->data;
846
847 #ifdef USE_SPNEGO
848 curlnegotiate *negstate = proxy ? &conn->proxy_negotiate_state :
849 &conn->http_negotiate_state;
850 #endif
851 unsigned long *availp;
852 struct auth *authp;
853
854 if(proxy) {
855 availp = &data->info.proxyauthavail;
856 authp = &data->state.authproxy;
857 }
858 else {
859 availp = &data->info.httpauthavail;
860 authp = &data->state.authhost;
861 }
862
863 /*
864 * Here we check if we want the specific single authentication (using ==) and
865 * if we do, we initiate usage of it.
866 *
867 * If the provided authentication is wanted as one out of several accepted
868 * types (using &), we OR this authentication type to the authavail
869 * variable.
870 *
871 * Note:
872 *
873 * ->picked is first set to the 'want' value (one or more bits) before the
874 * request is sent, and then it is again set _after_ all response 401/407
875 * headers have been received but then only to a single preferred method
876 * (bit).
877 */
878
879 while(*auth) {
880 #ifdef USE_SPNEGO
881 if(checkprefix("Negotiate", auth)) {
882 if((authp->avail & CURLAUTH_NEGOTIATE) ||
883 Curl_auth_is_spnego_supported()) {
884 *availp |= CURLAUTH_NEGOTIATE;
885 authp->avail |= CURLAUTH_NEGOTIATE;
886
887 if(authp->picked == CURLAUTH_NEGOTIATE) {
888 CURLcode result = Curl_input_negotiate(conn, proxy, auth);
889 if(!result) {
890 DEBUGASSERT(!data->req.newurl);
891 data->req.newurl = strdup(data->change.url);
892 if(!data->req.newurl)
893 return CURLE_OUT_OF_MEMORY;
894 data->state.authproblem = FALSE;
895 /* we received a GSS auth token and we dealt with it fine */
896 *negstate = GSS_AUTHRECV;
897 }
898 else
899 data->state.authproblem = TRUE;
900 }
901 }
902 }
903 else
904 #endif
905 #ifdef USE_NTLM
906 /* NTLM support requires the SSL crypto libs */
907 if(checkprefix("NTLM", auth)) {
908 if((authp->avail & CURLAUTH_NTLM) ||
909 (authp->avail & CURLAUTH_NTLM_WB) ||
910 Curl_auth_is_ntlm_supported()) {
911 *availp |= CURLAUTH_NTLM;
912 authp->avail |= CURLAUTH_NTLM;
913
914 if(authp->picked == CURLAUTH_NTLM ||
915 authp->picked == CURLAUTH_NTLM_WB) {
916 /* NTLM authentication is picked and activated */
917 CURLcode result = Curl_input_ntlm(conn, proxy, auth);
918 if(!result) {
919 data->state.authproblem = FALSE;
920 #ifdef NTLM_WB_ENABLED
921 if(authp->picked == CURLAUTH_NTLM_WB) {
922 *availp &= ~CURLAUTH_NTLM;
923 authp->avail &= ~CURLAUTH_NTLM;
924 *availp |= CURLAUTH_NTLM_WB;
925 authp->avail |= CURLAUTH_NTLM_WB;
926
927 result = Curl_input_ntlm_wb(conn, proxy, auth);
928 if(result) {
929 infof(data, "Authentication problem. Ignoring this.\n");
930 data->state.authproblem = TRUE;
931 }
932 }
933 #endif
934 }
935 else {
936 infof(data, "Authentication problem. Ignoring this.\n");
937 data->state.authproblem = TRUE;
938 }
939 }
940 }
941 }
942 else
943 #endif
944 #ifndef CURL_DISABLE_CRYPTO_AUTH
945 if(checkprefix("Digest", auth)) {
946 if((authp->avail & CURLAUTH_DIGEST) != 0)
947 infof(data, "Ignoring duplicate digest auth header.\n");
948 else if(Curl_auth_is_digest_supported()) {
949 CURLcode result;
950
951 *availp |= CURLAUTH_DIGEST;
952 authp->avail |= CURLAUTH_DIGEST;
953
954 /* We call this function on input Digest headers even if Digest
955 * authentication isn't activated yet, as we need to store the
956 * incoming data from this header in case we are going to use
957 * Digest */
958 result = Curl_input_digest(conn, proxy, auth);
959 if(result) {
960 infof(data, "Authentication problem. Ignoring this.\n");
961 data->state.authproblem = TRUE;
962 }
963 }
964 }
965 else
966 #endif
967 if(checkprefix("Basic", auth)) {
968 *availp |= CURLAUTH_BASIC;
969 authp->avail |= CURLAUTH_BASIC;
970 if(authp->picked == CURLAUTH_BASIC) {
971 /* We asked for Basic authentication but got a 40X back
972 anyway, which basically means our name+password isn't
973 valid. */
974 authp->avail = CURLAUTH_NONE;
975 infof(data, "Authentication problem. Ignoring this.\n");
976 data->state.authproblem = TRUE;
977 }
978 }
979 else
980 if(checkprefix("Bearer", auth)) {
981 *availp |= CURLAUTH_BEARER;
982 authp->avail |= CURLAUTH_BEARER;
983 if(authp->picked == CURLAUTH_BEARER) {
984 /* We asked for Bearer authentication but got a 40X back
985 anyway, which basically means our token isn't valid. */
986 authp->avail = CURLAUTH_NONE;
987 infof(data, "Authentication problem. Ignoring this.\n");
988 data->state.authproblem = TRUE;
989 }
990 }
991
992 /* there may be multiple methods on one line, so keep reading */
993 while(*auth && *auth != ',') /* read up to the next comma */
994 auth++;
995 if(*auth == ',') /* if we're on a comma, skip it */
996 auth++;
997 while(*auth && ISSPACE(*auth))
998 auth++;
999 }
1000
1001 return CURLE_OK;
1002 }
1003
1004 /**
1005 * http_should_fail() determines whether an HTTP response has gotten us
1006 * into an error state or not.
1007 *
1008 * @param conn all information about the current connection
1009 *
1010 * @retval 0 communications should continue
1011 *
1012 * @retval 1 communications should not continue
1013 */
1014 static int http_should_fail(struct connectdata *conn)
1015 {
1016 struct Curl_easy *data;
1017 int httpcode;
1018
1019 DEBUGASSERT(conn);
1020 data = conn->data;
1021 DEBUGASSERT(data);
1022
1023 httpcode = data->req.httpcode;
1024
1025 /*
1026 ** If we haven't been asked to fail on error,
1027 ** don't fail.
1028 */
1029 if(!data->set.http_fail_on_error)
1030 return 0;
1031
1032 /*
1033 ** Any code < 400 is never terminal.
1034 */
1035 if(httpcode < 400)
1036 return 0;
1037
1038 /*
1039 ** Any code >= 400 that's not 401 or 407 is always
1040 ** a terminal error
1041 */
1042 if((httpcode != 401) && (httpcode != 407))
1043 return 1;
1044
1045 /*
1046 ** All we have left to deal with is 401 and 407
1047 */
1048 DEBUGASSERT((httpcode == 401) || (httpcode == 407));
1049
1050 /*
1051 ** Examine the current authentication state to see if this
1052 ** is an error. The idea is for this function to get
1053 ** called after processing all the headers in a response
1054 ** message. So, if we've been to asked to authenticate a
1055 ** particular stage, and we've done it, we're OK. But, if
1056 ** we're already completely authenticated, it's not OK to
1057 ** get another 401 or 407.
1058 **
1059 ** It is possible for authentication to go stale such that
1060 ** the client needs to reauthenticate. Once that info is
1061 ** available, use it here.
1062 */
1063
1064 /*
1065 ** Either we're not authenticating, or we're supposed to
1066 ** be authenticating something else. This is an error.
1067 */
1068 if((httpcode == 401) && !conn->bits.user_passwd)
1069 return TRUE;
1070 if((httpcode == 407) && !conn->bits.proxy_user_passwd)
1071 return TRUE;
1072
1073 return data->state.authproblem;
1074 }
1075
1076 /*
1077 * readmoredata() is a "fread() emulation" to provide POST and/or request
1078 * data. It is used when a huge POST is to be made and the entire chunk wasn't
1079 * sent in the first send(). This function will then be called from the
1080 * transfer.c loop when more data is to be sent to the peer.
1081 *
1082 * Returns the amount of bytes it filled the buffer with.
1083 */
1084 static size_t readmoredata(char *buffer,
1085 size_t size,
1086 size_t nitems,
1087 void *userp)
1088 {
1089 struct connectdata *conn = (struct connectdata *)userp;
1090 struct HTTP *http = conn->data->req.protop;
1091 size_t fullsize = size * nitems;
1092
1093 if(!http->postsize)
1094 /* nothing to return */
1095 return 0;
1096
1097 /* make sure that a HTTP request is never sent away chunked! */
1098 conn->data->req.forbidchunk = (http->sending == HTTPSEND_REQUEST)?TRUE:FALSE;
1099
1100 if(http->postsize <= (curl_off_t)fullsize) {
1101 memcpy(buffer, http->postdata, (size_t)http->postsize);
1102 fullsize = (size_t)http->postsize;
1103
1104 if(http->backup.postsize) {
1105 /* move backup data into focus and continue on that */
1106 http->postdata = http->backup.postdata;
1107 http->postsize = http->backup.postsize;
1108 conn->data->state.fread_func = http->backup.fread_func;
1109 conn->data->state.in = http->backup.fread_in;
1110
1111 http->sending++; /* move one step up */
1112
1113 http->backup.postsize = 0;
1114 }
1115 else
1116 http->postsize = 0;
1117
1118 return fullsize;
1119 }
1120
1121 memcpy(buffer, http->postdata, fullsize);
1122 http->postdata += fullsize;
1123 http->postsize -= fullsize;
1124
1125 return fullsize;
1126 }
1127
1128 /* ------------------------------------------------------------------------- */
1129 /* add_buffer functions */
1130
1131 /*
1132 * Curl_add_buffer_init() sets up and returns a fine buffer struct
1133 */
1134 Curl_send_buffer *Curl_add_buffer_init(void)
1135 {
1136 return calloc(1, sizeof(Curl_send_buffer));
1137 }
1138
1139 /*
1140 * Curl_add_buffer_free() frees all associated resources.
1141 */
1142 void Curl_add_buffer_free(Curl_send_buffer **inp)
1143 {
1144 Curl_send_buffer *in;
1145 if(!inp)
1146 return;
1147 in = *inp;
1148 if(in) { /* deal with NULL input */
1149 free(in->buffer);
1150 free(in);
1151 }
1152 *inp = NULL;
1153 }
1154
1155 /*
1156 * Curl_add_buffer_send() sends a header buffer and frees all associated
1157 * memory. Body data may be appended to the header data if desired.
1158 *
1159 * Returns CURLcode
1160 */
1161 CURLcode Curl_add_buffer_send(Curl_send_buffer **inp,
1162 struct connectdata *conn,
1163
1164 /* add the number of sent bytes to this
1165 counter */
1166 curl_off_t *bytes_written,
1167
1168 /* how much of the buffer contains body data */
1169 size_t included_body_bytes,
1170 int socketindex)
1171 {
1172 ssize_t amount;
1173 CURLcode result;
1174 char *ptr;
1175 size_t size;
1176 struct Curl_easy *data = conn->data;
1177 struct HTTP *http = data->req.protop;
1178 size_t sendsize;
1179 curl_socket_t sockfd;
1180 size_t headersize;
1181 Curl_send_buffer *in = *inp;
1182
1183 DEBUGASSERT(socketindex <= SECONDARYSOCKET);
1184
1185 sockfd = conn->sock[socketindex];
1186
1187 /* The looping below is required since we use non-blocking sockets, but due
1188 to the circumstances we will just loop and try again and again etc */
1189
1190 ptr = in->buffer;
1191 size = in->size_used;
1192
1193 headersize = size - included_body_bytes; /* the initial part that isn't body
1194 is header */
1195
1196 DEBUGASSERT(size > included_body_bytes);
1197
1198 result = Curl_convert_to_network(data, ptr, headersize);
1199 /* Curl_convert_to_network calls failf if unsuccessful */
1200 if(result) {
1201 /* conversion failed, free memory and return to the caller */
1202 Curl_add_buffer_free(inp);
1203 return result;
1204 }
1205
1206 if((conn->handler->flags & PROTOPT_SSL ||
1207 conn->http_proxy.proxytype == CURLPROXY_HTTPS)
1208 && conn->httpversion != 20) {
1209 /* We never send more than CURL_MAX_WRITE_SIZE bytes in one single chunk
1210 when we speak HTTPS, as if only a fraction of it is sent now, this data
1211 needs to fit into the normal read-callback buffer later on and that
1212 buffer is using this size.
1213 */
1214
1215 sendsize = CURLMIN(size, CURL_MAX_WRITE_SIZE);
1216
1217 /* OpenSSL is very picky and we must send the SAME buffer pointer to the
1218 library when we attempt to re-send this buffer. Sending the same data
1219 is not enough, we must use the exact same address. For this reason, we
1220 must copy the data to the uploadbuffer first, since that is the buffer
1221 we will be using if this send is retried later.
1222 */
1223 result = Curl_get_upload_buffer(data);
1224 if(result) {
1225 /* malloc failed, free memory and return to the caller */
1226 Curl_add_buffer_free(&in);
1227 return result;
1228 }
1229 memcpy(data->state.ulbuf, ptr, sendsize);
1230 ptr = data->state.ulbuf;
1231 }
1232 else
1233 sendsize = size;
1234
1235 result = Curl_write(conn, sockfd, ptr, sendsize, &amount);
1236
1237 if(!result) {
1238 /*
1239 * Note that we may not send the entire chunk at once, and we have a set
1240 * number of data bytes at the end of the big buffer (out of which we may
1241 * only send away a part).
1242 */
1243 /* how much of the header that was sent */
1244 size_t headlen = (size_t)amount>headersize ? headersize : (size_t)amount;
1245 size_t bodylen = amount - headlen;
1246
1247 if(data->set.verbose) {
1248 /* this data _may_ contain binary stuff */
1249 Curl_debug(data, CURLINFO_HEADER_OUT, ptr, headlen);
1250 if(bodylen) {
1251 /* there was body data sent beyond the initial header part, pass that
1252 on to the debug callback too */
1253 Curl_debug(data, CURLINFO_DATA_OUT,
1254 ptr + headlen, bodylen);
1255 }
1256 }
1257
1258 /* 'amount' can never be a very large value here so typecasting it so a
1259 signed 31 bit value should not cause problems even if ssize_t is
1260 64bit */
1261 *bytes_written += (long)amount;
1262
1263 if(http) {
1264 /* if we sent a piece of the body here, up the byte counter for it
1265 accordingly */
1266 data->req.writebytecount += bodylen;
1267 Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
1268
1269 if((size_t)amount != size) {
1270 /* The whole request could not be sent in one system call. We must
1271 queue it up and send it later when we get the chance. We must not
1272 loop here and wait until it might work again. */
1273
1274 size -= amount;
1275
1276 ptr = in->buffer + amount;
1277
1278 /* backup the currently set pointers */
1279 http->backup.fread_func = data->state.fread_func;
1280 http->backup.fread_in = data->state.in;
1281 http->backup.postdata = http->postdata;
1282 http->backup.postsize = http->postsize;
1283
1284 /* set the new pointers for the request-sending */
1285 data->state.fread_func = (curl_read_callback)readmoredata;
1286 data->state.in = (void *)conn;
1287 http->postdata = ptr;
1288 http->postsize = (curl_off_t)size;
1289
1290 http->send_buffer = in;
1291 http->sending = HTTPSEND_REQUEST;
1292
1293 return CURLE_OK;
1294 }
1295 http->sending = HTTPSEND_BODY;
1296 /* the full buffer was sent, clean up and return */
1297 }
1298 else {
1299 if((size_t)amount != size)
1300 /* We have no continue-send mechanism now, fail. This can only happen
1301 when this function is used from the CONNECT sending function. We
1302 currently (stupidly) assume that the whole request is always sent
1303 away in the first single chunk.
1304
1305 This needs FIXing.
1306 */
1307 return CURLE_SEND_ERROR;
1308 }
1309 }
1310 Curl_add_buffer_free(&in);
1311
1312 return result;
1313 }
1314
1315
1316 /*
1317 * add_bufferf() add the formatted input to the buffer.
1318 */
1319 CURLcode Curl_add_bufferf(Curl_send_buffer **inp, const char *fmt, ...)
1320 {
1321 char *s;
1322 va_list ap;
1323 Curl_send_buffer *in = *inp;
1324 va_start(ap, fmt);
1325 s = vaprintf(fmt, ap); /* this allocs a new string to append */
1326 va_end(ap);
1327
1328 if(s) {
1329 CURLcode result = Curl_add_buffer(inp, s, strlen(s));
1330 free(s);
1331 return result;
1332 }
1333 /* If we failed, we cleanup the whole buffer and return error */
1334 free(in->buffer);
1335 free(in);
1336 *inp = NULL;
1337 return CURLE_OUT_OF_MEMORY;
1338 }
1339
1340 /*
1341 * Curl_add_buffer() appends a memory chunk to the existing buffer
1342 */
1343 CURLcode Curl_add_buffer(Curl_send_buffer **inp, const void *inptr,
1344 size_t size)
1345 {
1346 char *new_rb;
1347 Curl_send_buffer *in = *inp;
1348
1349 if(~size < in->size_used) {
1350 /* If resulting used size of send buffer would wrap size_t, cleanup
1351 the whole buffer and return error. Otherwise the required buffer
1352 size will fit into a single allocatable memory chunk */
1353 Curl_safefree(in->buffer);
1354 free(in);
1355 *inp = NULL;
1356 return CURLE_OUT_OF_MEMORY;
1357 }
1358
1359 if(!in->buffer ||
1360 ((in->size_used + size) > (in->size_max - 1))) {
1361 /* If current buffer size isn't enough to hold the result, use a
1362 buffer size that doubles the required size. If this new size
1363 would wrap size_t, then just use the largest possible one */
1364 size_t new_size;
1365
1366 if((size > (size_t)-1 / 2) || (in->size_used > (size_t)-1 / 2) ||
1367 (~(size * 2) < (in->size_used * 2)))
1368 new_size = (size_t)-1;
1369 else
1370 new_size = (in->size_used + size) * 2;
1371
1372 if(in->buffer)
1373 /* we have a buffer, enlarge the existing one */
1374 new_rb = Curl_saferealloc(in->buffer, new_size);
1375 else
1376 /* create a new buffer */
1377 new_rb = malloc(new_size);
1378
1379 if(!new_rb) {
1380 /* If we failed, we cleanup the whole buffer and return error */
1381 free(in);
1382 *inp = NULL;
1383 return CURLE_OUT_OF_MEMORY;
1384 }
1385
1386 in->buffer = new_rb;
1387 in->size_max = new_size;
1388 }
1389 memcpy(&in->buffer[in->size_used], inptr, size);
1390
1391 in->size_used += size;
1392
1393 return CURLE_OK;
1394 }
1395
1396 /* end of the add_buffer functions */
1397 /* ------------------------------------------------------------------------- */
1398
1399
1400
1401 /*
1402 * Curl_compareheader()
1403 *
1404 * Returns TRUE if 'headerline' contains the 'header' with given 'content'.
1405 * Pass headers WITH the colon.
1406 */
1407 bool
1408 Curl_compareheader(const char *headerline, /* line to check */
1409 const char *header, /* header keyword _with_ colon */
1410 const char *content) /* content string to find */
1411 {
1412 /* RFC2616, section 4.2 says: "Each header field consists of a name followed
1413 * by a colon (":") and the field value. Field names are case-insensitive.
1414 * The field value MAY be preceded by any amount of LWS, though a single SP
1415 * is preferred." */
1416
1417 size_t hlen = strlen(header);
1418 size_t clen;
1419 size_t len;
1420 const char *start;
1421 const char *end;
1422
1423 if(!strncasecompare(headerline, header, hlen))
1424 return FALSE; /* doesn't start with header */
1425
1426 /* pass the header */
1427 start = &headerline[hlen];
1428
1429 /* pass all white spaces */
1430 while(*start && ISSPACE(*start))
1431 start++;
1432
1433 /* find the end of the header line */
1434 end = strchr(start, '\r'); /* lines end with CRLF */
1435 if(!end) {
1436 /* in case there's a non-standard compliant line here */
1437 end = strchr(start, '\n');
1438
1439 if(!end)
1440 /* hm, there's no line ending here, use the zero byte! */
1441 end = strchr(start, '\0');
1442 }
1443
1444 len = end-start; /* length of the content part of the input line */
1445 clen = strlen(content); /* length of the word to find */
1446
1447 /* find the content string in the rest of the line */
1448 for(; len >= clen; len--, start++) {
1449 if(strncasecompare(start, content, clen))
1450 return TRUE; /* match! */
1451 }
1452
1453 return FALSE; /* no match */
1454 }
1455
1456 /*
1457 * Curl_http_connect() performs HTTP stuff to do at connect-time, called from
1458 * the generic Curl_connect().
1459 */
1460 CURLcode Curl_http_connect(struct connectdata *conn, bool *done)
1461 {
1462 CURLcode result;
1463
1464 /* We default to persistent connections. We set this already in this connect
1465 function to make the re-use checks properly be able to check this bit. */
1466 connkeep(conn, "HTTP default");
1467
1468 /* the CONNECT procedure might not have been completed */
1469 result = Curl_proxy_connect(conn, FIRSTSOCKET);
1470 if(result)
1471 return result;
1472
1473 if(conn->bits.proxy_connect_closed)
1474 /* this is not an error, just part of the connection negotiation */
1475 return CURLE_OK;
1476
1477 if(CONNECT_FIRSTSOCKET_PROXY_SSL())
1478 return CURLE_OK; /* wait for HTTPS proxy SSL initialization to complete */
1479
1480 if(Curl_connect_ongoing(conn))
1481 /* nothing else to do except wait right now - we're not done here. */
1482 return CURLE_OK;
1483
1484 #ifndef CURL_DISABLE_PROXY
1485 if(conn->data->set.haproxyprotocol) {
1486 /* add HAProxy PROXY protocol header */
1487 result = add_haproxy_protocol_header(conn);
1488 if(result)
1489 return result;
1490 }
1491 #endif
1492
1493 if(conn->given->protocol & CURLPROTO_HTTPS) {
1494 /* perform SSL initialization */
1495 result = https_connecting(conn, done);
1496 if(result)
1497 return result;
1498 }
1499 else
1500 *done = TRUE;
1501
1502 return CURLE_OK;
1503 }
1504
1505 /* this returns the socket to wait for in the DO and DOING state for the multi
1506 interface and then we're always _sending_ a request and thus we wait for
1507 the single socket to become writable only */
1508 static int http_getsock_do(struct connectdata *conn,
1509 curl_socket_t *socks)
1510 {
1511 /* write mode */
1512 socks[0] = conn->sock[FIRSTSOCKET];
1513 return GETSOCK_WRITESOCK(0);
1514 }
1515
1516 #ifndef CURL_DISABLE_PROXY
1517 static CURLcode add_haproxy_protocol_header(struct connectdata *conn)
1518 {
1519 char proxy_header[128];
1520 Curl_send_buffer *req_buffer;
1521 CURLcode result;
1522 char tcp_version[5];
1523
1524 /* Emit the correct prefix for IPv6 */
1525 if(conn->bits.ipv6) {
1526 strcpy(tcp_version, "TCP6");
1527 }
1528 else {
1529 strcpy(tcp_version, "TCP4");
1530 }
1531
1532 msnprintf(proxy_header,
1533 sizeof(proxy_header),
1534 "PROXY %s %s %s %li %li\r\n",
1535 tcp_version,
1536 conn->data->info.conn_local_ip,
1537 conn->data->info.conn_primary_ip,
1538 conn->data->info.conn_local_port,
1539 conn->data->info.conn_primary_port);
1540
1541 req_buffer = Curl_add_buffer_init();
1542 if(!req_buffer)
1543 return CURLE_OUT_OF_MEMORY;
1544
1545 result = Curl_add_bufferf(&req_buffer, proxy_header);
1546 if(result)
1547 return result;
1548
1549 result = Curl_add_buffer_send(&req_buffer,
1550 conn,
1551 &conn->data->info.request_size,
1552 0,
1553 FIRSTSOCKET);
1554
1555 return result;
1556 }
1557 #endif
1558
1559 #ifdef USE_SSL
1560 static CURLcode https_connecting(struct connectdata *conn, bool *done)
1561 {
1562 CURLcode result;
1563 DEBUGASSERT((conn) && (conn->handler->flags & PROTOPT_SSL));
1564
1565 #ifdef ENABLE_QUIC
1566 if(conn->transport == TRNSPRT_QUIC) {
1567 *done = TRUE;
1568 return CURLE_OK;
1569 }
1570 #endif
1571
1572 /* perform SSL initialization for this socket */
1573 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, done);
1574 if(result)
1575 connclose(conn, "Failed HTTPS connection");
1576
1577 return result;
1578 }
1579
1580 static int https_getsock(struct connectdata *conn,
1581 curl_socket_t *socks)
1582 {
1583 if(conn->handler->flags & PROTOPT_SSL)
1584 return Curl_ssl_getsock(conn, socks);
1585 return GETSOCK_BLANK;
1586 }
1587 #endif /* USE_SSL */
1588
1589 /*
1590 * Curl_http_done() gets called after a single HTTP request has been
1591 * performed.
1592 */
1593
1594 CURLcode Curl_http_done(struct connectdata *conn,
1595 CURLcode status, bool premature)
1596 {
1597 struct Curl_easy *data = conn->data;
1598 struct HTTP *http = data->req.protop;
1599
1600 /* Clear multipass flag. If authentication isn't done yet, then it will get
1601 * a chance to be set back to true when we output the next auth header */
1602 data->state.authhost.multipass = FALSE;
1603 data->state.authproxy.multipass = FALSE;
1604
1605 Curl_unencode_cleanup(conn);
1606
1607 /* set the proper values (possibly modified on POST) */
1608 conn->seek_func = data->set.seek_func; /* restore */
1609 conn->seek_client = data->set.seek_client; /* restore */
1610
1611 if(!http)
1612 return CURLE_OK;
1613
1614 if(http->send_buffer) {
1615 Curl_add_buffer_free(&http->send_buffer);
1616 }
1617
1618 Curl_http2_done(data, premature);
1619 Curl_quic_done(data, premature);
1620
1621 Curl_mime_cleanpart(&http->form);
1622
1623 if(status)
1624 return status;
1625
1626 if(!premature && /* this check is pointless when DONE is called before the
1627 entire operation is complete */
1628 !conn->bits.retry &&
1629 !data->set.connect_only &&
1630 (data->req.bytecount +
1631 data->req.headerbytecount -
1632 data->req.deductheadercount) <= 0) {
1633 /* If this connection isn't simply closed to be retried, AND nothing was
1634 read from the HTTP server (that counts), this can't be right so we
1635 return an error here */
1636 failf(data, "Empty reply from server");
1637 return CURLE_GOT_NOTHING;
1638 }
1639
1640 return CURLE_OK;
1641 }
1642
1643 /*
1644 * Determine if we should use HTTP 1.1 (OR BETTER) for this request. Reasons
1645 * to avoid it include:
1646 *
1647 * - if the user specifically requested HTTP 1.0
1648 * - if the server we are connected to only supports 1.0
1649 * - if any server previously contacted to handle this request only supports
1650 * 1.0.
1651 */
1652 static bool use_http_1_1plus(const struct Curl_easy *data,
1653 const struct connectdata *conn)
1654 {
1655 if((data->state.httpversion == 10) || (conn->httpversion == 10))
1656 return FALSE;
1657 if((data->set.httpversion == CURL_HTTP_VERSION_1_0) &&
1658 (conn->httpversion <= 10))
1659 return FALSE;
1660 return ((data->set.httpversion == CURL_HTTP_VERSION_NONE) ||
1661 (data->set.httpversion >= CURL_HTTP_VERSION_1_1));
1662 }
1663
1664 static const char *get_http_string(const struct Curl_easy *data,
1665 const struct connectdata *conn)
1666 {
1667 #ifdef ENABLE_QUIC
1668 if((data->set.httpversion == CURL_HTTP_VERSION_3) ||
1669 (conn->httpversion == 30))
1670 return "3";
1671 #endif
1672
1673 #ifdef USE_NGHTTP2
1674 if(conn->proto.httpc.h2)
1675 return "2";
1676 #endif
1677
1678 if(use_http_1_1plus(data, conn))
1679 return "1.1";
1680
1681 return "1.0";
1682 }
1683
1684 /* check and possibly add an Expect: header */
1685 static CURLcode expect100(struct Curl_easy *data,
1686 struct connectdata *conn,
1687 Curl_send_buffer *req_buffer)
1688 {
1689 CURLcode result = CURLE_OK;
1690 data->state.expect100header = FALSE; /* default to false unless it is set
1691 to TRUE below */
1692 if(!data->state.disableexpect && use_http_1_1plus(data, conn) &&
1693 (conn->httpversion < 20)) {
1694 /* if not doing HTTP 1.0 or version 2, or disabled explicitly, we add an
1695 Expect: 100-continue to the headers which actually speeds up post
1696 operations (as there is one packet coming back from the web server) */
1697 const char *ptr = Curl_checkheaders(conn, "Expect");
1698 if(ptr) {
1699 data->state.expect100header =
1700 Curl_compareheader(ptr, "Expect:", "100-continue");
1701 }
1702 else {
1703 result = Curl_add_bufferf(&req_buffer,
1704 "Expect: 100-continue\r\n");
1705 if(!result)
1706 data->state.expect100header = TRUE;
1707 }
1708 }
1709
1710 return result;
1711 }
1712
1713 enum proxy_use {
1714 HEADER_SERVER, /* direct to server */
1715 HEADER_PROXY, /* regular request to proxy */
1716 HEADER_CONNECT /* sending CONNECT to a proxy */
1717 };
1718
1719 /* used to compile the provided trailers into one buffer
1720 will return an error code if one of the headers is
1721 not formatted correctly */
1722 CURLcode Curl_http_compile_trailers(struct curl_slist *trailers,
1723 Curl_send_buffer **buffer,
1724 struct Curl_easy *handle)
1725 {
1726 char *ptr = NULL;
1727 CURLcode result = CURLE_OK;
1728 const char *endofline_native = NULL;
1729 const char *endofline_network = NULL;
1730
1731 if(
1732 #ifdef CURL_DO_LINEEND_CONV
1733 (handle->set.prefer_ascii) ||
1734 #endif
1735 (handle->set.crlf)) {
1736 /* \n will become \r\n later on */
1737 endofline_native = "\n";
1738 endofline_network = "\x0a";
1739 }
1740 else {
1741 endofline_native = "\r\n";
1742 endofline_network = "\x0d\x0a";
1743 }
1744
1745 while(trailers) {
1746 /* only add correctly formatted trailers */
1747 ptr = strchr(trailers->data, ':');
1748 if(ptr && *(ptr + 1) == ' ') {
1749 result = Curl_add_bufferf(buffer, "%s%s", trailers->data,
1750 endofline_native);
1751 if(result)
1752 return result;
1753 }
1754 else
1755 infof(handle, "Malformatted trailing header ! Skipping trailer.");
1756 trailers = trailers->next;
1757 }
1758 result = Curl_add_buffer(buffer, endofline_network,
1759 strlen(endofline_network));
1760 return result;
1761 }
1762
1763 CURLcode Curl_add_custom_headers(struct connectdata *conn,
1764 bool is_connect,
1765 Curl_send_buffer *req_buffer)
1766 {
1767 char *ptr;
1768 struct curl_slist *h[2];
1769 struct curl_slist *headers;
1770 int numlists = 1; /* by default */
1771 struct Curl_easy *data = conn->data;
1772 int i;
1773
1774 enum proxy_use proxy;
1775
1776 if(is_connect)
1777 proxy = HEADER_CONNECT;
1778 else
1779 proxy = conn->bits.httpproxy && !conn->bits.tunnel_proxy?
1780 HEADER_PROXY:HEADER_SERVER;
1781
1782 switch(proxy) {
1783 case HEADER_SERVER:
1784 h[0] = data->set.headers;
1785 break;
1786 case HEADER_PROXY:
1787 h[0] = data->set.headers;
1788 if(data->set.sep_headers) {
1789 h[1] = data->set.proxyheaders;
1790 numlists++;
1791 }
1792 break;
1793 case HEADER_CONNECT:
1794 if(data->set.sep_headers)
1795 h[0] = data->set.proxyheaders;
1796 else
1797 h[0] = data->set.headers;
1798 break;
1799 }
1800
1801 /* loop through one or two lists */
1802 for(i = 0; i < numlists; i++) {
1803 headers = h[i];
1804
1805 while(headers) {
1806 char *semicolonp = NULL;
1807 ptr = strchr(headers->data, ':');
1808 if(!ptr) {
1809 char *optr;
1810 /* no colon, semicolon? */
1811 ptr = strchr(headers->data, ';');
1812 if(ptr) {
1813 optr = ptr;
1814 ptr++; /* pass the semicolon */
1815 while(*ptr && ISSPACE(*ptr))
1816 ptr++;
1817
1818 if(*ptr) {
1819 /* this may be used for something else in the future */
1820 optr = NULL;
1821 }
1822 else {
1823 if(*(--ptr) == ';') {
1824 /* copy the source */
1825 semicolonp = strdup(headers->data);
1826 if(!semicolonp) {
1827 Curl_add_buffer_free(&req_buffer);
1828 return CURLE_OUT_OF_MEMORY;
1829 }
1830 /* put a colon where the semicolon is */
1831 semicolonp[ptr - headers->data] = ':';
1832 /* point at the colon */
1833 optr = &semicolonp [ptr - headers->data];
1834 }
1835 }
1836 ptr = optr;
1837 }
1838 }
1839 if(ptr) {
1840 /* we require a colon for this to be a true header */
1841
1842 ptr++; /* pass the colon */
1843 while(*ptr && ISSPACE(*ptr))
1844 ptr++;
1845
1846 if(*ptr || semicolonp) {
1847 /* only send this if the contents was non-blank or done special */
1848 CURLcode result = CURLE_OK;
1849 char *compare = semicolonp ? semicolonp : headers->data;
1850
1851 if(conn->allocptr.host &&
1852 /* a Host: header was sent already, don't pass on any custom Host:
1853 header as that will produce *two* in the same request! */
1854 checkprefix("Host:", compare))
1855 ;
1856 else if(data->set.httpreq == HTTPREQ_POST_FORM &&
1857 /* this header (extended by formdata.c) is sent later */
1858 checkprefix("Content-Type:", compare))
1859 ;
1860 else if(data->set.httpreq == HTTPREQ_POST_MIME &&
1861 /* this header is sent later */
1862 checkprefix("Content-Type:", compare))
1863 ;
1864 else if(conn->bits.authneg &&
1865 /* while doing auth neg, don't allow the custom length since
1866 we will force length zero then */
1867 checkprefix("Content-Length:", compare))
1868 ;
1869 else if(conn->allocptr.te &&
1870 /* when asking for Transfer-Encoding, don't pass on a custom
1871 Connection: */
1872 checkprefix("Connection:", compare))
1873 ;
1874 else if((conn->httpversion >= 20) &&
1875 checkprefix("Transfer-Encoding:", compare))
1876 /* HTTP/2 doesn't support chunked requests */
1877 ;
1878 else if((checkprefix("Authorization:", compare) ||
1879 checkprefix("Cookie:", compare)) &&
1880 /* be careful of sending this potentially sensitive header to
1881 other hosts */
1882 (data->state.this_is_a_follow &&
1883 data->state.first_host &&
1884 !data->set.allow_auth_to_other_hosts &&
1885 !strcasecompare(data->state.first_host, conn->host.name)))
1886 ;
1887 else {
1888 result = Curl_add_bufferf(&req_buffer, "%s\r\n", compare);
1889 }
1890 if(semicolonp)
1891 free(semicolonp);
1892 if(result)
1893 return result;
1894 }
1895 }
1896 headers = headers->next;
1897 }
1898 }
1899
1900 return CURLE_OK;
1901 }
1902
1903 #ifndef CURL_DISABLE_PARSEDATE
1904 CURLcode Curl_add_timecondition(const struct connectdata *conn,
1905 Curl_send_buffer *req_buffer)
1906 {
1907 struct Curl_easy *data = conn->data;
1908 const struct tm *tm;
1909 struct tm keeptime;
1910 CURLcode result;
1911 char datestr[80];
1912 const char *condp;
1913
1914 if(data->set.timecondition == CURL_TIMECOND_NONE)
1915 /* no condition was asked for */
1916 return CURLE_OK;
1917
1918 result = Curl_gmtime(data->set.timevalue, &keeptime);
1919 if(result) {
1920 failf(data, "Invalid TIMEVALUE");
1921 return result;
1922 }
1923 tm = &keeptime;
1924
1925 switch(data->set.timecondition) {
1926 default:
1927 return CURLE_BAD_FUNCTION_ARGUMENT;
1928
1929 case CURL_TIMECOND_IFMODSINCE:
1930 condp = "If-Modified-Since";
1931 break;
1932 case CURL_TIMECOND_IFUNMODSINCE:
1933 condp = "If-Unmodified-Since";
1934 break;
1935 case CURL_TIMECOND_LASTMOD:
1936 condp = "Last-Modified";
1937 break;
1938 }
1939
1940 if(Curl_checkheaders(conn, condp)) {
1941 /* A custom header was specified; it will be sent instead. */
1942 return CURLE_OK;
1943 }
1944
1945 /* The If-Modified-Since header family should have their times set in
1946 * GMT as RFC2616 defines: "All HTTP date/time stamps MUST be
1947 * represented in Greenwich Mean Time (GMT), without exception. For the
1948 * purposes of HTTP, GMT is exactly equal to UTC (Coordinated Universal
1949 * Time)." (see page 20 of RFC2616).
1950 */
1951
1952 /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
1953 msnprintf(datestr, sizeof(datestr),
1954 "%s: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
1955 condp,
1956 Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
1957 tm->tm_mday,
1958 Curl_month[tm->tm_mon],
1959 tm->tm_year + 1900,
1960 tm->tm_hour,
1961 tm->tm_min,
1962 tm->tm_sec);
1963
1964 result = Curl_add_buffer(&req_buffer, datestr, strlen(datestr));
1965
1966 return result;
1967 }
1968 #else
1969 /* disabled */
1970 CURLcode Curl_add_timecondition(const struct connectdata *conn,
1971 Curl_send_buffer *req_buffer)
1972 {
1973 (void)conn;
1974 (void)req_buffer;
1975 return CURLE_OK;
1976 }
1977 #endif
1978
1979 /*
1980 * Curl_http() gets called from the generic multi_do() function when a HTTP
1981 * request is to be performed. This creates and sends a properly constructed
1982 * HTTP request.
1983 */
1984 CURLcode Curl_http(struct connectdata *conn, bool *done)
1985 {
1986 struct Curl_easy *data = conn->data;
1987 CURLcode result = CURLE_OK;
1988 struct HTTP *http;
1989 const char *path = data->state.up.path;
1990 const char *query = data->state.up.query;
1991 bool paste_ftp_userpwd = FALSE;
1992 char ftp_typecode[sizeof("/;type=?")] = "";
1993 const char *host = conn->host.name;
1994 const char *te = ""; /* transfer-encoding */
1995 const char *ptr;
1996 const char *request;
1997 Curl_HttpReq httpreq = data->set.httpreq;
1998 #if !defined(CURL_DISABLE_COOKIES)
1999 char *addcookies = NULL;
2000 #endif
2001 curl_off_t included_body = 0;
2002 const char *httpstring;
2003 Curl_send_buffer *req_buffer;
2004 curl_off_t postsize = 0; /* curl_off_t to handle large file sizes */
2005 char *altused = NULL;
2006
2007 /* Always consider the DO phase done after this function call, even if there
2008 may be parts of the request that is not yet sent, since we can deal with
2009 the rest of the request in the PERFORM phase. */
2010 *done = TRUE;
2011
2012 if(conn->transport != TRNSPRT_QUIC) {
2013 if(conn->httpversion < 20) { /* unless the connection is re-used and
2014 already http2 */
2015 switch(conn->negnpn) {
2016 case CURL_HTTP_VERSION_2:
2017 conn->httpversion = 20; /* we know we're on HTTP/2 now */
2018
2019 result = Curl_http2_switched(conn, NULL, 0);
2020 if(result)
2021 return result;
2022 break;
2023 case CURL_HTTP_VERSION_1_1:
2024 /* continue with HTTP/1.1 when explicitly requested */
2025 break;
2026 default:
2027 /* Check if user wants to use HTTP/2 with clear TCP*/
2028 #ifdef USE_NGHTTP2
2029 if(conn->data->set.httpversion ==
2030 CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE) {
2031 if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
2032 /* We don't support HTTP/2 proxies yet. Also it's debatable
2033 whether or not this setting should apply to HTTP/2 proxies. */
2034 infof(data, "Ignoring HTTP/2 prior knowledge due to proxy\n");
2035 break;
2036 }
2037
2038 DEBUGF(infof(data, "HTTP/2 over clean TCP\n"));
2039 conn->httpversion = 20;
2040
2041 result = Curl_http2_switched(conn, NULL, 0);
2042 if(result)
2043 return result;
2044 }
2045 #endif
2046 break;
2047 }
2048 }
2049 else {
2050 /* prepare for a http2 request */
2051 result = Curl_http2_setup(conn);
2052 if(result)
2053 return result;
2054 }
2055 }
2056 http = data->req.protop;
2057 DEBUGASSERT(http);
2058
2059 if(!data->state.this_is_a_follow) {
2060 /* Free to avoid leaking memory on multiple requests*/
2061 free(data->state.first_host);
2062
2063 data->state.first_host = strdup(conn->host.name);
2064 if(!data->state.first_host)
2065 return CURLE_OUT_OF_MEMORY;
2066
2067 data->state.first_remote_port = conn->remote_port;
2068 }
2069
2070 if((conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_FTP)) &&
2071 data->set.upload) {
2072 httpreq = HTTPREQ_PUT;
2073 }
2074
2075 /* Now set the 'request' pointer to the proper request string */
2076 if(data->set.str[STRING_CUSTOMREQUEST])
2077 request = data->set.str[STRING_CUSTOMREQUEST];
2078 else {
2079 if(data->set.opt_no_body)
2080 request = "HEAD";
2081 else {
2082 DEBUGASSERT((httpreq > HTTPREQ_NONE) && (httpreq < HTTPREQ_LAST));
2083 switch(httpreq) {
2084 case HTTPREQ_POST:
2085 case HTTPREQ_POST_FORM:
2086 case HTTPREQ_POST_MIME:
2087 request = "POST";
2088 break;
2089 case HTTPREQ_PUT:
2090 request = "PUT";
2091 break;
2092 case HTTPREQ_OPTIONS:
2093 request = "OPTIONS";
2094 break;
2095 default: /* this should never happen */
2096 case HTTPREQ_GET:
2097 request = "GET";
2098 break;
2099 case HTTPREQ_HEAD:
2100 request = "HEAD";
2101 break;
2102 }
2103 }
2104 }
2105
2106 /* The User-Agent string might have been allocated in url.c already, because
2107 it might have been used in the proxy connect, but if we have got a header
2108 with the user-agent string specified, we erase the previously made string
2109 here. */
2110 if(Curl_checkheaders(conn, "User-Agent")) {
2111 free(conn->allocptr.uagent);
2112 conn->allocptr.uagent = NULL;
2113 }
2114
2115 /* setup the authentication headers */
2116 {
2117 char *pq = NULL;
2118 if(query && *query) {
2119 pq = aprintf("%s?%s", path, query);
2120 if(!pq)
2121 return CURLE_OUT_OF_MEMORY;
2122 }
2123 result = Curl_http_output_auth(conn, request, (pq ? pq : path), FALSE);
2124 free(pq);
2125 if(result)
2126 return result;
2127 }
2128
2129 if(((data->state.authhost.multipass && !data->state.authhost.done)
2130 || (data->state.authproxy.multipass && !data->state.authproxy.done)) &&
2131 (httpreq != HTTPREQ_GET) &&
2132 (httpreq != HTTPREQ_HEAD)) {
2133 /* Auth is required and we are not authenticated yet. Make a PUT or POST
2134 with content-length zero as a "probe". */
2135 conn->bits.authneg = TRUE;
2136 }
2137 else
2138 conn->bits.authneg = FALSE;
2139
2140 Curl_safefree(conn->allocptr.ref);
2141 if(data->change.referer && !Curl_checkheaders(conn, "Referer")) {
2142 conn->allocptr.ref = aprintf("Referer: %s\r\n", data->change.referer);
2143 if(!conn->allocptr.ref)
2144 return CURLE_OUT_OF_MEMORY;
2145 }
2146 else
2147 conn->allocptr.ref = NULL;
2148
2149 #if !defined(CURL_DISABLE_COOKIES)
2150 if(data->set.str[STRING_COOKIE] && !Curl_checkheaders(conn, "Cookie"))
2151 addcookies = data->set.str[STRING_COOKIE];
2152 #endif
2153
2154 if(!Curl_checkheaders(conn, "Accept-Encoding") &&
2155 data->set.str[STRING_ENCODING]) {
2156 Curl_safefree(conn->allocptr.accept_encoding);
2157 conn->allocptr.accept_encoding =
2158 aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
2159 if(!conn->allocptr.accept_encoding)
2160 return CURLE_OUT_OF_MEMORY;
2161 }
2162 else {
2163 Curl_safefree(conn->allocptr.accept_encoding);
2164 conn->allocptr.accept_encoding = NULL;
2165 }
2166
2167 #ifdef HAVE_LIBZ
2168 /* we only consider transfer-encoding magic if libz support is built-in */
2169
2170 if(!Curl_checkheaders(conn, "TE") &&
2171 data->set.http_transfer_encoding) {
2172 /* When we are to insert a TE: header in the request, we must also insert
2173 TE in a Connection: header, so we need to merge the custom provided
2174 Connection: header and prevent the original to get sent. Note that if
2175 the user has inserted his/hers own TE: header we don't do this magic
2176 but then assume that the user will handle it all! */
2177 char *cptr = Curl_checkheaders(conn, "Connection");
2178 #define TE_HEADER "TE: gzip\r\n"
2179
2180 Curl_safefree(conn->allocptr.te);
2181
2182 if(cptr) {
2183 cptr = Curl_copy_header_value(cptr);
2184 if(!cptr)
2185 return CURLE_OUT_OF_MEMORY;
2186 }
2187
2188 /* Create the (updated) Connection: header */
2189 conn->allocptr.te = aprintf("Connection: %s%sTE\r\n" TE_HEADER,
2190 cptr ? cptr : "", (cptr && *cptr) ? ", ":"");
2191
2192 free(cptr);
2193 if(!conn->allocptr.te)
2194 return CURLE_OUT_OF_MEMORY;
2195 }
2196 #endif
2197
2198 switch(httpreq) {
2199 case HTTPREQ_POST_MIME:
2200 http->sendit = &data->set.mimepost;
2201 break;
2202 case HTTPREQ_POST_FORM:
2203 /* Convert the form structure into a mime structure. */
2204 Curl_mime_cleanpart(&http->form);
2205 result = Curl_getformdata(data, &http->form, data->set.httppost,
2206 data->state.fread_func);
2207 if(result)
2208 return result;
2209 http->sendit = &http->form;
2210 break;
2211 default:
2212 http->sendit = NULL;
2213 }
2214
2215 #ifndef CURL_DISABLE_MIME
2216 if(http->sendit) {
2217 const char *cthdr = Curl_checkheaders(conn, "Content-Type");
2218
2219 /* Read and seek body only. */
2220 http->sendit->flags |= MIME_BODY_ONLY;
2221
2222 /* Prepare the mime structure headers & set content type. */
2223
2224 if(cthdr)
2225 for(cthdr += 13; *cthdr == ' '; cthdr++)
2226 ;
2227 else if(http->sendit->kind == MIMEKIND_MULTIPART)
2228 cthdr = "multipart/form-data";
2229
2230 curl_mime_headers(http->sendit, data->set.headers, 0);
2231 result = Curl_mime_prepare_headers(http->sendit, cthdr,
2232 NULL, MIMESTRATEGY_FORM);
2233 curl_mime_headers(http->sendit, NULL, 0);
2234 if(!result)
2235 result = Curl_mime_rewind(http->sendit);
2236 if(result)
2237 return result;
2238 http->postsize = Curl_mime_size(http->sendit);
2239 }
2240 #endif
2241
2242 ptr = Curl_checkheaders(conn, "Transfer-Encoding");
2243 if(ptr) {
2244 /* Some kind of TE is requested, check if 'chunked' is chosen */
2245 data->req.upload_chunky =
2246 Curl_compareheader(ptr, "Transfer-Encoding:", "chunked");
2247 }
2248 else {
2249 if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
2250 (((httpreq == HTTPREQ_POST_MIME || httpreq == HTTPREQ_POST_FORM) &&
2251 http->postsize < 0) ||
2252 ((data->set.upload || httpreq == HTTPREQ_POST) &&
2253 data->state.infilesize == -1))) {
2254 if(conn->bits.authneg)
2255 /* don't enable chunked during auth neg */
2256 ;
2257 else if(use_http_1_1plus(data, conn)) {
2258 if(conn->httpversion < 20)
2259 /* HTTP, upload, unknown file size and not HTTP 1.0 */
2260 data->req.upload_chunky = TRUE;
2261 }
2262 else {
2263 failf(data, "Chunky upload is not supported by HTTP 1.0");
2264 return CURLE_UPLOAD_FAILED;
2265 }
2266 }
2267 else {
2268 /* else, no chunky upload */
2269 data->req.upload_chunky = FALSE;
2270 }
2271
2272 if(data->req.upload_chunky)
2273 te = "Transfer-Encoding: chunked\r\n";
2274 }
2275
2276 Curl_safefree(conn->allocptr.host);
2277
2278 ptr = Curl_checkheaders(conn, "Host");
2279 if(ptr && (!data->state.this_is_a_follow ||
2280 strcasecompare(data->state.first_host, conn->host.name))) {
2281 #if !defined(CURL_DISABLE_COOKIES)
2282 /* If we have a given custom Host: header, we extract the host name in
2283 order to possibly use it for cookie reasons later on. We only allow the
2284 custom Host: header if this is NOT a redirect, as setting Host: in the
2285 redirected request is being out on thin ice. Except if the host name
2286 is the same as the first one! */
2287 char *cookiehost = Curl_copy_header_value(ptr);
2288 if(!cookiehost)
2289 return CURLE_OUT_OF_MEMORY;
2290 if(!*cookiehost)
2291 /* ignore empty data */
2292 free(cookiehost);
2293 else {
2294 /* If the host begins with '[', we start searching for the port after
2295 the bracket has been closed */
2296 if(*cookiehost == '[') {
2297 char *closingbracket;
2298 /* since the 'cookiehost' is an allocated memory area that will be
2299 freed later we cannot simply increment the pointer */
2300 memmove(cookiehost, cookiehost + 1, strlen(cookiehost) - 1);
2301 closingbracket = strchr(cookiehost, ']');
2302 if(closingbracket)
2303 *closingbracket = 0;
2304 }
2305 else {
2306 int startsearch = 0;
2307 char *colon = strchr(cookiehost + startsearch, ':');
2308 if(colon)
2309 *colon = 0; /* The host must not include an embedded port number */
2310 }
2311 Curl_safefree(conn->allocptr.cookiehost);
2312 conn->allocptr.cookiehost = cookiehost;
2313 }
2314 #endif
2315
2316 if(strcmp("Host:", ptr)) {
2317 conn->allocptr.host = aprintf("Host:%s\r\n", &ptr[5]);
2318 if(!conn->allocptr.host)
2319 return CURLE_OUT_OF_MEMORY;
2320 }
2321 else
2322 /* when clearing the header */
2323 conn->allocptr.host = NULL;
2324 }
2325 else {
2326 /* When building Host: headers, we must put the host name within
2327 [brackets] if the host name is a plain IPv6-address. RFC2732-style. */
2328
2329 if(((conn->given->protocol&CURLPROTO_HTTPS) &&
2330 (conn->remote_port == PORT_HTTPS)) ||
2331 ((conn->given->protocol&CURLPROTO_HTTP) &&
2332 (conn->remote_port == PORT_HTTP)) )
2333 /* if(HTTPS on port 443) OR (HTTP on port 80) then don't include
2334 the port number in the host string */
2335 conn->allocptr.host = aprintf("Host: %s%s%s\r\n",
2336 conn->bits.ipv6_ip?"[":"",
2337 host,
2338 conn->bits.ipv6_ip?"]":"");
2339 else
2340 conn->allocptr.host = aprintf("Host: %s%s%s:%d\r\n",
2341 conn->bits.ipv6_ip?"[":"",
2342 host,
2343 conn->bits.ipv6_ip?"]":"",
2344 conn->remote_port);
2345
2346 if(!conn->allocptr.host)
2347 /* without Host: we can't make a nice request */
2348 return CURLE_OUT_OF_MEMORY;
2349 }
2350
2351 #ifndef CURL_DISABLE_PROXY
2352 if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
2353 /* Using a proxy but does not tunnel through it */
2354
2355 /* The path sent to the proxy is in fact the entire URL. But if the remote
2356 host is a IDN-name, we must make sure that the request we produce only
2357 uses the encoded host name! */
2358
2359 /* and no fragment part */
2360 CURLUcode uc;
2361 CURLU *h = curl_url_dup(data->state.uh);
2362 if(!h)
2363 return CURLE_OUT_OF_MEMORY;
2364
2365 if(conn->host.dispname != conn->host.name) {
2366 uc = curl_url_set(h, CURLUPART_HOST, conn->host.name, 0);
2367 if(uc) {
2368 curl_url_cleanup(h);
2369 return CURLE_OUT_OF_MEMORY;
2370 }
2371 }
2372 uc = curl_url_set(h, CURLUPART_FRAGMENT, NULL, 0);
2373 if(uc) {
2374 curl_url_cleanup(h);
2375 return CURLE_OUT_OF_MEMORY;
2376 }
2377
2378 if(strcasecompare("http", data->state.up.scheme)) {
2379 /* when getting HTTP, we don't want the userinfo the URL */
2380 uc = curl_url_set(h, CURLUPART_USER, NULL, 0);
2381 if(uc) {
2382 curl_url_cleanup(h);
2383 return CURLE_OUT_OF_MEMORY;
2384 }
2385 uc = curl_url_set(h, CURLUPART_PASSWORD, NULL, 0);
2386 if(uc) {
2387 curl_url_cleanup(h);
2388 return CURLE_OUT_OF_MEMORY;
2389 }
2390 }
2391 /* Extract the URL to use in the request. Store in STRING_TEMP_URL for
2392 clean-up reasons if the function returns before the free() further
2393 down. */
2394 uc = curl_url_get(h, CURLUPART_URL, &data->set.str[STRING_TEMP_URL], 0);
2395 if(uc) {
2396 curl_url_cleanup(h);
2397 return CURLE_OUT_OF_MEMORY;
2398 }
2399
2400 curl_url_cleanup(h);
2401
2402 if(strcasecompare("ftp", data->state.up.scheme)) {
2403 if(data->set.proxy_transfer_mode) {
2404 /* when doing ftp, append ;type=<a|i> if not present */
2405 char *type = strstr(path, ";type=");
2406 if(type && type[6] && type[7] == 0) {
2407 switch(Curl_raw_toupper(type[6])) {
2408 case 'A':
2409 case 'D':
2410 case 'I':
2411 break;
2412 default:
2413 type = NULL;
2414 }
2415 }
2416 if(!type) {
2417 char *p = ftp_typecode;
2418 /* avoid sending invalid URLs like ftp://example.com;type=i if the
2419 * user specified ftp://example.com without the slash */
2420 if(!*data->state.up.path && path[strlen(path) - 1] != '/') {
2421 *p++ = '/';
2422 }
2423 msnprintf(p, sizeof(ftp_typecode) - 1, ";type=%c",
2424 data->set.prefer_ascii ? 'a' : 'i');
2425 }
2426 }
2427 if(conn->bits.user_passwd && !conn->bits.userpwd_in_url)
2428 paste_ftp_userpwd = TRUE;
2429 }
2430 }
2431 #endif /* CURL_DISABLE_PROXY */
2432
2433 http->p_accept = Curl_checkheaders(conn, "Accept")?NULL:"Accept: */*\r\n";
2434
2435 if((HTTPREQ_POST == httpreq || HTTPREQ_PUT == httpreq) &&
2436 data->state.resume_from) {
2437 /**********************************************************************
2438 * Resuming upload in HTTP means that we PUT or POST and that we have
2439 * got a resume_from value set. The resume value has already created
2440 * a Range: header that will be passed along. We need to "fast forward"
2441 * the file the given number of bytes and decrease the assume upload
2442 * file size before we continue this venture in the dark lands of HTTP.
2443 * Resuming mime/form posting at an offset > 0 has no sense and is ignored.
2444 *********************************************************************/
2445
2446 if(data->state.resume_from < 0) {
2447 /*
2448 * This is meant to get the size of the present remote-file by itself.
2449 * We don't support this now. Bail out!
2450 */
2451 data->state.resume_from = 0;
2452 }
2453
2454 if(data->state.resume_from && !data->state.this_is_a_follow) {
2455 /* do we still game? */
2456
2457 /* Now, let's read off the proper amount of bytes from the
2458 input. */
2459 int seekerr = CURL_SEEKFUNC_CANTSEEK;
2460 if(conn->seek_func) {
2461 Curl_set_in_callback(data, true);
2462 seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
2463 SEEK_SET);
2464 Curl_set_in_callback(data, false);
2465 }
2466
2467 if(seekerr != CURL_SEEKFUNC_OK) {
2468 curl_off_t passed = 0;
2469
2470 if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
2471 failf(data, "Could not seek stream");
2472 return CURLE_READ_ERROR;
2473 }
2474 /* when seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
2475 do {
2476 size_t readthisamountnow =
2477 (data->state.resume_from - passed > data->set.buffer_size) ?
2478 (size_t)data->set.buffer_size :
2479 curlx_sotouz(data->state.resume_from - passed);
2480
2481 size_t actuallyread =
2482 data->state.fread_func(data->state.buffer, 1, readthisamountnow,
2483 data->state.in);
2484
2485 passed += actuallyread;
2486 if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
2487 /* this checks for greater-than only to make sure that the
2488 CURL_READFUNC_ABORT return code still aborts */
2489 failf(data, "Could only read %" CURL_FORMAT_CURL_OFF_T
2490 " bytes from the input", passed);
2491 return CURLE_READ_ERROR;
2492 }
2493 } while(passed < data->state.resume_from);
2494 }
2495
2496 /* now, decrease the size of the read */
2497 if(data->state.infilesize>0) {
2498 data->state.infilesize -= data->state.resume_from;
2499
2500 if(data->state.infilesize <= 0) {
2501 failf(data, "File already completely uploaded");
2502 return CURLE_PARTIAL_FILE;
2503 }
2504 }
2505 /* we've passed, proceed as normal */
2506 }
2507 }
2508 if(data->state.use_range) {
2509 /*
2510 * A range is selected. We use different headers whether we're downloading
2511 * or uploading and we always let customized headers override our internal
2512 * ones if any such are specified.
2513 */
2514 if(((httpreq == HTTPREQ_GET) || (httpreq == HTTPREQ_HEAD)) &&
2515 !Curl_checkheaders(conn, "Range")) {
2516 /* if a line like this was already allocated, free the previous one */
2517 free(conn->allocptr.rangeline);
2518 conn->allocptr.rangeline = aprintf("Range: bytes=%s\r\n",
2519 data->state.range);
2520 }
2521 else if((httpreq == HTTPREQ_POST || httpreq == HTTPREQ_PUT) &&
2522 !Curl_checkheaders(conn, "Content-Range")) {
2523
2524 /* if a line like this was already allocated, free the previous one */
2525 free(conn->allocptr.rangeline);
2526
2527 if(data->set.set_resume_from < 0) {
2528 /* Upload resume was asked for, but we don't know the size of the
2529 remote part so we tell the server (and act accordingly) that we
2530 upload the whole file (again) */
2531 conn->allocptr.rangeline =
2532 aprintf("Content-Range: bytes 0-%" CURL_FORMAT_CURL_OFF_T
2533 "/%" CURL_FORMAT_CURL_OFF_T "\r\n",
2534 data->state.infilesize - 1, data->state.infilesize);
2535
2536 }
2537 else if(data->state.resume_from) {
2538 /* This is because "resume" was selected */
2539 curl_off_t total_expected_size =
2540 data->state.resume_from + data->state.infilesize;
2541 conn->allocptr.rangeline =
2542 aprintf("Content-Range: bytes %s%" CURL_FORMAT_CURL_OFF_T
2543 "/%" CURL_FORMAT_CURL_OFF_T "\r\n",
2544 data->state.range, total_expected_size-1,
2545 total_expected_size);
2546 }
2547 else {
2548 /* Range was selected and then we just pass the incoming range and
2549 append total size */
2550 conn->allocptr.rangeline =
2551 aprintf("Content-Range: bytes %s/%" CURL_FORMAT_CURL_OFF_T "\r\n",
2552 data->state.range, data->state.infilesize);
2553 }
2554 if(!conn->allocptr.rangeline)
2555 return CURLE_OUT_OF_MEMORY;
2556 }
2557 }
2558
2559 httpstring = get_http_string(data, conn);
2560
2561 /* initialize a dynamic send-buffer */
2562 req_buffer = Curl_add_buffer_init();
2563
2564 if(!req_buffer)
2565 return CURLE_OUT_OF_MEMORY;
2566
2567 /* add the main request stuff */
2568 /* GET/HEAD/POST/PUT */
2569 result = Curl_add_bufferf(&req_buffer, "%s ", request);
2570 if(result)
2571 return result;
2572
2573 if(data->set.str[STRING_TARGET]) {
2574 path = data->set.str[STRING_TARGET];
2575 query = NULL;
2576 }
2577
2578 #ifndef CURL_DISABLE_PROXY
2579 /* url */
2580 if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
2581 char *url = data->set.str[STRING_TEMP_URL];
2582 result = Curl_add_buffer(&req_buffer, url, strlen(url));
2583 Curl_safefree(data->set.str[STRING_TEMP_URL]);
2584 }
2585 else
2586 #endif
2587 if(paste_ftp_userpwd)
2588 result = Curl_add_bufferf(&req_buffer, "ftp://%s:%s@%s",
2589 conn->user, conn->passwd,
2590 path + sizeof("ftp://") - 1);
2591 else {
2592 result = Curl_add_buffer(&req_buffer, path, strlen(path));
2593 if(result)
2594 return result;
2595 if(query)
2596 result = Curl_add_bufferf(&req_buffer, "?%s", query);
2597 }
2598 if(result)
2599 return result;
2600
2601 #ifdef USE_ALTSVC
2602 if(conn->bits.altused && !Curl_checkheaders(conn, "Alt-Used")) {
2603 altused = aprintf("Alt-Used: %s:%d\r\n",
2604 conn->conn_to_host.name, conn->conn_to_port);
2605 if(!altused)
2606 return CURLE_OUT_OF_MEMORY;
2607 }
2608 #endif
2609 result =
2610 Curl_add_bufferf(&req_buffer,
2611 "%s" /* ftp typecode (;type=x) */
2612 " HTTP/%s\r\n" /* HTTP version */
2613 "%s" /* host */
2614 "%s" /* proxyuserpwd */
2615 "%s" /* userpwd */
2616 "%s" /* range */
2617 "%s" /* user agent */
2618 "%s" /* accept */
2619 "%s" /* TE: */
2620 "%s" /* accept-encoding */
2621 "%s" /* referer */
2622 "%s" /* Proxy-Connection */
2623 "%s" /* transfer-encoding */
2624 "%s",/* Alt-Used */
2625
2626 ftp_typecode,
2627 httpstring,
2628 (conn->allocptr.host?conn->allocptr.host:""),
2629 conn->allocptr.proxyuserpwd?
2630 conn->allocptr.proxyuserpwd:"",
2631 conn->allocptr.userpwd?conn->allocptr.userpwd:"",
2632 (data->state.use_range && conn->allocptr.rangeline)?
2633 conn->allocptr.rangeline:"",
2634 (data->set.str[STRING_USERAGENT] &&
2635 *data->set.str[STRING_USERAGENT] &&
2636 conn->allocptr.uagent)?
2637 conn->allocptr.uagent:"",
2638 http->p_accept?http->p_accept:"",
2639 conn->allocptr.te?conn->allocptr.te:"",
2640 (data->set.str[STRING_ENCODING] &&
2641 *data->set.str[STRING_ENCODING] &&
2642 conn->allocptr.accept_encoding)?
2643 conn->allocptr.accept_encoding:"",
2644 (data->change.referer && conn->allocptr.ref)?
2645 conn->allocptr.ref:"" /* Referer: <data> */,
2646 (conn->bits.httpproxy &&
2647 !conn->bits.tunnel_proxy &&
2648 !Curl_checkProxyheaders(conn, "Proxy-Connection"))?
2649 "Proxy-Connection: Keep-Alive\r\n":"",
2650 te,
2651 altused ? altused : ""
2652 );
2653
2654 /* clear userpwd and proxyuserpwd to avoid re-using old credentials
2655 * from re-used connections */
2656 Curl_safefree(conn->allocptr.userpwd);
2657 Curl_safefree(conn->allocptr.proxyuserpwd);
2658 free(altused);
2659
2660 if(result)
2661 return result;
2662
2663 if(!(conn->handler->flags&PROTOPT_SSL) &&
2664 conn->httpversion != 20 &&
2665 (data->set.httpversion == CURL_HTTP_VERSION_2)) {
2666 /* append HTTP2 upgrade magic stuff to the HTTP request if it isn't done
2667 over SSL */
2668 result = Curl_http2_request_upgrade(req_buffer, conn);
2669 if(result)
2670 return result;
2671 }
2672
2673 #if !defined(CURL_DISABLE_COOKIES)
2674 if(data->cookies || addcookies) {
2675 struct Cookie *co = NULL; /* no cookies from start */
2676 int count = 0;
2677
2678 if(data->cookies && data->state.cookie_engine) {
2679 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
2680 co = Curl_cookie_getlist(data->cookies,
2681 conn->allocptr.cookiehost?
2682 conn->allocptr.cookiehost:host,
2683 data->state.up.path,
2684 (conn->handler->protocol&CURLPROTO_HTTPS)?
2685 TRUE:FALSE);
2686 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
2687 }
2688 if(co) {
2689 struct Cookie *store = co;
2690 /* now loop through all cookies that matched */
2691 while(co) {
2692 if(co->value) {
2693 if(0 == count) {
2694 result = Curl_add_bufferf(&req_buffer, "Cookie: ");
2695 if(result)
2696 break;
2697 }
2698 result = Curl_add_bufferf(&req_buffer,
2699 "%s%s=%s", count?"; ":"",
2700 co->name, co->value);
2701 if(result)
2702 break;
2703 count++;
2704 }
2705 co = co->next; /* next cookie please */
2706 }
2707 Curl_cookie_freelist(store);
2708 }
2709 if(addcookies && !result) {
2710 if(!count)
2711 result = Curl_add_bufferf(&req_buffer, "Cookie: ");
2712 if(!result) {
2713 result = Curl_add_bufferf(&req_buffer, "%s%s", count?"; ":"",
2714 addcookies);
2715 count++;
2716 }
2717 }
2718 if(count && !result)
2719 result = Curl_add_buffer(&req_buffer, "\r\n", 2);
2720
2721 if(result)
2722 return result;
2723 }
2724 #endif
2725
2726 result = Curl_add_timecondition(conn, req_buffer);
2727 if(result)
2728 return result;
2729
2730 result = Curl_add_custom_headers(conn, FALSE, req_buffer);
2731 if(result)
2732 return result;
2733
2734 http->postdata = NULL; /* nothing to post at this point */
2735 Curl_pgrsSetUploadSize(data, -1); /* upload size is unknown atm */
2736
2737 /* If 'authdone' is FALSE, we must not set the write socket index to the
2738 Curl_transfer() call below, as we're not ready to actually upload any
2739 data yet. */
2740
2741 switch(httpreq) {
2742
2743 case HTTPREQ_PUT: /* Let's PUT the data to the server! */
2744
2745 if(conn->bits.authneg)
2746 postsize = 0;
2747 else
2748 postsize = data->state.infilesize;
2749
2750 if((postsize != -1) && !data->req.upload_chunky &&
2751 (conn->bits.authneg || !Curl_checkheaders(conn, "Content-Length"))) {
2752 /* only add Content-Length if not uploading chunked */
2753 result = Curl_add_bufferf(&req_buffer,
2754 "Content-Length: %" CURL_FORMAT_CURL_OFF_T
2755 "\r\n", postsize);
2756 if(result)
2757 return result;
2758 }
2759
2760 if(postsize != 0) {
2761 result = expect100(data, conn, req_buffer);
2762 if(result)
2763 return result;
2764 }
2765
2766 result = Curl_add_buffer(&req_buffer, "\r\n", 2); /* end of headers */
2767 if(result)
2768 return result;
2769
2770 /* set the upload size to the progress meter */
2771 Curl_pgrsSetUploadSize(data, postsize);
2772
2773 /* this sends the buffer and frees all the buffer resources */
2774 result = Curl_add_buffer_send(&req_buffer, conn,
2775 &data->info.request_size, 0, FIRSTSOCKET);
2776 if(result)
2777 failf(data, "Failed sending PUT request");
2778 else
2779 /* prepare for transfer */
2780 Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE,
2781 postsize?FIRSTSOCKET:-1);
2782 if(result)
2783 return result;
2784 break;
2785
2786 case HTTPREQ_POST_FORM:
2787 case HTTPREQ_POST_MIME:
2788 /* This is form posting using mime data. */
2789 if(conn->bits.authneg) {
2790 /* nothing to post! */
2791 result = Curl_add_bufferf(&req_buffer, "Content-Length: 0\r\n\r\n");
2792 if(result)
2793 return result;
2794
2795 result = Curl_add_buffer_send(&req_buffer, conn,
2796 &data->info.request_size, 0, FIRSTSOCKET);
2797 if(result)
2798 failf(data, "Failed sending POST request");
2799 else
2800 /* setup variables for the upcoming transfer */
2801 Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
2802 break;
2803 }
2804
2805 data->state.infilesize = postsize = http->postsize;
2806
2807 /* We only set Content-Length and allow a custom Content-Length if
2808 we don't upload data chunked, as RFC2616 forbids us to set both
2809 kinds of headers (Transfer-Encoding: chunked and Content-Length) */
2810 if(postsize != -1 && !data->req.upload_chunky &&
2811 (conn->bits.authneg || !Curl_checkheaders(conn, "Content-Length"))) {
2812 /* we allow replacing this header if not during auth negotiation,
2813 although it isn't very wise to actually set your own */
2814 result = Curl_add_bufferf(&req_buffer,
2815 "Content-Length: %" CURL_FORMAT_CURL_OFF_T
2816 "\r\n", postsize);
2817 if(result)
2818 return result;
2819 }
2820
2821 #ifndef CURL_DISABLE_MIME
2822 /* Output mime-generated headers. */
2823 {
2824 struct curl_slist *hdr;
2825
2826 for(hdr = http->sendit->curlheaders; hdr; hdr = hdr->next) {
2827 result = Curl_add_bufferf(&req_buffer, "%s\r\n", hdr->data);
2828 if(result)
2829 return result;
2830 }
2831 }
2832 #endif
2833
2834 /* For really small posts we don't use Expect: headers at all, and for
2835 the somewhat bigger ones we allow the app to disable it. Just make
2836 sure that the expect100header is always set to the preferred value
2837 here. */
2838 ptr = Curl_checkheaders(conn, "Expect");
2839 if(ptr) {
2840 data->state.expect100header =
2841 Curl_compareheader(ptr, "Expect:", "100-continue");
2842 }
2843 else if(postsize > EXPECT_100_THRESHOLD || postsize < 0) {
2844 result = expect100(data, conn, req_buffer);
2845 if(result)
2846 return result;
2847 }
2848 else
2849 data->state.expect100header = FALSE;
2850
2851 /* make the request end in a true CRLF */
2852 result = Curl_add_buffer(&req_buffer, "\r\n", 2);
2853 if(result)
2854 return result;
2855
2856 /* set the upload size to the progress meter */
2857 Curl_pgrsSetUploadSize(data, postsize);
2858
2859 /* Read from mime structure. */
2860 data->state.fread_func = (curl_read_callback) Curl_mime_read;
2861 data->state.in = (void *) http->sendit;
2862 http->sending = HTTPSEND_BODY;
2863
2864 /* this sends the buffer and frees all the buffer resources */
2865 result = Curl_add_buffer_send(&req_buffer, conn,
2866 &data->info.request_size, 0, FIRSTSOCKET);
2867 if(result)
2868 failf(data, "Failed sending POST request");
2869 else
2870 /* prepare for transfer */
2871 Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE,
2872 postsize?FIRSTSOCKET:-1);
2873 if(result)
2874 return result;
2875
2876 break;
2877
2878 case HTTPREQ_POST:
2879 /* this is the simple POST, using x-www-form-urlencoded style */
2880
2881 if(conn->bits.authneg)
2882 postsize = 0;
2883 else
2884 /* the size of the post body */
2885 postsize = data->state.infilesize;
2886
2887 /* We only set Content-Length and allow a custom Content-Length if
2888 we don't upload data chunked, as RFC2616 forbids us to set both
2889 kinds of headers (Transfer-Encoding: chunked and Content-Length) */
2890 if((postsize != -1) && !data->req.upload_chunky &&
2891 (conn->bits.authneg || !Curl_checkheaders(conn, "Content-Length"))) {
2892 /* we allow replacing this header if not during auth negotiation,
2893 although it isn't very wise to actually set your own */
2894 result = Curl_add_bufferf(&req_buffer,
2895 "Content-Length: %" CURL_FORMAT_CURL_OFF_T
2896 "\r\n", postsize);
2897 if(result)
2898 return result;
2899 }
2900
2901 if(!Curl_checkheaders(conn, "Content-Type")) {
2902 result = Curl_add_bufferf(&req_buffer,
2903 "Content-Type: application/"
2904 "x-www-form-urlencoded\r\n");
2905 if(result)
2906 return result;
2907 }
2908
2909 /* For really small posts we don't use Expect: headers at all, and for
2910 the somewhat bigger ones we allow the app to disable it. Just make
2911 sure that the expect100header is always set to the preferred value
2912 here. */
2913 ptr = Curl_checkheaders(conn, "Expect");
2914 if(ptr) {
2915 data->state.expect100header =
2916 Curl_compareheader(ptr, "Expect:", "100-continue");
2917 }
2918 else if(postsize > EXPECT_100_THRESHOLD || postsize < 0) {
2919 result = expect100(data, conn, req_buffer);
2920 if(result)
2921 return result;
2922 }
2923 else
2924 data->state.expect100header = FALSE;
2925
2926 if(data->set.postfields) {
2927
2928 /* In HTTP2, we send request body in DATA frame regardless of
2929 its size. */
2930 if(conn->httpversion != 20 &&
2931 !data->state.expect100header &&
2932 (postsize < MAX_INITIAL_POST_SIZE)) {
2933 /* if we don't use expect: 100 AND
2934 postsize is less than MAX_INITIAL_POST_SIZE
2935
2936 then append the post data to the HTTP request header. This limit
2937 is no magic limit but only set to prevent really huge POSTs to
2938 get the data duplicated with malloc() and family. */
2939
2940 result = Curl_add_buffer(&req_buffer, "\r\n", 2); /* end of headers! */
2941 if(result)
2942 return result;
2943
2944 if(!data->req.upload_chunky) {
2945 /* We're not sending it 'chunked', append it to the request
2946 already now to reduce the number if send() calls */
2947 result = Curl_add_buffer(&req_buffer, data->set.postfields,
2948 (size_t)postsize);
2949 included_body = postsize;
2950 }
2951 else {
2952 if(postsize) {
2953 /* Append the POST data chunky-style */
2954 result = Curl_add_bufferf(&req_buffer, "%x\r\n", (int)postsize);
2955 if(!result) {
2956 result = Curl_add_buffer(&req_buffer, data->set.postfields,
2957 (size_t)postsize);
2958 if(!result)
2959 result = Curl_add_buffer(&req_buffer, "\r\n", 2);
2960 included_body = postsize + 2;
2961 }
2962 }
2963 if(!result)
2964 result = Curl_add_buffer(&req_buffer, "\x30\x0d\x0a\x0d\x0a", 5);
2965 /* 0 CR LF CR LF */
2966 included_body += 5;
2967 }
2968 if(result)
2969 return result;
2970 /* Make sure the progress information is accurate */
2971 Curl_pgrsSetUploadSize(data, postsize);
2972 }
2973 else {
2974 /* A huge POST coming up, do data separate from the request */
2975 http->postsize = postsize;
2976 http->postdata = data->set.postfields;
2977
2978 http->sending = HTTPSEND_BODY;
2979
2980 data->state.fread_func = (curl_read_callback)readmoredata;
2981 data->state.in = (void *)conn;
2982
2983 /* set the upload size to the progress meter */
2984 Curl_pgrsSetUploadSize(data, http->postsize);
2985
2986 result = Curl_add_buffer(&req_buffer, "\r\n", 2); /* end of headers! */
2987 if(result)
2988 return result;
2989 }
2990 }
2991 else {
2992 result = Curl_add_buffer(&req_buffer, "\r\n", 2); /* end of headers! */
2993 if(result)
2994 return result;
2995
2996 if(data->req.upload_chunky && conn->bits.authneg) {
2997 /* Chunky upload is selected and we're negotiating auth still, send
2998 end-of-data only */
2999 result = Curl_add_buffer(&req_buffer,
3000 "\x30\x0d\x0a\x0d\x0a", 5);
3001 /* 0 CR LF CR LF */
3002 if(result)
3003 return result;
3004 }
3005
3006 else if(data->state.infilesize) {
3007 /* set the upload size to the progress meter */
3008 Curl_pgrsSetUploadSize(data, postsize?postsize:-1);
3009
3010 /* set the pointer to mark that we will send the post body using the
3011 read callback, but only if we're not in authenticate
3012 negotiation */
3013 if(!conn->bits.authneg) {
3014 http->postdata = (char *)&http->postdata;
3015 http->postsize = postsize;
3016 }
3017 }
3018 }
3019 /* issue the request */
3020 result = Curl_add_buffer_send(&req_buffer, conn, &data->info.request_size,
3021 (size_t)included_body, FIRSTSOCKET);
3022
3023 if(result)
3024 failf(data, "Failed sending HTTP POST request");
3025 else
3026 Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE,
3027 http->postdata?FIRSTSOCKET:-1);
3028 break;
3029
3030 default:
3031 result = Curl_add_buffer(&req_buffer, "\r\n", 2);
3032 if(result)
3033 return result;
3034
3035 /* issue the request */
3036 result = Curl_add_buffer_send(&req_buffer, conn,
3037 &data->info.request_size, 0, FIRSTSOCKET);
3038
3039 if(result)
3040 failf(data, "Failed sending HTTP request");
3041 else
3042 /* HTTP GET/HEAD download: */
3043 Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
3044 }
3045 if(result)
3046 return result;
3047 if(!postsize)
3048 data->req.upload_done = TRUE;
3049
3050 if(data->req.writebytecount) {
3051 /* if a request-body has been sent off, we make sure this progress is noted
3052 properly */
3053 Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
3054 if(Curl_pgrsUpdate(conn))
3055 result = CURLE_ABORTED_BY_CALLBACK;
3056
3057 if(data->req.writebytecount >= postsize) {
3058 /* already sent the entire request body, mark the "upload" as
3059 complete */
3060 infof(data, "upload completely sent off: %" CURL_FORMAT_CURL_OFF_T
3061 " out of %" CURL_FORMAT_CURL_OFF_T " bytes\n",
3062 data->req.writebytecount, postsize);
3063 data->req.upload_done = TRUE;
3064 data->req.keepon &= ~KEEP_SEND; /* we're done writing */
3065 data->req.exp100 = EXP100_SEND_DATA; /* already sent */
3066 Curl_expire_done(data, EXPIRE_100_TIMEOUT);
3067 }
3068 }
3069
3070 if((conn->httpversion == 20) && data->req.upload_chunky)
3071 /* upload_chunky was set above to set up the request in a chunky fashion,
3072 but is disabled here again to avoid that the chunked encoded version is
3073 actually used when sending the request body over h2 */
3074 data->req.upload_chunky = FALSE;
3075 return result;
3076 }
3077
3078 typedef enum {
3079 STATUS_UNKNOWN, /* not enough data to tell yet */
3080 STATUS_DONE, /* a status line was read */
3081 STATUS_BAD /* not a status line */
3082 } statusline;
3083
3084
3085 /* Check a string for a prefix. Check no more than 'len' bytes */
3086 static bool checkprefixmax(const char *prefix, const char *buffer, size_t len)
3087 {
3088 size_t ch = CURLMIN(strlen(prefix), len);
3089 return curl_strnequal(prefix, buffer, ch);
3090 }
3091
3092 /*
3093 * checkhttpprefix()
3094 *
3095 * Returns TRUE if member of the list matches prefix of string
3096 */
3097 static statusline
3098 checkhttpprefix(struct Curl_easy *data,
3099 const char *s, size_t len)
3100 {
3101 struct curl_slist *head = data->set.http200aliases;
3102 statusline rc = STATUS_BAD;
3103 statusline onmatch = len >= 5? STATUS_DONE : STATUS_UNKNOWN;
3104 #ifdef CURL_DOES_CONVERSIONS
3105 /* convert from the network encoding using a scratch area */
3106 char *scratch = strdup(s);
3107 if(NULL == scratch) {
3108 failf(data, "Failed to allocate memory for conversion!");
3109 return FALSE; /* can't return CURLE_OUT_OF_MEMORY so return FALSE */
3110 }
3111 if(CURLE_OK != Curl_convert_from_network(data, scratch, strlen(s) + 1)) {
3112 /* Curl_convert_from_network calls failf if unsuccessful */
3113 free(scratch);
3114 return FALSE; /* can't return CURLE_foobar so return FALSE */
3115 }
3116 s = scratch;
3117 #endif /* CURL_DOES_CONVERSIONS */
3118
3119 while(head) {
3120 if(checkprefixmax(head->data, s, len)) {
3121 rc = onmatch;
3122 break;
3123 }
3124 head = head->next;
3125 }
3126
3127 if((rc != STATUS_DONE) && (checkprefixmax("HTTP/", s, len)))
3128 rc = onmatch;
3129
3130 #ifdef CURL_DOES_CONVERSIONS
3131 free(scratch);
3132 #endif /* CURL_DOES_CONVERSIONS */
3133 return rc;
3134 }
3135
3136 #ifndef CURL_DISABLE_RTSP
3137 static statusline
3138 checkrtspprefix(struct Curl_easy *data,
3139 const char *s, size_t len)
3140 {
3141 statusline result = STATUS_BAD;
3142 statusline onmatch = len >= 5? STATUS_DONE : STATUS_UNKNOWN;
3143
3144 #ifdef CURL_DOES_CONVERSIONS
3145 /* convert from the network encoding using a scratch area */
3146 char *scratch = strdup(s);
3147 if(NULL == scratch) {
3148 failf(data, "Failed to allocate memory for conversion!");
3149 return FALSE; /* can't return CURLE_OUT_OF_MEMORY so return FALSE */
3150 }
3151 if(CURLE_OK != Curl_convert_from_network(data, scratch, strlen(s) + 1)) {
3152 /* Curl_convert_from_network calls failf if unsuccessful */
3153 result = FALSE; /* can't return CURLE_foobar so return FALSE */
3154 }
3155 else if(checkprefixmax("RTSP/", scratch, len))
3156 result = onmatch;
3157 free(scratch);
3158 #else
3159 (void)data; /* unused */
3160 if(checkprefixmax("RTSP/", s, len))
3161 result = onmatch;
3162 #endif /* CURL_DOES_CONVERSIONS */
3163
3164 return result;
3165 }
3166 #endif /* CURL_DISABLE_RTSP */
3167
3168 static statusline
3169 checkprotoprefix(struct Curl_easy *data, struct connectdata *conn,
3170 const char *s, size_t len)
3171 {
3172 #ifndef CURL_DISABLE_RTSP
3173 if(conn->handler->protocol & CURLPROTO_RTSP)
3174 return checkrtspprefix(data, s, len);
3175 #else
3176 (void)conn;
3177 #endif /* CURL_DISABLE_RTSP */
3178
3179 return checkhttpprefix(data, s, len);
3180 }
3181
3182 /*
3183 * header_append() copies a chunk of data to the end of the already received
3184 * header. We make sure that the full string fit in the allocated header
3185 * buffer, or else we enlarge it.
3186 */
3187 static CURLcode header_append(struct Curl_easy *data,
3188 struct SingleRequest *k,
3189 size_t length)
3190 {
3191 /* length is at most the size of a full read buffer, for which the upper
3192 bound is CURL_MAX_READ_SIZE. There is thus no chance of overflow in this
3193 calculation. */
3194 size_t newsize = k->hbuflen + length;
3195 if(newsize > CURL_MAX_HTTP_HEADER) {
3196 /* The reason to have a max limit for this is to avoid the risk of a bad
3197 server feeding libcurl with a never-ending header that will cause
3198 reallocs infinitely */
3199 failf(data, "Rejected %zu bytes header (max is %d)!", newsize,
3200 CURL_MAX_HTTP_HEADER);
3201 return CURLE_OUT_OF_MEMORY;
3202 }
3203 if(newsize >= data->state.headersize) {
3204 /* We enlarge the header buffer as it is too small */
3205 char *newbuff;
3206 size_t hbufp_index;
3207
3208 newsize = CURLMAX((k->hbuflen + length) * 3 / 2, data->state.headersize*2);
3209 hbufp_index = k->hbufp - data->state.headerbuff;
3210 newbuff = realloc(data->state.headerbuff, newsize);
3211 if(!newbuff) {
3212 failf(data, "Failed to alloc memory for big header!");
3213 return CURLE_OUT_OF_MEMORY;
3214 }
3215 data->state.headersize = newsize;
3216 data->state.headerbuff = newbuff;
3217 k->hbufp = data->state.headerbuff + hbufp_index;
3218 }
3219 memcpy(k->hbufp, k->str_start, length);
3220 k->hbufp += length;
3221 k->hbuflen += length;
3222 *k->hbufp = 0;
3223
3224 return CURLE_OK;
3225 }
3226
3227 static void print_http_error(struct Curl_easy *data)
3228 {
3229 struct SingleRequest *k = &data->req;
3230 char *beg = k->p;
3231
3232 /* make sure that data->req.p points to the HTTP status line */
3233 if(!strncmp(beg, "HTTP", 4)) {
3234
3235 /* skip to HTTP status code */
3236 beg = strchr(beg, ' ');
3237 if(beg && *++beg) {
3238
3239 /* find trailing CR */
3240 char end_char = '\r';
3241 char *end = strchr(beg, end_char);
3242 if(!end) {
3243 /* try to find LF (workaround for non-compliant HTTP servers) */
3244 end_char = '\n';
3245 end = strchr(beg, end_char);
3246 }
3247
3248 if(end) {
3249 /* temporarily replace CR or LF by NUL and print the error message */
3250 *end = '\0';
3251 failf(data, "The requested URL returned error: %s", beg);
3252
3253 /* restore the previously replaced CR or LF */
3254 *end = end_char;
3255 return;
3256 }
3257 }
3258 }
3259
3260 /* fall-back to printing the HTTP status code only */
3261 failf(data, "The requested URL returned error: %d", k->httpcode);
3262 }
3263
3264 /*
3265 * Read any HTTP header lines from the server and pass them to the client app.
3266 */
3267 CURLcode Curl_http_readwrite_headers(struct Curl_easy *data,
3268 struct connectdata *conn,
3269 ssize_t *nread,
3270 bool *stop_reading)
3271 {
3272 CURLcode result;
3273 struct SingleRequest *k = &data->req;
3274 ssize_t onread = *nread;
3275 char *ostr = k->str;
3276
3277 /* header line within buffer loop */
3278 do {
3279 size_t rest_length;
3280 size_t full_length;
3281 int writetype;
3282
3283 /* str_start is start of line within buf */
3284 k->str_start = k->str;
3285
3286 /* data is in network encoding so use 0x0a instead of '\n' */
3287 k->end_ptr = memchr(k->str_start, 0x0a, *nread);
3288
3289 if(!k->end_ptr) {
3290 /* Not a complete header line within buffer, append the data to
3291 the end of the headerbuff. */
3292 result = header_append(data, k, *nread);
3293 if(result)
3294 return result;
3295
3296 if(!k->headerline) {
3297 /* check if this looks like a protocol header */
3298 statusline st = checkprotoprefix(data, conn, data->state.headerbuff,
3299 k->hbuflen);
3300 if(st == STATUS_BAD) {
3301 /* this is not the beginning of a protocol first header line */
3302 k->header = FALSE;
3303 k->badheader = HEADER_ALLBAD;
3304 streamclose(conn, "bad HTTP: No end-of-message indicator");
3305 if(!data->set.http09_allowed) {
3306 failf(data, "Received HTTP/0.9 when not allowed\n");
3307 return CURLE_UNSUPPORTED_PROTOCOL;
3308 }
3309 break;
3310 }
3311 }
3312
3313 break; /* read more and try again */
3314 }
3315
3316 /* decrease the size of the remaining (supposed) header line */
3317 rest_length = (k->end_ptr - k->str) + 1;
3318 *nread -= (ssize_t)rest_length;
3319
3320 k->str = k->end_ptr + 1; /* move past new line */
3321
3322 full_length = k->str - k->str_start;
3323
3324 result = header_append(data, k, full_length);
3325 if(result)
3326 return result;
3327
3328 k->end_ptr = k->hbufp;
3329 k->p = data->state.headerbuff;
3330
3331 /****
3332 * We now have a FULL header line that p points to
3333 *****/
3334
3335 if(!k->headerline) {
3336 /* the first read header */
3337 statusline st = checkprotoprefix(data, conn, data->state.headerbuff,
3338 k->hbuflen);
3339 if(st == STATUS_BAD) {
3340 streamclose(conn, "bad HTTP: No end-of-message indicator");
3341 /* this is not the beginning of a protocol first header line */
3342 if(!data->set.http09_allowed) {
3343 failf(data, "Received HTTP/0.9 when not allowed\n");
3344 return CURLE_UNSUPPORTED_PROTOCOL;
3345 }
3346 k->header = FALSE;
3347 if(*nread)
3348 /* since there's more, this is a partial bad header */
3349 k->badheader = HEADER_PARTHEADER;
3350 else {
3351 /* this was all we read so it's all a bad header */
3352 k->badheader = HEADER_ALLBAD;
3353 *nread = onread;
3354 k->str = ostr;
3355 return CURLE_OK;
3356 }
3357 break;
3358 }
3359 }
3360
3361 /* headers are in network encoding so
3362 use 0x0a and 0x0d instead of '\n' and '\r' */
3363 if((0x0a == *k->p) || (0x0d == *k->p)) {
3364 size_t headerlen;
3365 /* Zero-length header line means end of headers! */
3366
3367 #ifdef CURL_DOES_CONVERSIONS
3368 if(0x0d == *k->p) {
3369 *k->p = '\r'; /* replace with CR in host encoding */
3370 k->p++; /* pass the CR byte */
3371 }
3372 if(0x0a == *k->p) {
3373 *k->p = '\n'; /* replace with LF in host encoding */
3374 k->p++; /* pass the LF byte */
3375 }
3376 #else
3377 if('\r' == *k->p)
3378 k->p++; /* pass the \r byte */
3379 if('\n' == *k->p)
3380 k->p++; /* pass the \n byte */
3381 #endif /* CURL_DOES_CONVERSIONS */
3382
3383 if(100 <= k->httpcode && 199 >= k->httpcode) {
3384 /* "A user agent MAY ignore unexpected 1xx status responses." */
3385 switch(k->httpcode) {
3386 case 100:
3387 /*
3388 * We have made a HTTP PUT or POST and this is 1.1-lingo
3389 * that tells us that the server is OK with this and ready
3390 * to receive the data.
3391 * However, we'll get more headers now so we must get
3392 * back into the header-parsing state!
3393 */
3394 k->header = TRUE;
3395 k->headerline = 0; /* restart the header line counter */
3396
3397 /* if we did wait for this do enable write now! */
3398 if(k->exp100 > EXP100_SEND_DATA) {
3399 k->exp100 = EXP100_SEND_DATA;
3400 k->keepon |= KEEP_SEND;
3401 Curl_expire_done(data, EXPIRE_100_TIMEOUT);
3402 }
3403 break;
3404 case 101:
3405 /* Switching Protocols */
3406 if(k->upgr101 == UPGR101_REQUESTED) {
3407 /* Switching to HTTP/2 */
3408 infof(data, "Received 101\n");
3409 k->upgr101 = UPGR101_RECEIVED;
3410
3411 /* we'll get more headers (HTTP/2 response) */
3412 k->header = TRUE;
3413 k->headerline = 0; /* restart the header line counter */
3414
3415 /* switch to http2 now. The bytes after response headers
3416 are also processed here, otherwise they are lost. */
3417 result = Curl_http2_switched(conn, k->str, *nread);
3418 if(result)
3419 return result;
3420 *nread = 0;
3421 }
3422 else {
3423 /* Switching to another protocol (e.g. WebSocket) */
3424 k->header = FALSE; /* no more header to parse! */
3425 }
3426 break;
3427 default:
3428 /* the status code 1xx indicates a provisional response, so
3429 we'll get another set of headers */
3430 k->header = TRUE;
3431 k->headerline = 0; /* restart the header line counter */
3432 break;
3433 }
3434 }
3435 else {
3436 k->header = FALSE; /* no more header to parse! */
3437
3438 if((k->size == -1) && !k->chunk && !conn->bits.close &&
3439 (conn->httpversion == 11) &&
3440 !(conn->handler->protocol & CURLPROTO_RTSP) &&
3441 data->set.httpreq != HTTPREQ_HEAD) {
3442 /* On HTTP 1.1, when connection is not to get closed, but no
3443 Content-Length nor Transfer-Encoding chunked have been
3444 received, according to RFC2616 section 4.4 point 5, we
3445 assume that the server will close the connection to
3446 signal the end of the document. */
3447 infof(data, "no chunk, no close, no size. Assume close to "
3448 "signal end\n");
3449 streamclose(conn, "HTTP: No end-of-message indicator");
3450 }
3451 }
3452
3453 /* At this point we have some idea about the fate of the connection.
3454 If we are closing the connection it may result auth failure. */
3455 #if defined(USE_NTLM)
3456 if(conn->bits.close &&
3457 (((data->req.httpcode == 401) &&
3458 (conn->http_ntlm_state == NTLMSTATE_TYPE2)) ||
3459 ((data->req.httpcode == 407) &&
3460 (conn->proxy_ntlm_state == NTLMSTATE_TYPE2)))) {
3461 infof(data, "Connection closure while negotiating auth (HTTP 1.0?)\n");
3462 data->state.authproblem = TRUE;
3463 }
3464 #endif
3465 #if defined(USE_SPNEGO)
3466 if(conn->bits.close &&
3467 (((data->req.httpcode == 401) &&
3468 (conn->http_negotiate_state == GSS_AUTHRECV)) ||
3469 ((data->req.httpcode == 407) &&
3470 (conn->proxy_negotiate_state == GSS_AUTHRECV)))) {
3471 infof(data, "Connection closure while negotiating auth (HTTP 1.0?)\n");
3472 data->state.authproblem = TRUE;
3473 }
3474 if((conn->http_negotiate_state == GSS_AUTHDONE) &&
3475 (data->req.httpcode != 401)) {
3476 conn->http_negotiate_state = GSS_AUTHSUCC;
3477 }
3478 if((conn->proxy_negotiate_state == GSS_AUTHDONE) &&
3479 (data->req.httpcode != 407)) {
3480 conn->proxy_negotiate_state = GSS_AUTHSUCC;
3481 }
3482 #endif
3483 /*
3484 * When all the headers have been parsed, see if we should give
3485 * up and return an error.
3486 */
3487 if(http_should_fail(conn)) {
3488 failf(data, "The requested URL returned error: %d",
3489 k->httpcode);
3490 return CURLE_HTTP_RETURNED_ERROR;
3491 }
3492
3493 /* now, only output this if the header AND body are requested:
3494 */
3495 writetype = CLIENTWRITE_HEADER;
3496 if(data->set.include_header)
3497 writetype |= CLIENTWRITE_BODY;
3498
3499 headerlen = k->p - data->state.headerbuff;
3500
3501 result = Curl_client_write(conn, writetype,
3502 data->state.headerbuff,
3503 headerlen);
3504 if(result)
3505 return result;
3506
3507 data->info.header_size += (long)headerlen;
3508 data->req.headerbytecount += (long)headerlen;
3509
3510 data->req.deductheadercount =
3511 (100 <= k->httpcode && 199 >= k->httpcode)?data->req.headerbytecount:0;
3512
3513 /* Curl_http_auth_act() checks what authentication methods
3514 * that are available and decides which one (if any) to
3515 * use. It will set 'newurl' if an auth method was picked. */
3516 result = Curl_http_auth_act(conn);
3517
3518 if(result)
3519 return result;
3520
3521 if(k->httpcode >= 300) {
3522 if((!conn->bits.authneg) && !conn->bits.close &&
3523 !conn->bits.rewindaftersend) {
3524 /*
3525 * General treatment of errors when about to send data. Including :
3526 * "417 Expectation Failed", while waiting for 100-continue.
3527 *
3528 * The check for close above is done simply because of something
3529 * else has already deemed the connection to get closed then
3530 * something else should've considered the big picture and we
3531 * avoid this check.
3532 *
3533 * rewindaftersend indicates that something has told libcurl to
3534 * continue sending even if it gets discarded
3535 */
3536
3537 switch(data->set.httpreq) {
3538 case HTTPREQ_PUT:
3539 case HTTPREQ_POST:
3540 case HTTPREQ_POST_FORM:
3541 case HTTPREQ_POST_MIME:
3542 /* We got an error response. If this happened before the whole
3543 * request body has been sent we stop sending and mark the
3544 * connection for closure after we've read the entire response.
3545 */
3546 Curl_expire_done(data, EXPIRE_100_TIMEOUT);
3547 if(!k->upload_done) {
3548 if((k->httpcode == 417) && data->state.expect100header) {
3549 /* 417 Expectation Failed - try again without the Expect
3550 header */
3551 infof(data, "Got 417 while waiting for a 100\n");
3552 data->state.disableexpect = TRUE;
3553 DEBUGASSERT(!data->req.newurl);
3554 data->req.newurl = strdup(conn->data->change.url);
3555 Curl_done_sending(conn, k);
3556 }
3557 else if(data->set.http_keep_sending_on_error) {
3558 infof(data, "HTTP error before end of send, keep sending\n");
3559 if(k->exp100 > EXP100_SEND_DATA) {
3560 k->exp100 = EXP100_SEND_DATA;
3561 k->keepon |= KEEP_SEND;
3562 }
3563 }
3564 else {
3565 infof(data, "HTTP error before end of send, stop sending\n");
3566 streamclose(conn, "Stop sending data before everything sent");
3567 result = Curl_done_sending(conn, k);
3568 if(result)
3569 return result;
3570 k->upload_done = TRUE;
3571 if(data->state.expect100header)
3572 k->exp100 = EXP100_FAILED;
3573 }
3574 }
3575 break;
3576
3577 default: /* default label present to avoid compiler warnings */
3578 break;
3579 }
3580 }
3581
3582 if(conn->bits.rewindaftersend) {
3583 /* We rewind after a complete send, so thus we continue
3584 sending now */
3585 infof(data, "Keep sending data to get tossed away!\n");
3586 k->keepon |= KEEP_SEND;
3587 }
3588 }
3589
3590 if(!k->header) {
3591 /*
3592 * really end-of-headers.
3593 *
3594 * If we requested a "no body", this is a good time to get
3595 * out and return home.
3596 */
3597 if(data->set.opt_no_body)
3598 *stop_reading = TRUE;
3599 #ifndef CURL_DISABLE_RTSP
3600 else if((conn->handler->protocol & CURLPROTO_RTSP) &&
3601 (data->set.rtspreq == RTSPREQ_DESCRIBE) &&
3602 (k->size <= -1))
3603 /* Respect section 4.4 of rfc2326: If the Content-Length header is
3604 absent, a length 0 must be assumed. It will prevent libcurl from
3605 hanging on DESCRIBE request that got refused for whatever
3606 reason */
3607 *stop_reading = TRUE;
3608 #endif
3609 else {
3610 /* If we know the expected size of this document, we set the
3611 maximum download size to the size of the expected
3612 document or else, we won't know when to stop reading!
3613
3614 Note that we set the download maximum even if we read a
3615 "Connection: close" header, to make sure that
3616 "Content-Length: 0" still prevents us from attempting to
3617 read the (missing) response-body.
3618 */
3619 /* According to RFC2616 section 4.4, we MUST ignore
3620 Content-Length: headers if we are now receiving data
3621 using chunked Transfer-Encoding.
3622 */
3623 if(k->chunk)
3624 k->maxdownload = k->size = -1;
3625 }
3626 if(-1 != k->size) {
3627 /* We do this operation even if no_body is true, since this
3628 data might be retrieved later with curl_easy_getinfo()
3629 and its CURLINFO_CONTENT_LENGTH_DOWNLOAD option. */
3630
3631 Curl_pgrsSetDownloadSize(data, k->size);
3632 k->maxdownload = k->size;
3633 }
3634
3635 /* If max download size is *zero* (nothing) we already have
3636 nothing and can safely return ok now! But for HTTP/2, we'd
3637 like to call http2_handle_stream_close to properly close a
3638 stream. In order to do this, we keep reading until we
3639 close the stream. */
3640 if(0 == k->maxdownload
3641 #if defined(USE_NGHTTP2)
3642 && !((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
3643 conn->httpversion == 20)
3644 #endif
3645 )
3646 *stop_reading = TRUE;
3647
3648 if(*stop_reading) {
3649 /* we make sure that this socket isn't read more now */
3650 k->keepon &= ~KEEP_RECV;
3651 }
3652
3653 if(data->set.verbose)
3654 Curl_debug(data, CURLINFO_HEADER_IN,
3655 k->str_start, headerlen);
3656 break; /* exit header line loop */
3657 }
3658
3659 /* We continue reading headers, so reset the line-based
3660 header parsing variables hbufp && hbuflen */
3661 k->hbufp = data->state.headerbuff;
3662 k->hbuflen = 0;
3663 continue;
3664 }
3665
3666 /*
3667 * Checks for special headers coming up.
3668 */
3669
3670 if(!k->headerline++) {
3671 /* This is the first header, it MUST be the error code line
3672 or else we consider this to be the body right away! */
3673 int httpversion_major;
3674 int rtspversion_major;
3675 int nc = 0;
3676 #ifdef CURL_DOES_CONVERSIONS
3677 #define HEADER1 scratch
3678 #define SCRATCHSIZE 21
3679 CURLcode res;
3680 char scratch[SCRATCHSIZE + 1]; /* "HTTP/major.minor 123" */
3681 /* We can't really convert this yet because we
3682 don't know if it's the 1st header line or the body.
3683 So we do a partial conversion into a scratch area,
3684 leaving the data at k->p as-is.
3685 */
3686 strncpy(&scratch[0], k->p, SCRATCHSIZE);
3687 scratch[SCRATCHSIZE] = 0; /* null terminate */
3688 res = Curl_convert_from_network(data,
3689 &scratch[0],
3690 SCRATCHSIZE);
3691 if(res)
3692 /* Curl_convert_from_network calls failf if unsuccessful */
3693 return res;
3694 #else
3695 #define HEADER1 k->p /* no conversion needed, just use k->p */
3696 #endif /* CURL_DOES_CONVERSIONS */
3697
3698 if(conn->handler->protocol & PROTO_FAMILY_HTTP) {
3699 /*
3700 * https://tools.ietf.org/html/rfc7230#section-3.1.2
3701 *
3702 * The response code is always a three-digit number in HTTP as the spec
3703 * says. We try to allow any number here, but we cannot make
3704 * guarantees on future behaviors since it isn't within the protocol.
3705 */
3706 char separator;
3707 char twoorthree[2];
3708 nc = sscanf(HEADER1,
3709 " HTTP/%1d.%1d%c%3d",
3710 &httpversion_major,
3711 &conn->httpversion,
3712 &separator,
3713 &k->httpcode);
3714
3715 if(nc == 1 && httpversion_major >= 2 &&
3716 2 == sscanf(HEADER1, " HTTP/%1[23] %d", twoorthree, &k->httpcode)) {
3717 conn->httpversion = 0;
3718 nc = 4;
3719 separator = ' ';
3720 }
3721
3722 if((nc == 4) && (' ' == separator)) {
3723 conn->httpversion += 10 * httpversion_major;
3724
3725 if(k->upgr101 == UPGR101_RECEIVED) {
3726 /* supposedly upgraded to http2 now */
3727 if(conn->httpversion != 20)
3728 infof(data, "Lying server, not serving HTTP/2\n");
3729 }
3730 if(conn->httpversion < 20) {
3731 conn->bundle->multiuse = BUNDLE_NO_MULTIUSE;
3732 infof(data, "Mark bundle as not supporting multiuse\n");
3733 }
3734 }
3735 else if(!nc) {
3736 /* this is the real world, not a Nirvana
3737 NCSA 1.5.x returns this crap when asked for HTTP/1.1
3738 */
3739 nc = sscanf(HEADER1, " HTTP %3d", &k->httpcode);
3740 conn->httpversion = 10;
3741
3742 /* If user has set option HTTP200ALIASES,
3743 compare header line against list of aliases
3744 */
3745 if(!nc) {
3746 if(checkhttpprefix(data, k->p, k->hbuflen) == STATUS_DONE) {
3747 nc = 1;
3748 k->httpcode = 200;
3749 conn->httpversion = 10;
3750 }
3751 }
3752 }
3753 else {
3754 failf(data, "Unsupported HTTP version in response");
3755 return CURLE_UNSUPPORTED_PROTOCOL;
3756 }
3757 }
3758 else if(conn->handler->protocol & CURLPROTO_RTSP) {
3759 char separator;
3760 nc = sscanf(HEADER1,
3761 " RTSP/%1d.%1d%c%3d",
3762 &rtspversion_major,
3763 &conn->rtspversion,
3764 &separator,
3765 &k->httpcode);
3766 if((nc == 4) && (' ' == separator)) {
3767 conn->rtspversion += 10 * rtspversion_major;
3768 conn->httpversion = 11; /* For us, RTSP acts like HTTP 1.1 */
3769 }
3770 else {
3771 nc = 0;
3772 }
3773 }
3774
3775 if(nc) {
3776 data->info.httpcode = k->httpcode;
3777
3778 data->info.httpversion = conn->httpversion;
3779 if(!data->state.httpversion ||
3780 data->state.httpversion > conn->httpversion)
3781 /* store the lowest server version we encounter */
3782 data->state.httpversion = conn->httpversion;
3783
3784 /*
3785 * This code executes as part of processing the header. As a
3786 * result, it's not totally clear how to interpret the
3787 * response code yet as that depends on what other headers may
3788 * be present. 401 and 407 may be errors, but may be OK
3789 * depending on how authentication is working. Other codes
3790 * are definitely errors, so give up here.
3791 */
3792 if(data->state.resume_from && data->set.httpreq == HTTPREQ_GET &&
3793 k->httpcode == 416) {
3794 /* "Requested Range Not Satisfiable", just proceed and
3795 pretend this is no error */
3796 k->ignorebody = TRUE; /* Avoid appending error msg to good data. */
3797 }
3798 else if(data->set.http_fail_on_error && (k->httpcode >= 400) &&
3799 ((k->httpcode != 401) || !conn->bits.user_passwd) &&
3800 ((k->httpcode != 407) || !conn->bits.proxy_user_passwd) ) {
3801 /* serious error, go home! */
3802 print_http_error(data);
3803 return CURLE_HTTP_RETURNED_ERROR;
3804 }
3805
3806 if(conn->httpversion == 10) {
3807 /* Default action for HTTP/1.0 must be to close, unless
3808 we get one of those fancy headers that tell us the
3809 server keeps it open for us! */
3810 infof(data, "HTTP 1.0, assume close after body\n");
3811 connclose(conn, "HTTP/1.0 close after body");
3812 }
3813 else if(conn->httpversion == 20 ||
3814 (k->upgr101 == UPGR101_REQUESTED && k->httpcode == 101)) {
3815 DEBUGF(infof(data, "HTTP/2 found, allow multiplexing\n"));
3816
3817 /* HTTP/2 cannot blacklist multiplexing since it is a core
3818 functionality of the protocol */
3819 conn->bundle->multiuse = BUNDLE_MULTIPLEX;
3820 }
3821 else if(conn->httpversion >= 11 &&
3822 !conn->bits.close) {
3823 /* If HTTP version is >= 1.1 and connection is persistent */
3824 DEBUGF(infof(data,
3825 "HTTP 1.1 or later with persistent connection\n"));
3826 }
3827
3828 k->http_bodyless = k->httpcode >= 100 && k->httpcode < 200;
3829 switch(k->httpcode) {
3830 case 304:
3831 /* (quote from RFC2616, section 10.3.5): The 304 response
3832 * MUST NOT contain a message-body, and thus is always
3833 * terminated by the first empty line after the header
3834 * fields. */
3835 if(data->set.timecondition)
3836 data->info.timecond = TRUE;
3837 /* FALLTHROUGH */
3838 case 204:
3839 /* (quote from RFC2616, section 10.2.5): The server has
3840 * fulfilled the request but does not need to return an
3841 * entity-body ... The 204 response MUST NOT include a
3842 * message-body, and thus is always terminated by the first
3843 * empty line after the header fields. */
3844 k->size = 0;
3845 k->maxdownload = 0;
3846 k->http_bodyless = TRUE;
3847 break;
3848 default:
3849 break;
3850 }
3851 }
3852 else {
3853 k->header = FALSE; /* this is not a header line */
3854 break;
3855 }
3856 }
3857
3858 result = Curl_convert_from_network(data, k->p, strlen(k->p));
3859 /* Curl_convert_from_network calls failf if unsuccessful */
3860 if(result)
3861 return result;
3862
3863 /* Check for Content-Length: header lines to get size */
3864 if(!k->http_bodyless &&
3865 !data->set.ignorecl && checkprefix("Content-Length:", k->p)) {
3866 curl_off_t contentlength;
3867 CURLofft offt = curlx_strtoofft(k->p + 15, NULL, 10, &contentlength);
3868
3869 if(offt == CURL_OFFT_OK) {
3870 if(data->set.max_filesize &&
3871 contentlength > data->set.max_filesize) {
3872 failf(data, "Maximum file size exceeded");
3873 return CURLE_FILESIZE_EXCEEDED;
3874 }
3875 k->size = contentlength;
3876 k->maxdownload = k->size;
3877 /* we set the progress download size already at this point
3878 just to make it easier for apps/callbacks to extract this
3879 info as soon as possible */
3880 Curl_pgrsSetDownloadSize(data, k->size);
3881 }
3882 else if(offt == CURL_OFFT_FLOW) {
3883 /* out of range */
3884 if(data->set.max_filesize) {
3885 failf(data, "Maximum file size exceeded");
3886 return CURLE_FILESIZE_EXCEEDED;
3887 }
3888 streamclose(conn, "overflow content-length");
3889 infof(data, "Overflow Content-Length: value!\n");
3890 }
3891 else {
3892 /* negative or just rubbish - bad HTTP */
3893 failf(data, "Invalid Content-Length: value");
3894 return CURLE_WEIRD_SERVER_REPLY;
3895 }
3896 }
3897 /* check for Content-Type: header lines to get the MIME-type */
3898 else if(checkprefix("Content-Type:", k->p)) {
3899 char *contenttype = Curl_copy_header_value(k->p);
3900 if(!contenttype)
3901 return CURLE_OUT_OF_MEMORY;
3902 if(!*contenttype)
3903 /* ignore empty data */
3904 free(contenttype);
3905 else {
3906 Curl_safefree(data->info.contenttype);
3907 data->info.contenttype = contenttype;
3908 }
3909 }
3910 else if((conn->httpversion == 10) &&
3911 conn->bits.httpproxy &&
3912 Curl_compareheader(k->p,
3913 "Proxy-Connection:", "keep-alive")) {
3914 /*
3915 * When a HTTP/1.0 reply comes when using a proxy, the
3916 * 'Proxy-Connection: keep-alive' line tells us the
3917 * connection will be kept alive for our pleasure.
3918 * Default action for 1.0 is to close.
3919 */
3920 connkeep(conn, "Proxy-Connection keep-alive"); /* don't close */
3921 infof(data, "HTTP/1.0 proxy connection set to keep alive!\n");
3922 }
3923 else if((conn->httpversion == 11) &&
3924 conn->bits.httpproxy &&
3925 Curl_compareheader(k->p,
3926 "Proxy-Connection:", "close")) {
3927 /*
3928 * We get a HTTP/1.1 response from a proxy and it says it'll
3929 * close down after this transfer.
3930 */
3931 connclose(conn, "Proxy-Connection: asked to close after done");
3932 infof(data, "HTTP/1.1 proxy connection set close!\n");
3933 }
3934 else if((conn->httpversion == 10) &&
3935 Curl_compareheader(k->p, "Connection:", "keep-alive")) {
3936 /*
3937 * A HTTP/1.0 reply with the 'Connection: keep-alive' line
3938 * tells us the connection will be kept alive for our
3939 * pleasure. Default action for 1.0 is to close.
3940 *
3941 * [RFC2068, section 19.7.1] */
3942 connkeep(conn, "Connection keep-alive");
3943 infof(data, "HTTP/1.0 connection set to keep alive!\n");
3944 }
3945 else if(Curl_compareheader(k->p, "Connection:", "close")) {
3946 /*
3947 * [RFC 2616, section 8.1.2.1]
3948 * "Connection: close" is HTTP/1.1 language and means that
3949 * the connection will close when this request has been
3950 * served.
3951 */
3952 streamclose(conn, "Connection: close used");
3953 }
3954 else if(!k->http_bodyless && checkprefix("Transfer-Encoding:", k->p)) {
3955 /* One or more encodings. We check for chunked and/or a compression
3956 algorithm. */
3957 /*
3958 * [RFC 2616, section 3.6.1] A 'chunked' transfer encoding
3959 * means that the server will send a series of "chunks". Each
3960 * chunk starts with line with info (including size of the
3961 * coming block) (terminated with CRLF), then a block of data
3962 * with the previously mentioned size. There can be any amount
3963 * of chunks, and a chunk-data set to zero signals the
3964 * end-of-chunks. */
3965
3966 result = Curl_build_unencoding_stack(conn, k->p + 18, TRUE);
3967 if(result)
3968 return result;
3969 }
3970 else if(!k->http_bodyless && checkprefix("Content-Encoding:", k->p) &&
3971 data->set.str[STRING_ENCODING]) {
3972 /*
3973 * Process Content-Encoding. Look for the values: identity,
3974 * gzip, deflate, compress, x-gzip and x-compress. x-gzip and
3975 * x-compress are the same as gzip and compress. (Sec 3.5 RFC
3976 * 2616). zlib cannot handle compress. However, errors are
3977 * handled further down when the response body is processed
3978 */
3979 result = Curl_build_unencoding_stack(conn, k->p + 17, FALSE);
3980 if(result)
3981 return result;
3982 }
3983 else if(checkprefix("Retry-After:", k->p)) {
3984 /* Retry-After = HTTP-date / delay-seconds */
3985 curl_off_t retry_after = 0; /* zero for unknown or "now" */
3986 time_t date = Curl_getdate_capped(&k->p[12]);
3987 if(-1 == date) {
3988 /* not a date, try it as a decimal number */
3989 (void)curlx_strtoofft(&k->p[12], NULL, 10, &retry_after);
3990 }
3991 else
3992 /* convert date to number of seconds into the future */
3993 retry_after = date - time(NULL);
3994 data->info.retry_after = retry_after; /* store it */
3995 }
3996 else if(!k->http_bodyless && checkprefix("Content-Range:", k->p)) {
3997 /* Content-Range: bytes [num]-
3998 Content-Range: bytes: [num]-
3999 Content-Range: [num]-
4000 Content-Range: [asterisk]/[total]
4001
4002 The second format was added since Sun's webserver
4003 JavaWebServer/1.1.1 obviously sends the header this way!
4004 The third added since some servers use that!
4005 The forth means the requested range was unsatisfied.
4006 */
4007
4008 char *ptr = k->p + 14;
4009
4010 /* Move forward until first digit or asterisk */
4011 while(*ptr && !ISDIGIT(*ptr) && *ptr != '*')
4012 ptr++;
4013
4014 /* if it truly stopped on a digit */
4015 if(ISDIGIT(*ptr)) {
4016 if(!curlx_strtoofft(ptr, NULL, 10, &k->offset)) {
4017 if(data->state.resume_from == k->offset)
4018 /* we asked for a resume and we got it */
4019 k->content_range = TRUE;
4020 }
4021 }
4022 else
4023 data->state.resume_from = 0; /* get everything */
4024 }
4025 #if !defined(CURL_DISABLE_COOKIES)
4026 else if(data->cookies && data->state.cookie_engine &&
4027 checkprefix("Set-Cookie:", k->p)) {
4028 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE,
4029 CURL_LOCK_ACCESS_SINGLE);
4030 Curl_cookie_add(data,
4031 data->cookies, TRUE, FALSE, k->p + 11,
4032 /* If there is a custom-set Host: name, use it
4033 here, or else use real peer host name. */
4034 conn->allocptr.cookiehost?
4035 conn->allocptr.cookiehost:conn->host.name,
4036 data->state.up.path,
4037 (conn->handler->protocol&CURLPROTO_HTTPS)?
4038 TRUE:FALSE);
4039 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
4040 }
4041 #endif
4042 else if(!k->http_bodyless && checkprefix("Last-Modified:", k->p) &&
4043 (data->set.timecondition || data->set.get_filetime) ) {
4044 k->timeofdoc = Curl_getdate_capped(k->p + strlen("Last-Modified:"));
4045 if(data->set.get_filetime)
4046 data->info.filetime = k->timeofdoc;
4047 }
4048 else if((checkprefix("WWW-Authenticate:", k->p) &&
4049 (401 == k->httpcode)) ||
4050 (checkprefix("Proxy-authenticate:", k->p) &&
4051 (407 == k->httpcode))) {
4052
4053 bool proxy = (k->httpcode == 407) ? TRUE : FALSE;
4054 char *auth = Curl_copy_header_value(k->p);
4055 if(!auth)
4056 return CURLE_OUT_OF_MEMORY;
4057
4058 result = Curl_http_input_auth(conn, proxy, auth);
4059
4060 free(auth);
4061
4062 if(result)
4063 return result;
4064 }
4065 #ifdef USE_SPNEGO
4066 else if(checkprefix("Persistent-Auth", k->p)) {
4067 struct negotiatedata *negdata = &conn->negotiate;
4068 struct auth *authp = &data->state.authhost;
4069 if(authp->picked == CURLAUTH_NEGOTIATE) {
4070 char *persistentauth = Curl_copy_header_value(k->p);
4071 if(!persistentauth)
4072 return CURLE_OUT_OF_MEMORY;
4073 negdata->noauthpersist = checkprefix("false", persistentauth)?
4074 TRUE:FALSE;
4075 negdata->havenoauthpersist = TRUE;
4076 infof(data, "Negotiate: noauthpersist -> %d, header part: %s",
4077 negdata->noauthpersist, persistentauth);
4078 free(persistentauth);
4079 }
4080 }
4081 #endif
4082 else if((k->httpcode >= 300 && k->httpcode < 400) &&
4083 checkprefix("Location:", k->p) &&
4084 !data->req.location) {
4085 /* this is the URL that the server advises us to use instead */
4086 char *location = Curl_copy_header_value(k->p);
4087 if(!location)
4088 return CURLE_OUT_OF_MEMORY;
4089 if(!*location)
4090 /* ignore empty data */
4091 free(location);
4092 else {
4093 data->req.location = location;
4094
4095 if(data->set.http_follow_location) {
4096 DEBUGASSERT(!data->req.newurl);
4097 data->req.newurl = strdup(data->req.location); /* clone */
4098 if(!data->req.newurl)
4099 return CURLE_OUT_OF_MEMORY;
4100
4101 /* some cases of POST and PUT etc needs to rewind the data
4102 stream at this point */
4103 result = http_perhapsrewind(conn);
4104 if(result)
4105 return result;
4106 }
4107 }
4108 }
4109 #ifdef USE_ALTSVC
4110 /* If enabled, the header is incoming and this is over HTTPS */
4111 else if(data->asi && checkprefix("Alt-Svc:", k->p) &&
4112 ((conn->handler->flags & PROTOPT_SSL) ||
4113 #ifdef CURLDEBUG
4114 /* allow debug builds to circumvent the HTTPS restriction */
4115 getenv("CURL_ALTSVC_HTTP")
4116 #else
4117 0
4118 #endif
4119 )) {
4120 /* the ALPN of the current request */
4121 enum alpnid id = (conn->httpversion == 20) ? ALPN_h2 : ALPN_h1;
4122 result = Curl_altsvc_parse(data, data->asi,
4123 &k->p[ strlen("Alt-Svc:") ],
4124 id, conn->host.name,
4125 curlx_uitous(conn->remote_port));
4126 if(result)
4127 return result;
4128 }
4129 #endif
4130 else if(conn->handler->protocol & CURLPROTO_RTSP) {
4131 result = Curl_rtsp_parseheader(conn, k->p);
4132 if(result)
4133 return result;
4134 }
4135
4136 /*
4137 * End of header-checks. Write them to the client.
4138 */
4139
4140 writetype = CLIENTWRITE_HEADER;
4141 if(data->set.include_header)
4142 writetype |= CLIENTWRITE_BODY;
4143
4144 if(data->set.verbose)
4145 Curl_debug(data, CURLINFO_HEADER_IN, k->p, (size_t)k->hbuflen);
4146
4147 result = Curl_client_write(conn, writetype, k->p, k->hbuflen);
4148 if(result)
4149 return result;
4150
4151 data->info.header_size += (long)k->hbuflen;
4152 data->req.headerbytecount += (long)k->hbuflen;
4153
4154 /* reset hbufp pointer && hbuflen */
4155 k->hbufp = data->state.headerbuff;
4156 k->hbuflen = 0;
4157 }
4158 while(*k->str); /* header line within buffer */
4159
4160 /* We might have reached the end of the header part here, but
4161 there might be a non-header part left in the end of the read
4162 buffer. */
4163
4164 return CURLE_OK;
4165 }
4166
4167 #endif /* CURL_DISABLE_HTTP */