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
46 time_t ConvertTimeString(LPCWSTR asctime
)
48 WCHAR tmpChar
[TIME_STRING_LEN
];
51 int timelen
= strlenW(asctime
);
53 if(!asctime
|| !timelen
)
56 strncpyW(tmpChar
, asctime
, TIME_STRING_LEN
);
58 /* Assert that the string is the expected length */
59 if (tmpChar
[TIME_STRING_LEN
- 1] != '\0')
61 tmpChar
[TIME_STRING_LEN
- 1] = '\0';
65 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
66 * We assume the time is in this format
67 * and divide it into easy to swallow chunks
77 t
.tm_year
= atoiW(tmpChar
+12) - 1900;
78 t
.tm_mday
= atoiW(tmpChar
+5);
79 t
.tm_hour
= atoiW(tmpChar
+17);
80 t
.tm_min
= atoiW(tmpChar
+20);
81 t
.tm_sec
= atoiW(tmpChar
+23);
84 tmpChar2
= tmpChar
+ 8;
131 BOOL
GetAddress(LPCWSTR lpszServerName
, INTERNET_PORT nServerPort
,
132 struct hostent
**phe
, struct sockaddr_in
*psa
)
138 TRACE("%s\n", debugstr_w(lpszServerName
));
140 /* Validate server name first
141 * Check if there is sth. like
142 * pinger.macromedia.com:80
143 * if yes, eliminate the :80....
145 found
= strchrW(lpszServerName
, ':');
147 len
= found
- lpszServerName
;
149 len
= strlenW(lpszServerName
);
151 sz
= WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, NULL
, 0, NULL
, NULL
);
152 name
= HeapAlloc(GetProcessHeap(), 0, sz
+1);
153 WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, name
, sz
, NULL
, NULL
);
155 *phe
= gethostbyname(name
);
156 HeapFree( GetProcessHeap(), 0, name
);
160 TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName
) );
164 memset(psa
,0,sizeof(struct sockaddr_in
));
165 memcpy((char *)&psa
->sin_addr
, (*phe
)->h_addr
, (*phe
)->h_length
);
166 psa
->sin_family
= (*phe
)->h_addrtype
;
167 psa
->sin_port
= htons((u_short
)nServerPort
);
173 * Helper function for sending async Callbacks
176 static const char *get_callback_name(DWORD dwInternetStatus
) {
177 static const wininet_flag_info internet_status
[] = {
178 #define FE(x) { x, #x }
179 FE(INTERNET_STATUS_RESOLVING_NAME
),
180 FE(INTERNET_STATUS_NAME_RESOLVED
),
181 FE(INTERNET_STATUS_CONNECTING_TO_SERVER
),
182 FE(INTERNET_STATUS_CONNECTED_TO_SERVER
),
183 FE(INTERNET_STATUS_SENDING_REQUEST
),
184 FE(INTERNET_STATUS_REQUEST_SENT
),
185 FE(INTERNET_STATUS_RECEIVING_RESPONSE
),
186 FE(INTERNET_STATUS_RESPONSE_RECEIVED
),
187 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED
),
188 FE(INTERNET_STATUS_PREFETCH
),
189 FE(INTERNET_STATUS_CLOSING_CONNECTION
),
190 FE(INTERNET_STATUS_CONNECTION_CLOSED
),
191 FE(INTERNET_STATUS_HANDLE_CREATED
),
192 FE(INTERNET_STATUS_HANDLE_CLOSING
),
193 FE(INTERNET_STATUS_REQUEST_COMPLETE
),
194 FE(INTERNET_STATUS_REDIRECT
),
195 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE
),
196 FE(INTERNET_STATUS_USER_INPUT_REQUIRED
),
197 FE(INTERNET_STATUS_STATE_CHANGE
),
198 FE(INTERNET_STATUS_COOKIE_SENT
),
199 FE(INTERNET_STATUS_COOKIE_RECEIVED
),
200 FE(INTERNET_STATUS_PRIVACY_IMPACTED
),
201 FE(INTERNET_STATUS_P3P_HEADER
),
202 FE(INTERNET_STATUS_P3P_POLICYREF
),
203 FE(INTERNET_STATUS_COOKIE_HISTORY
)
208 for (i
= 0; i
< (sizeof(internet_status
) / sizeof(internet_status
[0])); i
++) {
209 if (internet_status
[i
].val
== dwInternetStatus
) return internet_status
[i
].name
;
214 VOID
SendSyncCallback(LPWININETHANDLEHEADER hdr
, DWORD dwContext
,
215 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
216 DWORD dwStatusInfoLength
)
218 HINTERNET hHttpSession
;
219 LPVOID lpvNewInfo
= NULL
;
221 if( !hdr
->lpfnStatusCB
)
224 /* the IE5 version of wininet does not
225 send callbacks if dwContext is zero */
229 hHttpSession
= WININET_FindHandle( hdr
);
230 if( !hHttpSession
) {
231 TRACE(" Could not convert header '%p' into a handle !\n", hdr
);
235 lpvNewInfo
= lpvStatusInfo
;
236 if(!(hdr
->dwInternalFlags
& INET_CALLBACKW
)) {
237 switch(dwInternetStatus
)
239 case INTERNET_STATUS_RESOLVING_NAME
:
240 case INTERNET_STATUS_REDIRECT
:
241 lpvNewInfo
= WININET_strdup_WtoA(lpvStatusInfo
);
245 TRACE(" callback(%p) (%08lx (%p), %08lx, %ld (%s), %p, %ld)\n",
246 hdr
->lpfnStatusCB
, (DWORD
) hHttpSession
, hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
247 lpvNewInfo
, dwStatusInfoLength
);
249 hdr
->lpfnStatusCB(hHttpSession
, dwContext
, dwInternetStatus
,
250 lpvNewInfo
, dwStatusInfoLength
);
252 TRACE(" end callback().\n");
254 if(lpvNewInfo
!= lpvStatusInfo
)
255 HeapFree(GetProcessHeap(), 0, lpvNewInfo
);
257 WININET_Release( hdr
);
262 VOID
SendAsyncCallback(LPWININETHANDLEHEADER hdr
, DWORD dwContext
,
263 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
264 DWORD dwStatusInfoLength
)
266 TRACE("(%p, %08lx, %ld (%s), %p, %ld): %sasync call with callback %p\n",
267 hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
268 lpvStatusInfo
, dwStatusInfoLength
,
269 hdr
->dwFlags
& INTERNET_FLAG_ASYNC
? "" : "non ",
272 if (!(hdr
->lpfnStatusCB
))
275 if (hdr
->dwFlags
& INTERNET_FLAG_ASYNC
)
277 WORKREQUEST workRequest
;
278 struct WORKREQ_SENDCALLBACK
*req
;
279 void *lpvStatusInfo_copy
= lpvStatusInfo
;
283 lpvStatusInfo_copy
= HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength
);
284 memcpy(lpvStatusInfo_copy
, lpvStatusInfo
, dwStatusInfoLength
);
287 workRequest
.asyncall
= SENDCALLBACK
;
288 workRequest
.hdr
= WININET_AddRef( hdr
);
289 req
= &workRequest
.u
.SendCallback
;
290 req
->dwContext
= dwContext
;
291 req
->dwInternetStatus
= dwInternetStatus
;
292 req
->lpvStatusInfo
= lpvStatusInfo_copy
;
293 req
->dwStatusInfoLength
= dwStatusInfoLength
;
295 INTERNET_AsyncCall(&workRequest
);
298 SendSyncCallback(hdr
, dwContext
, dwInternetStatus
,
299 lpvStatusInfo
, dwStatusInfoLength
);