comdlg32: Remove spaces before '\n' (and some after) in resource strings.
[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 /* critical section to protect non-reentrant gethostbyname() */
44 static CRITICAL_SECTION cs_gethostbyname;
45 static CRITICAL_SECTION_DEBUG critsect_debug =
46 {
47     0, 0, &cs_gethostbyname,
48     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
49       0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
50 };
51 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
52
53 #define TIME_STRING_LEN  30
54
55 time_t ConvertTimeString(LPCWSTR asctime)
56 {
57     WCHAR tmpChar[TIME_STRING_LEN];
58     WCHAR *tmpChar2;
59     struct tm t;
60     int timelen = strlenW(asctime);
61
62     if(!timelen)
63         return 0;
64
65     /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
66     memset( tmpChar, 0, sizeof(tmpChar) );
67     lstrcpynW(tmpChar, asctime, TIME_STRING_LEN);
68
69     /* Assert that the string is the expected length */
70     if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
71
72     /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
73      * We assume the time is in this format
74      * and divide it into easy to swallow chunks
75      */
76     tmpChar[3]='\0';
77     tmpChar[7]='\0';
78     tmpChar[11]='\0';
79     tmpChar[16]='\0';
80     tmpChar[19]='\0';
81     tmpChar[22]='\0';
82     tmpChar[25]='\0';
83
84     memset( &t, 0, sizeof(t) );
85     t.tm_year = atoiW(tmpChar+12) - 1900;
86     t.tm_mday = atoiW(tmpChar+5);
87     t.tm_hour = atoiW(tmpChar+17);
88     t.tm_min = atoiW(tmpChar+20);
89     t.tm_sec = atoiW(tmpChar+23);
90
91     /* and month */
92     tmpChar2 = tmpChar + 8;
93     switch(tmpChar2[2])
94     {
95         case 'n':
96             if(tmpChar2[1]=='a')
97                 t.tm_mon = 0;
98             else
99                 t.tm_mon = 5;
100             break;
101         case 'b':
102             t.tm_mon = 1;
103             break;
104         case 'r':
105             if(tmpChar2[1]=='a')
106                 t.tm_mon = 2;
107             else
108                 t.tm_mon = 3;
109             break;
110         case 'y':
111             t.tm_mon = 4;
112             break;
113         case 'l':
114             t.tm_mon = 6;
115             break;
116         case 'g':
117             t.tm_mon = 7;
118             break;
119         case 'p':
120             t.tm_mon = 8;
121             break;
122         case 't':
123             t.tm_mon = 9;
124             break;
125         case 'v':
126             t.tm_mon = 10;
127             break;
128         case 'c':
129             t.tm_mon = 11;
130             break;
131         default:
132             FIXME("\n");
133     }
134
135     return mktime(&t);
136 }
137
138
139 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
140         struct sockaddr_in *psa)
141 {
142     WCHAR *found;
143     char *name;
144     int len, sz;
145     struct hostent *phe;
146
147     TRACE("%s\n", debugstr_w(lpszServerName));
148
149     /* Validate server name first
150      * Check if there is sth. like
151      * pinger.macromedia.com:80
152      * if yes, eliminate the :80....
153      */
154     found = strchrW(lpszServerName, ':');
155     if (found)
156         len = found - lpszServerName;
157     else
158         len = strlenW(lpszServerName);
159
160     sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
161     name = HeapAlloc(GetProcessHeap(), 0, sz+1);
162     WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
163     name[sz] = 0;
164
165     EnterCriticalSection( &cs_gethostbyname );
166     phe = gethostbyname(name);
167     HeapFree( GetProcessHeap(), 0, name );
168
169     if (NULL == phe)
170     {
171         TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName) );
172         LeaveCriticalSection( &cs_gethostbyname );
173         return FALSE;
174     }
175
176     memset(psa,0,sizeof(struct sockaddr_in));
177     memcpy((char *)&psa->sin_addr, phe->h_addr, phe->h_length);
178     psa->sin_family = phe->h_addrtype;
179     psa->sin_port = htons(nServerPort);
180
181     LeaveCriticalSection( &cs_gethostbyname );
182     return TRUE;
183 }
184
185 /*
186  * Helper function for sending async Callbacks
187  */
188
189 static const char *get_callback_name(DWORD dwInternetStatus) {
190     static const wininet_flag_info internet_status[] = {
191 #define FE(x) { x, #x }
192         FE(INTERNET_STATUS_RESOLVING_NAME),
193         FE(INTERNET_STATUS_NAME_RESOLVED),
194         FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
195         FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
196         FE(INTERNET_STATUS_SENDING_REQUEST),
197         FE(INTERNET_STATUS_REQUEST_SENT),
198         FE(INTERNET_STATUS_RECEIVING_RESPONSE),
199         FE(INTERNET_STATUS_RESPONSE_RECEIVED),
200         FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
201         FE(INTERNET_STATUS_PREFETCH),
202         FE(INTERNET_STATUS_CLOSING_CONNECTION),
203         FE(INTERNET_STATUS_CONNECTION_CLOSED),
204         FE(INTERNET_STATUS_HANDLE_CREATED),
205         FE(INTERNET_STATUS_HANDLE_CLOSING),
206         FE(INTERNET_STATUS_REQUEST_COMPLETE),
207         FE(INTERNET_STATUS_REDIRECT),
208         FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
209         FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
210         FE(INTERNET_STATUS_STATE_CHANGE),
211         FE(INTERNET_STATUS_COOKIE_SENT),
212         FE(INTERNET_STATUS_COOKIE_RECEIVED),
213         FE(INTERNET_STATUS_PRIVACY_IMPACTED),
214         FE(INTERNET_STATUS_P3P_HEADER),
215         FE(INTERNET_STATUS_P3P_POLICYREF),
216         FE(INTERNET_STATUS_COOKIE_HISTORY)
217 #undef FE
218     };
219     DWORD i;
220
221     for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
222         if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
223     }
224     return "Unknown";
225 }
226
227 VOID INTERNET_SendCallback(LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext,
228                            DWORD dwInternetStatus, LPVOID lpvStatusInfo,
229                            DWORD dwStatusInfoLength)
230 {
231     LPVOID lpvNewInfo = NULL;
232
233     if( !hdr->lpfnStatusCB )
234         return;
235
236     /* the IE5 version of wininet does not
237        send callbacks if dwContext is zero */
238     if( !dwContext )
239         return;
240
241     lpvNewInfo = lpvStatusInfo;
242     if(hdr->dwInternalFlags & INET_CALLBACKW) {
243         switch(dwInternetStatus) {
244         case INTERNET_STATUS_NAME_RESOLVED:
245         case INTERNET_STATUS_CONNECTING_TO_SERVER:
246         case INTERNET_STATUS_CONNECTED_TO_SERVER:
247             lpvNewInfo = WININET_strdup_AtoW(lpvStatusInfo);
248             break;
249         case INTERNET_STATUS_RESOLVING_NAME:
250         case INTERNET_STATUS_REDIRECT:
251             lpvNewInfo = WININET_strdupW(lpvStatusInfo);
252             break;
253         }
254     }else {
255         switch(dwInternetStatus)
256         {
257         case INTERNET_STATUS_NAME_RESOLVED:
258         case INTERNET_STATUS_CONNECTING_TO_SERVER:
259         case INTERNET_STATUS_CONNECTED_TO_SERVER:
260             lpvNewInfo = HeapAlloc(GetProcessHeap(), 0, strlen(lpvStatusInfo) + 1);
261             if (lpvNewInfo) strcpy(lpvNewInfo, lpvStatusInfo);
262             break;
263         case INTERNET_STATUS_RESOLVING_NAME:
264         case INTERNET_STATUS_REDIRECT:
265             lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
266             break;
267         }
268     }
269     
270     TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %p, %d)\n",
271           hdr->lpfnStatusCB, hdr->hInternet, hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
272           lpvNewInfo, dwStatusInfoLength);
273     
274     hdr->lpfnStatusCB(hdr->hInternet, dwContext, dwInternetStatus,
275                       lpvNewInfo, dwStatusInfoLength);
276
277     TRACE(" end callback().\n");
278
279     if(lpvNewInfo != lpvStatusInfo)
280         HeapFree(GetProcessHeap(), 0, lpvNewInfo);
281 }
282
283 static void SendAsyncCallbackProc(WORKREQUEST *workRequest)
284 {
285     struct WORKREQ_SENDCALLBACK const *req = &workRequest->u.SendCallback;
286
287     TRACE("%p\n", workRequest->hdr);
288
289     INTERNET_SendCallback(workRequest->hdr,
290                           req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
291                           req->dwStatusInfoLength);
292
293     /* And frees the copy of the status info */
294     HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
295 }
296
297 VOID SendAsyncCallback(LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext,
298                        DWORD dwInternetStatus, LPVOID lpvStatusInfo,
299                        DWORD dwStatusInfoLength)
300 {
301     TRACE("(%p, %08lx, %d (%s), %p, %d): %sasync call with callback %p\n",
302           hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
303           lpvStatusInfo, dwStatusInfoLength,
304           hdr->dwFlags & INTERNET_FLAG_ASYNC ? "" : "non ",
305           hdr->lpfnStatusCB);
306     
307     if (!(hdr->lpfnStatusCB))
308         return;
309     
310     if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
311     {
312         WORKREQUEST workRequest;
313         struct WORKREQ_SENDCALLBACK *req;
314         void *lpvStatusInfo_copy = lpvStatusInfo;
315
316         if (lpvStatusInfo)
317         {
318             lpvStatusInfo_copy = HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength);
319             memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
320         }
321
322         workRequest.asyncproc = SendAsyncCallbackProc;
323         workRequest.hdr = WININET_AddRef( hdr );
324         req = &workRequest.u.SendCallback;
325         req->dwContext = dwContext;
326         req->dwInternetStatus = dwInternetStatus;
327         req->lpvStatusInfo = lpvStatusInfo_copy;
328         req->dwStatusInfoLength = dwStatusInfoLength;
329         
330         INTERNET_AsyncCall(&workRequest);
331     }
332     else
333         INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
334                               lpvStatusInfo, dwStatusInfoLength);
335 }