source: trunk/poppler/mypoppler/poppler/CurlCachedFile.cc @ 461

Last change on this file since 461 was 461, checked in by Silvan Scherrer, 11 years ago

poppler update to 0.14.2

File size: 2.4 KB
Line 
1//========================================================================
2//
3// CurlCachedFile.cc
4//
5// This file is licensed under the GPLv2 or later
6//
7// Copyright 2009 Stefan Thomas <thomas@eload24.com>
8// Copyright 2010 Hib Eris <hib@hiberis.nl>
9// Copyright 2010 Albert Astals Cid <aacid@kde.org>
10//
11//========================================================================
12
13#include <config.h>
14
15#include "CurlCachedFile.h"
16
17#include "goo/GooString.h"
18#include "goo/GooVector.h"
19
20//------------------------------------------------------------------------
21
22CurlCachedFileLoader::CurlCachedFileLoader()
23{
24  url = NULL;
25  cachedFile = NULL;
26  curl = NULL;
27}
28
29CurlCachedFileLoader::~CurlCachedFileLoader() {
30  curl_easy_cleanup(curl);
31}
32
33static size_t
34noop_cb(char *ptr, size_t size, size_t nmemb, void *ptr2)
35{
36  return size*nmemb;
37}
38
39size_t
40CurlCachedFileLoader::init(GooString *urlA, CachedFile *cachedFileA)
41{
42  long code = NULL;
43  double contentLength = -1;
44  size_t size;
45
46  url = urlA;
47  cachedFile = cachedFileA;
48  curl = curl_easy_init();
49
50  curl_easy_setopt(curl, CURLOPT_URL, url->getCString());
51  curl_easy_setopt(curl, CURLOPT_HEADER, 1);
52  curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
53  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &noop_cb);
54  curl_easy_perform(curl);
55  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
56  curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLength);
57  curl_easy_reset(curl);
58
59  size = contentLength;
60
61  return size;
62}
63
64static
65size_t load_cb(const char *ptr, size_t size, size_t nmemb, void *data)
66{
67  CachedFileWriter *writer = (CachedFileWriter *) data;
68  return (writer->write) (ptr, size*nmemb);
69}
70
71int CurlCachedFileLoader::load(const GooVector<ByteRange> &ranges, CachedFileWriter *writer)
72{
73  CURLcode r = CURLE_OK;
74  size_t fromByte, toByte;
75  for (size_t i = 0; i < ranges.size(); i++) {
76
77     fromByte = ranges[i].offset;
78     toByte = fromByte + ranges[i].length - 1;
79     GooString *range = GooString::format("{0:ud}-{1:ud}", fromByte, toByte);
80
81     curl_easy_setopt(curl, CURLOPT_URL, url->getCString());
82     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, load_cb);
83     curl_easy_setopt(curl, CURLOPT_WRITEDATA, writer);
84     curl_easy_setopt(curl, CURLOPT_RANGE, range->getCString());
85     r = curl_easy_perform(curl);
86     curl_easy_reset(curl);
87
88     delete range;
89
90     if (r != CURLE_OK) break;
91  }
92  return r;
93}
94
95//------------------------------------------------------------------------
96
Note: See TracBrowser for help on using the repository browser.