diff --git a/enet/Http.cpp b/enet/Http.cpp index 8ce9452..274818d 100644 --- a/enet/Http.cpp +++ b/enet/Http.cpp @@ -9,6 +9,7 @@ #include #include #include +#include extern "C" { #include } @@ -126,7 +127,9 @@ void enet::Http::threadCallback() { // READ section data: if (m_headerIsSend == false) { getHeader(); - m_headerIsSend = true; + if (m_headerIsSend == false) { + continue; + } } if (m_observerRaw != nullptr) { m_observerRaw(m_connection); @@ -145,6 +148,15 @@ void enet::Http::threadCallback() { ENET_DEBUG("End of thread HTTP"); } +void enet::Http::redirectTo(const etk::String& _addressRedirect, bool _inThreadStop) { + if (m_isServer == true) { + ENET_ERROR("Request a redirect in Server mode ==> not authorised"); + return; + } + stop(_inThreadStop); + m_headerIsSend = false; + m_connection = etk::move(connectTcpClient(_addressRedirect)); +} void enet::Http::start() { ENET_DEBUG("connect [START]"); @@ -508,6 +520,7 @@ void enet::Http::getHeader() { m_connection.unlink(); } } + m_headerIsSend = true; if (m_isServer == false) { if (m_observerAnswer != nullptr) { m_observerAnswer(m_answerHeader); diff --git a/enet/Http.hpp b/enet/Http.hpp index fbb8548..6daba91 100644 --- a/enet/Http.hpp +++ b/enet/Http.hpp @@ -219,6 +219,14 @@ namespace enet { ethread::Thread* m_thread; bool m_threadRunning; etk::Vector m_temporaryBuffer; + public: + /** + * @brief Get the adress of the connection source IP:port + * @return string with the remote address name. + */ + const etk::String& getRemoteAddress() const { + return m_connection.getRemoteName(); + } private: void threadCallback(); private: @@ -229,6 +237,13 @@ namespace enet { bool isAlive() const { return m_connection.getConnectionStatus() == enet::Tcp::status::link; } + public: + /** + * @brief Redirect the current HTTP request to an other network address. + * @param[in] _addressRedirect new redirection address:port + * @param[in] _inThreadStop the http thread request an auto-stop. + */ + void redirectTo(const etk::String& _addressRedirect, bool _inThreadStop=false); public: using Observer = etk::Function&)>; //!< Define an Observer: function pointer Observer m_observer; @@ -323,6 +338,7 @@ namespace enet { HttpClient(enet::Tcp _connection); public: void setHeader(const enet::HttpRequest& _header) { + _header.display(); setRequestHeader(_header); } public: @@ -352,6 +368,7 @@ namespace enet { HttpServer(enet::Tcp _connection); public: void setHeader(const enet::HttpAnswer& _header) { + _header.display(); setAnswerHeader(_header); } public: