add HandleRedirect

This commit is contained in:
aozhiwei 2019-05-18 16:52:38 +08:00
parent fc99a3615d
commit ff8c00652d
2 changed files with 30 additions and 1 deletions

View File

@ -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);

View File

@ -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);