Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 447 lines (375 sloc) 15.779 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
/*
Copyright (c) 2009 Eucalyptus Systems, Inc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, only version 3 of the License.
This file is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
Please contact Eucalyptus Systems, Inc., 130 Castilian
Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
if you need additional information or have any questions.

This file may incorporate work covered under the following copyright and
permission notice:

Software License Agreement (BSD License)

Copyright (c) 2008, Regents of the University of California

Redistribution and use of this software in source and binary forms, with
or without modification, are permitted provided that the following
conditions are met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
ANY SUCH LICENSES OR RIGHTS.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h> // close, stat
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h> // open
#include <ctype.h> // tolower, isdigit
#include <sys/types.h> // stat
#include <sys/stat.h> // stat
#include <curl/curl.h>
#include <curl/easy.h>

#ifndef _UNIT_TEST // http_ functions aren't part of the unit test
#include "config.h"
#include "eucalyptus.h"
#include "misc.h"
#include "http.h"

#define TOTAL_RETRIES 3 /* download is retried in case of connection problems */
#define FIRST_TIMEOUT 4 /* in seconds, goes in powers of two afterwards */
#define STRSIZE 245 /* for short strings: files, hosts, URLs */

static size_t read_data (char *bufptr, size_t size, size_t nitems, void *userp);

struct read_request {
FILE * fp; /* input file pointer to be used by curl READERs */
    long long total_read; /* bytes written during the operation */
    long long total_calls; /* write calls made during the operation */
    time_t timestamp; // timestamp for periodically printing progress messages
    long long file_size; // file size in bytes, to print in progress messages
};

struct write_request {
FILE * fp; /* output file pointer to be used by curl WRITERs */
    long long total_wrote; /* bytes written during the operation */
    long long total_calls; /* write calls made during the operation */
#if defined (CAN_GZIP)
z_stream strm; /* stream struct used by zlib */
int ret; /* return value of last inflate() call */
#endif
};

static int curl_initialized = 0;

int http_put (const char * file_path, const char * url, const char * login, const char * password)
{
    int code = ERROR;

    if (curl_initialized!=1) {
        curl_global_init(CURL_GLOBAL_SSL);
        curl_initialized = 1;
    }

    struct stat64 mystat;
    if (stat64 (file_path, &mystat)) {
        logprintfl (EUCAERROR, "http_put(): failed to stat %s\n", file_path);
return code;
    }
    if (!S_ISREG(mystat.st_mode)) {
        logprintfl (EUCAERROR, "http_put(): %s is not a regular file\n", file_path);
return code;
    }

FILE * fp = fopen64 (file_path, "r");
if (fp==NULL) {
logprintfl (EUCAERROR, "http_put(): failed to open %s for reading\n", file_path);
return code;
}

CURL * curl;
CURLcode result;
curl = curl_easy_init ();
if (curl==NULL) {
logprintfl (EUCAERROR, "http_put(): could not initialize libcurl\n");
fclose (fp);
return code;
}

    logprintfl (EUCAINFO, "http_put(): uploading %s\n", file_path);
    logprintfl (EUCAINFO, " to %s\n", url);

char error_msg [CURL_ERROR_SIZE];
curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, error_msg);
curl_easy_setopt (curl, CURLOPT_URL, url);
    curl_easy_setopt (curl, CURLOPT_UPLOAD, 1L);
    curl_easy_setopt (curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)mystat.st_size);
    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L); // TODO: make this optional?
    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
    curl_easy_setopt (curl, CURLOPT_LOW_SPEED_LIMIT, 360L); // must have at least a 360 baud modem
    curl_easy_setopt (curl, CURLOPT_LOW_SPEED_TIME, 10L); // abort if below speed limit for this many seconds

    if (login!=NULL && password!=NULL) {
        char userpwd [STRSIZE];
        snprintf (userpwd, STRSIZE, "%s:%s", login, password);
        curl_easy_setopt (curl, CURLOPT_USERPWD, userpwd);
    }

struct read_request params;
    params.fp = fp;
    params.timestamp = time(NULL);
    params.file_size = (long long)mystat.st_size;
    curl_easy_setopt (curl, CURLOPT_READDATA, &params);
    curl_easy_setopt (curl, CURLOPT_READFUNCTION, read_data);

    int retries = TOTAL_RETRIES;
    int timeout = FIRST_TIMEOUT;
    do {
        params.total_read = 0L;
        params.total_calls = 0L;
        result = curl_easy_perform (curl); /* do it */
        logprintfl (EUCADEBUG, "http_put(): uploaded %ld bytes in %ld sends\n", params.total_read, params.total_calls);

        if (result) { // curl error (connection or transfer failed)
            logprintfl (EUCAERROR, "http_put(): %s (%d)\n", error_msg, result);

        } else {
            long httpcode;
            curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &httpcode);
            // TODO: pull out response message, too?
            
            switch (httpcode) {
            case 200L: // all good
                logprintfl (EUCAINFO, "http_put(): file updated sucessfully\n");
                code = OK;
                break;
            case 201L: // all good, created
                logprintfl (EUCAINFO, "http_put(): file created sucessfully\n");
                code = OK;
                break;
            case 408L: // timeout, retry
                logprintfl (EUCAWARN, "http_put(): server responded with HTTP code %ld (timeout)\n", httpcode);
                break;
case 500L: // internal server error (could be a fluke, so we'll retry)
logprintfl (EUCAWARN, "http_put(): server responded with HTTP code %ld (transient?)\n", httpcode);
                break;
            default: // some kind of error, will not retry
                logprintfl (EUCAERROR, "http_put(): server responded with HTTP code %ld\n", httpcode);
                retries = 0;
            }
        }

        if (code!=OK && retries > 0) {
            logprintfl (EUCAERROR, " upload retry %d of %d will commence in %d seconds\n", TOTAL_RETRIES-retries+1, TOTAL_RETRIES, timeout);
            sleep (timeout);
            fseek (fp, 0L, SEEK_SET);
            timeout <<= 1;
        }

        retries--;
    } while (code!=OK && retries>0);
    fclose (fp);
    
curl_easy_cleanup (curl);
    return code;
}

/* libcurl read handler */
static size_t read_data (char *buffer, size_t size, size_t nitems, void *params)
{
    assert (params != NULL);

    FILE * fp = ((struct read_request *)params)->fp;
    int items_read = 0;
    do {
        items_read += fread (buffer, size, nitems-items_read, fp);
    } while (items_read!=nitems && !feof(fp));
        
    ((struct read_request *)params)->total_read += items_read * size;
    ((struct read_request *)params)->total_calls++;

    if (((struct read_request *)params)->total_calls%50==0) {
        time_t prev = ((struct read_request *)params)->timestamp;
        time_t now = time(NULL);
        if ((now-prev)>10) {
            ((struct read_request *)params)->timestamp = now;
            long long bytes_read = ((struct read_request *)params)->total_read;
            long long bytes_file = ((struct read_request *)params)->file_size;
            int percent = (int)((bytes_read*100)/bytes_file);
            logprintfl (EUCADEBUG, "http_put(): upload progress %ld/%ld bytes (%d%%)\n", bytes_read, bytes_file, percent);
        }
    }

    return items_read;
}

/* libcurl write handler */
static size_t write_data (void *buffer, size_t size, size_t nmemb, void *params)
{
assert (params !=NULL);
FILE * fp = ((struct write_request *)params)->fp;
int wrote = fwrite (buffer, size, nmemb, fp);
    ((struct write_request *)params)->total_wrote += wrote;
    ((struct write_request *)params)->total_calls++;

return wrote;
}

#endif

// converts hex character to integer
static char hch_to_int (char ch) {
  return isdigit (ch) ? (ch - '0') : (10 + tolower (ch) - 'a');
}

// converts integer to hex character
static char int_to_hch (char i) {
  static char hex[] = "0123456789ABCDEF";
  return hex [i & 15];
}

// converts a string to url-encoded string (which must be freed)
char * url_encode (const char * unencoded) {
  char * encoded = malloc (strlen (unencoded) * 3 + 1);
  if (encoded==NULL) return NULL;

  const char * pu = unencoded;
  char * pe = encoded;
  while (*pu) {
    if (isalnum (*pu)
|| *pu == '-'
|| *pu == '_'
|| *pu == '.'
|| *pu == '~')
      *pe++ = *pu;
    else if (*pu == ' ')
      *pe++ = '+';
    else {
      *pe++ = '%';
      *pe++ = int_to_hch (*pu >> 4);
      *pe++ = int_to_hch (*pu & 15);
    }
    pu++;
  }
  *pe = '\0';

  return encoded;
}

// converts a url-encoded string to regular (which must be freed)
char * url_decode (const char * encoded) {
  char * unencoded = malloc (strlen (encoded) + 1);
  if (unencoded==NULL) return NULL;
  
  const char * pe = encoded;
  char * pu = unencoded;
  while (*pe) {
    if (*pe == '%') {
      if (pe[1] && pe[2]) {
        *pu++ = hch_to_int (pe[1]) << 4 | hch_to_int (pe[2]);
        pe += 2;
      }
    } else if (*pe == '+') {
      *pu++ = ' ';
    } else {
      *pu++ = *pe;
    }
    pe++;
  }
  *pu = '\0';
  
  return unencoded;
}

int http_get (const char * url, const char * outfile)
{
  return(http_get_timeout(url, outfile, TOTAL_RETRIES, FIRST_TIMEOUT, 0, 0));
}

int http_get_timeout (const char * url, const char * outfile, int total_retries, int first_timeout, int connect_timeout, int total_timeout)
{
int code = ERROR;

logprintfl (EUCAINFO, "http_get(): downloading %s\n", outfile);
logprintfl (EUCAINFO, " from %s\n", url);

/* isolate the PATH in the URL as it will be needed for signing */
if (strncasecmp (url, "http://", 7)!=0) {
logprintfl (EUCAERROR, "http_get(): URL must start with http://...\n");
return code;
}

FILE * fp = fopen64 (outfile, "w");
if (fp==NULL) {
logprintfl (EUCAERROR, "http_get(): failed to open %s for writing\n", outfile);
return code;
}

CURL * curl;
CURLcode result;
curl = curl_easy_init ();
if (curl==NULL) {
logprintfl (EUCAERROR, "http_get(): could not initialize libcurl\n");
fclose(fp);
return code;
}

char error_msg [CURL_ERROR_SIZE];
curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, error_msg);
curl_easy_setopt (curl, CURLOPT_URL, url);
// curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, write_header);

        curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L);

/* set up the default write function, but possibly override it below, if compression is desired and possible */
struct write_request params;
params.fp = fp;
curl_easy_setopt (curl, CURLOPT_WRITEDATA, &params);
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data);

if (connect_timeout > 0) {
curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, connect_timeout);
}
if (total_timeout > 0) {
curl_easy_setopt (curl, CURLOPT_TIMEOUT, total_timeout);
}
// curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers); /* register headers */

        logprintfl (EUCADEBUG, "http_get(): writing %s output to %s\n", "GET", outfile);

int retries = total_retries;
int timeout = first_timeout;
do {
params.total_wrote = 0L;
params.total_calls = 0L;

result = curl_easy_perform (curl); /* do it */
logprintfl (EUCADEBUG, "http_get(): wrote %ld bytes in %ld writes\n", params.total_wrote, params.total_calls);


if (result) { // curl error (connection or transfer failed)
            logprintfl (EUCAERROR, "http_get(): %s (%d)\n", error_msg, result);

} else {
            long httpcode;
            curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &httpcode);
            /* TODO: pull out response message, too */

            switch (httpcode) {
            case 200L: /* all good */
                logprintfl (EUCAINFO, "http_get(): saved image in %s\n", outfile);
                code = OK;
                break;
case 408L: /* timeout, retry */
logprintfl (EUCAWARN, "http_get(): server responded with HTTP code %ld (timeout)\n", httpcode);
//logcat (EUCADEBUG, outfile); /* dump the error from outfile into the log */
break;
case 404L:
logprintfl (EUCAWARN, "http_get(): server responded with HTTP code %ld (file not found)\n", httpcode);
break;
            default: /* some kind of error */
                logprintfl (EUCAERROR, "http_get(): server responded with HTTP code %ld\n", httpcode);
                //logcat (EUCADEBUG, outfile); /* dump the error from outfile into the log */
                retries=0;
            }
}
        
if (code!=OK && retries>0) {
            logprintfl (EUCAERROR, " download retry %d of %d will commence in %d seconds\n", retries, total_retries, timeout);
            sleep (timeout);
            fseek (fp, 0L, SEEK_SET);
            timeout <<= 1;
}
        
retries--;
} while (code!=OK && retries>0);
fclose (fp);

if ( code != OK ) {
logprintfl (EUCAINFO, "http_get(): due to error, removing %s\n", outfile);
remove (outfile);
}

// curl_slist_free_all (headers);
curl_easy_cleanup (curl);
return code;
}

#ifdef _UNIT_TEST
int main (int argc, char ** argv)
{
#define _T(_S) { char * e = url_encode (_S); char * u = url_decode (e); printf ("orig: %s\nenco: %s\ndeco: %s\n\n", _S, e, u); free (e); free (u); }
  _T("hello world");
  _T("~`!1@2#3$4%5^6&7*8(9)0_-+={[}]|\\:;\"'<,>.?/");
  _T("[datastore1 (1)] windows 2003 enterprise/windows 2003 enterprise.vmx");
}
#endif
Something went wrong with that request. Please try again.