2 * Wininet - Utility functions
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
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
26 #include "wine/port.h"
38 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
43 #ifndef HAVE_GETADDRINFO
45 /* critical section to protect non-reentrant gethostbyname() */
46 static CRITICAL_SECTION cs_gethostbyname;
47 static CRITICAL_SECTION_DEBUG critsect_debug =
49 0, 0, &cs_gethostbyname,
50 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
51 0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
53 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
57 #define TIME_STRING_LEN 30
59 time_t ConvertTimeString(LPCWSTR asctime)
61 WCHAR tmpChar[TIME_STRING_LEN];
64 int timelen = strlenW(asctime);
69 /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
70 memset( tmpChar, 0, sizeof(tmpChar) );
71 lstrcpynW(tmpChar, asctime, TIME_STRING_LEN);
73 /* Assert that the string is the expected length */
74 if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
76 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
77 * We assume the time is in this format
78 * and divide it into easy to swallow chunks
88 memset( &t, 0, sizeof(t) );
89 t.tm_year = atoiW(tmpChar+12) - 1900;
90 t.tm_mday = atoiW(tmpChar+5);
91 t.tm_hour = atoiW(tmpChar+17);
92 t.tm_min = atoiW(tmpChar+20);
93 t.tm_sec = atoiW(tmpChar+23);
96 tmpChar2 = tmpChar + 8;
143 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
144 struct sockaddr_in *psa)
149 #ifdef HAVE_GETADDRINFO
150 struct addrinfo *res, hints;
156 TRACE("%s\n", debugstr_w(lpszServerName));
158 /* Validate server name first
159 * Check if there is sth. like
160 * pinger.macromedia.com:80
161 * if yes, eliminate the :80....
163 found = strchrW(lpszServerName, ':');
165 len = found - lpszServerName;
167 len = strlenW(lpszServerName);
169 sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
170 if (!(name = HeapAlloc( GetProcessHeap(), 0, sz + 1 ))) return FALSE;
171 WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
174 #ifdef HAVE_GETADDRINFO
175 memset( &hints, 0, sizeof(struct addrinfo) );
176 hints.ai_family = AF_INET;
178 ret = getaddrinfo( name, NULL, &hints, &res );
179 HeapFree( GetProcessHeap(), 0, name );
182 TRACE("failed to get address of %s (%s)\n", debugstr_w(lpszServerName), gai_strerror(ret));
185 memset( psa, 0, sizeof(struct sockaddr_in) );
186 memcpy( &psa->sin_addr, &((struct sockaddr_in *)res->ai_addr)->sin_addr, sizeof(struct in_addr) );
187 psa->sin_family = res->ai_family;
188 psa->sin_port = htons(nServerPort);
192 EnterCriticalSection( &cs_gethostbyname );
193 phe = gethostbyname(name);
194 HeapFree( GetProcessHeap(), 0, name );
198 TRACE("failed to get address of %s (%d)\n", debugstr_w(lpszServerName), h_errno);
199 LeaveCriticalSection( &cs_gethostbyname );
202 memset(psa,0,sizeof(struct sockaddr_in));
203 memcpy((char *)&psa->sin_addr, phe->h_addr, phe->h_length);
204 psa->sin_family = phe->h_addrtype;
205 psa->sin_port = htons(nServerPort);
207 LeaveCriticalSection( &cs_gethostbyname );
213 * Helper function for sending async Callbacks
216 static const char *get_callback_name(DWORD dwInternetStatus) {
217 static const wininet_flag_info internet_status[] = {
218 #define FE(x) { x, #x }
219 FE(INTERNET_STATUS_RESOLVING_NAME),
220 FE(INTERNET_STATUS_NAME_RESOLVED),
221 FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
222 FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
223 FE(INTERNET_STATUS_SENDING_REQUEST),
224 FE(INTERNET_STATUS_REQUEST_SENT),
225 FE(INTERNET_STATUS_RECEIVING_RESPONSE),
226 FE(INTERNET_STATUS_RESPONSE_RECEIVED),
227 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
228 FE(INTERNET_STATUS_PREFETCH),
229 FE(INTERNET_STATUS_CLOSING_CONNECTION),
230 FE(INTERNET_STATUS_CONNECTION_CLOSED),
231 FE(INTERNET_STATUS_HANDLE_CREATED),
232 FE(INTERNET_STATUS_HANDLE_CLOSING),
233 FE(INTERNET_STATUS_REQUEST_COMPLETE),
234 FE(INTERNET_STATUS_REDIRECT),
235 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
236 FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
237 FE(INTERNET_STATUS_STATE_CHANGE),
238 FE(INTERNET_STATUS_COOKIE_SENT),
239 FE(INTERNET_STATUS_COOKIE_RECEIVED),
240 FE(INTERNET_STATUS_PRIVACY_IMPACTED),
241 FE(INTERNET_STATUS_P3P_HEADER),
242 FE(INTERNET_STATUS_P3P_POLICYREF),
243 FE(INTERNET_STATUS_COOKIE_HISTORY)
248 for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
249 if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
254 VOID INTERNET_SendCallback(LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext,
255 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
256 DWORD dwStatusInfoLength)
258 LPVOID lpvNewInfo = NULL;
260 if( !hdr->lpfnStatusCB )
263 /* the IE5 version of wininet does not
264 send callbacks if dwContext is zero */
268 lpvNewInfo = lpvStatusInfo;
269 if(hdr->dwInternalFlags & INET_CALLBACKW) {
270 switch(dwInternetStatus) {
271 case INTERNET_STATUS_NAME_RESOLVED:
272 case INTERNET_STATUS_CONNECTING_TO_SERVER:
273 case INTERNET_STATUS_CONNECTED_TO_SERVER:
274 lpvNewInfo = WININET_strdup_AtoW(lpvStatusInfo);
276 case INTERNET_STATUS_RESOLVING_NAME:
277 case INTERNET_STATUS_REDIRECT:
278 lpvNewInfo = WININET_strdupW(lpvStatusInfo);
282 switch(dwInternetStatus)
284 case INTERNET_STATUS_NAME_RESOLVED:
285 case INTERNET_STATUS_CONNECTING_TO_SERVER:
286 case INTERNET_STATUS_CONNECTED_TO_SERVER:
287 lpvNewInfo = HeapAlloc(GetProcessHeap(), 0, strlen(lpvStatusInfo) + 1);
288 if (lpvNewInfo) strcpy(lpvNewInfo, lpvStatusInfo);
290 case INTERNET_STATUS_RESOLVING_NAME:
291 case INTERNET_STATUS_REDIRECT:
292 lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
297 TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %p, %d)\n",
298 hdr->lpfnStatusCB, hdr->hInternet, hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
299 lpvNewInfo, dwStatusInfoLength);
301 hdr->lpfnStatusCB(hdr->hInternet, dwContext, dwInternetStatus,
302 lpvNewInfo, dwStatusInfoLength);
304 TRACE(" end callback().\n");
306 if(lpvNewInfo != lpvStatusInfo)
307 HeapFree(GetProcessHeap(), 0, lpvNewInfo);
310 static void SendAsyncCallbackProc(WORKREQUEST *workRequest)
312 struct WORKREQ_SENDCALLBACK const *req = &workRequest->u.SendCallback;
314 TRACE("%p\n", workRequest->hdr);
316 INTERNET_SendCallback(workRequest->hdr,
317 req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
318 req->dwStatusInfoLength);
320 /* And frees the copy of the status info */
321 HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
324 VOID SendAsyncCallback(LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext,
325 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
326 DWORD dwStatusInfoLength)
328 TRACE("(%p, %08lx, %d (%s), %p, %d): %sasync call with callback %p\n",
329 hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
330 lpvStatusInfo, dwStatusInfoLength,
331 hdr->dwFlags & INTERNET_FLAG_ASYNC ? "" : "non ",
334 if (!(hdr->lpfnStatusCB))
337 if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
339 WORKREQUEST workRequest;
340 struct WORKREQ_SENDCALLBACK *req;
341 void *lpvStatusInfo_copy = lpvStatusInfo;
345 lpvStatusInfo_copy = HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength);
346 memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
349 workRequest.asyncproc = SendAsyncCallbackProc;
350 workRequest.hdr = WININET_AddRef( hdr );
351 req = &workRequest.u.SendCallback;
352 req->dwContext = dwContext;
353 req->dwInternetStatus = dwInternetStatus;
354 req->lpvStatusInfo = lpvStatusInfo_copy;
355 req->dwStatusInfoLength = dwStatusInfoLength;
357 INTERNET_AsyncCall(&workRequest);
360 INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
361 lpvStatusInfo, dwStatusInfoLength);