Changeset 44530 for titan/plugins/mc/mc_global.h
- Timestamp:
- 01/16/20 15:12:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
titan/plugins/mc/mc_global.h
r42897 r44530 1175 1175 } 1176 1176 1177 #include <stdio.h> 1178 #include <curl/curl.h> 1179 1180 /* This callback is, currently, a simple wrapper around fwrite(). You 1181 could get it to write to memory, or do anything else you'd like 1182 with the output. For more info, see 1183 http://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html 1184 */ 1185 1186 struct MemoryStruct { 1187 char *memory; 1188 size_t size; 1189 }; 1190 1191 static size_t 1192 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) 1193 { 1194 size_t realsize = size * nmemb; 1195 struct MemoryStruct *mem = (struct MemoryStruct *)userp; 1196 1197 mem->memory = realloc(mem->memory, mem->size + realsize + 1); 1198 if(mem->memory == NULL) { 1199 /* out of memory! */ 1200 printf("not enough memory (realloc returned NULL)\n"); 1201 return 0; 1202 } 1203 1204 memcpy(&(mem->memory[mem->size]), contents, realsize); 1205 mem->size += realsize; 1206 mem->memory[mem->size] = 0; 1207 1208 return realsize; 1209 } 1210 1211 static size_t writeCallback(void *contents, size_t size, size_t nitems, FILE *file) { 1212 return fwrite(contents, size, nitems, file); 1213 } 1214 1215 // flag = 0 (without header in output) 1216 // flag = 1 (with header in output) 1217 char* gethttps(char* url, char* localfile, char* data, char* user, char* pass, char* referer, int flag) 1218 { 1219 debug(99, "url: %s", url); 1220 1221 int debuglevel = getconfigint("debuglevel", NULL); 1222 1223 char* tmpstr = NULL; 1224 FILE *fp; 1225 1226 CURL *curl_handle; 1227 CURLcode res; 1228 1229 struct MemoryStruct chunk; 1230 1231 chunk.memory = malloc(1); /* will be grown as needed by the realloc above */ 1232 chunk.size = 0; /* no data at this point */ 1233 1234 curl_global_init(CURL_GLOBAL_ALL); 1235 1236 /* init the curl session */ 1237 curl_handle = curl_easy_init(); 1238 if(curl_handle) 1239 { 1240 if(localfile != NULL) 1241 fp = fopen(localfile,"wb"); 1242 1243 /* specify URL to get */ 1244 curl_easy_setopt(curl_handle, CURLOPT_URL, url); 1245 1246 if(user != NULL && pass != NULL) 1247 { 1248 curl_easy_setopt(curl_handle, CURLOPT_USERNAME, user); 1249 curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, pass); 1250 curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 1251 } 1252 if(data == NULL) 1253 curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1L); 1254 else 1255 { 1256 curl_easy_setopt(curl_handle, CURLOPT_POST, 1); 1257 curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data); 1258 1259 /* example.com is redirected, so we tell libcurl to send POST on 301, 302 and 1260 303 HTTP response codes */ 1261 curl_easy_setopt(curl_handle, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL); 1262 } 1263 if(flag == 1) 1264 curl_easy_setopt(curl_handle, CURLOPT_HEADER, 1L); 1265 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 5); 1266 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 3); 1267 // curl_easy_setopt(curl_handle, CURLOPT_RETURNTRANSFER, 1); 1268 1269 if(localfile == NULL) 1270 { 1271 /* send all data to this function */ 1272 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); 1273 } 1274 else 1275 { 1276 /* When data arrives, curl will call writeCallback. */ 1277 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback); 1278 } 1279 1280 1281 1282 if(localfile == NULL) 1283 { 1284 /* we pass our 'chunk' struct to the callback function */ 1285 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); 1286 } 1287 else 1288 { 1289 /* The last argument to writeCallback will be our file: */ 1290 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)fp); 1291 } 1292 1293 /* some servers don't like requests that are made without a user-agent field, so we provide one */ 1294 // curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 1295 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.7.3000 Chrome/30.0.1599.101 Safari/537.36"); 1296 1297 // This is occassionally required to stop CURL from verifying the peers certificate. 1298 // CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if 1299 // CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2 - check the existence of a 1300 // common name and also verify that it matches the hostname provided) 1301 #ifdef MIPSEL 1302 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 1L); 1303 #else 1304 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L); 1177 1305 #endif 1306 1307 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L); 1308 if(debuglevel == 99) 1309 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1); 1310 curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "/mnt/network/cookies"); 1311 curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, "/mnt/network/cookies"); 1312 /* enable redirect following */ 1313 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); 1314 /* allow three redirects */ 1315 curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 3L); 1316 1317 1318 /* enable all supported built-in compressions */ 1319 curl_easy_setopt(curl_handle, CURLOPT_ACCEPT_ENCODING, ""); 1320 1321 if(referer == NULL) 1322 curl_easy_setopt(curl_handle, CURLOPT_AUTOREFERER, 1L); 1323 else 1324 curl_easy_setopt(curl_handle, CURLOPT_REFERER, referer); 1325 1326 1327 /* get it! */ 1328 res = curl_easy_perform(curl_handle); 1329 1330 /* check for errors */ 1331 if(res != CURLE_OK) 1332 { 1333 err("failed: %s", curl_easy_strerror(res)); 1334 printf("curl error\n"); 1335 } 1336 else 1337 { 1338 /* 1339 * Now, our chunk.memory points to a memory block that is chunk.size 1340 * bytes big and contains the remote file. 1341 * 1342 * Do something nice with it! 1343 */ 1344 printf("%lu bytes retrieved\n", (long)chunk.size); 1345 } 1346 1347 /* cleanup curl stuff */ 1348 curl_easy_cleanup(curl_handle); 1349 if(localfile != NULL) 1350 fclose(fp); 1351 } 1352 1353 if(localfile == NULL) 1354 tmpstr = ostrcat(chunk.memory, NULL, 0, 0); 1355 1356 free(chunk.memory); 1357 /* we're done with libcurl, so clean it up */ 1358 1359 curl_global_cleanup(); 1360 1361 if(localfile != NULL) 1362 free(tmpstr), tmpstr = NULL; 1363 1364 return tmpstr; 1365 } 1366 1367 int update_iptv(char* file) 1368 { 1369 char* tmpstr = NULL, *link = NULL; 1370 int ret = 0; 1371 1372 tmpstr = dirname(file); 1373 link = getconfig("iptvserver", NULL); 1374 1375 if(link != NULL) 1376 { 1377 debug(10, "update %s", link); 1378 1379 if(!file_exist(tmpstr)) 1380 mkdir(tmpstr, 0777); 1381 1382 tmpstr = ostrcat(tmpstr, "/iptv.m3u", 1, 0); 1383 if(file_exist(tmpstr)) 1384 unlink(tmpstr); 1385 1386 if(!file_exist(tmpstr)) 1387 gethttps(link, tmpstr, NULL, NULL, NULL, NULL, 0); 1388 1389 if(file_exist(tmpstr)) ret = 1; 1390 1391 } 1392 1393 free(tmpstr), tmpstr = NULL; 1394 free(link), link = NULL; 1395 1396 return ret; 1397 } 1398 1399 #endif
Note: See TracChangeset
for help on using the changeset viewer.