SA-MP/saco/httpclient.h

92 lines
2.6 KiB
C
Raw Normal View History

2023-10-26 15:32:16 +08:00
2023-10-28 18:45:48 +08:00
#define HTTP_GET 1
#define HTTP_POST 2
#define HTTP_HEAD 3
//----------------------------------------------------
#define MAX_ENTITY_LENGTH 500000
//----------------------------------------------------
#define HTTP_SUCCESS 0
#define HTTP_ERROR_BAD_HOST 1
#define HTTP_ERROR_NO_SOCKET 2
#define HTTP_ERROR_CANT_CONNECT 3
#define HTTP_ERROR_CANT_WRITE 4
#define HTTP_ERROR_CONTENT_TOO_BIG 5
#define HTTP_ERROR_MALFORMED_RESPONSE 6
//----------------------------------------------------
#define CONTENT_TYPE_UNKNOWN 0
#define CONTENT_TYPE_TEXT 1
#define CONTENT_TYPE_HTML 2
//----------------------------------------------------
#define USER_AGENT "SAMP/0.2.0"
#define GET_FORMAT "GET %s HTTP/1.0\r\nAccept: */*\r\nUser-Agent: %s\r\nReferer: http://%s\r\nHost: %s\r\n\r\n"
#define POST_FORMAT "POST %s HTTP/1.0\r\nAccept: */*\r\nUser-Agent: %s\r\nReferer: http://%s\r\nHost: %s\r\nContent-type: application/x-www-form-urlencoded\r\nContent-length: %u\r\n\r\n%s"
#define HEAD_FORMAT "HEAD %s HTTP/1.0\r\nAccept: */*\r\nUser-Agent: %s\r\nReferer: http://%s\r\nHost: %s\r\n\r\n"
//----------------------------------------------------
typedef struct{
unsigned short port; /* remote port */
int rtype; /* request type */
char * host; /* hostname */
char * file; /* GET/POST request file */
char * data; /* POST data (if rtype HTTP_POST) */
char * referer; /* http referer. */
} HTTP_REQUEST;
//----------------------------------------------------
typedef struct{
char * header;
char * response;
unsigned int header_len;
unsigned long response_len;
unsigned int response_code;
unsigned int content_type;
} HTTP_RESPONSE;
2023-10-26 15:32:16 +08:00
//----------------------------------------------------
class CHttpClient
{
private:
2023-10-28 18:45:48 +08:00
int m_iSocket;
HTTP_REQUEST m_Request;
HTTP_RESPONSE m_Response;
int m_iError;
bool Connect(char *szHost, int iPort);
void CloseConnection();
bool Send(char *szData);
int Recv(char *szBuffer, int iBufferSize);
void InitRequest(int iType, char *szURL, char *szPostData, char *szReferer);
void HandleEntity();
void Process();
2023-10-26 15:32:16 +08:00
public:
2023-10-28 18:45:48 +08:00
int ProcessURL(int iType, char *szURL, char *szData, char *szReferer);
bool GetHeaderValue(char *szHeaderName,char *szReturnBuffer, int iBufSize);
int GetResponseCode() { return m_Response.response_code; };
int GetContentType() { return m_Response.content_type; };
char *GetResponseHeaders() { return m_Response.header; };
char *GetDocument() { return m_Response.response; };
int GetDocumentLength() { return m_Response.response_len; };
2023-10-26 15:32:16 +08:00
CHttpClient();
~CHttpClient();
};
//----------------------------------------------------