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"
39 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
44 #define TIME_STRING_LEN 30
46 time_t ConvertTimeString(LPCWSTR asctime
)
48 WCHAR tmpChar
[TIME_STRING_LEN
];
51 int timelen
= strlenW(asctime
);
56 /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
57 memset( tmpChar
, 0, sizeof(tmpChar
) );
58 lstrcpynW(tmpChar
, asctime
, TIME_STRING_LEN
);
60 /* Assert that the string is the expected length */
61 if (strlenW(asctime
) >= TIME_STRING_LEN
) FIXME("\n");
63 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
64 * We assume the time is in this format
65 * and divide it into easy to swallow chunks
75 t
.tm_year
= atoiW(tmpChar
+12) - 1900;
76 t
.tm_mday
= atoiW(tmpChar
+5);
77 t
.tm_hour
= atoiW(tmpChar
+17);
78 t
.tm_min
= atoiW(tmpChar
+20);
79 t
.tm_sec
= atoiW(tmpChar
+23);
82 tmpChar2
= tmpChar
+ 8;
129 BOOL
GetAddress(LPCWSTR lpszServerName
, INTERNET_PORT nServerPort
,
130 struct sockaddr_in
*psa
)
137 TRACE("%s\n", debugstr_w(lpszServerName
));
139 /* Validate server name first
140 * Check if there is sth. like
141 * pinger.macromedia.com:80
142 * if yes, eliminate the :80....
144 found
= strchrW(lpszServerName
, ':');
146 len
= found
- lpszServerName
;
148 len
= strlenW(lpszServerName
);
150 sz
= WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, NULL
, 0, NULL
, NULL
);
151 name
= HeapAlloc(GetProcessHeap(), 0, sz
+1);
152 WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, name
, sz
, NULL
, NULL
);
154 phe
= gethostbyname(name
);
155 HeapFree( GetProcessHeap(), 0, name
);
159 TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName
) );
163 memset(psa
,0,sizeof(struct sockaddr_in
));
164 memcpy((char *)&psa
->sin_addr
, phe
->h_addr
, phe
->h_length
);
165 psa
->sin_family
= phe
->h_addrtype
;
166 psa
->sin_port
= htons((u_short
)nServerPort
);
172 * Helper function for sending async Callbacks
175 static const char *get_callback_name(DWORD dwInternetStatus
) {
176 static const wininet_flag_info internet_status
[] = {
177 #define FE(x) { x, #x }
178 FE(INTERNET_STATUS_RESOLVING_NAME
),
179 FE(INTERNET_STATUS_NAME_RESOLVED
),
180 FE(INTERNET_STATUS_CONNECTING_TO_SERVER
),
181 FE(INTERNET_STATUS_CONNECTED_TO_SERVER
),
182 FE(INTERNET_STATUS_SENDING_REQUEST
),
183 FE(INTERNET_STATUS_REQUEST_SENT
),
184 FE(INTERNET_STATUS_RECEIVING_RESPONSE
),
185 FE(INTERNET_STATUS_RESPONSE_RECEIVED
),
186 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED
),
187 FE(INTERNET_STATUS_PREFETCH
),
188 FE(INTERNET_STATUS_CLOSING_CONNECTION
),
189 FE(INTERNET_STATUS_CONNECTION_CLOSED
),
190 FE(INTERNET_STATUS_HANDLE_CREATED
),
191 FE(INTERNET_STATUS_HANDLE_CLOSING
),
192 FE(INTERNET_STATUS_REQUEST_COMPLETE
),
193 FE(INTERNET_STATUS_REDIRECT
),
194 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE
),
195 FE(INTERNET_STATUS_USER_INPUT_REQUIRED
),
196 FE(INTERNET_STATUS_STATE_CHANGE
),
197 FE(INTERNET_STATUS_COOKIE_SENT
),
198 FE(INTERNET_STATUS_COOKIE_RECEIVED
),
199 FE(INTERNET_STATUS_PRIVACY_IMPACTED
),
200 FE(INTERNET_STATUS_P3P_HEADER
),
201 FE(INTERNET_STATUS_P3P_POLICYREF
),
202 FE(INTERNET_STATUS_COOKIE_HISTORY
)
207 for (i
= 0; i
< (sizeof(internet_status
) / sizeof(internet_status
[0])); i
++) {
208 if (internet_status
[i
].val
== dwInternetStatus
) return internet_status
[i
].name
;
213 VOID
INTERNET_SendCallback(LPWININETHANDLEHEADER hdr
, DWORD dwContext
,
214 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
215 DWORD dwStatusInfoLength
)
217 LPVOID lpvNewInfo
= NULL
;
219 if( !hdr
->lpfnStatusCB
)
222 /* the IE5 version of wininet does not
223 send callbacks if dwContext is zero */
227 lpvNewInfo
= lpvStatusInfo
;
228 if(hdr
->dwInternalFlags
& INET_CALLBACKW
) {
229 switch(dwInternetStatus
) {
230 case INTERNET_STATUS_NAME_RESOLVED
:
231 case INTERNET_STATUS_CONNECTING_TO_SERVER
:
232 case INTERNET_STATUS_CONNECTED_TO_SERVER
:
233 lpvNewInfo
= WININET_strdup_AtoW(lpvStatusInfo
);
235 case INTERNET_STATUS_RESOLVING_NAME
:
236 case INTERNET_STATUS_REDIRECT
:
237 lpvNewInfo
= WININET_strdupW(lpvStatusInfo
);
241 switch(dwInternetStatus
)
243 case INTERNET_STATUS_NAME_RESOLVED
:
244 case INTERNET_STATUS_CONNECTING_TO_SERVER
:
245 case INTERNET_STATUS_CONNECTED_TO_SERVER
:
246 lpvNewInfo
= HeapAlloc(GetProcessHeap(), 0, strlen(lpvStatusInfo
) + 1);
247 if (lpvNewInfo
) strcpy(lpvNewInfo
, lpvStatusInfo
);
249 case INTERNET_STATUS_RESOLVING_NAME
:
250 case INTERNET_STATUS_REDIRECT
:
251 lpvNewInfo
= WININET_strdup_WtoA(lpvStatusInfo
);
256 TRACE(" callback(%p) (%p (%p), %08x, %d (%s), %p, %d)\n",
257 hdr
->lpfnStatusCB
, hdr
->hInternet
, hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
258 lpvNewInfo
, dwStatusInfoLength
);
260 hdr
->lpfnStatusCB(hdr
->hInternet
, dwContext
, dwInternetStatus
,
261 lpvNewInfo
, dwStatusInfoLength
);
263 TRACE(" end callback().\n");
265 if(lpvNewInfo
!= lpvStatusInfo
)
266 HeapFree(GetProcessHeap(), 0, lpvNewInfo
);
269 static void SendAsyncCallbackProc(WORKREQUEST
*workRequest
)
271 struct WORKREQ_SENDCALLBACK
const *req
= &workRequest
->u
.SendCallback
;
273 TRACE("%p\n", workRequest
->hdr
);
275 INTERNET_SendCallback(workRequest
->hdr
,
276 req
->dwContext
, req
->dwInternetStatus
, req
->lpvStatusInfo
,
277 req
->dwStatusInfoLength
);
279 /* And frees the copy of the status info */
280 HeapFree(GetProcessHeap(), 0, req
->lpvStatusInfo
);
283 VOID
SendAsyncCallback(LPWININETHANDLEHEADER hdr
, DWORD dwContext
,
284 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
285 DWORD dwStatusInfoLength
)
287 TRACE("(%p, %08x, %d (%s), %p, %d): %sasync call with callback %p\n",
288 hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
289 lpvStatusInfo
, dwStatusInfoLength
,
290 hdr
->dwFlags
& INTERNET_FLAG_ASYNC
? "" : "non ",
293 if (!(hdr
->lpfnStatusCB
))
296 if (hdr
->dwFlags
& INTERNET_FLAG_ASYNC
)
298 WORKREQUEST workRequest
;
299 struct WORKREQ_SENDCALLBACK
*req
;
300 void *lpvStatusInfo_copy
= lpvStatusInfo
;
304 lpvStatusInfo_copy
= HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength
);
305 memcpy(lpvStatusInfo_copy
, lpvStatusInfo
, dwStatusInfoLength
);
308 workRequest
.asyncproc
= SendAsyncCallbackProc
;
309 workRequest
.hdr
= WININET_AddRef( hdr
);
310 req
= &workRequest
.u
.SendCallback
;
311 req
->dwContext
= dwContext
;
312 req
->dwInternetStatus
= dwInternetStatus
;
313 req
->lpvStatusInfo
= lpvStatusInfo_copy
;
314 req
->dwStatusInfoLength
= dwStatusInfoLength
;
316 INTERNET_AsyncCall(&workRequest
);
319 INTERNET_SendCallback(hdr
, dwContext
, dwInternetStatus
,
320 lpvStatusInfo
, dwStatusInfoLength
);