diff --git a/a8/mixedsession.cc b/a8/mixedsession.cc new file mode 100644 index 0000000..6b1c35b --- /dev/null +++ b/a8/mixedsession.cc @@ -0,0 +1,60 @@ +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +namespace a8 +{ + + static bool IsHttpHead(char* buf, int buflen) + { + return (buflen > 4 && strncmp("GET ", buf, 4)) || + (buflen > 4 && strncmp("get ", buf, 4)) || + (buflen > 5 && strncmp("POST ", buf, 5)) || + (buflen > 5 && strncmp("post ", buf, 5)); + } + + void MixedSession::Reset() + { + a8::BaseHttpSession::Reset(); + is_first_packet_ = true; + } + + void MixedSession::Destory() + { + a8::TcpSession::Destory(); + is_first_packet_ = true; + } + + void MixedSession::DecodePacket(char* buf, int& offset, unsigned int buflen) + { + if (is_first_packet_) { + if (IsHttpHead(buf, buflen)) { + buf[buflen] = '\0'; + ProcessHandShake(buf, offset, buflen); + } else { + DecodeUserPacket(buf, offset, buflen); + } + is_first_packet_ = offset == 0; + } else { + DecodeUserPacket(buf, offset, buflen); + } + } + + void MixedSession::ProcessHandShake(char* buf, int& offset, unsigned int buflen) + { + char* pend = strstr(buf + offset, "\r\n\r\n"); + if (!pend) { + return; + } + ProcessHttpHandShake(buf, offset, buflen); + } + +} diff --git a/a8/mixedsession.h b/a8/mixedsession.h new file mode 100644 index 0000000..874f7c0 --- /dev/null +++ b/a8/mixedsession.h @@ -0,0 +1,24 @@ +#ifndef A8_MIXEDSESSION_H +#define A8_MIXEDSESSION_H + +#include + +namespace a8 +{ + class MixedSession: public BaseHttpSession + { + protected: + virtual void Reset() override; + virtual void Destory() override; + virtual void DecodePacket(char* buf, int& offset, unsigned int buflen) override; + + void ProcessHandShake(char* buf, int& offset, unsigned int buflen); + + private: + bool is_first_packet_ = true; + + }; + +} + +#endif diff --git a/a8/tcpsession.cc b/a8/tcpsession.cc index f4e60e2..29d5fc9 100644 --- a/a8/tcpsession.cc +++ b/a8/tcpsession.cc @@ -141,6 +141,7 @@ namespace a8 bot_node_ = NULL; work_node_ = NULL; socket_handle = 0; + recv_bufflen_ = 0; } void TcpSession::Destory()