Updated configure with autoconf 2.58. Set the bug report address.
[wine.git] / programs / winetest / send.c
blob167212c8268da4c86aaf5fdea769aba3e9c31374
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
20 #include <winsock.h>
21 #include <stdio.h>
23 #include "winetest.h"
25 SOCKET
26 open_http (const char *ipnum)
28 WSADATA wsad;
29 struct sockaddr_in sa;
30 SOCKET s;
32 if (WSAStartup (MAKEWORD (2,2), &wsad)) return INVALID_SOCKET;
34 s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
35 if (s != INVALID_SOCKET) {
36 sa.sin_family = AF_INET;
37 sa.sin_port = htons (80);
38 sa.sin_addr.s_addr = inet_addr (ipnum);
39 if (!connect (s, (struct sockaddr*)&sa,
40 sizeof (struct sockaddr_in)))
41 return s;
43 WSACleanup ();
44 return INVALID_SOCKET;
47 int
48 close_http (SOCKET s)
50 int ret;
52 ret = closesocket (s);
53 return (WSACleanup () || ret);
56 int
57 send_buf (SOCKET s, const char *buf, size_t length)
59 int sent;
61 while (length > 0) {
62 sent = send (s, buf, length, 0);
63 if (sent == SOCKET_ERROR) return 1;
64 buf += sent;
65 length -= sent;
67 return 0;
70 int
71 send_str (SOCKET s, const char *fmt, ...)
73 va_list ap;
74 char *p;
75 int ret;
76 size_t len;
78 va_start (ap, fmt);
79 p = vstrmake (&len, fmt, ap);
80 va_end (ap);
81 if (!p) return 1;
82 ret = send_buf (s, p, len);
83 free (p);
84 return ret;
87 int
88 send_file (const char *name)
90 SOCKET s;
91 FILE *f;
92 unsigned char buffer[8192];
93 size_t bytes_read, total, filesize;
94 char *str;
95 int ret;
97 /* RFC 2068 */
98 #define SEP "-"
99 const char head[] = "POST /~wferi/cgi-bin/winetests.cgi HTTP/1.0\r\n"
100 "Host: afavant\r\n"
101 "User-Agent: Winetests Shell\r\n"
102 "Content-Type: multipart/form-data; boundary=" SEP "\r\n"
103 "Content-Length: %u\r\n\r\n";
104 const char body1[] = "--" SEP "\r\n"
105 "Content-Disposition: form-data; name=reportfile; filename=\"%s\"\r\n"
106 "Content-Type: application/octet-stream\r\n\r\n";
107 const char body2[] = "\r\n--" SEP "\r\n"
108 "Content-Dispoition: form-data; name=submit\r\n\r\n"
109 "Upload File\r\n"
110 "--" SEP "--\r\n";
112 s = open_http ("157.181.170.47");
113 if (s == INVALID_SOCKET) {
114 fprintf (stderr, "Can't open connection: %x.\n",
115 WSAGetLastError ());
116 return 1;
119 f = fopen (name, "rb");
120 if (!f) goto abort1;
121 fseek (f, 0, SEEK_END);
122 filesize = ftell (f);
123 if (filesize > 1024*1024) goto abort2;
124 fseek (f, 0, SEEK_SET);
126 str = strmake (&total, body1, name);
127 ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
128 send_buf (s, str, total);
129 free (str);
130 if (ret) {
131 fprintf (stderr, "Can't send header.\n");
132 goto abort2;
135 while ((bytes_read = fread (buffer, 1, sizeof buffer, f)))
136 if (send_buf (s, buffer, bytes_read)) {
137 fprintf (stderr, "Can't send body.\n");
138 goto abort2;
140 fclose (f);
142 if (send_buf (s, body2, sizeof body2 - 1)) {
143 fprintf (stderr, "Can't send trailer.\n");
144 goto abort2;
147 total = 0;
148 while ((bytes_read = recv (s, buffer + total,
149 sizeof buffer - total, 0))) {
150 if ((signed)bytes_read == SOCKET_ERROR) {
151 fprintf (stderr, "Error receiving response: %d.\n",
152 WSAGetLastError ());
153 goto abort1;
155 total += bytes_read;
156 if (total == sizeof buffer) {
157 fprintf (stderr, "Buffer overflow.\n");
158 goto abort1;
161 if (close_http (s)) {
162 fprintf (stderr, "Error closing connection.\n");
163 return 1;
166 str = strmake (&bytes_read, "Received %s (%d bytes).\n",
167 name, filesize);
168 ret = memcmp (str, buffer + total - bytes_read, bytes_read);
169 free (str);
170 return ret!=0;
172 abort2:
173 fclose (f);
174 abort1:
175 close_http (s);
176 return 1;