2022-06-30 01:35:02 +02:00
|
|
|
#include "examplewebserver.h"
|
|
|
|
|
|
2022-06-30 02:08:09 +02:00
|
|
|
// esp-idf includes
|
|
|
|
|
#include <esp_log.h>
|
|
|
|
|
|
|
|
|
|
// local includes
|
2022-06-30 01:35:02 +02:00
|
|
|
#include "rootresponsehandler.h"
|
|
|
|
|
#include "debugresponsehandler.h"
|
2022-06-30 02:08:09 +02:00
|
|
|
#include "chunkedresponsehandler.h"
|
2022-06-30 01:35:02 +02:00
|
|
|
#include "errorresponsehandler.h"
|
2022-06-30 06:55:32 +02:00
|
|
|
#include "websocketresponsehandler.h"
|
2022-06-30 01:35:02 +02:00
|
|
|
|
2022-06-30 02:08:09 +02:00
|
|
|
namespace {
|
|
|
|
|
constexpr const char * const TAG = "ASIO_WEBSERVER";
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2022-06-30 01:35:02 +02:00
|
|
|
std::unique_ptr<ResponseHandler> ExampleWebserver::makeResponseHandler(ClientConnection &clientConnection, std::string_view method, std::string_view path, std::string_view protocol)
|
|
|
|
|
{
|
2022-06-30 05:59:32 +02:00
|
|
|
ESP_LOGI(TAG, "method=\"%.*s\" path=\"%.*s\" protocol=\"%.*s\"",
|
|
|
|
|
method.size(), method.data(), path.size(), path.data(), protocol.size(), protocol.data());
|
2022-06-30 01:35:02 +02:00
|
|
|
const std::string_view processedPath{[&](){
|
|
|
|
|
const auto index = path.find('?');
|
|
|
|
|
return index == std::string_view::npos ?
|
|
|
|
|
path : path.substr(0, index);
|
|
|
|
|
}()};
|
|
|
|
|
|
|
|
|
|
if (processedPath.empty() || processedPath == "/")
|
|
|
|
|
return std::make_unique<RootResponseHandler>(clientConnection);
|
2022-06-30 02:08:09 +02:00
|
|
|
else if (processedPath == "/debug" || processedPath.starts_with("/debug/"))
|
2022-06-30 01:35:02 +02:00
|
|
|
return std::make_unique<DebugResponseHandler>(clientConnection, method, path, protocol);
|
2022-06-30 02:08:09 +02:00
|
|
|
else if (processedPath == "/chunked")
|
|
|
|
|
return std::make_unique<ChunkedResponseHandler>(clientConnection);
|
2022-06-30 05:59:32 +02:00
|
|
|
else if (processedPath == "/ws")
|
2022-06-30 06:55:32 +02:00
|
|
|
return std::make_unique<WebsocketResponseHandler>(clientConnection);
|
2022-06-30 01:35:02 +02:00
|
|
|
else
|
|
|
|
|
return std::make_unique<ErrorResponseHandler>(clientConnection, path);
|
|
|
|
|
}
|