diff options
Diffstat (limited to 'server.cpp')
| -rw-r--r-- | server.cpp | 47 |
1 files changed, 34 insertions, 13 deletions
| @@ -7,6 +7,7 @@ | |||
| 7 | #include <exception> | 7 | #include <exception> |
| 8 | #include <iostream> | 8 | #include <iostream> |
| 9 | #include <sstream> | 9 | #include <sstream> |
| 10 | #include <stdexcept> | ||
| 10 | #include <string> | 11 | #include <string> |
| 11 | #include <string_view> | 12 | #include <string_view> |
| 12 | #include <utility> | 13 | #include <utility> |
| @@ -18,7 +19,18 @@ using namespace std; | |||
| 18 | 19 | ||
| 19 | namespace { | 20 | namespace { |
| 20 | struct Configuration { | 21 | struct Configuration { |
| 21 | int value; | 22 | int value; |
| 23 | string listen_address; | ||
| 24 | }; | ||
| 25 | |||
| 26 | struct RequestError : runtime_error { | ||
| 27 | RequestError(int status, const string& meldung) | ||
| 28 | : runtime_error(meldung) | ||
| 29 | , status(status) | ||
| 30 | { | ||
| 31 | } | ||
| 32 | |||
| 33 | int status; | ||
| 22 | }; | 34 | }; |
| 23 | 35 | ||
| 24 | atomic_bool quit{ false }; | 36 | atomic_bool quit{ false }; |
| @@ -55,7 +67,8 @@ bool | |||
| 55 | load_configuration(Configuration& configuration) | 67 | load_configuration(Configuration& configuration) |
| 56 | { | 68 | { |
| 57 | cerr << "Load configuration ...\n"; | 69 | cerr << "Load configuration ...\n"; |
| 58 | configuration.value = 1; | 70 | configuration.value = 1; |
| 71 | configuration.listen_address = "127.0.0.1:5000"; | ||
| 59 | return true; | 72 | return true; |
| 60 | } | 73 | } |
| 61 | 74 | ||
| @@ -70,11 +83,11 @@ handle_request(char** env, const Configuration&, string_view body, ostream& out) | |||
| 70 | } | 83 | } |
| 71 | 84 | ||
| 72 | void | 85 | void |
| 73 | send_internal_error(const FCGX_Request& request) | 86 | send_error(const FCGX_Request& request, int status, const char* meldung) |
| 74 | { | 87 | { |
| 75 | FCGX_FPrintF(request.out, "Status: 500 Internal Server Error\r\n"); | 88 | FCGX_FPrintF(request.out, "Status: %d\r\n", status); |
| 76 | FCGX_FPrintF(request.out, "Content-Type: text/plain; charset=utf-8\r\n\r\n"); | 89 | FCGX_FPrintF(request.out, "Content-Type: text/plain; charset=utf-8\r\n\r\n"); |
| 77 | FCGX_FPrintF(request.out, "Die Anfrage konnte nicht verarbeitet werden.\n"); | 90 | FCGX_FPrintF(request.out, "%s\n", meldung); |
| 78 | } | 91 | } |
| 79 | 92 | ||
| 80 | string | 93 | string |
| @@ -97,7 +110,13 @@ main() | |||
| 97 | { | 110 | { |
| 98 | FCGX_Init(); | 111 | FCGX_Init(); |
| 99 | 112 | ||
| 100 | auto socket = FCGX_OpenSocket(":5000", 20); | 113 | Configuration configuration{}; |
| 114 | if ( !load_configuration(configuration) ) { | ||
| 115 | cerr << "Die Konfiguration konnte nicht geladen werden.\n"; | ||
| 116 | return EXIT_FAILURE; | ||
| 117 | } | ||
| 118 | |||
| 119 | auto socket = FCGX_OpenSocket(configuration.listen_address.c_str(), 20); | ||
| 101 | if ( socket == -1 ) { | 120 | if ( socket == -1 ) { |
| 102 | cerr << "Fehler beim Erstellen des Listen-Sockets...\n"; | 121 | cerr << "Fehler beim Erstellen des Listen-Sockets...\n"; |
| 103 | return EXIT_FAILURE; | 122 | return EXIT_FAILURE; |
| @@ -111,12 +130,6 @@ main() | |||
| 111 | 130 | ||
| 112 | init_signal_handler(); | 131 | init_signal_handler(); |
| 113 | 132 | ||
| 114 | Configuration configuration{}; | ||
| 115 | if ( !load_configuration(configuration) ) { | ||
| 116 | cerr << "Die Konfiguration konnte nicht geladen werden.\n"; | ||
| 117 | return EXIT_FAILURE; | ||
| 118 | } | ||
| 119 | |||
| 120 | cout << "Handle Requests...\n"; | 133 | cout << "Handle Requests...\n"; |
| 121 | 134 | ||
| 122 | while ( !quit ) { | 135 | while ( !quit ) { |
| @@ -124,6 +137,10 @@ main() | |||
| 124 | Configuration new_configuration{}; | 137 | Configuration new_configuration{}; |
| 125 | 138 | ||
| 126 | if ( load_configuration(new_configuration) ) { | 139 | if ( load_configuration(new_configuration) ) { |
| 140 | if ( new_configuration.listen_address != configuration.listen_address ) { | ||
| 141 | cerr << "Listen-Adresse geaendert, wirksam erst nach Neustart.\n"; | ||
| 142 | } | ||
| 143 | |||
| 127 | configuration = std::move(new_configuration); | 144 | configuration = std::move(new_configuration); |
| 128 | } | 145 | } |
| 129 | else { | 146 | else { |
| @@ -148,9 +165,13 @@ main() | |||
| 148 | const string_view text = response.view(); | 165 | const string_view text = response.view(); |
| 149 | FCGX_PutStr(text.data(), static_cast<int>(text.size()), request.out); | 166 | FCGX_PutStr(text.data(), static_cast<int>(text.size()), request.out); |
| 150 | } | 167 | } |
| 168 | catch ( const RequestError& e ) { | ||
| 169 | FCGX_FPrintF(request.err, "Anfrage abgebrochen (%d): %s\n", e.status, e.what()); | ||
| 170 | send_error(request, e.status, e.what()); | ||
| 171 | } | ||
| 151 | catch ( const exception& e ) { | 172 | catch ( const exception& e ) { |
| 152 | FCGX_FPrintF(request.err, "Fehler beim Verarbeiten der Anfrage: %s\n", e.what()); | 173 | FCGX_FPrintF(request.err, "Fehler beim Verarbeiten der Anfrage: %s\n", e.what()); |
| 153 | send_internal_error(request); | 174 | send_error(request, 500, "Die Anfrage konnte nicht verarbeitet werden."); |
| 154 | } | 175 | } |
| 155 | 176 | ||
| 156 | FCGX_Finish_r(&request); | 177 | FCGX_Finish_r(&request); |
