Changeset 44294
- Timestamp:
- 11/09/19 17:46:39 (4 years ago)
- Location:
- titan/titan
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
titan/titan/global.h
r44291 r44294 8213 8213 8214 8214 if(!file_exist(tmpstr)) 8215 gethttp(ip, path, port, tmpstr, HTTPAUTH, 5000, NULL, 0); 8215 // gethttp(ip, path, port, tmpstr, HTTPAUTH, 5000, NULL, 0); 8216 gethttps(link, tmpstr, NULL, NULL, NULL, NULL, 1); 8216 8217 8217 8218 if(file_exist(tmpstr)) ret = 1; -
titan/titan/header.h
r44115 r44294 313 313 void gethttpstruct(struct stimerthread* timernode, struct download* node, int flag); 314 314 void gethttpstructmsg(struct stimerthread* timernode, struct download* node, int flag); 315 char* gethttps(char* url, char* localfile, char* data, char* user, char* pass, char* referer, int flag); 315 316 316 317 //numinput.h -
titan/titan/sock.h
r41541 r44294 1150 1150 } 1151 1151 1152 #include <stdio.h> 1153 #include <curl/curl.h> 1154 1155 struct MemoryStruct { 1156 char *memory; 1157 size_t size; 1158 }; 1159 1160 static size_t 1161 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) 1162 { 1163 size_t realsize = size * nmemb; 1164 struct MemoryStruct *mem = (struct MemoryStruct *)userp; 1165 1166 mem->memory = realloc(mem->memory, mem->size + realsize + 1); 1167 if(mem->memory == NULL) { 1168 /* out of memory! */ 1169 printf("not enough memory (realloc returned NULL)\n"); 1170 return 0; 1171 } 1172 1173 memcpy(&(mem->memory[mem->size]), contents, realsize); 1174 mem->size += realsize; 1175 mem->memory[mem->size] = 0; 1176 1177 return realsize; 1178 } 1179 1180 static size_t writeCallback(void *contents, size_t size, size_t nitems, FILE *file) { 1181 return fwrite(contents, size, nitems, file); 1182 } 1183 1184 // flag = 0 (without header in output) 1185 // flag = 1 (with header in output) 1186 char* gethttps(char* url, char* localfile, char* data, char* user, char* pass, char* referer, int flag) 1187 { 1188 debug(99, "url: %s", url); 1189 1190 int debuglevel = getconfigint("debuglevel", NULL); 1191 1192 char* tmpstr = NULL; 1193 FILE *fp; 1194 1195 CURL *curl_handle; 1196 CURLcode res; 1197 1198 struct MemoryStruct chunk; 1199 1200 chunk.memory = malloc(1); /* will be grown as needed by the realloc above */ 1201 chunk.size = 0; /* no data at this point */ 1202 1203 curl_global_init(CURL_GLOBAL_ALL); 1204 1205 /* init the curl session */ 1206 curl_handle = curl_easy_init(); 1207 if(curl_handle) 1208 { 1209 if(localfile != NULL) 1210 fp = fopen(localfile,"wb"); 1211 1212 /* specify URL to get */ 1213 curl_easy_setopt(curl_handle, CURLOPT_URL, url); 1214 1215 if(user != NULL && pass != NULL) 1216 { 1217 curl_easy_setopt(curl_handle, CURLOPT_USERNAME, user); 1218 curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, pass); 1219 curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 1220 } 1221 if(data == NULL) 1222 curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1L); 1223 else 1224 { 1225 curl_easy_setopt(curl_handle, CURLOPT_POST, 1); 1226 curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data); 1227 1228 /* example.com is redirected, so we tell libcurl to send POST on 301, 302 and 1229 303 HTTP response codes */ 1230 curl_easy_setopt(curl_handle, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL); 1231 } 1232 if(flag == 1) 1233 curl_easy_setopt(curl_handle, CURLOPT_HEADER, 1L); 1234 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 5); 1235 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 3); 1236 // curl_easy_setopt(curl_handle, CURLOPT_RETURNTRANSFER, 1); 1237 1238 if(localfile == NULL) 1239 { 1240 /* send all data to this function */ 1241 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); 1242 } 1243 else 1244 { 1245 /* When data arrives, curl will call writeCallback. */ 1246 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback); 1247 } 1248 1249 1250 1251 if(localfile == NULL) 1252 { 1253 /* we pass our 'chunk' struct to the callback function */ 1254 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); 1255 } 1256 else 1257 { 1258 /* The last argument to writeCallback will be our file: */ 1259 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)fp); 1260 } 1261 1262 /* some servers don't like requests that are made without a user-agent field, so we provide one */ 1263 // curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 1264 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"); 1265 1266 // This is occassionally required to stop CURL from verifying the peers certificate. 1267 // CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if 1268 // CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2 - check the existence of a 1269 // common name and also verify that it matches the hostname provided) 1270 #ifdef MIPSEL 1271 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 1L); 1272 #else 1273 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L); 1152 1274 #endif 1275 1276 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L); 1277 if(debuglevel == 99) 1278 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1); 1279 curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "/mnt/network/cookies"); 1280 curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, "/mnt/network/cookies"); 1281 /* enable redirect following */ 1282 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); 1283 /* allow three redirects */ 1284 curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 3L); 1285 1286 1287 /* enable all supported built-in compressions */ 1288 curl_easy_setopt(curl_handle, CURLOPT_ACCEPT_ENCODING, ""); 1289 1290 if(referer == NULL) 1291 curl_easy_setopt(curl_handle, CURLOPT_AUTOREFERER, 1L); 1292 else 1293 curl_easy_setopt(curl_handle, CURLOPT_REFERER, referer); 1294 1295 1296 /* get it! */ 1297 res = curl_easy_perform(curl_handle); 1298 1299 /* check for errors */ 1300 if(res != CURLE_OK) 1301 { 1302 err("failed: %s", curl_easy_strerror(res)); 1303 printf("curl error\n"); 1304 } 1305 else 1306 { 1307 /* 1308 * Now, our chunk.memory points to a memory block that is chunk.size 1309 * bytes big and contains the remote file. 1310 * 1311 * Do something nice with it! 1312 */ 1313 printf("%lu bytes retrieved\n", (long)chunk.size); 1314 } 1315 1316 /* cleanup curl stuff */ 1317 curl_easy_cleanup(curl_handle); 1318 if(localfile != NULL) 1319 fclose(fp); 1320 } 1321 1322 if(localfile == NULL) 1323 tmpstr = ostrcat(chunk.memory, NULL, 0, 0); 1324 1325 free(chunk.memory); 1326 /* we're done with libcurl, so clean it up */ 1327 1328 curl_global_cleanup(); 1329 1330 if(localfile != NULL) 1331 free(tmpstr), tmpstr = NULL; 1332 1333 return tmpstr; 1334 } 1335 1336 #endif
Note: See TracChangeset
for help on using the changeset viewer.