From 856f93f30c5856dc4ecd5eddd23f59e5ccc4bbf7 Mon Sep 17 00:00:00 2001 From: cxl Date: Mon, 21 May 2012 16:30:27 +0000 Subject: [PATCH] Core: HttpResponse git-svn-id: svn://ultimatepp.org/upp/trunk@4991 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Inet.h | 911 +++++++++++++++-------------- uppsrc/Core/InetUtil.cpp | 21 + uppsrc/Core/src.tpp/Inet$en-us.tpp | 29 +- 3 files changed, 504 insertions(+), 457 deletions(-) diff --git a/uppsrc/Core/Inet.h b/uppsrc/Core/Inet.h index 51cbe0cb1..59fff2da4 100644 --- a/uppsrc/Core/Inet.h +++ b/uppsrc/Core/Inet.h @@ -1,453 +1,458 @@ -String WwwFormat(Time tm); - -String MIMECharsetName(byte charset); - -String UrlEncode(const char *s, const char *end); -String UrlEncode(const char *s, int len); -String UrlEncode(const String& s); -String UrlDecode(const char *s, const char *end); -String UrlDecode(const char *s, int len); -String UrlDecode(const String& s); - -String Base64Encode(const char *s, const char *end); -String Base64Encode(const char *s, int len); -String Base64Encode(const String& data); -String Base64Decode(const char *s, const char *end); -String Base64Decode(const char *s, int len); -String Base64Decode(const String& data); - -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 auxthread_t auxthread__ 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 }; - -struct SSLInfo { - String cipher; - bool cert_avail; - bool cert_verified; // Peer verification not yet working - this is always false - String cert_subject; - String cert_issuer; - Date cert_notbefore; - Date cert_notafter; - int cert_version; - String cert_serial; -}; - -class TcpSocket { - enum { BUFFERSIZE = 512 }; - enum { NONE, CONNECT, ACCEPT, SSL_CONNECTED }; - SOCKET socket; - int mode; - 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 global_timeout; - int start_time; - - int errorcode; - String errordesc; - - struct SSL { - virtual bool Start() = 0; - virtual bool Wait(dword flags, int end_time) = 0; - virtual int Send(const void *buffer, int maxlen) = 0; - virtual int Recv(void *buffer, int maxlen) = 0; - virtual void Close() = 0; - virtual dword Handshake() = 0; - - virtual ~SSL() {} - }; - - One ssl; - One sslinfo; - String cert, pkey; - bool asn1; - - struct SSLImp; - friend struct SSLImp; - - static SSL *(*CreateSSL)(TcpSocket& socket); - static SSL *CreateSSLImp(TcpSocket& socket); - - friend void InitCreateSSL(); - friend class IpAddrInfo; - - int GetEndTime() const; - bool RawWait(dword flags, int end_time); - bool Wait(dword events, int end_time); - SOCKET AcceptRaw(dword *ipaddr, int timeout_msec); - bool Open(int family, int type, int protocol); - int RawRecv(void *buffer, int maxlen); - int Recv(void *buffer, int maxlen); - int RawSend(const void *buffer, int maxlen); - int Send(const void *buffer, int maxlen); - bool RawConnect(addrinfo *info); - void RawClose(); - - void ReadBuffer(int end_time); - int Get_(); - int Peek_(); - int Peek_(int end_time); - int Peek(int end_time) { return ptr < end ? *ptr : Peek_(end_time); } - bool IsGlobalTimeout(); - - void Reset(); - - void SetSockError(const char *context, int code, const char *errdesc); - void SetSockError(const char *context, const char *errdesc); - void SetSockError(const char *context); - - static int GetErrorCode(); - static bool WouldBlock(); - static void Init(); - - -public: - Callback WhenWait; - - enum { ERROR_GLOBAL_TIMEOUT = -1000000 }; - - static String GetHostName(); - - int GetDone() const { return done; } - - 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); } - - 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 Put(const char *s, int len); - int Put(const String& s) { return Put(s.Begin(), s.GetLength()); } - - bool GetAll(void *buffer, int len); - String GetAll(int len); - String GetLine(int maxlen = 65536); - - bool PutAll(const char *s, int len); - bool PutAll(const String& s); - - bool StartSSL(); - bool IsSSL() const { return ssl; } - bool SSLHandshake(); - void SSLCertificate(const String& cert, const String& pkey, bool asn1); - const SSLInfo *GetSSLInfo() const { return ~sslinfo; } - - TcpSocket& Timeout(int ms) { timeout = ms; return *this; } - int GetTimeout() const { return timeout; } - TcpSocket& GlobalTimeout(int ms); - TcpSocket& NoGlobalTimeout() { return GlobalTimeout(Null); } - TcpSocket& Blocking() { return Timeout(Null); } - TcpSocket& WaitStep(int ms) { waitstep = ms; return *this; } - int GetWaitStep() const { return waitstep; } - - TcpSocket(); - ~TcpSocket() { Close(); } -}; - -class SocketWaitEvent { - Vector< Tuple2 > 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; - String f1, f2, f3; - VectorMap fields; - - String operator[](const char *id) const { return fields.Get(id, Null); } - - bool Response(String& protocol, int& code, String& reason); - bool Request(String& method, String& uri, String& version); - - String GetProtocol() const { return f1; } - int GetCode() const; - String GetReason() const { return f3; } - - String GetMethod() const { return f1; } - String GetURI() const { return f2; } - String GetVersion() const { return f3; } - - int64 GetContentLength() const; - - void Clear(); - bool ParseAdd(const String& hdrs); - bool Parse(const String& hdrs); - bool ParseSCGI(const String& scgi_hdr); - - bool Read(TcpSocket& socket); -}; - -class HttpRequest : public TcpSocket { - int phase; - String data; - int count; - - HttpHeader header; - - String error; - String body; - - enum { - DEFAULT_HTTP_PORT = 80, - DEFAULT_HTTPS_PORT = 443 - }; - - 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 ssl_proxy_host; - int ssl_proxy_port; - String ssl_proxy_username; - String ssl_proxy_password; - String path; - bool ssl; - - 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 cookies; - - 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 StartConnect(); - void ProcessSSLProxyResponse(); - void AfterConnect(); - void StartRequest(); - bool SendingData(); - bool ReadingHeader(); - void StartBody(); - bool ReadingBody(); - void ReadingChunkHeader(); - void Finish(); - bool IsRequestTimeout(); - void CopyCookies(); - - void HttpError(const char *s); - void ContentOut(const void *ptr, int size); - void Out(const void *ptr, int size); - - String CalculateDigest(const String& authenticate) const; - -public: - Callback2 WhenContent; - Callback WhenStart; - Callback WhenDo; - - 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& SSL(bool b = true) { ssl = b; 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& ClearPost() { PostData(Null); GET(); return *this; } - - 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& Cookie(const String& cookie) { return Header("Cookie", cookie); } - - HttpRequest& StdHeaders(bool sh) { std_headers = sh; return *this; } - HttpRequest& NoStdHeaders() { return StdHeaders(false); } - HttpRequest& Accept(const String& a) { accept = a; return *this; } - HttpRequest& UserAgent(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 *p); - HttpRequest& ProxyAuth(const String& u, const String& p) { proxy_username = u; proxy_password = p; return *this; } - - HttpRequest& SSLProxy(const String& host, int port) { ssl_proxy_host = host; ssl_proxy_port = port; return *this; } - HttpRequest& SSLProxy(const char *p); - HttpRequest& SSLProxyAuth(const String& u, const String& p) { ssl_proxy_username = u; ssl_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 *id) { return header[id]; } - String operator[](const char *id) { return GetHeader(id); } - 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 { - BEGIN, START, DNS, - SSLPROXYREQUEST, SSLPROXYRESPONSE, SSLHANDSHAKE, - REQUEST, HEADER, BODY, - CHUNK_HEADER, CHUNK_BODY, TRAILER, - FINISHED, FAILED - }; - - bool Do(); - int GetPhase() const { return phase; } - String GetPhaseName() const; - 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(); - - void New(); - - HttpRequest(); - HttpRequest(const char *url); - - static void Trace(bool b = true); -}; +String WwwFormat(Time tm); + +String MIMECharsetName(byte charset); + +String UrlEncode(const char *s, const char *end); +String UrlEncode(const char *s, int len); +String UrlEncode(const String& s); +String UrlDecode(const char *s, const char *end); +String UrlDecode(const char *s, int len); +String UrlDecode(const String& s); + +String Base64Encode(const char *s, const char *end); +String Base64Encode(const char *s, int len); +String Base64Encode(const String& data); +String Base64Decode(const char *s, const char *end); +String Base64Decode(const char *s, int len); +String Base64Decode(const String& data); + +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 auxthread_t auxthread__ 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 }; + +struct SSLInfo { + String cipher; + bool cert_avail; + bool cert_verified; // Peer verification not yet working - this is always false + String cert_subject; + String cert_issuer; + Date cert_notbefore; + Date cert_notafter; + int cert_version; + String cert_serial; +}; + +class TcpSocket { + enum { BUFFERSIZE = 512 }; + enum { NONE, CONNECT, ACCEPT, SSL_CONNECTED }; + SOCKET socket; + int mode; + 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 global_timeout; + int start_time; + + int errorcode; + String errordesc; + + struct SSL { + virtual bool Start() = 0; + virtual bool Wait(dword flags, int end_time) = 0; + virtual int Send(const void *buffer, int maxlen) = 0; + virtual int Recv(void *buffer, int maxlen) = 0; + virtual void Close() = 0; + virtual dword Handshake() = 0; + + virtual ~SSL() {} + }; + + One ssl; + One sslinfo; + String cert, pkey; + bool asn1; + + struct SSLImp; + friend struct SSLImp; + + static SSL *(*CreateSSL)(TcpSocket& socket); + static SSL *CreateSSLImp(TcpSocket& socket); + + friend void InitCreateSSL(); + friend class IpAddrInfo; + + int GetEndTime() const; + bool RawWait(dword flags, int end_time); + bool Wait(dword events, int end_time); + SOCKET AcceptRaw(dword *ipaddr, int timeout_msec); + bool Open(int family, int type, int protocol); + int RawRecv(void *buffer, int maxlen); + int Recv(void *buffer, int maxlen); + int RawSend(const void *buffer, int maxlen); + int Send(const void *buffer, int maxlen); + bool RawConnect(addrinfo *info); + void RawClose(); + + void ReadBuffer(int end_time); + int Get_(); + int Peek_(); + int Peek_(int end_time); + int Peek(int end_time) { return ptr < end ? *ptr : Peek_(end_time); } + bool IsGlobalTimeout(); + + void Reset(); + + void SetSockError(const char *context, int code, const char *errdesc); + void SetSockError(const char *context, const char *errdesc); + void SetSockError(const char *context); + + static int GetErrorCode(); + static bool WouldBlock(); + static void Init(); + + +public: + Callback WhenWait; + + enum { ERROR_GLOBAL_TIMEOUT = -1000000 }; + + static String GetHostName(); + + int GetDone() const { return done; } + + 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); } + + 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 Put(const char *s, int len); + int Put(const String& s) { return Put(s.Begin(), s.GetLength()); } + + bool GetAll(void *buffer, int len); + String GetAll(int len); + String GetLine(int maxlen = 65536); + + bool PutAll(const char *s, int len); + bool PutAll(const String& s); + + bool StartSSL(); + bool IsSSL() const { return ssl; } + bool SSLHandshake(); + void SSLCertificate(const String& cert, const String& pkey, bool asn1); + const SSLInfo *GetSSLInfo() const { return ~sslinfo; } + + TcpSocket& Timeout(int ms) { timeout = ms; return *this; } + int GetTimeout() const { return timeout; } + TcpSocket& GlobalTimeout(int ms); + TcpSocket& NoGlobalTimeout() { return GlobalTimeout(Null); } + TcpSocket& Blocking() { return Timeout(Null); } + TcpSocket& WaitStep(int ms) { waitstep = ms; return *this; } + int GetWaitStep() const { return waitstep; } + + TcpSocket(); + ~TcpSocket() { Close(); } +}; + +class SocketWaitEvent { + Vector< Tuple2 > 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; + String f1, f2, f3; + VectorMap fields; + bool scgi; + + String operator[](const char *id) const { return fields.Get(id, Null); } + + bool Response(String& protocol, int& code, String& reason); + bool Request(String& method, String& uri, String& version); + + String GetProtocol() const { return f1; } + int GetCode() const; + String GetReason() const { return f3; } + + String GetMethod() const { return f1; } + String GetURI() const { return f2; } + String GetVersion() const { return f3; } + + int64 GetContentLength() const; + + void Clear(); + bool ParseAdd(const String& hdrs); + bool Parse(const String& hdrs); + bool ParseSCGI(const String& scgi_hdr); + + bool Read(TcpSocket& socket); +}; + +class HttpRequest : public TcpSocket { + int phase; + String data; + int count; + + HttpHeader header; + + String error; + String body; + + enum { + DEFAULT_HTTP_PORT = 80, + DEFAULT_HTTPS_PORT = 443 + }; + + 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 ssl_proxy_host; + int ssl_proxy_port; + String ssl_proxy_username; + String ssl_proxy_password; + String path; + bool ssl; + + 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 cookies; + + 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 StartConnect(); + void ProcessSSLProxyResponse(); + void AfterConnect(); + void StartRequest(); + bool SendingData(); + bool ReadingHeader(); + void StartBody(); + bool ReadingBody(); + void ReadingChunkHeader(); + void Finish(); + bool IsRequestTimeout(); + void CopyCookies(); + + void HttpError(const char *s); + void ContentOut(const void *ptr, int size); + void Out(const void *ptr, int size); + + String CalculateDigest(const String& authenticate) const; + +public: + Callback2 WhenContent; + Callback WhenStart; + Callback WhenDo; + + 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& SSL(bool b = true) { ssl = b; 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& ClearPost() { PostData(Null); GET(); return *this; } + + 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& Cookie(const String& cookie) { return Header("Cookie", cookie); } + + HttpRequest& StdHeaders(bool sh) { std_headers = sh; return *this; } + HttpRequest& NoStdHeaders() { return StdHeaders(false); } + HttpRequest& Accept(const String& a) { accept = a; return *this; } + HttpRequest& UserAgent(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 *p); + HttpRequest& ProxyAuth(const String& u, const String& p) { proxy_username = u; proxy_password = p; return *this; } + + HttpRequest& SSLProxy(const String& host, int port) { ssl_proxy_host = host; ssl_proxy_port = port; return *this; } + HttpRequest& SSLProxy(const char *p); + HttpRequest& SSLProxyAuth(const String& u, const String& p) { ssl_proxy_username = u; ssl_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 *id) { return header[id]; } + String operator[](const char *id) { return GetHeader(id); } + 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 { + BEGIN, START, DNS, + SSLPROXYREQUEST, SSLPROXYRESPONSE, SSLHANDSHAKE, + REQUEST, HEADER, BODY, + CHUNK_HEADER, CHUNK_BODY, TRAILER, + FINISHED, FAILED + }; + + bool Do(); + int GetPhase() const { return phase; } + String GetPhaseName() const; + 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(); + + void New(); + + HttpRequest(); + HttpRequest(const char *url); + + static void Trace(bool b = true); +}; + +bool HttpResponse(TcpSocket& socket, bool scgi, int code, const char *phrase, + const char *content_type = NULL, const String& data = Null, + const char *server = NULL); diff --git a/uppsrc/Core/InetUtil.cpp b/uppsrc/Core/InetUtil.cpp index 85b65b953..e3cfccea9 100644 --- a/uppsrc/Core/InetUtil.cpp +++ b/uppsrc/Core/InetUtil.cpp @@ -230,6 +230,7 @@ void HttpHeader::Clear() first_line.Clear(); fields.Clear(); f1 = f2 = f3 = Null; + scgi = false; } int64 HttpHeader::GetContentLength() const @@ -282,6 +283,7 @@ int CharFilterScgiHttp(int c) bool HttpHeader::ParseSCGI(const String& scgi_hdr) { Clear(); + scgi = true; String key, uri, qs; const char *b = scgi_hdr; const char *e = scgi_hdr.End(); @@ -366,4 +368,23 @@ bool HttpHeader::Response(String& protocol, int& code, String& reason) return !IsNull(code); } +bool HttpResponse(TcpSocket& socket, bool scgi, int code, const char *phrase, + const char *content_type, const String& data, const char *server) +{ + String r; + r << (scgi ? "Status: " : "HTTP/1.1 ") << code << ' ' << phrase << "\r\n" + "Date: " << WwwFormat(GetUtcTime()) << "\r\n" + "Server: " << (server ? server : "U++ based server") << "\r\n" + "Connection: close\r\n"; + if(data.GetCount()) + r << "Content-Length: " << data.GetCount() << "\r\n"; + if(content_type) + r << "Content-Type: " << content_type << "\r\n"; + r << "\r\n"; + LOG(r + data); + if(!socket.PutAll(r)) + return false; + return data.GetCount() == 0 || socket.PutAll(data); +} + END_UPP_NAMESPACE diff --git a/uppsrc/Core/src.tpp/Inet$en-us.tpp b/uppsrc/Core/src.tpp/Inet$en-us.tpp index 6f3ba481a..2cf146d38 100644 --- a/uppsrc/Core/src.tpp/Inet$en-us.tpp +++ b/uppsrc/Core/src.tpp/Inet$en-us.tpp @@ -63,14 +63,31 @@ st]_[@(0.0.255) char]_`*[*@3 s], [@(0.0.255) int]_[*@3 len])&] [_^String^ String][@(0.0.255) `&]_[*@3 data])&] [s2;%% Decodes binary [%-*@3 data] from [^http`:`/`/en`.wikipedia`.org`/wiki`/Base64^ Bas e64 format]..&] -[s3;%% &] -[s1;@(0.0.255)3 &] +[s3; &] +[s4; &] +[s5;:HttpResponse`(TcpSocket`&`,bool`,int`,const char`*`,const char`*`,const String`&`,const char`*`): [@(0.0.255) b +ool]_[* HttpResponse]([_^TcpSocket^ TcpSocket][@(0.0.255) `&]_[*@3 socket], +[@(0.0.255) bool]_[*@3 scgi], [@(0.0.255) int]_[*@3 code], [@(0.0.255) const]_[@(0.0.255) cha +r]_`*[*@3 phrase], [@(0.0.255) const]_[@(0.0.255) char]_`*[*@3 content`_type]_`=_NULL, +[@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_[*@3 data]_`=_Null, +[@(0.0.255) const]_[@(0.0.255) char]_`*[*@3 server]_`=_NULL)&] +[s2;%% This function sends simple HTTP response over [%-*@3 socket]. +If [%-*@3 scgi] is true, first line of response is formatted for +SCGI (rest is the same for normal HTTP and SCGI). [%-*@3 code] +and [%-*@3 phrase] set the first line of response, [%-*@3 content`_type] +sets the respective http header (if NULL, it is not send). If +[%-*@3 data] is not empty, `"content`-length`" header is sent and +[%-*@3 data] is sent after the header. [%-*@3 server] sets the `"server`" +field of http header, if not present, it is set to `"U`+`+ based +server`".&] +[s3; &] +[s0; &] [ {{10000@(113.42.0) [s0;%% [*@7;4 HttpHeader]]}}&] [s3; &] [s1;:HttpHeader`:`:struct: [@(0.0.255)3 struct][3 _][*3 HttpHeader]&] [s2;%% This simple structure simplifies parsing of http request and -response headers.&] -[s3; &] +response headers. It is also capable of parsing SCGI requests.&] +[s3;%% &] [s4; &] [s5;:HttpHeader`:`:first`_line: [_^String^ String]_[* first`_line]&] [s2;%% The first line of request/response `- the content of this @@ -88,6 +105,10 @@ different for response and request.&] Note that the same key can be present multiple times.&] [s3; &] [s4; &] +[s5;:HttpHeader`:`:scgi: [@(0.0.255) bool]_[* scgi]&] +[s2;%% Indicates that this header is a result of parsing scgi.&] +[s3; &] +[s4; &] [s5;:HttpHeader`:`:operator`[`]`(const char`*`)const: [_^String^ String]_[* operator`[`]]( [@(0.0.255) const]_[@(0.0.255) char]_`*[*@3 id])_[@(0.0.255) const]&] [s2;%% Returns the first http header field with key [%-*@3 id] (must