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
26 open_http (const char *ipnum)
29 struct sockaddr_in sa;
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)))
44 return INVALID_SOCKET;
52 ret = closesocket (s);
53 return (WSACleanup () || ret);
57 send_buf (SOCKET s, const char *buf, size_t length)
62 sent = send (s, buf, length, 0);
63 if (sent == SOCKET_ERROR) return 1;
71 send_str (SOCKET s, const char *fmt, ...)
79 p = vstrmake (&len, fmt, ap);
82 ret = send_buf (s, p, len);
88 send_file (const char *name)
92 unsigned char buffer[8192];
93 size_t bytes_read, total, filesize;
99 const char head[] = "POST /~wferi/cgi-bin/winetests.cgi HTTP/1.0\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"
112 s = open_http ("157.181.170.47");
113 if (s == INVALID_SOCKET) {
114 fprintf (stderr, "Can't open connection: %x.\n",
119 f = fopen (name, "rb");
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);
131 fprintf (stderr, "Can't send header.\n");
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");
142 if (send_buf (s, body2, sizeof body2 - 1)) {
143 fprintf (stderr, "Can't send trailer.\n");
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",
156 if (total == sizeof buffer) {
157 fprintf (stderr, "Buffer overflow.\n");
161 if (close_http (s)) {
162 fprintf (stderr, "Error closing connection.\n");
166 str = strmake (&bytes_read, "Received %s (%d bytes).\n",
168 ret = memcmp (str, buffer + total - bytes_read, bytes_read);