d3d9: Fix a few ok conditions in the visual tests.
[wine.git] / dlls / wininet / utility.c
blob352b2609c1752ee21f26bbb4e28ac16fd35474f2
1 /*
2 * Wininet - Utility functions
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
7 * Ulrich Czekalla
8 * Aric Stewart
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wininet.h"
36 #include "winnls.h"
38 #include "wine/debug.h"
39 #include "internet.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];
48 WCHAR *tmpChar2;
49 struct tm t;
50 int timelen = strlenW(asctime);
52 if(!timelen)
53 return 0;
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
66 tmpChar[3]='\0';
67 tmpChar[7]='\0';
68 tmpChar[11]='\0';
69 tmpChar[16]='\0';
70 tmpChar[19]='\0';
71 tmpChar[22]='\0';
72 tmpChar[25]='\0';
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);
80 /* and month */
81 tmpChar2 = tmpChar + 8;
82 switch(tmpChar2[2])
84 case 'n':
85 if(tmpChar2[1]=='a')
86 t.tm_mon = 0;
87 else
88 t.tm_mon = 5;
89 break;
90 case 'b':
91 t.tm_mon = 1;
92 break;
93 case 'r':
94 if(tmpChar2[1]=='a')
95 t.tm_mon = 2;
96 else
97 t.tm_mon = 3;
98 break;
99 case 'y':
100 t.tm_mon = 4;
101 break;
102 case 'l':
103 t.tm_mon = 6;
104 break;
105 case 'g':
106 t.tm_mon = 7;
107 break;
108 case 'p':
109 t.tm_mon = 8;
110 break;
111 case 't':
112 t.tm_mon = 9;
113 break;
114 case 'v':
115 t.tm_mon = 10;
116 break;
117 case 'c':
118 t.tm_mon = 11;
119 break;
120 default:
121 FIXME("\n");
124 return mktime(&t);
128 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
129 struct sockaddr_in *psa)
131 WCHAR *found;
132 char *name;
133 int len, sz;
134 struct hostent *phe;
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, ':');
144 if (found)
145 len = found - lpszServerName;
146 else
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 );
152 name[sz] = 0;
153 phe = gethostbyname(name);
154 HeapFree( GetProcessHeap(), 0, name );
156 if (NULL == phe)
158 TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName) );
159 return FALSE;
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(nServerPort);
167 return TRUE;
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)
202 #undef FE
204 DWORD i;
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;
209 return "Unknown";
212 VOID INTERNET_SendCallback(LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext,
213 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
214 DWORD dwStatusInfoLength)
216 LPVOID lpvNewInfo = NULL;
218 if( !hdr->lpfnStatusCB )
219 return;
221 /* the IE5 version of wininet does not
222 send callbacks if dwContext is zero */
223 if( !dwContext )
224 return;
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);
233 break;
234 case INTERNET_STATUS_RESOLVING_NAME:
235 case INTERNET_STATUS_REDIRECT:
236 lpvNewInfo = WININET_strdupW(lpvStatusInfo);
237 break;
239 }else {
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);
247 break;
248 case INTERNET_STATUS_RESOLVING_NAME:
249 case INTERNET_STATUS_REDIRECT:
250 lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
251 break;
255 TRACE(" callback(%p) (%p (%p), %08lx, %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_PTR dwContext,
283 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
284 DWORD dwStatusInfoLength)
286 TRACE("(%p, %08lx, %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 ",
290 hdr->lpfnStatusCB);
292 if (!(hdr->lpfnStatusCB))
293 return;
295 if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
297 WORKREQUEST workRequest;
298 struct WORKREQ_SENDCALLBACK *req;
299 void *lpvStatusInfo_copy = lpvStatusInfo;
301 if (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);
317 else
318 INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
319 lpvStatusInfo, dwStatusInfoLength);