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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
39 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
44 #define TIME_STRING_LEN 30
47 time_t ConvertTimeString(LPCWSTR asctime
)
49 WCHAR tmpChar
[TIME_STRING_LEN
];
52 int timelen
= strlenW(asctime
);
54 if(!asctime
|| !timelen
)
57 /* The atoiWs below relie on that tmpChar is \0 padded? */
58 strncpyW(tmpChar
, asctime
, TIME_STRING_LEN
);
60 /* Assert that the string is the expected length */
61 if (tmpChar
[TIME_STRING_LEN
- 1] != '\0')
63 tmpChar
[TIME_STRING_LEN
- 1] = '\0';
67 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
68 * We assume the time is in this format
69 * and divide it into easy to swallow chunks
79 t
.tm_year
= atoiW(tmpChar
+12) - 1900;
80 t
.tm_mday
= atoiW(tmpChar
+5);
81 t
.tm_hour
= atoiW(tmpChar
+17);
82 t
.tm_min
= atoiW(tmpChar
+20);
83 t
.tm_sec
= atoiW(tmpChar
+23);
86 tmpChar2
= tmpChar
+ 8;
133 BOOL
GetAddress(LPCWSTR lpszServerName
, INTERNET_PORT nServerPort
,
134 struct hostent
**phe
, struct sockaddr_in
*psa
)
140 TRACE("%s\n", debugstr_w(lpszServerName
));
142 /* Validate server name first
143 * Check if there is sth. like
144 * pinger.macromedia.com:80
145 * if yes, eliminate the :80....
147 found
= strchrW(lpszServerName
, ':');
149 len
= found
- lpszServerName
;
151 len
= strlenW(lpszServerName
);
153 sz
= WideCharToMultiByte( CP_THREAD_ACP
, 0, lpszServerName
, len
, NULL
, 0, NULL
, NULL
);
154 name
= HeapAlloc(GetProcessHeap(), 0, sz
+1);
155 WideCharToMultiByte( CP_THREAD_ACP
, 0, lpszServerName
, len
, name
, sz
, NULL
, NULL
);
157 *phe
= gethostbyname(name
);
158 HeapFree( GetProcessHeap(), 0, name
);
162 TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName
) );
166 memset(psa
,0,sizeof(struct sockaddr_in
));
167 memcpy((char *)&psa
->sin_addr
, (*phe
)->h_addr
, (*phe
)->h_length
);
168 psa
->sin_family
= (*phe
)->h_addrtype
;
169 psa
->sin_port
= htons((u_short
)nServerPort
);
175 * Helper function for sending async Callbacks
178 static const char *get_callback_name(DWORD dwInternetStatus
) {
179 static const wininet_flag_info internet_status
[] = {
180 #define FE(x) { x, #x }
181 FE(INTERNET_STATUS_RESOLVING_NAME
),
182 FE(INTERNET_STATUS_NAME_RESOLVED
),
183 FE(INTERNET_STATUS_CONNECTING_TO_SERVER
),
184 FE(INTERNET_STATUS_CONNECTED_TO_SERVER
),
185 FE(INTERNET_STATUS_SENDING_REQUEST
),
186 FE(INTERNET_STATUS_REQUEST_SENT
),
187 FE(INTERNET_STATUS_RECEIVING_RESPONSE
),
188 FE(INTERNET_STATUS_RESPONSE_RECEIVED
),
189 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED
),
190 FE(INTERNET_STATUS_PREFETCH
),
191 FE(INTERNET_STATUS_CLOSING_CONNECTION
),
192 FE(INTERNET_STATUS_CONNECTION_CLOSED
),
193 FE(INTERNET_STATUS_HANDLE_CREATED
),
194 FE(INTERNET_STATUS_HANDLE_CLOSING
),
195 FE(INTERNET_STATUS_REQUEST_COMPLETE
),
196 FE(INTERNET_STATUS_REDIRECT
),
197 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE
),
198 FE(INTERNET_STATUS_USER_INPUT_REQUIRED
),
199 FE(INTERNET_STATUS_STATE_CHANGE
),
200 FE(INTERNET_STATUS_COOKIE_SENT
),
201 FE(INTERNET_STATUS_COOKIE_RECEIVED
),
202 FE(INTERNET_STATUS_PRIVACY_IMPACTED
),
203 FE(INTERNET_STATUS_P3P_HEADER
),
204 FE(INTERNET_STATUS_P3P_POLICYREF
),
205 FE(INTERNET_STATUS_COOKIE_HISTORY
)
210 for (i
= 0; i
< (sizeof(internet_status
) / sizeof(internet_status
[0])); i
++) {
211 if (internet_status
[i
].val
== dwInternetStatus
) return internet_status
[i
].name
;
216 VOID
SendSyncCallback(LPWININETHANDLEHEADER hdr
, DWORD dwContext
,
217 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
218 DWORD dwStatusInfoLength
)
220 HINTERNET hHttpSession
;
221 LPVOID lpvNewInfo
= NULL
;
223 if( !hdr
->lpfnStatusCB
)
226 /* the IE5 version of wininet does not
227 send callbacks if dwContext is zero */
231 hHttpSession
= WININET_FindHandle( hdr
);
232 if( !hHttpSession
) {
233 TRACE(" Could not convert header '%p' into a handle !\n", hdr
);
237 lpvNewInfo
= lpvStatusInfo
;
238 if(!(hdr
->dwInternalFlags
& INET_CALLBACKW
)) {
239 switch(dwInternetStatus
)
241 case INTERNET_STATUS_RESOLVING_NAME
:
242 case INTERNET_STATUS_REDIRECT
:
243 lpvNewInfo
= WININET_strdup_WtoA(lpvStatusInfo
);
247 TRACE(" callback(%p) (%08lx (%p), %08lx, %ld (%s), %p, %ld)\n",
248 hdr
->lpfnStatusCB
, (DWORD
) hHttpSession
, hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
249 lpvNewInfo
, dwStatusInfoLength
);
251 hdr
->lpfnStatusCB(hHttpSession
, dwContext
, dwInternetStatus
,
252 lpvNewInfo
, dwStatusInfoLength
);
254 TRACE(" end callback().\n");
256 if(lpvNewInfo
!= lpvStatusInfo
)
257 HeapFree(GetProcessHeap(), 0, lpvNewInfo
);
259 WININET_Release( hdr
);
264 VOID
SendAsyncCallback(LPWININETHANDLEHEADER hdr
, DWORD dwContext
,
265 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
266 DWORD dwStatusInfoLength
)
268 TRACE("(%p, %08lx, %ld (%s), %p, %ld): %sasync call with callback %p\n",
269 hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
270 lpvStatusInfo
, dwStatusInfoLength
,
271 hdr
->dwFlags
& INTERNET_FLAG_ASYNC
? "" : "non ",
274 if (!(hdr
->lpfnStatusCB
))
277 if (hdr
->dwFlags
& INTERNET_FLAG_ASYNC
)
279 WORKREQUEST workRequest
;
280 struct WORKREQ_SENDCALLBACK
*req
;
281 void *lpvStatusInfo_copy
= lpvStatusInfo
;
285 lpvStatusInfo_copy
= HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength
);
286 memcpy(lpvStatusInfo_copy
, lpvStatusInfo
, dwStatusInfoLength
);
289 workRequest
.asyncall
= SENDCALLBACK
;
290 workRequest
.hdr
= WININET_AddRef( hdr
);
291 req
= &workRequest
.u
.SendCallback
;
292 req
->dwContext
= dwContext
;
293 req
->dwInternetStatus
= dwInternetStatus
;
294 req
->lpvStatusInfo
= lpvStatusInfo_copy
;
295 req
->dwStatusInfoLength
= dwStatusInfoLength
;
297 INTERNET_AsyncCall(&workRequest
);
300 SendSyncCallback(hdr
, dwContext
, dwInternetStatus
,
301 lpvStatusInfo
, dwStatusInfoLength
);