mshtml: Implement HTMLElement_insertAdjacentHTML and HTMLElement_insertAdjacentText.
[wine] / dlls / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "winnls.h"
37
38 #include "wine/debug.h"
39 #include "internet.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
42
43 #define TIME_STRING_LEN  30
44
45 time_t ConvertTimeString(LPCWSTR asctime)
46 {
47     WCHAR tmpChar[TIME_STRING_LEN];
48     WCHAR *tmpChar2;
49     struct tm t;
50     int timelen = strlenW(asctime);
51
52     if(!timelen)
53         return 0;
54
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);
58
59     /* Assert that the string is the expected length */
60     if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
61
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
65      */
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';
73
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);
79
80     /* and month */
81     tmpChar2 = tmpChar + 8;
82     switch(tmpChar2[2])
83     {
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");
122     }
123
124     return mktime(&t);
125 }
126
127
128 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
129         struct sockaddr_in *psa)
130 {
131     WCHAR *found;
132     char *name;
133     int len, sz;
134     struct hostent *phe;
135
136     TRACE("%s\n", debugstr_w(lpszServerName));
137
138     /* Validate server name first
139      * Check if there is sth. like
140      * pinger.macromedia.com:80
141      * if yes, eliminate the :80....
142      */
143     found = strchrW(lpszServerName, ':');
144     if (found)
145         len = found - lpszServerName;
146     else
147         len = strlenW(lpszServerName);
148
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 );
155
156     if (NULL == phe)
157     {
158         TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName) );
159         return FALSE;
160     }
161
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((u_short)nServerPort);
166
167     return TRUE;
168 }
169
170 /*
171  * Helper function for sending async Callbacks
172  */
173
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
203     };
204     DWORD i;
205
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;
208     }
209     return "Unknown";
210 }
211
212 VOID INTERNET_SendCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
213                            DWORD dwInternetStatus, LPVOID lpvStatusInfo,
214                            DWORD dwStatusInfoLength)
215 {
216     LPVOID lpvNewInfo = NULL;
217
218     if( !hdr->lpfnStatusCB )
219         return;
220
221     /* the IE5 version of wininet does not
222        send callbacks if dwContext is zero */
223     if( !dwContext )
224         return;
225
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;
238         }
239     }else {
240         switch(dwInternetStatus)
241         {
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;
252         }
253     }
254     
255     TRACE(" callback(%p) (%p (%p), %08x, %d (%s), %p, %d)\n",
256           hdr->lpfnStatusCB, hdr->hInternet, hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
257           lpvNewInfo, dwStatusInfoLength);
258     
259     hdr->lpfnStatusCB(hdr->hInternet, dwContext, dwInternetStatus,
260                       lpvNewInfo, dwStatusInfoLength);
261
262     TRACE(" end callback().\n");
263
264     if(lpvNewInfo != lpvStatusInfo)
265         HeapFree(GetProcessHeap(), 0, lpvNewInfo);
266 }
267
268 static void SendAsyncCallbackProc(WORKREQUEST *workRequest)
269 {
270     struct WORKREQ_SENDCALLBACK const *req = &workRequest->u.SendCallback;
271
272     TRACE("%p\n", workRequest->hdr);
273
274     INTERNET_SendCallback(workRequest->hdr,
275                           req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
276                           req->dwStatusInfoLength);
277
278     /* And frees the copy of the status info */
279     HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
280 }
281
282 VOID SendAsyncCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
283                        DWORD dwInternetStatus, LPVOID lpvStatusInfo,
284                        DWORD dwStatusInfoLength)
285 {
286     TRACE("(%p, %08x, %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);
291     
292     if (!(hdr->lpfnStatusCB))
293         return;
294     
295     if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
296     {
297         WORKREQUEST workRequest;
298         struct WORKREQ_SENDCALLBACK *req;
299         void *lpvStatusInfo_copy = lpvStatusInfo;
300
301         if (lpvStatusInfo)
302         {
303             lpvStatusInfo_copy = HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength);
304             memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
305         }
306
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;
314         
315         INTERNET_AsyncCall(&workRequest);
316     }
317     else
318         INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
319                               lpvStatusInfo, dwStatusInfoLength);
320 }