From 6fc6bbd1fbc8d4a7fc55ea5ada31fdf45e459044 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Fri, 1 Jun 2012 18:49:40 +0200 Subject: [PATCH] Do not call _OnRequestDone() and _OnClose() if following a redirect and autofollow is on. This makes the redirect fully transparent and invisible for the outside. --- minihttp.cpp | 31 +++++++++++++++++++++++++++++-- minihttp.h | 13 ++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/minihttp.cpp b/minihttp.cpp index 5fd366f..257049e 100644 --- a/minihttp.cpp +++ b/minihttp.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #ifdef _WIN32 @@ -207,7 +208,7 @@ void TcpSocket::close(void) if(!SOCKETVALID(_s)) return; - _OnClose(); + _OnCloseInternal(); #ifdef _WIN32 ::closesocket((SOCKET)_s); @@ -217,6 +218,11 @@ void TcpSocket::close(void) _s = INVALID_SOCKET; } +void TcpSocket::_OnCloseInternal() +{ + _OnClose(); +} + bool TcpSocket::SetNonBlocking(bool nonblock) { _nonblocking = nonblock; @@ -397,6 +403,12 @@ void HttpSocket::_OnOpen() _chunkedTransfer = false; } +void HttpSocket::_OnCloseInternal() +{ + if(!IsRedirecting() || _alwaysHandle) + _OnClose(); +} + bool HttpSocket::_OnUpdate() { if(!TcpSocket::_OnUpdate()) @@ -511,7 +523,8 @@ void HttpSocket::_FinishRequest(void) { if(_inProgress) { - _OnRequestDone(); // notify about finished request + if(!IsRedirecting() || _alwaysHandle) + _OnRequestDone(); // notify about finished request _inProgress = false; _hdrs.clear(); } @@ -665,6 +678,20 @@ bool HttpSocket::_HandleStatus() } } +bool HttpSocket::IsRedirecting() const +{ + switch(_status) + { + case 301: + case 302: + case 303: + case 307: + case 308: + return true; + } + return false; +} + void HttpSocket::_ParseHeader(void) { diff --git a/minihttp.h b/minihttp.h index bd31654..443ef4b 100644 --- a/minihttp.h +++ b/minihttp.h @@ -47,7 +47,7 @@ public: bool SendBytes(const char *str, unsigned int len); protected: - + virtual void _OnCloseInternal(); virtual void _OnData(); // data received callback. Internal, should only be overloaded to call _OnRecv() virtual void _OnRecv(char *buf, unsigned int size) = 0; @@ -139,8 +139,10 @@ public: const Request &GetCurrentRequest() const { return _curRequest; } const char *Hdr(const char *h) const; -protected: + bool IsRedirecting() const; +protected: + virtual void _OnCloseInternal(); virtual void _OnClose(); virtual void _OnData(); // data received callback. Internal, should only be overloaded to call _OnRecv() virtual void _OnRecv(char *buf, unsigned int size) = 0; @@ -206,7 +208,7 @@ public: void remove(TcpSocket *s); inline size_t size() { return _store.size(); } -protected: +//protected: struct SocketSetData { @@ -219,9 +221,10 @@ protected: Store _store; }; +#endif + + } // end namespace minihttp -#endif - #endif