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.
This commit is contained in:
fgenesis 2012-06-01 18:49:40 +02:00
parent d8bcc2f830
commit 6fc6bbd1fb
2 changed files with 37 additions and 7 deletions

View file

@ -15,6 +15,7 @@
#include <string.h> #include <string.h>
#include <sstream> #include <sstream>
#include <cctype> #include <cctype>
#include <cerrno>
#include <algorithm> #include <algorithm>
#ifdef _WIN32 #ifdef _WIN32
@ -207,7 +208,7 @@ void TcpSocket::close(void)
if(!SOCKETVALID(_s)) if(!SOCKETVALID(_s))
return; return;
_OnClose(); _OnCloseInternal();
#ifdef _WIN32 #ifdef _WIN32
::closesocket((SOCKET)_s); ::closesocket((SOCKET)_s);
@ -217,6 +218,11 @@ void TcpSocket::close(void)
_s = INVALID_SOCKET; _s = INVALID_SOCKET;
} }
void TcpSocket::_OnCloseInternal()
{
_OnClose();
}
bool TcpSocket::SetNonBlocking(bool nonblock) bool TcpSocket::SetNonBlocking(bool nonblock)
{ {
_nonblocking = nonblock; _nonblocking = nonblock;
@ -397,6 +403,12 @@ void HttpSocket::_OnOpen()
_chunkedTransfer = false; _chunkedTransfer = false;
} }
void HttpSocket::_OnCloseInternal()
{
if(!IsRedirecting() || _alwaysHandle)
_OnClose();
}
bool HttpSocket::_OnUpdate() bool HttpSocket::_OnUpdate()
{ {
if(!TcpSocket::_OnUpdate()) if(!TcpSocket::_OnUpdate())
@ -511,6 +523,7 @@ void HttpSocket::_FinishRequest(void)
{ {
if(_inProgress) if(_inProgress)
{ {
if(!IsRedirecting() || _alwaysHandle)
_OnRequestDone(); // notify about finished request _OnRequestDone(); // notify about finished request
_inProgress = false; _inProgress = false;
_hdrs.clear(); _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) void HttpSocket::_ParseHeader(void)
{ {

View file

@ -47,7 +47,7 @@ public:
bool SendBytes(const char *str, unsigned int len); bool SendBytes(const char *str, unsigned int len);
protected: protected:
virtual void _OnCloseInternal();
virtual void _OnData(); // data received callback. Internal, should only be overloaded to call _OnRecv() virtual void _OnData(); // data received callback. Internal, should only be overloaded to call _OnRecv()
virtual void _OnRecv(char *buf, unsigned int size) = 0; virtual void _OnRecv(char *buf, unsigned int size) = 0;
@ -139,8 +139,10 @@ public:
const Request &GetCurrentRequest() const { return _curRequest; } const Request &GetCurrentRequest() const { return _curRequest; }
const char *Hdr(const char *h) const; const char *Hdr(const char *h) const;
protected: bool IsRedirecting() const;
protected:
virtual void _OnCloseInternal();
virtual void _OnClose(); virtual void _OnClose();
virtual void _OnData(); // data received callback. Internal, should only be overloaded to call _OnRecv() virtual void _OnData(); // data received callback. Internal, should only be overloaded to call _OnRecv()
virtual void _OnRecv(char *buf, unsigned int size) = 0; virtual void _OnRecv(char *buf, unsigned int size) = 0;
@ -206,7 +208,7 @@ public:
void remove(TcpSocket *s); void remove(TcpSocket *s);
inline size_t size() { return _store.size(); } inline size_t size() { return _store.size(); }
protected: //protected:
struct SocketSetData struct SocketSetData
{ {
@ -219,9 +221,10 @@ protected:
Store _store; Store _store;
}; };
#endif
} // end namespace minihttp } // end namespace minihttp
#endif
#endif #endif