summaryrefslogtreecommitdiff
path: root/lib/curl.c
blob: 6eff24ccf3581113079e23ead2ede89f90c96d29 (plain)
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
/*
 * curl.c: 
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <curl/curl.h>

#include "common.h"
#include "config.h"

//***************************************************************************
// Callbacks
//***************************************************************************

static size_t WriteMemoryCallback(void* ptr, size_t size, size_t nmemb, void* data)
{
   size_t realsize = size * nmemb;
   struct MemoryStruct* mem = (struct MemoryStruct*)data;
   
   if (mem->memory)
      mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);
   else
      mem->memory = (char*)malloc(mem->size + realsize + 1);
   
   if (mem->memory)
   {
      memcpy (&(mem->memory[mem->size]), ptr, realsize);
      mem->size += realsize;
      mem->memory[mem->size] = 0;
   }

   return realsize;
}

static size_t WriteHeaderCallback(void* ptr, size_t size, size_t nmemb, void* data)
{
   size_t realsize = size * nmemb;
   struct MemoryStruct* mem = (struct MemoryStruct*)data;
   char* p;

   if (ptr)
   {
      // get filename
      {
         // Content-Disposition: attachment; filename="20140103_20140103_de_qy.zip"

         const char* attribute = "Content-disposition: ";
         
         if ((p = strcasestr((char*)ptr, attribute)))
         {
            if (p = strcasestr(p, "filename="))
            {
               p += strlen("filename=");

               tell(4, "found filename at [%s]", p);

               if (*p == '"')
                  p++;

               sprintf(mem->name, "%s", p);
               
               if ((p = strchr(mem->name, '\n')))
                  *p = 0;
               
               if ((p = strchr(mem->name, '\r')))
                  *p = 0;
               
               if ((p = strchr(mem->name, '"')))
                  *p = 0;

               tell(4, "set name to '%s'", mem->name);
            }
         }
      }
      
      // since some sources update "ETag" an "Last-Modified:" without changing the contents
      //  we have to use "Content-Length:" to check for updates :(
      {
         const char* attribute = "Content-Length: ";
         
         if ((p = strcasestr((char*)ptr, attribute)))
         {
            sprintf(mem->tag, "%s", p+strlen(attribute));
            
            if ((p = strchr(mem->tag, '\n')))
               *p = 0;
            
            if ((p = strchr(mem->tag, '\r')))
               *p = 0;
            
            if ((p = strchr(mem->tag, '"')))
               *p = 0;
         }
      }
   }

   return realsize;
}

//***************************************************************************
// Download File
//***************************************************************************

int downloadFile(const char* url, int& size, MemoryStruct* data, int timeout, const char* userAgent)
{
   CURL* curl_handle;
   long code;
   CURLcode res = CURLE_OK;

   size = 0;

   // init curl

   if (curl_global_init(CURL_GLOBAL_ALL) != 0)
   {
      tell(0, "Error, something went wrong with curl_global_init()");
      
      return fail;
   }
   
   curl_handle = curl_easy_init();
   
   if (!curl_handle)
   {
      tell(0, "Error, unable to get handle from curl_easy_init()");
      
      return fail;
   }
  
   if (EPG2VDRConfig.useproxy)
   {
      curl_easy_setopt(curl_handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
      curl_easy_setopt(curl_handle, CURLOPT_PROXY, EPG2VDRConfig.httpproxy);   // Specify HTTP proxy
   }

   curl_easy_setopt(curl_handle, CURLOPT_URL, url);                            // Specify URL to get   
   curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 0);                   // don't follow redirects
   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);  // Send all data to this function
   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)data);              // Pass our 'data' struct to the callback function
   curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, WriteHeaderCallback); // Send header to this function
   curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, (void*)data);            // Pass some header details to this struct
   curl_easy_setopt(curl_handle, CURLOPT_MAXFILESIZE, 100*1024*1024);          // Set maximum file size to get (bytes)
   curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);                       // No progress meter
   curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);                         // No signaling
   curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, timeout);                    // Set timeout
   curl_easy_setopt(curl_handle, CURLOPT_NOBODY, data->headerOnly ? 1 : 0);    // 
   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, userAgent);                // Some servers don't like requests 
                                                                               // that are made without a user-agent field
   // perform http-get

   if ((res = curl_easy_perform(curl_handle)) != 0)
   {
      data->clear();

      tell(1, "Error, download failed; %s (%d)",
           curl_easy_strerror(res), res);
      curl_easy_cleanup(curl_handle);  // Cleanup curl stuff

      return fail;
   }

   curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &code); 
   curl_easy_cleanup(curl_handle);     // cleanup curl stuff

   if (code == 404)
   {
      data->clear();
      return fail;
   }

   size = data->size;

   return success;
}