diff --git a/a8/websocketsession.cc b/a8/websocketsession.cc index 2e70d58..5567291 100644 --- a/a8/websocketsession.cc +++ b/a8/websocketsession.cc @@ -93,6 +93,12 @@ namespace a8 } } + bool WebSocketSession::HandleRedirect(const std::string& url, const std::string& querystr, + std::string& location) + { + return false; + } + void WebSocketSession::ProcessHandShake(char* buf, int& offset, unsigned int buflen) { char* pend = strstr(buf + offset, "\r\n\r\n"); @@ -112,6 +118,28 @@ namespace a8 if (!pend) { return; } + if (strncmp(buf + offset, "GET ", strlen("GET ")) == 0) { + std::string url; + std::string querystr; + std::string location; + + a8::ParserQueryStr(buf + offset + 4, url, querystr); + bool need_redirect = HandleRedirect(url, querystr, location); + if (need_redirect) { + std::string response = a8::Format( + "HTTP/1.1 %d OK\r\n" + "Location: %s\r\n" + "Connection: close\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 0\r\n\r\n", + { + 301, + location, + }); + SendBuff(response.data(), response.size()); + return; + } + } std::string server_key; { char* p1 = strstr(buf + offset, WEB_SOCKET_KEY); diff --git a/a8/websocketsession.h b/a8/websocketsession.h index 4600bdb..a8c4146 100644 --- a/a8/websocketsession.h +++ b/a8/websocketsession.h @@ -9,12 +9,13 @@ namespace a8 { public: virtual void SendBuff(const char* buff, unsigned int bufflen) override; - protected: virtual void Reset() override; virtual void Destory() override; virtual void DecodePacket(char* buf, int& offset, unsigned int buflen) override; + virtual bool HandleRedirect(const std::string& url, const std::string& querystr, + std::string& location); void ProcessHandShake(char* buf, int& offset, unsigned int buflen); void ProcessWsHandShake(char* buf, int& offset, unsigned int buflen);