po: Fix a mistake in Dutch translation.
[wine.git] / programs / winetest / send.c
blobd04b6e9890e3583992f1d9c12c8aa18593d992fa
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 if (sa.sin_addr.s_addr == INADDR_NONE) {
56 struct hostent *host = gethostbyname (server);
57 if (!host) {
58 report (R_ERROR, "Hostname lookup failed for %s", server);
59 goto failure;
61 sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
63 s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
64 if (s == INVALID_SOCKET) {
65 report (R_ERROR, "Can't open network socket: %d",
66 WSAGetLastError ());
67 goto failure;
69 if (!connect (s, (struct sockaddr*)&sa, sizeof (struct sockaddr_in)))
70 return s;
72 report (R_ERROR, "Can't connect: %d", WSAGetLastError ());
73 closesocket (s);
74 failure:
75 WSACleanup ();
76 return INVALID_SOCKET;
79 static int
80 close_http (SOCKET s)
82 int ret;
84 ret = closesocket (s);
85 return (WSACleanup () || ret);
88 static int
89 send_buf (SOCKET s, const char *buf, size_t length)
91 int sent;
93 while (length > 0) {
94 sent = send (s, buf, length, 0);
95 if (sent == SOCKET_ERROR) {
96 if (errno == EINTR) continue;
97 return 1;
99 buf += sent;
100 length -= sent;
102 return 0;
105 static int
106 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 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 *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 s = open_http (SERVER_NAME);
142 if (s == INVALID_SOCKET) return 1;
144 file = CreateFileA( name, GENERIC_READ,
145 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
146 NULL, OPEN_EXISTING, 0, NULL );
148 if ((file == INVALID_HANDLE_VALUE) &&
149 (GetLastError() == ERROR_INVALID_PARAMETER)) {
150 /* FILE_SHARE_DELETE not supported on win9x */
151 file = CreateFileA( name, GENERIC_READ,
152 FILE_SHARE_READ | FILE_SHARE_WRITE,
153 NULL, OPEN_EXISTING, 0, NULL );
155 if (file == INVALID_HANDLE_VALUE)
157 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
158 goto abort1;
160 filesize = GetFileSize( file, NULL );
161 if (filesize > 1.5*1024*1024) {
162 report (R_WARNING,
163 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
164 filesize/1024.0/1024);
165 filesize = (DWORD) 1.5*1024*1024;
168 report (R_STATUS, "Sending header");
169 str = strmake (&total, body1, name);
170 ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
171 send_buf (s, str, total);
172 heap_free (str);
173 if (ret) {
174 report (R_WARNING, "Error sending header: %d, %d",
175 errno, WSAGetLastError ());
176 goto abort2;
179 report (R_STATUS, "Sending %u bytes of data", filesize);
180 report (R_PROGRESS, 2, filesize);
181 total = 0;
182 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
183 if (aborting) goto abort2;
184 if (!bytes_read) break;
185 total += bytes_read;
186 if (total > filesize) bytes_read -= total - filesize;
187 if (send_buf (s, buffer, bytes_read)) {
188 report (R_WARNING, "Error sending body: %d, %d",
189 errno, WSAGetLastError ());
190 goto abort2;
192 report (R_DELTA, bytes_read, "Network transfer: In progress");
194 CloseHandle( file );
196 if (send_buf (s, body2, sizeof body2 - 1)) {
197 report (R_WARNING, "Error sending trailer: %d, %d",
198 errno, WSAGetLastError ());
199 goto abort1;
201 report (R_DELTA, 0, "Network transfer: Done");
203 total = 0;
204 while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
205 if ((signed)bytes_read == SOCKET_ERROR) {
206 if (errno == EINTR) continue;
207 report (R_WARNING, "Error receiving reply: %d, %d",
208 errno, WSAGetLastError ());
209 goto abort1;
211 total += bytes_read;
212 if (total == BUFLEN) {
213 report (R_WARNING, "Buffer overflow");
214 goto abort1;
217 if (close_http (s)) {
218 report (R_WARNING, "Error closing connection: %d, %d",
219 errno, WSAGetLastError ());
220 return 1;
223 str = strmake (&count, "Received %s (%d bytes).\n",
224 name, filesize);
225 ret = total < count || memcmp (str, buffer + total - count, count) != 0;
226 heap_free (str);
227 if (ret) {
228 buffer[total] = 0;
229 str = strstr (buffer, "\r\n\r\n");
230 if (!str) str = buffer;
231 else str = str + 4;
232 report (R_ERROR, "Can't submit logfile '%s'. "
233 "Server response: %s", name, str);
235 return ret;
237 abort2:
238 CloseHandle( file );
239 abort1:
240 close_http (s);
241 return 1;
244 static int
245 send_file_wininet (const char *name)
247 int ret = 0;
248 HMODULE wininet_mod = NULL;
249 HINTERNET (WINAPI *pInternetOpen)(LPCSTR agent, DWORD access_type, LPCSTR proxy_name, LPCSTR proxy_bypass, DWORD flags);
250 HINTERNET (WINAPI *pInternetConnect)(HINTERNET session, LPCSTR server_name, INTERNET_PORT server_port, LPCSTR username, LPCSTR password, DWORD service, DWORD flags, DWORD_PTR *context);
251 HINTERNET (WINAPI *pHttpOpenRequest)(HINTERNET connection, LPCSTR verb, LPCSTR object_name, LPCSTR version, LPCSTR referer, LPCSTR *accept_types, DWORD flags, DWORD_PTR context);
252 BOOL (WINAPI *pHttpSendRequestEx)(HINTERNET request, LPINTERNET_BUFFERSA buffers_in, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
253 BOOL (WINAPI *pInternetWriteFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written);
254 BOOL (WINAPI *pHttpEndRequest)(HINTERNET request, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
255 BOOL (WINAPI *pInternetReadFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read);
256 BOOL (WINAPI *pInternetCloseHandle)(HINTERNET Handle) = NULL;
257 HANDLE file = INVALID_HANDLE_VALUE;
258 DWORD filesize, bytes_read, bytes_written;
259 size_t total, count;
260 char *str = NULL;
261 HINTERNET session = NULL;
262 HINTERNET connection = NULL;
263 HINTERNET request = NULL;
264 INTERNET_BUFFERSA buffers_in = { 0 };
265 char buffer[BUFLEN+1];
267 static const char extra_headers[] =
268 CONTENT_HEADERS;
270 wininet_mod = LoadLibraryA("wininet.dll");
271 if (wininet_mod == NULL)
272 goto done;
273 pInternetOpen = (void *)GetProcAddress(wininet_mod, "InternetOpenA");
274 pInternetConnect = (void *)GetProcAddress(wininet_mod, "InternetConnectA");
275 pHttpOpenRequest = (void *)GetProcAddress(wininet_mod, "HttpOpenRequestA");
276 pHttpSendRequestEx = (void *)GetProcAddress(wininet_mod, "HttpSendRequestExA");
277 pInternetWriteFile = (void *)GetProcAddress(wininet_mod, "InternetWriteFile");
278 pHttpEndRequest = (void *)GetProcAddress(wininet_mod, "HttpEndRequestA");
279 pInternetReadFile = (void *)GetProcAddress(wininet_mod, "InternetReadFile");
280 pInternetCloseHandle = (void *)GetProcAddress(wininet_mod, "InternetCloseHandle");
281 if (pInternetOpen == NULL || pInternetConnect == NULL || pHttpOpenRequest == NULL || pHttpSendRequestEx == NULL || pHttpEndRequest == NULL ||
282 pInternetWriteFile == NULL || pInternetReadFile == NULL || pInternetCloseHandle == NULL) {
283 goto done;
286 ret = 1;
288 file = CreateFileA( name, GENERIC_READ,
289 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
290 NULL, OPEN_EXISTING, 0, NULL );
292 if ((file == INVALID_HANDLE_VALUE) &&
293 (GetLastError() == ERROR_INVALID_PARAMETER)) {
294 /* FILE_SHARE_DELETE not supported on win9x */
295 file = CreateFileA( name, GENERIC_READ,
296 FILE_SHARE_READ | FILE_SHARE_WRITE,
297 NULL, OPEN_EXISTING, 0, NULL );
299 if (file == INVALID_HANDLE_VALUE) {
300 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
301 goto done;
304 filesize = GetFileSize( file, NULL );
305 if (filesize > 1.5*1024*1024) {
306 report (R_WARNING,
307 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
308 filesize/1024.0/1024);
309 filesize = 1.5*1024*1024;
312 report (R_STATUS, "Opening HTTP connection to " SERVER_NAME);
313 session = pInternetOpen (USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
314 if (session == NULL) {
315 report (R_WARNING, "Unable to open connection, error %u", GetLastError());
316 goto done;
318 connection = pInternetConnect (session, SERVER_NAME, INTERNET_DEFAULT_HTTP_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
319 if (connection == NULL) {
320 report (R_WARNING, "Unable to connect, error %u", GetLastError());
321 goto done;
323 request = pHttpOpenRequest (connection, "POST", URL_PATH, NULL, NULL, NULL,
324 INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI |
325 INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);
326 if (request == NULL) {
327 report (R_WARNING, "Unable to open request, error %u", GetLastError());
328 goto done;
331 report (R_STATUS, "Sending request");
332 str = strmake (&total, body1, name);
333 memset(&buffers_in, 0, sizeof(INTERNET_BUFFERSA));
334 buffers_in.dwStructSize = sizeof(INTERNET_BUFFERSA);
335 buffers_in.dwBufferTotal = filesize + total + sizeof body2 - 1;
336 buffers_in.lpcszHeader = strmake (&count, extra_headers, buffers_in.dwBufferTotal);
337 buffers_in.dwHeadersLength = count;
338 if (! pHttpSendRequestEx(request, &buffers_in, NULL, 0, 0)) {
339 report (R_WARNING, "Unable to send request, error %u", GetLastError());
340 goto done;
343 if (! pInternetWriteFile(request, str, total, &bytes_written) || bytes_written != total) {
344 report (R_WARNING, "Unable to write body data, error %u", GetLastError());
345 goto done;
348 report (R_STATUS, "Sending %u bytes of data", filesize);
349 report (R_PROGRESS, 2, filesize);
350 total = 0;
351 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
352 if (aborting) goto done;
353 if (!bytes_read) break;
354 total += bytes_read;
355 if (total > filesize) bytes_read -= total - filesize;
356 if (! pInternetWriteFile (request, buffer, bytes_read, &bytes_written) || bytes_written != bytes_read) {
357 report (R_WARNING, "Error sending body: %u", GetLastError ());
358 goto done;
360 report (R_DELTA, bytes_read, "Network transfer: In progress");
363 if (! pInternetWriteFile(request, body2, sizeof body2 - 1, &bytes_written) || bytes_written != sizeof body2 - 1) {
364 report (R_WARNING, "Unable to write final body data, error %u", GetLastError());
365 goto done;
367 if (! pHttpEndRequest(request, NULL, 0, 0)) {
368 report (R_WARNING, "Unable to end request, error %u", GetLastError());
369 goto done;
371 report (R_DELTA, 0, "Network transfer: Done");
373 total = 0;
376 if (! pInternetReadFile(request, buffer+total, BUFLEN-total, &bytes_read)) {
377 report (R_WARNING, "Error receiving reply: %u", GetLastError ());
378 goto done;
380 total += bytes_read;
381 if (total == BUFLEN) {
382 report (R_WARNING, "Buffer overflow");
383 goto done;
386 while (bytes_read != 0);
388 heap_free (str);
389 str = strmake (&count, "Received %s (%d bytes).\n",
390 name, filesize);
391 if (total < count || memcmp (str, buffer + total - count, count) != 0) {
392 buffer[total] = 0;
393 report (R_ERROR, "Can't submit logfile '%s'. "
394 "Server response: %s", name, buffer);
397 done:
398 if (buffers_in.lpcszHeader != NULL)
399 heap_free((void *) buffers_in.lpcszHeader);
400 if (str != NULL)
401 heap_free (str);
402 if (pInternetCloseHandle != NULL && request != NULL)
403 pInternetCloseHandle (request);
404 if (pInternetCloseHandle != NULL && connection != NULL)
405 pInternetCloseHandle (connection);
406 if (pInternetCloseHandle != NULL && session != NULL)
407 pInternetCloseHandle (session);
408 if (file != INVALID_HANDLE_VALUE)
409 CloseHandle (file);
410 if (wininet_mod != NULL)
411 FreeLibrary (wininet_mod);
413 return ret;
417 send_file (const char *name)
419 return send_file_wininet( name ) || send_file_direct( name );