Fixed some duplicate CRYPT_Free calls (spotted by Michael Jung).
[wine/multimedia.git] / programs / winetest / send.c
blob6cd2e9fcc824d1642719b93123f8c5d689de5bfc
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <winsock.h>
22 #include <stdio.h>
23 #include <errno.h>
25 #include "winetest.h"
27 SOCKET
28 open_http (const char *server)
30 WSADATA wsad;
31 struct sockaddr_in sa;
32 SOCKET s;
34 report (R_STATUS, "Opening HTTP connection to %s", server);
35 if (WSAStartup (MAKEWORD (2,2), &wsad)) return INVALID_SOCKET;
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;
46 sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
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;
54 if (!connect (s, (struct sockaddr*)&sa, sizeof (struct sockaddr_in)))
55 return s;
57 report (R_ERROR, "Can't connect: %d", WSAGetLastError ());
58 closesocket (s);
59 failure:
60 WSACleanup ();
61 return INVALID_SOCKET;
64 int
65 close_http (SOCKET s)
67 int ret;
69 ret = closesocket (s);
70 return (WSACleanup () || ret);
73 int
74 send_buf (SOCKET s, const char *buf, size_t length)
76 int sent;
78 while (length > 0) {
79 sent = send (s, buf, length, 0);
80 if (sent == SOCKET_ERROR) return 1;
81 buf += sent;
82 length -= sent;
84 return 0;
87 int
88 send_str (SOCKET s, ...)
90 va_list ap;
91 char *p;
92 int ret;
93 size_t len;
95 va_start (ap, s);
96 p = vstrmake (&len, ap);
97 va_end (ap);
98 if (!p) return 1;
99 ret = send_buf (s, p, len);
100 free (p);
101 return ret;
105 send_file (const char *name)
107 SOCKET s;
108 FILE *f;
109 #define BUFLEN 8192
110 unsigned char *buffer;
111 size_t bytes_read, total, filesize;
112 char *str;
113 int ret;
115 /* RFC 2068 */
116 #define SEP "-"
117 static const char head[] = "POST /submit HTTP/1.0\r\n"
118 "Host: test.winehq.org\r\n"
119 "User-Agent: Winetest Shell\r\n"
120 "Content-Type: multipart/form-data; boundary=" SEP "\r\n"
121 "Content-Length: %u\r\n\r\n";
122 static const char body1[] = "--" SEP "\r\n"
123 "Content-Disposition: form-data; name=reportfile; filename=\"%s\"\r\n"
124 "Content-Type: application/octet-stream\r\n\r\n";
125 static const char body2[] = "\r\n--" SEP "\r\n"
126 "Content-Disposition: form-data; name=submit\r\n\r\n"
127 "Upload File\r\n"
128 "--" SEP "--\r\n";
130 buffer = xmalloc (BUFLEN + 1);
131 s = open_http ("test.winehq.org");
132 if (s == INVALID_SOCKET) return 1;
134 f = fopen (name, "rb");
135 if (!f) {
136 report (R_WARNING, "Can't open file '%s': %d", name, errno);
137 goto abort1;
139 fseek (f, 0, SEEK_END);
140 filesize = ftell (f);
141 if (filesize > 1024*1024) {
142 report (R_WARNING,
143 "File too big (%.1f MB > 1 MB); submitting partial report.",
144 filesize/1024.0/1024);
145 filesize = 1024*1024;
147 fseek (f, 0, SEEK_SET);
149 report (R_STATUS, "Sending header");
150 str = strmake (&total, body1, name);
151 ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
152 send_buf (s, str, total);
153 free (str);
154 if (ret) {
155 report (R_WARNING, "Error sending header: %d, %d",
156 errno, WSAGetLastError ());
157 goto abort2;
160 report (R_STATUS, "Sending %u bytes of data", filesize);
161 report (R_PROGRESS, 2, filesize);
162 total = 0;
163 while (total < filesize && (bytes_read = fread (buffer, 1, BUFLEN/2, f))) {
164 if ((signed)bytes_read == -1) {
165 report (R_WARNING, "Error reading log file: %d", errno);
166 goto abort2;
168 total += bytes_read;
169 if (total > filesize) bytes_read -= total - filesize;
170 if (send_buf (s, buffer, bytes_read)) {
171 report (R_WARNING, "Error sending body: %d, %d",
172 errno, WSAGetLastError ());
173 goto abort2;
175 report (R_DELTA, bytes_read, "Network transfer: In progress");
177 fclose (f);
179 if (send_buf (s, body2, sizeof body2 - 1)) {
180 report (R_WARNING, "Error sending trailer: %d, %d",
181 errno, WSAGetLastError ());
182 goto abort2;
184 report (R_DELTA, 0, "Network transfer: Done");
186 total = 0;
187 while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
188 if ((signed)bytes_read == SOCKET_ERROR) {
189 report (R_WARNING, "Error receiving reply: %d, %d",
190 errno, WSAGetLastError ());
191 goto abort1;
193 total += bytes_read;
194 if (total == BUFLEN) {
195 report (R_WARNING, "Buffer overflow");
196 goto abort1;
199 if (close_http (s)) {
200 report (R_WARNING, "Error closing connection: %d, %d",
201 errno, WSAGetLastError ());
202 return 1;
205 str = strmake (&bytes_read, "Received %s (%d bytes).\n",
206 name, filesize);
207 ret = memcmp (str, buffer + total - bytes_read, bytes_read);
208 free (str);
209 if (ret) {
210 buffer[total] = 0;
211 str = strstr (buffer, "\r\n\r\n");
212 if (str) buffer = str + 4;
213 report (R_ERROR, "Can't submit logfile '%s'. "
214 "Server response: %s", name, buffer);
216 free (buffer);
217 return ret;
219 abort2:
220 fclose (f);
221 abort1:
222 close_http (s);
223 free (buffer);
224 return 1;