~ubuntu-branches/ubuntu/quantal/curl/quantal-updates

« back to all changes in this revision

Viewing changes to lib/easy.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-04-02 23:35:45 UTC
  • mto: (1.2.1 upstream) (3.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20090402233545-geixkwhe3izccjt7
Tags: upstream-7.19.4
ImportĀ upstreamĀ versionĀ 7.19.4

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: easy.c,v 1.120 2008-05-12 21:43:29 bagder Exp $
 
21
 * $Id: easy.c,v 1.133 2009-01-29 20:32:31 yangtse Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
61
61
#ifdef HAVE_SYS_IOCTL_H
62
62
#include <sys/ioctl.h>
63
63
#endif
64
 
#include <signal.h>
65
64
 
66
65
#ifdef HAVE_SYS_PARAM_H
67
66
#include <sys/param.h>
83
82
#include "easyif.h"
84
83
#include "select.h"
85
84
#include "sendf.h" /* for failf function prototype */
 
85
#include "http_ntlm.h"
86
86
#include "connect.h" /* for Curl_getconnectinfo */
87
87
 
88
88
#define _MPRINTF_REPLACE /* use our functions only */
103
103
/* The last #include file should be: */
104
104
#include "memdebug.h"
105
105
 
106
 
#ifdef USE_WINSOCK
107
106
/* win32_cleanup() is for win32 socket cleanup functionality, the opposite
108
107
   of win32_init() */
109
108
static void win32_cleanup(void)
110
109
{
 
110
#ifdef USE_WINSOCK
111
111
  WSACleanup();
 
112
#endif
 
113
#ifdef USE_WINDOWS_SSPI
 
114
  Curl_sspi_global_cleanup();
 
115
#endif
112
116
}
113
117
 
114
118
/* win32_init() performs win32 socket initialization to properly setup the
115
119
   stack to allow networking */
116
120
static CURLcode win32_init(void)
117
121
{
 
122
#ifdef USE_WINSOCK
118
123
  WORD wVersionRequested;
119
124
  WSADATA wsaData;
120
 
  int err;
 
125
  int res;
121
126
 
122
127
#if defined(ENABLE_IPV6) && (USE_WINSOCK < 2)
123
128
  Error IPV6_requires_winsock2
125
130
 
126
131
  wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
127
132
 
128
 
  err = WSAStartup(wVersionRequested, &wsaData);
 
133
  res = WSAStartup(wVersionRequested, &wsaData);
129
134
 
130
 
  if(err != 0)
 
135
  if(res != 0)
131
136
    /* Tell the user that we couldn't find a useable */
132
137
    /* winsock.dll.     */
133
138
    return CURLE_FAILED_INIT;
147
152
    return CURLE_FAILED_INIT;
148
153
  }
149
154
  /* The Windows Sockets DLL is acceptable. Proceed. */
 
155
#endif
 
156
 
 
157
#ifdef USE_WINDOWS_SSPI
 
158
  {
 
159
    CURLcode err = Curl_sspi_global_init();
 
160
    if (err != CURLE_OK)
 
161
      return err;
 
162
  }
 
163
#endif
 
164
 
150
165
  return CURLE_OK;
151
166
}
152
167
 
153
 
#else
154
 
/* These functions exist merely to prevent compiler warnings */
155
 
static CURLcode win32_init(void) { return CURLE_OK; }
156
 
static void win32_cleanup(void) { }
157
 
#endif
158
 
 
159
168
#ifdef USE_LIBIDN
160
169
/*
161
170
 * Initialise use of IDNA library.
195
204
#define system_strdup strdup
196
205
#endif
197
206
 
 
207
#if defined(_MSC_VER) && defined(_DLL)
 
208
#  pragma warning(disable:4232) /* MSVC extension, dllimport identity */
 
209
#endif
 
210
 
198
211
#ifndef __SYMBIAN32__
199
212
/*
200
213
 * If a memory-using function (like curl_getenv) is used before
217
230
curl_calloc_callback Curl_ccalloc;
218
231
#endif
219
232
 
 
233
#if defined(_MSC_VER) && defined(_DLL)
 
234
#  pragma warning(default:4232) /* MSVC extension, dllimport identity */
 
235
#endif
 
236
 
220
237
/**
221
238
 * curl_global_init() globally initializes cURL given a bitwise set of the
222
239
 * different features of what to initialize.
485
502
      /* global dns cache was requested but still isn't */
486
503
      struct curl_hash *ptr;
487
504
 
488
 
      if(data->dns.hostcachetype == HCACHE_PRIVATE)
 
505
      if(data->dns.hostcachetype == HCACHE_PRIVATE) {
489
506
        /* if the current cache is private, kill it first */
490
507
        Curl_hash_destroy(data->dns.hostcache);
 
508
        data->dns.hostcachetype = HCACHE_NONE;
 
509
        data->dns.hostcache = NULL;
 
510
      }
491
511
 
492
512
      ptr = Curl_global_host_cache_init();
493
513
      if(ptr) {
582
602
  bool fail = TRUE;
583
603
  struct SessionHandle *data=(struct SessionHandle *)incurl;
584
604
 
585
 
  struct SessionHandle *outcurl = (struct SessionHandle *)
586
 
    calloc(sizeof(struct SessionHandle), 1);
 
605
  struct SessionHandle *outcurl = calloc(sizeof(struct SessionHandle), 1);
587
606
 
588
607
  if(NULL == outcurl)
589
608
    return NULL; /* failure */
595
614
     * get setup on-demand in the code, as that would probably decrease
596
615
     * the likeliness of us forgetting to init a buffer here in the future.
597
616
     */
598
 
    outcurl->state.headerbuff=(char*)malloc(HEADERSIZE);
 
617
    outcurl->state.headerbuff = malloc(HEADERSIZE);
599
618
    if(!outcurl->state.headerbuff) {
600
619
      break;
601
620
    }
605
624
    if(Curl_dupset(outcurl, data) != CURLE_OK)
606
625
      break;
607
626
 
608
 
    if(data->state.used_interface == Curl_if_multi)
609
 
      outcurl->state.connc = data->state.connc;
610
 
    else
611
 
      outcurl->state.connc = Curl_mk_connc(CONNCACHE_PRIVATE, -1);
612
 
 
613
 
    if(!outcurl->state.connc)
614
 
      break;
 
627
    /* the connection cache is setup on demand */
 
628
    outcurl->state.connc = NULL;
615
629
 
616
630
    outcurl->state.lastconnect = -1;
617
631
 
708
722
  /* zero out UserDefined data: */
709
723
  Curl_freeset(data);
710
724
  memset(&data->set, 0, sizeof(struct UserDefined));
 
725
  (void)Curl_init_userdefined(&data->set);
711
726
 
712
727
  /* zero out Progress data: */
713
728
  memset(&data->progress, 0, sizeof(struct Progress));
715
730
  /* init Handle data */
716
731
  Curl_easy_initHandleData(data);
717
732
 
718
 
  /* The remainder of these calls have been taken from Curl_open() */
719
 
 
720
 
  data->set.out = stdout; /* default output to stdout */
721
 
  data->set.in  = stdin;  /* default input from stdin */
722
 
  data->set.err  = stderr;  /* default stderr to stderr */
723
 
 
724
 
  /* use fwrite as default function to store output */
725
 
  data->set.fwrite_func = (curl_write_callback)fwrite;
726
 
 
727
 
  /* use fread as default function to read input */
728
 
  data->set.fread_func = (curl_read_callback)fread;
729
 
 
730
 
  data->set.infilesize = -1;      /* we don't know any size */
731
 
  data->set.postfieldsize = -1;   /* unknown size */
732
 
  data->set.maxredirs = -1;       /* allow any amount by default */
 
733
  data->progress.flags |= PGRS_HIDE;
733
734
  data->state.current_speed = -1; /* init to negative == impossible */
734
 
 
735
 
  data->set.httpreq = HTTPREQ_GET; /* Default HTTP request */
736
 
  data->set.ftp_use_epsv = TRUE;   /* FTP defaults to EPSV operations */
737
 
  data->set.ftp_use_eprt = TRUE;   /* FTP defaults to EPRT operations */
738
 
 
739
 
  data->set.dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
740
 
 
741
 
  /* make libcurl quiet by default: */
742
 
  data->set.hide_progress = TRUE;  /* CURLOPT_NOPROGRESS changes these */
743
 
  data->progress.flags |= PGRS_HIDE;
744
 
 
745
 
  /* Set the default size of the SSL session ID cache */
746
 
  data->set.ssl.numsessions = 5;
747
 
 
748
 
  data->set.proxyport = CURL_DEFAULT_PROXY_PORT; /* from url.h */
749
 
  data->set.proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
750
 
  data->set.httpauth = CURLAUTH_BASIC;  /* defaults to basic */
751
 
  data->set.proxyauth = CURLAUTH_BASIC; /* defaults to basic */
752
 
 
753
 
  /*
754
 
   * libcurl 7.10 introduced SSL verification *by default*! This needs to be
755
 
   * switched off unless wanted.
756
 
   */
757
 
  data->set.ssl.verifypeer = TRUE;
758
 
  data->set.ssl.verifyhost = 2;
759
 
  /* This is our prefered CA cert bundle/path since install time */
760
 
#if defined(CURL_CA_BUNDLE)
761
 
  (void) curl_easy_setopt(curl, CURLOPT_CAINFO, (char *) CURL_CA_BUNDLE);
762
 
#elif defined(CURL_CA_PATH)
763
 
  (void) curl_easy_setopt(curl, CURLOPT_CAPATH, (char *) CURL_CA_PATH);
764
 
#endif
765
 
 
766
 
  data->set.ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
767
 
                                                      type */
768
 
  data->set.new_file_perms = 0644;    /* Default permissions */
769
 
  data->set.new_directory_perms = 0755; /* Default permissions */
770
735
}
771
736
 
772
737
/*
803
768
       return PAUSE again and then we'll get a new copy allocted and stored in
804
769
       the tempwrite variables */
805
770
    char *tempwrite = data->state.tempwrite;
 
771
    char *freewrite = tempwrite; /* store this pointer to free it later */
806
772
    size_t tempsize = data->state.tempwritesize;
807
773
    int temptype = data->state.tempwritetype;
808
774
    size_t chunklen;
827
793
 
828
794
      result = Curl_client_write(data->state.current_conn,
829
795
                                 temptype, tempwrite, chunklen);
830
 
      if(!result)
 
796
      if(result)
831
797
        /* failures abort the loop at once */
832
798
        break;
833
799
 
834
800
      if(data->state.tempwrite && (tempsize - chunklen)) {
835
801
        /* Ouch, the reading is again paused and the block we send is now
836
802
           "cached". If this is the final chunk we can leave it like this, but
837
 
           if we have more chunks that is cached after this, we need to free
 
803
           if we have more chunks that are cached after this, we need to free
838
804
           the newly cached one and put back a version that is truly the entire
839
805
           contents that is saved for later
840
806
        */
841
807
        char *newptr;
842
808
 
843
 
        free(data->state.tempwrite); /* free the one just cached as it isn't
844
 
                                        enough */
845
 
 
846
809
        /* note that tempsize is still the size as before the callback was
847
810
           used, and thus the whole piece of data to keep */
848
 
        newptr = malloc(tempsize);
 
811
        newptr = realloc(data->state.tempwrite, tempsize);
 
812
 
849
813
        if(!newptr) {
 
814
          free(data->state.tempwrite); /* free old area */
 
815
          data->state.tempwrite = NULL;
850
816
          result = CURLE_OUT_OF_MEMORY;
851
817
          /* tempwrite will be freed further down */
852
818
          break;
864
830
 
865
831
    } while((result == CURLE_OK) && tempsize);
866
832
 
867
 
    free(tempwrite); /* this is unconditionally no longer used */
 
833
    free(freewrite); /* this is unconditionally no longer used */
868
834
  }
869
835
 
870
836
  return result;
949
915
            rc, curl_easy_strerror(rc));
950
916
    }
951
917
    return(rc);
952
 
  } else {
 
918
  }
 
919
  else {
953
920
#ifdef HAVE_ICONV
954
921
    /* do the translation ourselves */
955
922
    char *input_ptr, *output_ptr;
964
931
        error = ERRNO;
965
932
        failf(data,
966
933
              "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
967
 
               CURL_ICONV_CODESET_OF_HOST,
968
 
               CURL_ICONV_CODESET_OF_NETWORK,
969
 
               error, strerror(error));
 
934
              CURL_ICONV_CODESET_OF_HOST,
 
935
              CURL_ICONV_CODESET_OF_NETWORK,
 
936
              error, strerror(error));
970
937
        return CURLE_CONV_FAILED;
971
938
      }
972
939
    }
978
945
    if((rc == ICONV_ERROR) || (in_bytes != 0)) {
979
946
      error = ERRNO;
980
947
      failf(data,
981
 
        "The Curl_convert_from_network iconv call failed with errno %i: %s",
982
 
             error, strerror(error));
 
948
            "The Curl_convert_from_network iconv call failed with errno %i: %s",
 
949
            error, strerror(error));
983
950
      return CURLE_CONV_FAILED;
984
951
    }
985
952
#else
1025
992
        error = ERRNO;
1026
993
        failf(data,
1027
994
              "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
1028
 
               CURL_ICONV_CODESET_OF_HOST,
1029
 
               CURL_ICONV_CODESET_FOR_UTF8,
1030
 
               error, strerror(error));
 
995
              CURL_ICONV_CODESET_OF_HOST,
 
996
              CURL_ICONV_CODESET_FOR_UTF8,
 
997
              error, strerror(error));
1031
998
        return CURLE_CONV_FAILED;
1032
999
      }
1033
1000
    }
1039
1006
    if((rc == ICONV_ERROR) || (in_bytes != 0)) {
1040
1007
      error = ERRNO;
1041
1008
      failf(data,
1042
 
        "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
1043
 
             error, strerror(error));
 
1009
            "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
 
1010
            error, strerror(error));
1044
1011
      return CURLE_CONV_FAILED;
1045
1012
    }
1046
1013
    if(output_ptr < input_ptr) {