Remove Wine-ism
[reactos.git] / reactos / lib / wininet / utility.c
1 /*
2 * Wininet - Utility functions
3 *
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 *
7 * Ulrich Czekalla
8 * Aric Stewart
9 *
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.
14 *
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.
19 *
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
23 */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wininet.h"
36 #include "winerror.h"
37 #include "winnls.h"
38
39 #include "wine/debug.h"
40 #include "internet.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
43
44 #define TIME_STRING_LEN 30
45
46
47 time_t ConvertTimeString(LPCWSTR asctime)
48 {
49 WCHAR tmpChar[TIME_STRING_LEN];
50 WCHAR *tmpChar2;
51 struct tm t;
52 int timelen = strlenW(asctime);
53
54 if(!asctime || !timelen)
55 return 0;
56
57 /* The atoiWs below relie on that tmpChar is \0 padded? */
58 strncpyW(tmpChar, asctime, TIME_STRING_LEN);
59
60 /* Assert that the string is the expected length */
61 if (tmpChar[TIME_STRING_LEN - 1] != '\0')
62 {
63 tmpChar[TIME_STRING_LEN - 1] = '\0';
64 FIXME("\n");
65 }
66
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
70 */
71 tmpChar[3]='\0';
72 tmpChar[7]='\0';
73 tmpChar[11]='\0';
74 tmpChar[16]='\0';
75 tmpChar[19]='\0';
76 tmpChar[22]='\0';
77 tmpChar[25]='\0';
78
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);
84
85 /* and month */
86 tmpChar2 = tmpChar + 8;
87 switch(tmpChar2[2])
88 {
89 case 'n':
90 if(tmpChar2[1]=='a')
91 t.tm_mon = 0;
92 else
93 t.tm_mon = 5;
94 break;
95 case 'b':
96 t.tm_mon = 1;
97 break;
98 case 'r':
99 if(tmpChar2[1]=='a')
100 t.tm_mon = 2;
101 else
102 t.tm_mon = 3;
103 break;
104 case 'y':
105 t.tm_mon = 4;
106 break;
107 case 'l':
108 t.tm_mon = 6;
109 break;
110 case 'g':
111 t.tm_mon = 7;
112 break;
113 case 'p':
114 t.tm_mon = 8;
115 break;
116 case 't':
117 t.tm_mon = 9;
118 break;
119 case 'v':
120 t.tm_mon = 10;
121 break;
122 case 'c':
123 t.tm_mon = 11;
124 break;
125 default:
126 FIXME("\n");
127 }
128
129 return mktime(&t);
130 }
131
132
133 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
134 struct hostent **phe, struct sockaddr_in *psa)
135 {
136 WCHAR *found;
137 char *name;
138 int len, sz;
139
140 TRACE("%s\n", debugstr_w(lpszServerName));
141
142 /* Validate server name first
143 * Check if there is sth. like
144 * pinger.macromedia.com:80
145 * if yes, eliminate the :80....
146 */
147 found = strchrW(lpszServerName, ':');
148 if (found)
149 len = found - lpszServerName;
150 else
151 len = strlenW(lpszServerName);
152
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 );
156 name[sz] = 0;
157 *phe = gethostbyname(name);
158 HeapFree( GetProcessHeap(), 0, name );
159
160 if (NULL == *phe)
161 {
162 TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName) );
163 return FALSE;
164 }
165
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);
170
171 return TRUE;
172 }
173
174 /*
175 * Helper function for sending async Callbacks
176 */
177
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)
206 #undef FE
207 };
208 DWORD i;
209
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;
212 }
213 return "Unknown";
214 }
215
216 VOID SendSyncCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
217 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
218 DWORD dwStatusInfoLength)
219 {
220 HINTERNET hHttpSession;
221 LPVOID lpvNewInfo = NULL;
222
223 if( !hdr->lpfnStatusCB )
224 return;
225
226 /* the IE5 version of wininet does not
227 send callbacks if dwContext is zero */
228 if( !dwContext )
229 return;
230
231 hHttpSession = WININET_FindHandle( hdr );
232 if( !hHttpSession ) {
233 TRACE(" Could not convert header '%p' into a handle !\n", hdr);
234 return;
235 }
236
237 lpvNewInfo = lpvStatusInfo;
238 if(!(hdr->dwInternalFlags & INET_CALLBACKW)) {
239 switch(dwInternetStatus)
240 {
241 case INTERNET_STATUS_RESOLVING_NAME:
242 case INTERNET_STATUS_REDIRECT:
243 lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
244 }
245 }
246
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);
250
251 hdr->lpfnStatusCB(hHttpSession, dwContext, dwInternetStatus,
252 lpvNewInfo, dwStatusInfoLength);
253
254 TRACE(" end callback().\n");
255
256 if(lpvNewInfo != lpvStatusInfo)
257 HeapFree(GetProcessHeap(), 0, lpvNewInfo);
258
259 WININET_Release( hdr );
260 }
261
262
263
264 VOID SendAsyncCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
265 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
266 DWORD dwStatusInfoLength)
267 {
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 ",
272 hdr->lpfnStatusCB);
273
274 if (!(hdr->lpfnStatusCB))
275 return;
276
277 if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
278 {
279 WORKREQUEST workRequest;
280 struct WORKREQ_SENDCALLBACK *req;
281 void *lpvStatusInfo_copy = lpvStatusInfo;
282
283 if (lpvStatusInfo)
284 {
285 lpvStatusInfo_copy = HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength);
286 memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
287 }
288
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;
296
297 INTERNET_AsyncCall(&workRequest);
298 }
299 else
300 SendSyncCallback(hdr, dwContext, dwInternetStatus,
301 lpvStatusInfo, dwStatusInfoLength);
302 }