summaryrefslogtreecommitdiff
path: root/plugins/CloudFile/src/http_request.h
blob: 0ef03a22fb19933532d31964442affca1a18cfbb (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
165
166
167
168
#ifndef _HTTP_REQUEST_H_
#define _HTTP_REQUEST_H_

class HttpRequestException
{
	CMStringA message;

public:
	HttpRequestException(const char *message) :
		message(message)
	{
	}

	const char* what() const throw()
	{
		return message.c_str();
	}
};

class HttpRequest : protected NETLIBHTTPREQUEST
{
private:
	CMStringA m_szUrl;

	void Init(int type)
	{
		cbSize = sizeof(NETLIBHTTPREQUEST);
		requestType = type;
		flags = NLHRF_HTTP11 | NLHRF_SSL | NLHRF_NODUMPSEND | NLHRF_DUMPASTEXT;
		szUrl = NULL;
		headers = NULL;
		headersCount = 0;
		pData = NULL;
		dataLength = 0;
		resultCode = 0;
		szResultDescr = NULL;
		nlc = NULL;
		timeout = 0;
	}

protected:
	enum HttpRequestUrlFormat { FORMAT };

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

	void AddBasicAuthHeader(LPCSTR szLogin, LPCSTR szPassword)
	{
		size_t length = mir_strlen(szLogin) + mir_strlen(szPassword) + 1;
		ptrA cPair((char*)mir_calloc(length + 1));
		mir_snprintf(
			cPair,
			length,
			"%s:%s",
			szLogin,
			szPassword);

		ptrA ePair(mir_base64_encode((BYTE*)(char*)cPair, length));

		length = mir_strlen(ePair) + 7;
		char *value = (char*)mir_calloc(length + 1);
		mir_snprintf(
			value,
			length,
			"Basic %s",
			ePair);

		headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1));
		headers[headersCount].szName = mir_strdup("Authorization");
		headers[headersCount].szValue = value;
		headersCount++;
	}

	void AddBearerAuthHeader(LPCSTR szValue)
	{
		size_t length = mir_strlen(szValue) + 8;
		char *value = (char*)mir_calloc(length + 1);
		mir_snprintf(
			value,
			length,
			"Bearer %s",
			szValue);

		headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1));
		headers[headersCount].szName = mir_strdup("Authorization");
		headers[headersCount].szValue = value;
		headersCount++;
	}

	void AddOAuthHeader(LPCSTR szValue)
	{
		size_t length = mir_strlen(szValue) + 7;
		char *value = (char*)mir_calloc(length + 1);
		mir_snprintf(
			value,
			length,
			"OAuth %s",
			szValue);

		headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1));
		headers[headersCount].szName = mir_strdup("Authorization");
		headers[headersCount].szValue = value;
		headersCount++;
	}

	void AddUrlParameter(const char *urlFormat, ...)
	{
		va_list urlArgs;
		va_start(urlArgs, urlFormat);
		m_szUrl += m_szUrl.Find('?') == -1 ? '?' : '&';
		m_szUrl.AppendFormatV(urlFormat, urlArgs);
		va_end(urlArgs);
	}

	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);
	}

public:
	HttpRequest(int type, LPCSTR url)
	{
		Init(type);

		m_szUrl = url;
	}

	HttpRequest(int type, HttpRequestUrlFormat, LPCSTR urlFormat, ...)
	{
		Init(type);

		va_list formatArgs;
		va_start(formatArgs, urlFormat);
		m_szUrl.AppendFormatV(urlFormat, formatArgs);
		va_end(formatArgs);
	}

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

	NETLIBHTTPREQUEST* Send(HNETLIBUSER hConnection)
	{
		m_szUrl.Replace('\\', '/');
		szUrl = m_szUrl.GetBuffer();
		return Netlib_HttpTransaction(hConnection, this);
	}
};

#endif //_HTTP_REQUEST_H_