Core: Developing IpAddrInfo

git-svn-id: svn://ultimatepp.org/upp/trunk@4752 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-04-07 07:30:14 +00:00
parent 126bb18a7b
commit db0e8ff979
3 changed files with 1109 additions and 869 deletions

File diff suppressed because it is too large Load diff

View file

@ -18,6 +18,176 @@ NAMESPACE_UPP
#define LLOG(x) // DLOG("TCP " << x)
struct RawMutex {
CRITICAL_SECTION cs;
void Enter() { EnterCriticalSection(&cs); }
void Leave() { LeaveCriticalSection(&cs); }
RawMutex() { InitializeCriticalSection(&cs); }
~RawMutex() { DeleteCriticalSection(&cs); }
};
bool StartRawThread(uintptr_t (__stdcall *fn)(void *ptr), void *ptr)
{
#ifdef PLATFORM_WIN32
HANDLE handle;
handle = (HANDLE)_beginthreadex(0, 0, fn, ptr, 0, NULL);
if(handle) {
CloseHandle(handle);
return true;
}
#endif
#ifdef PLATFORM_POSIX
pthread_t handle;
if(pthread_create(&handle, 0, sThreadRoutine, cb) == 0) {
pthread_detach(handle)
return true;
}
#endif
return false;
}
IpAddrInfo::Entry IpAddrInfo::pool[COUNT];
RawMutex IpAddrInfoPoolMutex;
void IpAddrInfo::EnterPool()
{
IpAddrInfoPoolMutex.Enter();
}
void IpAddrInfo::LeavePool()
{
IpAddrInfoPoolMutex.Leave();
}
int sGetAddrInfo(const char *host, const char *port, addrinfo **result)
{
addrinfo hints;
memset(&hints, 0, sizeof(addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
return getaddrinfo(host, port, &hints, result);
}
uintptr_t __stdcall IpAddrInfo::Thread(void *ptr)
{
Entry *entry = (Entry *)ptr;
EnterPool();
if(entry->status == WORKING) {
char host[1025];
char port[257];
strcpy(host, entry->host);
strcpy(port, entry->port);
LeavePool();
addrinfo *result;
if(sGetAddrInfo(host, port, &result) == 0 && result) {
EnterPool();
if(entry->status == WORKING) {
entry->addr = result;
entry->status = RESOLVED;
}
else {
freeaddrinfo(entry->addr);
entry->status = EMPTY;
}
}
else {
EnterPool();
if(entry->status == CANCELED)
entry->status = EMPTY;
else
entry->status = FAILED;
}
}
LeavePool();
return 0;
}
bool IpAddrInfo::Execute(const String& host, int port)
{
Clear();
entry = exe;
addrinfo *result;
entry->addr = sGetAddrInfo(~host, ~AsString(port), &result) == 0 ? result : NULL;
return entry->addr;
}
void IpAddrInfo::Start()
{
if(entry)
return;
EnterPool();
for(int i = 0; i < COUNT; i++) {
Entry *e = pool + i;
if(e->status == EMPTY) {
entry = e;
e->addr = NULL;
if(host.GetCount() > 1024 || port.GetCount() > 256)
e->status = FAILED;
else {
e->status = WORKING;
e->host = host;
e->port = port;
StartRawThread(&IpAddrInfo::Thread, e);
}
break;
}
}
LeavePool();
}
void IpAddrInfo::Start(const String& host_, int port_)
{
Clear();
port = AsString(port_);
host = host_;
Start();
}
bool IpAddrInfo::InProgress()
{
if(!entry) {
Start();
return true;
}
EnterPool();
int s = entry->status;
LeavePool();
return s == WORKING;
}
addrinfo *IpAddrInfo::GetResult()
{
EnterPool();
addrinfo *ai = entry ? entry->addr : NULL;
LeavePool();
return ai;
}
void IpAddrInfo::Clear()
{
EnterPool();
if(entry) {
if(entry->status == RESOLVED && entry->addr)
freeaddrinfo(entry->addr);
if(entry->status == WORKING)
entry->status = CANCELED;
entry->status = EMPTY;
entry = NULL;
}
LeavePool();
}
IpAddrInfo::IpAddrInfo()
{
TcpSocket::Init();
entry = NULL;
}
#ifdef PLATFORM_POSIX
#define SOCKERR(x) x
@ -122,7 +292,6 @@ TcpSocket::TcpSocket()
Reset();
timeout = Null;
waitstep = 20;
global = false;
}
bool TcpSocket::Open(int family, int type, int protocol)
@ -146,6 +315,8 @@ bool TcpSocket::Open(int family, int type, int protocol)
bool TcpSocket::Listen(int port, int listen_count, bool ipv6_, bool reuse)
{
Init();
ipv6 = ipv6_;
if(!Open(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0))
return false;
@ -244,25 +415,8 @@ void TcpSocket::Attach(SOCKET s)
socket = s;
}
bool TcpSocket::Connect(const char *host, int port)
bool TcpSocket::RawConnect(addrinfo *rp)
{
LLOG("TcpSocket::Data::OpenClient(" << host << ':' << port << ')');
Init();
addrinfo hints;
memset(&hints, 0, sizeof(addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
addrinfo *result;
if(getaddrinfo(host, ~AsString(port), &hints, &result) || !result) {
SetSockError(Format("getaddrinfo(%s) failed", host));
return false;
}
addrinfo *rp = result;
for(;;) {
if(Open(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) {
if(connect(socket, rp->ai_addr, rp->ai_addrlen) == 0 ||
@ -272,16 +426,35 @@ bool TcpSocket::Connect(const char *host, int port)
}
rp = rp->ai_next;
if(!rp) {
SetSockError(Format("unable to open or bind socket for %s", host));
freeaddrinfo(result);
SetSockError("connect has failed");
return false;
}
}
freeaddrinfo(result);
return true;
}
bool TcpSocket::Connect(IpAddrInfo& info)
{
LLOG("TCP Connect addrinfo");
Init();
addrinfo *result = info.GetResult();
return result && RawConnect(result);
}
bool TcpSocket::Connect(const char *host, int port)
{
LLOG("TCP Connect(" << host << ':' << port << ')');
Init();
IpAddrInfo info;
if(!info.Execute(host, port)) {
SetSockError(Format("getaddrinfo(%s) failed", host));
return false;
}
return Connect(info);
}
void TcpSocket::Close()
{
LLOG("TCP close " << (int)socket);
@ -307,7 +480,7 @@ bool TcpSocket::WouldBlock()
return c == SOCKERR(EWOULDBLOCK) || c == SOCKERR(EAGAIN);
#endif
#ifdef PLATFORM_WIN32
return c == SOCKERR(EWOULDBLOCK);
return c == SOCKERR(EWOULDBLOCK) || c == SOCKERR(ENOTCONN);
#endif
}

View file

@ -1,300 +1,340 @@
String FormatIP(dword _ip);
String UrlEncode(const String& s);
String UrlEncode(const String& s, const char *specials);
String UrlDecode(const char *b, const char *e);
inline String UrlDecode(const String& s) { return UrlDecode(s.Begin(), s.End() ); }
String Base64Encode(const char *b, const char *e);
inline String Base64Encode(const String& data) { return Base64Encode(data.Begin(), data.End()); }
String Base64Decode(const char *b, const char *e);
inline String Base64Decode(const String& data) { return Base64Decode(data.Begin(), data.End()); }
enum { WAIT_READ = 1, WAIT_WRITE = 2, WAIT_EXCEPTION = 4, WAIT_ALL = 7 };
class TcpSocket {
enum { BUFFERSIZE = 512 };
SOCKET socket;
char buffer[BUFFERSIZE];
char *ptr;
char *end;
bool is_eof;
bool is_error;
bool is_abort;
bool ipv6;
bool global;
int timeout;
int waitstep;
int done;
int errorcode;
String errordesc;
SOCKET AcceptRaw(dword *ipaddr, int timeout_msec);
bool Open(int family, int type, int protocol);
int Recv(void *buffer, int maxlen);
int Send(const void *buffer, int maxlen);
void ReadBuffer();
int Get_();
int Peek_();
void Reset();
void SetSockError(const char *context, const char *errdesc);
void SetSockError(const char *context);
static int GetErrorCode();
static bool WouldBlock();
public:
Callback WhenWait;
static String GetHostName();
int GetDone() const { return done; }
static void Init();
bool IsOpen() const { return socket != INVALID_SOCKET; }
bool IsEof() const { return is_eof && ptr == end; }
bool IsError() const { return is_error; }
void ClearError() { is_error = false; errorcode = 0; errordesc.Clear(); }
int GetError() const { return errorcode; }
String GetErrorDesc() const { return errordesc; }
void Abort() { is_abort = true; }
bool IsAbort() const { return is_abort; }
void ClearAbort() { is_abort = false; }
SOCKET GetSOCKET() const { return socket; }
String GetPeerAddr() const;
void Attach(SOCKET socket);
bool Connect(const char *host, int port);
bool Listen(int port, int listen_count, bool ipv6 = false, bool reuse = true);
bool Accept(TcpSocket& listen_socket);
void Close();
void Shutdown();
void NoDelay();
void Linger(int msecs);
void NoLinger() { Linger(Null); }
void Reuse(bool reuse = true);
bool Wait(dword events);
bool WaitRead() { return Wait(WAIT_READ); }
bool WaitWrite() { return Wait(WAIT_WRITE); }
int Peek() { return ptr < end ? *ptr : Peek_(); }
int Term() { return Peek(); }
int Get() { return ptr < end ? *ptr++ : Get_(); }
int Get(void *buffer, int len);
String Get(int len);
int GetAll(void *buffer, int len) { return Get(buffer, len) == len; }
String GetAll(int len) { String s = Get(len); return s.GetCount() == len ? s : String::GetVoid(); }
String GetLine(int maxlen = 2000000);
int Put(const char *s, int len);
int Put(const String& s) { return Put(s.Begin(), s.GetLength()); }
bool PutAll(const char *s, int len) { return Put(s, len) == len; }
bool PutAll(const String& s) { return Put(s) == s.GetCount(); }
TcpSocket& Timeout(int ms) { timeout = ms; global = false; return *this; }
TcpSocket& Blocking() { return Timeout(Null); }
TcpSocket();
~TcpSocket() { Close(); }
};
class SocketWaitEvent {
Vector< Tuple2<int, dword> > socket;
fd_set read[1], write[1], exception[1];
public:
void Clear() { socket.Clear(); }
void Add(SOCKET s, dword events = WAIT_ALL) { socket.Add(MakeTuple((int)s, events)); }
void Add(TcpSocket& s, dword events = WAIT_ALL) { Add(s.GetSOCKET(), events); }
int Wait(int timeout);
dword Get(int i) const;
dword operator[](int i) const { return Get(i); }
SocketWaitEvent();
};
struct HttpHeader {
String first_line;
VectorMap<String, String> fields;
String operator[](const char *id) { return fields.Get(id, Null); }
bool Response(String& protocol, int& code, String& reason);
bool Request(String& method, String& uri, String& version);
void Clear();
bool Parse(const String& hdrs);
};
class HttpRequest : public TcpSocket {
int phase;
String data;
int count;
HttpHeader header;
String error;
String body;
enum {
DEFAULT_HTTP_PORT = 80,
};
enum {
METHOD_GET,
METHOD_POST,
METHOD_HEAD,
METHOD_PUT,
};
int max_header_size;
int max_content_size;
int max_redirects;
int max_retries;
int timeout;
String host;
int port;
String proxy_host;
int proxy_port;
String proxy_username;
String proxy_password;
String path;
int method;
String accept;
String agent;
bool force_digest;
bool is_post;
bool std_headers;
bool hasurlvar;
String contenttype;
String username;
String password;
String digest;
String request_headers;
String postdata;
String protocol;
int status_code;
String reason_phrase;
int start_time;
int retry_count;
int redirect_count;
int chunk;
int bodylen;
bool gzip;
Zlib z;
void Init();
void StartPhase(int s);
void StartBody();
bool SendingData();
bool ReadingHeader();
bool ReadingBody();
void StartRequest();
void ReadingChunkHeader();
void Finish();
void HttpError(const char *s);
void ContentOut(const void *ptr, dword size);
void Out(const void *ptr, dword size);
String CalculateDigest(const String& authenticate) const;
public:
Callback2<const void *, dword> WhenContent;
HttpRequest& MaxHeaderSize(int m) { max_header_size = m; return *this; }
HttpRequest& MaxContentSize(int m) { max_content_size = m; return *this; }
HttpRequest& MaxRedirect(int n) { max_redirects = n; return *this; }
HttpRequest& MaxRetries(int n) { max_retries = n; return *this; }
HttpRequest& RequestTimeout(int ms) { timeout = ms; return *this; }
HttpRequest& ChunkSize(int n) { chunk = n; return *this; }
HttpRequest& Method(int m) { method = m; return *this; }
HttpRequest& GET() { return Method(METHOD_GET); }
HttpRequest& POST() { return Method(METHOD_POST); }
HttpRequest& HEAD() { return Method(METHOD_HEAD); }
HttpRequest& PUT() { return Method(METHOD_PUT); }
HttpRequest& Host(const String& h) { host = h; return *this; }
HttpRequest& Port(int p) { port = p; return *this; }
HttpRequest& Path(const String& p) { path = p; return *this; }
HttpRequest& User(const String& u, const String& p) { username = u; password = p; return *this; }
HttpRequest& Digest() { force_digest = true; return *this; }
HttpRequest& Digest(const String& d) { digest = d; return *this; }
HttpRequest& Url(const char *url);
HttpRequest& UrlVar(const char *id, const String& data);
HttpRequest& operator()(const char *id, const String& data) { return UrlVar(id, data); }
HttpRequest& PostData(const String& pd) { postdata = pd; return *this; }
HttpRequest& PostUData(const String& pd) { return PostData(UrlEncode(pd)); }
HttpRequest& Post(const String& data) { POST(); return PostData(data); }
HttpRequest& Post(const char *id, const String& data);
HttpRequest& Headers(const String& h) { request_headers = h; return *this; }
HttpRequest& ClearHeaders() { return Headers(Null); }
HttpRequest& AddHeaders(const String& h) { request_headers.Cat(h); return *this; }
HttpRequest& Header(const char *id, const String& data);
HttpRequest& StdHeaders(bool sh) { std_headers = sh; return *this; }
HttpRequest& NoStdHeaders() { return StdHeaders(false); }
HttpRequest& Accept(const String& a) { accept = a; return *this; }
HttpRequest& Agent(const String& a) { agent = a; return *this; }
HttpRequest& ContentType(const String& a) { contenttype = a; return *this; }
HttpRequest& Proxy(const String& host, int port) { proxy_host = host; proxy_port = port; return *this; }
HttpRequest& Proxy(const char *url);
HttpRequest& ProxyAuth(const String& u, const String& p) { proxy_username = u; proxy_password = p; return *this; }
bool IsSocketError() const { return TcpSocket::IsError(); }
bool IsHttpError() const { return !IsNull(error) ; }
bool IsError() const { return IsSocketError() || IsHttpError(); }
String GetErrorDesc() const { return IsSocketError() ? TcpSocket::GetErrorDesc() : error; }
void ClearError() { TcpSocket::ClearError(); error.Clear(); }
String GetHeader(const char *s) { return header[s]; }
String operator[](const char *s) { return GetHeader(s); }
String GetRedirectUrl();
int GetContentLength();
int GetStatusCode() const { return status_code; }
String GetReasonPhrase() const { return reason_phrase; }
String GetContent() const { return body; }
String operator~() const { return GetContent(); }
operator String() const { return GetContent(); }
void ClearContent() { body.Clear(); }
enum Phase {
START, REQUEST, HEADER, BODY, CHUNK_HEADER, CHUNK_BODY, TRAILER, FINISHED, FAILED
};
bool Do();
int GetPhase() const { return phase; }
bool InProgress() const { return phase != FAILED && phase != FINISHED; }
bool IsFailure() const { return phase == FAILED; }
bool IsSuccess() const { return phase == FINISHED && status_code >= 200 && status_code < 300; }
String Execute();
HttpRequest();
HttpRequest(const char *url);
static void Trace(bool b = true);
};
String FormatIP(dword _ip);
String UrlEncode(const String& s);
String UrlEncode(const String& s, const char *specials);
String UrlDecode(const char *b, const char *e);
inline String UrlDecode(const String& s) { return UrlDecode(s.Begin(), s.End() ); }
String Base64Encode(const char *b, const char *e);
inline String Base64Encode(const String& data) { return Base64Encode(data.Begin(), data.End()); }
String Base64Decode(const char *b, const char *e);
inline String Base64Decode(const String& data) { return Base64Decode(data.Begin(), data.End()); }
class IpAddrInfo {
enum { COUNT = 32 };
struct Entry {
const char *host;
const char *port;
int status;
addrinfo *addr;
};
static Entry pool[COUNT];
enum {
EMPTY = 0, WORKING, CANCELED, RESOLVED, FAILED
};
String host, port;
Entry *entry;
Entry exe[1];
static void EnterPool();
static void LeavePool();
static uintptr_t __stdcall Thread(void *ptr);
void Start();
public:
void Start(const String& host, int port);
bool InProgress();
bool Execute(const String& host, int port);
addrinfo *GetResult();
void Clear();
IpAddrInfo();
~IpAddrInfo() { Clear(); }
};
enum { WAIT_READ = 1, WAIT_WRITE = 2, WAIT_EXCEPTION = 4, WAIT_ALL = 7 };
class TcpSocket {
enum { BUFFERSIZE = 512 };
SOCKET socket;
char buffer[BUFFERSIZE];
char *ptr;
char *end;
bool is_eof;
bool is_error;
bool is_abort;
bool ipv6;
int timeout;
int waitstep;
int done;
int errorcode;
String errordesc;
SOCKET AcceptRaw(dword *ipaddr, int timeout_msec);
bool Open(int family, int type, int protocol);
int Recv(void *buffer, int maxlen);
int Send(const void *buffer, int maxlen);
bool RawConnect(addrinfo *info);
void ReadBuffer();
int Get_();
int Peek_();
void Reset();
void SetSockError(const char *context, const char *errdesc);
void SetSockError(const char *context);
static int GetErrorCode();
static bool WouldBlock();
public:
Callback WhenWait;
static String GetHostName();
int GetDone() const { return done; }
static void Init();
bool IsOpen() const { return socket != INVALID_SOCKET; }
bool IsEof() const { return is_eof && ptr == end; }
bool IsError() const { return is_error; }
void ClearError() { is_error = false; errorcode = 0; errordesc.Clear(); }
int GetError() const { return errorcode; }
String GetErrorDesc() const { return errordesc; }
void Abort() { is_abort = true; }
bool IsAbort() const { return is_abort; }
void ClearAbort() { is_abort = false; }
SOCKET GetSOCKET() const { return socket; }
String GetPeerAddr() const;
void Attach(SOCKET socket);
bool Connect(const char *host, int port);
bool Connect(IpAddrInfo& info);
bool Listen(int port, int listen_count, bool ipv6 = false, bool reuse = true);
bool Accept(TcpSocket& listen_socket);
void Close();
void Shutdown();
void NoDelay();
void Linger(int msecs);
void NoLinger() { Linger(Null); }
void Reuse(bool reuse = true);
bool Wait(dword events);
bool WaitRead() { return Wait(WAIT_READ); }
bool WaitWrite() { return Wait(WAIT_WRITE); }
int Peek() { return ptr < end ? *ptr : Peek_(); }
int Term() { return Peek(); }
int Get() { return ptr < end ? *ptr++ : Get_(); }
int Get(void *buffer, int len);
String Get(int len);
int GetAll(void *buffer, int len) { return Get(buffer, len) == len; }
String GetAll(int len) { String s = Get(len); return s.GetCount() == len ? s : String::GetVoid(); }
String GetLine(int maxlen = 2000000);
int Put(const char *s, int len);
int Put(const String& s) { return Put(s.Begin(), s.GetLength()); }
bool PutAll(const char *s, int len) { return Put(s, len) == len; }
bool PutAll(const String& s) { return Put(s) == s.GetCount(); }
TcpSocket& Timeout(int ms) { timeout = ms; return *this; }
int GetTimeout() const { return timeout; }
TcpSocket& Blocking() { return Timeout(Null); }
TcpSocket();
~TcpSocket() { Close(); }
};
class SocketWaitEvent {
Vector< Tuple2<int, dword> > socket;
fd_set read[1], write[1], exception[1];
public:
void Clear() { socket.Clear(); }
void Add(SOCKET s, dword events = WAIT_ALL) { socket.Add(MakeTuple((int)s, events)); }
void Add(TcpSocket& s, dword events = WAIT_ALL) { Add(s.GetSOCKET(), events); }
int Wait(int timeout);
dword Get(int i) const;
dword operator[](int i) const { return Get(i); }
SocketWaitEvent();
};
struct HttpHeader {
String first_line;
VectorMap<String, String> fields;
String operator[](const char *id) { return fields.Get(id, Null); }
bool Response(String& protocol, int& code, String& reason);
bool Request(String& method, String& uri, String& version);
void Clear();
bool Parse(const String& hdrs);
};
class HttpRequest : public TcpSocket {
int phase;
String data;
int count;
HttpHeader header;
String error;
String body;
enum {
DEFAULT_HTTP_PORT = 80,
};
enum {
METHOD_GET,
METHOD_POST,
METHOD_HEAD,
METHOD_PUT,
};
int max_header_size;
int max_content_size;
int max_redirects;
int max_retries;
int timeout;
String host;
int port;
String proxy_host;
int proxy_port;
String proxy_username;
String proxy_password;
String path;
int method;
String accept;
String agent;
bool force_digest;
bool is_post;
bool std_headers;
bool hasurlvar;
String contenttype;
String username;
String password;
String digest;
String request_headers;
String postdata;
String protocol;
int status_code;
String reason_phrase;
int start_time;
int retry_count;
int redirect_count;
int chunk;
IpAddrInfo addrinfo;
int bodylen;
bool gzip;
Zlib z;
void Init();
void StartPhase(int s);
void Start();
void Dns();
void StartRequest();
bool SendingData();
bool ReadingHeader();
void StartBody();
bool ReadingBody();
void ReadingChunkHeader();
void Finish();
void HttpError(const char *s);
void ContentOut(const void *ptr, dword size);
void Out(const void *ptr, dword size);
String CalculateDigest(const String& authenticate) const;
public:
Callback2<const void *, dword> WhenContent;
HttpRequest& MaxHeaderSize(int m) { max_header_size = m; return *this; }
HttpRequest& MaxContentSize(int m) { max_content_size = m; return *this; }
HttpRequest& MaxRedirect(int n) { max_redirects = n; return *this; }
HttpRequest& MaxRetries(int n) { max_retries = n; return *this; }
HttpRequest& RequestTimeout(int ms) { timeout = ms; return *this; }
HttpRequest& ChunkSize(int n) { chunk = n; return *this; }
HttpRequest& Method(int m) { method = m; return *this; }
HttpRequest& GET() { return Method(METHOD_GET); }
HttpRequest& POST() { return Method(METHOD_POST); }
HttpRequest& HEAD() { return Method(METHOD_HEAD); }
HttpRequest& PUT() { return Method(METHOD_PUT); }
HttpRequest& Host(const String& h) { host = h; return *this; }
HttpRequest& Port(int p) { port = p; return *this; }
HttpRequest& Path(const String& p) { path = p; return *this; }
HttpRequest& User(const String& u, const String& p) { username = u; password = p; return *this; }
HttpRequest& Digest() { force_digest = true; return *this; }
HttpRequest& Digest(const String& d) { digest = d; return *this; }
HttpRequest& Url(const char *url);
HttpRequest& UrlVar(const char *id, const String& data);
HttpRequest& operator()(const char *id, const String& data) { return UrlVar(id, data); }
HttpRequest& PostData(const String& pd) { postdata = pd; return *this; }
HttpRequest& PostUData(const String& pd) { return PostData(UrlEncode(pd)); }
HttpRequest& Post(const String& data) { POST(); return PostData(data); }
HttpRequest& Post(const char *id, const String& data);
HttpRequest& Headers(const String& h) { request_headers = h; return *this; }
HttpRequest& ClearHeaders() { return Headers(Null); }
HttpRequest& AddHeaders(const String& h) { request_headers.Cat(h); return *this; }
HttpRequest& Header(const char *id, const String& data);
HttpRequest& StdHeaders(bool sh) { std_headers = sh; return *this; }
HttpRequest& NoStdHeaders() { return StdHeaders(false); }
HttpRequest& Accept(const String& a) { accept = a; return *this; }
HttpRequest& Agent(const String& a) { agent = a; return *this; }
HttpRequest& ContentType(const String& a) { contenttype = a; return *this; }
HttpRequest& Proxy(const String& host, int port) { proxy_host = host; proxy_port = port; return *this; }
HttpRequest& Proxy(const char *url);
HttpRequest& ProxyAuth(const String& u, const String& p) { proxy_username = u; proxy_password = p; return *this; }
bool IsSocketError() const { return TcpSocket::IsError(); }
bool IsHttpError() const { return !IsNull(error) ; }
bool IsError() const { return IsSocketError() || IsHttpError(); }
String GetErrorDesc() const { return IsSocketError() ? TcpSocket::GetErrorDesc() : error; }
void ClearError() { TcpSocket::ClearError(); error.Clear(); }
String GetHeader(const char *s) { return header[s]; }
String operator[](const char *s) { return GetHeader(s); }
String GetRedirectUrl();
int GetContentLength();
int GetStatusCode() const { return status_code; }
String GetReasonPhrase() const { return reason_phrase; }
String GetContent() const { return body; }
String operator~() const { return GetContent(); }
operator String() const { return GetContent(); }
void ClearContent() { body.Clear(); }
enum Phase {
START, DNS, REQUEST, HEADER, BODY, CHUNK_HEADER, CHUNK_BODY, TRAILER, FINISHED, FAILED
};
bool Do();
int GetPhase() const { return phase; }
bool InProgress() const { return phase != FAILED && phase != FINISHED; }
bool IsFailure() const { return phase == FAILED; }
bool IsSuccess() const { return phase == FINISHED && status_code >= 200 && status_code < 300; }
String Execute();
HttpRequest();
HttpRequest(const char *url);
static void Trace(bool b = true);
};