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
|
#include <Networking/WebSocket.hpp>
namespace Networking
{
// Initialises a basic HTTP socket.
bool WebSocket::Start(const char *Address, const char *Username, const char *Password)
{
m_Internet = InternetOpenA("none", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if(!m_Internet)
return false;
m_Address = InternetConnectA(m_Internet, Address, INTERNET_DEFAULT_HTTPS_PORT, Username, Password, INTERNET_SERVICE_HTTP, 0, 0);
if(!m_Address)
return false;
return true;
}
// Receives a response from a request.
ByteArray WebSocket::Request(const char *File, const char *Header, ByteArray &Data)
{
DWORD ReceivedSize{};
ByteArray Response;
InternetHandle WebRequest = HttpOpenRequestA(m_Address, "POST", File, 0, 0, 0, INTERNET_FLAG_SECURE | INTERNET_FLAG_KEEP_CONNECTION, 0);
if(!WebRequest)
return Response;
// Make connection request.
bool Sent = HttpSendRequestA(WebRequest, Header, (DWORD)strlen(Header), Data.data(), (DWORD)Data.size());
if(Sent)
{
// Allocate a buffer to read the response into.
uint8_t *Block = (uint8_t *)malloc(0x1000);
// Read response.
while(InternetReadFile(WebRequest, Block, 0x1000, &ReceivedSize))
{
const size_t RequestRange = std::min< int >(0x1000, ReceivedSize);
for(size_t n{}; n < RequestRange; ++n)
Response.push_back(Block[n]);
}
// Free the buffer to avoid leaking memory.
free(Block);
}
return Response;
}
}
|