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 #define TIME_STRING_LEN 30
45 time_t ConvertTimeString(LPCWSTR asctime)
47 WCHAR tmpChar[TIME_STRING_LEN];
50 int timelen = strlenW(asctime);
55 /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
56 memset( tmpChar, 0, sizeof(tmpChar) );
57 lstrcpynW(tmpChar, asctime, TIME_STRING_LEN);
59 /* Assert that the string is the expected length */
60 if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
62 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
63 * We assume the time is in this format
64 * and divide it into easy to swallow chunks
74 t.tm_year = atoiW(tmpChar+12) - 1900;
75 t.tm_mday = atoiW(tmpChar+5);
76 t.tm_hour = atoiW(tmpChar+17);
77 t.tm_min = atoiW(tmpChar+20);
78 t.tm_sec = atoiW(tmpChar+23);
81 tmpChar2 = tmpChar + 8;
128 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
129 struct sockaddr_in *psa)
136 TRACE("%s\n", debugstr_w(lpszServerName));
138 /* Validate server name first
139 * Check if there is sth. like
140 * pinger.macromedia.com:80
141 * if yes, eliminate the :80....
143 found = strchrW(lpszServerName, ':');
145 len = found - lpszServerName;
147 len = strlenW(lpszServerName);
149 sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
150 name = HeapAlloc(GetProcessHeap(), 0, sz+1);
151 WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
153 phe = gethostbyname(name);
154 HeapFree( GetProcessHeap(), 0, name );
158 TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName) );
162 memset(psa,0,sizeof(struct sockaddr_in));
163 memcpy((char *)&psa->sin_addr, phe->h_addr, phe->h_length);
164 psa->sin_family = phe->h_addrtype;
165 psa->sin_port = htons((u_short)nServerPort);
171 * Helper function for sending async Callbacks
174 static const char *get_callback_name(DWORD dwInternetStatus) {
175 static const wininet_flag_info internet_status[] = {
176 #define FE(x) { x, #x }
177 FE(INTERNET_STATUS_RESOLVING_NAME),
178 FE(INTERNET_STATUS_NAME_RESOLVED),
179 FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
180 FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
181 FE(INTERNET_STATUS_SENDING_REQUEST),
182 FE(INTERNET_STATUS_REQUEST_SENT),
183 FE(INTERNET_STATUS_RECEIVING_RESPONSE),
184 FE(INTERNET_STATUS_RESPONSE_RECEIVED),
185 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
186 FE(INTERNET_STATUS_PREFETCH),
187 FE(INTERNET_STATUS_CLOSING_CONNECTION),
188 FE(INTERNET_STATUS_CONNECTION_CLOSED),
189 FE(INTERNET_STATUS_HANDLE_CREATED),
190 FE(INTERNET_STATUS_HANDLE_CLOSING),
191 FE(INTERNET_STATUS_REQUEST_COMPLETE),
192 FE(INTERNET_STATUS_REDIRECT),
193 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
194 FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
195 FE(INTERNET_STATUS_STATE_CHANGE),
196 FE(INTERNET_STATUS_COOKIE_SENT),
197 FE(INTERNET_STATUS_COOKIE_RECEIVED),
198 FE(INTERNET_STATUS_PRIVACY_IMPACTED),
199 FE(INTERNET_STATUS_P3P_HEADER),
200 FE(INTERNET_STATUS_P3P_POLICYREF),
201 FE(INTERNET_STATUS_COOKIE_HISTORY)
206 for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
207 if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
212 VOID INTERNET_SendCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
213 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
214 DWORD dwStatusInfoLength)
216 LPVOID lpvNewInfo = NULL;
218 if( !hdr->lpfnStatusCB )
221 /* the IE5 version of wininet does not
222 send callbacks if dwContext is zero */
226 lpvNewInfo = lpvStatusInfo;
227 if(hdr->dwInternalFlags & INET_CALLBACKW) {
228 switch(dwInternetStatus) {
229 case INTERNET_STATUS_NAME_RESOLVED:
230 case INTERNET_STATUS_CONNECTING_TO_SERVER:
231 case INTERNET_STATUS_CONNECTED_TO_SERVER:
232 lpvNewInfo = WININET_strdup_AtoW(lpvStatusInfo);
234 case INTERNET_STATUS_RESOLVING_NAME:
235 case INTERNET_STATUS_REDIRECT:
236 lpvNewInfo = WININET_strdupW(lpvStatusInfo);
240 switch(dwInternetStatus)
242 case INTERNET_STATUS_NAME_RESOLVED:
243 case INTERNET_STATUS_CONNECTING_TO_SERVER:
244 case INTERNET_STATUS_CONNECTED_TO_SERVER:
245 lpvNewInfo = HeapAlloc(GetProcessHeap(), 0, strlen(lpvStatusInfo) + 1);
246 if (lpvNewInfo) strcpy(lpvNewInfo, lpvStatusInfo);
248 case INTERNET_STATUS_RESOLVING_NAME:
249 case INTERNET_STATUS_REDIRECT:
250 lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
255 TRACE(" callback(%p) (%p (%p), %08x, %d (%s), %p, %d)\n",
256 hdr->lpfnStatusCB, hdr->hInternet, hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
257 lpvNewInfo, dwStatusInfoLength);
259 hdr->lpfnStatusCB(hdr->hInternet, dwContext, dwInternetStatus,
260 lpvNewInfo, dwStatusInfoLength);
262 TRACE(" end callback().\n");
264 if(lpvNewInfo != lpvStatusInfo)
265 HeapFree(GetProcessHeap(), 0, lpvNewInfo);
268 static void SendAsyncCallbackProc(WORKREQUEST *workRequest)
270 struct WORKREQ_SENDCALLBACK const *req = &workRequest->u.SendCallback;
272 TRACE("%p\n", workRequest->hdr);
274 INTERNET_SendCallback(workRequest->hdr,
275 req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
276 req->dwStatusInfoLength);
278 /* And frees the copy of the status info */
279 HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
282 VOID SendAsyncCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
283 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
284 DWORD dwStatusInfoLength)
286 TRACE("(%p, %08x, %d (%s), %p, %d): %sasync call with callback %p\n",
287 hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
288 lpvStatusInfo, dwStatusInfoLength,
289 hdr->dwFlags & INTERNET_FLAG_ASYNC ? "" : "non ",
292 if (!(hdr->lpfnStatusCB))
295 if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
297 WORKREQUEST workRequest;
298 struct WORKREQ_SENDCALLBACK *req;
299 void *lpvStatusInfo_copy = lpvStatusInfo;
303 lpvStatusInfo_copy = HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength);
304 memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
307 workRequest.asyncproc = SendAsyncCallbackProc;
308 workRequest.hdr = WININET_AddRef( hdr );
309 req = &workRequest.u.SendCallback;
310 req->dwContext = dwContext;
311 req->dwInternetStatus = dwInternetStatus;
312 req->lpvStatusInfo = lpvStatusInfo_copy;
313 req->dwStatusInfoLength = dwStatusInfoLength;
315 INTERNET_AsyncCall(&workRequest);
318 INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
319 lpvStatusInfo, dwStatusInfoLength);