ddraw/tests: Use compare_uint() in compare_float() instead of abs().
[wine.git] / programs / winetest / send.c
blob257d9b75f1d5b84c2d1c61bf586a062c416e81bd
1 /*
2 * HTTP handling functions.
4 * Copyright 2003 Ferenc Wagner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <winsock2.h>
22 #include <wininet.h>
23 #include <stdio.h>
24 #include <errno.h>
26 #include "winetest.h"
28 #define USER_AGENT "Winetest Shell"
29 #define SERVER_NAME "test.winehq.org"
30 #define URL_PATH "/submit"
31 #define SEP "--8<--cut-here--8<--"
32 #define CONTENT_HEADERS "Content-Type: multipart/form-data; boundary=\"" SEP "\"\r\n" \
33 "Content-Length: %u\r\n\r\n"
34 static const char body1[] = "--" SEP "\r\n"
35 "Content-Disposition: form-data; name=\"reportfile\"; filename=\"%s\"\r\n"
36 "Content-Type: application/octet-stream\r\n\r\n";
37 static const char body2[] = "\r\n--" SEP "\r\n"
38 "Content-Disposition: form-data; name=\"submit\"\r\n\r\n"
39 "Upload File\r\n"
40 "--" SEP "--\r\n";
42 static SOCKET
43 open_http (const char *server)
45 WSADATA wsad;
46 struct sockaddr_in sa;
47 SOCKET s;
49 report (R_STATUS, "Opening HTTP connection to %s", server);
50 if (WSAStartup (MAKEWORD (2,2), &wsad)) return INVALID_SOCKET;
52 sa.sin_family = AF_INET;
53 sa.sin_port = htons (80);
54 sa.sin_addr.s_addr = inet_addr (server);
55 memset (sa.sin_zero, 0, sizeof(sa.sin_zero));
56 if (sa.sin_addr.s_addr == INADDR_NONE) {
57 struct hostent *host = gethostbyname (server);
58 if (!host) {
59 report (R_ERROR, "Hostname lookup failed for %s", server);
60 goto failure;
62 sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
64 s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
65 if (s == INVALID_SOCKET) {
66 report (R_ERROR, "Can't open network socket: %d",
67 WSAGetLastError ());
68 goto failure;
70 if (!connect (s, (struct sockaddr*)&sa, sizeof (struct sockaddr_in)))
71 return s;
73 report (R_ERROR, "Can't connect: %d", WSAGetLastError ());
74 closesocket (s);
75 failure:
76 WSACleanup ();
77 return INVALID_SOCKET;
80 static int
81 close_http (SOCKET s)
83 int ret;
85 ret = closesocket (s);
86 return (WSACleanup () || ret);
89 static int
90 send_buf (SOCKET s, const char *buf, size_t length)
92 int sent;
94 while (length > 0) {
95 sent = send (s, buf, length, 0);
96 if (sent == SOCKET_ERROR) {
97 if (errno == EINTR) continue;
98 return 1;
100 buf += sent;
101 length -= sent;
103 return 0;
106 static int WINAPIV send_str (SOCKET s, ...)
108 va_list ap;
109 char *p;
110 int ret;
111 size_t len;
113 va_start (ap, s);
114 p = vstrmake (&len, ap);
115 va_end (ap);
116 ret = send_buf (s, p, len);
117 free(p);
118 return ret;
121 static int
122 send_file_direct (const char * url, const char *name)
124 SOCKET s;
125 HANDLE file;
126 #define BUFLEN 8192
127 char buffer[BUFLEN+1];
128 DWORD bytes_read, filesize;
129 size_t total, count;
130 char *str;
131 int ret;
133 /* RFC 2616 */
134 static const char head[] = "POST " URL_PATH " HTTP/1.0\r\n"
135 "Host: " SERVER_NAME "\r\n"
136 "User-Agent: " USER_AGENT "\r\n"
137 CONTENT_HEADERS
138 "\r\n";
140 if (url) {
141 report (R_WARNING, "Can't submit to an alternative URL");
142 return 0;
145 s = open_http (SERVER_NAME);
146 if (s == INVALID_SOCKET) return 1;
148 file = CreateFileA( name, GENERIC_READ,
149 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
150 NULL, OPEN_EXISTING, 0, NULL );
152 if ((file == INVALID_HANDLE_VALUE) &&
153 (GetLastError() == ERROR_INVALID_PARAMETER)) {
154 /* FILE_SHARE_DELETE not supported on win9x */
155 file = CreateFileA( name, GENERIC_READ,
156 FILE_SHARE_READ | FILE_SHARE_WRITE,
157 NULL, OPEN_EXISTING, 0, NULL );
159 if (file == INVALID_HANDLE_VALUE)
161 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
162 goto abort1;
165 report (R_STATUS, "Sending header");
166 filesize = GetFileSize( file, NULL );
167 str = strmake (&total, body1, name);
168 ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
169 send_buf (s, str, total);
170 free(str);
171 if (ret) {
172 report (R_WARNING, "Error sending header: %d, %d",
173 errno, WSAGetLastError ());
174 goto abort2;
177 report (R_STATUS, "Sending %u bytes of data", filesize);
178 report (R_PROGRESS, 2, filesize);
179 total = 0;
180 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
181 if (aborting) goto abort2;
182 if (!bytes_read) break;
183 total += bytes_read;
184 if (total > filesize) bytes_read -= total - filesize;
185 if (send_buf (s, buffer, bytes_read)) {
186 report (R_WARNING, "Error sending body: %d, %d",
187 errno, WSAGetLastError ());
188 goto abort2;
190 report (R_DELTA, bytes_read, "Network transfer: In progress");
192 CloseHandle( file );
194 if (send_buf (s, body2, sizeof body2 - 1)) {
195 report (R_WARNING, "Error sending trailer: %d, %d",
196 errno, WSAGetLastError ());
197 goto abort1;
199 report (R_DELTA, 0, "Network transfer: Done");
201 total = 0;
202 while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
203 if ((signed)bytes_read == SOCKET_ERROR) {
204 if (errno == EINTR) continue;
205 report (R_WARNING, "Error receiving reply: %d, %d",
206 errno, WSAGetLastError ());
207 goto abort1;
209 total += bytes_read;
210 if (total == BUFLEN) {
211 report (R_WARNING, "Buffer overflow");
212 goto abort1;
215 if (close_http (s)) {
216 report (R_WARNING, "Error closing connection: %d, %d",
217 errno, WSAGetLastError ());
218 return 1;
221 str = strmake (&count, "Received %s (%d bytes).\n",
222 name, filesize);
223 ret = total < count || memcmp (str, buffer + total - count, count) != 0;
224 free(str);
225 if (ret) {
226 buffer[total] = 0;
227 str = strstr (buffer, "\r\n\r\n");
228 if (!str) str = buffer;
229 else str = str + 4;
230 report (R_ERROR, "Can't submit logfile '%s'. "
231 "Server response: %s", name, str);
233 return ret;
235 abort2:
236 CloseHandle( file );
237 abort1:
238 close_http (s);
239 return 1;
242 static int
243 send_file_wininet (const char *url, const char *name)
245 int ret = 0;
246 HMODULE wininet_mod = NULL;
247 HINTERNET (WINAPI *pInternetOpen)(LPCSTR agent, DWORD access_type, LPCSTR proxy_name, LPCSTR proxy_bypass, DWORD flags);
248 HINTERNET (WINAPI *pInternetConnect)(HINTERNET session, LPCSTR server_name, INTERNET_PORT server_port, LPCSTR username, LPCSTR password, DWORD service, DWORD flags, DWORD_PTR *context);
249 HINTERNET (WINAPI *pHttpOpenRequest)(HINTERNET connection, LPCSTR verb, LPCSTR object_name, LPCSTR version, LPCSTR referer, LPCSTR *accept_types, DWORD flags, DWORD_PTR context);
250 BOOL (WINAPI *pHttpSendRequestEx)(HINTERNET request, LPINTERNET_BUFFERSA buffers_in, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
251 BOOL (WINAPI *pInternetWriteFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written);
252 BOOL (WINAPI *pHttpEndRequest)(HINTERNET request, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
253 BOOL (WINAPI *pInternetReadFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read);
254 BOOL (WINAPI *pInternetCloseHandle)(HINTERNET Handle) = NULL;
255 HANDLE file = INVALID_HANDLE_VALUE;
256 DWORD filesize, bytes_read, bytes_written;
257 size_t total, count;
258 char *str = NULL;
259 HINTERNET session = NULL;
260 HINTERNET connection = NULL;
261 HINTERNET request = NULL;
262 INTERNET_BUFFERSA buffers_in = { 0 };
263 char buffer[BUFLEN+1];
264 URL_COMPONENTSA uc = { 0 };
266 static const char extra_headers[] =
267 CONTENT_HEADERS;
269 wininet_mod = LoadLibraryA("wininet.dll");
270 if (wininet_mod == NULL)
271 goto done;
272 pInternetOpen = (void *)GetProcAddress(wininet_mod, "InternetOpenA");
273 pInternetConnect = (void *)GetProcAddress(wininet_mod, "InternetConnectA");
274 pHttpOpenRequest = (void *)GetProcAddress(wininet_mod, "HttpOpenRequestA");
275 pHttpSendRequestEx = (void *)GetProcAddress(wininet_mod, "HttpSendRequestExA");
276 pInternetWriteFile = (void *)GetProcAddress(wininet_mod, "InternetWriteFile");
277 pHttpEndRequest = (void *)GetProcAddress(wininet_mod, "HttpEndRequestA");
278 pInternetReadFile = (void *)GetProcAddress(wininet_mod, "InternetReadFile");
279 pInternetCloseHandle = (void *)GetProcAddress(wininet_mod, "InternetCloseHandle");
280 if (pInternetOpen == NULL || pInternetConnect == NULL || pHttpOpenRequest == NULL || pHttpSendRequestEx == NULL || pHttpEndRequest == NULL ||
281 pInternetWriteFile == NULL || pInternetReadFile == NULL || pInternetCloseHandle == NULL) {
282 goto done;
284 if (url) {
285 BOOL (WINAPI *pInternetCrackUrlA)(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp);
286 pInternetCrackUrlA = (void *)GetProcAddress(wininet_mod, "InternetCrackUrlA");
287 if (pInternetCrackUrlA == NULL)
288 goto done;
289 uc.dwStructSize = sizeof(uc);
290 uc.dwSchemeLength = uc.dwHostNameLength = uc.dwUserNameLength =
291 uc.dwPasswordLength = uc.dwUrlPathLength = uc.dwExtraInfoLength =
292 strlen(url) + 1;
293 uc.lpszScheme = xalloc(uc.dwSchemeLength);
294 uc.lpszHostName = xalloc(uc.dwHostNameLength);
295 uc.lpszUserName = xalloc(uc.dwUserNameLength);
296 uc.lpszPassword = xalloc(uc.dwPasswordLength);
297 uc.lpszUrlPath = xalloc(uc.dwUrlPathLength);
298 uc.lpszExtraInfo = xalloc(uc.dwExtraInfoLength);
299 if (!pInternetCrackUrlA(url, 0, 0, &uc)) {
300 report (R_WARNING, "Can't parse submit URL '%s'", url);
301 goto done;
303 if ((uc.nScheme != INTERNET_SCHEME_HTTP && uc.nScheme != INTERNET_SCHEME_HTTPS) || *uc.lpszExtraInfo) {
304 report (R_WARNING, "Can't submit report to scheme %s or extra info '%s'", uc.lpszScheme, uc.lpszExtraInfo);
305 goto done;
308 } else {
309 uc.nScheme = INTERNET_SCHEME_HTTP;
310 uc.lpszHostName = xstrdup(SERVER_NAME);
311 uc.nPort = INTERNET_DEFAULT_HTTP_PORT;
312 uc.lpszUserName = xstrdup("");
313 uc.lpszPassword = xstrdup("");
314 uc.lpszUrlPath = xstrdup(URL_PATH);
317 ret = 1;
319 file = CreateFileA( name, GENERIC_READ,
320 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
321 NULL, OPEN_EXISTING, 0, NULL );
323 if ((file == INVALID_HANDLE_VALUE) &&
324 (GetLastError() == ERROR_INVALID_PARAMETER)) {
325 /* FILE_SHARE_DELETE not supported on win9x */
326 file = CreateFileA( name, GENERIC_READ,
327 FILE_SHARE_READ | FILE_SHARE_WRITE,
328 NULL, OPEN_EXISTING, 0, NULL );
330 if (file == INVALID_HANDLE_VALUE) {
331 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
332 goto done;
335 report (R_STATUS, "Opening %s connection to %s:%d",
336 (uc.nScheme == INTERNET_SCHEME_HTTP ? "http" : "https"),
337 uc.lpszHostName, uc.nPort);
338 session = pInternetOpen (USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
339 if (session == NULL) {
340 report (R_WARNING, "Unable to open connection, error %u", GetLastError());
341 goto done;
343 connection = pInternetConnect (session, uc.lpszHostName, uc.nPort, uc.lpszUserName, uc.lpszPassword, INTERNET_SERVICE_HTTP, 0, 0);
344 if (connection == NULL) {
345 report (R_WARNING, "Unable to connect, error %u", GetLastError());
346 goto done;
348 request = pHttpOpenRequest (connection, "POST", uc.lpszUrlPath, NULL, NULL, NULL,
349 INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI |
350 INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD | (uc.nScheme == INTERNET_SCHEME_HTTPS ? INTERNET_FLAG_SECURE : 0), 0);
351 if (request == NULL) {
352 report (R_WARNING, "Unable to open request, error %u", GetLastError());
353 goto done;
356 report (R_STATUS, "Sending request");
357 filesize = GetFileSize( file, NULL );
358 str = strmake (&total, body1, name);
359 memset(&buffers_in, 0, sizeof(INTERNET_BUFFERSA));
360 buffers_in.dwStructSize = sizeof(INTERNET_BUFFERSA);
361 buffers_in.dwBufferTotal = filesize + total + sizeof body2 - 1;
362 buffers_in.lpcszHeader = strmake (&count, extra_headers, buffers_in.dwBufferTotal);
363 buffers_in.dwHeadersLength = count;
364 if (! pHttpSendRequestEx(request, &buffers_in, NULL, 0, 0)) {
365 report (R_WARNING, "Unable to send request, error %u", GetLastError());
366 goto done;
369 if (! pInternetWriteFile(request, str, total, &bytes_written) || bytes_written != total) {
370 report (R_WARNING, "Unable to write body data, error %u", GetLastError());
371 goto done;
374 report (R_STATUS, "Sending %u bytes of data", filesize);
375 report (R_PROGRESS, 2, filesize);
376 total = 0;
377 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
378 if (aborting) goto done;
379 if (!bytes_read) break;
380 total += bytes_read;
381 if (total > filesize) bytes_read -= total - filesize;
382 if (! pInternetWriteFile (request, buffer, bytes_read, &bytes_written) || bytes_written != bytes_read) {
383 report (R_WARNING, "Error sending body: %u", GetLastError ());
384 goto done;
386 report (R_DELTA, bytes_read, "Network transfer: In progress");
389 if (! pInternetWriteFile(request, body2, sizeof body2 - 1, &bytes_written) || bytes_written != sizeof body2 - 1) {
390 report (R_WARNING, "Unable to write final body data, error %u", GetLastError());
391 goto done;
393 if (! pHttpEndRequest(request, NULL, 0, 0)) {
394 report (R_WARNING, "Unable to end request, error %u", GetLastError());
395 goto done;
397 report (R_DELTA, 0, "Network transfer: Done");
399 total = 0;
402 if (! pInternetReadFile(request, buffer+total, BUFLEN-total, &bytes_read)) {
403 report (R_WARNING, "Error receiving reply: %u", GetLastError ());
404 goto done;
406 total += bytes_read;
407 if (total == BUFLEN) {
408 report (R_WARNING, "Buffer overflow");
409 goto done;
412 while (bytes_read != 0);
414 free(str);
415 str = strmake (&count, "Received %s (%d bytes).\n",
416 name, filesize);
417 if (total < count || memcmp (str, buffer + total - count, count) != 0) {
418 buffer[total] = 0;
419 report (R_ERROR, "Can't submit logfile '%s'. "
420 "Server response: %s", name, buffer);
423 done:
424 free(uc.lpszScheme);
425 free(uc.lpszHostName);
426 free(uc.lpszUserName);
427 free(uc.lpszPassword);
428 free(uc.lpszUrlPath);
429 free(uc.lpszExtraInfo);
430 free((void *)buffers_in.lpcszHeader);
431 free(str);
432 if (pInternetCloseHandle != NULL && request != NULL)
433 pInternetCloseHandle (request);
434 if (pInternetCloseHandle != NULL && connection != NULL)
435 pInternetCloseHandle (connection);
436 if (pInternetCloseHandle != NULL && session != NULL)
437 pInternetCloseHandle (session);
438 if (file != INVALID_HANDLE_VALUE)
439 CloseHandle (file);
440 if (wininet_mod != NULL)
441 FreeLibrary (wininet_mod);
443 return ret;
447 send_file (const char *url, const char *name)
449 return send_file_wininet( url, name ) || send_file_direct( url, name );