ReadFile and WriteFile must be passed a parameter for the number of
[wine] / dlls / wininet / utility.c
1 /*
2  * Wininet - Utility functions
3  *
4  * Copyright 1999 Corel Corporation
5  * Copyright 2002 CodeWeavers Inc.
6  *
7  * Ulrich Czekalla
8  * Aric Stewart
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wininet.h"
36 #include "winerror.h"
37 #include "winnls.h"
38
39 #include "wine/debug.h"
40 #include "internet.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
43
44 #define TIME_STRING_LEN  30
45
46 time_t ConvertTimeString(LPCWSTR asctime)
47 {
48     WCHAR tmpChar[TIME_STRING_LEN];
49     WCHAR *tmpChar2;
50     struct tm t;
51     int timelen = strlenW(asctime);
52
53     if(!asctime || !timelen)
54         return 0;
55
56     strncpyW(tmpChar, asctime, TIME_STRING_LEN);
57
58     /* Assert that the string is the expected length */
59     if (tmpChar[TIME_STRING_LEN - 1] != '\0')
60     {
61         tmpChar[TIME_STRING_LEN - 1] = '\0';
62         FIXME("\n");
63     }
64
65     /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
66      * We assume the time is in this format
67      * and divide it into easy to swallow chunks
68      */
69     tmpChar[3]='\0';
70     tmpChar[7]='\0';
71     tmpChar[11]='\0';
72     tmpChar[16]='\0';
73     tmpChar[19]='\0';
74     tmpChar[22]='\0';
75     tmpChar[25]='\0';
76
77     t.tm_year = atoiW(tmpChar+12) - 1900;
78     t.tm_mday = atoiW(tmpChar+5);
79     t.tm_hour = atoiW(tmpChar+17);
80     t.tm_min = atoiW(tmpChar+20);
81     t.tm_sec = atoiW(tmpChar+23);
82
83     /* and month */
84     tmpChar2 = tmpChar + 8;
85     switch(tmpChar2[2])
86     {
87         case 'n':
88             if(tmpChar2[1]=='a')
89                 t.tm_mon = 0;
90             else
91                 t.tm_mon = 5;
92             break;
93         case 'b':
94             t.tm_mon = 1;
95             break;
96         case 'r':
97             if(tmpChar2[1]=='a')
98                 t.tm_mon = 2;
99             else
100                 t.tm_mon = 3;
101             break;
102         case 'y':
103             t.tm_mon = 4;
104             break;
105         case 'l':
106             t.tm_mon = 6;
107             break;
108         case 'g':
109             t.tm_mon = 7;
110             break;
111         case 'p':
112             t.tm_mon = 8;
113             break;
114         case 't':
115             t.tm_mon = 9;
116             break;
117         case 'v':
118             t.tm_mon = 10;
119             break;
120         case 'c':
121             t.tm_mon = 11;
122             break;
123         default:
124             FIXME("\n");
125     }
126
127     return mktime(&t);
128 }
129
130
131 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
132         struct hostent **phe, struct sockaddr_in *psa)
133 {
134     WCHAR *found;
135     char *name;
136     int len, sz;
137
138     TRACE("%s\n", debugstr_w(lpszServerName));
139
140     /* Validate server name first
141      * Check if there is sth. like
142      * pinger.macromedia.com:80
143      * if yes, eliminate the :80....
144      */
145     found = strchrW(lpszServerName, ':');
146     if (found)
147         len = found - lpszServerName;
148     else
149         len = strlenW(lpszServerName);
150
151     sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
152     name = HeapAlloc(GetProcessHeap(), 0, sz+1);
153     WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
154     name[sz] = 0;
155     *phe = gethostbyname(name);
156     HeapFree( GetProcessHeap(), 0, name );
157
158     if (NULL == *phe)
159     {
160         TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName) );
161         return FALSE;
162     }
163
164     memset(psa,0,sizeof(struct sockaddr_in));
165     memcpy((char *)&psa->sin_addr, (*phe)->h_addr, (*phe)->h_length);
166     psa->sin_family = (*phe)->h_addrtype;
167     psa->sin_port = htons((u_short)nServerPort);
168
169     return TRUE;
170 }
171
172 /*
173  * Helper function for sending async Callbacks
174  */
175
176 static const char *get_callback_name(DWORD dwInternetStatus) {
177     static const wininet_flag_info internet_status[] = {
178 #define FE(x) { x, #x }
179         FE(INTERNET_STATUS_RESOLVING_NAME),
180         FE(INTERNET_STATUS_NAME_RESOLVED),
181         FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
182         FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
183         FE(INTERNET_STATUS_SENDING_REQUEST),
184         FE(INTERNET_STATUS_REQUEST_SENT),
185         FE(INTERNET_STATUS_RECEIVING_RESPONSE),
186         FE(INTERNET_STATUS_RESPONSE_RECEIVED),
187         FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
188         FE(INTERNET_STATUS_PREFETCH),
189         FE(INTERNET_STATUS_CLOSING_CONNECTION),
190         FE(INTERNET_STATUS_CONNECTION_CLOSED),
191         FE(INTERNET_STATUS_HANDLE_CREATED),
192         FE(INTERNET_STATUS_HANDLE_CLOSING),
193         FE(INTERNET_STATUS_REQUEST_COMPLETE),
194         FE(INTERNET_STATUS_REDIRECT),
195         FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
196         FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
197         FE(INTERNET_STATUS_STATE_CHANGE),
198         FE(INTERNET_STATUS_COOKIE_SENT),
199         FE(INTERNET_STATUS_COOKIE_RECEIVED),
200         FE(INTERNET_STATUS_PRIVACY_IMPACTED),
201         FE(INTERNET_STATUS_P3P_HEADER),
202         FE(INTERNET_STATUS_P3P_POLICYREF),
203         FE(INTERNET_STATUS_COOKIE_HISTORY),
204         FE(INTERNET_STATE_CONNECTED),
205         FE(INTERNET_STATE_DISCONNECTED),
206         FE(INTERNET_STATE_DISCONNECTED_BY_USER),
207         FE(INTERNET_STATE_IDLE),
208         FE(INTERNET_STATE_BUSY)
209 #undef FE
210     };
211     DWORD i;
212
213     for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
214         if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
215     }
216     return "Unknown";
217 }
218
219 VOID SendSyncCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
220                       DWORD dwInternetStatus, LPVOID lpvStatusInfo,
221                       DWORD dwStatusInfoLength)
222 {
223     HINTERNET hHttpSession;
224     LPVOID lpvNewInfo = NULL;
225
226     if( !hdr->lpfnStatusCB )
227         return;
228
229     /* the IE5 version of wininet does not
230        send callbacks if dwContext is zero */
231     if( !dwContext )
232         return;
233
234     TRACE("--> Callback %ld (%s)\n",dwInternetStatus, get_callback_name(dwInternetStatus));
235
236     hHttpSession = WININET_FindHandle( hdr );
237     if( !hHttpSession )
238         return;
239
240     lpvNewInfo = lpvStatusInfo;
241     if(!(hdr->dwInternalFlags & INET_CALLBACKW)) {
242         switch(dwInternetStatus)
243         {
244         case INTERNET_STATUS_RESOLVING_NAME:
245         case INTERNET_STATUS_REDIRECT:
246             lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
247         }
248     }
249     hdr->lpfnStatusCB(hHttpSession, dwContext, dwInternetStatus,
250                       lpvNewInfo, dwStatusInfoLength);
251     if(lpvNewInfo != lpvStatusInfo)
252         HeapFree(GetProcessHeap(), 0, lpvNewInfo);
253
254     TRACE("<-- Callback %ld (%s)\n",dwInternetStatus, get_callback_name(dwInternetStatus));
255
256     WININET_Release( hdr );
257 }
258
259
260
261 VOID SendAsyncCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
262                        DWORD dwInternetStatus, LPVOID lpvStatusInfo,
263                        DWORD dwStatusInfoLength)
264 {
265         TRACE("Send Callback %ld (%s)\n",dwInternetStatus, get_callback_name(dwInternetStatus));
266
267         if (!(hdr->lpfnStatusCB))
268             return;
269         if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
270         {
271             WORKREQUEST workRequest;
272             struct WORKREQ_SENDCALLBACK *req;
273
274             workRequest.asyncall = SENDCALLBACK;
275             workRequest.hdr = WININET_AddRef( hdr );
276             req = &workRequest.u.SendCallback;
277             req->dwContext = dwContext;
278             req->dwInternetStatus = dwInternetStatus;
279             req->lpvStatusInfo = lpvStatusInfo;
280             req->dwStatusInfoLength = dwStatusInfoLength;
281
282             INTERNET_AsyncCall(&workRequest);
283         }
284         else
285             SendSyncCallback(hdr, dwContext, dwInternetStatus,
286                              lpvStatusInfo, dwStatusInfoLength);
287 }