URIReader fixes / improvements

This commit is contained in:
Thulinma 2020-02-20 14:03:28 +01:00
parent 6cea9f2092
commit 029c96879d

View file

@ -3,11 +3,10 @@
class URITest : public Util::DataCallback{
public:
int goStdin(bool useCallback = true);
int goHTTP(char *, bool useCallback = true);
void dump(const char *ptr, size_t size);
size_t wanted = 100000000;
void dataCallback(const char *ptr, size_t size);
int main(int argc, char ** argv);
};
void URITest::dataCallback(const char *ptr, size_t size){
@ -18,82 +17,38 @@ void URITest::dump(const char *ptr, size_t size){
if (fwrite(ptr, sizeof(char), size, stdout) != size){INFO_MSG("error: %s", strerror(errno));}
}
int URITest::goStdin(bool useCallback){
HTTP::URIReader U;
HTTP::URL uri;
uri.path = "-";
U.open(uri);
int URITest::main(int argc, char **argv){
Util::redirectLogsIfNeeded();
Util::Config cfg(argv[0]);
JSON::Value option;
option["arg_num"] = 1;
option["arg"] = "string";
option["help"] = "Name of the input URI or - for stdin";
option["value"].append("-");
cfg.addOption("input", option);
option.null();
option["short"] = "r";
option["long"] = "readall";
option["help"] = "Read all data all at once in blocking mode";
option["value"].append(0);
cfg.addOption("readall", option);
if (!cfg.parseArgs(argc, argv)){return 1;}
if (useCallback){
MEDIUM_MSG("read from STDIN with callbacks");
U.readAll(*this);
}else{
MEDIUM_MSG("read from STDIN without callbacks");
cfg.activate();
HTTP::URIReader R(cfg.getString("input"));
if (cfg.getBool("readall")){
char *dPtr = 0;
size_t dLen = 0;
U.readAll(dPtr, dLen);
R.readAll(dPtr, dLen);
dump(dPtr, dLen);
// INFO_MSG("length: %d", dLen);
}
return 0;
}
int URITest::goHTTP(char *uri, bool useCallback){
HTTP::URIReader d;
d.open(uri);
if (useCallback){
MEDIUM_MSG("read file or url with callbacks");
// d.readAll(*this);
while (!d.isEOF()){d.readSome(10486, *this);}
}else{
MEDIUM_MSG("read file or url without callbacks");
char *dPtr = 0;
size_t dLen = 0;
d.readAll(dPtr, dLen);
dump(dPtr, dLen);
while (!R.isEOF() && cfg.is_active){R.readSome(10486, *this);}
}
return 0;
}
int main(int argc, char **argv){
// Util::Config::printDebugLevel = 10;
Util::Config cfg;
cfg.activate();
URITest t;
if (!isatty(fileno(stdin))){
if (argv[1]){
t.goStdin(false);
}else{
t.goStdin();
}
}else{
if (argc == 1){
std::cout << "no arguments applied!" << std::endl;
std::cout << "usage: " << std::endl;
std::cout << "STDIN:\t urireader < filename " << std::endl;
std::cout << "URL:\t urireader http://url " << std::endl;
std::cout << "FILE:\t urireader path_to_file " << std::endl << std::endl;
std::cout << "Outputs content to stdout, use ' > outputfile' after the command to write "
"contents to disk"
<< std::endl
<< std::endl;
return 0;
}else{
if (argv[2]){
t.goHTTP(argv[1], false);
}else{
t.goHTTP(argv[1]);
}
}
}
return 0;
t.main(argc, argv);
}