Skip to content
Snippets Groups Projects
http.c 40.7 KiB
Newer Older
  • Learn to ignore specific revisions
  •                 (conn->bits.httpproxy && conn->allocptr.proxyuserpwd)?
                    conn->allocptr.proxyuserpwd:"",
    
                    conn->allocptr.userpwd?conn->allocptr.userpwd:"",
    
                    (conn->bits.use_range && conn->allocptr.rangeline)?
    
                    (data->set.useragent && *data->set.useragent && conn->allocptr.uagent)?
    
                    (conn->allocptr.cookie?conn->allocptr.cookie:""), /* Cookie: <data> */
                    (conn->allocptr.host?conn->allocptr.host:""), /* Host: host */
    
                    http->p_pragma?http->p_pragma:"",
                    http->p_accept?http->p_accept:"",
    
                    (data->set.encoding && *data->set.encoding && conn->allocptr.accept_encoding)?
                    conn->allocptr.accept_encoding:"", /* 08/28/02 jhrg */
    
                    (data->change.referer && conn->allocptr.ref)?conn->allocptr.ref:"" /* Referer: <data> <CRLF> */,
                    te
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    
        if(co) {
          int count=0;
    
          struct Cookie *store=co;
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          /* now loop through all cookies that matched */
          while(co) {
    
            if(co->value && strlen(co->value)) {
              if(0 == count) {
    
                add_bufferf(req_buffer, "Cookie: ");
    
              add_bufferf(req_buffer,
                          "%s%s=%s", count?"; ":"", co->name, co->value);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
            }
            co = co->next; /* next cookie please */
          }
          if(count) {
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          }
    
          Curl_cookie_freelist(store); /* free the cookie list */
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          co=NULL;
        }
    
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          struct tm *thistime;
    
    
          /* Phil Karn (Fri, 13 Apr 2001) pointed out that the If-Modified-Since
           * header family should have their times set in GMT as RFC2616 defines:
           * "All HTTP date/time stamps MUST be represented in Greenwich Mean Time
           * (GMT), without exception. For the purposes of HTTP, GMT is exactly
           * equal to UTC (Coordinated Universal Time)." (see page 20 of RFC2616).
           */
    
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    #ifdef HAVE_GMTIME_R
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          /* thread-safe version */
          struct tm keeptime;
    
          thistime = (struct tm *)gmtime_r(&data->set.timevalue, &keeptime);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    #else
    
          thistime = gmtime(&data->set.timevalue);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    #endif
          if(NULL == thistime) {
            failf(data, "localtime() failed!");
            return CURLE_OUT_OF_MEMORY;
          }
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    
    
    #ifdef HAVE_STRFTIME
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
    
          strftime(buf, BUFSIZE-1, "%a, %d %b %Y %H:%M:%S GMT", thistime);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    #else
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          /* TODO: Right, we *could* write a replacement here */
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          strcpy(buf, "no strftime() support");
    #endif
    
          case CURL_TIMECOND_IFMODSINCE:
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          default:
    
            add_bufferf(req_buffer,
                        "If-Modified-Since: %s\r\n", buf);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
            break;
    
          case CURL_TIMECOND_IFUNMODSINCE:
    
            add_bufferf(req_buffer,
                        "If-Unmodified-Since: %s\r\n", buf);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
            break;
    
          case CURL_TIMECOND_LASTMOD:
    
            add_bufferf(req_buffer,
                        "Last-Modified: %s\r\n", buf);
    
          char *ptr = strchr(headers->data, ':');
          if(ptr) {
            /* we require a colon for this to be a true header */
    
            ptr++; /* pass the colon */
    
              ptr++;
    
            if(*ptr) {
              /* only send this if the contents was non-blank */
    
    
              add_bufferf(req_buffer, "%s\r\n", headers->data);
    
          headers = headers->next;
    
        http->postdata = NULL;  /* nothing to post at this point */
        Curl_pgrsSetUploadSize(data, 0); /* upload size is 0 atm */
    
        switch(data->set.httpreq) {
    
        case HTTPREQ_POST_FORM:
    
          if(Curl_FormInit(&http->form, http->sendit)) {
    
            failf(data, "Internal HTTP POST error!");
    
            return CURLE_HTTP_POST_ERROR;
    
          /* set the read function to read from the generated form data */
          conn->fread = (curl_read_callback)Curl_FormReader;
          conn->fread_in = &http->form;
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    
    
          if(!conn->bits.upload_chunky)
            /* only add Content-Length if not uploading chunked */
            add_bufferf(req_buffer,
                        "Content-Length: %d\r\n", http->postsize);
    
          if(!checkheaders(data, "Expect:")) {
            /* if not disabled explicitly we add a Expect: 100-continue
               to the headers which actually speeds up post operations (as
               there is one packet coming back from the web server) */
            add_bufferf(req_buffer,
                        "Expect: 100-continue\r\n");
    
          if(!checkheaders(data, "Content-Type:")) {
    
            /* Get Content-Type: line from Curl_FormReadOneLine, which happens
               to always be the first line. We can know this for sure since
    
               we always build the formpost linked list the same way!
    
               The Content-Type header line also contains the MIME boundary
               string etc why disabling this header is likely to not make things
               work, but we support it anyway.
            */
            char contentType[256];
            int linelength=0;
    
            linelength = Curl_FormReadOneLine (contentType,
                                               sizeof(contentType),
                                               1,
                                               (FILE *)&http->form);
            if(linelength == -1) {
    
              failf(data, "Could not get Content-Type header line!");
    
              return CURLE_HTTP_POST_ERROR;
            }
            add_buffer(req_buffer, contentType, linelength);
          }
    
    
          /* make the request end in a true CRLF */
          add_buffer(req_buffer, "\r\n", 2);
    
    
          /* set upload size to the progress meter */
    
          Curl_pgrsSetUploadSize(data, http->postsize);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    
    
          /* fire away the whole request to the server */
    
          result = add_buffer_send(req_buffer, conn->firstsocket, conn, 
    
                                   &data->info.request_size);
          if(result)
            failf(data, "Failed sending POST request");
          else
            /* setup variables for the upcoming transfer */
            result = Curl_Transfer(conn, conn->firstsocket, -1, TRUE,
                                   &http->readbytecount,
                                   conn->firstsocket,
                                   &http->writebytecount);
    
            Curl_formclean(http->sendit); /* free that whole lot */
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          }
    
          break;
    
        case HTTPREQ_PUT: /* Let's PUT the data to the server! */
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    
    
          if((data->set.infilesize>0) && !conn->bits.upload_chunky)
            /* only add Content-Length if not uploading chunked */
    
                        "Content-Length: %d\r\n", /* file size */
    
          /* set the upload size to the progress meter */
    
          Curl_pgrsSetUploadSize(data, data->set.infilesize);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    
    
          /* this sends the buffer and frees all the buffer resources */
    
          result = add_buffer_send(req_buffer, conn->firstsocket, conn,
    
            failf(data, "Failed sending POST request");
    
          else
            /* prepare for transfer */
            result = Curl_Transfer(conn, conn->firstsocket, -1, TRUE,
                                   &http->readbytecount,
                                   conn->firstsocket,
                                   &http->writebytecount);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
          if(result)
            return result;
    
        case HTTPREQ_POST:
          /* this is the simple POST, using x-www-form-urlencoded style */
    
          /* store the size of the postfields */
          postsize = data->set.postfieldsize?
            data->set.postfieldsize:
            (data->set.postfields?strlen(data->set.postfields):0);
          
    
          if(!conn->bits.upload_chunky) {
            /* We only set Content-Length and allow a custom Content-Length if
               we don't upload data chunked, as RFC2616 forbids us to set both
               kinds of headers (Transfer-Encoding: chunked and Content-Length) */
    
            if(!checkheaders(data, "Content-Length:"))
              /* we allow replacing this header, although it isn't very wise to
                 actually set your own */
    
              add_bufferf(req_buffer, "Content-Length: %d\r\n", postsize);
    
          if(!checkheaders(data, "Content-Type:"))
            add_bufferf(req_buffer,
                        "Content-Type: application/x-www-form-urlencoded\r\n");
    
          add_buffer(req_buffer, "\r\n", 2);
    
    
            if(postsize < (100*1024)) {
              /* The post data is less than 100K, then append it to the header.
                 This limit is no magic limit but only set to prevent really huge
                 POSTs to get the data duplicated with malloc() and family. */
    
              if(!conn->bits.upload_chunky)
                /* We're not sending it 'chunked', append it to the request
                   already now to reduce the number if send() calls */
                add_buffer(req_buffer, data->set.postfields, postsize);
              else {
                /* Append the POST data chunky-style */
                add_bufferf(req_buffer, "%x\r\n", postsize);
                add_buffer(req_buffer, data->set.postfields, postsize);
                add_buffer(req_buffer, "\r\n0\r\n", 5); /* end of a chunked
                                                           transfer stream */
              }
    
              /* A huge POST coming up, do data separate from the request */
    
              http->postsize = postsize;
              http->postdata = data->set.postfields;
    
              conn->fread = (curl_read_callback)readmoredata;
              conn->fread_in = (void *)conn;
    
              /* set the upload size to the progress meter */
              Curl_pgrsSetUploadSize(data, http->postsize);
            }
    
          else
            /* set the upload size to the progress meter */
            Curl_pgrsSetUploadSize(data, data->set.infilesize);
    
          /* issue the request, headers-only */
    
          result = add_buffer_send(req_buffer, conn->firstsocket, conn,
    
                                   &data->info.request_size);
    
          if(result)
            failf(data, "Failed sending HTTP POST request");
    
              Curl_Transfer(conn, conn->firstsocket, -1, TRUE,
                            &http->readbytecount,
    
                            http->postdata?conn->firstsocket:-1,
                            http->postdata?&http->writebytecount:NULL);
    
          /* issue the request */
    
          result = add_buffer_send(req_buffer, conn->firstsocket, conn,
    
          if(result)
            failf(data, "Failed sending HTTP request");
          else
            /* HTTP GET/HEAD download: */
    
            result = Curl_Transfer(conn, conn->firstsocket, -1, TRUE,
                                   &http->readbytecount,
    
                                   http->postdata?conn->firstsocket:-1,
                                   http->postdata?&http->writebytecount:NULL);
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
        if(result)
          return result;
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    
    
      return CURLE_OK;
    
    Daniel Stenberg's avatar
    Daniel Stenberg committed
    }