1#include "Conditions/URLStreamer.h"
12#include "Framework/Exception/Exception.h"
16static unsigned int http_requests_ = 0;
17static unsigned int http_failures_ = 0;
19void urlstatistics(
unsigned int& http_requests,
unsigned int& http_failures) {
20 http_requests = http_requests_;
21 http_failures = http_failures_;
24std::unique_ptr<std::istream> urlstream(
const std::string& url) {
25 if (url.find(
"file://") == 0 || (url.length() > 0 && url[0] ==
'/')) {
26 std::string fname = url;
27 if (fname.find(
"file://") == 0)
28 fname = url.substr(url.find(
"file://") + strlen(
"file://"));
29 std::ifstream* fs =
new std::ifstream(fname);
32 EXCEPTION_RAISE(
"ConditionsException",
33 "Unable to open CSV file '" + fname +
"'");
35 return std::unique_ptr<std::istream>(fs);
37 if ((url.find(
"http://") != std::string::npos) ||
38 (url.find(
"https://") != std::string::npos)) {
41 static int istream = 0;
43 snprintf(fname, 250,
"/tmp/httpstream_%d_%d.csv ", getpid(), istream++);
46 execl(
"/usr/bin/wget",
"wget",
"-q",
"--no-check-certificate",
"-O",
47 fname,
"-o",
"/tmp/wget.log", url.c_str(), (
char*)0);
50 waitpid(apid, &wstatus, 0);
53 if (WIFEXITED(wstatus) != 1 || WEXITSTATUS(wstatus) != 0) {
55 EXCEPTION_RAISE(
"ConditionsException",
56 "Wget error " + std::to_string(WEXITSTATUS(wstatus)) +
57 " retreiving URL '" + url +
"'");
60 std::ifstream ib(fname);
63 EXCEPTION_RAISE(
"ConditionsException",
64 "Bad/empty file retreiving URL '" + url +
"'");
66 std::stringstream* ss =
new std::stringstream();
71 return std::unique_ptr<std::istream>(ss);
73 EXCEPTION_RAISE(
"ConditionsException",
"Unable to handle URL '" + url +
"'");
74 return std::unique_ptr<std::istream>(
nullptr);