Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * HTTP handling functions. |
| 3 | * |
| 4 | * Copyright 2003 Ferenc Wagner |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 19 | */ |
Ferenc Wagner | 24624f6 | 2004-06-15 22:45:15 +0000 | [diff] [blame] | 20 | |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 21 | #include <winsock.h> |
| 22 | #include <stdio.h> |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 23 | #include <errno.h> |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 24 | |
| 25 | #include "winetest.h" |
| 26 | |
Mike McCormack | d73dad6 | 2005-06-04 10:01:25 +0000 | [diff] [blame] | 27 | static SOCKET |
Juan Lang | d89279a | 2004-03-01 23:28:02 +0000 | [diff] [blame] | 28 | open_http (const char *server) |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 29 | { |
| 30 | WSADATA wsad; |
| 31 | struct sockaddr_in sa; |
| 32 | SOCKET s; |
| 33 | |
Juan Lang | d89279a | 2004-03-01 23:28:02 +0000 | [diff] [blame] | 34 | report (R_STATUS, "Opening HTTP connection to %s", server); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 35 | if (WSAStartup (MAKEWORD (2,2), &wsad)) return INVALID_SOCKET; |
| 36 | |
Ferenc Wagner | ca0f579 | 2004-03-19 19:15:23 +0000 | [diff] [blame] | 37 | sa.sin_family = AF_INET; |
| 38 | sa.sin_port = htons (80); |
| 39 | sa.sin_addr.s_addr = inet_addr (server); |
| 40 | if (sa.sin_addr.s_addr == INADDR_NONE) { |
| 41 | struct hostent *host = gethostbyname (server); |
| 42 | if (!host) { |
| 43 | report (R_ERROR, "Hostname lookup failed for %s", server); |
| 44 | goto failure; |
Juan Lang | d89279a | 2004-03-01 23:28:02 +0000 | [diff] [blame] | 45 | } |
Ferenc Wagner | ca0f579 | 2004-03-19 19:15:23 +0000 | [diff] [blame] | 46 | sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr; |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 47 | } |
Ferenc Wagner | ca0f579 | 2004-03-19 19:15:23 +0000 | [diff] [blame] | 48 | s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 49 | if (s == INVALID_SOCKET) { |
| 50 | report (R_ERROR, "Can't open network socket: %d", |
| 51 | WSAGetLastError ()); |
| 52 | goto failure; |
| 53 | } |
| 54 | if (!connect (s, (struct sockaddr*)&sa, sizeof (struct sockaddr_in))) |
| 55 | return s; |
| 56 | |
| 57 | report (R_ERROR, "Can't connect: %d", WSAGetLastError ()); |
| 58 | closesocket (s); |
| 59 | failure: |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 60 | WSACleanup (); |
| 61 | return INVALID_SOCKET; |
| 62 | } |
| 63 | |
Mike McCormack | d73dad6 | 2005-06-04 10:01:25 +0000 | [diff] [blame] | 64 | static int |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 65 | close_http (SOCKET s) |
| 66 | { |
| 67 | int ret; |
| 68 | |
| 69 | ret = closesocket (s); |
| 70 | return (WSACleanup () || ret); |
| 71 | } |
| 72 | |
Mike McCormack | d73dad6 | 2005-06-04 10:01:25 +0000 | [diff] [blame] | 73 | static int |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 74 | send_buf (SOCKET s, const char *buf, size_t length) |
| 75 | { |
| 76 | int sent; |
| 77 | |
| 78 | while (length > 0) { |
| 79 | sent = send (s, buf, length, 0); |
| 80 | if (sent == SOCKET_ERROR) return 1; |
| 81 | buf += sent; |
| 82 | length -= sent; |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
Mike McCormack | d73dad6 | 2005-06-04 10:01:25 +0000 | [diff] [blame] | 87 | static int |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 88 | send_str (SOCKET s, ...) |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 89 | { |
| 90 | va_list ap; |
| 91 | char *p; |
| 92 | int ret; |
| 93 | size_t len; |
| 94 | |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 95 | va_start (ap, s); |
| 96 | p = vstrmake (&len, ap); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 97 | va_end (ap); |
| 98 | if (!p) return 1; |
| 99 | ret = send_buf (s, p, len); |
Francois Gouget | bf6b95e | 2009-05-17 13:28:23 +0200 | [diff] [blame^] | 100 | heap_free (p); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | int |
| 105 | send_file (const char *name) |
| 106 | { |
| 107 | SOCKET s; |
Alexandre Julliard | 23296f0 | 2008-07-10 17:45:22 +0200 | [diff] [blame] | 108 | HANDLE file; |
Ferenc Wagner | feaad96 | 2004-02-21 04:01:56 +0000 | [diff] [blame] | 109 | #define BUFLEN 8192 |
Mike McCormack | 93ab695 | 2005-08-26 08:53:31 +0000 | [diff] [blame] | 110 | char buffer[BUFLEN+1]; |
Alexandre Julliard | 23296f0 | 2008-07-10 17:45:22 +0200 | [diff] [blame] | 111 | DWORD bytes_read, filesize; |
Alexandre Julliard | b4e26f0 | 2008-07-21 13:00:14 +0200 | [diff] [blame] | 112 | size_t total, count; |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 113 | char *str; |
| 114 | int ret; |
| 115 | |
Ferenc Wagner | ffc30c8 | 2004-10-05 02:10:37 +0000 | [diff] [blame] | 116 | /* RFC 2616 */ |
Ferenc Wagner | 5853c9e | 2004-10-04 19:28:59 +0000 | [diff] [blame] | 117 | #define SEP "--8<--cut-here--8<--" |
Francois Gouget | cfc3943 | 2004-05-04 04:13:05 +0000 | [diff] [blame] | 118 | static const char head[] = "POST /submit HTTP/1.0\r\n" |
Ferenc Wagner | ca0f579 | 2004-03-19 19:15:23 +0000 | [diff] [blame] | 119 | "Host: test.winehq.org\r\n" |
Ferenc Wagner | 8387770 | 2004-03-19 01:54:10 +0000 | [diff] [blame] | 120 | "User-Agent: Winetest Shell\r\n" |
Ferenc Wagner | 5853c9e | 2004-10-04 19:28:59 +0000 | [diff] [blame] | 121 | "Content-Type: multipart/form-data; boundary=\"" SEP "\"\r\n" |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 122 | "Content-Length: %u\r\n\r\n"; |
Francois Gouget | cfc3943 | 2004-05-04 04:13:05 +0000 | [diff] [blame] | 123 | static const char body1[] = "--" SEP "\r\n" |
Ferenc Wagner | 5853c9e | 2004-10-04 19:28:59 +0000 | [diff] [blame] | 124 | "Content-Disposition: form-data; name=\"reportfile\"; filename=\"%s\"\r\n" |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 125 | "Content-Type: application/octet-stream\r\n\r\n"; |
Francois Gouget | cfc3943 | 2004-05-04 04:13:05 +0000 | [diff] [blame] | 126 | static const char body2[] = "\r\n--" SEP "\r\n" |
Ferenc Wagner | ffc30c8 | 2004-10-05 02:10:37 +0000 | [diff] [blame] | 127 | "Content-Disposition: form-data; name=\"submit\"\r\n\r\n" |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 128 | "Upload File\r\n" |
| 129 | "--" SEP "--\r\n"; |
| 130 | |
Ferenc Wagner | ca0f579 | 2004-03-19 19:15:23 +0000 | [diff] [blame] | 131 | s = open_http ("test.winehq.org"); |
| 132 | if (s == INVALID_SOCKET) return 1; |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 133 | |
Alexandre Julliard | 23296f0 | 2008-07-10 17:45:22 +0200 | [diff] [blame] | 134 | file = CreateFileA( name, GENERIC_READ, |
| 135 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 136 | NULL, OPEN_EXISTING, 0, NULL ); |
Detlef Riekenberg | fbc9dc1 | 2008-07-16 18:33:12 +0200 | [diff] [blame] | 137 | |
| 138 | if ((file == INVALID_HANDLE_VALUE) && |
| 139 | (GetLastError() == ERROR_INVALID_PARAMETER)) { |
| 140 | /* FILE_SHARE_DELETE not supported on win9x */ |
| 141 | file = CreateFileA( name, GENERIC_READ, |
| 142 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 143 | NULL, OPEN_EXISTING, 0, NULL ); |
| 144 | } |
Alexandre Julliard | 23296f0 | 2008-07-10 17:45:22 +0200 | [diff] [blame] | 145 | if (file == INVALID_HANDLE_VALUE) |
| 146 | { |
| 147 | report (R_WARNING, "Can't open file '%s': %u", name, GetLastError()); |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 148 | goto abort1; |
| 149 | } |
Alexandre Julliard | 23296f0 | 2008-07-10 17:45:22 +0200 | [diff] [blame] | 150 | filesize = GetFileSize( file, NULL ); |
Paul Vriens | 70ce548 | 2006-11-30 19:01:02 +0100 | [diff] [blame] | 151 | if (filesize > 1.5*1024*1024) { |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 152 | report (R_WARNING, |
Paul Vriens | 70ce548 | 2006-11-30 19:01:02 +0100 | [diff] [blame] | 153 | "File too big (%.1f MB > 1.5 MB); submitting partial report.", |
Ferenc Wagner | f8dba77 | 2004-07-06 21:03:22 +0000 | [diff] [blame] | 154 | filesize/1024.0/1024); |
Paul Vriens | 70ce548 | 2006-11-30 19:01:02 +0100 | [diff] [blame] | 155 | filesize = 1.5*1024*1024; |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 156 | } |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 157 | |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 158 | report (R_STATUS, "Sending header"); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 159 | str = strmake (&total, body1, name); |
| 160 | ret = send_str (s, head, filesize + total + sizeof body2 - 1) || |
| 161 | send_buf (s, str, total); |
Francois Gouget | bf6b95e | 2009-05-17 13:28:23 +0200 | [diff] [blame^] | 162 | heap_free (str); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 163 | if (ret) { |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 164 | report (R_WARNING, "Error sending header: %d, %d", |
| 165 | errno, WSAGetLastError ()); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 166 | goto abort2; |
| 167 | } |
| 168 | |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 169 | report (R_STATUS, "Sending %u bytes of data", filesize); |
Ferenc Wagner | feaad96 | 2004-02-21 04:01:56 +0000 | [diff] [blame] | 170 | report (R_PROGRESS, 2, filesize); |
Ferenc Wagner | f8dba77 | 2004-07-06 21:03:22 +0000 | [diff] [blame] | 171 | total = 0; |
Alexandre Julliard | 23296f0 | 2008-07-10 17:45:22 +0200 | [diff] [blame] | 172 | while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) { |
| 173 | if (!bytes_read) break; |
Ferenc Wagner | f8dba77 | 2004-07-06 21:03:22 +0000 | [diff] [blame] | 174 | total += bytes_read; |
| 175 | if (total > filesize) bytes_read -= total - filesize; |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 176 | if (send_buf (s, buffer, bytes_read)) { |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 177 | report (R_WARNING, "Error sending body: %d, %d", |
| 178 | errno, WSAGetLastError ()); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 179 | goto abort2; |
| 180 | } |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 181 | report (R_DELTA, bytes_read, "Network transfer: In progress"); |
| 182 | } |
Alexandre Julliard | 23296f0 | 2008-07-10 17:45:22 +0200 | [diff] [blame] | 183 | CloseHandle( file ); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 184 | |
| 185 | if (send_buf (s, body2, sizeof body2 - 1)) { |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 186 | report (R_WARNING, "Error sending trailer: %d, %d", |
| 187 | errno, WSAGetLastError ()); |
Mike McCormack | a4fff8b | 2006-10-14 09:55:40 +0900 | [diff] [blame] | 188 | goto abort1; |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 189 | } |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 190 | report (R_DELTA, 0, "Network transfer: Done"); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 191 | |
| 192 | total = 0; |
Ferenc Wagner | feaad96 | 2004-02-21 04:01:56 +0000 | [diff] [blame] | 193 | while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) { |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 194 | if ((signed)bytes_read == SOCKET_ERROR) { |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 195 | report (R_WARNING, "Error receiving reply: %d, %d", |
| 196 | errno, WSAGetLastError ()); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 197 | goto abort1; |
| 198 | } |
| 199 | total += bytes_read; |
Ferenc Wagner | feaad96 | 2004-02-21 04:01:56 +0000 | [diff] [blame] | 200 | if (total == BUFLEN) { |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 201 | report (R_WARNING, "Buffer overflow"); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 202 | goto abort1; |
| 203 | } |
| 204 | } |
| 205 | if (close_http (s)) { |
Ferenc Wagner | 354f662 | 2004-01-15 01:48:05 +0000 | [diff] [blame] | 206 | report (R_WARNING, "Error closing connection: %d, %d", |
| 207 | errno, WSAGetLastError ()); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 208 | return 1; |
| 209 | } |
| 210 | |
Alexandre Julliard | b4e26f0 | 2008-07-21 13:00:14 +0200 | [diff] [blame] | 211 | str = strmake (&count, "Received %s (%d bytes).\n", |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 212 | name, filesize); |
Alexandre Julliard | b4e26f0 | 2008-07-21 13:00:14 +0200 | [diff] [blame] | 213 | ret = memcmp (str, buffer + total - count, count); |
Francois Gouget | bf6b95e | 2009-05-17 13:28:23 +0200 | [diff] [blame^] | 214 | heap_free (str); |
Ferenc Wagner | feaad96 | 2004-02-21 04:01:56 +0000 | [diff] [blame] | 215 | if (ret) { |
| 216 | buffer[total] = 0; |
| 217 | str = strstr (buffer, "\r\n\r\n"); |
Ferenc Wagner | ae1238b | 2004-08-18 20:56:00 +0000 | [diff] [blame] | 218 | if (!str) str = buffer; |
| 219 | else str = str + 4; |
Ferenc Wagner | feaad96 | 2004-02-21 04:01:56 +0000 | [diff] [blame] | 220 | report (R_ERROR, "Can't submit logfile '%s'. " |
Ferenc Wagner | ae1238b | 2004-08-18 20:56:00 +0000 | [diff] [blame] | 221 | "Server response: %s", name, str); |
Ferenc Wagner | feaad96 | 2004-02-21 04:01:56 +0000 | [diff] [blame] | 222 | } |
Ferenc Wagner | feaad96 | 2004-02-21 04:01:56 +0000 | [diff] [blame] | 223 | return ret; |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 224 | |
| 225 | abort2: |
Alexandre Julliard | 23296f0 | 2008-07-10 17:45:22 +0200 | [diff] [blame] | 226 | CloseHandle( file ); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 227 | abort1: |
| 228 | close_http (s); |
Alexandre Julliard | 9f71bd9 | 2003-12-04 02:01:39 +0000 | [diff] [blame] | 229 | return 1; |
| 230 | } |