JD2022-TU1/main/extern/Camcam/Inc/camcam_httpclient.h

105 lines
3.3 KiB
C++

#ifndef CAMCAM_HTTPCLIENT_H
#define CAMCAM_HTTPCLIENT_H
#include "http_client_curl.h"
#include <string>
enum ErrorMsgTextId {
SILENT,
USE_MORE_INFO,
COULD_NOT_LOG_IN,
COULD_NOT_REFRESH_STATUS,
COULD_NOT_GET_ASSET_LIST,
COULD_NOT_LOG_OUT,
COULD_NOT_CREATE_ARTICLE,
COULD_NOT_GET_AUTH_TOKEN,
S_COULD_NOT_RESOLVE,
S_CONNECTION_FAILED,
S_ERROR_CODE,
YOUR_APPLICATION_WILL_BE_PATCHED_NOW,
YOUR_ACCOUNT_IS_BEING_USED_ON_ANOTHER_COMPUTER___,
FAILED_SCRIPT_REQUEST,
NOT_AVAILABLE_WHILE_DRIVER_USED,
YOUR_ACCOUNT_HAS_BEEN_DELETED,
YOUR_ACCOUNT_HAS_BEEN_BANNED,
COULD_NOT_CHECK_ARTICLE,
COULD_NOT_CANCEL_ARTICLE,
NUM_ACCOUNT_ERROR_MESSAGES,
};
#ifndef u32
#define u32 unsigned int
#endif
struct lua_State;
/**
* A http client, use one per thread
*/
class CamCamHttpClient
{
public:
CamCamHttpClient() : _WSErrorCode(0), _IdAccount(NULL) {}
// Download file identified by ZCC Id
bool downloadFile(u32 fileId, const char *fileDesc, std::string& actualFilename);
// Download file from CDN
bool downloadFileFromURL(const char *url, std::string& actualFilename, bool silentError=false);
// Return the web service (application server) error code (0=OK, -1=Unknown, >0=Error, explained by getErrorStr())
int getWSErrorCode() const { return _WSErrorCode; }
/// Return the error string, filled if a method has returned false
const char * getErrorStr() const { return _ErrorString.c_str(); }
// When called from another thread, ask the request to stop immediately
void abortCurrentRequest() { _HttpClient.abortRequest(); }
/// HttpClient RO accessor
const CHttpClient& getHttpClient() const { return _HttpClient; }
/// Set proxy config
void setProxy(const char *proxy) { _HttpClient.setProxy(proxy); }
/// Get Memory usage
#ifndef USER_MODE
virtual unsigned int getMemoryUsage() const;
#endif
const char *getLocErrorMsg(ErrorMsgTextId msgId);
protected:
friend class CCamCamAccount;
friend class RefreshCardStatusAction;
bool init(const SimpleString<16> *pIdAccount);
void initUserPassword();
void setCookie(const char *cookieName, const char *cookieValue);
void resetCookie();
/// Fill the cookie for CURL with a specific domain
void setDomainCookie(std::string domain);
/// Add new cookies in a specific domain
void addDomainCookie(std::string domain, std::string cookie, bool eraseIfExist = true);
bool doRequest(const char *action, const char *args, ErrorMsgTextId errorMsgId, const char *func, const char *moreErrorInfo="", bool expectErrorCode=true, bool actionIsFullURL=false, bool useSecureLine=false);
bool onPageError(const char *errorText, const char *func, bool logError=true);
bool writeDownloadedFile(const char *path);
CHttpClient _HttpClient;
SimpleString<1024> _ErrorString;
int _WSErrorCode;
const SimpleString<16> *_IdAccount;
static SimpleString<2> _LanguageCode; // shared among all threads (only modified at startup)
typedef std::map<std::string, std::string> CookieMap; // Domain, Cookies
CookieMap _Cookie;
friend CHttpClient *getChannelClient(int channel);
friend int HTTPRequest(lua_State *L, int channel, bool isPostRequest, bool isMultipart);
};
std::string getNextResultField(char const**ppSrc, char delimiter='|');
std::string getNextResultField(char **ppSrc, char delimiter='|');
bool isFilePresent(const char *filename);
#endif