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
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"
43 open_http (const char *server)
46 struct sockaddr_in sa;
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);
58 report (R_ERROR, "Hostname lookup failed for %s", server);
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",
69 if (!connect (s, (struct sockaddr*)&sa, sizeof (struct sockaddr_in)))
72 report (R_ERROR, "Can't connect: %d", WSAGetLastError ());
76 return INVALID_SOCKET;
84 ret = closesocket (s);
85 return (WSACleanup () || ret);
89 send_buf (SOCKET s, const char *buf, size_t length)
94 sent = send (s, buf, length, 0);
95 if (sent == SOCKET_ERROR) return 1;
103 send_str (SOCKET s, ...)
111 p = vstrmake (&len, ap);
114 ret = send_buf (s, p, len);
120 send_file_direct (const char *name)
125 char buffer[BUFLEN+1];
126 DWORD bytes_read, filesize;
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"
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());
157 filesize = GetFileSize( file, NULL );
158 if (filesize > 1.5*1024*1024) {
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);
171 report (R_WARNING, "Error sending header: %d, %d",
172 errno, WSAGetLastError ());
176 report (R_STATUS, "Sending %u bytes of data", filesize);
177 report (R_PROGRESS, 2, filesize);
179 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
180 if (aborting) goto abort2;
181 if (!bytes_read) break;
183 if (total > filesize) bytes_read -= total - filesize;
184 if (send_buf (s, buffer, bytes_read)) {
185 report (R_WARNING, "Error sending body: %d, %d",
186 errno, WSAGetLastError ());
189 report (R_DELTA, bytes_read, "Network transfer: In progress");
193 if (send_buf (s, body2, sizeof body2 - 1)) {
194 report (R_WARNING, "Error sending trailer: %d, %d",
195 errno, WSAGetLastError ());
198 report (R_DELTA, 0, "Network transfer: Done");
201 while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
202 if ((signed)bytes_read == SOCKET_ERROR) {
203 report (R_WARNING, "Error receiving reply: %d, %d",
204 errno, WSAGetLastError ());
208 if (total == BUFLEN) {
209 report (R_WARNING, "Buffer overflow");
213 if (close_http (s)) {
214 report (R_WARNING, "Error closing connection: %d, %d",
215 errno, WSAGetLastError ());
219 str = strmake (&count, "Received %s (%d bytes).\n",
221 ret = total < count || memcmp (str, buffer + total - count, count) != 0;
225 str = strstr (buffer, "\r\n\r\n");
226 if (!str) str = buffer;
228 report (R_ERROR, "Can't submit logfile '%s'. "
229 "Server response: %s", name, str);
241 send_file_wininet (const char *name)
244 HMODULE wininet_mod = NULL;
245 HINTERNET (WINAPI *pInternetOpen)(LPCSTR agent, DWORD access_type, LPCSTR proxy_name, LPCSTR proxy_bypass, DWORD flags);
246 HINTERNET (WINAPI *pInternetConnect)(HINTERNET session, LPCSTR server_name, INTERNET_PORT server_port, LPCSTR username, LPCSTR password, DWORD service, DWORD flags, DWORD_PTR *context);
247 HINTERNET (WINAPI *pHttpOpenRequest)(HINTERNET connection, LPCSTR verb, LPCSTR object_name, LPCSTR version, LPCSTR referer, LPCSTR *accept_types, DWORD flags, DWORD_PTR context);
248 BOOL (WINAPI *pHttpSendRequestEx)(HINTERNET request, LPINTERNET_BUFFERSA buffers_in, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
249 BOOL (WINAPI *pInternetWriteFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_write, LPDWORD number_of_bytes_written);
250 BOOL (WINAPI *pHttpEndRequest)(HINTERNET request, LPINTERNET_BUFFERSA buffers_out, DWORD flags, DWORD_PTR context);
251 BOOL (WINAPI *pInternetReadFile)(HINTERNET file, LPCVOID buffer, DWORD number_of_bytes_to_read, LPDWORD number_of_bytes_read);
252 BOOL (WINAPI *pInternetCloseHandle)(HINTERNET Handle) = NULL;
253 HANDLE file = INVALID_HANDLE_VALUE;
254 DWORD filesize, bytes_read, bytes_written;
257 HINTERNET session = NULL;
258 HINTERNET connection = NULL;
259 HINTERNET request = NULL;
260 INTERNET_BUFFERSA buffers_in = { 0 };
261 char buffer[BUFLEN+1];
263 static const char extra_headers[] =
266 wininet_mod = LoadLibrary ("wininet.dll");
267 if (wininet_mod == NULL)
269 pInternetOpen = (void *)GetProcAddress(wininet_mod, "InternetOpenA");
270 pInternetConnect = (void *)GetProcAddress(wininet_mod, "InternetConnectA");
271 pHttpOpenRequest = (void *)GetProcAddress(wininet_mod, "HttpOpenRequestA");
272 pHttpSendRequestEx = (void *)GetProcAddress(wininet_mod, "HttpSendRequestExA");
273 pInternetWriteFile = (void *)GetProcAddress(wininet_mod, "InternetWriteFile");
274 pHttpEndRequest = (void *)GetProcAddress(wininet_mod, "HttpEndRequestA");
275 pInternetReadFile = (void *)GetProcAddress(wininet_mod, "InternetReadFile");
276 pInternetCloseHandle = (void *)GetProcAddress(wininet_mod, "InternetCloseHandle");
277 if (pInternetOpen == NULL || pInternetConnect == NULL || pHttpOpenRequest == NULL || pHttpSendRequestEx == NULL || pHttpEndRequest == NULL ||
278 pInternetWriteFile == NULL || pInternetReadFile == NULL || pInternetCloseHandle == NULL) {
284 file = CreateFileA( name, GENERIC_READ,
285 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
286 NULL, OPEN_EXISTING, 0, NULL );
288 if ((file == INVALID_HANDLE_VALUE) &&
289 (GetLastError() == ERROR_INVALID_PARAMETER)) {
290 /* FILE_SHARE_DELETE not supported on win9x */
291 file = CreateFileA( name, GENERIC_READ,
292 FILE_SHARE_READ | FILE_SHARE_WRITE,
293 NULL, OPEN_EXISTING, 0, NULL );
295 if (file == INVALID_HANDLE_VALUE) {
296 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
300 filesize = GetFileSize( file, NULL );
301 if (filesize > 1.5*1024*1024) {
303 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
304 filesize/1024.0/1024);
305 filesize = 1.5*1024*1024;
308 report (R_STATUS, "Opening HTTP connection to " SERVER_NAME);
309 session = pInternetOpen (USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
310 if (session == NULL) {
311 report (R_WARNING, "Unable to open connection, error %u", GetLastError());
314 connection = pInternetConnect (session, SERVER_NAME, INTERNET_DEFAULT_HTTP_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
315 if (connection == NULL) {
316 report (R_WARNING, "Unable to connect, error %u", GetLastError());
319 request = pHttpOpenRequest (connection, "POST", URL_PATH, NULL, NULL, NULL,
320 INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI |
321 INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);
322 if (request == NULL) {
323 report (R_WARNING, "Unable to open request, error %u", GetLastError());
327 report (R_STATUS, "Sending request");
328 str = strmake (&total, body1, name);
329 memset(&buffers_in, 0, sizeof(INTERNET_BUFFERSA));
330 buffers_in.dwStructSize = sizeof(INTERNET_BUFFERSA);
331 buffers_in.dwBufferTotal = filesize + total + sizeof body2 - 1;
332 buffers_in.lpcszHeader = strmake (&count, extra_headers, buffers_in.dwBufferTotal);
333 buffers_in.dwHeadersLength = count;
334 if (! pHttpSendRequestEx(request, &buffers_in, NULL, 0, 0)) {
335 report (R_WARNING, "Unable to send request, error %u", GetLastError());
339 if (! pInternetWriteFile(request, str, total, &bytes_written) || bytes_written != total) {
340 report (R_WARNING, "Unable to write body data, error %u", GetLastError());
344 report (R_STATUS, "Sending %u bytes of data", filesize);
345 report (R_PROGRESS, 2, filesize);
347 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
348 if (aborting) goto done;
349 if (!bytes_read) break;
351 if (total > filesize) bytes_read -= total - filesize;
352 if (! pInternetWriteFile (request, buffer, bytes_read, &bytes_written) || bytes_written != bytes_read) {
353 report (R_WARNING, "Error sending body: %u", GetLastError ());
356 report (R_DELTA, bytes_read, "Network transfer: In progress");
359 if (! pInternetWriteFile(request, body2, sizeof body2 - 1, &bytes_written) || bytes_written != sizeof body2 - 1) {
360 report (R_WARNING, "Unable to write final body data, error %u", GetLastError());
363 if (! pHttpEndRequest(request, NULL, 0, 0)) {
364 report (R_WARNING, "Unable to end request, error %u", GetLastError());
367 report (R_DELTA, 0, "Network transfer: Done");
372 if (! pInternetReadFile(request, buffer+total, BUFLEN-total, &bytes_read)) {
373 report (R_WARNING, "Error receiving reply: %u", GetLastError ());
377 if (total == BUFLEN) {
378 report (R_WARNING, "Buffer overflow");
382 while (bytes_read != 0);
385 str = strmake (&count, "Received %s (%d bytes).\n",
387 if (total < count || memcmp (str, buffer + total - count, count) != 0) {
389 report (R_ERROR, "Can't submit logfile '%s'. "
390 "Server response: %s", name, buffer);
394 if (buffers_in.lpcszHeader != NULL)
395 heap_free((void *) buffers_in.lpcszHeader);
398 if (pInternetCloseHandle != NULL && request != NULL)
399 pInternetCloseHandle (request);
400 if (pInternetCloseHandle != NULL && connection != NULL)
401 pInternetCloseHandle (connection);
402 if (pInternetCloseHandle != NULL && session != NULL)
403 pInternetCloseHandle (session);
404 if (file != INVALID_HANDLE_VALUE)
406 if (wininet_mod != NULL)
407 FreeLibrary (wininet_mod);
413 send_file (const char *name)
415 return send_file_wininet( name ) || send_file_direct( name );