Skip to content
url.c 55.2 KiB
Newer Older
Daniel Stenberg's avatar
Daniel Stenberg committed
   * full URL to be sure, and we need to make it at least 256 bytes since
   * other parts of the code will rely on this fact
   ***********************************************************/
#define LEAST_PATH_ALLOC 256
  urllen=strlen(data->url);
  if(urllen < LEAST_PATH_ALLOC)
    urllen=LEAST_PATH_ALLOC;
  
  conn->path=(char *)malloc(urllen);
  if(NULL == conn->path)
    return CURLE_OUT_OF_MEMORY; /* really bad error */

Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Parse the URL.
   *
   * We need to parse the url even when using the proxy, because we will need
   * the hostname and port in case we are trying to SSL connect through the
   * proxy -- and we don't know if we will need to use SSL until we parse the
   * url ...
   ************************************************************/
  if((2 == sscanf(data->url, "%64[^:]://%[^\n]",
                  conn->protostr,
                  conn->path)) && strequal(conn->protostr, "file")) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    /*
     * we deal with file://<host>/<path> differently since it supports no
     * hostname other than "localhost" and "127.0.0.1", which is unique among
     * the URL protocols specified in RFC 1738
     */

    if (strnequal(conn->path, "localhost/", 10) ||
        strnequal(conn->path, "127.0.0.1/", 10))
Daniel Stenberg's avatar
Daniel Stenberg committed
      /* If there's another host name than the one we support, <host>/ is
       * quietly ommitted */
      strcpy(conn->path, &conn->path[10]);

    strcpy(conn->protostr, "file"); /* store protocol string lowercase */
Daniel Stenberg's avatar
Daniel Stenberg committed
  }
  else {
    /* Set default host and default path */
Daniel Stenberg's avatar
Daniel Stenberg committed
    strcpy(conn->gname, "curl.haxx.se");
    strcpy(conn->path, "/");

    if (2 > sscanf(data->url,
                   "%64[^\n:]://%256[^\n/]%[^\n]",
                   conn->protostr, conn->gname, conn->path)) {
Daniel Stenberg's avatar
Daniel Stenberg committed
      
Daniel Stenberg's avatar
Daniel Stenberg committed
      /*
       * The URL was badly formatted, let's try the browser-style _without_
       * protocol specified like 'http://'.
       */
      if((1 > sscanf(data->url, "%256[^\n/]%[^\n]",
                     conn->gname, conn->path)) ) {
Daniel Stenberg's avatar
Daniel Stenberg committed
        /*
         * We couldn't even get this format.
         */
        failf(data, "<url> malformed");
        return CURLE_URL_MALFORMAT;
      }
Daniel Stenberg's avatar
Daniel Stenberg committed

      /*
       * Since there was no protocol part specified, we guess what protocol it
       * is based on the first letters of the server name.
       */

      if(strnequal(conn->gname, "FTP", 3)) {
        strcpy(conn->protostr, "ftp");
      }
      else if(strnequal(conn->gname, "GOPHER", 6))
        strcpy(conn->protostr, "gopher");
Daniel Stenberg's avatar
Daniel Stenberg committed
#ifdef USE_SSLEAY
      else if(strnequal(conn->gname, "HTTPS", 5))
        strcpy(conn->protostr, "https");
Daniel Stenberg's avatar
Daniel Stenberg committed
#endif /* USE_SSLEAY */
      else if(strnequal(conn->gname, "TELNET", 6))
        strcpy(conn->protostr, "telnet");
      else if (strnequal(conn->gname, "DICT", sizeof("DICT")-1))
        strcpy(conn->protostr, "DICT");
      else if (strnequal(conn->gname, "LDAP", sizeof("LDAP")-1))
        strcpy(conn->protostr, "LDAP");
        strcpy(conn->protostr, "http");
      }

      conn->protocol |= PROT_MISSING; /* not given in URL */
    }
  buf = data->buffer; /* this is our buffer */

  /*************************************************************
   * Set signal handler
   *************************************************************/
#ifdef HAVE_SIGACTION
  sigaction(SIGALRM, NULL, &sigact);
  sigact.sa_handler = alarmfunc;
#ifdef SA_RESTART
  /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
  sigact.sa_flags &= ~SA_RESTART;
#endif
  sigaction(SIGALRM, &sigact, NULL);
#else
  /* no sigaction(), revert to the much lamer signal() */
#ifdef HAVE_SIGNAL
  signal(SIGALRM, alarmfunc);
#endif

#endif

Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Take care of user and password authentication stuff
   *************************************************************/
Daniel Stenberg's avatar
Daniel Stenberg committed

  if(data->bits.user_passwd && !data->bits.use_netrc) {
    data->user[0] =0;
    data->passwd[0]=0;

    if(*data->userpwd != ':') {
      /* the name is given, get user+password */
      sscanf(data->userpwd, "%127[^:]:%127[^\n]",
             data->user, data->passwd);
    else
      /* no name given, get the password only */
      sscanf(data->userpwd+1, "%127[^\n]", data->passwd);

    /* check for password, if no ask for one */
    if( !data->passwd[0] ) {
      if(!data->fpasswd ||
         data->fpasswd(data->passwd_client,
                       "password:", data->passwd, sizeof(data->passwd)))
        return CURLE_BAD_PASSWORD_ENTERED;
Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Take care of proxy authentication stuff
   *************************************************************/
  if(data->bits.proxy_user_passwd) {
    data->proxyuser[0] =0;
    data->proxypasswd[0]=0;

    if(*data->proxyuserpwd != ':') {
      /* the name is given, get user+password */
      sscanf(data->proxyuserpwd, "%127[^:]:%127[^\n]",
             data->proxyuser, data->proxypasswd);
Daniel Stenberg's avatar
Daniel Stenberg committed
      }
    else
      /* no name given, get the password only */
      sscanf(data->proxyuserpwd+1, "%127[^\n]", data->proxypasswd);

    /* check for password, if no ask for one */
    if( !data->proxypasswd[0] ) {
      if(!data->fpasswd ||
         data->fpasswd( data->passwd_client,
                        "proxy password:",
                        data->proxypasswd,
                        sizeof(data->proxypasswd)))
        return CURLE_BAD_PASSWORD_ENTERED;
Daniel Stenberg's avatar
Daniel Stenberg committed
    }
Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Set a few convenience pointers 
   *************************************************************/
  conn->name = conn->gname;
  conn->ppath = conn->path;
  conn->hostname = conn->name;
Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Detect what (if any) proxy to use
   *************************************************************/
  if(!data->bits.httpproxy) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    /* If proxy was not specified, we check for default proxy environment
Daniel Stenberg's avatar
Daniel Stenberg committed
     * variables, to enable i.e Lynx compliance:
     *
     * http_proxy=http://some.server.dom:port/
     * https_proxy=http://some.server.dom:port/
     * ftp_proxy=http://some.server.dom:port/
     * gopher_proxy=http://some.server.dom:port/
     * no_proxy=domain1.dom,host.domain2.dom
     *   (a comma-separated list of hosts which should
     *   not be proxied, or an asterisk to override
     *   all proxy variables)
     * all_proxy=http://some.server.dom:port/
     *   (seems to exist for the CERN www lib. Probably
     *   the first to check for.)
     *
     * For compatibility, the all-uppercase versions of these variables are
     * checked if the lowercase versions don't exist.
     */
Daniel Stenberg's avatar
Daniel Stenberg committed
    char *proxy=NULL;
    char proxy_env[128];

    no_proxy=curl_getenv("no_proxy");
      no_proxy=curl_getenv("NO_PROXY");
Daniel Stenberg's avatar
Daniel Stenberg committed
    if(!no_proxy || !strequal("*", no_proxy)) {
      /* NO_PROXY wasn't specified or it wasn't just an asterisk */
      char *nope;

      nope=no_proxy?strtok(no_proxy, ", "):NULL;
      while(nope) {
        if(strlen(nope) <= strlen(conn->name)) {
Daniel Stenberg's avatar
Daniel Stenberg committed
          char *checkn=
            conn->name + strlen(conn->name) - strlen(nope);
Daniel Stenberg's avatar
Daniel Stenberg committed
          if(strnequal(nope, checkn, strlen(nope))) {
            /* no proxy for this host! */
            break;
          }
        }
	nope=strtok(NULL, ", ");
      }
      if(!nope) {
	/* It was not listed as without proxy */
	char *protop = conn->protostr;
Daniel Stenberg's avatar
Daniel Stenberg committed
	char *envp = proxy_env;
	char *prox;

	/* Now, build <protocol>_proxy and check for such a one to use */
	while(*protop)
	  *envp++ = tolower(*protop++);

	/* append _proxy */
	strcpy(envp, "_proxy");

Daniel Stenberg's avatar
Daniel Stenberg committed
	/* read the protocol proxy: */
Daniel Stenberg's avatar
Daniel Stenberg committed

	if(!prox) {
          /* There was no lowercase variable, try the uppercase version: */
	  for(envp = proxy_env; *envp; envp++)
	    *envp = toupper(*envp);
Daniel Stenberg's avatar
Daniel Stenberg committed
	if(prox && *prox) { /* don't count "" strings */
	  proxy = prox; /* use this */
	  proxy = curl_getenv("all_proxy"); /* default proxy to use */
Daniel Stenberg's avatar
Daniel Stenberg committed

        if(proxy && *proxy) {
          /* we have a proxy here to set */
          data->proxy = proxy;
          data->bits.proxystringalloc=1; /* this needs to be freed later */
          data->bits.httpproxy=1;
Daniel Stenberg's avatar
Daniel Stenberg committed
        }
      } /* if (!nope) - it wasn't specified non-proxy */
Daniel Stenberg's avatar
Daniel Stenberg committed
    } /* NO_PROXY wasn't specified or '*' */
    if(no_proxy)
      free(no_proxy);
Daniel Stenberg's avatar
Daniel Stenberg committed
  } /* if not using proxy */

Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * No protocol but proxy usage needs attention
   *************************************************************/
  if((conn->protocol&PROT_MISSING) && data->bits.httpproxy ) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    /* We're guessing prefixes here and since we're told to use a proxy, we
       need to add the protocol prefix to the URL string before we continue!
       */
    char *reurl;

    reurl = aprintf("%s://%s", conn->protostr, data->url);
Daniel Stenberg's avatar
Daniel Stenberg committed

    if(!reurl)
      return CURLE_OUT_OF_MEMORY;
Daniel Stenberg's avatar
Daniel Stenberg committed

    data->url = reurl;
    if(data->freethis)
      free(data->freethis);
    data->freethis = reurl;

    conn->protocol &= ~PROT_MISSING; /* switch that one off again */
Daniel Stenberg's avatar
Daniel Stenberg committed
  /************************************************************
   * RESUME on a HTTP page is a tricky business. First, let's just check that
   * 'range' isn't used, then set the range parameter and leave the resume as
   * it is to inform about this situation for later use. We will then
   * "attempt" to resume, and if we're talking to a HTTP/1.1 (or later)
   * server, we will get the document resumed. If we talk to a HTTP/1.0
   * server, we just fail since we can't rewind the file writing from within
   * this function.
   ***********************************************************/
Daniel Stenberg's avatar
Daniel Stenberg committed
  if(data->resume_from) {
    if(!data->bits.set_range) {
Daniel Stenberg's avatar
Daniel Stenberg committed
      /* if it already was in use, we just skip this */
Daniel Stenberg's avatar
Daniel Stenberg committed
      snprintf(resumerange, sizeof(resumerange), "%d-", data->resume_from);
      data->range=strdup(resumerange); /* tell ourselves to fetch this range */
      data->bits.rangestringalloc = TRUE; /* mark as allocated */
      data->bits.set_range = 1; /* switch on range usage */
Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Set timeout if that is being used
   *************************************************************/
Daniel Stenberg's avatar
Daniel Stenberg committed
  if(data->timeout) {
    /* We set the timeout on the connection/resolving phase first, separately
Daniel Stenberg's avatar
Daniel Stenberg committed
     * from the download/upload part to allow a maximum time on everything */
Daniel Stenberg's avatar
Daniel Stenberg committed
    myalarm(data->timeout); /* this sends a signal when the timeout fires
			       off, and that will abort system calls */
  }

Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Setup internals depending on protocol
   *************************************************************/
Daniel Stenberg's avatar
Daniel Stenberg committed

  if (strequal(conn->protostr, "HTTP")) {
    conn->port = (data->use_port && allow_port)?data->use_port:PORT_HTTP;
    conn->remote_port = PORT_HTTP;
    conn->protocol |= PROT_HTTP;
    conn->curl_do = Curl_http;
    conn->curl_done = Curl_http_done;
    conn->curl_close = Curl_http_close;
Daniel Stenberg's avatar
Daniel Stenberg committed
  }
  else if (strequal(conn->protostr, "HTTPS")) {
Daniel Stenberg's avatar
Daniel Stenberg committed
#ifdef USE_SSLEAY

    conn->port = (data->use_port && allow_port)?data->use_port:PORT_HTTPS;
    conn->remote_port = PORT_HTTPS;
    conn->protocol |= PROT_HTTP;
    conn->protocol |= PROT_HTTPS;

    conn->curl_do = Curl_http;
    conn->curl_done = Curl_http_done;
    conn->curl_connect = Curl_http_connect;
    conn->curl_close = Curl_http_close;
Daniel Stenberg's avatar
Daniel Stenberg committed
#else /* USE_SSLEAY */
    failf(data, "libcurl was built with SSL disabled, https: not supported!");
    return CURLE_UNSUPPORTED_PROTOCOL;
Daniel Stenberg's avatar
Daniel Stenberg committed
#endif /* !USE_SSLEAY */
  }
  else if (strequal(conn->protostr, "GOPHER")) {
    conn->port = (data->use_port && allow_port)?data->use_port:PORT_GOPHER;
    conn->remote_port = PORT_GOPHER;
Daniel Stenberg's avatar
Daniel Stenberg committed
    /* Skip /<item-type>/ in path if present */
    if (isdigit((int)conn->path[1])) {
      conn->ppath = strchr(&conn->path[1], '/');
      if (conn->ppath == NULL)
	conn->ppath = conn->path;
Daniel Stenberg's avatar
Daniel Stenberg committed
      }
    conn->protocol |= PROT_GOPHER;
    conn->curl_do = Curl_http;
    conn->curl_done = Curl_http_done;
    conn->curl_close = Curl_http_close;
Daniel Stenberg's avatar
Daniel Stenberg committed
  }
  else if(strequal(conn->protostr, "FTP")) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    char *type;
    conn->port = (data->use_port && allow_port)?data->use_port:PORT_FTP;
    conn->remote_port = PORT_FTP;
    conn->protocol |= PROT_FTP;
Daniel Stenberg's avatar
Daniel Stenberg committed

    if(data->bits.httpproxy &&
       !data->bits.tunnel_thru_httpproxy) {
      /* Unless we have asked to tunnel ftp operations through the proxy, we
         switch and use HTTP operations only */
      conn->curl_do = Curl_http;
      conn->curl_done = Curl_http_done;
      conn->curl_close = Curl_http_close;
      conn->curl_do = Curl_ftp;
      conn->curl_done = Curl_ftp_done;
      conn->curl_connect = Curl_ftp_connect;
Daniel Stenberg's avatar
Daniel Stenberg committed
      conn->curl_disconnect = Curl_ftp_disconnect;
    }

    conn->ppath++; /* don't include the initial slash */
Daniel Stenberg's avatar
Daniel Stenberg committed

    /* FTP URLs support an extension like ";type=<typecode>" that
Daniel Stenberg's avatar
Daniel Stenberg committed
     * we'll try to get now! */
    type=strstr(conn->ppath, ";type=");
Daniel Stenberg's avatar
Daniel Stenberg committed
    if(!type) {
      type=strstr(conn->gname, ";type=");
Daniel Stenberg's avatar
Daniel Stenberg committed
    }
    if(type) {
      char command;
      *type=0;
      command = toupper(type[6]);
      switch(command) {
      case 'A': /* ASCII mode */
	data->bits.ftp_ascii = 1;
Daniel Stenberg's avatar
Daniel Stenberg committed
	break;
      case 'D': /* directory mode */
	data->bits.ftp_list_only = 1;
Daniel Stenberg's avatar
Daniel Stenberg committed
	break;
      case 'I': /* binary mode */
      default:
	/* switch off ASCII */
	data->bits.ftp_ascii = 0;
  else if(strequal(conn->protostr, "TELNET")) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    /* telnet testing factory */
    conn->protocol |= PROT_TELNET;

    conn->port = (data->use_port && allow_port)?data->use_port: PORT_TELNET;
    conn->remote_port = PORT_TELNET;
    conn->curl_do = Curl_telnet;
    conn->curl_done = Curl_telnet_done;
Daniel Stenberg's avatar
Daniel Stenberg committed
  }
  else if (strequal(conn->protostr, "DICT")) {
    conn->protocol |= PROT_DICT;
    conn->port = (data->use_port && allow_port)?data->use_port:PORT_DICT;
    conn->remote_port = PORT_DICT;
    conn->curl_do = Curl_dict;
    conn->curl_done = Curl_dict_done;
Daniel Stenberg's avatar
Daniel Stenberg committed
  }
  else if (strequal(conn->protostr, "LDAP")) {
    conn->protocol |= PROT_LDAP;
    conn->port = (data->use_port && allow_port)?data->use_port:PORT_LDAP;
    conn->remote_port = PORT_LDAP;
    conn->curl_do = Curl_ldap;
    conn->curl_done = Curl_ldap_done;
Daniel Stenberg's avatar
Daniel Stenberg committed
  }
  else if (strequal(conn->protostr, "FILE")) {
    conn->protocol |= PROT_FILE;
Daniel Stenberg's avatar
Daniel Stenberg committed

    /* no done() function */
    /* anyway, this is supposed to be the connect function so we better
       at least check that the file is present here! */
    result = Curl_file_connect(conn);
    /* Setup a "faked" transfer that'll do nothing */
    if(CURLE_OK == result) {
      result = Curl_Transfer(conn, -1, -1, FALSE, NULL, /* no download */
                             -1, NULL); /* no upload */
    }

    return result;
Daniel Stenberg's avatar
Daniel Stenberg committed
  else {
Daniel Stenberg's avatar
Daniel Stenberg committed
    /* We fell through all checks and thus we don't support the specified
       protocol */
    failf(data, "Unsupported protocol: %s", conn->protostr);
    return CURLE_UNSUPPORTED_PROTOCOL;
Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * .netrc scanning coming up
   *************************************************************/
  if(data->bits.use_netrc) {
    if(Curl_parsenetrc(conn->hostname, data->user, data->passwd)) {
Daniel Stenberg's avatar
Daniel Stenberg committed
      infof(data, "Couldn't find host %s in the .netrc file, using defaults",
Daniel Stenberg's avatar
Daniel Stenberg committed
    }
    else
      data->bits.user_passwd = 1; /* enable user+password */

Daniel Stenberg's avatar
Daniel Stenberg committed
    /* weather we failed or not, we don't know which fields that were filled
       in anyway */
    if(!data->user[0])
      strcpy(data->user, CURL_DEFAULT_USER);
    if(!data->passwd[0])
      strcpy(data->passwd, CURL_DEFAULT_PASSWORD);
  }
  else if(!(data->bits.user_passwd) &&
	  (conn->protocol & (PROT_FTP|PROT_HTTP)) ) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    /* This is a FTP or HTTP URL, and we haven't got the user+password in
Daniel Stenberg's avatar
Daniel Stenberg committed
     * the extra parameter, we will now try to extract the possible
     * user+password pair in a string like:
     * ftp://user:password@ftp.my.site:8021/README */
Daniel Stenberg's avatar
Daniel Stenberg committed
    char *ptr=NULL; /* assign to remove possible warnings */
      /* there's a user+password given here, to the left of the @ */

      data->user[0] =0;
      data->passwd[0]=0;

      if(*conn->name != ':') {
        /* the name is given, get user+password */
        sscanf(conn->name, "%127[^:@]:%127[^@]",
               data->user, data->passwd);
      }
      else
        /* no name given, get the password only */
        sscanf(conn->name+1, "%127[^@]", data->passwd);

      if(data->user[0]) {
        char *newname=curl_unescape(data->user, 0);
        if(strlen(newname) < sizeof(data->user)) {
          strcpy(data->user, newname);
        }
        /* if the new name is longer than accepted, then just use
           the unconverted name, it'll be wrong but what the heck */
        free(newname);
      }

      /* check for password, if no ask for one */
      if( !data->passwd[0] ) {
        if(!data->fpasswd ||
           data->fpasswd(data->passwd_client,
                         "password:",data->passwd,sizeof(data->passwd)))
          return CURLE_BAD_PASSWORD_ENTERED;
      else {
        /* we have a password found in the URL, decode it! */
        char *newpasswd=curl_unescape(data->passwd, 0);
        if(strlen(newpasswd) < sizeof(data->passwd)) {
          strcpy(data->passwd, newpasswd);
        }
        free(newpasswd);
      }
      conn->name = ++ptr;
      data->bits.user_passwd=1; /* enable user+password */
Daniel Stenberg's avatar
Daniel Stenberg committed
    }
    else {
      strcpy(data->user, CURL_DEFAULT_USER);
      strcpy(data->passwd, CURL_DEFAULT_PASSWORD);
    }
  }

Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Figure out the remote port number
   *
   * No matter if we use a proxy or not, we have to figure out the remote
   * port number of various reasons.
   *
   * To be able to detect port number flawlessly, we must not confuse them
   * IPv6-specified addresses in the [0::1] style.
   *************************************************************/

  if((1 == sscanf(conn->name, "[%*39[0-9a-fA-F:]%c", &endbracket)) &&
     (']' == endbracket)) {
    /* this is a IPv6-style specified IP-address */
#ifndef ENABLE_IPV6
    failf(data, "You haven't enabled IPv6 support");
    return CURLE_URL_MALFORMAT;
#else
    tmp = strchr(conn->name, ']');

    tmp++; /* pass the ending bracket */
    if(':' != *tmp)
      tmp = NULL; /* no port number available */
#endif
  }
  else {
    /* traditional IPv4-style port-extracting */
    tmp = strchr(conn->name, ':');
  }

  if (tmp) {
    *tmp++ = '\0'; /* cut off the name there */
  if(data->bits.httpproxy) {
    /* If this is supposed to use a proxy, we need to figure out the proxy
       host name name, so that we can re-use an existing connection
       that may exist registered to the same proxy host. */

#ifdef ENABLE_IPV6
    failf(data, "proxy yet to be supported");
    return CURLE_OUT_OF_MEMORY;
#else
    char *prox_portno;
    char *endofprot;

    /* We need to make a duplicate of the proxy so that we can modify the
       string safely. */
    char *proxydup=strdup(data->proxy);

    /* We use 'proxyptr' to point to the proxy name from now on... */
    char *proxyptr=proxydup;

    if(NULL == proxydup) {
      failf(data, "memory shortage");
      return CURLE_OUT_OF_MEMORY;
    }

    /* Daniel Dec 10, 1998:
       We do the proxy host string parsing here. We want the host name and the
       port name. Accept a protocol:// prefix, even though it should just be
       ignored. */

    /* 1. skip the protocol part if present */
    endofprot=strstr(proxyptr, "://");
    if(endofprot) {
      proxyptr = endofprot+3;
    }

    /* allow user to specify proxy.server.com:1080 if desired */
    prox_portno = strchr (proxyptr, ':');
    if (prox_portno) {
      *prox_portno = 0x0; /* cut off number from host name */
      prox_portno ++;
      /* now set the local port number */
      conn->port = atoi(prox_portno);
    }
    else if(data->proxyport) {
      /* None given in the proxy string, then get the default one if it is
         given */
      conn->port = data->proxyport;
    }

    /* now, clone the cleaned proxy host name */
    conn->proxyhost = strdup(proxyptr);

    free(proxydup); /* free the duplicate pointer and not the modified */
#endif /* end of IPv4-section */
  }

  /*************************************************************
   * Check the current list of connections to see if we can
   * re-use an already existing one or if we have to create a
   * new one.
   *************************************************************/

  if(ConnectionExists(data, conn, &conn_temp)) {
    /*
     * We already have a connection for this, we got the former connection
     * in the conn_temp variable and thus we need to cleanup the one we
     * just allocated before we can move along and use the previously
     * existing one.
     */
    struct connectdata *old_conn = conn;
    char *path = old_conn->path; /* setup the current path pointer properly */
    if(old_conn->proxyhost)
      free(old_conn->proxyhost);
    conn = conn_temp;        /* use this connection from now on */
    free(conn->path);        /* free the previous path pointer */

    /* we need these pointers if we speak over a proxy */
    conn->name = old_conn->name;
    conn->hostname = old_conn->hostname;

    conn->path = path;       /* use this one */
    conn->ppath = path;      /* set this too */
Daniel Stenberg's avatar
Daniel Stenberg committed
    conn->maxdownload = 0;   /* might have been used previously! */
    conn->bits.reuse = TRUE; /* yes, we're re-using here */
    free(old_conn);          /* we don't need this anymore */

    *in_connect = conn;      /* return this instead! */

    infof(data, "Re-using existing connection! (#%d)\n", conn->connectindex);
  }
  else {
    /*
     * This is a brand new connection, so let's store it in the connection
     * cache of ours!
     */
    ConnectionStore(data, conn);
  }

Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Resolve the name of the server or proxy
   *************************************************************/
  if(!data->bits.httpproxy) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    /* If not connecting via a proxy, extract the port from the URL, if it is
     * there, thus overriding any defaults that might have been set above. */
    conn->port =  conn->remote_port; /* it is the same port */
    /* Resolve target host right on */
Daniel Stenberg's avatar
Daniel Stenberg committed
    if(!conn->hp) {
#ifdef ENABLE_IPV6
      /* it might already be set if reusing a connection */
      conn->hp = Curl_getaddrinfo(data, conn->name, conn->port);
      /* it might already be set if reusing a connection */
      conn->hp = Curl_gethost(data, conn->name, &conn->hostent_buf);
Daniel Stenberg's avatar
Daniel Stenberg committed
    }
    if(!conn->hp)
      failf(data, "Couldn't resolve host '%s'", conn->name);
      return CURLE_COULDNT_RESOLVE_HOST;
  else if(!conn->hp) {
    /* This is a proxy that hasn't been resolved yet. It may be resolved
       if we're reusing an existing connection. */
Daniel Stenberg's avatar
Daniel Stenberg committed

    /* resolve proxy */
    conn->hp = Curl_gethost(data, conn->proxyhost, &conn->hostent_buf);
Daniel Stenberg's avatar
Daniel Stenberg committed
    if(!conn->hp) {
      failf(data, "Couldn't resolve proxy '%s'", conn->proxyhost);
      return CURLE_COULDNT_RESOLVE_PROXY;
  Curl_pgrsTime(data, TIMER_NAMELOOKUP);
Daniel Stenberg's avatar
Daniel Stenberg committed

Daniel Stenberg's avatar
Daniel Stenberg committed
  /*************************************************************
   * Proxy authentication
   *************************************************************/
  if(data->bits.proxy_user_passwd) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    char *authorization;
Daniel Stenberg's avatar
Daniel Stenberg committed
    snprintf(data->buffer, BUFSIZE, "%s:%s",
             data->proxyuser, data->proxypasswd);
    if(Curl_base64_encode(data->buffer, strlen(data->buffer),
                          &authorization) >= 0) {
      if(conn->allocptr.proxyuserpwd)
        free(conn->allocptr.proxyuserpwd);
      conn->allocptr.proxyuserpwd =
        aprintf("Proxy-authorization: Basic %s\015\012", authorization);
Daniel Stenberg's avatar
Daniel Stenberg committed
      free(authorization);
    }
Daniel Stenberg's avatar
Daniel Stenberg committed
  }
Daniel Stenberg's avatar
Daniel Stenberg committed

  /*************************************************************
   * Send user-agent to HTTP proxies even if the target protocol
   * isn't HTTP.
   *************************************************************/
  if((conn->protocol&PROT_HTTP) || data->bits.httpproxy) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    if(data->useragent) {
      if(conn->allocptr.uagent)
        free(conn->allocptr.uagent);
      conn->allocptr.uagent =
        aprintf("User-Agent: %s\015\012", data->useragent);
Daniel Stenberg's avatar
Daniel Stenberg committed
  if(-1 == conn->firstsocket) {
    /* Connect only if not already connected! */
    result = ConnectPlease(data, conn);
    if(CURLE_OK != result)
      return result;

    if(conn->curl_connect) {
      /* is there a connect() procedure? */
Daniel Stenberg's avatar
Daniel Stenberg committed
      /* set start time here for timeout purposes in the
       * connect procedure, it is later set again for the
       * progress meter purpose */
      conn->now = Curl_tvnow();
Daniel Stenberg's avatar
Daniel Stenberg committed
      /* Call the protocol-specific connect function */
      result = conn->curl_connect(conn);
      if(result != CURLE_OK)
        return result; /* pass back errors */
    }
Daniel Stenberg's avatar
Daniel Stenberg committed
  }
  Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected */
Daniel Stenberg's avatar
Daniel Stenberg committed

  conn->now = Curl_tvnow(); /* time this *after* the connect is done */
  conn->bytecount = 0;
Daniel Stenberg's avatar
Daniel Stenberg committed
  
Daniel Stenberg's avatar
Daniel Stenberg committed
  /* Figure out the ip-number and display the first host name it shows: */
#ifdef ENABLE_IPV6
  {
    char hbuf[NI_MAXHOST];
#ifdef NI_WITHSCOPEID
    const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
#else
    const int niflags = NI_NUMERICHOST;
#endif
Daniel Stenberg's avatar
Daniel Stenberg committed
    struct addrinfo *ai = conn->ai;

    if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0,
	niflags)) {
      snprintf(hbuf, sizeof(hbuf), "?");
    }
    if (ai->ai_canonname) {
      infof(data, "Connected to %s (%s)\n", ai->ai_canonname, hbuf);
    } else {
      infof(data, "Connected to %s\n", hbuf);
    }
  }
#else
Daniel Stenberg's avatar
Daniel Stenberg committed
  {
    struct in_addr in;
    (void) memcpy(&in.s_addr, *conn->hp->h_addr_list, sizeof (in.s_addr));
    infof(data, "Connected to %s (%s)\n", conn->hp->h_name, inet_ntoa(in));
Daniel Stenberg's avatar
Daniel Stenberg committed
  }
Daniel Stenberg's avatar
Daniel Stenberg committed

#ifdef __EMX__
  /* 20000330 mgs
   * the check is quite a hack...
   * we're calling _fsetmode to fix the problem with fwrite converting newline
   * characters (you get mangled text files, and corrupted binary files when
   * you download to stdout and redirect it to a file). */

  if ((data->out)->_handle == NULL) {
    _fsetmode(stdout, "b");
  }
#endif

CURLcode Curl_connect(struct UrlData *data,
                      struct connectdata **in_connect,
{
  CURLcode code;
  struct connectdata *conn;

  /* call the stuff that needs to be called */
  code = Connect(data, in_connect, allow_port);

  if(CURLE_OK != code) {
    /* We're not allowed to return failure with memory left allocated
       in the connectdata struct, free those here */
    conn = (struct connectdata *)*in_connect;
    if(conn) {
      int index;
      index = conn->connectindex; /* get the index */
      Curl_disconnect(conn);      /* close the connection */
      if(-1 != index)
        data->connects[index]=NULL; /* clear the pointer */
CURLcode Curl_done(struct connectdata *conn)
{
  struct UrlData *data;
  CURLcode result;

  data = conn->data;

  /* this calls the protocol-specific function pointer previously set */
  if(conn->curl_done)
    result = conn->curl_done(conn);
  else
    result = CURLE_OK;

  Curl_pgrsDone(data); /* done with the operation */
  /* if bits.close is TRUE, it means that the connection should be closed
     in spite of all our efforts to be nice */
  if((CURLE_OK == result) && conn->bits.close) {
    index = conn->connectindex;     /* get the index */
    result = Curl_disconnect(conn); /* close the connection */
    data->connects[index]=NULL;     /* clear the pointer */
  }

CURLcode Curl_do(struct connectdata *conn)
  CURLcode result=CURLE_OK;
    /* generic protocol-specific function pointer set in curl_connect() */
    result = conn->curl_do(conn);