fusion/tests: Simplify directory recursion and avoid redundant buffers.
[wine.git] / programs / winetest / send.c
blob1b0b94a9baa7d48a223489e81e3478a2b8fa9196
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
107 send_str (SOCKET s, ...)
109 va_list ap;
110 char *p;
111 int ret;
112 size_t len;
114 va_start (ap, s);
115 p = vstrmake (&len, ap);
116 va_end (ap);
117 if (!p) return 1;
118 ret = send_buf (s, p, len);
119 heap_free (p);
120 return ret;
123 static int
124 send_file_direct (const char * url, const char *name)
126 SOCKET s;
127 HANDLE file;
128 #define BUFLEN 8192
129 char buffer[BUFLEN+1];
130 DWORD bytes_read, filesize;
131 size_t total, count;
132 char *str;
133 int ret;
135 /* RFC 2616 */
136 static const char head[] = "POST " URL_PATH " HTTP/1.0\r\n"
137 "Host: " SERVER_NAME "\r\n"
138 "User-Agent: " USER_AGENT "\r\n"
139 CONTENT_HEADERS
140 "\r\n";
142 if (url) {
143 report (R_WARNING, "Can't submit to an alternative URL");
144 return 0;
147 s = open_http (SERVER_NAME);
148 if (s == INVALID_SOCKET) return 1;
150 file = CreateFileA( name, GENERIC_READ,
151 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
152 NULL, OPEN_EXISTING, 0, NULL );
154 if ((file == INVALID_HANDLE_VALUE) &&
155 (GetLastError() == ERROR_INVALID_PARAMETER)) {
156 /* FILE_SHARE_DELETE not supported on win9x */
157 file = CreateFileA( name, GENERIC_READ,
158 FILE_SHARE_READ | FILE_SHARE_WRITE,
159 NULL, OPEN_EXISTING, 0, NULL );
161 if (file == INVALID_HANDLE_VALUE)
163 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
164 goto abort1;
166 filesize = GetFileSize( file, NULL );
167 if (filesize > 1.5*1024*1024) {
168 report (R_WARNING,
169 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
170 filesize/1024.0/1024);
171 filesize = (DWORD) 1.5*1024*1024;
174 report (R_STATUS, "Sending header");
175 str = strmake (&total, body1, name);
176 ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
177 send_buf (s, str, total);
178 heap_free (str);
179 if (ret) {
180 report (R_WARNING, "Error sending header: %d, %d",
181 errno, WSAGetLastError ());
182 goto abort2;
185 report (R_STATUS, "Sending %u bytes of data", filesize);
186 report (R_PROGRESS, 2, filesize);
187 total = 0;
188 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
189 if (aborting) goto abort2;
190 if (!bytes_read) break;
191 total += bytes_read;
192 if (total > filesize) bytes_read -= total - filesize;
193 if (send_buf (s, buffer, bytes_read)) {
194 report (R_WARNING, "Error sending body: %d, %d",
195 errno, WSAGetLastError ());
196 goto abort2;
198 report (R_DELTA, bytes_read, "Network transfer: In progress");
200 CloseHandle( file );
202 if (send_buf (s, body2, sizeof body2 - 1)) {
203 report (R_WARNING, "Error sending trailer: %d, %d",
204 errno, WSAGetLastError ());
205 goto abort1;
207 report (R_DELTA, 0, "Network transfer: Done");
209 total = 0;
210 while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
211 if ((signed)bytes_read == SOCKET_ERROR) {
212 if (errno == EINTR) continue;
213 report (R_WARNING, "Error receiving reply: %d, %d",
214 errno, WSAGetLastError ());
215 goto abort1;
217 total += bytes_read;
218 if (total == BUFLEN) {
219 report (R_WARNING, "Buffer overflow");
220 goto abort1;
223 if (close_http (s)) {
224 report (R_WARNING, "Error closing connection: %d, %d",
225 errno, WSAGetLastError ());
226 return 1;
229 str = strmake (&count, "Received %s (%d bytes).\n",
230 name, filesize);
231 ret = total < count || memcmp (str, buffer + total - count, count) != 0;
232 heap_free (str);
233 if (ret) {
234 buffer[total] = 0;
235 str = strstr (buffer, "\r\n\r\n");
236 if (!str) str = buffer;
237 else str = str + 4;
238 report (R_ERROR, "Can't submit logfile '%s'. "
239 "Server response: %s", name, str);
241 return ret;
243 abort2:
244 CloseHandle( file );
245 abort1:
246 close_http (s);
247 return 1;
250 static int
251 send_file_wininet (const char *url, const char *name)
253 int ret = 0;
254 HMODULE wininet_mod = NULL;
255 HINTERNET (WINAPI *pInternetOpen)(LPCSTR agent, DWORD access_type, LPCSTR proxy_name, LPCSTR proxy_bypass, DWORD flags);
256 HINTERNET (WINAPI *pInternetConnect)(HINTERNET session, LPCSTR server_name, INTERNET_PORT server_port, LPCSTR username, LPCSTR password, DWORD service, DWORD flags, DWORD_PTR *context);
257 HINTERNET (WINAPI *pHttpOpenRequest)(HINTERNET connection, LPCSTR verb, LPCSTR object_name, LPCSTR version, LPCSTR referer, LPCSTR *accept_types, DWORD flags, DWORD_PTR context);
258 BOOL (WINAPI *pHttpSendRequestEx)(HINTERNET request, LPINTERNET_BUFFERSA buffers_in, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
259 BOOL (WINAPI *pInternetWriteFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written);
260 BOOL (WINAPI *pHttpEndRequest)(HINTERNET request, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
261 BOOL (WINAPI *pInternetReadFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read);
262 BOOL (WINAPI *pInternetCloseHandle)(HINTERNET Handle) = NULL;
263 HANDLE file = INVALID_HANDLE_VALUE;
264 DWORD filesize, bytes_read, bytes_written;
265 size_t total, count;
266 char *str = NULL;
267 HINTERNET session = NULL;
268 HINTERNET connection = NULL;
269 HINTERNET request = NULL;
270 INTERNET_BUFFERSA buffers_in = { 0 };
271 char buffer[BUFLEN+1];
272 URL_COMPONENTSA uc = { 0 };
274 static const char extra_headers[] =
275 CONTENT_HEADERS;
277 wininet_mod = LoadLibraryA("wininet.dll");
278 if (wininet_mod == NULL)
279 goto done;
280 pInternetOpen = (void *)GetProcAddress(wininet_mod, "InternetOpenA");
281 pInternetConnect = (void *)GetProcAddress(wininet_mod, "InternetConnectA");
282 pHttpOpenRequest = (void *)GetProcAddress(wininet_mod, "HttpOpenRequestA");
283 pHttpSendRequestEx = (void *)GetProcAddress(wininet_mod, "HttpSendRequestExA");
284 pInternetWriteFile = (void *)GetProcAddress(wininet_mod, "InternetWriteFile");
285 pHttpEndRequest = (void *)GetProcAddress(wininet_mod, "HttpEndRequestA");
286 pInternetReadFile = (void *)GetProcAddress(wininet_mod, "InternetReadFile");
287 pInternetCloseHandle = (void *)GetProcAddress(wininet_mod, "InternetCloseHandle");
288 if (pInternetOpen == NULL || pInternetConnect == NULL || pHttpOpenRequest == NULL || pHttpSendRequestEx == NULL || pHttpEndRequest == NULL ||
289 pInternetWriteFile == NULL || pInternetReadFile == NULL || pInternetCloseHandle == NULL) {
290 goto done;
292 if (url) {
293 BOOL (WINAPI *pInternetCrackUrlA)(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp);
294 pInternetCrackUrlA = (void *)GetProcAddress(wininet_mod, "InternetCrackUrlA");
295 if (pInternetCrackUrlA == NULL)
296 goto done;
297 uc.dwStructSize = sizeof(uc);
298 uc.dwSchemeLength = uc.dwHostNameLength = uc.dwUserNameLength =
299 uc.dwPasswordLength = uc.dwUrlPathLength = uc.dwExtraInfoLength =
300 strlen(url) + 1;
301 uc.lpszScheme = heap_alloc (uc.dwSchemeLength);
302 uc.lpszHostName = heap_alloc (uc.dwHostNameLength);
303 uc.lpszUserName = heap_alloc (uc.dwUserNameLength);
304 uc.lpszPassword = heap_alloc (uc.dwPasswordLength);
305 uc.lpszUrlPath = heap_alloc (uc.dwUrlPathLength);
306 uc.lpszExtraInfo = heap_alloc (uc.dwExtraInfoLength);
307 if (!pInternetCrackUrlA(url, 0, 0, &uc)) {
308 report (R_WARNING, "Can't parse submit URL '%s'", url);
309 goto done;
311 if ((uc.nScheme != INTERNET_SCHEME_HTTP && uc.nScheme != INTERNET_SCHEME_HTTPS) || *uc.lpszExtraInfo) {
312 report (R_WARNING, "Can't submit report to scheme %s or extra info '%s'", uc.lpszScheme, uc.lpszExtraInfo);
313 goto done;
316 } else {
317 uc.nScheme = INTERNET_SCHEME_HTTP;
318 uc.lpszHostName = heap_strdup (SERVER_NAME);
319 uc.nPort = INTERNET_DEFAULT_HTTP_PORT;
320 uc.lpszUserName = heap_strdup ("");
321 uc.lpszPassword = heap_strdup ("");
322 uc.lpszUrlPath = heap_strdup (URL_PATH);
325 ret = 1;
327 file = CreateFileA( name, GENERIC_READ,
328 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
329 NULL, OPEN_EXISTING, 0, NULL );
331 if ((file == INVALID_HANDLE_VALUE) &&
332 (GetLastError() == ERROR_INVALID_PARAMETER)) {
333 /* FILE_SHARE_DELETE not supported on win9x */
334 file = CreateFileA( name, GENERIC_READ,
335 FILE_SHARE_READ | FILE_SHARE_WRITE,
336 NULL, OPEN_EXISTING, 0, NULL );
338 if (file == INVALID_HANDLE_VALUE) {
339 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
340 goto done;
343 filesize = GetFileSize( file, NULL );
344 if (filesize > 1.5*1024*1024) {
345 report (R_WARNING,
346 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
347 filesize/1024.0/1024);
348 filesize = 1.5*1024*1024;
351 report (R_STATUS, "Opening %s connection to %s:%d",
352 (uc.nScheme == INTERNET_SCHEME_HTTP ? "http" : "https"),
353 uc.lpszHostName, uc.nPort);
354 session = pInternetOpen (USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
355 if (session == NULL) {
356 report (R_WARNING, "Unable to open connection, error %u", GetLastError());
357 goto done;
359 connection = pInternetConnect (session, uc.lpszHostName, uc.nPort, uc.lpszUserName, uc.lpszPassword, INTERNET_SERVICE_HTTP, 0, 0);
360 if (connection == NULL) {
361 report (R_WARNING, "Unable to connect, error %u", GetLastError());
362 goto done;
364 request = pHttpOpenRequest (connection, "POST", uc.lpszUrlPath, NULL, NULL, NULL,
365 INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI |
366 INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD | (uc.nScheme == INTERNET_SCHEME_HTTPS ? INTERNET_FLAG_SECURE : 0), 0);
367 if (request == NULL) {
368 report (R_WARNING, "Unable to open request, error %u", GetLastError());
369 goto done;
372 report (R_STATUS, "Sending request");
373 str = strmake (&total, body1, name);
374 memset(&buffers_in, 0, sizeof(INTERNET_BUFFERSA));
375 buffers_in.dwStructSize = sizeof(INTERNET_BUFFERSA);
376 buffers_in.dwBufferTotal = filesize + total + sizeof body2 - 1;
377 buffers_in.lpcszHeader = strmake (&count, extra_headers, buffers_in.dwBufferTotal);
378 buffers_in.dwHeadersLength = count;
379 if (! pHttpSendRequestEx(request, &buffers_in, NULL, 0, 0)) {
380 report (R_WARNING, "Unable to send request, error %u", GetLastError());
381 goto done;
384 if (! pInternetWriteFile(request, str, total, &bytes_written) || bytes_written != total) {
385 report (R_WARNING, "Unable to write body data, error %u", GetLastError());
386 goto done;
389 report (R_STATUS, "Sending %u bytes of data", filesize);
390 report (R_PROGRESS, 2, filesize);
391 total = 0;
392 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
393 if (aborting) goto done;
394 if (!bytes_read) break;
395 total += bytes_read;
396 if (total > filesize) bytes_read -= total - filesize;
397 if (! pInternetWriteFile (request, buffer, bytes_read, &bytes_written) || bytes_written != bytes_read) {
398 report (R_WARNING, "Error sending body: %u", GetLastError ());
399 goto done;
401 report (R_DELTA, bytes_read, "Network transfer: In progress");
404 if (! pInternetWriteFile(request, body2, sizeof body2 - 1, &bytes_written) || bytes_written != sizeof body2 - 1) {
405 report (R_WARNING, "Unable to write final body data, error %u", GetLastError());
406 goto done;
408 if (! pHttpEndRequest(request, NULL, 0, 0)) {
409 report (R_WARNING, "Unable to end request, error %u", GetLastError());
410 goto done;
412 report (R_DELTA, 0, "Network transfer: Done");
414 total = 0;
417 if (! pInternetReadFile(request, buffer+total, BUFLEN-total, &bytes_read)) {
418 report (R_WARNING, "Error receiving reply: %u", GetLastError ());
419 goto done;
421 total += bytes_read;
422 if (total == BUFLEN) {
423 report (R_WARNING, "Buffer overflow");
424 goto done;
427 while (bytes_read != 0);
429 heap_free (str);
430 str = strmake (&count, "Received %s (%d bytes).\n",
431 name, filesize);
432 if (total < count || memcmp (str, buffer + total - count, count) != 0) {
433 buffer[total] = 0;
434 report (R_ERROR, "Can't submit logfile '%s'. "
435 "Server response: %s", name, buffer);
438 done:
439 heap_free (uc.lpszScheme);
440 heap_free (uc.lpszHostName);
441 heap_free (uc.lpszUserName);
442 heap_free (uc.lpszPassword);
443 heap_free (uc.lpszUrlPath);
444 heap_free (uc.lpszExtraInfo);
445 heap_free((void *)buffers_in.lpcszHeader);
446 heap_free(str);
447 if (pInternetCloseHandle != NULL && request != NULL)
448 pInternetCloseHandle (request);
449 if (pInternetCloseHandle != NULL && connection != NULL)
450 pInternetCloseHandle (connection);
451 if (pInternetCloseHandle != NULL && session != NULL)
452 pInternetCloseHandle (session);
453 if (file != INVALID_HANDLE_VALUE)
454 CloseHandle (file);
455 if (wininet_mod != NULL)
456 FreeLibrary (wininet_mod);
458 return ret;
462 send_file (const char *url, const char *name)
464 return send_file_wininet( url, name ) || send_file_direct( url, name );