summaryrefslogtreecommitdiffstats
path: root/Src/auth/post.cpp
blob: db75567104df75f3ebfc91cb6289c69e0d81ddd9 (plain) (blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "api.h"
#include "../xml/obj_xml.h"
#include "api_auth.h"
#include "../nu/AutoChar.h"
#include "../jnetlib/api_httpget.h"
#include "ifc_authcallback.h"
#include <api/service/waservicefactory.h>
#include <strsafe.h>


static const GUID internetConfigGroupGUID =
{
	0xc0a565dc, 0xcfe, 0x405a, { 0xa2, 0x7c, 0x46, 0x8b, 0xc, 0x8a, 0x3a, 0x5c }
};


#define USER_AGENT_SIZE (10 /*User-Agent*/ + 2 /*: */ + 6 /*Winamp*/ + 1 /*/*/ + 1 /*5*/ + 3/*.55*/ + 1 /*Null*/)
static void SetUserAgent(api_httpreceiver *http)
{
	char user_agent[USER_AGENT_SIZE] = {0};
	StringCchPrintfA(user_agent, USER_AGENT_SIZE, "User-Agent: Winamp/%S", WASABI_API_APP->main_getVersionNumString());
	http->addheader(user_agent);
}


#define HTTP_BUFFER_SIZE 8192

static int FeedXMLHTTP(api_httpreceiver *http, obj_xml *parser, bool *noData)
{
	char downloadedData[HTTP_BUFFER_SIZE] = {0};
	int xmlResult = API_XML_SUCCESS; 
	int downloadSize = http->get_bytes(downloadedData, HTTP_BUFFER_SIZE);
	if (downloadSize)
	{
		xmlResult = parser->xmlreader_feed((void *)downloadedData, downloadSize);
		*noData=false;
	}
	else	
		*noData = true;

	return xmlResult;
}


static int RunXMLDownload(api_httpreceiver *http, obj_xml *parser, ifc_authcallback *callback)
{
	int ret;
	bool noData;
	do
	{
		if (callback && callback->OnIdle())
		{
			return AUTH_ABORT;
		}
		else if (!callback)
		{
			Sleep(50);
		}

		ret = http->run();
		if (FeedXMLHTTP(http, parser, &noData) != API_XML_SUCCESS)
			return AUTH_ERROR_PARSING_XML;
	}
	while (ret == HTTPRECEIVER_RUN_OK);

	// finish off the data
	do
	{
		if (FeedXMLHTTP(http, parser, &noData) != API_XML_SUCCESS)
			return AUTH_ERROR_PARSING_XML;
	} while (!noData);

	parser->xmlreader_feed(0, 0);
	if (ret != HTTPRECEIVER_RUN_ERROR)
		return AUTH_SUCCESS;
	else
		return AUTH_CONNECTIONRESET;
}


int PostXML(const char *url, const char *post_data, obj_xml *parser, ifc_authcallback *callback)
{
	if (!parser)
		return AUTH_NOPARSER;

	api_httpreceiver *http = 0;
	waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(httpreceiverGUID);
	if (sf)
		http = (api_httpreceiver *)sf->getInterface();

	if (!http)
		return AUTH_NOHTTP;

	int use_proxy = 1;
	bool proxy80 = AGAVE_API_CONFIG->GetBool(internetConfigGroupGUID, L"proxy80", false);
	if (proxy80 && strstr(url, ":") && (!strstr(url, ":80/") && strstr(url, ":80") != (url + strlen(url) - 3)))
		use_proxy = 0;

	const wchar_t *proxy = use_proxy?AGAVE_API_CONFIG->GetString(internetConfigGroupGUID, L"proxy", 0):0;
	size_t clen = strlen(post_data);

	http->open(API_DNS_AUTODNS, HTTP_BUFFER_SIZE, (proxy && proxy[0]) ? (const char *)AutoChar(proxy) : NULL);
	SetUserAgent(http);

	char clen_header[1024] = {0};
	StringCbPrintfA(clen_header, sizeof(clen_header), "Content-Length: %u", clen);
	http->addheader(clen_header);
	http->addheader("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
	if (callback && callback->OnConnecting())
	{
		sf->releaseInterface(http);
		return AUTH_ABORT;
	}
	http->connect(url, 0, "POST");

	// POST the data
	api_connection *connection = http->GetConnection();
	if (connection)
	{
		if (callback && callback->OnIdle())
		{
			sf->releaseInterface(http);
			return AUTH_ABORT;
		}
		else if (!callback)
		{
			Sleep(50);
		}

		if (http->run() == -1)
			goto connection_failed;

		if (callback && callback->OnSending())
		{
			sf->releaseInterface(http);
			return AUTH_ABORT;
		}

		const char *dataIndex = post_data;
		while (clen)
		{
			if (callback && callback->OnIdle())
			{
				sf->releaseInterface(http);
				return AUTH_ABORT;
			}
			else if (!callback)
			{
				Sleep(50);
			}

			if (http->run() == -1)
				goto connection_failed;

			size_t lengthToSend = min(clen, connection->GetSendBytesAvailable());
			if (lengthToSend)
			{
				connection->send(dataIndex, (int)lengthToSend);
				dataIndex+=lengthToSend;
				clen-=lengthToSend;
			}			
		}
	}

	// retrieve reply
	if (callback && callback->OnReceiving())
	{
		sf->releaseInterface(http);
		return AUTH_ABORT;
	}

	int ret;
	do
	{
		if (callback && callback->OnIdle())
		{
			sf->releaseInterface(http);
			return AUTH_ABORT;
		}
		else if (!callback)
		{
			Sleep(50);
		}

		ret = http->run();
		if (ret == -1) // connection failed
			break;

		// ---- check our reply code ----
		int status = http->get_status();
		switch (status)
		{
		case HTTPRECEIVER_STATUS_CONNECTING:
		case HTTPRECEIVER_STATUS_READING_HEADERS:
			break;

		case HTTPRECEIVER_STATUS_READING_CONTENT:
			{
				int downloadError;
				downloadError = RunXMLDownload(http, parser, callback);
				sf->releaseInterface(http);
				return downloadError;
			}
			break;
		case HTTPRECEIVER_STATUS_ERROR:
		default:
			sf->releaseInterface(http);
			return AUTH_404;
		}
	}
	while (ret == HTTPRECEIVER_RUN_OK);


connection_failed:
	sf->releaseInterface(http);
	return AUTH_404;
}