| 203 | } |
| 204 | |
| 205 | void Server::on_client_handle_request(Client *who, RequestBody &&request) { |
| 206 | ResponseBody response(request.r); |
| 207 | response.r.status = 422; |
| 208 | response.set_body(std::string{}); |
| 209 | |
| 210 | bool result = true; |
| 211 | try { |
| 212 | result = r_handler(who, std::move(request), response); |
| 213 | } catch (const ErrorAuthorization &e) { |
| 214 | std::cout << "HTTP unauthorized request" << std::endl; |
| 215 | response.r.headers.push_back({"WWW-Authenticate", "Basic realm=\"" + e.realm + "\", charset=\"UTF-8\""}); |
| 216 | response.r.status = 401; |
| 217 | } catch (const std::exception &e) { |
| 218 | std::cout << "HTTP request leads to throw/catch, what=" << common::what(e) << std::endl; |
| 219 | response.r.status = 422; |
| 220 | response.set_body(common::what(e)); |
| 221 | } catch (...) { |
| 222 | std::cout << "HTTP request leads to throw/catch" << std::endl; |
| 223 | response.r.status = 422; |
| 224 | } |
| 225 | if (result) |
| 226 | who->write(std::move(response)); |
| 227 | } |