Skip to content

Commit

Permalink
Modify HttpServer
Browse files Browse the repository at this point in the history
-Remove BBL specific logic

-Add check for valid request handler on start. With BBL no longer the default, it is possible for a request handler to not be set during development. There is now a check that will throw a RuntimeException if it is not valid.
  • Loading branch information
Ocraftyone committed Jul 22, 2024
1 parent 1ceb20a commit 1ce7877
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 63 deletions.
66 changes: 6 additions & 60 deletions src/slic3r/GUI/HttpServer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "HttpServer.hpp"
#include <boost/log/trivial.hpp>
#include "GUI_App.hpp"
#include "slic3r/Utils/Http.hpp"
#include <boost/log/trivial.hpp>
#include <libslic3r/Thread.hpp>

namespace Slic3r {
namespace GUI {
Expand Down Expand Up @@ -140,6 +141,10 @@ HttpServer::HttpServer(port_type port) : port(port) {}
void HttpServer::start()
{
BOOST_LOG_TRIVIAL(info) << "start_http_service...";
if (!m_request_handler) {
BOOST_LOG_TRIVIAL(error) << "Http Server failed to start! Reason: Started without a request handler";
throw RuntimeError("Http Server failed to start! Reason: Started without a request handler");
}
start_http_server = true;
m_http_server_thread = create_thread([this] {
set_current_thread_name("http_server");
Expand Down Expand Up @@ -169,65 +174,6 @@ void HttpServer::set_request_handler(const std::function<std::shared_ptr<Respons
this->m_request_handler = request_handler;
}

std::shared_ptr<HttpServer::Response> HttpServer::bbl_auth_handle_request(const std::string& url)
{
BOOST_LOG_TRIVIAL(info) << "thirdparty_login: get_response";

if (boost::contains(url, "access_token")) {
std::string redirect_url = url_get_param(url, "redirect_url");
std::string access_token = url_get_param(url, "access_token");
std::string refresh_token = url_get_param(url, "refresh_token");
std::string expires_in_str = url_get_param(url, "expires_in");
std::string refresh_expires_in_str = url_get_param(url, "refresh_expires_in");
NetworkAgent* agent = wxGetApp().getAgent();

unsigned int http_code;
std::string http_body;
int result = agent->get_my_profile(access_token, &http_code, &http_body);
if (result == 0) {
std::string user_id;
std::string user_name;
std::string user_account;
std::string user_avatar;
try {
json user_j = json::parse(http_body);
if (user_j.contains("uidStr"))
user_id = user_j["uidStr"].get<std::string>();
if (user_j.contains("name"))
user_name = user_j["name"].get<std::string>();
if (user_j.contains("avatar"))
user_avatar = user_j["avatar"].get<std::string>();
if (user_j.contains("account"))
user_account = user_j["account"].get<std::string>();
} catch (...) {
;
}
json j;
j["data"]["refresh_token"] = refresh_token;
j["data"]["token"] = access_token;
j["data"]["expires_in"] = expires_in_str;
j["data"]["refresh_expires_in"] = refresh_expires_in_str;
j["data"]["user"]["uid"] = user_id;
j["data"]["user"]["name"] = user_name;
j["data"]["user"]["account"] = user_account;
j["data"]["user"]["avatar"] = user_avatar;
agent->change_user(j.dump());
if (agent->is_user_login()) {
wxGetApp().request_user_login(1);
}
GUI::wxGetApp().CallAfter([] { wxGetApp().ShowUserLogin(false); });
std::string location_str = (boost::format("%1%?result=success") % redirect_url).str();
return std::make_shared<ResponseRedirect>(location_str);
} else {
std::string error_str = "get_user_profile_error_" + std::to_string(result);
std::string location_str = (boost::format("%1%?result=fail&error=%2%") % redirect_url % error_str).str();
return std::make_shared<ResponseRedirect>(location_str);
}
} else {
return std::make_shared<ResponseNotFound>();
}
}

void HttpServer::ResponseNotFound::write_response(std::stringstream& ssOut)
{
const std::string sHTML = "<html><body><h1>404 Not Found</h1><p>There's nothing here.</p></body></html>";
Expand Down
4 changes: 1 addition & 3 deletions src/slic3r/GUI/HttpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ class HttpServer
void stop();
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);

private:
class IOServer
{
Expand All @@ -132,7 +130,7 @@ class HttpServer

std::unique_ptr<IOServer> server_{nullptr};

std::function<std::shared_ptr<Response>(const std::string&)> m_request_handler{&HttpServer::bbl_auth_handle_request};
std::function<std::shared_ptr<Response>(const std::string&)> m_request_handler;
};

class session : public std::enable_shared_from_this<session>
Expand Down

0 comments on commit 1ce7877

Please sign in to comment.