summaryrefslogtreecommitdiff
path: root/protocols/Steam/src/http_request.h
blob: b4f312111b7861381bb7067846cd974e48ade00f (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef _HTTP_REQUEST_H_
#define _HTTP_REQUEST_H_

#include "common.h"

enum HTTP_STATUS
{
	HTTP_STATUS_NONE = 0,
	HTTP_STATUS_OK = 200,
	HTTP_STATUS_FOUND = 302,
	HTTP_STATUS_BAD_REQUEST = 400,
	HTTP_STATUS_UNAUTHORIZED = 401,
	HTTP_STATUS_FORBIDDEN = 403,
	HTTP_STATUS_NOT_FOUND = 404,
	HTTP_STATUS_METHOD_NOT_ALLOWED = 405,
	HTTP_STATUS_TOO_MANY_REQUESTS = 429,
	HTTP_STATUS_SERVICE_UNAVAILABLE = 503,
	HTTP_STATUS_INSUFICIENTE_STORAGE = 507
};

class HttpRequest : protected NETLIBHTTPREQUEST//, public MZeroedObject
{
public:
	HttpRequest(HANDLE hNetlibUser, int request, LPCSTR url)
	{
		cbSize = sizeof(NETLIBHTTPREQUEST);
		pData = NULL;
		szUrl = NULL;
		headers = NULL;
		dataLength = 0;
		headersCount = 0;
		szResultDescr = NULL;
		flags = NLHRF_HTTP11 | NLHRF_NODUMPSEND | NLHRF_DUMPASTEXT;
		requestType = request;

		m_hNetlibUser = hNetlibUser;
		szUrl = NULL;
		m_szUrl = url;

		AddHeader("User-Agent", "Steam App / Miranda / 0.0.1");
	}

	~HttpRequest()
	{
		if (headers != NULL)
		{
			for (int i = 0; i < headersCount; i++)
			{
				mir_free(headers[i].szName);
				mir_free(headers[i].szValue);
			}
			mir_free(headers);
		}
		if (szUrl != NULL)
			mir_free(szUrl);
		if (pData != NULL)
			mir_free(pData);
	}

	void ResetFlags(int newFlags)
	{
		flags = newFlags;
	}

	void AddHeader(LPCSTR szName, LPCSTR szValue)
	{
		if (headers == NULL)
			headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER));
		else
			headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER) * (headersCount + 1));
		headers[headersCount].szName = mir_strdup(szName);
		headers[headersCount].szValue = mir_strdup(szValue);
		headersCount++;
	}

	void SetData(const char *data, size_t size)
	{
		if (pData != NULL)
			mir_free(pData);
		
		dataLength = (int)size;
		pData = (char*)mir_alloc(size);
		memcpy(pData, data, size);
	}

	void AddUrlPart(LPCSTR szPart)
	{
		m_szUrl += szPart;
	}

	void AddParameter(LPCSTR szName, LPCSTR szValue)
	{
		if (m_szUrl.find('?') == -1)
			m_szUrl.append("?").append(szName).append("=").append(szValue);
		else
			m_szUrl.append("&").append(szName).append("=").append(szValue);
	}

	void AddParameter(LPCSTR szValue)
	{
		if (m_szUrl.find('?') == -1)
			m_szUrl.append("?").append(szValue);
		else
			m_szUrl.append("&").append(szValue);
	}

	void SetTimeout(int timeout)
	{
		timeout = timeout;
	}

	NETLIBHTTPREQUEST *Send()
	{
		szUrl = mir_strdup(m_szUrl.c_str());
		NETLIBHTTPREQUEST *response = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)m_hNetlibUser, (LPARAM)this);
		mir_free(szUrl);szUrl = NULL;
		return response;
	}

private:
	std::string m_szUrl;
	HANDLE m_hNetlibUser;
};

class HttpGetRequest : public HttpRequest
{
public:
	HttpGetRequest(HANDLE hNetlibUser, LPCSTR url) : HttpRequest(hNetlibUser, REQUEST_GET, url) { }
};

class HttpPostRequest : public HttpRequest
{
public:
	HttpPostRequest(HANDLE hNetlibUser, LPCSTR url) : HttpRequest(hNetlibUser, REQUEST_POST, url) { }
};

class SecureHttpRequest : public HttpRequest
{
public:
	SecureHttpRequest(HANDLE hNetlibUser, int request, LPCSTR url)
		: HttpRequest(hNetlibUser, request, url)
	{
		flags = NLHRF_HTTP11 | NLHRF_SSL | NLHRF_NODUMPSEND | NLHRF_DUMPASTEXT;
	}
};

class SecureHttpGetRequest : public SecureHttpRequest
{
public:
	SecureHttpGetRequest(HANDLE hNetlibUser, LPCSTR url)
		: SecureHttpRequest(hNetlibUser, REQUEST_GET, url) { }
};

class SecureHttpPostRequest : public SecureHttpRequest
{
public:
	SecureHttpPostRequest(HANDLE hNetlibUser, LPCSTR url)
		: SecureHttpRequest(hNetlibUser, REQUEST_POST, url)
	{
		AddHeader("Content-Type", "application/x-www-form-urlencoded");
	}
};

#endif //_HTTP_REQUEST_H_