1 |
torben |
328 |
/*************************************************************************** |
2 |
|
|
* Copyright (C) 2006 by Torben H. Nielsen * |
3 |
|
|
* torben@t-hoerup.dk * |
4 |
|
|
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
* * |
10 |
|
|
* This program is distributed in the hope that it will be useful, * |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
|
|
* GNU General Public License for more details. * |
14 |
|
|
* * |
15 |
|
|
* You should have received a copy of the GNU General Public License * |
16 |
|
|
* along with this program; if not, write to the * |
17 |
|
|
* Free Software Foundation, Inc., * |
18 |
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 |
|
|
***************************************************************************/ |
20 |
|
|
|
21 |
|
|
#include <curl/curl.h> |
22 |
|
|
|
23 |
|
|
#include <stdio.h> |
24 |
|
|
#include <string.h> |
25 |
|
|
#include <errno.h> |
26 |
|
|
|
27 |
|
|
#include "dblayer.h" |
28 |
|
|
#include "daemon.h" |
29 |
|
|
#include "dlworker.h" |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
typedef struct { |
33 |
|
|
char *filename; |
34 |
|
|
FILE *stream; |
35 |
|
|
int error; |
36 |
|
|
} CurlFile; |
37 |
|
|
|
38 |
|
|
int writefile(void *buffer, size_t size, size_t nmemb, void *stream) |
39 |
|
|
{ |
40 |
|
|
CurlFile *out = (CurlFile*) stream; |
41 |
|
|
int retval; |
42 |
|
|
|
43 |
|
|
if( !out->stream) { |
44 |
|
|
/* open file for writing */ |
45 |
|
|
out->stream=fopen(out->filename, "wb"); |
46 |
|
|
if(!out->stream) { |
47 |
|
|
out->error = errno; |
48 |
|
|
return -1; /* failure, can't open file to write */ |
49 |
|
|
} |
50 |
|
|
} |
51 |
|
|
return fwrite(buffer, size, nmemb, out->stream); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
void downloadfile(dlrecord *record) { |
56 |
|
|
char buf[200]; |
57 |
|
|
CURL *curl = 0; |
58 |
|
|
CURLcode res; |
59 |
|
|
CurlFile outfile; |
60 |
|
|
int dlok = 1; |
61 |
|
|
long responsecode; |
62 |
|
|
|
63 |
|
|
outfile.error = 0; |
64 |
|
|
outfile.stream = 0; |
65 |
|
|
outfile.filename = record->filename; |
66 |
|
|
|
67 |
|
|
curl_global_init(CURL_GLOBAL_DEFAULT); // meybe we should have this in main() ??? |
68 |
|
|
curl = curl_easy_init(); |
69 |
|
|
|
70 |
|
|
if (!curl) { |
71 |
|
|
sprintf(buf, "Could not initialize libcurl"); |
72 |
|
|
log_message(buf); |
73 |
|
|
sql_abort_download(record, buf); |
74 |
|
|
return; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
sql_start_download( record ); |
78 |
|
|
curl_easy_setopt(curl, CURLOPT_URL, record->URL); |
79 |
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outfile); |
80 |
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefile); |
81 |
|
|
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); |
82 |
|
|
|
83 |
|
|
/* Switch on full protocol/debug output */ |
84 |
|
|
if (CONFIG->debug) |
85 |
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); |
86 |
|
|
|
87 |
|
|
res = curl_easy_perform(curl); |
88 |
|
|
|
89 |
|
|
if (res == CURLE_OK) { |
90 |
|
|
sql_stop_download( record ); |
91 |
|
|
sprintf(buf, "download of %s completed successfully", record->URL); |
92 |
|
|
log_message(buf); |
93 |
|
|
} else { |
94 |
|
|
if (outfile.error == 0) { // could we open the output file stream ? |
95 |
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responsecode); |
96 |
|
|
sprintf(buf, "download of %s failed : %s (response code:%ld)", |
97 |
|
|
record->URL, curl_easy_strerror(res),responsecode ); |
98 |
|
|
} else { |
99 |
|
|
sprintf(buf,"download of %s failed : %s", record->URL, strerror(outfile.error)); |
100 |
|
|
} |
101 |
|
|
log_message(buf); |
102 |
|
|
sql_abort_download(record, buf); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
curl_easy_cleanup(curl); |
106 |
|
|
curl_global_cleanup(); |
107 |
|
|
if (outfile.stream) |
108 |
|
|
fclose(outfile.stream); |
109 |
|
|
} |