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 /* critical section to protect non-reentrant gethostbyname() */
44 static CRITICAL_SECTION cs_gethostbyname
;
45 static CRITICAL_SECTION_DEBUG critsect_debug
=
47 0, 0, &cs_gethostbyname
,
48 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
49 0, 0, { (DWORD_PTR
)(__FILE__
": cs_gethostbyname") }
51 static CRITICAL_SECTION cs_gethostbyname
= { &critsect_debug
, -1, 0, 0, 0, 0 };
53 #define TIME_STRING_LEN 30
55 time_t ConvertTimeString(LPCWSTR asctime
)
57 WCHAR tmpChar
[TIME_STRING_LEN
];
60 int timelen
= strlenW(asctime
);
65 /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
66 memset( tmpChar
, 0, sizeof(tmpChar
) );
67 lstrcpynW(tmpChar
, asctime
, TIME_STRING_LEN
);
69 /* Assert that the string is the expected length */
70 if (strlenW(asctime
) >= TIME_STRING_LEN
) FIXME("\n");
72 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
73 * We assume the time is in this format
74 * and divide it into easy to swallow chunks
84 memset( &t
, 0, sizeof(t
) );
85 t
.tm_year
= atoiW(tmpChar
+12) - 1900;
86 t
.tm_mday
= atoiW(tmpChar
+5);
87 t
.tm_hour
= atoiW(tmpChar
+17);
88 t
.tm_min
= atoiW(tmpChar
+20);
89 t
.tm_sec
= atoiW(tmpChar
+23);
92 tmpChar2
= tmpChar
+ 8;
139 BOOL
GetAddress(LPCWSTR lpszServerName
, INTERNET_PORT nServerPort
,
140 struct sockaddr_in
*psa
)
147 TRACE("%s\n", debugstr_w(lpszServerName
));
149 /* Validate server name first
150 * Check if there is sth. like
151 * pinger.macromedia.com:80
152 * if yes, eliminate the :80....
154 found
= strchrW(lpszServerName
, ':');
156 len
= found
- lpszServerName
;
158 len
= strlenW(lpszServerName
);
160 sz
= WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, NULL
, 0, NULL
, NULL
);
161 name
= HeapAlloc(GetProcessHeap(), 0, sz
+1);
162 WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, name
, sz
, NULL
, NULL
);
165 EnterCriticalSection( &cs_gethostbyname
);
166 phe
= gethostbyname(name
);
167 HeapFree( GetProcessHeap(), 0, name
);
171 TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName
) );
172 LeaveCriticalSection( &cs_gethostbyname
);
176 memset(psa
,0,sizeof(struct sockaddr_in
));
177 memcpy((char *)&psa
->sin_addr
, phe
->h_addr
, phe
->h_length
);
178 psa
->sin_family
= phe
->h_addrtype
;
179 psa
->sin_port
= htons(nServerPort
);
181 LeaveCriticalSection( &cs_gethostbyname
);
186 * Helper function for sending async Callbacks
189 static const char *get_callback_name(DWORD dwInternetStatus
) {
190 static const wininet_flag_info internet_status
[] = {
191 #define FE(x) { x, #x }
192 FE(INTERNET_STATUS_RESOLVING_NAME
),
193 FE(INTERNET_STATUS_NAME_RESOLVED
),
194 FE(INTERNET_STATUS_CONNECTING_TO_SERVER
),
195 FE(INTERNET_STATUS_CONNECTED_TO_SERVER
),
196 FE(INTERNET_STATUS_SENDING_REQUEST
),
197 FE(INTERNET_STATUS_REQUEST_SENT
),
198 FE(INTERNET_STATUS_RECEIVING_RESPONSE
),
199 FE(INTERNET_STATUS_RESPONSE_RECEIVED
),
200 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED
),
201 FE(INTERNET_STATUS_PREFETCH
),
202 FE(INTERNET_STATUS_CLOSING_CONNECTION
),
203 FE(INTERNET_STATUS_CONNECTION_CLOSED
),
204 FE(INTERNET_STATUS_HANDLE_CREATED
),
205 FE(INTERNET_STATUS_HANDLE_CLOSING
),
206 FE(INTERNET_STATUS_REQUEST_COMPLETE
),
207 FE(INTERNET_STATUS_REDIRECT
),
208 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE
),
209 FE(INTERNET_STATUS_USER_INPUT_REQUIRED
),
210 FE(INTERNET_STATUS_STATE_CHANGE
),
211 FE(INTERNET_STATUS_COOKIE_SENT
),
212 FE(INTERNET_STATUS_COOKIE_RECEIVED
),
213 FE(INTERNET_STATUS_PRIVACY_IMPACTED
),
214 FE(INTERNET_STATUS_P3P_HEADER
),
215 FE(INTERNET_STATUS_P3P_POLICYREF
),
216 FE(INTERNET_STATUS_COOKIE_HISTORY
)
221 for (i
= 0; i
< (sizeof(internet_status
) / sizeof(internet_status
[0])); i
++) {
222 if (internet_status
[i
].val
== dwInternetStatus
) return internet_status
[i
].name
;
227 VOID
INTERNET_SendCallback(LPWININETHANDLEHEADER hdr
, DWORD_PTR dwContext
,
228 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
229 DWORD dwStatusInfoLength
)
231 LPVOID lpvNewInfo
= NULL
;
233 if( !hdr
->lpfnStatusCB
)
236 /* the IE5 version of wininet does not
237 send callbacks if dwContext is zero */
241 lpvNewInfo
= lpvStatusInfo
;
242 if(hdr
->dwInternalFlags
& INET_CALLBACKW
) {
243 switch(dwInternetStatus
) {
244 case INTERNET_STATUS_NAME_RESOLVED
:
245 case INTERNET_STATUS_CONNECTING_TO_SERVER
:
246 case INTERNET_STATUS_CONNECTED_TO_SERVER
:
247 lpvNewInfo
= WININET_strdup_AtoW(lpvStatusInfo
);
249 case INTERNET_STATUS_RESOLVING_NAME
:
250 case INTERNET_STATUS_REDIRECT
:
251 lpvNewInfo
= WININET_strdupW(lpvStatusInfo
);
255 switch(dwInternetStatus
)
257 case INTERNET_STATUS_NAME_RESOLVED
:
258 case INTERNET_STATUS_CONNECTING_TO_SERVER
:
259 case INTERNET_STATUS_CONNECTED_TO_SERVER
:
260 lpvNewInfo
= HeapAlloc(GetProcessHeap(), 0, strlen(lpvStatusInfo
) + 1);
261 if (lpvNewInfo
) strcpy(lpvNewInfo
, lpvStatusInfo
);
263 case INTERNET_STATUS_RESOLVING_NAME
:
264 case INTERNET_STATUS_REDIRECT
:
265 lpvNewInfo
= WININET_strdup_WtoA(lpvStatusInfo
);
270 TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %p, %d)\n",
271 hdr
->lpfnStatusCB
, hdr
->hInternet
, hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
272 lpvNewInfo
, dwStatusInfoLength
);
274 hdr
->lpfnStatusCB(hdr
->hInternet
, dwContext
, dwInternetStatus
,
275 lpvNewInfo
, dwStatusInfoLength
);
277 TRACE(" end callback().\n");
279 if(lpvNewInfo
!= lpvStatusInfo
)
280 HeapFree(GetProcessHeap(), 0, lpvNewInfo
);
283 static void SendAsyncCallbackProc(WORKREQUEST
*workRequest
)
285 struct WORKREQ_SENDCALLBACK
const *req
= &workRequest
->u
.SendCallback
;
287 TRACE("%p\n", workRequest
->hdr
);
289 INTERNET_SendCallback(workRequest
->hdr
,
290 req
->dwContext
, req
->dwInternetStatus
, req
->lpvStatusInfo
,
291 req
->dwStatusInfoLength
);
293 /* And frees the copy of the status info */
294 HeapFree(GetProcessHeap(), 0, req
->lpvStatusInfo
);
297 VOID
SendAsyncCallback(LPWININETHANDLEHEADER hdr
, DWORD_PTR dwContext
,
298 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
299 DWORD dwStatusInfoLength
)
301 TRACE("(%p, %08lx, %d (%s), %p, %d): %sasync call with callback %p\n",
302 hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
303 lpvStatusInfo
, dwStatusInfoLength
,
304 hdr
->dwFlags
& INTERNET_FLAG_ASYNC
? "" : "non ",
307 if (!(hdr
->lpfnStatusCB
))
310 if (hdr
->dwFlags
& INTERNET_FLAG_ASYNC
)
312 WORKREQUEST workRequest
;
313 struct WORKREQ_SENDCALLBACK
*req
;
314 void *lpvStatusInfo_copy
= lpvStatusInfo
;
318 lpvStatusInfo_copy
= HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength
);
319 memcpy(lpvStatusInfo_copy
, lpvStatusInfo
, dwStatusInfoLength
);
322 workRequest
.asyncproc
= SendAsyncCallbackProc
;
323 workRequest
.hdr
= WININET_AddRef( hdr
);
324 req
= &workRequest
.u
.SendCallback
;
325 req
->dwContext
= dwContext
;
326 req
->dwInternetStatus
= dwInternetStatus
;
327 req
->lpvStatusInfo
= lpvStatusInfo_copy
;
328 req
->dwStatusInfoLength
= dwStatusInfoLength
;
330 INTERNET_AsyncCall(&workRequest
);
333 INTERNET_SendCallback(hdr
, dwContext
, dwInternetStatus
,
334 lpvStatusInfo
, dwStatusInfoLength
);