From 14c4e44a0a91e1ad701d4ae3c58185d25118e64e Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 5 Jan 2024 15:54:03 +0300 Subject: Netlib: - NETLIBHTTPHEADER & NETLIBHTTPREQUEST obsoleted; - NETLIBHTTPREQUEST divided into MHttpRequest & MHttpResponse; - MHttpHeaders now manager headers both for MHttpRequest & MHttpResponse; --- include/m_netlib.h | 133 ++++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 63 deletions(-) (limited to 'include') diff --git a/include/m_netlib.h b/include/m_netlib.h index 0f8cdc2015..3ccf546860 100644 --- a/include/m_netlib.h +++ b/include/m_netlib.h @@ -39,7 +39,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // a pointer, I have decided to diverge from the rest of Miranda and go with // the convention that functions return false on failure and nonzero on success. -struct NETLIBHTTPREQUEST; +struct MHttpRequest; struct NETLIBOPENCONNECTION; #define NETLIB_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.112 Safari/537.36" @@ -402,16 +402,6 @@ EXTERN_C MIR_APP_DLL(NETLIBIPLIST*) Netlib_GetMyIp(bool bGlobalOnly); ///////////////////////////////////////////////////////////////////////////////////////// -struct NETLIBHTTPHEADER -{ - char *szName; - char *szValue; -}; - -EXTERN_C MIR_APP_DLL(char*) Netlib_GetHeader(const NETLIBHTTPREQUEST *pRec, const char *pszName); - -///////////////////////////////////////////////////////////////////////////////////////// - #define REQUEST_RESPONSE 0 // used by structure returned by MS_NETLIB_RECVHTTPHEADERS #define REQUEST_GET 1 #define REQUEST_POST 2 @@ -434,80 +424,93 @@ EXTERN_C MIR_APP_DLL(char*) Netlib_GetHeader(const NETLIBHTTPREQUEST *pRec, cons #define NLHRF_DUMPASTEXT 0x00080000 // dump posted and reply data as text. Headers are always dumped as text. #define NLHRF_NODUMPSEND 0x00100000 // do not dump sent message. -struct MIR_APP_EXPORT NETLIBHTTPREQUEST +struct MHttpHeader { - int requestType; // a REQUEST_ - uint32_t flags; - char *szUrl; - NETLIBHTTPHEADER *headers; // If this is a POST request and headers doesn't contain a Content-Length it'll be added automatically - int headersCount; - char *pData; // data to be sent in POST request. - int dataLength; // must be 0 for REQUEST_GET/REQUEST_CONNECT - int resultCode; - char *szResultDescr; - HNETLIBCONN nlc; - int timeout; + MHttpHeader(const char *_1, const char *_2) : + szName(mir_strdup(_1)), + szValue(mir_strdup(_2)) + {} - CMStringA GetCookies() const; + ptrA szName, szValue; +}; + +struct MIR_APP_EXPORT MHttpHeaders : public OBJLIST +{ + MHttpHeaders(); + ~MHttpHeaders(); __forceinline const char *operator[](const char *pszName) { - return Netlib_GetHeader(this, pszName); + return FindHeader(pszName); } + + void AddHeader(const char *pszName, const char *pszValue); + void DeleteHeader(const char *pszName); + char* FindHeader(const char *pszName) const; + CMStringA GetCookies() const; }; -///////////////////////////////////////////////////////////////////////////////////////// -// Free the memory used by a NETLIBHTTPREQUEST structure -// -// Returns true on success, false on failure (!! this is different to most of the rest of Miranda, but consistent with netlib) -// This should only be called on structures returned by -// MS_NETLIB_RECVHTTPHEADERS or MS_NETLIB_HTTPTRANSACTION. Calling it on an -// arbitrary structure will have disastrous results. -// Errors: ERROR_INVALID_PARAMETER +struct MIR_APP_EXPORT MHttpRequest : public MHttpHeaders, public MNonCopyable, public MZeroedObject +{ + MHttpRequest(); + ~MHttpRequest(); -EXTERN_C MIR_APP_DLL(bool) Netlib_FreeHttpRequest(NETLIBHTTPREQUEST*); + int requestType = REQUEST_GET; // a REQUEST_ + uint32_t flags; + CMStringA m_szUrl, m_szParam; + HNETLIBCONN nlc; + int timeout; + void *pUserInfo; -///////////////////////////////////////////////////////////////////////////////////////// -// smart pointer for NETLIBHTTPREQUEST via a call of Netlib_FreeHttpRequest() + void SetData(const void *pData, size_t cbLen); +}; + +struct MIR_APP_EXPORT MHttpResponse : public MHttpHeaders, public MNonCopyable, public MZeroedObject +{ + MHttpResponse(); + ~MHttpResponse(); + + int resultCode; + char *szUrl, *szResultDescr; + HNETLIBCONN nlc; + CMStringA body; +}; -#ifdef __cplusplus class NLHR_PTR { -protected: - NETLIBHTTPREQUEST *_p; + MHttpResponse *_p; public: - __forceinline explicit NLHR_PTR(NETLIBHTTPREQUEST *p) : _p(p) {} + __forceinline explicit NLHR_PTR(MHttpResponse *p) : _p(p) {} - __forceinline NETLIBHTTPREQUEST* operator=(INT_PTR i_p) + __forceinline MHttpResponse *operator=(INT_PTR i_p) { - return operator=((NETLIBHTTPREQUEST*)i_p); + return operator=((MHttpResponse *)i_p); } - __forceinline NETLIBHTTPREQUEST* operator=(NETLIBHTTPREQUEST *p) + __forceinline MHttpResponse* operator=(MHttpResponse *p) { if (_p) - Netlib_FreeHttpRequest(_p); + delete _p; _p = p; return _p; } - __forceinline operator NETLIBHTTPREQUEST*() const { return _p; } - __forceinline NETLIBHTTPREQUEST* operator->() const { return _p; } + __forceinline operator MHttpResponse*() const { return _p; } + __forceinline MHttpResponse* operator->() const { return _p; } __forceinline ~NLHR_PTR() { - Netlib_FreeHttpRequest(_p); + delete _p; } }; -struct MIR_APP_EXPORT MHttpRequest : public NETLIBHTTPREQUEST, public MZeroedObject -{ - MHttpRequest(); - ~MHttpRequest(); +EXTERN_C MIR_APP_DLL(bool) Netlib_FreeHttpRequest(MHttpResponse *); - CMStringA m_szUrl; - CMStringA m_szParam; - void *pUserInfo = nullptr; - - void AddHeader(const char *szName, const char *szValue); -}; +///////////////////////////////////////////////////////////////////////////////////////// +// Free the memory used by a MHttpRequest structure +// +// Returns true on success, false on failure (!! this is different to most of the rest of Miranda, but consistent with netlib) +// This should only be called on structures returned by +// MS_NETLIB_RECVHTTPHEADERS or MS_NETLIB_HTTPTRANSACTION. Calling it on an +// arbitrary structure will have disastrous results. +// Errors: ERROR_INVALID_PARAMETER template class MTHttpRequest : public MHttpRequest @@ -516,21 +519,25 @@ public: __forceinline MTHttpRequest() {} - typedef void (T::*MTHttpRequestHandler)(NETLIBHTTPREQUEST*, struct AsyncHttpRequest*); + typedef void (T::*MTHttpRequestHandler)(MHttpResponse*, struct AsyncHttpRequest*); MTHttpRequestHandler m_pFunc = nullptr; }; MIR_APP_DLL(MHttpRequest*) operator<<(MHttpRequest*, const INT_PARAM&); -MIR_APP_DLL(MHttpRequest*) operator<<(MHttpRequest*, const INT64_PARAM&); MIR_APP_DLL(MHttpRequest*) operator<<(MHttpRequest*, const CHAR_PARAM&); +MIR_APP_DLL(MHttpRequest*) operator<<(MHttpRequest*, const INT64_PARAM&); MIR_APP_DLL(MHttpRequest*) operator<<(MHttpRequest*, const WCHAR_PARAM&); -#endif +__forceinline MHttpRequest* operator<<(MHttpRequest &req, const INT_PARAM ¶m) { return &req << param; } +__forceinline MHttpRequest* operator<<(MHttpRequest &req, const CHAR_PARAM ¶m) { return &req << param; } +__forceinline MHttpRequest* operator<<(MHttpRequest &req, const INT64_PARAM ¶m) { return &req << param; } +__forceinline MHttpRequest* operator<<(MHttpRequest &req, const WCHAR_PARAM ¶m) { return &req << param; } + ///////////////////////////////////////////////////////////////////////////////////////// // Do an entire HTTP transaction // -// Returns a pointer to another NETLIBHTTPREQUEST structure on success, NULL on failure. +// Returns a pointer to another MHttpRequest structure on success, NULL on failure. // Call Netlib_FreeHttpRequest() to free this. // hUser must have been returned by MS_NETLIB_REGISTERUSER // nlhr.szUrl should be a full HTTP URL. If it does not start with http:// , that @@ -555,7 +562,7 @@ MIR_APP_DLL(MHttpRequest*) operator<<(MHttpRequest*, const WCHAR_PARAM&); // Errors: ERROR_INVALID_PARAMETER, ERROR_OUTOFMEMORY, anything from the above // list of functions -EXTERN_C MIR_APP_DLL(NETLIBHTTPREQUEST*) Netlib_HttpTransaction(HNETLIBUSER hNlu, NETLIBHTTPREQUEST *pRequest); +EXTERN_C MIR_APP_DLL(MHttpResponse *) Netlib_HttpTransaction(HNETLIBUSER hNlu, MHttpRequest *pRequest); ///////////////////////////////////////////////////////////////////////////////////////// // Send data over a connection @@ -753,7 +760,7 @@ struct WSHeader }; // connects to a WebSocket server -EXTERN_C MIR_APP_DLL(NETLIBHTTPREQUEST*) WebSocket_Connect(HNETLIBUSER, const char *szHost, NETLIBHTTPHEADER *pHeaders = nullptr); +EXTERN_C MIR_APP_DLL(MHttpResponse*) WebSocket_Connect(HNETLIBUSER, const char *szHost, const MHttpHeaders *pHeaders = nullptr); // validates that the provided buffer contains full WebSocket datagram EXTERN_C MIR_APP_DLL(bool) WebSocket_InitHeader(WSHeader &hdr, const void *pData, size_t bufSize); -- cgit v1.2.3