Files
OrcaSlicer/src/slic3r/GUI/HttpServer.hpp
T
HanifKoh 1a5f91d727 Add Missing Includes Across src/slic3r/GUI (#16048)
* Add Missing Includes Across src/slic3r/GUI

Every GUI source and header now directly includes the headers declaring what it uses, rather than relying on the precompiled header or transitive includes. Generated with clang-tidy misc-include-cleaner, plus one hand edit making CalibrationPanel.hpp self-contained.

* Drop the OS-Specific Includes Added Outside Their Platform Guards

GLib, GTK, D-Bus and POSIX headers are only used inside platform #if blocks, which already include them. Added unconditionally at the top of the file they broke the Windows build.

* Add the clang-tidy Configuration That Generated These Includes

Only misc-include-cleaner's missing-include check, with the headers it must never suggest: per-platform, internal and OS-specific ones that would break other platforms or are not meant to be included directly.

* Match Windows Paths in the clang-tidy Ignore List

Header paths use backslashes on Windows, so every / in a pattern is now [/\\]. The Windows SDK headers are ignored alongside the other OS-specific ones, and the list is one pattern per line. Suggested by @raistlin7447 from a Windows clang-cl run.
2026-10-02 14:56:56 +08: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_service.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_service 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