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