int CWebServer::HandleRequest(IHTTPRequestHandler *handler) { if (handler == NULL) return MHD_NO; HTTPRequest request = handler->GetRequest(); int ret = handler->HandleRequest(); if (ret == MHD_NO) { CLog::Log(LOGERROR, "CWebServer: failed to handle HTTP request for %s", request.pathUrl.c_str()); delete handler; return SendErrorResponse(request.connection, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method); } const HTTPResponseDetails &responseDetails = handler->GetResponseDetails(); struct MHD_Response *response = NULL; switch (responseDetails.type) { case HTTPNone: CLog::Log(LOGERROR, "CWebServer: HTTP request handler didn't process %s", request.pathUrl.c_str()); delete handler; return MHD_NO; case HTTPRedirect: ret = CreateRedirect(request.connection, handler->GetRedirectUrl(), response); break; case HTTPFileDownload: ret = CreateFileDownloadResponse(handler, response); break; case HTTPMemoryDownloadNoFreeNoCopy: case HTTPMemoryDownloadNoFreeCopy: case HTTPMemoryDownloadFreeNoCopy: case HTTPMemoryDownloadFreeCopy: ret = CreateMemoryDownloadResponse(handler, response); break; case HTTPError: ret = CreateErrorResponse(request.connection, responseDetails.status, request.method, response); break; default: CLog::Log(LOGERROR, "CWebServer: internal error while HTTP request handler processed %s", request.pathUrl.c_str()); delete handler; return SendErrorResponse(request.connection, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method); } if (ret == MHD_NO) { CLog::Log(LOGERROR, "CWebServer: failed to create HTTP response for %s", request.pathUrl.c_str()); delete handler; return SendErrorResponse(request.connection, MHD_HTTP_INTERNAL_SERVER_ERROR, request.method); } return FinalizeRequest(handler, responseDetails.status, response); }
int CWebServer::SendErrorResponse(const HTTPRequest& request, int errorType, HTTPMethod method) const { struct MHD_Response *response = nullptr; int ret = CreateErrorResponse(request.connection, errorType, method, response); if (ret == MHD_NO) return MHD_NO; return SendResponse(request, errorType, response); }
int CWebServer::SendErrorResponse(struct MHD_Connection *connection, int errorType, HTTPMethod method) { struct MHD_Response *response = NULL; int ret = CreateErrorResponse(connection, errorType, method, response); if (ret == MHD_YES) { #ifdef WEBSERVER_DEBUG std::multimap<std::string, std::string> headerValues; GetRequestHeaderValues(connection, MHD_RESPONSE_HEADER_KIND, headerValues); CLog::Log(LOGDEBUG, "webserver [OUT] HTTP %d", errorType); for (std::multimap<std::string, std::string>::const_iterator header = headerValues.begin(); header != headerValues.end(); ++header) CLog::Log(LOGDEBUG, "webserver [OUT] %s: %s", header->first.c_str(), header->second.c_str()); #endif ret = MHD_queue_response(connection, errorType, response); MHD_destroy_response(response); } return ret; }