Files
OrcaSlicer/src/slic3r/GUI/HttpServer.hpp
T
Kenneth Raplee 88890b9bb1 Build against recent system Boost (>= 1.87) (#15835)
Boost 1.87 removed io_service and io_context::reset(), and 1.88 moved the classic Boost.Process API under boost/process/v1/ and dropped deadline_timer from boost/asio.hpp. Use io_context, restart() and steady_timer, and include Boost.Process only through the new slic3r/Utils/BoostProcessCompat.hpp, which picks the v1 headers on 1.88+. The bundled Boost 1.84 build is unchanged.
2026-10-25 14:33:17 -05:00

183 lines
4.8 KiB
C++

#ifndef slic3r_Http_App_hpp_
#define slic3r_Http_App_hpp_
#include <boost/asio/ip/basic_endpoint.hpp>
#include <functional>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/streambuf.hpp>
#include <iostream>
#include <map>
#include <mutex>
#include <sstream>
#include <stack>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <string>
#include <set>
#include <memory>
#include <utility>
#define LOCALHOST_PORT 13618
#define LOCALHOST_URL "http://localhost:"
namespace Slic3r { namespace GUI {
class session;
class http_headers
{
std::string method;
std::string url;
std::string version;
std::map<std::string, std::string> headers;
friend class session;
public:
std::string get_url() { return url; }
int content_length()
{
auto request = headers.find("content-length");
if (request != headers.end()) {
std::stringstream ssLength(request->second);
int content_length;
ssLength >> content_length;
return content_length;
}
return 0;
}
void on_read_header(std::string line)
{
// std::cout << "header: " << line << std::endl;
std::stringstream ssHeader(line);
std::string headerName;
std::getline(ssHeader, headerName, ':');
std::string value;
std::getline(ssHeader, value);
headers[headerName] = value;
}
void on_read_request_line(std::string line)
{
std::stringstream ssRequestLine(line);
ssRequestLine >> method;
ssRequestLine >> url;
ssRequestLine >> version;
std::cout << "request for resource: " << url << std::endl;
}
};
class HttpServer
{
boost::asio::ip::port_type port;
public:
class Response
{
public:
virtual ~Response() = default;
virtual void write_response(std::stringstream& ssOut) = 0;
};
class ResponseNotFound : public Response
{
public:
~ResponseNotFound() override = default;
void write_response(std::stringstream& ssOut) override;
};
class ResponseRedirect : public Response
{
const std::string location_str;
public:
ResponseRedirect(const std::string& location) : location_str(location) {}
~ResponseRedirect() override = default;
void write_response(std::stringstream& ssOut) override;
};
class ResponseHtml : public Response
{
const std::string html;
public:
explicit ResponseHtml(std::string html) : html(std::move(html)) {}
~ResponseHtml() override = default;
void write_response(std::stringstream& ssOut) override;
};
HttpServer(boost::asio::ip::port_type port = LOCALHOST_PORT);
~HttpServer();
boost::thread m_http_server_thread;
bool start_http_server = false;
bool is_started() { return start_http_server; }
void start();
void stop();
void set_port(boost::asio::ip::port_type new_port) { port = new_port; }
boost::asio::ip::port_type get_port() const { return port; }
void set_request_handler(const std::function<std::shared_ptr<Response>(const std::string&)>& m_request_handler);
static std::shared_ptr<Response> bbl_auth_handle_request(const std::string& url);
static std::shared_ptr<Response> auth_handle_request(const std::string& url, const std::string& provider);
private:
class IOServer
{
public:
HttpServer& server;
boost::asio::io_context io_service;
boost::asio::ip::tcp::acceptor acceptor;
std::set<std::shared_ptr<session>> sessions;
IOServer(HttpServer& server) : server(server), acceptor(io_service, {boost::asio::ip::tcp::v4(), server.port}) {}
void do_accept();
void start(std::shared_ptr<session> session);
void stop(std::shared_ptr<session> session);
void stop_all();
};
friend class session;
std::unique_ptr<IOServer> server_{nullptr};
std::function<std::shared_ptr<Response>(const std::string&)> m_request_handler{&HttpServer::bbl_auth_handle_request};
};
class session : public std::enable_shared_from_this<session>
{
HttpServer::IOServer& server;
boost::asio::ip::tcp::socket socket;
boost::asio::streambuf buff;
http_headers headers;
void read_first_line();
void read_next_line();
void read_body();
public:
session(HttpServer::IOServer& server, boost::asio::ip::tcp::socket socket) : server(server), socket(std::move(socket)) {}
void start();
void stop();
};
std::string url_get_param(const std::string& url, const std::string& key);
}};
#endif