d3d10/effect: Implement GetString().
[wine.git] / programs / winetest / send.c
blob1d4e3a78674bb1cece2283f46fa9bd12ef166240
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 __ms_va_list ap;
109 char *p;
110 int ret;
111 size_t len;
113 __ms_va_start (ap, s);
114 p = vstrmake (&len, ap);
115 __ms_va_end (ap);
116 if (!p) return 1;
117 ret = send_buf (s, p, len);
118 heap_free (p);
119 return ret;
122 static int
123 send_file_direct (const char * url, const char *name)
125 SOCKET s;
126 HANDLE file;
127 #define BUFLEN 8192
128 char buffer[BUFLEN+1];
129 DWORD bytes_read, filesize;
130 size_t total, count;
131 char *str;
132 int ret;
134 /* RFC 2616 */
135 static const char head[] = "POST " URL_PATH " HTTP/1.0\r\n"
136 "Host: " SERVER_NAME "\r\n"
137 "User-Agent: " USER_AGENT "\r\n"
138 CONTENT_HEADERS
139 "\r\n";
141 if (url) {
142 report (R_WARNING, "Can't submit to an alternative URL");
143 return 0;
146 s = open_http (SERVER_NAME);
147 if (s == INVALID_SOCKET) return 1;
149 file = CreateFileA( name, GENERIC_READ,
150 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
151 NULL, OPEN_EXISTING, 0, NULL );
153 if ((file == INVALID_HANDLE_VALUE) &&
154 (GetLastError() == ERROR_INVALID_PARAMETER)) {
155 /* FILE_SHARE_DELETE not supported on win9x */
156 file = CreateFileA( name, GENERIC_READ,
157 FILE_SHARE_READ | FILE_SHARE_WRITE,
158 NULL, OPEN_EXISTING, 0, NULL );
160 if (file == INVALID_HANDLE_VALUE)
162 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
163 goto abort1;
165 filesize = GetFileSize( file, NULL );
166 if (filesize > 1.5*1024*1024) {
167 report (R_WARNING,
168 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
169 filesize/1024.0/1024);
170 filesize = (DWORD) 1.5*1024*1024;
173 report (R_STATUS, "Sending header");
174 str = strmake (&total, body1, name);
175 ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
176 send_buf (s, str, total);
177 heap_free (str);
178 if (ret) {
179 report (R_WARNING, "Error sending header: %d, %d",
180 errno, WSAGetLastError ());
181 goto abort2;
184 report (R_STATUS, "Sending %u bytes of data", filesize);
185 report (R_PROGRESS, 2, filesize);
186 total = 0;
187 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
188 if (aborting) goto abort2;
189 if (!bytes_read) break;
190 total += bytes_read;
191 if (total > filesize) bytes_read -= total - filesize;
192 if (send_buf (s, buffer, bytes_read)) {
193 report (R_WARNING, "Error sending body: %d, %d",
194 errno, WSAGetLastError ());
195 goto abort2;
197 report (R_DELTA, bytes_read, "Network transfer: In progress");
199 CloseHandle( file );
201 if (send_buf (s, body2, sizeof body2 - 1)) {
202 report (R_WARNING, "Error sending trailer: %d, %d",
203 errno, WSAGetLastError ());
204 goto abort1;
206 report (R_DELTA, 0, "Network transfer: Done");
208 total = 0;
209 while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
210 if ((signed)bytes_read == SOCKET_ERROR) {
211 if (errno == EINTR) continue;
212 report (R_WARNING, "Error receiving reply: %d, %d",
213 errno, WSAGetLastError ());
214 goto abort1;
216 total += bytes_read;
217 if (total == BUFLEN) {
218 report (R_WARNING, "Buffer overflow");
219 goto abort1;
222 if (close_http (s)) {
223 report (R_WARNING, "Error closing connection: %d, %d",
224 errno, WSAGetLastError ());
225 return 1;
228 str = strmake (&count, "Received %s (%d bytes).\n",
229 name, filesize);
230 ret = total < count || memcmp (str, buffer + total - count, count) != 0;
231 heap_free (str);
232 if (ret) {
233 buffer[total] = 0;
234 str = strstr (buffer, "\r\n\r\n");
235 if (!str) str = buffer;
236 else str = str + 4;
237 report (R_ERROR, "Can't submit logfile '%s'. "
238 "Server response: %s", name, str);
240 return ret;
242 abort2:
243 CloseHandle( file );
244 abort1:
245 close_http (s);
246 return 1;
249 static int
250 send_file_wininet (const char *url, const char *name)
252 int ret = 0;
253 HMODULE wininet_mod = NULL;
254 HINTERNET (WINAPI *pInternetOpen)(LPCSTR agent, DWORD access_type, LPCSTR proxy_name, LPCSTR proxy_bypass, DWORD flags);
255 HINTERNET (WINAPI *pInternetConnect)(HINTERNET session, LPCSTR server_name, INTERNET_PORT server_port, LPCSTR username, LPCSTR password, DWORD service, DWORD flags, DWORD_PTR *context);
256 HINTERNET (WINAPI *pHttpOpenRequest)(HINTERNET connection, LPCSTR verb, LPCSTR object_name, LPCSTR version, LPCSTR referer, LPCSTR *accept_types, DWORD flags, DWORD_PTR context);
257 BOOL (WINAPI *pHttpSendRequestEx)(HINTERNET request, LPINTERNET_BUFFERSA buffers_in, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
258 BOOL (WINAPI *pInternetWriteFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written);
259 BOOL (WINAPI *pHttpEndRequest)(HINTERNET request, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
260 BOOL (WINAPI *pInternetReadFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read);
261 BOOL (WINAPI *pInternetCloseHandle)(HINTERNET Handle) = NULL;
262 HANDLE file = INVALID_HANDLE_VALUE;
263 DWORD filesize, bytes_read, bytes_written;
264 size_t total, count;
265 char *str = NULL;
266 HINTERNET session = NULL;
267 HINTERNET connection = NULL;
268 HINTERNET request = NULL;
269 INTERNET_BUFFERSA buffers_in = { 0 };
270 char buffer[BUFLEN+1];
271 URL_COMPONENTSA uc = { 0 };
273 static const char extra_headers[] =
274 CONTENT_HEADERS;
276 wininet_mod = LoadLibraryA("wininet.dll");
277 if (wininet_mod == NULL)
278 goto done;
279 pInternetOpen = (void *)GetProcAddress(wininet_mod, "InternetOpenA");
280 pInternetConnect = (void *)GetProcAddress(wininet_mod, "InternetConnectA");
281 pHttpOpenRequest = (void *)GetProcAddress(wininet_mod, "HttpOpenRequestA");
282 pHttpSendRequestEx = (void *)GetProcAddress(wininet_mod, "HttpSendRequestExA");
283 pInternetWriteFile = (void *)GetProcAddress(wininet_mod, "InternetWriteFile");
284 pHttpEndRequest = (void *)GetProcAddress(wininet_mod, "HttpEndRequestA");
285 pInternetReadFile = (void *)GetProcAddress(wininet_mod, "InternetReadFile");
286 pInternetCloseHandle = (void *)GetProcAddress(wininet_mod, "InternetCloseHandle");
287 if (pInternetOpen == NULL || pInternetConnect == NULL || pHttpOpenRequest == NULL || pHttpSendRequestEx == NULL || pHttpEndRequest == NULL ||
288 pInternetWriteFile == NULL || pInternetReadFile == NULL || pInternetCloseHandle == NULL) {
289 goto done;
291 if (url) {
292 BOOL (WINAPI *pInternetCrackUrlA)(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp);
293 pInternetCrackUrlA = (void *)GetProcAddress(wininet_mod, "InternetCrackUrlA");
294 if (pInternetCrackUrlA == NULL)
295 goto done;
296 uc.dwStructSize = sizeof(uc);
297 uc.dwSchemeLength = uc.dwHostNameLength = uc.dwUserNameLength =
298 uc.dwPasswordLength = uc.dwUrlPathLength = uc.dwExtraInfoLength =
299 strlen(url) + 1;
300 uc.lpszScheme = heap_alloc (uc.dwSchemeLength);
301 uc.lpszHostName = heap_alloc (uc.dwHostNameLength);
302 uc.lpszUserName = heap_alloc (uc.dwUserNameLength);
303 uc.lpszPassword = heap_alloc (uc.dwPasswordLength);
304 uc.lpszUrlPath = heap_alloc (uc.dwUrlPathLength);
305 uc.lpszExtraInfo = heap_alloc (uc.dwExtraInfoLength);
306 if (!pInternetCrackUrlA(url, 0, 0, &uc)) {
307 report (R_WARNING, "Can't parse submit URL '%s'", url);
308 goto done;
310 if ((uc.nScheme != INTERNET_SCHEME_HTTP && uc.nScheme != INTERNET_SCHEME_HTTPS) || *uc.lpszExtraInfo) {
311 report (R_WARNING, "Can't submit report to scheme %s or extra info '%s'", uc.lpszScheme, uc.lpszExtraInfo);
312 goto done;
315 } else {
316 uc.nScheme = INTERNET_SCHEME_HTTP;
317 uc.lpszHostName = heap_strdup (SERVER_NAME);
318 uc.nPort = INTERNET_DEFAULT_HTTP_PORT;
319 uc.lpszUserName = heap_strdup ("");
320 uc.lpszPassword = heap_strdup ("");
321 uc.lpszUrlPath = heap_strdup (URL_PATH);
324 ret = 1;
326 file = CreateFileA( name, GENERIC_READ,
327 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
328 NULL, OPEN_EXISTING, 0, NULL );
330 if ((file == INVALID_HANDLE_VALUE) &&
331 (GetLastError() == ERROR_INVALID_PARAMETER)) {
332 /* FILE_SHARE_DELETE not supported on win9x */
333 file = CreateFileA( name, GENERIC_READ,
334 FILE_SHARE_READ | FILE_SHARE_WRITE,
335 NULL, OPEN_EXISTING, 0, NULL );
337 if (file == INVALID_HANDLE_VALUE) {
338 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
339 goto done;
342 filesize = GetFileSize( file, NULL );
343 if (filesize > 1.5*1024*1024) {
344 report (R_WARNING,
345 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
346 filesize/1024.0/1024);
347 filesize = 1.5*1024*1024;
350 report (R_STATUS, "Opening %s connection to %s:%d",
351 (uc.nScheme == INTERNET_SCHEME_HTTP ? "http" : "https"),
352 uc.lpszHostName, uc.nPort);
353 session = pInternetOpen (USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
354 if (session == NULL) {
355 report (R_WARNING, "Unable to open connection, error %u", GetLastError());
356 goto done;
358 connection = pInternetConnect (session, uc.lpszHostName, uc.nPort, uc.lpszUserName, uc.lpszPassword, INTERNET_SERVICE_HTTP, 0, 0);
359 if (connection == NULL) {
360 report (R_WARNING, "Unable to connect, error %u", GetLastError());
361 goto done;
363 request = pHttpOpenRequest (connection, "POST", uc.lpszUrlPath, NULL, NULL, NULL,
364 INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI |
365 INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD | (uc.nScheme == INTERNET_SCHEME_HTTPS ? INTERNET_FLAG_SECURE : 0), 0);
366 if (request == NULL) {
367 report (R_WARNING, "Unable to open request, error %u", GetLastError());
368 goto done;
371 report (R_STATUS, "Sending request");
372 str = strmake (&total, body1, name);
373 memset(&buffers_in, 0, sizeof(INTERNET_BUFFERSA));
374 buffers_in.dwStructSize = sizeof(INTERNET_BUFFERSA);
375 buffers_in.dwBufferTotal = filesize + total + sizeof body2 - 1;
376 buffers_in.lpcszHeader = strmake (&count, extra_headers, buffers_in.dwBufferTotal);
377 buffers_in.dwHeadersLength = count;
378 if (! pHttpSendRequestEx(request, &buffers_in, NULL, 0, 0)) {
379 report (R_WARNING, "Unable to send request, error %u", GetLastError());
380 goto done;
383 if (! pInternetWriteFile(request, str, total, &bytes_written) || bytes_written != total) {
384 report (R_WARNING, "Unable to write body data, error %u", GetLastError());
385 goto done;
388 report (R_STATUS, "Sending %u bytes of data", filesize);
389 report (R_PROGRESS, 2, filesize);
390 total = 0;
391 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
392 if (aborting) goto done;
393 if (!bytes_read) break;
394 total += bytes_read;
395 if (total > filesize) bytes_read -= total - filesize;
396 if (! pInternetWriteFile (request, buffer, bytes_read, &bytes_written) || bytes_written != bytes_read) {
397 report (R_WARNING, "Error sending body: %u", GetLastError ());
398 goto done;
400 report (R_DELTA, bytes_read, "Network transfer: In progress");
403 if (! pInternetWriteFile(request, body2, sizeof body2 - 1, &bytes_written) || bytes_written != sizeof body2 - 1) {
404 report (R_WARNING, "Unable to write final body data, error %u", GetLastError());
405 goto done;
407 if (! pHttpEndRequest(request, NULL, 0, 0)) {
408 report (R_WARNING, "Unable to end request, error %u", GetLastError());
409 goto done;
411 report (R_DELTA, 0, "Network transfer: Done");
413 total = 0;
416 if (! pInternetReadFile(request, buffer+total, BUFLEN-total, &bytes_read)) {
417 report (R_WARNING, "Error receiving reply: %u", GetLastError ());
418 goto done;
420 total += bytes_read;
421 if (total == BUFLEN) {
422 report (R_WARNING, "Buffer overflow");
423 goto done;
426 while (bytes_read != 0);
428 heap_free (str);
429 str = strmake (&count, "Received %s (%d bytes).\n",
430 name, filesize);
431 if (total < count || memcmp (str, buffer + total - count, count) != 0) {
432 buffer[total] = 0;
433 report (R_ERROR, "Can't submit logfile '%s'. "
434 "Server response: %s", name, buffer);
437 done:
438 heap_free (uc.lpszScheme);
439 heap_free (uc.lpszHostName);
440 heap_free (uc.lpszUserName);
441 heap_free (uc.lpszPassword);
442 heap_free (uc.lpszUrlPath);
443 heap_free (uc.lpszExtraInfo);
444 heap_free((void *)buffers_in.lpcszHeader);
445 heap_free(str);
446 if (pInternetCloseHandle != NULL && request != NULL)
447 pInternetCloseHandle (request);
448 if (pInternetCloseHandle != NULL && connection != NULL)
449 pInternetCloseHandle (connection);
450 if (pInternetCloseHandle != NULL && session != NULL)
451 pInternetCloseHandle (session);
452 if (file != INVALID_HANDLE_VALUE)
453 CloseHandle (file);
454 if (wininet_mod != NULL)
455 FreeLibrary (wininet_mod);
457 return ret;
461 send_file (const char *url, const char *name)
463 return send_file_wininet( url, name ) || send_file_direct( url, name );