shell32: Add explorer toolbar bitmaps.
[wine/hramrach.git] / dlls / wininet / utility.c
blobb68cf8d4a571b364832fa0e9a8c19765166f28c5
1 /*
2 * Wininet - Utility functions
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
7 * Ulrich Czekalla
8 * Aric Stewart
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #if defined(__MINGW32__) || defined (_MSC_VER)
29 #include <ws2tcpip.h>
30 #endif
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wininet.h"
40 #include "winnls.h"
42 #include "wine/debug.h"
43 #include "internet.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
47 #ifndef HAVE_GETADDRINFO
49 /* critical section to protect non-reentrant gethostbyname() */
50 static CRITICAL_SECTION cs_gethostbyname;
51 static CRITICAL_SECTION_DEBUG critsect_debug =
53 0, 0, &cs_gethostbyname,
54 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55 0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
57 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
59 #endif
61 #define TIME_STRING_LEN 30
63 time_t ConvertTimeString(LPCWSTR asctime)
65 WCHAR tmpChar[TIME_STRING_LEN];
66 WCHAR *tmpChar2;
67 struct tm t;
68 int timelen = strlenW(asctime);
70 if(!timelen)
71 return 0;
73 /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
74 memset( tmpChar, 0, sizeof(tmpChar) );
75 lstrcpynW(tmpChar, asctime, TIME_STRING_LEN);
77 /* Assert that the string is the expected length */
78 if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
80 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
81 * We assume the time is in this format
82 * and divide it into easy to swallow chunks
84 tmpChar[3]='\0';
85 tmpChar[7]='\0';
86 tmpChar[11]='\0';
87 tmpChar[16]='\0';
88 tmpChar[19]='\0';
89 tmpChar[22]='\0';
90 tmpChar[25]='\0';
92 memset( &t, 0, sizeof(t) );
93 t.tm_year = atoiW(tmpChar+12) - 1900;
94 t.tm_mday = atoiW(tmpChar+5);
95 t.tm_hour = atoiW(tmpChar+17);
96 t.tm_min = atoiW(tmpChar+20);
97 t.tm_sec = atoiW(tmpChar+23);
99 /* and month */
100 tmpChar2 = tmpChar + 8;
101 switch(tmpChar2[2])
103 case 'n':
104 if(tmpChar2[1]=='a')
105 t.tm_mon = 0;
106 else
107 t.tm_mon = 5;
108 break;
109 case 'b':
110 t.tm_mon = 1;
111 break;
112 case 'r':
113 if(tmpChar2[1]=='a')
114 t.tm_mon = 2;
115 else
116 t.tm_mon = 3;
117 break;
118 case 'y':
119 t.tm_mon = 4;
120 break;
121 case 'l':
122 t.tm_mon = 6;
123 break;
124 case 'g':
125 t.tm_mon = 7;
126 break;
127 case 'p':
128 t.tm_mon = 8;
129 break;
130 case 't':
131 t.tm_mon = 9;
132 break;
133 case 'v':
134 t.tm_mon = 10;
135 break;
136 case 'c':
137 t.tm_mon = 11;
138 break;
139 default:
140 FIXME("\n");
143 return mktime(&t);
147 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
148 struct sockaddr_in *psa)
150 WCHAR *found;
151 char *name;
152 int len, sz;
153 #ifdef HAVE_GETADDRINFO
154 struct addrinfo *res, hints;
155 int ret;
156 #else
157 struct hostent *phe;
158 #endif
160 TRACE("%s\n", debugstr_w(lpszServerName));
162 /* Validate server name first
163 * Check if there is sth. like
164 * pinger.macromedia.com:80
165 * if yes, eliminate the :80....
167 found = strchrW(lpszServerName, ':');
168 if (found)
169 len = found - lpszServerName;
170 else
171 len = strlenW(lpszServerName);
173 sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
174 if (!(name = HeapAlloc( GetProcessHeap(), 0, sz + 1 ))) return FALSE;
175 WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
176 name[sz] = 0;
178 #ifdef HAVE_GETADDRINFO
179 memset( &hints, 0, sizeof(struct addrinfo) );
180 hints.ai_family = AF_INET;
182 ret = getaddrinfo( name, NULL, &hints, &res );
183 HeapFree( GetProcessHeap(), 0, name );
184 if (ret != 0)
186 TRACE("failed to get address of %s (%s)\n", debugstr_w(lpszServerName), gai_strerror(ret));
187 return FALSE;
189 memset( psa, 0, sizeof(struct sockaddr_in) );
190 memcpy( &psa->sin_addr, &((struct sockaddr_in *)res->ai_addr)->sin_addr, sizeof(struct in_addr) );
191 psa->sin_family = res->ai_family;
192 psa->sin_port = htons(nServerPort);
194 freeaddrinfo( res );
195 #else
196 EnterCriticalSection( &cs_gethostbyname );
197 phe = gethostbyname(name);
198 HeapFree( GetProcessHeap(), 0, name );
200 if (NULL == phe)
202 TRACE("failed to get address of %s (%d)\n", debugstr_w(lpszServerName), h_errno);
203 LeaveCriticalSection( &cs_gethostbyname );
204 return FALSE;
206 memset(psa,0,sizeof(struct sockaddr_in));
207 memcpy((char *)&psa->sin_addr, phe->h_addr, phe->h_length);
208 psa->sin_family = phe->h_addrtype;
209 psa->sin_port = htons(nServerPort);
211 LeaveCriticalSection( &cs_gethostbyname );
212 #endif
213 return TRUE;
217 * Helper function for sending async Callbacks
220 static const char *get_callback_name(DWORD dwInternetStatus) {
221 static const wininet_flag_info internet_status[] = {
222 #define FE(x) { x, #x }
223 FE(INTERNET_STATUS_RESOLVING_NAME),
224 FE(INTERNET_STATUS_NAME_RESOLVED),
225 FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
226 FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
227 FE(INTERNET_STATUS_SENDING_REQUEST),
228 FE(INTERNET_STATUS_REQUEST_SENT),
229 FE(INTERNET_STATUS_RECEIVING_RESPONSE),
230 FE(INTERNET_STATUS_RESPONSE_RECEIVED),
231 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
232 FE(INTERNET_STATUS_PREFETCH),
233 FE(INTERNET_STATUS_CLOSING_CONNECTION),
234 FE(INTERNET_STATUS_CONNECTION_CLOSED),
235 FE(INTERNET_STATUS_HANDLE_CREATED),
236 FE(INTERNET_STATUS_HANDLE_CLOSING),
237 FE(INTERNET_STATUS_REQUEST_COMPLETE),
238 FE(INTERNET_STATUS_REDIRECT),
239 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
240 FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
241 FE(INTERNET_STATUS_STATE_CHANGE),
242 FE(INTERNET_STATUS_COOKIE_SENT),
243 FE(INTERNET_STATUS_COOKIE_RECEIVED),
244 FE(INTERNET_STATUS_PRIVACY_IMPACTED),
245 FE(INTERNET_STATUS_P3P_HEADER),
246 FE(INTERNET_STATUS_P3P_POLICYREF),
247 FE(INTERNET_STATUS_COOKIE_HISTORY)
248 #undef FE
250 DWORD i;
252 for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
253 if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
255 return "Unknown";
258 VOID INTERNET_SendCallback(LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext,
259 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
260 DWORD dwStatusInfoLength)
262 LPVOID lpvNewInfo = NULL;
264 if( !hdr->lpfnStatusCB )
265 return;
267 /* the IE5 version of wininet does not
268 send callbacks if dwContext is zero */
269 if( !dwContext )
270 return;
272 lpvNewInfo = lpvStatusInfo;
273 if(hdr->dwInternalFlags & INET_CALLBACKW) {
274 switch(dwInternetStatus) {
275 case INTERNET_STATUS_NAME_RESOLVED:
276 case INTERNET_STATUS_CONNECTING_TO_SERVER:
277 case INTERNET_STATUS_CONNECTED_TO_SERVER:
278 lpvNewInfo = WININET_strdup_AtoW(lpvStatusInfo);
279 break;
280 case INTERNET_STATUS_RESOLVING_NAME:
281 case INTERNET_STATUS_REDIRECT:
282 lpvNewInfo = WININET_strdupW(lpvStatusInfo);
283 break;
285 }else {
286 switch(dwInternetStatus)
288 case INTERNET_STATUS_NAME_RESOLVED:
289 case INTERNET_STATUS_CONNECTING_TO_SERVER:
290 case INTERNET_STATUS_CONNECTED_TO_SERVER:
291 lpvNewInfo = HeapAlloc(GetProcessHeap(), 0, strlen(lpvStatusInfo) + 1);
292 if (lpvNewInfo) strcpy(lpvNewInfo, lpvStatusInfo);
293 break;
294 case INTERNET_STATUS_RESOLVING_NAME:
295 case INTERNET_STATUS_REDIRECT:
296 lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
297 break;
301 TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %p, %d)\n",
302 hdr->lpfnStatusCB, hdr->hInternet, hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
303 lpvNewInfo, dwStatusInfoLength);
305 hdr->lpfnStatusCB(hdr->hInternet, dwContext, dwInternetStatus,
306 lpvNewInfo, dwStatusInfoLength);
308 TRACE(" end callback().\n");
310 if(lpvNewInfo != lpvStatusInfo)
311 HeapFree(GetProcessHeap(), 0, lpvNewInfo);
314 static void SendAsyncCallbackProc(WORKREQUEST *workRequest)
316 struct WORKREQ_SENDCALLBACK const *req = &workRequest->u.SendCallback;
318 TRACE("%p\n", workRequest->hdr);
320 INTERNET_SendCallback(workRequest->hdr,
321 req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
322 req->dwStatusInfoLength);
324 /* And frees the copy of the status info */
325 HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
328 VOID SendAsyncCallback(LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext,
329 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
330 DWORD dwStatusInfoLength)
332 TRACE("(%p, %08lx, %d (%s), %p, %d): %sasync call with callback %p\n",
333 hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
334 lpvStatusInfo, dwStatusInfoLength,
335 hdr->dwFlags & INTERNET_FLAG_ASYNC ? "" : "non ",
336 hdr->lpfnStatusCB);
338 if (!(hdr->lpfnStatusCB))
339 return;
341 if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
343 WORKREQUEST workRequest;
344 struct WORKREQ_SENDCALLBACK *req;
345 void *lpvStatusInfo_copy = lpvStatusInfo;
347 if (lpvStatusInfo)
349 lpvStatusInfo_copy = HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength);
350 memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
353 workRequest.asyncproc = SendAsyncCallbackProc;
354 workRequest.hdr = WININET_AddRef( hdr );
355 req = &workRequest.u.SendCallback;
356 req->dwContext = dwContext;
357 req->dwInternetStatus = dwInternetStatus;
358 req->lpvStatusInfo = lpvStatusInfo_copy;
359 req->dwStatusInfoLength = dwStatusInfoLength;
361 INTERNET_AsyncCall(&workRequest);
363 else
364 INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
365 lpvStatusInfo, dwStatusInfoLength);