push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / programs / winetest / send.c
blob2b4073a7a3845e23ed5bb1767a7ba101ca0136da
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 <winsock.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) return 1;
96 buf += sent;
97 length -= sent;
99 return 0;
102 static int
103 send_str (SOCKET s, ...)
105 va_list ap;
106 char *p;
107 int ret;
108 size_t len;
110 va_start (ap, s);
111 p = vstrmake (&len, ap);
112 va_end (ap);
113 if (!p) return 1;
114 ret = send_buf (s, p, len);
115 heap_free (p);
116 return ret;
119 static int
120 send_file_direct (const char *name)
122 SOCKET s;
123 HANDLE file;
124 #define BUFLEN 8192
125 char buffer[BUFLEN+1];
126 DWORD bytes_read, filesize;
127 size_t total, count;
128 char *str;
129 int ret;
131 /* RFC 2616 */
132 static const char head[] = "POST " URL_PATH " HTTP/1.0\r\n"
133 "Host: " SERVER_NAME "\r\n"
134 "User-Agent: " USER_AGENT "\r\n"
135 CONTENT_HEADERS
136 "\r\n";
138 s = open_http (SERVER_NAME);
139 if (s == INVALID_SOCKET) return 1;
141 file = CreateFileA( name, GENERIC_READ,
142 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
143 NULL, OPEN_EXISTING, 0, NULL );
145 if ((file == INVALID_HANDLE_VALUE) &&
146 (GetLastError() == ERROR_INVALID_PARAMETER)) {
147 /* FILE_SHARE_DELETE not supported on win9x */
148 file = CreateFileA( name, GENERIC_READ,
149 FILE_SHARE_READ | FILE_SHARE_WRITE,
150 NULL, OPEN_EXISTING, 0, NULL );
152 if (file == INVALID_HANDLE_VALUE)
154 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
155 goto abort1;
157 filesize = GetFileSize( file, NULL );
158 if (filesize > 1.5*1024*1024) {
159 report (R_WARNING,
160 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
161 filesize/1024.0/1024);
162 filesize = (DWORD) 1.5*1024*1024;
165 report (R_STATUS, "Sending header");
166 str = strmake (&total, body1, name);
167 ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
168 send_buf (s, str, total);
169 heap_free (str);
170 if (ret) {
171 report (R_WARNING, "Error sending header: %d, %d",
172 errno, WSAGetLastError ());
173 goto abort2;
176 report (R_STATUS, "Sending %u bytes of data", filesize);
177 report (R_PROGRESS, 2, filesize);
178 total = 0;
179 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
180 if (!bytes_read) break;
181 total += bytes_read;
182 if (total > filesize) bytes_read -= total - filesize;
183 if (send_buf (s, buffer, bytes_read)) {
184 report (R_WARNING, "Error sending body: %d, %d",
185 errno, WSAGetLastError ());
186 goto abort2;
188 report (R_DELTA, bytes_read, "Network transfer: In progress");
190 CloseHandle( file );
192 if (send_buf (s, body2, sizeof body2 - 1)) {
193 report (R_WARNING, "Error sending trailer: %d, %d",
194 errno, WSAGetLastError ());
195 goto abort1;
197 report (R_DELTA, 0, "Network transfer: Done");
199 total = 0;
200 while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
201 if ((signed)bytes_read == SOCKET_ERROR) {
202 report (R_WARNING, "Error receiving reply: %d, %d",
203 errno, WSAGetLastError ());
204 goto abort1;
206 total += bytes_read;
207 if (total == BUFLEN) {
208 report (R_WARNING, "Buffer overflow");
209 goto abort1;
212 if (close_http (s)) {
213 report (R_WARNING, "Error closing connection: %d, %d",
214 errno, WSAGetLastError ());
215 return 1;
218 str = strmake (&count, "Received %s (%d bytes).\n",
219 name, filesize);
220 ret = total < count || memcmp (str, buffer + total - count, count) != 0;
221 heap_free (str);
222 if (ret) {
223 buffer[total] = 0;
224 str = strstr (buffer, "\r\n\r\n");
225 if (!str) str = buffer;
226 else str = str + 4;
227 report (R_ERROR, "Can't submit logfile '%s'. "
228 "Server response: %s", name, str);
230 return ret;
232 abort2:
233 CloseHandle( file );
234 abort1:
235 close_http (s);
236 return 1;
239 static int
240 send_file_wininet (const char *name)
242 int ret = 0;
243 HMODULE wininet_mod = NULL;
244 HINTERNET (WINAPI *pInternetOpen)(LPCSTR agent, DWORD access_type, LPCSTR proxy_name, LPCSTR proxy_bypass, DWORD flags);
245 HINTERNET (WINAPI *pInternetConnect)(HINTERNET session, LPCSTR server_name, INTERNET_PORT server_port, LPCSTR username, LPCSTR password, DWORD service, DWORD flags, DWORD_PTR *context);
246 HINTERNET (WINAPI *pHttpOpenRequest)(HINTERNET connection, LPCSTR verb, LPCSTR object_name, LPCSTR version, LPCSTR referer, LPCSTR *accept_types, DWORD flags, DWORD_PTR context);
247 BOOL (WINAPI *pHttpSendRequestEx)(HINTERNET request, LPINTERNET_BUFFERSA buffers_in, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
248 BOOL (WINAPI *pInternetWriteFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written);
249 BOOL (WINAPI *pHttpEndRequest)(HINTERNET request, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
250 BOOL (WINAPI *pInternetReadFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read);
251 BOOL (WINAPI *pInternetCloseHandle)(HINTERNET Handle) = NULL;
252 HANDLE file = INVALID_HANDLE_VALUE;
253 DWORD filesize, bytes_read, bytes_written;
254 size_t total, count;
255 char *str = NULL;
256 HINTERNET session = NULL;
257 HINTERNET connection = NULL;
258 HINTERNET request = NULL;
259 INTERNET_BUFFERSA buffers_in = { 0 };
260 char buffer[BUFLEN+1];
262 static const char extra_headers[] =
263 CONTENT_HEADERS;
265 wininet_mod = LoadLibrary ("wininet.dll");
266 if (wininet_mod == NULL)
267 goto done;
268 pInternetOpen = (void *)GetProcAddress(wininet_mod, "InternetOpenA");
269 pInternetConnect = (void *)GetProcAddress(wininet_mod, "InternetConnectA");
270 pHttpOpenRequest = (void *)GetProcAddress(wininet_mod, "HttpOpenRequestA");
271 pHttpSendRequestEx = (void *)GetProcAddress(wininet_mod, "HttpSendRequestExA");
272 pInternetWriteFile = (void *)GetProcAddress(wininet_mod, "InternetWriteFile");
273 pHttpEndRequest = (void *)GetProcAddress(wininet_mod, "HttpEndRequestA");
274 pInternetReadFile = (void *)GetProcAddress(wininet_mod, "InternetReadFile");
275 pInternetCloseHandle = (void *)GetProcAddress(wininet_mod, "InternetCloseHandle");
276 if (pInternetOpen == NULL || pInternetConnect == NULL || pHttpOpenRequest == NULL || pHttpSendRequestEx == NULL || pHttpEndRequest == NULL ||
277 pInternetWriteFile == NULL || pInternetReadFile == NULL || pInternetCloseHandle == NULL) {
278 goto done;
281 ret = 1;
283 file = CreateFileA( name, GENERIC_READ,
284 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
285 NULL, OPEN_EXISTING, 0, NULL );
287 if ((file == INVALID_HANDLE_VALUE) &&
288 (GetLastError() == ERROR_INVALID_PARAMETER)) {
289 /* FILE_SHARE_DELETE not supported on win9x */
290 file = CreateFileA( name, GENERIC_READ,
291 FILE_SHARE_READ | FILE_SHARE_WRITE,
292 NULL, OPEN_EXISTING, 0, NULL );
294 if (file == INVALID_HANDLE_VALUE) {
295 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
296 goto done;
299 filesize = GetFileSize( file, NULL );
300 if (filesize > 1.5*1024*1024) {
301 report (R_WARNING,
302 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
303 filesize/1024.0/1024);
304 filesize = (DWORD) 1.5*1024*1024;
307 report (R_STATUS, "Opening HTTP connection to " SERVER_NAME);
308 session = pInternetOpen (USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
309 if (session == NULL) {
310 report (R_WARNING, "Unable to open connection, error %u", GetLastError());
311 goto done;
313 connection = pInternetConnect (session, SERVER_NAME, INTERNET_DEFAULT_HTTP_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
314 if (connection == NULL) {
315 report (R_WARNING, "Unable to connect, error %u", GetLastError());
316 goto done;
318 request = pHttpOpenRequest (connection, "POST", URL_PATH, NULL, NULL, NULL,
319 INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI |
320 INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);
321 if (request == NULL) {
322 report (R_WARNING, "Unable to open request, error %u", GetLastError());
323 goto done;
326 report (R_STATUS, "Sending request");
327 str = strmake (&total, body1, name);
328 memset(&buffers_in, 0, sizeof(INTERNET_BUFFERSA));
329 buffers_in.dwStructSize = sizeof(INTERNET_BUFFERSA);
330 buffers_in.dwBufferTotal = filesize + total + sizeof body2 - 1;
331 buffers_in.lpcszHeader = strmake (&count, extra_headers, buffers_in.dwBufferTotal);
332 buffers_in.dwHeadersLength = count;
333 if (! pHttpSendRequestEx(request, &buffers_in, NULL, 0, 0)) {
334 report (R_WARNING, "Unable to send request, error %u", GetLastError());
335 goto done;
338 if (! pInternetWriteFile(request, str, total, &bytes_written) || bytes_written != total) {
339 report (R_WARNING, "Unable to write body data, error %u", GetLastError());
340 goto done;
343 report (R_STATUS, "Sending %u bytes of data", filesize);
344 report (R_PROGRESS, 2, filesize);
345 total = 0;
346 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
347 if (!bytes_read) break;
348 total += bytes_read;
349 if (total > filesize) bytes_read -= total - filesize;
350 if (! pInternetWriteFile (request, buffer, bytes_read, &bytes_written) || bytes_written != bytes_read) {
351 report (R_WARNING, "Error sending body: %u", GetLastError ());
352 goto done;
354 report (R_DELTA, bytes_read, "Network transfer: In progress");
357 if (! pInternetWriteFile(request, body2, sizeof body2 - 1, &bytes_written) || bytes_written != sizeof body2 - 1) {
358 report (R_WARNING, "Unable to write final body data, error %u", GetLastError());
359 goto done;
361 if (! pHttpEndRequest(request, NULL, 0, 0)) {
362 report (R_WARNING, "Unable to end request, error %u", GetLastError());
363 goto done;
365 report (R_DELTA, 0, "Network transfer: Done");
367 total = 0;
370 if (! pInternetReadFile(request, buffer+total, BUFLEN-total, &bytes_read)) {
371 report (R_WARNING, "Error receiving reply: %u", GetLastError ());
372 goto done;
374 total += bytes_read;
375 if (total == BUFLEN) {
376 report (R_WARNING, "Buffer overflow");
377 goto done;
380 while (bytes_read != 0);
382 heap_free (str);
383 str = strmake (&count, "Received %s (%d bytes).\n",
384 name, filesize);
385 if (total < count || memcmp (str, buffer + total - count, count) != 0) {
386 buffer[total] = 0;
387 report (R_ERROR, "Can't submit logfile '%s'. "
388 "Server response: %s", name, buffer);
391 done:
392 if (buffers_in.lpcszHeader != NULL)
393 heap_free((void *) buffers_in.lpcszHeader);
394 if (str != NULL)
395 heap_free (str);
396 if (pInternetCloseHandle != NULL && request != NULL)
397 pInternetCloseHandle (request);
398 if (pInternetCloseHandle != NULL && connection != NULL)
399 pInternetCloseHandle (connection);
400 if (pInternetCloseHandle != NULL && session != NULL)
401 pInternetCloseHandle (session);
402 if (file != INVALID_HANDLE_VALUE)
403 CloseHandle (file);
404 if (wininet_mod != NULL)
405 FreeLibrary (wininet_mod);
407 return ret;
411 send_file (const char *name)
413 return send_file_wininet( name ) || send_file_direct( name );