~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/programs/winetest/send.c

Version: ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  1 /*
  2  * HTTP handling functions.
  3  *
  4  * Copyright 2003 Ferenc Wagner
  5  *
  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.
 10  *
 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.
 15  *
 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
 19  */
 20 
 21 #include <winsock.h>
 22 #include <stdio.h>
 23 #include <errno.h>
 24 
 25 #include "winetest.h"
 26 
 27 static SOCKET
 28 open_http (const char *server)
 29 {
 30     WSADATA wsad;
 31     struct sockaddr_in sa;
 32     SOCKET s;
 33 
 34     report (R_STATUS, "Opening HTTP connection to %s", server);
 35     if (WSAStartup (MAKEWORD (2,2), &wsad)) return INVALID_SOCKET;
 36 
 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;
 45         }
 46         sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
 47     }
 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;
 53     }
 54     if (!connect (s, (struct sockaddr*)&sa, sizeof (struct sockaddr_in)))
 55         return s;
 56 
 57     report (R_ERROR, "Can't connect: %d", WSAGetLastError ());
 58     closesocket (s);
 59  failure:
 60     WSACleanup ();
 61     return INVALID_SOCKET;
 62 }
 63 
 64 static int
 65 close_http (SOCKET s)
 66 {
 67     int ret;
 68 
 69     ret = closesocket (s);
 70     return (WSACleanup () || ret);
 71 }
 72 
 73 static int
 74 send_buf (SOCKET s, const char *buf, size_t length)
 75 {
 76     int sent;
 77 
 78     while (length > 0) {
 79         sent = send (s, buf, length, 0);
 80         if (sent == SOCKET_ERROR) return 1;
 81         buf += sent;
 82         length -= sent;
 83     }
 84     return 0;
 85 }
 86 
 87 static int
 88 send_str (SOCKET s, ...)
 89 {
 90     va_list ap;
 91     char *p;
 92     int ret;
 93     size_t len;
 94 
 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;
102 }
103 
104 int
105 send_file (const char *name)
106 {
107     SOCKET s;
108     HANDLE file;
109 #define BUFLEN 8192
110     char buffer[BUFLEN+1];
111     DWORD bytes_read, filesize;
112     size_t total, count;
113     char *str;
114     int ret;
115 
116     /* RFC 2616 */
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"
128         "Upload File\r\n"
129         "--" SEP "--\r\n";
130 
131     s = open_http ("test.winehq.org");
132     if (s == INVALID_SOCKET) return 1;
133 
134     file = CreateFileA( name, GENERIC_READ,
135                         FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
136                         NULL, OPEN_EXISTING, 0, NULL );
137 
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 );
144     }
145     if (file == INVALID_HANDLE_VALUE)
146     {
147         report (R_WARNING, "Can't open file '%s': %u", name, GetLastError());
148         goto abort1;
149     }
150     filesize = GetFileSize( file, NULL );
151     if (filesize > 1.5*1024*1024) {
152         report (R_WARNING,
153                 "File too big (%.1f MB > 1.5 MB); submitting partial report.",
154                 filesize/1024.0/1024);
155         filesize = 1.5*1024*1024;
156     }
157 
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);
162     free (str);
163     if (ret) {
164         report (R_WARNING, "Error sending header: %d, %d",
165                 errno, WSAGetLastError ());
166         goto abort2;
167     }
168 
169     report (R_STATUS, "Sending %u bytes of data", filesize);
170     report (R_PROGRESS, 2, filesize);
171     total = 0;
172     while (total < filesize && ReadFile( file, buffer, BUFLEN/2, &bytes_read, NULL )) {
173         if (!bytes_read) break;
174         total += bytes_read;
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 ());
179             goto abort2;
180         }
181         report (R_DELTA, bytes_read, "Network transfer: In progress");
182     }
183     CloseHandle( file );
184 
185     if (send_buf (s, body2, sizeof body2 - 1)) {
186         report (R_WARNING, "Error sending trailer: %d, %d",
187                 errno, WSAGetLastError ());
188         goto abort1;
189     }
190     report (R_DELTA, 0, "Network transfer: Done");
191 
192     total = 0;
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 ());
197             goto abort1;
198         }
199         total += bytes_read;
200         if (total == BUFLEN) {
201             report (R_WARNING, "Buffer overflow");
202             goto abort1;
203         }
204     }
205     if (close_http (s)) {
206         report (R_WARNING, "Error closing connection: %d, %d",
207                 errno, WSAGetLastError ());
208         return 1;
209     }
210 
211     str = strmake (&count, "Received %s (%d bytes).\n",
212                    name, filesize);
213     ret = memcmp (str, buffer + total - count, count);
214     free (str);
215     if (ret) {
216         buffer[total] = 0;
217         str = strstr (buffer, "\r\n\r\n");
218         if (!str) str = buffer;
219         else str = str + 4;
220         report (R_ERROR, "Can't submit logfile '%s'. "
221                 "Server response: %s", name, str);
222     }
223     return ret;
224 
225  abort2:
226     CloseHandle( file );
227  abort1:
228     close_http (s);
229     return 1;
230 }
231 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.