CurlCachedFile.cc 2.51 KB
Newer Older
Hib Eris's avatar
Hib Eris committed
1 2 3 4 5 6 7
//========================================================================
//
// CurlCachedFile.cc
//
// This file is licensed under the GPLv2 or later
//
// Copyright 2009 Stefan Thomas <thomas@eload24.com>
Albert Astals Cid's avatar
Albert Astals Cid committed
8
// Copyright 2010, 2011 Hib Eris <hib@hiberis.nl>
9
// Copyright 2010 Albert Astals Cid <aacid@kde.org>
Hib Eris's avatar
Hib Eris committed
10 11 12 13 14 15 16 17 18 19 20 21 22
//
//========================================================================

#include <config.h>

#include "CurlCachedFile.h"

#include "goo/GooString.h"

//------------------------------------------------------------------------

CurlCachedFileLoader::CurlCachedFileLoader()
{
23 24 25
  url = nullptr;
  cachedFile = nullptr;
  curl = nullptr;
Hib Eris's avatar
Hib Eris committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
}

CurlCachedFileLoader::~CurlCachedFileLoader() {
  curl_easy_cleanup(curl);
}

static size_t
noop_cb(char *ptr, size_t size, size_t nmemb, void *ptr2)
{
  return size*nmemb;
}

size_t
CurlCachedFileLoader::init(GooString *urlA, CachedFile *cachedFileA)
{
  double contentLength = -1;
42
  long code = 0;
Hib Eris's avatar
Hib Eris committed
43 44 45 46 47 48
  size_t size;

  url = urlA;
  cachedFile = cachedFileA;
  curl = curl_easy_init();

49
  curl_easy_setopt(curl, CURLOPT_URL, url->c_str());
Hib Eris's avatar
Hib Eris committed
50 51 52 53
  curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &noop_cb);
  curl_easy_perform(curl);
54 55 56 57 58
  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  if (code) {
     curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLength);
     size = contentLength;
  } else {
59
     error(errInternal, -1, "Failed to get size of '{0:t}'.", url);
60 61
     size = -1;
  }
Hib Eris's avatar
Hib Eris committed
62 63 64 65 66 67 68 69 70 71 72 73
  curl_easy_reset(curl);

  return size;
}

static
size_t load_cb(const char *ptr, size_t size, size_t nmemb, void *data)
{
  CachedFileWriter *writer = (CachedFileWriter *) data;
  return (writer->write) (ptr, size*nmemb);
}

74
int CurlCachedFileLoader::load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer)
Hib Eris's avatar
Hib Eris committed
75 76 77
{
  CURLcode r = CURLE_OK;
  size_t fromByte, toByte;
78
  for (size_t i = 0; i < ranges.size(); i++) {
Hib Eris's avatar
Hib Eris committed
79

80 81
     fromByte = ranges[i].offset;
     toByte = fromByte + ranges[i].length - 1;
Hib Eris's avatar
Hib Eris committed
82 83
     GooString *range = GooString::format("{0:ud}-{1:ud}", fromByte, toByte);

84
     curl_easy_setopt(curl, CURLOPT_URL, url->c_str());
Hib Eris's avatar
Hib Eris committed
85 86
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, load_cb);
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, writer);
87
     curl_easy_setopt(curl, CURLOPT_RANGE, range->c_str());
Hib Eris's avatar
Hib Eris committed
88 89 90 91 92 93 94 95 96 97 98 99
     r = curl_easy_perform(curl);
     curl_easy_reset(curl);

     delete range;

     if (r != CURLE_OK) break;
  }
  return r;
}

//------------------------------------------------------------------------