blob: 9eae079c4bf42256ef9e6c23d34b2178018e6bc7 [file] [log] [blame]
Alexandre Julliard9f71bd92003-12-04 02:01:39 +00001/*
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 Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000019 */
Ferenc Wagner24624f62004-06-15 22:45:15 +000020
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000021#include <winsock.h>
22#include <stdio.h>
Ferenc Wagner354f6622004-01-15 01:48:05 +000023#include <errno.h>
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000024
25#include "winetest.h"
26
Mike McCormackd73dad62005-06-04 10:01:25 +000027static SOCKET
Juan Langd89279a2004-03-01 23:28:02 +000028open_http (const char *server)
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000029{
30 WSADATA wsad;
31 struct sockaddr_in sa;
32 SOCKET s;
33
Juan Langd89279a2004-03-01 23:28:02 +000034 report (R_STATUS, "Opening HTTP connection to %s", server);
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000035 if (WSAStartup (MAKEWORD (2,2), &wsad)) return INVALID_SOCKET;
36
Ferenc Wagnerca0f5792004-03-19 19:15:23 +000037 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 Langd89279a2004-03-01 23:28:02 +000045 }
Ferenc Wagnerca0f5792004-03-19 19:15:23 +000046 sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000047 }
Ferenc Wagnerca0f5792004-03-19 19:15:23 +000048 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 Julliard9f71bd92003-12-04 02:01:39 +000060 WSACleanup ();
61 return INVALID_SOCKET;
62}
63
Mike McCormackd73dad62005-06-04 10:01:25 +000064static int
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000065close_http (SOCKET s)
66{
67 int ret;
68
69 ret = closesocket (s);
70 return (WSACleanup () || ret);
71}
72
Mike McCormackd73dad62005-06-04 10:01:25 +000073static int
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000074send_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 McCormackd73dad62005-06-04 10:01:25 +000087static int
Ferenc Wagner354f6622004-01-15 01:48:05 +000088send_str (SOCKET s, ...)
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000089{
90 va_list ap;
91 char *p;
92 int ret;
93 size_t len;
94
Ferenc Wagner354f6622004-01-15 01:48:05 +000095 va_start (ap, s);
96 p = vstrmake (&len, ap);
Alexandre Julliard9f71bd92003-12-04 02:01:39 +000097 va_end (ap);
98 if (!p) return 1;
99 ret = send_buf (s, p, len);
Francois Gougetbf6b95e2009-05-17 13:28:23 +0200100 heap_free (p);
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000101 return ret;
102}
103
104int
105send_file (const char *name)
106{
107 SOCKET s;
Alexandre Julliard23296f02008-07-10 17:45:22 +0200108 HANDLE file;
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000109#define BUFLEN 8192
Mike McCormack93ab6952005-08-26 08:53:31 +0000110 char buffer[BUFLEN+1];
Alexandre Julliard23296f02008-07-10 17:45:22 +0200111 DWORD bytes_read, filesize;
Alexandre Julliardb4e26f02008-07-21 13:00:14 +0200112 size_t total, count;
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000113 char *str;
114 int ret;
115
Ferenc Wagnerffc30c82004-10-05 02:10:37 +0000116 /* RFC 2616 */
Ferenc Wagner5853c9e2004-10-04 19:28:59 +0000117#define SEP "--8<--cut-here--8<--"
Francois Gougetcfc39432004-05-04 04:13:05 +0000118 static const char head[] = "POST /submit HTTP/1.0\r\n"
Ferenc Wagnerca0f5792004-03-19 19:15:23 +0000119 "Host: test.winehq.org\r\n"
Ferenc Wagner83877702004-03-19 01:54:10 +0000120 "User-Agent: Winetest Shell\r\n"
Ferenc Wagner5853c9e2004-10-04 19:28:59 +0000121 "Content-Type: multipart/form-data; boundary=\"" SEP "\"\r\n"
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000122 "Content-Length: %u\r\n\r\n";
Francois Gougetcfc39432004-05-04 04:13:05 +0000123 static const char body1[] = "--" SEP "\r\n"
Ferenc Wagner5853c9e2004-10-04 19:28:59 +0000124 "Content-Disposition: form-data; name=\"reportfile\"; filename=\"%s\"\r\n"
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000125 "Content-Type: application/octet-stream\r\n\r\n";
Francois Gougetcfc39432004-05-04 04:13:05 +0000126 static const char body2[] = "\r\n--" SEP "\r\n"
Ferenc Wagnerffc30c82004-10-05 02:10:37 +0000127 "Content-Disposition: form-data; name=\"submit\"\r\n\r\n"
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000128 "Upload File\r\n"
129 "--" SEP "--\r\n";
130
Ferenc Wagnerca0f5792004-03-19 19:15:23 +0000131 s = open_http ("test.winehq.org");
132 if (s == INVALID_SOCKET) return 1;
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000133
Alexandre Julliard23296f02008-07-10 17:45:22 +0200134 file = CreateFileA( name, GENERIC_READ,
135 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
136 NULL, OPEN_EXISTING, 0, NULL );
Detlef Riekenbergfbc9dc12008-07-16 18:33:12 +0200137
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 Julliard23296f02008-07-10 17:45:22 +0200145 if (file == INVALID_HANDLE_VALUE)
146 {
147 report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
Ferenc Wagner354f6622004-01-15 01:48:05 +0000148 goto abort1;
149 }
Alexandre Julliard23296f02008-07-10 17:45:22 +0200150 filesize = GetFileSize( file, NULL );
Paul Vriens70ce5482006-11-30 19:01:02 +0100151 if (filesize > 1.5*1024*1024) {
Ferenc Wagner354f6622004-01-15 01:48:05 +0000152 report (R_WARNING,
Paul Vriens70ce5482006-11-30 19:01:02 +0100153 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
Ferenc Wagnerf8dba772004-07-06 21:03:22 +0000154 filesize/1024.0/1024);
Paul Vriens70ce5482006-11-30 19:01:02 +0100155 filesize = 1.5*1024*1024;
Ferenc Wagner354f6622004-01-15 01:48:05 +0000156 }
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000157
Ferenc Wagner354f6622004-01-15 01:48:05 +0000158 report (R_STATUS, "Sending header");
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000159 str = strmake (&total, body1, name);
160 ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
161 send_buf (s, str, total);
Francois Gougetbf6b95e2009-05-17 13:28:23 +0200162 heap_free (str);
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000163 if (ret) {
Ferenc Wagner354f6622004-01-15 01:48:05 +0000164 report (R_WARNING, "Error sending header: %d, %d",
165 errno, WSAGetLastError ());
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000166 goto abort2;
167 }
168
Ferenc Wagner354f6622004-01-15 01:48:05 +0000169 report (R_STATUS, "Sending %u bytes of data", filesize);
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000170 report (R_PROGRESS, 2, filesize);
Ferenc Wagnerf8dba772004-07-06 21:03:22 +0000171 total = 0;
Alexandre Julliard23296f02008-07-10 17:45:22 +0200172 while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
173 if (!bytes_read) break;
Ferenc Wagnerf8dba772004-07-06 21:03:22 +0000174 total += bytes_read;
175 if (total > filesize) bytes_read -= total - filesize;
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000176 if (send_buf (s, buffer, bytes_read)) {
Ferenc Wagner354f6622004-01-15 01:48:05 +0000177 report (R_WARNING, "Error sending body: %d, %d",
178 errno, WSAGetLastError ());
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000179 goto abort2;
180 }
Ferenc Wagner354f6622004-01-15 01:48:05 +0000181 report (R_DELTA, bytes_read, "Network transfer: In progress");
182 }
Alexandre Julliard23296f02008-07-10 17:45:22 +0200183 CloseHandle( file );
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000184
185 if (send_buf (s, body2, sizeof body2 - 1)) {
Ferenc Wagner354f6622004-01-15 01:48:05 +0000186 report (R_WARNING, "Error sending trailer: %d, %d",
187 errno, WSAGetLastError ());
Mike McCormacka4fff8b2006-10-14 09:55:40 +0900188 goto abort1;
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000189 }
Ferenc Wagner354f6622004-01-15 01:48:05 +0000190 report (R_DELTA, 0, "Network transfer: Done");
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000191
192 total = 0;
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000193 while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000194 if ((signed)bytes_read == SOCKET_ERROR) {
Ferenc Wagner354f6622004-01-15 01:48:05 +0000195 report (R_WARNING, "Error receiving reply: %d, %d",
196 errno, WSAGetLastError ());
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000197 goto abort1;
198 }
199 total += bytes_read;
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000200 if (total == BUFLEN) {
Ferenc Wagner354f6622004-01-15 01:48:05 +0000201 report (R_WARNING, "Buffer overflow");
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000202 goto abort1;
203 }
204 }
205 if (close_http (s)) {
Ferenc Wagner354f6622004-01-15 01:48:05 +0000206 report (R_WARNING, "Error closing connection: %d, %d",
207 errno, WSAGetLastError ());
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000208 return 1;
209 }
210
Alexandre Julliardb4e26f02008-07-21 13:00:14 +0200211 str = strmake (&count, "Received %s (%d bytes).\n",
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000212 name, filesize);
Alexandre Julliardb4e26f02008-07-21 13:00:14 +0200213 ret = memcmp (str, buffer + total - count, count);
Francois Gougetbf6b95e2009-05-17 13:28:23 +0200214 heap_free (str);
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000215 if (ret) {
216 buffer[total] = 0;
217 str = strstr (buffer, "\r\n\r\n");
Ferenc Wagnerae1238b2004-08-18 20:56:00 +0000218 if (!str) str = buffer;
219 else str = str + 4;
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000220 report (R_ERROR, "Can't submit logfile '%s'. "
Ferenc Wagnerae1238b2004-08-18 20:56:00 +0000221 "Server response: %s", name, str);
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000222 }
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000223 return ret;
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000224
225 abort2:
Alexandre Julliard23296f02008-07-10 17:45:22 +0200226 CloseHandle( file );
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000227 abort1:
228 close_http (s);
Alexandre Julliard9f71bd92003-12-04 02:01:39 +0000229 return 1;
230}