author | František Kučera <franta-hg@frantovo.cz> |
Thu, 21 Apr 2022 00:57:30 +0200 | |
branch | v_0 |
changeset 28 | fe61bf7d1716 |
parent 5 | 165f6162524d |
permissions | -rw-r--r-- |
0
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
0
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#include <cstdio> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <cstdlib> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <memory> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <relpipe/cli/CLI.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <relpipe/cli/RelpipeCLIException.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <relpipe/reader/Factory.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
#include <relpipe/reader/RelationalReader.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
#include <relpipe/reader/RelpipeReaderException.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
#include <relpipe/writer/RelationalWriter.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
#include <relpipe/writer/RelpipeWriterException.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
#include <relpipe/writer/Factory.h> |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
#include "HTTPHandler.h" |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
#include "CLIParser.h" |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
#include "Configuration.h" |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
using namespace relpipe::cli; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
using namespace relpipe::reader; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
using namespace relpipe::writer; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
using namespace relpipe::tr::http; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
int main(int argc, char**argv) { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
setlocale(LC_ALL, ""); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
CLI::untieStdIO(); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
CLI cli(argc, argv); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
try { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
CLIParser cliParser; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
Configuration configuration = cliParser.parse(cli.arguments()); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
std::shared_ptr<RelationalWriter> writer(relpipe::writer::Factory::create(std::cout)); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
std::shared_ptr<RelationalReader> reader(relpipe::reader::Factory::create(std::cin)); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
HTTPHandler handler(writer, configuration); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
reader->addHandler(&handler); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
reader->process(); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
} catch (RelpipeCLIException& e) { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str()); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
resultCode = e.getExitCode(); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
} catch (RelpipeReaderException& e) { |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessage().c_str()); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
3
9c710397ced6
return also the HTTP response code and URL
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
} catch (RelpipeWriterException& e) { |
9c710397ced6
return also the HTTP response code and URL
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessage().c_str()); |
9c710397ced6
return also the HTTP response code and URL
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
9c710397ced6
return also the HTTP response code and URL
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
0
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
} |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
|
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
return resultCode; |
ce520a238309
new project relpipe-tr-http
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
} |