a8/a8/basehttpsession.cc
2018-08-26 20:34:01 +08:00

58 lines
1.5 KiB
C++

#include <string.h>
#include <a8/a8.h>
#include <a8/basehttpsession.h>
namespace a8
{
void BaseHttpSession::OnRawHttpGet(const std::string& url, const std::string& querystr, std::string& response)
{
}
void BaseHttpSession::OnHttpGet(const std::string& url, a8::HTTPRequest& request, std::string& response)
{
}
void BaseHttpSession::ProcessHttpHandShake(char* buf, int& offset, unsigned int buflen)
{
if (handled_) {
return;
}
char* pend = strstr(buf + offset, "\r\n\r\n");
if (!pend) {
return;
}
if (strncmp(buf + offset, "GET ", strlen("GET ")) == 0) {
std::string url;
std::string querystr;
std::string response;
a8::ParserQueryStr(buf + offset + 4, url, querystr);
std::map<std::string, a8::XValue> request;
a8::ParserUrlQueryString(querystr.c_str(), request);
OnRawHttpGet(url, querystr, response);
OnHttpGet(url, request, response);
if (!response.empty()) {
SendText(HttpResponse(response));
}
}
handled_ = true;
offset += pend - buf - offset + strlen("\r\n\r\n");
}
void BaseHttpSession::Reset()
{
a8::TcpSession::Reset();
handled_ = false;
}
void BaseHttpSession::DecodePacket(char* buf, int& offset, unsigned int buflen)
{
buf[buflen] = '\0';
ProcessHttpHandShake(buf, offset, buflen);
}
}