~ubuntu-branches/ubuntu/natty/curl/natty-proposed

« back to all changes in this revision

Viewing changes to lib/transfer.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-05-24 21:12:19 UTC
  • mfrom: (1.1.12 upstream)
  • mto: (3.3.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: james.westby@ubuntu.com-20090524211219-7jgcwuhl04ixuqsm
Tags: upstream-7.19.5
ImportĀ upstreamĀ versionĀ 7.19.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: transfer.c,v 1.392 2008-05-26 20:39:41 bagder Exp $
 
21
 * $Id: transfer.c,v 1.431 2009-05-11 07:53:38 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
33
33
 
34
34
#include "strtoofft.h"
35
35
#include "strequal.h"
 
36
#include "rawstr.h"
36
37
 
37
38
#ifdef WIN32
38
39
#include <time.h>
62
63
#ifdef HAVE_SYS_IOCTL_H
63
64
#include <sys/ioctl.h>
64
65
#endif
 
66
#ifdef HAVE_SIGNAL_H
65
67
#include <signal.h>
 
68
#endif
66
69
 
67
70
#ifdef HAVE_SYS_PARAM_H
68
71
#include <sys/param.h>
96
99
#include "http_ntlm.h"
97
100
#include "http_negotiate.h"
98
101
#include "share.h"
99
 
#include "memory.h"
 
102
#include "curl_memory.h"
100
103
#include "select.h"
101
104
#include "multiif.h"
102
105
#include "easyif.h" /* for Curl_convert_to_network prototype */
109
112
 
110
113
#define CURL_TIMEOUT_EXPECT_100 1000 /* counting ms here */
111
114
 
 
115
 
 
116
#ifndef CURL_DISABLE_HTTP
 
117
static CURLcode readwrite_http_headers(struct SessionHandle *data,
 
118
                                       struct connectdata *conn,
 
119
                                       struct SingleRequest *k,
 
120
                                       ssize_t *nread,
 
121
                                       bool *stop_reading);
 
122
#endif /* CURL_DISABLE_HTTP */
 
123
 
112
124
/*
113
125
 * This function will call the read callback to fill our buffer with data
114
126
 * to upload.
118
130
  struct SessionHandle *data = conn->data;
119
131
  size_t buffersize = (size_t)bytes;
120
132
  int nread;
 
133
  bool sending_http_headers = FALSE;
121
134
 
122
135
  if(data->req.upload_chunky) {
123
136
    /* if chunked Transfer-Encoding */
124
137
    buffersize -= (8 + 2 + 2);   /* 32bit hex + CRLF + CRLF */
125
 
    data->req.upload_fromhere += 10; /* 32bit hex + CRLF */
 
138
    data->req.upload_fromhere += (8 + 2); /* 32bit hex + CRLF */
 
139
  }
 
140
  if((conn->protocol&PROT_HTTP) &&
 
141
     (data->state.proto.http->sending == HTTPSEND_REQUEST)) {
 
142
    /* We're sending the HTTP request headers, not the data.
 
143
       Remember that so we don't re-translate them into garbage. */
 
144
    sending_http_headers = TRUE;
126
145
  }
127
146
 
128
147
  /* this function returns a size_t, so we typecast to int to prevent warnings
132
151
 
133
152
  if(nread == CURL_READFUNC_ABORT) {
134
153
    failf(data, "operation aborted by callback");
 
154
    *nreadp = 0;
135
155
    return CURLE_ABORTED_BY_CALLBACK;
136
156
  }
137
157
  else if(nread == CURL_READFUNC_PAUSE) {
138
158
    struct SingleRequest *k = &data->req;
139
 
    k->keepon |= KEEP_READ_PAUSE; /* mark reading as paused */
 
159
    /* CURL_READFUNC_PAUSE pauses read callbacks that feed socket writes */
 
160
    k->keepon |= KEEP_SEND_PAUSE; /* mark socket send as paused */
 
161
    if(data->req.upload_chunky) {
 
162
      /* Back out the preallocation done above */
 
163
      data->req.upload_fromhere -= (8 + 2);
 
164
    }
 
165
    *nreadp = 0;
140
166
    return CURLE_OK; /* nothing was read */
141
167
  }
142
 
  else if((size_t)nread > buffersize)
 
168
  else if((size_t)nread > buffersize) {
143
169
    /* the read function returned a too large value */
 
170
    *nreadp = 0;
 
171
    failf(data, "read function returned funny value");
144
172
    return CURLE_READ_ERROR;
 
173
  }
145
174
 
146
175
  if(!data->req.forbidchunk && data->req.upload_chunky) {
147
 
    /* if chunked Transfer-Encoding */
 
176
    /* if chunked Transfer-Encoding 
 
177
     *    build chunk:
 
178
     *
 
179
     *        <HEX SIZE> CRLF
 
180
     *        <DATA> CRLF
 
181
     */
 
182
    /* On non-ASCII platforms the <DATA> may or may not be
 
183
       translated based on set.prefer_ascii while the protocol
 
184
       portion must always be translated to the network encoding.
 
185
       To further complicate matters, line end conversion might be
 
186
       done later on, so we need to prevent CRLFs from becoming
 
187
       CRCRLFs if that's the case.  To do this we use bare LFs
 
188
       here, knowing they'll become CRLFs later on.
 
189
     */
 
190
 
148
191
    char hexbuffer[11];
149
 
    int hexlen = snprintf(hexbuffer, sizeof(hexbuffer),
150
 
                          "%x\r\n", nread);
 
192
    const char *endofline_native;
 
193
    const char *endofline_network;
 
194
    int hexlen;
 
195
#ifdef CURL_DO_LINEEND_CONV
 
196
    if((data->set.crlf) || (data->set.prefer_ascii)) {
 
197
#else
 
198
    if(data->set.crlf) {
 
199
#endif /* CURL_DO_LINEEND_CONV */
 
200
      /* \n will become \r\n later on */
 
201
      endofline_native  = "\n";
 
202
      endofline_network = "\x0a";
 
203
    } else {
 
204
      endofline_native  = "\r\n";
 
205
      endofline_network = "\x0d\x0a";
 
206
    }
 
207
    hexlen = snprintf(hexbuffer, sizeof(hexbuffer),
 
208
                      "%x%s", nread, endofline_native);
 
209
 
151
210
    /* move buffer pointer */
152
211
    data->req.upload_fromhere -= hexlen;
153
212
    nread += hexlen;
154
213
 
155
 
    /* copy the prefix to the buffer */
 
214
    /* copy the prefix to the buffer, leaving out the NUL */
156
215
    memcpy(data->req.upload_fromhere, hexbuffer, hexlen);
157
216
 
158
 
    /* always append CRLF to the data */
159
 
    memcpy(data->req.upload_fromhere + nread, "\r\n", 2);
 
217
    /* always append ASCII CRLF to the data */  
 
218
    memcpy(data->req.upload_fromhere + nread, 
 
219
           endofline_network, 
 
220
           strlen(endofline_network));
 
221
 
 
222
#ifdef CURL_DOES_CONVERSIONS
 
223
    CURLcode res;
 
224
    int length;
 
225
    if(data->set.prefer_ascii) {
 
226
      /* translate the protocol and data */
 
227
      length = nread;
 
228
    } else {
 
229
      /* just translate the protocol portion */
 
230
      length = strlen(hexbuffer);
 
231
    }
 
232
    res = Curl_convert_to_network(data, data->req.upload_fromhere, length);
 
233
    /* Curl_convert_to_network calls failf if unsuccessful */
 
234
    if(res != CURLE_OK) {
 
235
      return(res);
 
236
    }
 
237
#endif /* CURL_DOES_CONVERSIONS */
160
238
 
161
239
    if((nread - hexlen) == 0) {
162
240
      /* mark this as done once this chunk is transfered */
163
241
      data->req.upload_done = TRUE;
164
242
    }
165
243
 
166
 
    nread+=2; /* for the added CRLF */
167
 
  }
 
244
    nread+=strlen(endofline_native); /* for the added end of line */
 
245
  }
 
246
#ifdef CURL_DOES_CONVERSIONS
 
247
  else if((data->set.prefer_ascii) && (!sending_http_headers)) {
 
248
    CURLcode res;
 
249
    res = Curl_convert_to_network(data, data->req.upload_fromhere, nread);
 
250
    /* Curl_convert_to_network calls failf if unsuccessful */
 
251
    if(res != CURLE_OK)
 
252
      return(res);
 
253
  }
 
254
#endif /* CURL_DOES_CONVERSIONS */
168
255
 
169
256
  *nreadp = nread;
170
257
 
171
 
#ifdef CURL_DOES_CONVERSIONS
172
 
  if(data->set.prefer_ascii) {
173
 
    CURLcode res;
174
 
    res = Curl_convert_to_network(data, data->req.upload_fromhere, nread);
175
 
    /* Curl_convert_to_network calls failf if unsuccessful */
176
 
    if(res != CURLE_OK) {
177
 
      return(res);
178
 
    }
179
 
  }
180
 
#endif /* CURL_DOES_CONVERSIONS */
181
 
 
182
258
  return CURLE_OK;
183
259
}
184
260
 
 
261
#ifndef CURL_DISABLE_HTTP
185
262
/*
186
263
 * checkhttpprefix()
187
264
 *
226
303
#endif /* CURL_DOES_CONVERSIONS */
227
304
  return rc;
228
305
}
 
306
#endif   /* CURL_DISABLE_HTTP */
229
307
 
230
308
/*
231
309
 * Curl_readrewind() rewinds the read stream. This is typically used for HTTP
242
320
     about to restart a new transfer and thus we want to avoid inadvertently
243
321
     sending more data on the existing connection until the next transfer
244
322
     starts */
245
 
  data->req.keepon &= ~KEEP_WRITE;
 
323
  data->req.keepon &= ~KEEP_SEND;
246
324
 
247
325
  /* We have sent away data. If not using CURLOPT_POSTFIELDS or
248
326
     CURLOPT_HTTPPOST, call app to rewind
283
361
          return CURLE_OK;
284
362
      }
285
363
 
286
 
      /* no callback set or failure aboe, makes us fail at once */
 
364
      /* no callback set or failure above, makes us fail at once */
287
365
      failf(data, "necessary data rewind wasn't possible");
288
366
      return CURLE_SEND_FAIL_REWIND;
289
367
    }
299
377
    Curl_ssl_data_pending(conn, FIRSTSOCKET);
300
378
}
301
379
 
302
 
#ifndef MIN
303
 
#define MIN(a,b) (a < b ? a : b)
304
 
#endif
305
 
 
306
380
static void read_rewind(struct connectdata *conn,
307
381
                        size_t thismuch)
308
382
{
314
388
    char buf[512 + 1];
315
389
    size_t show;
316
390
 
317
 
    show = MIN(conn->buf_len - conn->read_pos, sizeof(buf)-1);
 
391
    show = CURLMIN(conn->buf_len - conn->read_pos, sizeof(buf)-1);
318
392
    if(conn->master_buffer) {
319
393
        memcpy(buf, conn->master_buffer + conn->read_pos, show);
320
394
        buf[show] = '\0';
330
404
#endif
331
405
}
332
406
 
 
407
 
 
408
/*
 
409
 * Go ahead and do a read if we have a readable socket or if
 
410
 * the stream was rewound (in which case we have data in a
 
411
 * buffer)
 
412
 */
 
413
static CURLcode readwrite_data(struct SessionHandle *data,
 
414
                               struct connectdata *conn,
 
415
                               struct SingleRequest *k,
 
416
                               int *didwhat, bool *done)
 
417
{
 
418
  CURLcode result = CURLE_OK;
 
419
  ssize_t nread; /* number of bytes read */
 
420
  bool is_empty_data = FALSE;
 
421
 
 
422
  *done = FALSE;
 
423
 
 
424
  /* This is where we loop until we have read everything there is to
 
425
     read or we get a EWOULDBLOCK */
 
426
  do {
 
427
    size_t buffersize = data->set.buffer_size?
 
428
      data->set.buffer_size : BUFSIZE;
 
429
    size_t bytestoread = buffersize;
 
430
    int readrc;
 
431
 
 
432
    if(k->size != -1 && !k->header) {
 
433
      /* make sure we don't read "too much" if we can help it since we
 
434
         might be pipelining and then someone else might want to read what
 
435
         follows! */
 
436
      curl_off_t totalleft = k->size - k->bytecount;
 
437
      if(totalleft < (curl_off_t)bytestoread)
 
438
        bytestoread = (size_t)totalleft;
 
439
    }
 
440
 
 
441
    if(bytestoread) {
 
442
      /* receive data from the network! */
 
443
      readrc = Curl_read(conn, conn->sockfd, k->buf, bytestoread, &nread);
 
444
 
 
445
      /* subzero, this would've blocked */
 
446
      if(0 > readrc)
 
447
        break; /* get out of loop */
 
448
 
 
449
      /* get the CURLcode from the int */
 
450
      result = (CURLcode)readrc;
 
451
 
 
452
      if(result>0)
 
453
        return result;
 
454
    }
 
455
    else {
 
456
      /* read nothing but since we wanted nothing we consider this an OK
 
457
         situation to proceed from */
 
458
      nread = 0;
 
459
    }
 
460
 
 
461
    if((k->bytecount == 0) && (k->writebytecount == 0)) {
 
462
      Curl_pgrsTime(data, TIMER_STARTTRANSFER);
 
463
      if(k->exp100 > EXP100_SEND_DATA)
 
464
        /* set time stamp to compare with when waiting for the 100 */
 
465
        k->start100 = Curl_tvnow();
 
466
    }
 
467
 
 
468
    *didwhat |= KEEP_RECV;
 
469
    /* indicates data of zero size, i.e. empty file */
 
470
    is_empty_data = (bool)((nread == 0) && (k->bodywrites == 0));
 
471
 
 
472
    /* NUL terminate, allowing string ops to be used */
 
473
    if(0 < nread || is_empty_data) {
 
474
      k->buf[nread] = 0;
 
475
    }
 
476
    else if(0 >= nread) {
 
477
      /* if we receive 0 or less here, the server closed the connection
 
478
         and we bail out from this! */
 
479
      DEBUGF(infof(data, "nread <= 0, server closed connection, bailing\n"));
 
480
      k->keepon &= ~KEEP_RECV;
 
481
      break;
 
482
    }
 
483
 
 
484
    /* Default buffer to use when we write the buffer, it may be changed
 
485
       in the flow below before the actual storing is done. */
 
486
    k->str = k->buf;
 
487
 
 
488
#ifndef CURL_DISABLE_HTTP
 
489
    /* Since this is a two-state thing, we check if we are parsing
 
490
       headers at the moment or not. */
 
491
    if(k->header) {
 
492
      /* we are in parse-the-header-mode */
 
493
      bool stop_reading = FALSE;
 
494
      result = readwrite_http_headers(data, conn, k, &nread, &stop_reading);
 
495
      if(result)
 
496
        return result;
 
497
      if(stop_reading)
 
498
        /* We've stopped dealing with input, get out of the do-while loop */
 
499
        break;
 
500
    }
 
501
#endif /* CURL_DISABLE_HTTP */
 
502
 
 
503
 
 
504
    /* This is not an 'else if' since it may be a rest from the header
 
505
       parsing, where the beginning of the buffer is headers and the end
 
506
       is non-headers. */
 
507
    if(k->str && !k->header && (nread > 0 || is_empty_data)) {
 
508
 
 
509
#ifndef CURL_DISABLE_HTTP
 
510
      if(0 == k->bodywrites && !is_empty_data) {
 
511
        /* These checks are only made the first time we are about to
 
512
           write a piece of the body */
 
513
        if(conn->protocol&PROT_HTTP) {
 
514
          /* HTTP-only checks */
 
515
 
 
516
          if(data->req.newurl) {
 
517
            if(conn->bits.close) {
 
518
              /* Abort after the headers if "follow Location" is set
 
519
                 and we're set to close anyway. */
 
520
              k->keepon &= ~KEEP_RECV;
 
521
              *done = TRUE;
 
522
              return CURLE_OK;
 
523
            }
 
524
            /* We have a new url to load, but since we want to be able
 
525
               to re-use this connection properly, we read the full
 
526
               response in "ignore more" */
 
527
            k->ignorebody = TRUE;
 
528
            infof(data, "Ignoring the response-body\n");
 
529
          }
 
530
          if(data->state.resume_from && !k->content_range &&
 
531
             (data->set.httpreq==HTTPREQ_GET) &&
 
532
             !k->ignorebody) {
 
533
            /* we wanted to resume a download, although the server doesn't
 
534
             * seem to support this and we did this with a GET (if it
 
535
             * wasn't a GET we did a POST or PUT resume) */
 
536
            failf(data, "HTTP server doesn't seem to support "
 
537
                  "byte ranges. Cannot resume.");
 
538
            return CURLE_RANGE_ERROR;
 
539
          }
 
540
 
 
541
          if(data->set.timecondition && !data->state.range) {
 
542
            /* A time condition has been set AND no ranges have been
 
543
               requested. This seems to be what chapter 13.3.4 of
 
544
               RFC 2616 defines to be the correct action for a
 
545
               HTTP/1.1 client */
 
546
            if((k->timeofdoc > 0) && (data->set.timevalue > 0)) {
 
547
              switch(data->set.timecondition) {
 
548
              case CURL_TIMECOND_IFMODSINCE:
 
549
              default:
 
550
                if(k->timeofdoc < data->set.timevalue) {
 
551
                  infof(data,
 
552
                        "The requested document is not new enough\n");
 
553
                  *done = TRUE;
 
554
                  data->info.timecond = TRUE;
 
555
                  return CURLE_OK;
 
556
                }
 
557
                break;
 
558
              case CURL_TIMECOND_IFUNMODSINCE:
 
559
                if(k->timeofdoc > data->set.timevalue) {
 
560
                  infof(data,
 
561
                        "The requested document is not old enough\n");
 
562
                  *done = TRUE;
 
563
                  data->info.timecond = TRUE;
 
564
                  return CURLE_OK;
 
565
                }
 
566
                break;
 
567
              } /* switch */
 
568
            } /* two valid time strings */
 
569
          } /* we have a time condition */
 
570
 
 
571
        } /* this is HTTP */
 
572
      } /* this is the first time we write a body part */
 
573
#endif /* CURL_DISABLE_HTTP */
 
574
      k->bodywrites++;
 
575
 
 
576
      /* pass data to the debug function before it gets "dechunked" */
 
577
      if(data->set.verbose) {
 
578
        if(k->badheader) {
 
579
          Curl_debug(data, CURLINFO_DATA_IN, data->state.headerbuff,
 
580
                     (size_t)k->hbuflen, conn);
 
581
          if(k->badheader == HEADER_PARTHEADER)
 
582
            Curl_debug(data, CURLINFO_DATA_IN,
 
583
                       k->str, (size_t)nread, conn);
 
584
        }
 
585
        else
 
586
          Curl_debug(data, CURLINFO_DATA_IN,
 
587
                     k->str, (size_t)nread, conn);
 
588
      }
 
589
 
 
590
#ifndef CURL_DISABLE_HTTP
 
591
      if(k->chunk) {
 
592
        /*
 
593
         * Here comes a chunked transfer flying and we need to decode this
 
594
         * properly.  While the name says read, this function both reads
 
595
         * and writes away the data. The returned 'nread' holds the number
 
596
         * of actual data it wrote to the client.
 
597
         */
 
598
 
 
599
        CHUNKcode res =
 
600
          Curl_httpchunk_read(conn, k->str, nread, &nread);
 
601
 
 
602
        if(CHUNKE_OK < res) {
 
603
          if(CHUNKE_WRITE_ERROR == res) {
 
604
            failf(data, "Failed writing data");
 
605
            return CURLE_WRITE_ERROR;
 
606
          }
 
607
          failf(data, "Received problem %d in the chunky parser", res);
 
608
          return CURLE_RECV_ERROR;
 
609
        }
 
610
        else if(CHUNKE_STOP == res) {
 
611
          size_t dataleft;
 
612
          /* we're done reading chunks! */
 
613
          k->keepon &= ~KEEP_RECV; /* read no more */
 
614
 
 
615
          /* There are now possibly N number of bytes at the end of the
 
616
             str buffer that weren't written to the client.
 
617
 
 
618
             We DO care about this data if we are pipelining.
 
619
             Push it back to be read on the next pass. */
 
620
 
 
621
          dataleft = conn->chunk.dataleft;
 
622
          if(dataleft != 0) {
 
623
            infof(conn->data, "Leftovers after chunking. "
 
624
                  " Rewinding %d bytes\n",dataleft);
 
625
            read_rewind(conn, dataleft);
 
626
          }
 
627
        }
 
628
        /* If it returned OK, we just keep going */
 
629
      }
 
630
#endif   /* CURL_DISABLE_HTTP */
 
631
 
 
632
      if((-1 != k->maxdownload) &&
 
633
         (k->bytecount + nread >= k->maxdownload)) {
 
634
        /* The 'excess' amount below can't be more than BUFSIZE which
 
635
           always will fit in a size_t */
 
636
        size_t excess = (size_t)(k->bytecount + nread - k->maxdownload);
 
637
        if(excess > 0 && !k->ignorebody) {
 
638
          infof(data,
 
639
                "Rewinding stream by : %d"
 
640
                " bytes on url %s (size = %" FORMAT_OFF_T
 
641
                ", maxdownload = %" FORMAT_OFF_T
 
642
                ", bytecount = %" FORMAT_OFF_T ", nread = %d)\n",
 
643
                excess, data->state.path,
 
644
                k->size, k->maxdownload, k->bytecount, nread);
 
645
          read_rewind(conn, excess);
 
646
        }
 
647
 
 
648
        nread = (ssize_t) (k->maxdownload - k->bytecount);
 
649
        if(nread < 0 ) /* this should be unusual */
 
650
          nread = 0;
 
651
 
 
652
        k->keepon &= ~KEEP_RECV; /* we're done reading */
 
653
      }
 
654
 
 
655
      k->bytecount += nread;
 
656
 
 
657
      Curl_pgrsSetDownloadCounter(data, k->bytecount);
 
658
 
 
659
      if(!k->chunk && (nread || k->badheader || is_empty_data)) {
 
660
        /* If this is chunky transfer, it was already written */
 
661
 
 
662
        if(k->badheader && !k->ignorebody) {
 
663
          /* we parsed a piece of data wrongly assuming it was a header
 
664
             and now we output it as body instead */
 
665
          result = Curl_client_write(conn, CLIENTWRITE_BODY,
 
666
                                     data->state.headerbuff,
 
667
                                     k->hbuflen);
 
668
          if(result)
 
669
            return result;
 
670
        }
 
671
        if(k->badheader < HEADER_ALLBAD) {
 
672
          /* This switch handles various content encodings. If there's an
 
673
             error here, be sure to check over the almost identical code
 
674
             in http_chunks.c.
 
675
             Make sure that ALL_CONTENT_ENCODINGS contains all the
 
676
             encodings handled here. */
 
677
#ifdef HAVE_LIBZ
 
678
          switch (conn->data->set.http_ce_skip ?
 
679
                  IDENTITY : k->content_encoding) {
 
680
          case IDENTITY:
 
681
#endif
 
682
            /* This is the default when the server sends no
 
683
               Content-Encoding header. See Curl_readwrite_init; the
 
684
               memset() call initializes k->content_encoding to zero. */
 
685
            if(!k->ignorebody)
 
686
              result = Curl_client_write(conn, CLIENTWRITE_BODY, k->str,
 
687
                                         nread);
 
688
#ifdef HAVE_LIBZ
 
689
            break;
 
690
 
 
691
          case DEFLATE:
 
692
            /* Assume CLIENTWRITE_BODY; headers are not encoded. */
 
693
            if(!k->ignorebody)
 
694
              result = Curl_unencode_deflate_write(conn, k, nread);
 
695
            break;
 
696
 
 
697
          case GZIP:
 
698
            /* Assume CLIENTWRITE_BODY; headers are not encoded. */
 
699
            if(!k->ignorebody)
 
700
              result = Curl_unencode_gzip_write(conn, k, nread);
 
701
            break;
 
702
 
 
703
          case COMPRESS:
 
704
          default:
 
705
            failf (data, "Unrecognized content encoding type. "
 
706
                   "libcurl understands `identity', `deflate' and `gzip' "
 
707
                   "content encodings.");
 
708
            result = CURLE_BAD_CONTENT_ENCODING;
 
709
            break;
 
710
          }
 
711
#endif
 
712
        }
 
713
        k->badheader = HEADER_NORMAL; /* taken care of now */
 
714
 
 
715
        if(result)
 
716
          return result;
 
717
      }
 
718
 
 
719
    } /* if(! header and data to read ) */
 
720
 
 
721
    if(is_empty_data) {
 
722
      /* if we received nothing, the server closed the connection and we
 
723
         are done */
 
724
      k->keepon &= ~KEEP_RECV;
 
725
    }
 
726
 
 
727
  } while(data_pending(conn));
 
728
 
 
729
  if(((k->keepon & (KEEP_RECV|KEEP_SEND)) == KEEP_SEND) &&
 
730
     conn->bits.close ) {
 
731
    /* When we've read the entire thing and the close bit is set, the server
 
732
       may now close the connection. If there's now any kind of sending going
 
733
       on from our side, we need to stop that immediately. */
 
734
    infof(data, "we are done reading and this is set to close, stop send\n");
 
735
    k->keepon &= ~KEEP_SEND; /* no writing anymore either */
 
736
  }
 
737
 
 
738
  return CURLE_OK;
 
739
}
 
740
 
 
741
#ifndef CURL_DISABLE_HTTP
 
742
/*
 
743
 * Read any HTTP header lines from the server and pass them to the client app.
 
744
 */
 
745
static CURLcode readwrite_http_headers(struct SessionHandle *data,
 
746
                                       struct connectdata *conn,
 
747
                                       struct SingleRequest *k,
 
748
                                       ssize_t *nread,
 
749
                                       bool *stop_reading)
 
750
{
 
751
  CURLcode result;
 
752
 
 
753
  /* header line within buffer loop */
 
754
  do {
 
755
    size_t hbufp_index;
 
756
    size_t rest_length;
 
757
    size_t full_length;
 
758
    int writetype;
 
759
 
 
760
    /* str_start is start of line within buf */
 
761
    k->str_start = k->str;
 
762
 
 
763
    /* data is in network encoding so use 0x0a instead of '\n' */
 
764
    k->end_ptr = memchr(k->str_start, 0x0a, *nread);
 
765
 
 
766
    if(!k->end_ptr) {
 
767
      /* Not a complete header line within buffer, append the data to
 
768
         the end of the headerbuff. */
 
769
 
 
770
      if(k->hbuflen + *nread >= data->state.headersize) {
 
771
        /* We enlarge the header buffer as it is too small */
 
772
        char *newbuff;
 
773
        size_t newsize=CURLMAX((k->hbuflen+*nread)*3/2,
 
774
                               data->state.headersize*2);
 
775
        hbufp_index = k->hbufp - data->state.headerbuff;
 
776
        newbuff = realloc(data->state.headerbuff, newsize);
 
777
        if(!newbuff) {
 
778
          failf (data, "Failed to alloc memory for big header!");
 
779
          return CURLE_OUT_OF_MEMORY;
 
780
        }
 
781
        data->state.headersize=newsize;
 
782
        data->state.headerbuff = newbuff;
 
783
        k->hbufp = data->state.headerbuff + hbufp_index;
 
784
      }
 
785
      memcpy(k->hbufp, k->str, *nread);
 
786
      k->hbufp += *nread;
 
787
      k->hbuflen += *nread;
 
788
      if(!k->headerline && (k->hbuflen>5)) {
 
789
        /* make a first check that this looks like a HTTP header */
 
790
        if(!checkhttpprefix(data, data->state.headerbuff)) {
 
791
          /* this is not the beginning of a HTTP first header line */
 
792
          k->header = FALSE;
 
793
          k->badheader = HEADER_ALLBAD;
 
794
          break;
 
795
        }
 
796
      }
 
797
 
 
798
      break; /* read more and try again */
 
799
    }
 
800
 
 
801
    /* decrease the size of the remaining (supposed) header line */
 
802
    rest_length = (k->end_ptr - k->str)+1;
 
803
    *nread -= (ssize_t)rest_length;
 
804
 
 
805
    k->str = k->end_ptr + 1; /* move past new line */
 
806
 
 
807
    full_length = k->str - k->str_start;
 
808
 
 
809
    /*
 
810
     * We're about to copy a chunk of data to the end of the
 
811
     * already received header. We make sure that the full string
 
812
     * fit in the allocated header buffer, or else we enlarge
 
813
     * it.
 
814
     */
 
815
    if(k->hbuflen + full_length >=
 
816
       data->state.headersize) {
 
817
      char *newbuff;
 
818
      size_t newsize=CURLMAX((k->hbuflen+full_length)*3/2,
 
819
                             data->state.headersize*2);
 
820
      hbufp_index = k->hbufp - data->state.headerbuff;
 
821
      newbuff = realloc(data->state.headerbuff, newsize);
 
822
      if(!newbuff) {
 
823
        failf (data, "Failed to alloc memory for big header!");
 
824
        return CURLE_OUT_OF_MEMORY;
 
825
      }
 
826
      data->state.headersize= newsize;
 
827
      data->state.headerbuff = newbuff;
 
828
      k->hbufp = data->state.headerbuff + hbufp_index;
 
829
    }
 
830
 
 
831
    /* copy to end of line */
 
832
    memcpy(k->hbufp, k->str_start, full_length);
 
833
    k->hbufp += full_length;
 
834
    k->hbuflen += full_length;
 
835
    *k->hbufp = 0;
 
836
    k->end_ptr = k->hbufp;
 
837
 
 
838
    k->p = data->state.headerbuff;
 
839
 
 
840
    /****
 
841
     * We now have a FULL header line that p points to
 
842
     *****/
 
843
 
 
844
    if(!k->headerline) {
 
845
      /* the first read header */
 
846
      if((k->hbuflen>5) &&
 
847
         !checkhttpprefix(data, data->state.headerbuff)) {
 
848
        /* this is not the beginning of a HTTP first header line */
 
849
        k->header = FALSE;
 
850
        if(*nread)
 
851
          /* since there's more, this is a partial bad header */
 
852
          k->badheader = HEADER_PARTHEADER;
 
853
        else {
 
854
          /* this was all we read so it's all a bad header */
 
855
          k->badheader = HEADER_ALLBAD;
 
856
          *nread = (ssize_t)rest_length;
 
857
        }
 
858
        break;
 
859
      }
 
860
    }
 
861
 
 
862
    /* headers are in network encoding so
 
863
       use 0x0a and 0x0d instead of '\n' and '\r' */
 
864
    if((0x0a == *k->p) || (0x0d == *k->p)) {
 
865
      size_t headerlen;
 
866
      /* Zero-length header line means end of headers! */
 
867
 
 
868
#ifdef CURL_DOES_CONVERSIONS
 
869
      if(0x0d == *k->p) {
 
870
        *k->p = '\r'; /* replace with CR in host encoding */
 
871
        k->p++;       /* pass the CR byte */
 
872
      }
 
873
      if(0x0a == *k->p) {
 
874
        *k->p = '\n'; /* replace with LF in host encoding */
 
875
        k->p++;       /* pass the LF byte */
 
876
      }
 
877
#else
 
878
      if('\r' == *k->p)
 
879
        k->p++; /* pass the \r byte */
 
880
      if('\n' == *k->p)
 
881
        k->p++; /* pass the \n byte */
 
882
#endif /* CURL_DOES_CONVERSIONS */
 
883
 
 
884
      if(100 <= k->httpcode && 199 >= k->httpcode) {
 
885
        /*
 
886
         * We have made a HTTP PUT or POST and this is 1.1-lingo
 
887
         * that tells us that the server is OK with this and ready
 
888
         * to receive the data.
 
889
         * However, we'll get more headers now so we must get
 
890
         * back into the header-parsing state!
 
891
         */
 
892
        k->header = TRUE;
 
893
        k->headerline = 0; /* restart the header line counter */
 
894
 
 
895
        /* if we did wait for this do enable write now! */
 
896
        if(k->exp100) {
 
897
          k->exp100 = EXP100_SEND_DATA;
 
898
          k->keepon |= KEEP_SEND;
 
899
        }
 
900
      }
 
901
      else {
 
902
        k->header = FALSE; /* no more header to parse! */
 
903
 
 
904
        if((k->size == -1) && !k->chunk && !conn->bits.close &&
 
905
           (conn->httpversion >= 11) ) {
 
906
          /* On HTTP 1.1, when connection is not to get closed, but no
 
907
             Content-Length nor Content-Encoding chunked have been
 
908
             received, according to RFC2616 section 4.4 point 5, we
 
909
             assume that the server will close the connection to
 
910
             signal the end of the document. */
 
911
          infof(data, "no chunk, no close, no size. Assume close to "
 
912
                "signal end\n");
 
913
          conn->bits.close = TRUE;
 
914
        }
 
915
      }
 
916
 
 
917
      if(417 == k->httpcode) {
 
918
        /*
 
919
         * we got: "417 Expectation Failed" this means:
 
920
         * we have made a HTTP call and our Expect Header
 
921
         * seems to cause a problem => abort the write operations
 
922
         * (or prevent them from starting).
 
923
         */
 
924
        k->exp100 = EXP100_FAILED;
 
925
        k->keepon &= ~KEEP_SEND;
 
926
      }
 
927
 
 
928
      /*
 
929
       * When all the headers have been parsed, see if we should give
 
930
       * up and return an error.
 
931
       */
 
932
      if(Curl_http_should_fail(conn)) {
 
933
        failf (data, "The requested URL returned error: %d",
 
934
               k->httpcode);
 
935
        return CURLE_HTTP_RETURNED_ERROR;
 
936
      }
 
937
 
 
938
      /* now, only output this if the header AND body are requested:
 
939
       */
 
940
      writetype = CLIENTWRITE_HEADER;
 
941
      if(data->set.include_header)
 
942
        writetype |= CLIENTWRITE_BODY;
 
943
 
 
944
      headerlen = k->p - data->state.headerbuff;
 
945
 
 
946
      result = Curl_client_write(conn, writetype,
 
947
                                 data->state.headerbuff,
 
948
                                 headerlen);
 
949
      if(result)
 
950
        return result;
 
951
 
 
952
      data->info.header_size += (long)headerlen;
 
953
      data->req.headerbytecount += (long)headerlen;
 
954
 
 
955
      data->req.deductheadercount =
 
956
        (100 <= k->httpcode && 199 >= k->httpcode)?data->req.headerbytecount:0;
 
957
 
 
958
      if(data->state.resume_from &&
 
959
         (data->set.httpreq==HTTPREQ_GET) &&
 
960
         (k->httpcode == 416)) {
 
961
        /* "Requested Range Not Satisfiable" */
 
962
        *stop_reading = TRUE;
 
963
      }
 
964
 
 
965
      if(!*stop_reading) {
 
966
        /* Curl_http_auth_act() checks what authentication methods
 
967
         * that are available and decides which one (if any) to
 
968
         * use. It will set 'newurl' if an auth method was picked. */
 
969
        result = Curl_http_auth_act(conn);
 
970
 
 
971
        if(result)
 
972
          return result;
 
973
 
 
974
        if(conn->bits.rewindaftersend) {
 
975
          /* We rewind after a complete send, so thus we continue
 
976
             sending now */
 
977
          infof(data, "Keep sending data to get tossed away!\n");
 
978
          k->keepon |= KEEP_SEND;
 
979
        }
 
980
      }
 
981
 
 
982
      if(!k->header) {
 
983
        /*
 
984
         * really end-of-headers.
 
985
         *
 
986
         * If we requested a "no body", this is a good time to get
 
987
         * out and return home.
 
988
         */
 
989
        if(data->set.opt_no_body)
 
990
          *stop_reading = TRUE;
 
991
        else {
 
992
          /* If we know the expected size of this document, we set the
 
993
             maximum download size to the size of the expected
 
994
             document or else, we won't know when to stop reading!
 
995
 
 
996
             Note that we set the download maximum even if we read a
 
997
             "Connection: close" header, to make sure that
 
998
             "Content-Length: 0" still prevents us from attempting to
 
999
             read the (missing) response-body.
 
1000
          */
 
1001
          /* According to RFC2616 section 4.4, we MUST ignore
 
1002
             Content-Length: headers if we are now receiving data
 
1003
             using chunked Transfer-Encoding.
 
1004
          */
 
1005
          if(k->chunk)
 
1006
            k->size=-1;
 
1007
 
 
1008
        }
 
1009
        if(-1 != k->size) {
 
1010
          /* We do this operation even if no_body is true, since this
 
1011
             data might be retrieved later with curl_easy_getinfo()
 
1012
             and its CURLINFO_CONTENT_LENGTH_DOWNLOAD option. */
 
1013
 
 
1014
          Curl_pgrsSetDownloadSize(data, k->size);
 
1015
          k->maxdownload = k->size;
 
1016
        }
 
1017
        /* If max download size is *zero* (nothing) we already
 
1018
           have nothing and can safely return ok now! */
 
1019
        if(0 == k->maxdownload)
 
1020
          *stop_reading = TRUE;
 
1021
 
 
1022
        if(*stop_reading) {
 
1023
          /* we make sure that this socket isn't read more now */
 
1024
          k->keepon &= ~KEEP_RECV;
 
1025
        }
 
1026
 
 
1027
        if(data->set.verbose)
 
1028
          Curl_debug(data, CURLINFO_HEADER_IN,
 
1029
                     k->str_start, headerlen, conn);
 
1030
        break;          /* exit header line loop */
 
1031
      }
 
1032
 
 
1033
      /* We continue reading headers, so reset the line-based
 
1034
         header parsing variables hbufp && hbuflen */
 
1035
      k->hbufp = data->state.headerbuff;
 
1036
      k->hbuflen = 0;
 
1037
      continue;
 
1038
    }
 
1039
 
 
1040
    /*
 
1041
     * Checks for special headers coming up.
 
1042
     */
 
1043
 
 
1044
    if(!k->headerline++) {
 
1045
      /* This is the first header, it MUST be the error code line
 
1046
         or else we consider this to be the body right away! */
 
1047
      int httpversion_major;
 
1048
      int nc;
 
1049
#ifdef CURL_DOES_CONVERSIONS
 
1050
#define HEADER1 scratch
 
1051
#define SCRATCHSIZE 21
 
1052
      CURLcode res;
 
1053
      char scratch[SCRATCHSIZE+1]; /* "HTTP/major.minor 123" */
 
1054
      /* We can't really convert this yet because we
 
1055
         don't know if it's the 1st header line or the body.
 
1056
         So we do a partial conversion into a scratch area,
 
1057
         leaving the data at k->p as-is.
 
1058
      */
 
1059
      strncpy(&scratch[0], k->p, SCRATCHSIZE);
 
1060
      scratch[SCRATCHSIZE] = 0; /* null terminate */
 
1061
      res = Curl_convert_from_network(data,
 
1062
                                      &scratch[0],
 
1063
                                      SCRATCHSIZE);
 
1064
      if(CURLE_OK != res) {
 
1065
        /* Curl_convert_from_network calls failf if unsuccessful */
 
1066
        return res;
 
1067
      }
 
1068
#else
 
1069
#define HEADER1 k->p /* no conversion needed, just use k->p */
 
1070
#endif /* CURL_DOES_CONVERSIONS */
 
1071
 
 
1072
      nc = sscanf(HEADER1,
 
1073
                  " HTTP/%d.%d %3d",
 
1074
                  &httpversion_major,
 
1075
                  &conn->httpversion,
 
1076
                  &k->httpcode);
 
1077
      if(nc==3) {
 
1078
        conn->httpversion += 10 * httpversion_major;
 
1079
      }
 
1080
      else {
 
1081
        /* this is the real world, not a Nirvana
 
1082
           NCSA 1.5.x returns this crap when asked for HTTP/1.1
 
1083
        */
 
1084
        nc=sscanf(HEADER1, " HTTP %3d", &k->httpcode);
 
1085
        conn->httpversion = 10;
 
1086
 
 
1087
        /* If user has set option HTTP200ALIASES,
 
1088
           compare header line against list of aliases
 
1089
        */
 
1090
        if(!nc) {
 
1091
          if(checkhttpprefix(data, k->p)) {
 
1092
            nc = 1;
 
1093
            k->httpcode = 200;
 
1094
            conn->httpversion = 10;
 
1095
          }
 
1096
        }
 
1097
      }
 
1098
 
 
1099
      if(nc) {
 
1100
        data->info.httpcode = k->httpcode;
 
1101
        data->info.httpversion = conn->httpversion;
 
1102
        if (!data->state.httpversion ||
 
1103
            data->state.httpversion > conn->httpversion)
 
1104
          /* store the lowest server version we encounter */
 
1105
          data->state.httpversion = conn->httpversion;
 
1106
 
 
1107
        /*
 
1108
         * This code executes as part of processing the header.  As a
 
1109
         * result, it's not totally clear how to interpret the
 
1110
         * response code yet as that depends on what other headers may
 
1111
         * be present.  401 and 407 may be errors, but may be OK
 
1112
         * depending on how authentication is working.  Other codes
 
1113
         * are definitely errors, so give up here.
 
1114
         */
 
1115
        if(data->set.http_fail_on_error && (k->httpcode >= 400) &&
 
1116
           ((k->httpcode != 401) || !conn->bits.user_passwd) &&
 
1117
           ((k->httpcode != 407) || !conn->bits.proxy_user_passwd) ) {
 
1118
 
 
1119
          if(data->state.resume_from &&
 
1120
             (data->set.httpreq==HTTPREQ_GET) &&
 
1121
             (k->httpcode == 416)) {
 
1122
            /* "Requested Range Not Satisfiable", just proceed and
 
1123
               pretend this is no error */
 
1124
          }
 
1125
          else {
 
1126
            /* serious error, go home! */
 
1127
            failf (data, "The requested URL returned error: %d",
 
1128
                   k->httpcode);
 
1129
            return CURLE_HTTP_RETURNED_ERROR;
 
1130
          }
 
1131
        }
 
1132
 
 
1133
        if(conn->httpversion == 10) {
 
1134
          /* Default action for HTTP/1.0 must be to close, unless
 
1135
             we get one of those fancy headers that tell us the
 
1136
             server keeps it open for us! */
 
1137
          infof(data, "HTTP 1.0, assume close after body\n");
 
1138
          conn->bits.close = TRUE;
 
1139
        }
 
1140
        else if(conn->httpversion >= 11 &&
 
1141
                !conn->bits.close) {
 
1142
          /* If HTTP version is >= 1.1 and connection is persistent
 
1143
             server supports pipelining. */
 
1144
          DEBUGF(infof(data,
 
1145
                       "HTTP 1.1 or later with persistent connection, "
 
1146
                       "pipelining supported\n"));
 
1147
          conn->server_supports_pipelining = TRUE;
 
1148
        }
 
1149
 
 
1150
        switch(k->httpcode) {
 
1151
        case 204:
 
1152
          /* (quote from RFC2616, section 10.2.5): The server has
 
1153
           * fulfilled the request but does not need to return an
 
1154
           * entity-body ... The 204 response MUST NOT include a
 
1155
           * message-body, and thus is always terminated by the first
 
1156
           * empty line after the header fields. */
 
1157
          /* FALLTHROUGH */
 
1158
        case 416: /* Requested Range Not Satisfiable, it has the
 
1159
                     Content-Length: set as the "real" document but no
 
1160
                     actual response is sent. */
 
1161
        case 304:
 
1162
          /* (quote from RFC2616, section 10.3.5): The 304 response
 
1163
           * MUST NOT contain a message-body, and thus is always
 
1164
           * terminated by the first empty line after the header
 
1165
           * fields.  */
 
1166
          if(data->set.timecondition)
 
1167
            data->info.timecond = TRUE;
 
1168
          k->size=0;
 
1169
          k->maxdownload=0;
 
1170
          k->ignorecl = TRUE; /* ignore Content-Length headers */
 
1171
          break;
 
1172
        default:
 
1173
          /* nothing */
 
1174
          break;
 
1175
        }
 
1176
      }
 
1177
      else {
 
1178
        k->header = FALSE;   /* this is not a header line */
 
1179
        break;
 
1180
      }
 
1181
    }
 
1182
 
 
1183
#ifdef CURL_DOES_CONVERSIONS
 
1184
    /* convert from the network encoding */
 
1185
    result = Curl_convert_from_network(data, k->p, strlen(k->p));
 
1186
    if(CURLE_OK != result) {
 
1187
      return(result);
 
1188
    }
 
1189
    /* Curl_convert_from_network calls failf if unsuccessful */
 
1190
#endif /* CURL_DOES_CONVERSIONS */
 
1191
 
 
1192
    /* Check for Content-Length: header lines to get size. Ignore
 
1193
       the header completely if we get a 416 response as then we're
 
1194
       resuming a document that we don't get, and this header contains
 
1195
       info about the true size of the document we didn't get now. */
 
1196
    if(!k->ignorecl && !data->set.ignorecl &&
 
1197
       checkprefix("Content-Length:", k->p)) {
 
1198
      curl_off_t contentlength = curlx_strtoofft(k->p+15, NULL, 10);
 
1199
      if(data->set.max_filesize &&
 
1200
         contentlength > data->set.max_filesize) {
 
1201
        failf(data, "Maximum file size exceeded");
 
1202
        return CURLE_FILESIZE_EXCEEDED;
 
1203
      }
 
1204
      if(contentlength >= 0) {
 
1205
        k->size = contentlength;
 
1206
        k->maxdownload = k->size;
 
1207
        /* we set the progress download size already at this point
 
1208
           just to make it easier for apps/callbacks to extract this
 
1209
           info as soon as possible */
 
1210
        Curl_pgrsSetDownloadSize(data, k->size);
 
1211
      }
 
1212
      else {
 
1213
        /* Negative Content-Length is really odd, and we know it
 
1214
           happens for example when older Apache servers send large
 
1215
           files */
 
1216
        conn->bits.close = TRUE;
 
1217
        infof(data, "Negative content-length: %" FORMAT_OFF_T
 
1218
              ", closing after transfer\n", contentlength);
 
1219
      }
 
1220
    }
 
1221
    /* check for Content-Type: header lines to get the MIME-type */
 
1222
    else if(checkprefix("Content-Type:", k->p)) {
 
1223
      char *contenttype = Curl_copy_header_value(k->p);
 
1224
      if (!contenttype)
 
1225
        return CURLE_OUT_OF_MEMORY;
 
1226
      if (!*contenttype)
 
1227
        /* ignore empty data */
 
1228
        free(contenttype);
 
1229
      else {
 
1230
        Curl_safefree(data->info.contenttype);
 
1231
        data->info.contenttype = contenttype;
 
1232
      }
 
1233
    }
 
1234
    else if((conn->httpversion == 10) &&
 
1235
            conn->bits.httpproxy &&
 
1236
            Curl_compareheader(k->p,
 
1237
                               "Proxy-Connection:", "keep-alive")) {
 
1238
      /*
 
1239
       * When a HTTP/1.0 reply comes when using a proxy, the
 
1240
       * 'Proxy-Connection: keep-alive' line tells us the
 
1241
       * connection will be kept alive for our pleasure.
 
1242
       * Default action for 1.0 is to close.
 
1243
       */
 
1244
      conn->bits.close = FALSE; /* don't close when done */
 
1245
      infof(data, "HTTP/1.0 proxy connection set to keep alive!\n");
 
1246
    }
 
1247
    else if((conn->httpversion == 11) &&
 
1248
            conn->bits.httpproxy &&
 
1249
            Curl_compareheader(k->p,
 
1250
                               "Proxy-Connection:", "close")) {
 
1251
      /*
 
1252
       * We get a HTTP/1.1 response from a proxy and it says it'll
 
1253
       * close down after this transfer.
 
1254
       */
 
1255
      conn->bits.close = TRUE; /* close when done */
 
1256
      infof(data, "HTTP/1.1 proxy connection set close!\n");
 
1257
    }
 
1258
    else if((conn->httpversion == 10) &&
 
1259
            Curl_compareheader(k->p, "Connection:", "keep-alive")) {
 
1260
      /*
 
1261
       * A HTTP/1.0 reply with the 'Connection: keep-alive' line
 
1262
       * tells us the connection will be kept alive for our
 
1263
       * pleasure.  Default action for 1.0 is to close.
 
1264
       *
 
1265
       * [RFC2068, section 19.7.1] */
 
1266
      conn->bits.close = FALSE; /* don't close when done */
 
1267
      infof(data, "HTTP/1.0 connection set to keep alive!\n");
 
1268
    }
 
1269
    else if(Curl_compareheader(k->p, "Connection:", "close")) {
 
1270
      /*
 
1271
       * [RFC 2616, section 8.1.2.1]
 
1272
       * "Connection: close" is HTTP/1.1 language and means that
 
1273
       * the connection will close when this request has been
 
1274
       * served.
 
1275
       */
 
1276
      conn->bits.close = TRUE; /* close when done */
 
1277
    }
 
1278
    else if(Curl_compareheader(k->p,
 
1279
                               "Transfer-Encoding:", "chunked")) {
 
1280
      /*
 
1281
       * [RFC 2616, section 3.6.1] A 'chunked' transfer encoding
 
1282
       * means that the server will send a series of "chunks". Each
 
1283
       * chunk starts with line with info (including size of the
 
1284
       * coming block) (terminated with CRLF), then a block of data
 
1285
       * with the previously mentioned size. There can be any amount
 
1286
       * of chunks, and a chunk-data set to zero signals the
 
1287
       * end-of-chunks. */
 
1288
      k->chunk = TRUE; /* chunks coming our way */
 
1289
 
 
1290
      /* init our chunky engine */
 
1291
      Curl_httpchunk_init(conn);
 
1292
    }
 
1293
 
 
1294
    else if(checkprefix("Trailer:", k->p) ||
 
1295
            checkprefix("Trailers:", k->p)) {
 
1296
      /*
 
1297
       * This test helps Curl_httpchunk_read() to determine to look
 
1298
       * for well formed trailers after the zero chunksize record. In
 
1299
       * this case a CRLF is required after the zero chunksize record
 
1300
       * when no trailers are sent, or after the last trailer record.
 
1301
       *
 
1302
       * It seems both Trailer: and Trailers: occur in the wild.
 
1303
       */
 
1304
      k->trailerhdrpresent = TRUE;
 
1305
    }
 
1306
 
 
1307
    else if(checkprefix("Content-Encoding:", k->p) &&
 
1308
            data->set.str[STRING_ENCODING]) {
 
1309
      /*
 
1310
       * Process Content-Encoding. Look for the values: identity,
 
1311
       * gzip, deflate, compress, x-gzip and x-compress. x-gzip and
 
1312
       * x-compress are the same as gzip and compress. (Sec 3.5 RFC
 
1313
       * 2616). zlib cannot handle compress.  However, errors are
 
1314
       * handled further down when the response body is processed
 
1315
       */
 
1316
      char *start;
 
1317
 
 
1318
      /* Find the first non-space letter */
 
1319
      start = k->p + 17;
 
1320
      while(*start && ISSPACE(*start))
 
1321
        start++;
 
1322
 
 
1323
      /* Record the content-encoding for later use */
 
1324
      if(checkprefix("identity", start))
 
1325
        k->content_encoding = IDENTITY;
 
1326
      else if(checkprefix("deflate", start))
 
1327
        k->content_encoding = DEFLATE;
 
1328
      else if(checkprefix("gzip", start)
 
1329
              || checkprefix("x-gzip", start))
 
1330
        k->content_encoding = GZIP;
 
1331
      else if(checkprefix("compress", start)
 
1332
              || checkprefix("x-compress", start))
 
1333
        k->content_encoding = COMPRESS;
 
1334
    }
 
1335
    else if(checkprefix("Content-Range:", k->p)) {
 
1336
      /* Content-Range: bytes [num]-
 
1337
         Content-Range: bytes: [num]-
 
1338
         Content-Range: [num]-
 
1339
 
 
1340
         The second format was added since Sun's webserver
 
1341
         JavaWebServer/1.1.1 obviously sends the header this way!
 
1342
         The third added since some servers use that!
 
1343
      */
 
1344
 
 
1345
      char *ptr = k->p + 14;
 
1346
 
 
1347
      /* Move forward until first digit */
 
1348
      while(*ptr && !ISDIGIT(*ptr))
 
1349
        ptr++;
 
1350
 
 
1351
      k->offset = curlx_strtoofft(ptr, NULL, 10);
 
1352
 
 
1353
      if(data->state.resume_from == k->offset)
 
1354
        /* we asked for a resume and we got it */
 
1355
        k->content_range = TRUE;
 
1356
    }
 
1357
#if !defined(CURL_DISABLE_COOKIES)
 
1358
    else if(data->cookies &&
 
1359
            checkprefix("Set-Cookie:", k->p)) {
 
1360
      Curl_share_lock(data, CURL_LOCK_DATA_COOKIE,
 
1361
                      CURL_LOCK_ACCESS_SINGLE);
 
1362
      Curl_cookie_add(data,
 
1363
                      data->cookies, TRUE, k->p+11,
 
1364
                      /* If there is a custom-set Host: name, use it
 
1365
                         here, or else use real peer host name. */
 
1366
                      conn->allocptr.cookiehost?
 
1367
                      conn->allocptr.cookiehost:conn->host.name,
 
1368
                      data->state.path);
 
1369
      Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
 
1370
    }
 
1371
#endif
 
1372
    else if(checkprefix("Last-Modified:", k->p) &&
 
1373
            (data->set.timecondition || data->set.get_filetime) ) {
 
1374
      time_t secs=time(NULL);
 
1375
      k->timeofdoc = curl_getdate(k->p+strlen("Last-Modified:"),
 
1376
                                  &secs);
 
1377
      if(data->set.get_filetime)
 
1378
        data->info.filetime = (long)k->timeofdoc;
 
1379
    }
 
1380
    else if((checkprefix("WWW-Authenticate:", k->p) &&
 
1381
             (401 == k->httpcode)) ||
 
1382
            (checkprefix("Proxy-authenticate:", k->p) &&
 
1383
             (407 == k->httpcode))) {
 
1384
      result = Curl_http_input_auth(conn, k->httpcode, k->p);
 
1385
      if(result)
 
1386
        return result;
 
1387
    }
 
1388
    else if((k->httpcode >= 300 && k->httpcode < 400) &&
 
1389
            checkprefix("Location:", k->p)) {
 
1390
      /* this is the URL that the server advises us to use instead */
 
1391
      char *location = Curl_copy_header_value(k->p);
 
1392
      if (!location)
 
1393
        return CURLE_OUT_OF_MEMORY;
 
1394
      if (!*location)
 
1395
        /* ignore empty data */
 
1396
        free(location);
 
1397
      else {
 
1398
        DEBUGASSERT(!data->req.location);
 
1399
        data->req.location = location;
 
1400
 
 
1401
        if(data->set.http_follow_location) {
 
1402
          DEBUGASSERT(!data->req.newurl);
 
1403
          data->req.newurl = strdup(data->req.location); /* clone */
 
1404
          if(!data->req.newurl)
 
1405
            return CURLE_OUT_OF_MEMORY;
 
1406
 
 
1407
          /* some cases of POST and PUT etc needs to rewind the data
 
1408
             stream at this point */
 
1409
          result = Curl_http_perhapsrewind(conn);
 
1410
          if(result)
 
1411
            return result;
 
1412
        }
 
1413
      }
 
1414
    }
 
1415
 
 
1416
    /*
 
1417
     * End of header-checks. Write them to the client.
 
1418
     */
 
1419
 
 
1420
    writetype = CLIENTWRITE_HEADER;
 
1421
    if(data->set.include_header)
 
1422
      writetype |= CLIENTWRITE_BODY;
 
1423
 
 
1424
    if(data->set.verbose)
 
1425
      Curl_debug(data, CURLINFO_HEADER_IN,
 
1426
                 k->p, (size_t)k->hbuflen, conn);
 
1427
 
 
1428
    result = Curl_client_write(conn, writetype, k->p, k->hbuflen);
 
1429
    if(result)
 
1430
      return result;
 
1431
 
 
1432
    data->info.header_size += (long)k->hbuflen;
 
1433
    data->req.headerbytecount += (long)k->hbuflen;
 
1434
 
 
1435
    /* reset hbufp pointer && hbuflen */
 
1436
    k->hbufp = data->state.headerbuff;
 
1437
    k->hbuflen = 0;
 
1438
  }
 
1439
  while(!*stop_reading && *k->str); /* header line within buffer */
 
1440
 
 
1441
  /* We might have reached the end of the header part here, but
 
1442
     there might be a non-header part left in the end of the read
 
1443
     buffer. */
 
1444
 
 
1445
  return CURLE_OK;
 
1446
}
 
1447
#endif   /* CURL_DISABLE_HTTP */
 
1448
 
 
1449
/*
 
1450
 * Send data to upload to the server, when the socket is writable.
 
1451
 */
 
1452
static CURLcode readwrite_upload(struct SessionHandle *data,
 
1453
                                 struct connectdata *conn,
 
1454
                                 struct SingleRequest *k,
 
1455
                                 int *didwhat)
 
1456
{
 
1457
  ssize_t i, si;
 
1458
  ssize_t bytes_written;
 
1459
  CURLcode result;
 
1460
  ssize_t nread; /* number of bytes read */
 
1461
  bool sending_http_headers = FALSE;
 
1462
 
 
1463
  if((k->bytecount == 0) && (k->writebytecount == 0))
 
1464
    Curl_pgrsTime(data, TIMER_STARTTRANSFER);
 
1465
 
 
1466
  *didwhat |= KEEP_SEND;
 
1467
 
 
1468
  /*
 
1469
   * We loop here to do the READ and SEND loop until we run out of
 
1470
   * data to send or until we get EWOULDBLOCK back
 
1471
   */
 
1472
  do {
 
1473
 
 
1474
    /* only read more data if there's no upload data already
 
1475
       present in the upload buffer */
 
1476
    if(0 == data->req.upload_present) {
 
1477
      /* init the "upload from here" pointer */
 
1478
      data->req.upload_fromhere = k->uploadbuf;
 
1479
 
 
1480
      if(!k->upload_done) {
 
1481
        /* HTTP pollution, this should be written nicer to become more
 
1482
           protocol agnostic. */
 
1483
        int fillcount;
 
1484
 
 
1485
        if((k->exp100 == EXP100_SENDING_REQUEST) &&
 
1486
           (data->state.proto.http->sending == HTTPSEND_BODY)) {
 
1487
          /* If this call is to send body data, we must take some action:
 
1488
             We have sent off the full HTTP 1.1 request, and we shall now
 
1489
             go into the Expect: 100 state and await such a header */
 
1490
          k->exp100 = EXP100_AWAITING_CONTINUE; /* wait for the header */
 
1491
          k->keepon &= ~KEEP_SEND;         /* disable writing */
 
1492
          k->start100 = Curl_tvnow();       /* timeout count starts now */
 
1493
          *didwhat &= ~KEEP_SEND;  /* we didn't write anything actually */
 
1494
          break;
 
1495
        }
 
1496
 
 
1497
        if(conn->protocol&PROT_HTTP) {
 
1498
          if(data->state.proto.http->sending == HTTPSEND_REQUEST)
 
1499
            /* We're sending the HTTP request headers, not the data.
 
1500
               Remember that so we don't change the line endings. */
 
1501
            sending_http_headers = TRUE;
 
1502
          else
 
1503
            sending_http_headers = FALSE;
 
1504
        }
 
1505
 
 
1506
        result = Curl_fillreadbuffer(conn, BUFSIZE, &fillcount);
 
1507
        if(result)
 
1508
          return result;
 
1509
 
 
1510
        nread = (ssize_t)fillcount;
 
1511
      }
 
1512
      else
 
1513
        nread = 0; /* we're done uploading/reading */
 
1514
 
 
1515
      if(!nread && (k->keepon & KEEP_SEND_PAUSE)) {
 
1516
        /* this is a paused transfer */
 
1517
        break;
 
1518
      }
 
1519
      else if(nread<=0) {
 
1520
        /* done */
 
1521
        k->keepon &= ~KEEP_SEND; /* we're done writing */
 
1522
 
 
1523
        if(conn->bits.rewindaftersend) {
 
1524
          result = Curl_readrewind(conn);
 
1525
          if(result)
 
1526
            return result;
 
1527
        }
 
1528
        break;
 
1529
      }
 
1530
 
 
1531
      /* store number of bytes available for upload */
 
1532
      data->req.upload_present = nread;
 
1533
 
 
1534
      /* convert LF to CRLF if so asked */
 
1535
      if((!sending_http_headers) &&
 
1536
#ifdef CURL_DO_LINEEND_CONV
 
1537
        /* always convert if we're FTPing in ASCII mode */
 
1538
         ((data->set.crlf) || (data->set.prefer_ascii))) {
 
1539
#else
 
1540
         (data->set.crlf)) {
 
1541
#endif
 
1542
        if(data->state.scratch == NULL)
 
1543
          data->state.scratch = malloc(2*BUFSIZE);
 
1544
        if(data->state.scratch == NULL) {
 
1545
          failf (data, "Failed to alloc scratch buffer!");
 
1546
          return CURLE_OUT_OF_MEMORY;
 
1547
        }
 
1548
        /*
 
1549
         * ASCII/EBCDIC Note: This is presumably a text (not binary)
 
1550
         * transfer so the data should already be in ASCII.
 
1551
         * That means the hex values for ASCII CR (0x0d) & LF (0x0a)
 
1552
         * must be used instead of the escape sequences \r & \n.
 
1553
         */
 
1554
        for(i = 0, si = 0; i < nread; i++, si++) {
 
1555
          if(data->req.upload_fromhere[i] == 0x0a) {
 
1556
            data->state.scratch[si++] = 0x0d;
 
1557
            data->state.scratch[si] = 0x0a;
 
1558
            if(!data->set.crlf) {
 
1559
              /* we're here only because FTP is in ASCII mode...
 
1560
                 bump infilesize for the LF we just added */
 
1561
              data->set.infilesize++;
 
1562
            }
 
1563
          }
 
1564
          else
 
1565
            data->state.scratch[si] = data->req.upload_fromhere[i];
 
1566
        }
 
1567
        if(si != nread) {
 
1568
          /* only perform the special operation if we really did replace
 
1569
             anything */
 
1570
          nread = si;
 
1571
 
 
1572
          /* upload from the new (replaced) buffer instead */
 
1573
          data->req.upload_fromhere = data->state.scratch;
 
1574
 
 
1575
          /* set the new amount too */
 
1576
          data->req.upload_present = nread;
 
1577
        }
 
1578
      }
 
1579
    } /* if 0 == data->req.upload_present */
 
1580
    else {
 
1581
      /* We have a partial buffer left from a previous "round". Use
 
1582
         that instead of reading more data */
 
1583
    }
 
1584
 
 
1585
    /* write to socket (send away data) */
 
1586
    result = Curl_write(conn,
 
1587
                        conn->writesockfd,     /* socket to send to */
 
1588
                        data->req.upload_fromhere, /* buffer pointer */
 
1589
                        data->req.upload_present,  /* buffer size */
 
1590
                        &bytes_written);       /* actually send away */
 
1591
 
 
1592
    if(result)
 
1593
      return result;
 
1594
 
 
1595
    if(data->set.verbose)
 
1596
      /* show the data before we change the pointer upload_fromhere */
 
1597
      Curl_debug(data, CURLINFO_DATA_OUT, data->req.upload_fromhere,
 
1598
                 (size_t)bytes_written, conn);
 
1599
 
 
1600
    if(data->req.upload_present != bytes_written) {
 
1601
      /* we only wrote a part of the buffer (if anything), deal with it! */
 
1602
 
 
1603
      /* store the amount of bytes left in the buffer to write */
 
1604
      data->req.upload_present -= bytes_written;
 
1605
 
 
1606
      /* advance the pointer where to find the buffer when the next send
 
1607
         is to happen */
 
1608
      data->req.upload_fromhere += bytes_written;
 
1609
    }
 
1610
    else {
 
1611
      /* we've uploaded that buffer now */
 
1612
      data->req.upload_fromhere = k->uploadbuf;
 
1613
      data->req.upload_present = 0; /* no more bytes left */
 
1614
 
 
1615
      if(k->upload_done) {
 
1616
        /* switch off writing, we're done! */
 
1617
        k->keepon &= ~KEEP_SEND; /* we're done writing */
 
1618
      }
 
1619
    }
 
1620
 
 
1621
    k->writebytecount += bytes_written;
 
1622
    Curl_pgrsSetUploadCounter(data, k->writebytecount);
 
1623
 
 
1624
  } while(0); /* just to break out from! */
 
1625
 
 
1626
  return CURLE_OK;
 
1627
}
 
1628
 
333
1629
/*
334
1630
 * Curl_readwrite() is the low-level function to be called when data is to
335
1631
 * be read and written to/from the connection.
340
1636
  struct SessionHandle *data = conn->data;
341
1637
  struct SingleRequest *k = &data->req;
342
1638
  CURLcode result;
343
 
  ssize_t nread; /* number of bytes read */
344
1639
  int didwhat=0;
345
1640
 
346
1641
  curl_socket_t fd_read;
347
1642
  curl_socket_t fd_write;
348
 
  curl_off_t contentlength;
349
1643
  int select_res = conn->cselect_bits;
350
1644
 
351
1645
  conn->cselect_bits = 0;
353
1647
  /* only use the proper socket if the *_HOLD bit is not set simultaneously as
354
1648
     then we are in rate limiting state in that transfer direction */
355
1649
 
356
 
  if((k->keepon & KEEP_READBITS) == KEEP_READ) {
 
1650
  if((k->keepon & KEEP_RECVBITS) == KEEP_RECV) {
357
1651
    fd_read = conn->sockfd;
358
1652
#if defined(USE_LIBSSH2)
359
1653
    if(conn->protocol & (PROT_SCP|PROT_SFTP))
362
1656
  } else
363
1657
    fd_read = CURL_SOCKET_BAD;
364
1658
 
365
 
  if((k->keepon & KEEP_WRITEBITS) == KEEP_WRITE)
 
1659
  if((k->keepon & KEEP_SENDBITS) == KEEP_SEND)
366
1660
    fd_write = conn->writesockfd;
367
1661
  else
368
1662
    fd_write = CURL_SOCKET_BAD;
380
1674
  /* We go ahead and do a read if we have a readable socket or if
381
1675
     the stream was rewound (in which case we have data in a
382
1676
     buffer) */
383
 
  if((k->keepon & KEEP_READ) &&
 
1677
  if((k->keepon & KEEP_RECV) &&
384
1678
     ((select_res & CURL_CSELECT_IN) || conn->bits.stream_was_rewound)) {
385
 
    /* read */
386
 
    bool is_empty_data = FALSE;
387
 
 
388
 
    /* This is where we loop until we have read everything there is to
389
 
       read or we get a EWOULDBLOCK */
390
 
    do {
391
 
      size_t buffersize = data->set.buffer_size?
392
 
        data->set.buffer_size : BUFSIZE;
393
 
      size_t bytestoread = buffersize;
394
 
      int readrc;
395
 
 
396
 
      if(k->size != -1 && !k->header) {
397
 
        /* make sure we don't read "too much" if we can help it since we
398
 
           might be pipelining and then someone else might want to read what
399
 
           follows! */
400
 
        curl_off_t totalleft = k->size - k->bytecount;
401
 
        if(totalleft < (curl_off_t)bytestoread)
402
 
          bytestoread = (size_t)totalleft;
403
 
      }
404
 
 
405
 
      if(bytestoread) {
406
 
        /* receive data from the network! */
407
 
        readrc = Curl_read(conn, conn->sockfd, k->buf, bytestoread, &nread);
408
 
 
409
 
        /* subzero, this would've blocked */
410
 
        if(0 > readrc)
411
 
          break; /* get out of loop */
412
 
 
413
 
        /* get the CURLcode from the int */
414
 
        result = (CURLcode)readrc;
415
 
 
416
 
        if(result>0)
417
 
          return result;
418
 
      }
419
 
      else {
420
 
        /* read nothing but since we wanted nothing we consider this an OK
421
 
           situation to proceed from */
422
 
        nread = 0;
423
 
        result = CURLE_OK;
424
 
      }
425
 
 
426
 
      if((k->bytecount == 0) && (k->writebytecount == 0)) {
427
 
        Curl_pgrsTime(data, TIMER_STARTTRANSFER);
428
 
        if(k->exp100 > EXP100_SEND_DATA)
429
 
          /* set time stamp to compare with when waiting for the 100 */
430
 
          k->start100 = Curl_tvnow();
431
 
      }
432
 
 
433
 
      didwhat |= KEEP_READ;
434
 
      /* indicates data of zero size, i.e. empty file */
435
 
      is_empty_data = (bool)((nread == 0) && (k->bodywrites == 0));
436
 
 
437
 
      /* NULL terminate, allowing string ops to be used */
438
 
      if(0 < nread || is_empty_data) {
439
 
        k->buf[nread] = 0;
440
 
      }
441
 
      else if(0 >= nread) {
442
 
        /* if we receive 0 or less here, the server closed the connection
443
 
           and we bail out from this! */
444
 
        DEBUGF(infof(data, "nread <= 0, server closed connection, bailing\n"));
445
 
        k->keepon &= ~KEEP_READ;
446
 
        break;
447
 
      }
448
 
 
449
 
      /* Default buffer to use when we write the buffer, it may be changed
450
 
         in the flow below before the actual storing is done. */
451
 
      k->str = k->buf;
452
 
 
453
 
      /* Since this is a two-state thing, we check if we are parsing
454
 
         headers at the moment or not. */
455
 
      if(k->header) {
456
 
        /* we are in parse-the-header-mode */
457
 
        bool stop_reading = FALSE;
458
 
 
459
 
        /* header line within buffer loop */
460
 
        do {
461
 
          size_t hbufp_index;
462
 
          size_t rest_length;
463
 
          size_t full_length;
464
 
          int writetype;
465
 
 
466
 
          /* str_start is start of line within buf */
467
 
          k->str_start = k->str;
468
 
 
469
 
          /* data is in network encoding so use 0x0a instead of '\n' */
470
 
          k->end_ptr = memchr(k->str_start, 0x0a, nread);
471
 
 
472
 
          if(!k->end_ptr) {
473
 
            /* Not a complete header line within buffer, append the data to
474
 
               the end of the headerbuff. */
475
 
 
476
 
            if(k->hbuflen + nread >= data->state.headersize) {
477
 
              /* We enlarge the header buffer as it is too small */
478
 
              char *newbuff;
479
 
              size_t newsize=CURLMAX((k->hbuflen+nread)*3/2,
480
 
                                     data->state.headersize*2);
481
 
              hbufp_index = k->hbufp - data->state.headerbuff;
482
 
              newbuff = (char *)realloc(data->state.headerbuff, newsize);
483
 
              if(!newbuff) {
484
 
                failf (data, "Failed to alloc memory for big header!");
485
 
                return CURLE_OUT_OF_MEMORY;
486
 
              }
487
 
              data->state.headersize=newsize;
488
 
              data->state.headerbuff = newbuff;
489
 
              k->hbufp = data->state.headerbuff + hbufp_index;
490
 
            }
491
 
            memcpy(k->hbufp, k->str, nread);
492
 
            k->hbufp += nread;
493
 
            k->hbuflen += nread;
494
 
            if(!k->headerline && (k->hbuflen>5)) {
495
 
              /* make a first check that this looks like a HTTP header */
496
 
              if(!checkhttpprefix(data, data->state.headerbuff)) {
497
 
                /* this is not the beginning of a HTTP first header line */
498
 
                k->header = FALSE;
499
 
                k->badheader = HEADER_ALLBAD;
500
 
                break;
501
 
              }
502
 
            }
503
 
 
504
 
            break; /* read more and try again */
505
 
          }
506
 
 
507
 
          /* decrease the size of the remaining (supposed) header line */
508
 
          rest_length = (k->end_ptr - k->str)+1;
509
 
          nread -= (ssize_t)rest_length;
510
 
 
511
 
          k->str = k->end_ptr + 1; /* move past new line */
512
 
 
513
 
          full_length = k->str - k->str_start;
514
 
 
515
 
          /*
516
 
           * We're about to copy a chunk of data to the end of the
517
 
           * already received header. We make sure that the full string
518
 
           * fit in the allocated header buffer, or else we enlarge
519
 
           * it.
520
 
           */
521
 
          if(k->hbuflen + full_length >=
522
 
             data->state.headersize) {
523
 
            char *newbuff;
524
 
            size_t newsize=CURLMAX((k->hbuflen+full_length)*3/2,
525
 
                                   data->state.headersize*2);
526
 
            hbufp_index = k->hbufp - data->state.headerbuff;
527
 
            newbuff = (char *)realloc(data->state.headerbuff, newsize);
528
 
            if(!newbuff) {
529
 
              failf (data, "Failed to alloc memory for big header!");
530
 
              return CURLE_OUT_OF_MEMORY;
531
 
            }
532
 
            data->state.headersize= newsize;
533
 
            data->state.headerbuff = newbuff;
534
 
            k->hbufp = data->state.headerbuff + hbufp_index;
535
 
          }
536
 
 
537
 
          /* copy to end of line */
538
 
          memcpy(k->hbufp, k->str_start, full_length);
539
 
          k->hbufp += full_length;
540
 
          k->hbuflen += full_length;
541
 
          *k->hbufp = 0;
542
 
          k->end_ptr = k->hbufp;
543
 
 
544
 
          k->p = data->state.headerbuff;
545
 
 
546
 
          /****
547
 
           * We now have a FULL header line that p points to
548
 
           *****/
549
 
 
550
 
          if(!k->headerline) {
551
 
            /* the first read header */
552
 
            if((k->hbuflen>5) &&
553
 
               !checkhttpprefix(data, data->state.headerbuff)) {
554
 
              /* this is not the beginning of a HTTP first header line */
555
 
              k->header = FALSE;
556
 
              if(nread)
557
 
                /* since there's more, this is a partial bad header */
558
 
                k->badheader = HEADER_PARTHEADER;
559
 
              else {
560
 
                /* this was all we read so its all a bad header */
561
 
                k->badheader = HEADER_ALLBAD;
562
 
                nread = (ssize_t)rest_length;
563
 
              }
564
 
              break;
565
 
            }
566
 
          }
567
 
 
568
 
          /* headers are in network encoding so
569
 
             use 0x0a and 0x0d instead of '\n' and '\r' */
570
 
          if((0x0a == *k->p) || (0x0d == *k->p)) {
571
 
            size_t headerlen;
572
 
            /* Zero-length header line means end of headers! */
573
 
 
574
 
#ifdef CURL_DOES_CONVERSIONS
575
 
            if(0x0d == *k->p) {
576
 
              *k->p = '\r'; /* replace with CR in host encoding */
577
 
              k->p++;       /* pass the CR byte */
578
 
            }
579
 
            if(0x0a == *k->p) {
580
 
              *k->p = '\n'; /* replace with LF in host encoding */
581
 
              k->p++;       /* pass the LF byte */
582
 
            }
583
 
#else
584
 
            if('\r' == *k->p)
585
 
              k->p++; /* pass the \r byte */
586
 
            if('\n' == *k->p)
587
 
              k->p++; /* pass the \n byte */
588
 
#endif /* CURL_DOES_CONVERSIONS */
589
 
 
590
 
            if(100 == k->httpcode) {
591
 
              /*
592
 
               * We have made a HTTP PUT or POST and this is 1.1-lingo
593
 
               * that tells us that the server is OK with this and ready
594
 
               * to receive the data.
595
 
               * However, we'll get more headers now so we must get
596
 
               * back into the header-parsing state!
597
 
               */
598
 
              k->header = TRUE;
599
 
              k->headerline = 0; /* restart the header line counter */
600
 
 
601
 
              /* if we did wait for this do enable write now! */
602
 
              if(k->exp100) {
603
 
                k->exp100 = EXP100_SEND_DATA;
604
 
                k->keepon |= KEEP_WRITE;
605
 
              }
606
 
            }
607
 
            else {
608
 
              k->header = FALSE; /* no more header to parse! */
609
 
 
610
 
              if((k->size == -1) && !k->chunk && !conn->bits.close &&
611
 
                 (k->httpversion >= 11) ) {
612
 
                /* On HTTP 1.1, when connection is not to get closed, but no
613
 
                   Content-Length nor Content-Encoding chunked have been
614
 
                   received, according to RFC2616 section 4.4 point 5, we
615
 
                   assume that the server will close the connection to
616
 
                   signal the end of the document. */
617
 
                infof(data, "no chunk, no close, no size. Assume close to "
618
 
                      "signal end\n");
619
 
                conn->bits.close = TRUE;
620
 
              }
621
 
            }
622
 
 
623
 
            if(417 == k->httpcode) {
624
 
              /*
625
 
               * we got: "417 Expectation Failed" this means:
626
 
               * we have made a HTTP call and our Expect Header
627
 
               * seems to cause a problem => abort the write operations
628
 
               * (or prevent them from starting).
629
 
               */
630
 
              k->exp100 = EXP100_FAILED;
631
 
              k->keepon &= ~KEEP_WRITE;
632
 
            }
633
 
 
634
 
#ifndef CURL_DISABLE_HTTP
635
 
            /*
636
 
             * When all the headers have been parsed, see if we should give
637
 
             * up and return an error.
638
 
             */
639
 
            if(Curl_http_should_fail(conn)) {
640
 
              failf (data, "The requested URL returned error: %d",
641
 
                     k->httpcode);
642
 
              return CURLE_HTTP_RETURNED_ERROR;
643
 
            }
644
 
#endif   /* CURL_DISABLE_HTTP */
645
 
 
646
 
            /* now, only output this if the header AND body are requested:
647
 
             */
648
 
            writetype = CLIENTWRITE_HEADER;
649
 
            if(data->set.include_header)
650
 
              writetype |= CLIENTWRITE_BODY;
651
 
 
652
 
            headerlen = k->p - data->state.headerbuff;
653
 
 
654
 
            result = Curl_client_write(conn, writetype,
655
 
                                       data->state.headerbuff,
656
 
                                       headerlen);
657
 
            if(result)
658
 
              return result;
659
 
 
660
 
            data->info.header_size += (long)headerlen;
661
 
            data->req.headerbytecount += (long)headerlen;
662
 
 
663
 
            data->req.deductheadercount =
664
 
              (100 == k->httpcode)?data->req.headerbytecount:0;
665
 
 
666
 
            if(data->state.resume_from &&
667
 
               (data->set.httpreq==HTTPREQ_GET) &&
668
 
               (k->httpcode == 416)) {
669
 
              /* "Requested Range Not Satisfiable" */
670
 
              stop_reading = TRUE;
671
 
            }
672
 
 
673
 
#ifndef CURL_DISABLE_HTTP
674
 
            if(!stop_reading) {
675
 
              /* Curl_http_auth_act() checks what authentication methods
676
 
               * that are available and decides which one (if any) to
677
 
               * use. It will set 'newurl' if an auth metod was picked. */
678
 
              result = Curl_http_auth_act(conn);
679
 
 
680
 
              if(result)
681
 
                return result;
682
 
 
683
 
              if(conn->bits.rewindaftersend) {
684
 
                /* We rewind after a complete send, so thus we continue
685
 
                   sending now */
686
 
                infof(data, "Keep sending data to get tossed away!\n");
687
 
                k->keepon |= KEEP_WRITE;
688
 
              }
689
 
            }
690
 
#endif   /* CURL_DISABLE_HTTP */
691
 
 
692
 
            if(!k->header) {
693
 
              /*
694
 
               * really end-of-headers.
695
 
               *
696
 
               * If we requested a "no body", this is a good time to get
697
 
               * out and return home.
698
 
               */
699
 
              if(data->set.opt_no_body)
700
 
                stop_reading = TRUE;
701
 
              else {
702
 
                /* If we know the expected size of this document, we set the
703
 
                   maximum download size to the size of the expected
704
 
                   document or else, we won't know when to stop reading!
705
 
 
706
 
                   Note that we set the download maximum even if we read a
707
 
                   "Connection: close" header, to make sure that
708
 
                   "Content-Length: 0" still prevents us from attempting to
709
 
                   read the (missing) response-body.
710
 
                */
711
 
                /* According to RFC2616 section 4.4, we MUST ignore
712
 
                   Content-Length: headers if we are now receiving data
713
 
                   using chunked Transfer-Encoding.
714
 
                */
715
 
                if(k->chunk)
716
 
                  k->size=-1;
717
 
 
718
 
              }
719
 
              if(-1 != k->size) {
720
 
                /* We do this operation even if no_body is true, since this
721
 
                   data might be retrieved later with curl_easy_getinfo()
722
 
                   and its CURLINFO_CONTENT_LENGTH_DOWNLOAD option. */
723
 
 
724
 
                Curl_pgrsSetDownloadSize(data, k->size);
725
 
                k->maxdownload = k->size;
726
 
              }
727
 
              /* If max download size is *zero* (nothing) we already
728
 
                 have nothing and can safely return ok now! */
729
 
              if(0 == k->maxdownload)
730
 
                stop_reading = TRUE;
731
 
 
732
 
              if(stop_reading) {
733
 
                /* we make sure that this socket isn't read more now */
734
 
                k->keepon &= ~KEEP_READ;
735
 
              }
736
 
 
737
 
              if(data->set.verbose)
738
 
                Curl_debug(data, CURLINFO_HEADER_IN,
739
 
                           k->str_start, headerlen, conn);
740
 
              break;          /* exit header line loop */
741
 
            }
742
 
 
743
 
            /* We continue reading headers, so reset the line-based
744
 
               header parsing variables hbufp && hbuflen */
745
 
            k->hbufp = data->state.headerbuff;
746
 
            k->hbuflen = 0;
747
 
            continue;
748
 
          }
749
 
 
750
 
          /*
751
 
           * Checks for special headers coming up.
752
 
           */
753
 
 
754
 
          if(!k->headerline++) {
755
 
            /* This is the first header, it MUST be the error code line
756
 
               or else we consider this to be the body right away! */
757
 
            int httpversion_major;
758
 
            int nc;
759
 
#ifdef CURL_DOES_CONVERSIONS
760
 
#define HEADER1 scratch
761
 
#define SCRATCHSIZE 21
762
 
            CURLcode res;
763
 
            char scratch[SCRATCHSIZE+1]; /* "HTTP/major.minor 123" */
764
 
            /* We can't really convert this yet because we
765
 
               don't know if it's the 1st header line or the body.
766
 
               So we do a partial conversion into a scratch area,
767
 
               leaving the data at k->p as-is.
768
 
            */
769
 
            strncpy(&scratch[0], k->p, SCRATCHSIZE);
770
 
            scratch[SCRATCHSIZE] = 0; /* null terminate */
771
 
            res = Curl_convert_from_network(data,
772
 
                                            &scratch[0],
773
 
                                            SCRATCHSIZE);
774
 
            if(CURLE_OK != res) {
775
 
              /* Curl_convert_from_network calls failf if unsuccessful */
776
 
              return res;
777
 
            }
778
 
#else
779
 
#define HEADER1 k->p /* no conversion needed, just use k->p */
780
 
#endif /* CURL_DOES_CONVERSIONS */
781
 
 
782
 
            nc = sscanf(HEADER1,
783
 
                        " HTTP/%d.%d %3d",
784
 
                        &httpversion_major,
785
 
                        &k->httpversion,
786
 
                        &k->httpcode);
787
 
            if(nc==3) {
788
 
              k->httpversion += 10 * httpversion_major;
789
 
            }
790
 
            else {
791
 
              /* this is the real world, not a Nirvana
792
 
                 NCSA 1.5.x returns this crap when asked for HTTP/1.1
793
 
              */
794
 
              nc=sscanf(HEADER1, " HTTP %3d", &k->httpcode);
795
 
              k->httpversion = 10;
796
 
 
797
 
              /* If user has set option HTTP200ALIASES,
798
 
                 compare header line against list of aliases
799
 
              */
800
 
              if(!nc) {
801
 
                if(checkhttpprefix(data, k->p)) {
802
 
                  nc = 1;
803
 
                  k->httpcode = 200;
804
 
                  k->httpversion = 10;
805
 
                }
806
 
              }
807
 
            }
808
 
 
809
 
            if(nc) {
810
 
              data->info.httpcode = k->httpcode;
811
 
              data->info.httpversion = k->httpversion;
812
 
 
813
 
              /*
814
 
               * This code executes as part of processing the header.  As a
815
 
               * result, it's not totally clear how to interpret the
816
 
               * response code yet as that depends on what other headers may
817
 
               * be present.  401 and 407 may be errors, but may be OK
818
 
               * depending on how authentication is working.  Other codes
819
 
               * are definitely errors, so give up here.
820
 
               */
821
 
              if(data->set.http_fail_on_error && (k->httpcode >= 400) &&
822
 
                 ((k->httpcode != 401) || !conn->bits.user_passwd) &&
823
 
                 ((k->httpcode != 407) || !conn->bits.proxy_user_passwd) ) {
824
 
 
825
 
                if(data->state.resume_from &&
826
 
                   (data->set.httpreq==HTTPREQ_GET) &&
827
 
                   (k->httpcode == 416)) {
828
 
                  /* "Requested Range Not Satisfiable", just proceed and
829
 
                     pretend this is no error */
830
 
                }
831
 
                else {
832
 
                  /* serious error, go home! */
833
 
                  failf (data, "The requested URL returned error: %d",
834
 
                         k->httpcode);
835
 
                  return CURLE_HTTP_RETURNED_ERROR;
836
 
                }
837
 
              }
838
 
 
839
 
              if(k->httpversion == 10) {
840
 
                /* Default action for HTTP/1.0 must be to close, unless
841
 
                   we get one of those fancy headers that tell us the
842
 
                   server keeps it open for us! */
843
 
                infof(data, "HTTP 1.0, assume close after body\n");
844
 
                conn->bits.close = TRUE;
845
 
              }
846
 
              else if(k->httpversion >= 11 &&
847
 
                      !conn->bits.close) {
848
 
                /* If HTTP version is >= 1.1 and connection is persistent
849
 
                   server supports pipelining. */
850
 
                DEBUGF(infof(data,
851
 
                             "HTTP 1.1 or later with persistent connection, "
852
 
                             "pipelining supported\n"));
853
 
                conn->server_supports_pipelining = TRUE;
854
 
              }
855
 
 
856
 
              switch(k->httpcode) {
857
 
              case 204:
858
 
                /* (quote from RFC2616, section 10.2.5): The server has
859
 
                 * fulfilled the request but does not need to return an
860
 
                 * entity-body ... The 204 response MUST NOT include a
861
 
                 * message-body, and thus is always terminated by the first
862
 
                 * empty line after the header fields. */
863
 
                /* FALLTHROUGH */
864
 
              case 416: /* Requested Range Not Satisfiable, it has the
865
 
                           Content-Length: set as the "real" document but no
866
 
                           actual response is sent. */
867
 
              case 304:
868
 
                /* (quote from RFC2616, section 10.3.5): The 304 response
869
 
                 * MUST NOT contain a message-body, and thus is always
870
 
                 * terminated by the first empty line after the header
871
 
                 * fields.  */
872
 
                k->size=0;
873
 
                k->maxdownload=0;
874
 
                k->ignorecl = TRUE; /* ignore Content-Length headers */
875
 
                break;
876
 
              default:
877
 
                /* nothing */
878
 
                break;
879
 
              }
880
 
            }
881
 
            else {
882
 
              k->header = FALSE;   /* this is not a header line */
883
 
              break;
884
 
            }
885
 
          }
886
 
 
887
 
#ifdef CURL_DOES_CONVERSIONS
888
 
          /* convert from the network encoding */
889
 
          result = Curl_convert_from_network(data, k->p, strlen(k->p));
890
 
          if(CURLE_OK != result) {
891
 
            return(result);
892
 
          }
893
 
          /* Curl_convert_from_network calls failf if unsuccessful */
894
 
#endif /* CURL_DOES_CONVERSIONS */
895
 
 
896
 
          /* Check for Content-Length: header lines to get size. Ignore
897
 
             the header completely if we get a 416 response as then we're
898
 
             resuming a document that we don't get, and this header contains
899
 
             info about the true size of the document we didn't get now. */
900
 
          if(!k->ignorecl && !data->set.ignorecl &&
901
 
             checkprefix("Content-Length:", k->p)) {
902
 
            contentlength = curlx_strtoofft(k->p+15, NULL, 10);
903
 
            if(data->set.max_filesize &&
904
 
               contentlength > data->set.max_filesize) {
905
 
              failf(data, "Maximum file size exceeded");
906
 
              return CURLE_FILESIZE_EXCEEDED;
907
 
            }
908
 
            if(contentlength >= 0) {
909
 
              k->size = contentlength;
910
 
              k->maxdownload = k->size;
911
 
              /* we set the progress download size already at this point
912
 
                 just to make it easier for apps/callbacks to extract this
913
 
                 info as soon as possible */
914
 
              Curl_pgrsSetDownloadSize(data, k->size);
915
 
            }
916
 
            else {
917
 
              /* Negative Content-Length is really odd, and we know it
918
 
                 happens for example when older Apache servers send large
919
 
                 files */
920
 
              conn->bits.close = TRUE;
921
 
              infof(data, "Negative content-length: %" FORMAT_OFF_T
922
 
                    ", closing after transfer\n", contentlength);
923
 
            }
924
 
          }
925
 
          /* check for Content-Type: header lines to get the mime-type */
926
 
          else if(checkprefix("Content-Type:", k->p)) {
927
 
            char *start;
928
 
            char *end;
929
 
            size_t len;
930
 
 
931
 
            /* Find the first non-space letter */
932
 
            for(start=k->p+13;
933
 
                *start && ISSPACE(*start);
934
 
                start++)
935
 
              ;  /* empty loop */
936
 
 
937
 
            /* data is now in the host encoding so
938
 
               use '\r' and '\n' instead of 0x0d and 0x0a */
939
 
            end = strchr(start, '\r');
940
 
            if(!end)
941
 
              end = strchr(start, '\n');
942
 
 
943
 
            if(end) {
944
 
              /* skip all trailing space letters */
945
 
              for(; ISSPACE(*end) && (end > start); end--)
946
 
                ;  /* empty loop */
947
 
 
948
 
              /* get length of the type */
949
 
              len = end-start+1;
950
 
 
951
 
              /* allocate memory of a cloned copy */
952
 
              Curl_safefree(data->info.contenttype);
953
 
 
954
 
              data->info.contenttype = malloc(len + 1);
955
 
              if(NULL == data->info.contenttype)
956
 
                return CURLE_OUT_OF_MEMORY;
957
 
 
958
 
              /* copy the content-type string */
959
 
              memcpy(data->info.contenttype, start, len);
960
 
              data->info.contenttype[len] = 0; /* zero terminate */
961
 
            }
962
 
          }
963
 
#ifndef CURL_DISABLE_HTTP
964
 
          else if((k->httpversion == 10) &&
965
 
                  conn->bits.httpproxy &&
966
 
                  Curl_compareheader(k->p,
967
 
                                     "Proxy-Connection:", "keep-alive")) {
968
 
            /*
969
 
             * When a HTTP/1.0 reply comes when using a proxy, the
970
 
             * 'Proxy-Connection: keep-alive' line tells us the
971
 
             * connection will be kept alive for our pleasure.
972
 
             * Default action for 1.0 is to close.
973
 
             */
974
 
            conn->bits.close = FALSE; /* don't close when done */
975
 
            infof(data, "HTTP/1.0 proxy connection set to keep alive!\n");
976
 
          }
977
 
          else if((k->httpversion == 11) &&
978
 
                  conn->bits.httpproxy &&
979
 
                  Curl_compareheader(k->p,
980
 
                                     "Proxy-Connection:", "close")) {
981
 
            /*
982
 
             * We get a HTTP/1.1 response from a proxy and it says it'll
983
 
             * close down after this transfer.
984
 
             */
985
 
            conn->bits.close = TRUE; /* close when done */
986
 
            infof(data, "HTTP/1.1 proxy connection set close!\n");
987
 
          }
988
 
          else if((k->httpversion == 10) &&
989
 
                  Curl_compareheader(k->p, "Connection:", "keep-alive")) {
990
 
            /*
991
 
             * A HTTP/1.0 reply with the 'Connection: keep-alive' line
992
 
             * tells us the connection will be kept alive for our
993
 
             * pleasure.  Default action for 1.0 is to close.
994
 
             *
995
 
             * [RFC2068, section 19.7.1] */
996
 
            conn->bits.close = FALSE; /* don't close when done */
997
 
            infof(data, "HTTP/1.0 connection set to keep alive!\n");
998
 
          }
999
 
          else if(Curl_compareheader(k->p, "Connection:", "close")) {
1000
 
            /*
1001
 
             * [RFC 2616, section 8.1.2.1]
1002
 
             * "Connection: close" is HTTP/1.1 language and means that
1003
 
             * the connection will close when this request has been
1004
 
             * served.
1005
 
             */
1006
 
            conn->bits.close = TRUE; /* close when done */
1007
 
          }
1008
 
          else if(Curl_compareheader(k->p,
1009
 
                                     "Transfer-Encoding:", "chunked")) {
1010
 
            /*
1011
 
             * [RFC 2616, section 3.6.1] A 'chunked' transfer encoding
1012
 
             * means that the server will send a series of "chunks". Each
1013
 
             * chunk starts with line with info (including size of the
1014
 
             * coming block) (terminated with CRLF), then a block of data
1015
 
             * with the previously mentioned size. There can be any amount
1016
 
             * of chunks, and a chunk-data set to zero signals the
1017
 
             * end-of-chunks. */
1018
 
            k->chunk = TRUE; /* chunks coming our way */
1019
 
 
1020
 
            /* init our chunky engine */
1021
 
            Curl_httpchunk_init(conn);
1022
 
          }
1023
 
 
1024
 
          else if(checkprefix("Trailer:", k->p) ||
1025
 
                  checkprefix("Trailers:", k->p)) {
1026
 
            /*
1027
 
             * This test helps Curl_httpchunk_read() to determine to look
1028
 
             * for well formed trailers after the zero chunksize record. In
1029
 
             * this case a CRLF is required after the zero chunksize record
1030
 
             * when no trailers are sent, or after the last trailer record.
1031
 
             *
1032
 
             * It seems both Trailer: and Trailers: occur in the wild.
1033
 
             */
1034
 
            k->trailerhdrpresent = TRUE;
1035
 
          }
1036
 
 
1037
 
          else if(checkprefix("Content-Encoding:", k->p) &&
1038
 
                  data->set.str[STRING_ENCODING]) {
1039
 
            /*
1040
 
             * Process Content-Encoding. Look for the values: identity,
1041
 
             * gzip, deflate, compress, x-gzip and x-compress. x-gzip and
1042
 
             * x-compress are the same as gzip and compress. (Sec 3.5 RFC
1043
 
             * 2616). zlib cannot handle compress.  However, errors are
1044
 
             * handled further down when the response body is processed
1045
 
             */
1046
 
            char *start;
1047
 
 
1048
 
            /* Find the first non-space letter */
1049
 
            for(start=k->p+17;
1050
 
                *start && ISSPACE(*start);
1051
 
                start++)
1052
 
              ;  /* empty loop */
1053
 
 
1054
 
            /* Record the content-encoding for later use */
1055
 
            if(checkprefix("identity", start))
1056
 
              k->content_encoding = IDENTITY;
1057
 
            else if(checkprefix("deflate", start))
1058
 
              k->content_encoding = DEFLATE;
1059
 
            else if(checkprefix("gzip", start)
1060
 
                    || checkprefix("x-gzip", start))
1061
 
              k->content_encoding = GZIP;
1062
 
            else if(checkprefix("compress", start)
1063
 
                    || checkprefix("x-compress", start))
1064
 
              k->content_encoding = COMPRESS;
1065
 
          }
1066
 
          else if(checkprefix("Content-Range:", k->p)) {
1067
 
            /* Content-Range: bytes [num]-
1068
 
               Content-Range: bytes: [num]-
1069
 
               Content-Range: [num]-
1070
 
 
1071
 
               The second format was added since Sun's webserver
1072
 
               JavaWebServer/1.1.1 obviously sends the header this way!
1073
 
               The third added since some servers use that!
1074
 
            */
1075
 
 
1076
 
            char *ptr = k->p + 14;
1077
 
 
1078
 
            /* Move forward until first digit */
1079
 
            while(*ptr && !ISDIGIT(*ptr))
1080
 
              ptr++;
1081
 
 
1082
 
            k->offset = curlx_strtoofft(ptr, NULL, 10);
1083
 
 
1084
 
            if(data->state.resume_from == k->offset)
1085
 
              /* we asked for a resume and we got it */
1086
 
              k->content_range = TRUE;
1087
 
          }
1088
 
#if !defined(CURL_DISABLE_COOKIES)
1089
 
          else if(data->cookies &&
1090
 
                  checkprefix("Set-Cookie:", k->p)) {
1091
 
            Curl_share_lock(data, CURL_LOCK_DATA_COOKIE,
1092
 
                            CURL_LOCK_ACCESS_SINGLE);
1093
 
            Curl_cookie_add(data,
1094
 
                            data->cookies, TRUE, k->p+11,
1095
 
                            /* If there is a custom-set Host: name, use it
1096
 
                               here, or else use real peer host name. */
1097
 
                            conn->allocptr.cookiehost?
1098
 
                            conn->allocptr.cookiehost:conn->host.name,
1099
 
                            data->state.path);
1100
 
            Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1101
 
          }
1102
 
#endif
1103
 
          else if(checkprefix("Last-Modified:", k->p) &&
1104
 
                  (data->set.timecondition || data->set.get_filetime) ) {
1105
 
            time_t secs=time(NULL);
1106
 
            k->timeofdoc = curl_getdate(k->p+strlen("Last-Modified:"),
1107
 
                                        &secs);
1108
 
            if(data->set.get_filetime)
1109
 
              data->info.filetime = (long)k->timeofdoc;
1110
 
          }
1111
 
          else if((checkprefix("WWW-Authenticate:", k->p) &&
1112
 
                   (401 == k->httpcode)) ||
1113
 
                  (checkprefix("Proxy-authenticate:", k->p) &&
1114
 
                   (407 == k->httpcode))) {
1115
 
            result = Curl_http_input_auth(conn, k->httpcode, k->p);
1116
 
            if(result)
1117
 
              return result;
1118
 
          }
1119
 
          else if((k->httpcode >= 300 && k->httpcode < 400) &&
1120
 
                  checkprefix("Location:", k->p)) {
1121
 
            /* this is the URL that the server advices us to use instead */
1122
 
            char *ptr;
1123
 
            char *start=k->p;
1124
 
            char backup;
1125
 
 
1126
 
            start += 9; /* pass "Location:" */
1127
 
 
1128
 
            /* Skip spaces and tabs. We do this to support multiple
1129
 
               white spaces after the "Location:" keyword. */
1130
 
            while(*start && ISSPACE(*start ))
1131
 
              start++;
1132
 
 
1133
 
            /* Scan through the string from the end to find the last
1134
 
               non-space. k->end_ptr points to the actual terminating zero
1135
 
               letter, move pointer one letter back and start from
1136
 
               there. This logic strips off trailing whitespace, but keeps
1137
 
               any embedded whitespace. */
1138
 
            ptr = k->end_ptr-1;
1139
 
            while((ptr>=start) && ISSPACE(*ptr))
1140
 
              ptr--;
1141
 
            ptr++;
1142
 
 
1143
 
            backup = *ptr; /* store the ending letter */
1144
 
            if(ptr != start) {
1145
 
              *ptr = '\0';   /* zero terminate */
1146
 
              data->req.location = strdup(start); /* clone string */
1147
 
              *ptr = backup; /* restore ending letter */
1148
 
              if(!data->req.location)
1149
 
                return CURLE_OUT_OF_MEMORY;
1150
 
              if(data->set.http_follow_location) {
1151
 
                data->req.newurl = strdup(data->req.location); /* clone */
1152
 
                if(!data->req.newurl)
1153
 
                  return CURLE_OUT_OF_MEMORY;
1154
 
              }
1155
 
            }
1156
 
          }
1157
 
#endif   /* CURL_DISABLE_HTTP */
1158
 
 
1159
 
          /*
1160
 
           * End of header-checks. Write them to the client.
1161
 
           */
1162
 
 
1163
 
          writetype = CLIENTWRITE_HEADER;
1164
 
          if(data->set.include_header)
1165
 
            writetype |= CLIENTWRITE_BODY;
1166
 
 
1167
 
          if(data->set.verbose)
1168
 
            Curl_debug(data, CURLINFO_HEADER_IN,
1169
 
                       k->p, (size_t)k->hbuflen, conn);
1170
 
 
1171
 
          result = Curl_client_write(conn, writetype, k->p, k->hbuflen);
1172
 
          if(result)
1173
 
            return result;
1174
 
 
1175
 
          data->info.header_size += (long)k->hbuflen;
1176
 
          data->req.headerbytecount += (long)k->hbuflen;
1177
 
 
1178
 
          /* reset hbufp pointer && hbuflen */
1179
 
          k->hbufp = data->state.headerbuff;
1180
 
          k->hbuflen = 0;
1181
 
        }
1182
 
        while(!stop_reading && *k->str); /* header line within buffer */
1183
 
 
1184
 
        if(stop_reading)
1185
 
          /* We've stopped dealing with input, get out of the do-while loop */
1186
 
          break;
1187
 
 
1188
 
        /* We might have reached the end of the header part here, but
1189
 
           there might be a non-header part left in the end of the read
1190
 
           buffer. */
1191
 
 
1192
 
      }                       /* end if header mode */
1193
 
 
1194
 
      /* This is not an 'else if' since it may be a rest from the header
1195
 
         parsing, where the beginning of the buffer is headers and the end
1196
 
         is non-headers. */
1197
 
      if(k->str && !k->header && (nread > 0 || is_empty_data)) {
1198
 
 
1199
 
        if(0 == k->bodywrites && !is_empty_data) {
1200
 
          /* These checks are only made the first time we are about to
1201
 
             write a piece of the body */
1202
 
          if(conn->protocol&PROT_HTTP) {
1203
 
            /* HTTP-only checks */
1204
 
 
1205
 
            if(data->req.newurl) {
1206
 
              if(conn->bits.close) {
1207
 
                /* Abort after the headers if "follow Location" is set
1208
 
                   and we're set to close anyway. */
1209
 
                k->keepon &= ~KEEP_READ;
1210
 
                *done = TRUE;
1211
 
                return CURLE_OK;
1212
 
              }
1213
 
              /* We have a new url to load, but since we want to be able
1214
 
                 to re-use this connection properly, we read the full
1215
 
                 response in "ignore more" */
1216
 
              k->ignorebody = TRUE;
1217
 
              infof(data, "Ignoring the response-body\n");
1218
 
            }
1219
 
            if(data->state.resume_from && !k->content_range &&
1220
 
               (data->set.httpreq==HTTPREQ_GET) &&
1221
 
               !k->ignorebody) {
1222
 
              /* we wanted to resume a download, although the server doesn't
1223
 
               * seem to support this and we did this with a GET (if it
1224
 
               * wasn't a GET we did a POST or PUT resume) */
1225
 
              failf(data, "HTTP server doesn't seem to support "
1226
 
                    "byte ranges. Cannot resume.");
1227
 
              return CURLE_RANGE_ERROR;
1228
 
            }
1229
 
 
1230
 
            if(data->set.timecondition && !data->state.range) {
1231
 
              /* A time condition has been set AND no ranges have been
1232
 
                 requested. This seems to be what chapter 13.3.4 of
1233
 
                 RFC 2616 defines to be the correct action for a
1234
 
                 HTTP/1.1 client */
1235
 
              if((k->timeofdoc > 0) && (data->set.timevalue > 0)) {
1236
 
                switch(data->set.timecondition) {
1237
 
                case CURL_TIMECOND_IFMODSINCE:
1238
 
                default:
1239
 
                  if(k->timeofdoc < data->set.timevalue) {
1240
 
                    infof(data,
1241
 
                          "The requested document is not new enough\n");
1242
 
                    *done = TRUE;
1243
 
                    return CURLE_OK;
1244
 
                  }
1245
 
                  break;
1246
 
                case CURL_TIMECOND_IFUNMODSINCE:
1247
 
                  if(k->timeofdoc > data->set.timevalue) {
1248
 
                    infof(data,
1249
 
                          "The requested document is not old enough\n");
1250
 
                    *done = TRUE;
1251
 
                    return CURLE_OK;
1252
 
                  }
1253
 
                  break;
1254
 
                } /* switch */
1255
 
              } /* two valid time strings */
1256
 
            } /* we have a time condition */
1257
 
 
1258
 
          } /* this is HTTP */
1259
 
        } /* this is the first time we write a body part */
1260
 
        k->bodywrites++;
1261
 
 
1262
 
        /* pass data to the debug function before it gets "dechunked" */
1263
 
        if(data->set.verbose) {
1264
 
          if(k->badheader) {
1265
 
            Curl_debug(data, CURLINFO_DATA_IN, data->state.headerbuff,
1266
 
                       (size_t)k->hbuflen, conn);
1267
 
            if(k->badheader == HEADER_PARTHEADER)
1268
 
              Curl_debug(data, CURLINFO_DATA_IN,
1269
 
                         k->str, (size_t)nread, conn);
1270
 
          }
1271
 
          else
1272
 
            Curl_debug(data, CURLINFO_DATA_IN,
1273
 
                       k->str, (size_t)nread, conn);
1274
 
        }
1275
 
 
1276
 
#ifndef CURL_DISABLE_HTTP
1277
 
        if(k->chunk) {
1278
 
          /*
1279
 
           * Here comes a chunked transfer flying and we need to decode this
1280
 
           * properly.  While the name says read, this function both reads
1281
 
           * and writes away the data. The returned 'nread' holds the number
1282
 
           * of actual data it wrote to the client.
1283
 
           */
1284
 
 
1285
 
          CHUNKcode res =
1286
 
            Curl_httpchunk_read(conn, k->str, nread, &nread);
1287
 
 
1288
 
          if(CHUNKE_OK < res) {
1289
 
            if(CHUNKE_WRITE_ERROR == res) {
1290
 
              failf(data, "Failed writing data");
1291
 
              return CURLE_WRITE_ERROR;
1292
 
            }
1293
 
            failf(data, "Received problem %d in the chunky parser", res);
1294
 
            return CURLE_RECV_ERROR;
1295
 
          }
1296
 
          else if(CHUNKE_STOP == res) {
1297
 
            size_t dataleft;
1298
 
            /* we're done reading chunks! */
1299
 
            k->keepon &= ~KEEP_READ; /* read no more */
1300
 
 
1301
 
            /* There are now possibly N number of bytes at the end of the
1302
 
               str buffer that weren't written to the client.
1303
 
 
1304
 
               We DO care about this data if we are pipelining.
1305
 
               Push it back to be read on the next pass. */
1306
 
 
1307
 
            dataleft = conn->chunk.dataleft;
1308
 
            if(dataleft != 0) {
1309
 
              infof(conn->data, "Leftovers after chunking. "
1310
 
                    " Rewinding %d bytes\n",dataleft);
1311
 
              read_rewind(conn, dataleft);
1312
 
            }
1313
 
          }
1314
 
          /* If it returned OK, we just keep going */
1315
 
        }
1316
 
#endif   /* CURL_DISABLE_HTTP */
1317
 
 
1318
 
        if((-1 != k->maxdownload) &&
1319
 
           (k->bytecount + nread >= k->maxdownload)) {
1320
 
          /* The 'excess' amount below can't be more than BUFSIZE which
1321
 
             always will fit in a size_t */
1322
 
          size_t excess = (size_t)(k->bytecount + nread - k->maxdownload);
1323
 
          if(excess > 0 && !k->ignorebody) {
1324
 
            infof(data,
1325
 
                  "Rewinding stream by : %d"
1326
 
                  " bytes on url %s (size = %" FORMAT_OFF_T
1327
 
                  ", maxdownload = %" FORMAT_OFF_T
1328
 
                  ", bytecount = %" FORMAT_OFF_T ", nread = %d)\n",
1329
 
                  excess, data->state.path,
1330
 
                  k->size, k->maxdownload, k->bytecount, nread);
1331
 
            read_rewind(conn, excess);
1332
 
          }
1333
 
 
1334
 
          nread = (ssize_t) (k->maxdownload - k->bytecount);
1335
 
          if(nread < 0 ) /* this should be unusual */
1336
 
            nread = 0;
1337
 
 
1338
 
          k->keepon &= ~KEEP_READ; /* we're done reading */
1339
 
        }
1340
 
 
1341
 
        k->bytecount += nread;
1342
 
 
1343
 
        Curl_pgrsSetDownloadCounter(data, k->bytecount);
1344
 
 
1345
 
        if(!k->chunk && (nread || k->badheader || is_empty_data)) {
1346
 
          /* If this is chunky transfer, it was already written */
1347
 
 
1348
 
          if(k->badheader && !k->ignorebody) {
1349
 
            /* we parsed a piece of data wrongly assuming it was a header
1350
 
               and now we output it as body instead */
1351
 
            result = Curl_client_write(conn, CLIENTWRITE_BODY,
1352
 
                                       data->state.headerbuff,
1353
 
                                       k->hbuflen);
1354
 
            if(result)
1355
 
              return result;
1356
 
          }
1357
 
          if(k->badheader < HEADER_ALLBAD) {
1358
 
            /* This switch handles various content encodings. If there's an
1359
 
               error here, be sure to check over the almost identical code
1360
 
               in http_chunks.c.
1361
 
               Make sure that ALL_CONTENT_ENCODINGS contains all the
1362
 
               encodings handled here. */
1363
 
#ifdef HAVE_LIBZ
1364
 
            switch (conn->data->set.http_ce_skip ?
1365
 
                    IDENTITY : k->content_encoding) {
1366
 
            case IDENTITY:
1367
 
#endif
1368
 
              /* This is the default when the server sends no
1369
 
                 Content-Encoding header. See Curl_readwrite_init; the
1370
 
                 memset() call initializes k->content_encoding to zero. */
1371
 
              if(!k->ignorebody)
1372
 
                result = Curl_client_write(conn, CLIENTWRITE_BODY, k->str,
1373
 
                                           nread);
1374
 
#ifdef HAVE_LIBZ
1375
 
              break;
1376
 
 
1377
 
            case DEFLATE:
1378
 
              /* Assume CLIENTWRITE_BODY; headers are not encoded. */
1379
 
              if(!k->ignorebody)
1380
 
                result = Curl_unencode_deflate_write(conn, k, nread);
1381
 
              break;
1382
 
 
1383
 
            case GZIP:
1384
 
              /* Assume CLIENTWRITE_BODY; headers are not encoded. */
1385
 
              if(!k->ignorebody)
1386
 
                result = Curl_unencode_gzip_write(conn, k, nread);
1387
 
              break;
1388
 
 
1389
 
            case COMPRESS:
1390
 
            default:
1391
 
              failf (data, "Unrecognized content encoding type. "
1392
 
                     "libcurl understands `identity', `deflate' and `gzip' "
1393
 
                     "content encodings.");
1394
 
              result = CURLE_BAD_CONTENT_ENCODING;
1395
 
              break;
1396
 
            }
1397
 
#endif
1398
 
          }
1399
 
          k->badheader = HEADER_NORMAL; /* taken care of now */
1400
 
 
1401
 
          if(result)
1402
 
            return result;
1403
 
        }
1404
 
 
1405
 
      } /* if(! header and data to read ) */
1406
 
 
1407
 
      if(is_empty_data) {
1408
 
        /* if we received nothing, the server closed the connection and we
1409
 
           are done */
1410
 
        k->keepon &= ~KEEP_READ;
1411
 
      }
1412
 
 
1413
 
    } while(data_pending(conn));
1414
 
 
1415
 
  } /* if( read from socket ) */
 
1679
 
 
1680
    result = readwrite_data(data, conn, k, &didwhat, done);
 
1681
    if(result || *done)
 
1682
      return result;
 
1683
  }
1416
1684
 
1417
1685
  /* If we still have writing to do, we check if we have a writable socket. */
1418
 
  if((k->keepon & KEEP_WRITE) && (select_res & CURL_CSELECT_OUT)) {
 
1686
  if((k->keepon & KEEP_SEND) && (select_res & CURL_CSELECT_OUT)) {
1419
1687
    /* write */
1420
1688
 
1421
 
    ssize_t i, si;
1422
 
    ssize_t bytes_written;
1423
 
 
1424
 
    if((k->bytecount == 0) && (k->writebytecount == 0))
1425
 
      Curl_pgrsTime(data, TIMER_STARTTRANSFER);
1426
 
 
1427
 
    didwhat |= KEEP_WRITE;
1428
 
 
1429
 
    /*
1430
 
     * We loop here to do the READ and SEND loop until we run out of
1431
 
     * data to send or until we get EWOULDBLOCK back
1432
 
     */
1433
 
    do {
1434
 
 
1435
 
      /* only read more data if there's no upload data already
1436
 
         present in the upload buffer */
1437
 
      if(0 == data->req.upload_present) {
1438
 
        /* init the "upload from here" pointer */
1439
 
        data->req.upload_fromhere = k->uploadbuf;
1440
 
 
1441
 
        if(!k->upload_done) {
1442
 
          /* HTTP pollution, this should be written nicer to become more
1443
 
             protocol agnostic. */
1444
 
          int fillcount;
1445
 
 
1446
 
          if((k->exp100 == EXP100_SENDING_REQUEST) &&
1447
 
             (data->state.proto.http->sending == HTTPSEND_BODY)) {
1448
 
            /* If this call is to send body data, we must take some action:
1449
 
               We have sent off the full HTTP 1.1 request, and we shall now
1450
 
               go into the Expect: 100 state and await such a header */
1451
 
            k->exp100 = EXP100_AWAITING_CONTINUE; /* wait for the header */
1452
 
            k->keepon &= ~KEEP_WRITE;         /* disable writing */
1453
 
            k->start100 = Curl_tvnow();       /* timeout count starts now */
1454
 
            didwhat &= ~KEEP_WRITE;  /* we didn't write anything actually */
1455
 
            break;
1456
 
          }
1457
 
 
1458
 
          result = Curl_fillreadbuffer(conn, BUFSIZE, &fillcount);
1459
 
          if(result)
1460
 
            return result;
1461
 
 
1462
 
          nread = (ssize_t)fillcount;
1463
 
        }
1464
 
        else
1465
 
          nread = 0; /* we're done uploading/reading */
1466
 
 
1467
 
        if(!nread && (k->keepon & KEEP_READ_PAUSE)) {
1468
 
          /* this is a paused transfer */
1469
 
          break;
1470
 
        }
1471
 
        else if(nread<=0) {
1472
 
          /* done */
1473
 
          k->keepon &= ~KEEP_WRITE; /* we're done writing */
1474
 
 
1475
 
          if(conn->bits.rewindaftersend) {
1476
 
            result = Curl_readrewind(conn);
1477
 
            if(result)
1478
 
              return result;
1479
 
          }
1480
 
          break;
1481
 
        }
1482
 
 
1483
 
        /* store number of bytes available for upload */
1484
 
        data->req.upload_present = nread;
1485
 
 
1486
 
        /* convert LF to CRLF if so asked */
1487
 
#ifdef CURL_DO_LINEEND_CONV
1488
 
        /* always convert if we're FTPing in ASCII mode */
1489
 
        if((data->set.crlf) || (data->set.prefer_ascii))
1490
 
#else
1491
 
          if(data->set.crlf)
1492
 
#endif /* CURL_DO_LINEEND_CONV */
1493
 
          {
1494
 
            if(data->state.scratch == NULL)
1495
 
              data->state.scratch = malloc(2*BUFSIZE);
1496
 
            if(data->state.scratch == NULL) {
1497
 
              failf (data, "Failed to alloc scratch buffer!");
1498
 
              return CURLE_OUT_OF_MEMORY;
1499
 
            }
1500
 
            /*
1501
 
             * ASCII/EBCDIC Note: This is presumably a text (not binary)
1502
 
             * transfer so the data should already be in ASCII.
1503
 
             * That means the hex values for ASCII CR (0x0d) & LF (0x0a)
1504
 
             * must be used instead of the escape sequences \r & \n.
1505
 
             */
1506
 
            for(i = 0, si = 0; i < nread; i++, si++) {
1507
 
              if(data->req.upload_fromhere[i] == 0x0a) {
1508
 
                data->state.scratch[si++] = 0x0d;
1509
 
                data->state.scratch[si] = 0x0a;
1510
 
                if(!data->set.crlf) {
1511
 
                  /* we're here only because FTP is in ASCII mode...
1512
 
                     bump infilesize for the LF we just added */
1513
 
                  data->set.infilesize++;
1514
 
                }
1515
 
              }
1516
 
              else
1517
 
                data->state.scratch[si] = data->req.upload_fromhere[i];
1518
 
            }
1519
 
            if(si != nread) {
1520
 
              /* only perform the special operation if we really did replace
1521
 
                 anything */
1522
 
              nread = si;
1523
 
 
1524
 
              /* upload from the new (replaced) buffer instead */
1525
 
              data->req.upload_fromhere = data->state.scratch;
1526
 
 
1527
 
              /* set the new amount too */
1528
 
              data->req.upload_present = nread;
1529
 
            }
1530
 
          }
1531
 
      } /* if 0 == data->req.upload_present */
1532
 
      else {
1533
 
        /* We have a partial buffer left from a previous "round". Use
1534
 
           that instead of reading more data */
1535
 
      }
1536
 
 
1537
 
      /* write to socket (send away data) */
1538
 
      result = Curl_write(conn,
1539
 
                          conn->writesockfd,     /* socket to send to */
1540
 
                          data->req.upload_fromhere, /* buffer pointer */
1541
 
                          data->req.upload_present,  /* buffer size */
1542
 
                          &bytes_written);       /* actually send away */
1543
 
      if(result)
1544
 
        return result;
1545
 
 
1546
 
      if(data->set.verbose)
1547
 
        /* show the data before we change the pointer upload_fromhere */
1548
 
        Curl_debug(data, CURLINFO_DATA_OUT, data->req.upload_fromhere,
1549
 
                   (size_t)bytes_written, conn);
1550
 
 
1551
 
      if(data->req.upload_present != bytes_written) {
1552
 
        /* we only wrote a part of the buffer (if anything), deal with it! */
1553
 
 
1554
 
        /* store the amount of bytes left in the buffer to write */
1555
 
        data->req.upload_present -= bytes_written;
1556
 
 
1557
 
        /* advance the pointer where to find the buffer when the next send
1558
 
           is to happen */
1559
 
        data->req.upload_fromhere += bytes_written;
1560
 
      }
1561
 
      else {
1562
 
        /* we've uploaded that buffer now */
1563
 
        data->req.upload_fromhere = k->uploadbuf;
1564
 
        data->req.upload_present = 0; /* no more bytes left */
1565
 
 
1566
 
        if(k->upload_done) {
1567
 
          /* switch off writing, we're done! */
1568
 
          k->keepon &= ~KEEP_WRITE; /* we're done writing */
1569
 
        }
1570
 
      }
1571
 
 
1572
 
      k->writebytecount += bytes_written;
1573
 
      Curl_pgrsSetUploadCounter(data, k->writebytecount);
1574
 
 
1575
 
    } while(0); /* just to break out from! */
1576
 
 
1577
 
  } /* if(something to write) */
 
1689
    result = readwrite_upload(data, conn, k, &didwhat);
 
1690
    if(result)
 
1691
      return result;
 
1692
  }
1578
1693
 
1579
1694
  k->now = Curl_tvnow();
1580
1695
  if(didwhat) {
1604
1719
      if(ms > CURL_TIMEOUT_EXPECT_100) {
1605
1720
        /* we've waited long enough, continue anyway */
1606
1721
        k->exp100 = EXP100_SEND_DATA;
1607
 
        k->keepon |= KEEP_WRITE;
 
1722
        k->keepon |= KEEP_SEND;
 
1723
        infof(data, "Done waiting for 100-continue\n");
1608
1724
      }
1609
1725
    }
1610
1726
  }
1671
1787
  }
1672
1788
 
1673
1789
  /* Now update the "done" boolean we return */
1674
 
  *done = (bool)(0 == (k->keepon&(KEEP_READ|KEEP_WRITE|
1675
 
                                  KEEP_READ_PAUSE|KEEP_WRITE_PAUSE)));
 
1790
  *done = (bool)(0 == (k->keepon&(KEEP_RECV|KEEP_SEND|
 
1791
                                  KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)));
1676
1792
 
1677
1793
  return CURLE_OK;
1678
1794
}
1693
1809
  int bitmap = GETSOCK_BLANK;
1694
1810
  unsigned sockindex = 0;
1695
1811
 
 
1812
  if(conn->handler->perform_getsock)
 
1813
    return conn->handler->perform_getsock(conn, sock, numsocks);
 
1814
 
1696
1815
  if(numsocks < 2)
1697
1816
    /* simple check but we might need two slots */
1698
1817
    return GETSOCK_BLANK;
1699
1818
 
1700
1819
  /* don't include HOLD and PAUSE connections */
1701
 
  if((data->req.keepon & KEEP_READBITS) == KEEP_READ) {
 
1820
  if((data->req.keepon & KEEP_RECVBITS) == KEEP_RECV) {
1702
1821
 
1703
1822
    DEBUGASSERT(conn->sockfd != CURL_SOCKET_BAD);
1704
1823
 
1707
1826
  }
1708
1827
 
1709
1828
  /* don't include HOLD and PAUSE connections */
1710
 
  if((data->req.keepon & KEEP_WRITEBITS) == KEEP_WRITE) {
 
1829
  if((data->req.keepon & KEEP_SENDBITS) == KEEP_SEND) {
1711
1830
 
1712
1831
    if((conn->sockfd != conn->writesockfd) ||
1713
 
       !(data->req.keepon & KEEP_READ)) {
 
1832
       !(data->req.keepon & KEEP_RECV)) {
1714
1833
      /* only if they are not the same socket or we didn't have a readable
1715
1834
         one, we increase index */
1716
 
      if(data->req.keepon & KEEP_READ)
 
1835
      if(data->req.keepon & KEEP_RECV)
1717
1836
        sockindex++; /* increase index if we need two entries */
1718
1837
 
1719
1838
      DEBUGASSERT(conn->writesockfd != CURL_SOCKET_BAD);
1749
1868
  struct SessionHandle *data = conn->data;
1750
1869
  struct SingleRequest *k = &data->req;
1751
1870
  bool done=FALSE;
 
1871
  bool first=TRUE;
 
1872
  int timeout_ms;
1752
1873
 
1753
1874
  if((conn->sockfd == CURL_SOCKET_BAD) &&
1754
1875
     (conn->writesockfd == CURL_SOCKET_BAD))
1766
1887
    /* limit-rate logic: if speed exceeds threshold, then do not include fd in
1767
1888
       select set. The current speed is recalculated in each Curl_readwrite()
1768
1889
       call */
1769
 
    if((k->keepon & KEEP_WRITE) &&
 
1890
    if((k->keepon & KEEP_SEND) &&
1770
1891
        (!data->set.max_send_speed ||
1771
1892
         (data->progress.ulspeed < data->set.max_send_speed) )) {
1772
1893
      fd_write = conn->writesockfd;
1773
 
      k->keepon &= ~KEEP_WRITE_HOLD;
 
1894
      k->keepon &= ~KEEP_SEND_HOLD;
1774
1895
    }
1775
1896
    else {
1776
1897
      fd_write = CURL_SOCKET_BAD;
1777
 
      if(k->keepon & KEEP_WRITE)
1778
 
        k->keepon |= KEEP_WRITE_HOLD; /* hold it */
 
1898
      if(k->keepon & KEEP_SEND)
 
1899
        k->keepon |= KEEP_SEND_HOLD; /* hold it */
1779
1900
    }
1780
1901
 
1781
 
    if((k->keepon & KEEP_READ) &&
 
1902
    if((k->keepon & KEEP_RECV) &&
1782
1903
        (!data->set.max_recv_speed ||
1783
1904
         (data->progress.dlspeed < data->set.max_recv_speed)) ) {
1784
1905
      fd_read = conn->sockfd;
1785
 
      k->keepon &= ~KEEP_READ_HOLD;
 
1906
      k->keepon &= ~KEEP_RECV_HOLD;
1786
1907
    }
1787
1908
    else {
1788
1909
      fd_read = CURL_SOCKET_BAD;
1789
 
      if(k->keepon & KEEP_READ)
1790
 
        k->keepon |= KEEP_READ_HOLD; /* hold it */
 
1910
      if(k->keepon & KEEP_RECV)
 
1911
        k->keepon |= KEEP_RECV_HOLD; /* hold it */
1791
1912
    }
1792
1913
 
1793
1914
    /* pause logic. Don't check descriptors for paused connections */
1794
 
    if(k->keepon & KEEP_READ_PAUSE)
 
1915
    if(k->keepon & KEEP_RECV_PAUSE)
1795
1916
      fd_read = CURL_SOCKET_BAD;
1796
 
    if(k->keepon & KEEP_WRITE_PAUSE)
 
1917
    if(k->keepon & KEEP_SEND_PAUSE)
1797
1918
      fd_write = CURL_SOCKET_BAD;
1798
1919
 
1799
1920
    /* The *_HOLD and *_PAUSE logic is necessary since even though there might
1800
1921
       be no traffic during the select interval, we still call
1801
1922
       Curl_readwrite() for the timeout case and if we limit transfer speed we
1802
1923
       must make sure that this function doesn't transfer anything while in
1803
 
       HOLD status. */
1804
 
 
1805
 
    switch (Curl_socket_ready(fd_read, fd_write, 1000)) {
 
1924
       HOLD status.
 
1925
 
 
1926
       The no timeout for the first round is for the protocols for which data
 
1927
       has already been slurped off the socket and thus waiting for action
 
1928
       won't work since it'll wait even though there is already data present
 
1929
       to work with. */
 
1930
    if(first &&
 
1931
       ((fd_read != CURL_SOCKET_BAD) || (fd_write != CURL_SOCKET_BAD)))
 
1932
      /* if this is the first lap and one of the file descriptors is fine
 
1933
         to work with, skip the timeout */
 
1934
      timeout_ms = 0;
 
1935
    else
 
1936
      timeout_ms = 1000;
 
1937
 
 
1938
    switch (Curl_socket_ready(fd_read, fd_write, timeout_ms)) {
1806
1939
    case -1: /* select() error, stop reading */
1807
1940
#ifdef EINTR
1808
1941
      /* The EINTR is not serious, and it seems you might get this more
1809
 
         ofen when using the lib in a multi-threaded environment! */
 
1942
         often when using the lib in a multi-threaded environment! */
1810
1943
      if(SOCKERRNO == EINTR)
1811
 
        ;
1812
 
      else
 
1944
        continue;
1813
1945
#endif
1814
 
        done = TRUE; /* no more read or write */
1815
 
      continue;
 
1946
      return CURLE_RECV_ERROR;  /* indicate a network problem */
1816
1947
    case 0:  /* timeout */
1817
1948
    default: /* readable descriptors */
1818
1949
 
1819
1950
      result = Curl_readwrite(conn, &done);
 
1951
      /* "done" signals to us if the transfer(s) are ready */
1820
1952
      break;
1821
1953
    }
1822
1954
    if(result)
1823
1955
      return result;
1824
1956
 
1825
 
    /* "done" signals to us if the transfer(s) are ready */
 
1957
    first = FALSE; /* not the first lap anymore */
1826
1958
  }
1827
1959
 
1828
1960
  return CURLE_OK;
1835
1967
{
1836
1968
  CURLcode res;
1837
1969
  if(!data->change.url) {
1838
 
    /* we can't do anything wihout URL */
 
1970
    /* we can't do anything without URL */
1839
1971
    failf(data, "No URL set!");
1840
1972
    return CURLE_URL_MALFORMAT;
1841
1973
  }
1850
1982
  data->set.followlocation=0; /* reset the location-follow counter */
1851
1983
  data->state.this_is_a_follow = FALSE; /* reset this */
1852
1984
  data->state.errorbuf = FALSE; /* no error has occurred */
 
1985
  data->state.httpversion = 0; /* don't assume any particular server version */
1853
1986
 
1854
1987
  data->state.authproblem = FALSE;
1855
1988
  data->state.authhost.want = data->set.httpauth;
1856
1989
  data->state.authproxy.want = data->set.proxyauth;
 
1990
  Curl_safefree(data->info.wouldredirect);
 
1991
  data->info.wouldredirect = NULL;
1857
1992
 
1858
1993
  /* If there is a list of cookie files to read, do it now! */
1859
1994
  if(data->change.cookielist) {
1900
2035
  return CURLE_OK;
1901
2036
}
1902
2037
 
 
2038
#ifndef CURL_DISABLE_HTTP
1903
2039
/*
1904
2040
 * strlen_url() returns the length of the given URL if the spaces within the
1905
2041
 * URL were properly URL encoded.
1964
2100
}
1965
2101
 
1966
2102
/*
 
2103
 * Returns true if the given URL is absolute (as opposed to relative)
 
2104
 */
 
2105
static bool is_absolute_url(const char *url)
 
2106
{
 
2107
  char prot[16]; /* URL protocol string storage */
 
2108
  char letter;   /* used for a silly sscanf */
 
2109
 
 
2110
  return (bool)(2 == sscanf(url, "%15[^?&/:]://%c", prot, &letter));
 
2111
}
 
2112
 
 
2113
/*
 
2114
 * Concatenate a relative URL to a base URL making it absolute.
 
2115
 * URL-encodes any spaces.
 
2116
 * The returned pointer must be freed by the caller unless NULL
 
2117
 * (returns NULL on out of memory).
 
2118
 */
 
2119
static char *concat_url(const char *base, const char *relurl)
 
2120
{
 
2121
  /***
 
2122
   TRY to append this new path to the old URL
 
2123
   to the right of the host part. Oh crap, this is doomed to cause
 
2124
   problems in the future...
 
2125
  */
 
2126
  char *newest;
 
2127
  char *protsep;
 
2128
  char *pathsep;
 
2129
  size_t newlen;
 
2130
 
 
2131
  const char *useurl = relurl;
 
2132
  size_t urllen;
 
2133
 
 
2134
  /* we must make our own copy of the URL to play with, as it may
 
2135
     point to read-only data */
 
2136
  char *url_clone=strdup(base);
 
2137
 
 
2138
  if(!url_clone)
 
2139
    return NULL; /* skip out of this NOW */
 
2140
 
 
2141
  /* protsep points to the start of the host name */
 
2142
  protsep=strstr(url_clone, "//");
 
2143
  if(!protsep)
 
2144
    protsep=url_clone;
 
2145
  else
 
2146
    protsep+=2; /* pass the slashes */
 
2147
 
 
2148
  if('/' != relurl[0]) {
 
2149
    int level=0;
 
2150
 
 
2151
    /* First we need to find out if there's a ?-letter in the URL,
 
2152
       and cut it and the right-side of that off */
 
2153
    pathsep = strchr(protsep, '?');
 
2154
    if(pathsep)
 
2155
      *pathsep=0;
 
2156
 
 
2157
    /* we have a relative path to append to the last slash if there's one
 
2158
       available, or if the new URL is just a query string (starts with a
 
2159
       '?')  we append the new one at the end of the entire currently worked
 
2160
       out URL */
 
2161
    if(useurl[0] != '?') {
 
2162
      pathsep = strrchr(protsep, '/');
 
2163
      if(pathsep)
 
2164
        *pathsep=0;
 
2165
    }
 
2166
 
 
2167
    /* Check if there's any slash after the host name, and if so, remember
 
2168
       that position instead */
 
2169
    pathsep = strchr(protsep, '/');
 
2170
    if(pathsep)
 
2171
      protsep = pathsep+1;
 
2172
    else
 
2173
      protsep = NULL;
 
2174
 
 
2175
    /* now deal with one "./" or any amount of "../" in the newurl
 
2176
       and act accordingly */
 
2177
 
 
2178
    if((useurl[0] == '.') && (useurl[1] == '/'))
 
2179
      useurl+=2; /* just skip the "./" */
 
2180
 
 
2181
    while((useurl[0] == '.') &&
 
2182
          (useurl[1] == '.') &&
 
2183
          (useurl[2] == '/')) {
 
2184
      level++;
 
2185
      useurl+=3; /* pass the "../" */
 
2186
    }
 
2187
 
 
2188
    if(protsep) {
 
2189
      while(level--) {
 
2190
        /* cut off one more level from the right of the original URL */
 
2191
        pathsep = strrchr(protsep, '/');
 
2192
        if(pathsep)
 
2193
          *pathsep=0;
 
2194
        else {
 
2195
          *protsep=0;
 
2196
          break;
 
2197
        }
 
2198
      }
 
2199
    }
 
2200
  }
 
2201
  else {
 
2202
    /* We got a new absolute path for this server, cut off from the
 
2203
       first slash */
 
2204
    pathsep = strchr(protsep, '/');
 
2205
    if(pathsep) {
 
2206
      /* When people use badly formatted URLs, such as
 
2207
         "http://www.url.com?dir=/home/daniel" we must not use the first
 
2208
         slash, if there's a ?-letter before it! */
 
2209
      char *sep = strchr(protsep, '?');
 
2210
      if(sep && (sep < pathsep))
 
2211
        pathsep = sep;
 
2212
      *pathsep=0;
 
2213
    }
 
2214
    else {
 
2215
      /* There was no slash. Now, since we might be operating on a badly
 
2216
         formatted URL, such as "http://www.url.com?id=2380" which doesn't
 
2217
         use a slash separator as it is supposed to, we need to check for a
 
2218
         ?-letter as well! */
 
2219
      pathsep = strchr(protsep, '?');
 
2220
      if(pathsep)
 
2221
        *pathsep=0;
 
2222
    }
 
2223
  }
 
2224
 
 
2225
  /* If the new part contains a space, this is a mighty stupid redirect
 
2226
     but we still make an effort to do "right". To the left of a '?'
 
2227
     letter we replace each space with %20 while it is replaced with '+'
 
2228
     on the right side of the '?' letter.
 
2229
  */
 
2230
  newlen = strlen_url(useurl);
 
2231
 
 
2232
  urllen = strlen(url_clone);
 
2233
 
 
2234
  newest = malloc( urllen + 1 + /* possible slash */
 
2235
                         newlen + 1 /* zero byte */);
 
2236
 
 
2237
  if(!newest) {
 
2238
    free(url_clone); /* don't leak this */
 
2239
    return NULL;
 
2240
  }
 
2241
 
 
2242
  /* copy over the root url part */
 
2243
  memcpy(newest, url_clone, urllen);
 
2244
 
 
2245
  /* check if we need to append a slash */
 
2246
  if(('/' == useurl[0]) || (protsep && !*protsep) || ('?' == useurl[0]))
 
2247
    ;
 
2248
  else
 
2249
    newest[urllen++]='/';
 
2250
 
 
2251
  /* then append the new piece on the right side */
 
2252
  strcpy_url(&newest[urllen], useurl);
 
2253
 
 
2254
  free(url_clone);
 
2255
 
 
2256
  return newest;
 
2257
}
 
2258
#endif /* CURL_DISABLE_HTTP */
 
2259
 
 
2260
/*
1967
2261
 * Curl_follow() handles the URL redirect magic. Pass in the 'newurl' string
1968
2262
 * as given by the remote server and set up the new URL to request.
1969
2263
 */
1973
2267
                                      here */
1974
2268
                     followtype type) /* see transfer.h */
1975
2269
{
 
2270
#ifdef CURL_DISABLE_HTTP
 
2271
  (void)data;
 
2272
  (void)newurl;
 
2273
  (void)type;
 
2274
  /* Location: following will not happen when HTTP is disabled */
 
2275
  return CURLE_TOO_MANY_REDIRECTS;
 
2276
#else
 
2277
 
1976
2278
  /* Location: redirect */
1977
 
  char prot[16]; /* URL protocol string storage */
1978
 
  char letter;   /* used for a silly sscanf */
1979
 
  size_t newlen;
1980
 
  char *newest;
1981
2279
  bool disallowport = FALSE;
1982
2280
 
1983
2281
  if(type == FOLLOW_REDIR) {
2002
2300
        free(data->change.referer);
2003
2301
 
2004
2302
      data->change.referer = strdup(data->change.url);
 
2303
      if (!data->change.referer) {
 
2304
        data->change.referer_alloc = FALSE;
 
2305
        return CURLE_OUT_OF_MEMORY;
 
2306
      }
2005
2307
      data->change.referer_alloc = TRUE; /* yes, free this later */
2006
2308
    }
2007
2309
  }
2008
2310
 
2009
 
  if(2 != sscanf(newurl, "%15[^?&/:]://%c", prot, &letter)) {
 
2311
  if(!is_absolute_url(newurl))  {
2010
2312
    /***
2011
2313
     *DANG* this is an RFC 2068 violation. The URL is supposed
2012
2314
     to be absolute and this doesn't seem to be that!
2013
 
     ***
2014
 
     Instead, we have to TRY to append this new path to the old URL
2015
 
     to the right of the host part. Oh crap, this is doomed to cause
2016
 
     problems in the future...
2017
 
    */
2018
 
    char *protsep;
2019
 
    char *pathsep;
2020
 
 
2021
 
    char *useurl = newurl;
2022
 
    size_t urllen;
2023
 
 
2024
 
    /* we must make our own copy of the URL to play with, as it may
2025
 
       point to read-only data */
2026
 
    char *url_clone=strdup(data->change.url);
2027
 
 
2028
 
    if(!url_clone)
2029
 
      return CURLE_OUT_OF_MEMORY; /* skip out of this NOW */
2030
 
 
2031
 
    /* protsep points to the start of the host name */
2032
 
    protsep=strstr(url_clone, "//");
2033
 
    if(!protsep)
2034
 
      protsep=url_clone;
2035
 
    else
2036
 
      protsep+=2; /* pass the slashes */
2037
 
 
2038
 
    if('/' != newurl[0]) {
2039
 
      int level=0;
2040
 
 
2041
 
      /* First we need to find out if there's a ?-letter in the URL,
2042
 
         and cut it and the right-side of that off */
2043
 
      pathsep = strchr(protsep, '?');
2044
 
      if(pathsep)
2045
 
        *pathsep=0;
2046
 
 
2047
 
      /* we have a relative path to append to the last slash if there's one
2048
 
         available, or if the new URL is just a query string (starts with a
2049
 
         '?')  we append the new one at the end of the entire currently worked
2050
 
         out URL */
2051
 
      if(useurl[0] != '?') {
2052
 
        pathsep = strrchr(protsep, '/');
2053
 
        if(pathsep)
2054
 
          *pathsep=0;
2055
 
      }
2056
 
 
2057
 
      /* Check if there's any slash after the host name, and if so, remember
2058
 
         that position instead */
2059
 
      pathsep = strchr(protsep, '/');
2060
 
      if(pathsep)
2061
 
        protsep = pathsep+1;
2062
 
      else
2063
 
        protsep = NULL;
2064
 
 
2065
 
      /* now deal with one "./" or any amount of "../" in the newurl
2066
 
         and act accordingly */
2067
 
 
2068
 
      if((useurl[0] == '.') && (useurl[1] == '/'))
2069
 
        useurl+=2; /* just skip the "./" */
2070
 
 
2071
 
      while((useurl[0] == '.') &&
2072
 
            (useurl[1] == '.') &&
2073
 
            (useurl[2] == '/')) {
2074
 
        level++;
2075
 
        useurl+=3; /* pass the "../" */
2076
 
      }
2077
 
 
2078
 
      if(protsep) {
2079
 
        while(level--) {
2080
 
          /* cut off one more level from the right of the original URL */
2081
 
          pathsep = strrchr(protsep, '/');
2082
 
          if(pathsep)
2083
 
            *pathsep=0;
2084
 
          else {
2085
 
            *protsep=0;
2086
 
            break;
2087
 
          }
2088
 
        }
2089
 
      }
2090
 
    }
2091
 
    else {
2092
 
      /* We got a new absolute path for this server, cut off from the
2093
 
         first slash */
2094
 
      pathsep = strchr(protsep, '/');
2095
 
      if(pathsep) {
2096
 
        /* When people use badly formatted URLs, such as
2097
 
           "http://www.url.com?dir=/home/daniel" we must not use the first
2098
 
           slash, if there's a ?-letter before it! */
2099
 
        char *sep = strchr(protsep, '?');
2100
 
        if(sep && (sep < pathsep))
2101
 
          pathsep = sep;
2102
 
        *pathsep=0;
2103
 
      }
2104
 
      else {
2105
 
        /* There was no slash. Now, since we might be operating on a badly
2106
 
           formatted URL, such as "http://www.url.com?id=2380" which doesn't
2107
 
           use a slash separator as it is supposed to, we need to check for a
2108
 
           ?-letter as well! */
2109
 
        pathsep = strchr(protsep, '?');
2110
 
        if(pathsep)
2111
 
          *pathsep=0;
2112
 
      }
2113
 
    }
2114
 
 
2115
 
    /* If the new part contains a space, this is a mighty stupid redirect
2116
 
       but we still make an effort to do "right". To the left of a '?'
2117
 
       letter we replace each space with %20 while it is replaced with '+'
2118
 
       on the right side of the '?' letter.
2119
 
    */
2120
 
    newlen = strlen_url(useurl);
2121
 
 
2122
 
    urllen = strlen(url_clone);
2123
 
 
2124
 
    newest=(char *)malloc( urllen + 1 + /* possible slash */
2125
 
                           newlen + 1 /* zero byte */);
2126
 
 
2127
 
    if(!newest) {
2128
 
      free(url_clone); /* don't leak this */
2129
 
      return CURLE_OUT_OF_MEMORY; /* go out from this */
2130
 
    }
2131
 
 
2132
 
    /* copy over the root url part */
2133
 
    memcpy(newest, url_clone, urllen);
2134
 
 
2135
 
    /* check if we need to append a slash */
2136
 
    if(('/' == useurl[0]) || (protsep && !*protsep) || ('?' == useurl[0]))
2137
 
      ;
2138
 
    else
2139
 
      newest[urllen++]='/';
2140
 
 
2141
 
    /* then append the new piece on the right side */
2142
 
    strcpy_url(&newest[urllen], useurl);
2143
 
 
2144
 
    free(newurl); /* newurl is the allocated pointer */
2145
 
    free(url_clone);
2146
 
    newurl = newest;
 
2315
     */
 
2316
    char *absolute = concat_url(data->change.url, newurl);
 
2317
    if (!absolute)
 
2318
      return CURLE_OUT_OF_MEMORY;
 
2319
    free(newurl);
 
2320
    newurl = absolute;
2147
2321
  }
2148
2322
  else {
2149
2323
    /* This is an absolute URL, don't allow the custom port number */
2152
2326
    if(strchr(newurl, ' ')) {
2153
2327
      /* This new URL contains at least one space, this is a mighty stupid
2154
2328
         redirect but we still make an effort to do "right". */
2155
 
      newlen = strlen_url(newurl);
 
2329
      char *newest;
 
2330
      size_t newlen = strlen_url(newurl);
2156
2331
 
2157
2332
      newest = malloc(newlen+1); /* get memory for this */
2158
 
      if(newest) {
2159
 
        strcpy_url(newest, newurl); /* create a space-free URL */
 
2333
      if (!newest)
 
2334
        return CURLE_OUT_OF_MEMORY;
 
2335
      strcpy_url(newest, newurl); /* create a space-free URL */
2160
2336
 
2161
 
        free(newurl); /* that was no good */
2162
 
        newurl = newest; /* use this instead now */
2163
 
      }
 
2337
      free(newurl); /* that was no good */
 
2338
      newurl = newest; /* use this instead now */
2164
2339
    }
2165
2340
 
2166
2341
  }
2220
2395
     * libcurl gets the page that most user agents would get, libcurl has to
2221
2396
     * force GET.
2222
2397
     *
2223
 
     * This behaviour can be overriden with CURLOPT_POST301.
 
2398
     * This behaviour can be overridden with CURLOPT_POSTREDIR.
2224
2399
     */
2225
2400
    if( (data->set.httpreq == HTTPREQ_POST
2226
2401
         || data->set.httpreq == HTTPREQ_POST_FORM)
2247
2422
    status. When interoperability with such clients is a concern, the
2248
2423
    302 status code may be used instead, since most user agents react
2249
2424
    to a 302 response as described here for 303.
 
2425
 
 
2426
    This behaviour can be overriden with CURLOPT_POSTREDIR
2250
2427
    */
 
2428
    if( (data->set.httpreq == HTTPREQ_POST
 
2429
         || data->set.httpreq == HTTPREQ_POST_FORM)
 
2430
        && !data->set.post302) {
 
2431
      infof(data,
 
2432
            "Violate RFC 2616/10.3.3 and switch from POST to GET\n");
 
2433
      data->set.httpreq = HTTPREQ_GET;
 
2434
    }
 
2435
    break;
 
2436
 
2251
2437
  case 303: /* See Other */
2252
2438
    /* Disable both types of POSTs, since doing a second POST when
2253
2439
     * following isn't what anyone would want! */
2276
2462
  Curl_pgrsResetTimes(data);
2277
2463
 
2278
2464
  return CURLE_OK;
 
2465
#endif /* CURL_DISABLE_HTTP */
2279
2466
}
2280
2467
 
2281
2468
static CURLcode
2383
2570
 
2384
2571
      if(res == CURLE_OK) {
2385
2572
        res = Transfer(conn); /* now fetch that URL please */
2386
 
        if(res == CURLE_OK) {
 
2573
        if((res == CURLE_OK) || (res == CURLE_RECV_ERROR)) {
2387
2574
          bool retry = Curl_retry_request(conn, &newurl);
2388
2575
 
2389
 
          if(retry)
 
2576
          if(retry) {
 
2577
            res = CURLE_OK;
2390
2578
            follow = FOLLOW_RETRY;
2391
 
          else {
 
2579
            if (!newurl)
 
2580
              res = CURLE_OUT_OF_MEMORY;
 
2581
          }
 
2582
          else if (res == CURLE_OK) {
2392
2583
            /*
2393
2584
             * We must duplicate the new URL here as the connection data may
2394
2585
             * be free()ed in the Curl_done() function. We prefer the newurl
2398
2589
            if(data->req.newurl) {
2399
2590
              follow = FOLLOW_REDIR;
2400
2591
              newurl = strdup(data->req.newurl);
 
2592
              if (!newurl)
 
2593
                res = CURLE_OUT_OF_MEMORY;
2401
2594
            }
2402
2595
            else if(data->req.location) {
2403
2596
              follow = FOLLOW_FAKE;
2404
2597
              newurl = strdup(data->req.location);
 
2598
              if (!newurl)
 
2599
                res = CURLE_OUT_OF_MEMORY;
2405
2600
            }
2406
2601
          }
2407
2602
 
2408
2603
          /* in the above cases where 'newurl' gets assigned, we have a fresh
2409
2604
           * allocated memory pointed to */
2410
2605
        }
2411
 
        else {
 
2606
        if(res != CURLE_OK) {
2412
2607
          /* The transfer phase returned error, we mark the connection to get
2413
 
           * closed to prevent being re-used. This is becasue we can't
 
2608
           * closed to prevent being re-used. This is because we can't
2414
2609
           * possibly know if the connection is in a good shape or not now. */
2415
2610
          conn->bits.close = TRUE;
2416
2611
 
2476
2671
      failf(data, "%s", str);
2477
2672
  }
2478
2673
 
2479
 
  /* run post-transfer uncondionally, but don't clobber the return code if
 
2674
  /* run post-transfer unconditionally, but don't clobber the return code if
2480
2675
     we already have an error code recorder */
2481
2676
  res2 = Curl_posttransfer(data);
2482
2677
  if(!res && res2)
2535
2730
  if(k->getheader || !data->set.opt_no_body) {
2536
2731
 
2537
2732
    if(conn->sockfd != CURL_SOCKET_BAD) {
2538
 
      k->keepon |= KEEP_READ;
 
2733
      k->keepon |= KEEP_RECV;
2539
2734
    }
2540
2735
 
2541
2736
    if(conn->writesockfd != CURL_SOCKET_BAD) {
2561
2756
          k->exp100 = EXP100_SENDING_REQUEST;
2562
2757
 
2563
2758
        /* enable the write bit when we're not waiting for continue */
2564
 
        k->keepon |= KEEP_WRITE;
 
2759
        k->keepon |= KEEP_SEND;
2565
2760
      }
2566
2761
    } /* if(conn->writesockfd != CURL_SOCKET_BAD) */
2567
2762
  } /* if(k->getheader || !data->set.opt_no_body) */