include: Avoid redefinition of PEVENT_FILTER_DESCRIPTOR.
[wine.git] / programs / winetest / send.c
bloba419b166222975053494017d4959a3c260a7d7c7
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 *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 s = open_http (SERVER_NAME);
143 if (s == INVALID_SOCKET) return 1;
145 file = CreateFileA( name, GENERIC_READ,
146 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
147 NULL, OPEN_EXISTING, 0, NULL );
149 if ((file == INVALID_HANDLE_VALUE) &&
150 (GetLastError() == ERROR_INVALID_PARAMETER)) {
151 /* FILE_SHARE_DELETE not supported on win9x */
152 file = CreateFileA( name, GENERIC_READ,
153 FILE_SHARE_READ | FILE_SHARE_WRITE,
154 NULL, OPEN_EXISTING, 0, NULL );
156 if (file == INVALID_HANDLE_VALUE)
158 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
159 goto abort1;
161 filesize = GetFileSize( file, NULL );
162 if (filesize > 1.5*1024*1024) {
163 report (R_WARNING,
164 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
165 filesize/1024.0/1024);
166 filesize = (DWORD) 1.5*1024*1024;
169 report (R_STATUS, "Sending header");
170 str = strmake (&total, body1, name);
171 ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
172 send_buf (s, str, total);
173 heap_free (str);
174 if (ret) {
175 report (R_WARNING, "Error sending header: %d, %d",
176 errno, WSAGetLastError ());
177 goto abort2;
180 report (R_STATUS, "Sending %u bytes of data", filesize);
181 report (R_PROGRESS, 2, filesize);
182 total = 0;
183 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
184 if (aborting) goto abort2;
185 if (!bytes_read) break;
186 total += bytes_read;
187 if (total > filesize) bytes_read -= total - filesize;
188 if (send_buf (s, buffer, bytes_read)) {
189 report (R_WARNING, "Error sending body: %d, %d",
190 errno, WSAGetLastError ());
191 goto abort2;
193 report (R_DELTA, bytes_read, "Network transfer: In progress");
195 CloseHandle( file );
197 if (send_buf (s, body2, sizeof body2 - 1)) {
198 report (R_WARNING, "Error sending trailer: %d, %d",
199 errno, WSAGetLastError ());
200 goto abort1;
202 report (R_DELTA, 0, "Network transfer: Done");
204 total = 0;
205 while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
206 if ((signed)bytes_read == SOCKET_ERROR) {
207 if (errno == EINTR) continue;
208 report (R_WARNING, "Error receiving reply: %d, %d",
209 errno, WSAGetLastError ());
210 goto abort1;
212 total += bytes_read;
213 if (total == BUFLEN) {
214 report (R_WARNING, "Buffer overflow");
215 goto abort1;
218 if (close_http (s)) {
219 report (R_WARNING, "Error closing connection: %d, %d",
220 errno, WSAGetLastError ());
221 return 1;
224 str = strmake (&count, "Received %s (%d bytes).\n",
225 name, filesize);
226 ret = total < count || memcmp (str, buffer + total - count, count) != 0;
227 heap_free (str);
228 if (ret) {
229 buffer[total] = 0;
230 str = strstr (buffer, "\r\n\r\n");
231 if (!str) str = buffer;
232 else str = str + 4;
233 report (R_ERROR, "Can't submit logfile '%s'. "
234 "Server response: %s", name, str);
236 return ret;
238 abort2:
239 CloseHandle( file );
240 abort1:
241 close_http (s);
242 return 1;
245 static int
246 send_file_wininet (const char *name)
248 int ret = 0;
249 HMODULE wininet_mod = NULL;
250 HINTERNET (WINAPI *pInternetOpen)(LPCSTR agent, DWORD access_type, LPCSTR proxy_name, LPCSTR proxy_bypass, DWORD flags);
251 HINTERNET (WINAPI *pInternetConnect)(HINTERNET session, LPCSTR server_name, INTERNET_PORT server_port, LPCSTR username, LPCSTR password, DWORD service, DWORD flags, DWORD_PTR *context);
252 HINTERNET (WINAPI *pHttpOpenRequest)(HINTERNET connection, LPCSTR verb, LPCSTR object_name, LPCSTR version, LPCSTR referer, LPCSTR *accept_types, DWORD flags, DWORD_PTR context);
253 BOOL (WINAPI *pHttpSendRequestEx)(HINTERNET request, LPINTERNET_BUFFERSA buffers_in, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
254 BOOL (WINAPI *pInternetWriteFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written);
255 BOOL (WINAPI *pHttpEndRequest)(HINTERNET request, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
256 BOOL (WINAPI *pInternetReadFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read);
257 BOOL (WINAPI *pInternetCloseHandle)(HINTERNET Handle) = NULL;
258 HANDLE file = INVALID_HANDLE_VALUE;
259 DWORD filesize, bytes_read, bytes_written;
260 size_t total, count;
261 char *str = NULL;
262 HINTERNET session = NULL;
263 HINTERNET connection = NULL;
264 HINTERNET request = NULL;
265 INTERNET_BUFFERSA buffers_in = { 0 };
266 char buffer[BUFLEN+1];
268 static const char extra_headers[] =
269 CONTENT_HEADERS;
271 wininet_mod = LoadLibraryA("wininet.dll");
272 if (wininet_mod == NULL)
273 goto done;
274 pInternetOpen = (void *)GetProcAddress(wininet_mod, "InternetOpenA");
275 pInternetConnect = (void *)GetProcAddress(wininet_mod, "InternetConnectA");
276 pHttpOpenRequest = (void *)GetProcAddress(wininet_mod, "HttpOpenRequestA");
277 pHttpSendRequestEx = (void *)GetProcAddress(wininet_mod, "HttpSendRequestExA");
278 pInternetWriteFile = (void *)GetProcAddress(wininet_mod, "InternetWriteFile");
279 pHttpEndRequest = (void *)GetProcAddress(wininet_mod, "HttpEndRequestA");
280 pInternetReadFile = (void *)GetProcAddress(wininet_mod, "InternetReadFile");
281 pInternetCloseHandle = (void *)GetProcAddress(wininet_mod, "InternetCloseHandle");
282 if (pInternetOpen == NULL || pInternetConnect == NULL || pHttpOpenRequest == NULL || pHttpSendRequestEx == NULL || pHttpEndRequest == NULL ||
283 pInternetWriteFile == NULL || pInternetReadFile == NULL || pInternetCloseHandle == NULL) {
284 goto done;
287 ret = 1;
289 file = CreateFileA( name, GENERIC_READ,
290 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
291 NULL, OPEN_EXISTING, 0, NULL );
293 if ((file == INVALID_HANDLE_VALUE) &&
294 (GetLastError() == ERROR_INVALID_PARAMETER)) {
295 /* FILE_SHARE_DELETE not supported on win9x */
296 file = CreateFileA( name, GENERIC_READ,
297 FILE_SHARE_READ | FILE_SHARE_WRITE,
298 NULL, OPEN_EXISTING, 0, NULL );
300 if (file == INVALID_HANDLE_VALUE) {
301 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
302 goto done;
305 filesize = GetFileSize( file, NULL );
306 if (filesize > 1.5*1024*1024) {
307 report (R_WARNING,
308 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
309 filesize/1024.0/1024);
310 filesize = 1.5*1024*1024;
313 report (R_STATUS, "Opening HTTP connection to " SERVER_NAME);
314 session = pInternetOpen (USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
315 if (session == NULL) {
316 report (R_WARNING, "Unable to open connection, error %u", GetLastError());
317 goto done;
319 connection = pInternetConnect (session, SERVER_NAME, INTERNET_DEFAULT_HTTP_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
320 if (connection == NULL) {
321 report (R_WARNING, "Unable to connect, error %u", GetLastError());
322 goto done;
324 request = pHttpOpenRequest (connection, "POST", URL_PATH, NULL, NULL, NULL,
325 INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI |
326 INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);
327 if (request == NULL) {
328 report (R_WARNING, "Unable to open request, error %u", GetLastError());
329 goto done;
332 report (R_STATUS, "Sending request");
333 str = strmake (&total, body1, name);
334 memset(&buffers_in, 0, sizeof(INTERNET_BUFFERSA));
335 buffers_in.dwStructSize = sizeof(INTERNET_BUFFERSA);
336 buffers_in.dwBufferTotal = filesize + total + sizeof body2 - 1;
337 buffers_in.lpcszHeader = strmake (&count, extra_headers, buffers_in.dwBufferTotal);
338 buffers_in.dwHeadersLength = count;
339 if (! pHttpSendRequestEx(request, &buffers_in, NULL, 0, 0)) {
340 report (R_WARNING, "Unable to send request, error %u", GetLastError());
341 goto done;
344 if (! pInternetWriteFile(request, str, total, &bytes_written) || bytes_written != total) {
345 report (R_WARNING, "Unable to write body data, error %u", GetLastError());
346 goto done;
349 report (R_STATUS, "Sending %u bytes of data", filesize);
350 report (R_PROGRESS, 2, filesize);
351 total = 0;
352 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
353 if (aborting) goto done;
354 if (!bytes_read) break;
355 total += bytes_read;
356 if (total > filesize) bytes_read -= total - filesize;
357 if (! pInternetWriteFile (request, buffer, bytes_read, &bytes_written) || bytes_written != bytes_read) {
358 report (R_WARNING, "Error sending body: %u", GetLastError ());
359 goto done;
361 report (R_DELTA, bytes_read, "Network transfer: In progress");
364 if (! pInternetWriteFile(request, body2, sizeof body2 - 1, &bytes_written) || bytes_written != sizeof body2 - 1) {
365 report (R_WARNING, "Unable to write final body data, error %u", GetLastError());
366 goto done;
368 if (! pHttpEndRequest(request, NULL, 0, 0)) {
369 report (R_WARNING, "Unable to end request, error %u", GetLastError());
370 goto done;
372 report (R_DELTA, 0, "Network transfer: Done");
374 total = 0;
377 if (! pInternetReadFile(request, buffer+total, BUFLEN-total, &bytes_read)) {
378 report (R_WARNING, "Error receiving reply: %u", GetLastError ());
379 goto done;
381 total += bytes_read;
382 if (total == BUFLEN) {
383 report (R_WARNING, "Buffer overflow");
384 goto done;
387 while (bytes_read != 0);
389 heap_free (str);
390 str = strmake (&count, "Received %s (%d bytes).\n",
391 name, filesize);
392 if (total < count || memcmp (str, buffer + total - count, count) != 0) {
393 buffer[total] = 0;
394 report (R_ERROR, "Can't submit logfile '%s'. "
395 "Server response: %s", name, buffer);
398 done:
399 heap_free((void *)buffers_in.lpcszHeader);
400 heap_free(str);
401 if (pInternetCloseHandle != NULL && request != NULL)
402 pInternetCloseHandle (request);
403 if (pInternetCloseHandle != NULL && connection != NULL)
404 pInternetCloseHandle (connection);
405 if (pInternetCloseHandle != NULL && session != NULL)
406 pInternetCloseHandle (session);
407 if (file != INVALID_HANDLE_VALUE)
408 CloseHandle (file);
409 if (wininet_mod != NULL)
410 FreeLibrary (wininet_mod);
412 return ret;
416 send_file (const char *name)
418 return send_file_wininet( name ) || send_file_direct( name );