File size: 3,309 Bytes
d7c5875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
--[[
    Project: SAMP-API.lua <https://github.com/imring/SAMP-API.lua>
    Developers: imring, LUCHARE, FYP

    Special thanks:
        SAMemory (https://www.blast.hk/threads/20472/) for implementing the basic functions.
        SAMP-API (https://github.com/BlastHackNet/SAMP-API) for the structures and addresses.
]]

local sampapi = require 'sampapi'
local shared = sampapi.shared
local mt = require 'sampapi.metatype'
local ffi = require 'ffi'

shared.ffi.cdef[[
enum {
    RECEIVE_BUFFER_SIZE = 4096,
};

enum SRequestType {
    GET = 1,
    POST = 2,
    HEAD = 3,
};
typedef enum SRequestType SRequestType;

typedef struct SRequest SRequest;
#pragma pack(push, 1)
struct SRequest {
    short unsigned int m_nPort;
    int m_nType;
    char* m_szHost;
    char* m_szFile;
    char* m_szData;
    char* m_szReferer;
};
#pragma pack(pop)

enum SContentType {
    CONTENT_UNKNOWN = 0,
    CONTENT_TEXT = 1,
    CONTENT_HTML = 2,
};
typedef enum SContentType SContentType;

typedef struct SResponse SResponse;
#pragma pack(push, 1)
struct SResponse {
    char* m_szHeader;
    char* m_szResponse;
    unsigned int m_nHeaderLen;
    unsigned int m_nResponseLen;
    unsigned int m_nResponseCode;
    unsigned int m_nContentType;
};
#pragma pack(pop)

enum SErrorCode {
    ERROR_SUCCESS = 0,
    ERROR_BAD_HOST = 1,
    ERROR_NO_SOCKET = 2,
    ERROR_CANNOT_CONNECT = 3,
    ERROR_CANNOT_WRITE = 4,
    ERROR_TOO_BIG_CONTENT = 5,
    ERROR_INCORRECT_RESPONSE = 6,
};
typedef enum SErrorCode SErrorCode;

typedef struct SCHttpClient SCHttpClient;
#pragma pack(push, 1)
struct SCHttpClient {
    int m_nSocket;
    SRequest m_request;
    SResponse m_response;
    SErrorCode m_error;
};
#pragma pack(pop)
]]

shared.validate_size('struct SRequest', 0x16)
shared.validate_size('struct SResponse', 0x18)
shared.validate_size('struct SCHttpClient', 0x36)

local CHttpClient_constructor = ffi.cast('void(__thiscall*)(SCHttpClient*)', 0x22E0)
local CHttpClient_destructor = ffi.cast('void(__thiscall*)(SCHttpClient*)', 0x2340)
local function CHttpClient_new(...)
    local obj = ffi.gc(ffi.new('struct SCHttpClient[1]'), CHttpClient_destructor)
    CHttpClient_constructor(obj, ...)
    return obj
end

local SCHttpClient_mt = {
    GetHeaderValue = ffi.cast('bool(__thiscall*)(SCHttpClient*, const char*, char*, int)', sampapi.GetAddress(0x23B0)),
    InitializeRequest = ffi.cast('void(__thiscall*)(SCHttpClient*, int, const char*, const char*, const char*)', sampapi.GetAddress(0x24b0)),
    HandleEntity = ffi.cast('void(__thiscall*)(SCHttpClient*)', sampapi.GetAddress(0x2680)),
    Connect = ffi.cast('bool(__thiscall*)(SCHttpClient*, const char*, int)', sampapi.GetAddress(0x29A0)),
    Process = ffi.cast('void(__thiscall*)(SCHttpClient*)', sampapi.GetAddress(0x2A60)),
    Disconnect = ffi.cast('void(__thiscall*)(SCHttpClient*)', sampapi.GetAddress(0x2440)),
    ProcessUrl = ffi.cast('SErrorCode(__thiscall*)(SCHttpClient*, int, const char*, const char*, const char*)', sampapi.GetAddress(0x2C40)),
    Send = ffi.cast('bool(__thiscall*)(SCHttpClient*, const char*)', sampapi.GetAddress(0x2450)),
    Receive = ffi.cast('int(__thiscall*)(SCHttpClient*, char*, int)', sampapi.GetAddress(0x2490)),
}
mt.set_handler('struct SCHttpClient', '__index', SCHttpClient_mt)

return {
    new = CHttpClient_new,
}