| 1 | // |
| 2 | // AbstractHTTPRequestHandler.cpp |
| 3 | // |
| 4 | // Library: Net |
| 5 | // Package: HTTPServer |
| 6 | // Module: AbstractHTTPRequestHandler |
| 7 | // |
| 8 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Net/AbstractHTTPRequestHandler.h" |
| 16 | #include "Poco/Net/HTTPServerRequest.h" |
| 17 | #include "Poco/Net/HTTPServerResponse.h" |
| 18 | #include "Poco/Net/HTMLForm.h" |
| 19 | #include "Poco/NumberFormatter.h" |
| 20 | #include "Poco/Exception.h" |
| 21 | |
| 22 | |
| 23 | using Poco::NumberFormatter; |
| 24 | |
| 25 | |
| 26 | namespace Poco { |
| 27 | namespace Net { |
| 28 | |
| 29 | |
| 30 | AbstractHTTPRequestHandler::AbstractHTTPRequestHandler(): |
| 31 | _pRequest(0), |
| 32 | _pResponse(0), |
| 33 | _pForm(0) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | |
| 38 | AbstractHTTPRequestHandler::~AbstractHTTPRequestHandler() |
| 39 | { |
| 40 | delete _pForm; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void AbstractHTTPRequestHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) |
| 45 | { |
| 46 | _pRequest = &request; |
| 47 | _pResponse = &response; |
| 48 | if (authenticate()) |
| 49 | { |
| 50 | try |
| 51 | { |
| 52 | run(); |
| 53 | } |
| 54 | catch (Poco::Exception& exc) |
| 55 | { |
| 56 | if (!response.sent()) |
| 57 | { |
| 58 | sendErrorResponse(HTTPResponse::HTTP_INTERNAL_SERVER_ERROR, exc.displayText()); |
| 59 | } |
| 60 | } |
| 61 | catch (std::exception& exc) |
| 62 | { |
| 63 | if (!response.sent()) |
| 64 | { |
| 65 | sendErrorResponse(HTTPResponse::HTTP_INTERNAL_SERVER_ERROR, exc.what()); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | sendErrorResponse(HTTPResponse::HTTP_UNAUTHORIZED, "" ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | bool AbstractHTTPRequestHandler::authenticate() |
| 77 | { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | HTMLForm& AbstractHTTPRequestHandler::form() |
| 83 | { |
| 84 | if (!_pForm) |
| 85 | _pForm = new HTMLForm(request(), request().stream()); |
| 86 | |
| 87 | return *_pForm; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | void AbstractHTTPRequestHandler::sendErrorResponse(HTTPResponse::HTTPStatus status, const std::string& message) |
| 92 | { |
| 93 | response().setStatusAndReason(status); |
| 94 | std::string statusAndReason(NumberFormatter::format(static_cast<int>(response().getStatus()))); |
| 95 | statusAndReason += " - " ; |
| 96 | statusAndReason += response().getReason(); |
| 97 | std::string page("<HTML><HEAD><TITLE>" ); |
| 98 | page += statusAndReason; |
| 99 | page += "</TITLE></HEAD><BODY><H1>" ; |
| 100 | page += statusAndReason; |
| 101 | page += "</H1>" ; |
| 102 | page += "<P>" ; |
| 103 | page += message; |
| 104 | page += "</P></BODY></HTML>" ; |
| 105 | response().sendBuffer(page.data(), page.size()); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | } } // namespace Poco::Net |
| 110 | |