2003-12-04 02:01:39 +00:00
|
|
|
/*
|
|
|
|
* HTTP handling functions.
|
|
|
|
*
|
|
|
|
* Copyright 2003 Ferenc Wagner
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2004-06-15 22:45:15 +00:00
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2003-12-04 02:01:39 +00:00
|
|
|
*/
|
2004-06-15 22:45:15 +00:00
|
|
|
|
2003-12-04 02:01:39 +00:00
|
|
|
#include <winsock.h>
|
|
|
|
#include <stdio.h>
|
2004-01-15 01:48:05 +00:00
|
|
|
#include <errno.h>
|
2003-12-04 02:01:39 +00:00
|
|
|
|
|
|
|
#include "winetest.h"
|
|
|
|
|
|
|
|
SOCKET
|
2004-03-01 23:28:02 +00:00
|
|
|
open_http (const char *server)
|
2003-12-04 02:01:39 +00:00
|
|
|
{
|
|
|
|
WSADATA wsad;
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
SOCKET s;
|
|
|
|
|
2004-03-01 23:28:02 +00:00
|
|
|
report (R_STATUS, "Opening HTTP connection to %s", server);
|
2003-12-04 02:01:39 +00:00
|
|
|
if (WSAStartup (MAKEWORD (2,2), &wsad)) return INVALID_SOCKET;
|
|
|
|
|
2004-03-19 19:15:23 +00:00
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
sa.sin_port = htons (80);
|
|
|
|
sa.sin_addr.s_addr = inet_addr (server);
|
|
|
|
if (sa.sin_addr.s_addr == INADDR_NONE) {
|
|
|
|
struct hostent *host = gethostbyname (server);
|
|
|
|
if (!host) {
|
|
|
|
report (R_ERROR, "Hostname lookup failed for %s", server);
|
|
|
|
goto failure;
|
2004-03-01 23:28:02 +00:00
|
|
|
}
|
2004-03-19 19:15:23 +00:00
|
|
|
sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
|
|
|
|
}
|
|
|
|
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
report (R_ERROR, "Can't open network socket: %d",
|
|
|
|
WSAGetLastError ());
|
|
|
|
goto failure;
|
2003-12-04 02:01:39 +00:00
|
|
|
}
|
2004-03-19 19:15:23 +00:00
|
|
|
if (!connect (s, (struct sockaddr*)&sa, sizeof (struct sockaddr_in)))
|
|
|
|
return s;
|
|
|
|
|
|
|
|
report (R_ERROR, "Can't connect: %d", WSAGetLastError ());
|
|
|
|
closesocket (s);
|
|
|
|
failure:
|
2003-12-04 02:01:39 +00:00
|
|
|
WSACleanup ();
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
close_http (SOCKET s)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = closesocket (s);
|
|
|
|
return (WSACleanup () || ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
send_buf (SOCKET s, const char *buf, size_t length)
|
|
|
|
{
|
|
|
|
int sent;
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
sent = send (s, buf, length, 0);
|
|
|
|
if (sent == SOCKET_ERROR) return 1;
|
|
|
|
buf += sent;
|
|
|
|
length -= sent;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-01-15 01:48:05 +00:00
|
|
|
send_str (SOCKET s, ...)
|
2003-12-04 02:01:39 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *p;
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
2004-01-15 01:48:05 +00:00
|
|
|
va_start (ap, s);
|
|
|
|
p = vstrmake (&len, ap);
|
2003-12-04 02:01:39 +00:00
|
|
|
va_end (ap);
|
|
|
|
if (!p) return 1;
|
|
|
|
ret = send_buf (s, p, len);
|
|
|
|
free (p);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
send_file (const char *name)
|
|
|
|
{
|
|
|
|
SOCKET s;
|
|
|
|
FILE *f;
|
2004-02-21 04:01:56 +00:00
|
|
|
#define BUFLEN 8192
|
|
|
|
unsigned char *buffer;
|
2003-12-04 02:01:39 +00:00
|
|
|
size_t bytes_read, total, filesize;
|
|
|
|
char *str;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* RFC 2068 */
|
|
|
|
#define SEP "-"
|
2004-05-04 04:13:05 +00:00
|
|
|
static const char head[] = "POST /submit HTTP/1.0\r\n"
|
2004-03-19 19:15:23 +00:00
|
|
|
"Host: test.winehq.org\r\n"
|
2004-03-19 01:54:10 +00:00
|
|
|
"User-Agent: Winetest Shell\r\n"
|
2003-12-04 02:01:39 +00:00
|
|
|
"Content-Type: multipart/form-data; boundary=" SEP "\r\n"
|
|
|
|
"Content-Length: %u\r\n\r\n";
|
2004-05-04 04:13:05 +00:00
|
|
|
static const char body1[] = "--" SEP "\r\n"
|
2003-12-04 02:01:39 +00:00
|
|
|
"Content-Disposition: form-data; name=reportfile; filename=\"%s\"\r\n"
|
|
|
|
"Content-Type: application/octet-stream\r\n\r\n";
|
2004-05-04 04:13:05 +00:00
|
|
|
static const char body2[] = "\r\n--" SEP "\r\n"
|
2004-04-20 20:15:43 +00:00
|
|
|
"Content-Disposition: form-data; name=submit\r\n\r\n"
|
2003-12-04 02:01:39 +00:00
|
|
|
"Upload File\r\n"
|
|
|
|
"--" SEP "--\r\n";
|
|
|
|
|
2004-02-21 04:01:56 +00:00
|
|
|
buffer = xmalloc (BUFLEN + 1);
|
2004-03-19 19:15:23 +00:00
|
|
|
s = open_http ("test.winehq.org");
|
|
|
|
if (s == INVALID_SOCKET) return 1;
|
2003-12-04 02:01:39 +00:00
|
|
|
|
|
|
|
f = fopen (name, "rb");
|
2004-01-15 01:48:05 +00:00
|
|
|
if (!f) {
|
|
|
|
report (R_WARNING, "Can't open file '%s': %d", name, errno);
|
|
|
|
goto abort1;
|
|
|
|
}
|
2003-12-04 02:01:39 +00:00
|
|
|
fseek (f, 0, SEEK_END);
|
|
|
|
filesize = ftell (f);
|
2004-01-15 01:48:05 +00:00
|
|
|
if (filesize > 1024*1024) {
|
|
|
|
report (R_WARNING,
|
2004-07-06 21:03:22 +00:00
|
|
|
"File too big (%.1f MB > 1 MB); submitting partial report.",
|
|
|
|
filesize/1024.0/1024);
|
|
|
|
filesize = 1024*1024;
|
2004-01-15 01:48:05 +00:00
|
|
|
}
|
2003-12-04 02:01:39 +00:00
|
|
|
fseek (f, 0, SEEK_SET);
|
|
|
|
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_STATUS, "Sending header");
|
2003-12-04 02:01:39 +00:00
|
|
|
str = strmake (&total, body1, name);
|
|
|
|
ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
|
|
|
|
send_buf (s, str, total);
|
|
|
|
free (str);
|
|
|
|
if (ret) {
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_WARNING, "Error sending header: %d, %d",
|
|
|
|
errno, WSAGetLastError ());
|
2003-12-04 02:01:39 +00:00
|
|
|
goto abort2;
|
|
|
|
}
|
|
|
|
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_STATUS, "Sending %u bytes of data", filesize);
|
2004-02-21 04:01:56 +00:00
|
|
|
report (R_PROGRESS, 2, filesize);
|
2004-07-06 21:03:22 +00:00
|
|
|
total = 0;
|
|
|
|
while (total < filesize && (bytes_read = fread (buffer, 1, BUFLEN/2, f))) {
|
|
|
|
if ((signed)bytes_read == -1) {
|
|
|
|
report (R_WARNING, "Error reading log file: %d", errno);
|
|
|
|
goto abort2;
|
|
|
|
}
|
|
|
|
total += bytes_read;
|
|
|
|
if (total > filesize) bytes_read -= total - filesize;
|
2003-12-04 02:01:39 +00:00
|
|
|
if (send_buf (s, buffer, bytes_read)) {
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_WARNING, "Error sending body: %d, %d",
|
|
|
|
errno, WSAGetLastError ());
|
2003-12-04 02:01:39 +00:00
|
|
|
goto abort2;
|
|
|
|
}
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_DELTA, bytes_read, "Network transfer: In progress");
|
|
|
|
}
|
2003-12-04 02:01:39 +00:00
|
|
|
fclose (f);
|
|
|
|
|
|
|
|
if (send_buf (s, body2, sizeof body2 - 1)) {
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_WARNING, "Error sending trailer: %d, %d",
|
|
|
|
errno, WSAGetLastError ());
|
2003-12-04 02:01:39 +00:00
|
|
|
goto abort2;
|
|
|
|
}
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_DELTA, 0, "Network transfer: Done");
|
2003-12-04 02:01:39 +00:00
|
|
|
|
|
|
|
total = 0;
|
2004-02-21 04:01:56 +00:00
|
|
|
while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
|
2003-12-04 02:01:39 +00:00
|
|
|
if ((signed)bytes_read == SOCKET_ERROR) {
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_WARNING, "Error receiving reply: %d, %d",
|
|
|
|
errno, WSAGetLastError ());
|
2003-12-04 02:01:39 +00:00
|
|
|
goto abort1;
|
|
|
|
}
|
|
|
|
total += bytes_read;
|
2004-02-21 04:01:56 +00:00
|
|
|
if (total == BUFLEN) {
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_WARNING, "Buffer overflow");
|
2003-12-04 02:01:39 +00:00
|
|
|
goto abort1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (close_http (s)) {
|
2004-01-15 01:48:05 +00:00
|
|
|
report (R_WARNING, "Error closing connection: %d, %d",
|
|
|
|
errno, WSAGetLastError ());
|
2003-12-04 02:01:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = strmake (&bytes_read, "Received %s (%d bytes).\n",
|
|
|
|
name, filesize);
|
|
|
|
ret = memcmp (str, buffer + total - bytes_read, bytes_read);
|
|
|
|
free (str);
|
2004-02-21 04:01:56 +00:00
|
|
|
if (ret) {
|
|
|
|
buffer[total] = 0;
|
|
|
|
str = strstr (buffer, "\r\n\r\n");
|
|
|
|
if (str) buffer = str + 4;
|
|
|
|
report (R_ERROR, "Can't submit logfile '%s'. "
|
|
|
|
"Server response: %s", name, buffer);
|
|
|
|
}
|
|
|
|
free (buffer);
|
|
|
|
return ret;
|
2003-12-04 02:01:39 +00:00
|
|
|
|
|
|
|
abort2:
|
|
|
|
fclose (f);
|
|
|
|
abort1:
|
|
|
|
close_http (s);
|
2004-02-21 04:01:56 +00:00
|
|
|
free (buffer);
|
2003-12-04 02:01:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|