summaryrefslogtreecommitdiff
path: root/protocols/CloudFile/src/http_request.h
blob: c9921cfd76e6624755f67ffbe31372a8403db0e2 (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
#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 : public MHttpRequest
{
private:
	CMStringA m_szUrl;

	void Init()
	{
		flags = NLHRF_HTTP11 | NLHRF_SSL | NLHRF_NODUMP;
	}

protected:
	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(cPair, length));
		AddHeader("Authorization", CMStringA(FORMAT, "Basic %s", ePair.get()));
	}

	void AddBearerAuthHeader(LPCSTR szValue)
	{
		AddHeader("Authorization", CMStringA(FORMAT, "Bearer %s", szValue));
	}

	void AddOAuthHeader(LPCSTR szValue)
	{
		AddHeader("Authorization", CMStringA(FORMAT, "OAuth %s", szValue));
	}

	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 AddUrlParameterWithEncode(const char *name, const char *valueFormat, ...)
	{
		va_list valueArgs;
		va_start(valueArgs, valueFormat);
		m_szUrl += m_szUrl.Find('?') == -1 ? '?' : '&';
		m_szUrl.AppendFormat("%s=", name);
		CMStringA value;
		value.AppendFormatV(valueFormat, valueArgs);
		m_szUrl += mir_urlEncode(value);
		va_end(valueArgs);
	}

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

		m_szUrl = url;
	}

	HttpRequest(int type, CMStringDataFormat, LPCSTR urlFormat, ...) :
		MHttpRequest(type)
	{
		Init();

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

	MHttpResponse* Send(HNETLIBUSER hConnection)
	{
		m_szUrl.Replace('\\', '/');
		return Netlib_HttpTransaction(hConnection, this);
	}
};

#endif //_HTTP_REQUEST_H_