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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 open_http (const char *server
)
31 struct sockaddr_in sa
;
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
);
43 report (R_ERROR
, "Hostname lookup failed for %s", server
);
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",
54 if (!connect (s
, (struct sockaddr
*)&sa
, sizeof (struct sockaddr_in
)))
57 report (R_ERROR
, "Can't connect: %d", WSAGetLastError ());
61 return INVALID_SOCKET
;
69 ret
= closesocket (s
);
70 return (WSACleanup () || ret
);
74 send_buf (SOCKET s
, const char *buf
, size_t length
)
79 sent
= send (s
, buf
, length
, 0);
80 if (sent
== SOCKET_ERROR
) return 1;
88 send_str (SOCKET s
, ...)
96 p
= vstrmake (&len
, ap
);
99 ret
= send_buf (s
, p
, len
);
105 send_file (const char *name
)
110 char buffer
[BUFLEN
+1];
111 DWORD bytes_read
, filesize
;
117 #define SEP "--8<--cut-here--8<--"
118 static const char head
[] = "POST /submit HTTP/1.0\r\n"
119 "Host: test.winehq.org\r\n"
120 "User-Agent: Winetest Shell\r\n"
121 "Content-Type: multipart/form-data; boundary=\"" SEP
"\"\r\n"
122 "Content-Length: %u\r\n\r\n";
123 static const char body1
[] = "--" SEP
"\r\n"
124 "Content-Disposition: form-data; name=\"reportfile\"; filename=\"%s\"\r\n"
125 "Content-Type: application/octet-stream\r\n\r\n";
126 static const char body2
[] = "\r\n--" SEP
"\r\n"
127 "Content-Disposition: form-data; name=\"submit\"\r\n\r\n"
131 s
= open_http ("test.winehq.org");
132 if (s
== INVALID_SOCKET
) return 1;
134 file
= CreateFileA( name
, GENERIC_READ
,
135 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
136 NULL
, OPEN_EXISTING
, 0, NULL
);
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
);
145 if (file
== INVALID_HANDLE_VALUE
)
147 report (R_WARNING
, "Can't open file '%s': %u", name
, GetLastError());
150 filesize
= GetFileSize( file
, NULL
);
151 if (filesize
> 1.5*1024*1024) {
153 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
154 filesize
/1024.0/1024);
155 filesize
= 1.5*1024*1024;
158 report (R_STATUS
, "Sending header");
159 str
= strmake (&total
, body1
, name
);
160 ret
= send_str (s
, head
, filesize
+ total
+ sizeof body2
- 1) ||
161 send_buf (s
, str
, total
);
164 report (R_WARNING
, "Error sending header: %d, %d",
165 errno
, WSAGetLastError ());
169 report (R_STATUS
, "Sending %u bytes of data", filesize
);
170 report (R_PROGRESS
, 2, filesize
);
172 while (total
< filesize
&& ReadFile( file
, buffer
, BUFLEN
/2, &bytes_read
, NULL
)) {
173 if (!bytes_read
) break;
175 if (total
> filesize
) bytes_read
-= total
- filesize
;
176 if (send_buf (s
, buffer
, bytes_read
)) {
177 report (R_WARNING
, "Error sending body: %d, %d",
178 errno
, WSAGetLastError ());
181 report (R_DELTA
, bytes_read
, "Network transfer: In progress");
185 if (send_buf (s
, body2
, sizeof body2
- 1)) {
186 report (R_WARNING
, "Error sending trailer: %d, %d",
187 errno
, WSAGetLastError ());
190 report (R_DELTA
, 0, "Network transfer: Done");
193 while ((bytes_read
= recv (s
, buffer
+total
, BUFLEN
-total
, 0))) {
194 if ((signed)bytes_read
== SOCKET_ERROR
) {
195 report (R_WARNING
, "Error receiving reply: %d, %d",
196 errno
, WSAGetLastError ());
200 if (total
== BUFLEN
) {
201 report (R_WARNING
, "Buffer overflow");
205 if (close_http (s
)) {
206 report (R_WARNING
, "Error closing connection: %d, %d",
207 errno
, WSAGetLastError ());
211 str
= strmake (&count
, "Received %s (%d bytes).\n",
213 ret
= memcmp (str
, buffer
+ total
- count
, count
);
217 str
= strstr (buffer
, "\r\n\r\n");
218 if (!str
) str
= buffer
;
220 report (R_ERROR
, "Can't submit logfile '%s'. "
221 "Server response: %s", name
, str
);