Authors: Mike McCormack <mike@codeweavers.com>, Robert Shearman <rob@codeweavers...
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wininet.h"
35 #include "winerror.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(!asctime || !timelen)
53         return 0;
54
55     strncpyW(tmpChar, asctime, TIME_STRING_LEN);
56
57     /* Assert that the string is the expected length */
58     if (tmpChar[TIME_STRING_LEN] != '\0')
59     {
60         tmpChar[TIME_STRING_LEN] = '\0';
61         FIXME("\n");
62     }
63
64     /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
65      * We assume the time is in this format
66      * and divide it into easy to swallow chunks
67      */
68     tmpChar[3]='\0';
69     tmpChar[7]='\0';
70     tmpChar[11]='\0';
71     tmpChar[16]='\0';
72     tmpChar[19]='\0';
73     tmpChar[22]='\0';
74     tmpChar[25]='\0';
75
76     t.tm_year = atoiW(tmpChar+12) - 1900;
77     t.tm_mday = atoiW(tmpChar+5);
78     t.tm_hour = atoiW(tmpChar+17);
79     t.tm_min = atoiW(tmpChar+20);
80     t.tm_sec = atoiW(tmpChar+23);
81
82     /* and month */
83     tmpChar2 = tmpChar + 8;
84     switch(tmpChar2[2])
85     {
86         case 'n':
87             if(tmpChar2[1]=='a')
88                 t.tm_mon = 0;
89             else
90                 t.tm_mon = 5;
91             break;
92         case 'b':
93             t.tm_mon = 1;
94             break;
95         case 'r':
96             if(tmpChar2[1]=='a')
97                 t.tm_mon = 2;
98             else
99                 t.tm_mon = 3;
100             break;
101         case 'y':
102             t.tm_mon = 4;
103             break;
104         case 'l':
105             t.tm_mon = 6;
106             break;
107         case 'g':
108             t.tm_mon = 7;
109             break;
110         case 'p':
111             t.tm_mon = 8;
112             break;
113         case 't':
114             t.tm_mon = 9;
115             break;
116         case 'v':
117             t.tm_mon = 10;
118             break;
119         case 'c':
120             t.tm_mon = 11;
121             break;
122         default:
123             FIXME("\n");
124     }
125
126     return mktime(&t);
127 }
128
129
130 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
131         struct hostent **phe, struct sockaddr_in *psa)
132 {
133     WCHAR *found;
134     char *name;
135     int len, sz;
136
137     TRACE("%s\n", debugstr_w(lpszServerName));
138
139     /* Validate server name first
140      * Check if there is sth. like
141      * pinger.macromedia.com:80
142      * if yes, eliminate the :80....
143      */
144     found = strchrW(lpszServerName, ':');
145     if (found)
146         len = found - lpszServerName;
147     else
148         len = strlenW(lpszServerName);
149
150     sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
151     name = HeapAlloc(GetProcessHeap(), 0, sz+1);
152     WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
153     name[sz] = 0;
154     *phe = gethostbyname(name);
155     HeapFree( GetProcessHeap(), 0, name );
156
157     if (NULL == *phe)
158     {
159         TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName) );
160         return FALSE;
161     }
162
163     memset(psa,0,sizeof(struct sockaddr_in));
164     memcpy((char *)&psa->sin_addr, (*phe)->h_addr, (*phe)->h_length);
165     psa->sin_family = (*phe)->h_addrtype;
166     psa->sin_port = htons((u_short)nServerPort);
167
168     return TRUE;
169 }
170
171 /*
172  * Helper function for sending async Callbacks
173  */
174
175 static const char *get_callback_name(DWORD dwInternetStatus) {
176     static const wininet_flag_info internet_status[] = {
177 #define FE(x) { x, #x }
178         FE(INTERNET_STATUS_RESOLVING_NAME),
179         FE(INTERNET_STATUS_NAME_RESOLVED),
180         FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
181         FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
182         FE(INTERNET_STATUS_SENDING_REQUEST),
183         FE(INTERNET_STATUS_REQUEST_SENT),
184         FE(INTERNET_STATUS_RECEIVING_RESPONSE),
185         FE(INTERNET_STATUS_RESPONSE_RECEIVED),
186         FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
187         FE(INTERNET_STATUS_PREFETCH),
188         FE(INTERNET_STATUS_CLOSING_CONNECTION),
189         FE(INTERNET_STATUS_CONNECTION_CLOSED),
190         FE(INTERNET_STATUS_HANDLE_CREATED),
191         FE(INTERNET_STATUS_HANDLE_CLOSING),
192         FE(INTERNET_STATUS_REQUEST_COMPLETE),
193         FE(INTERNET_STATUS_REDIRECT),
194         FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
195         FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
196         FE(INTERNET_STATUS_STATE_CHANGE),
197         FE(INTERNET_STATUS_COOKIE_SENT),
198         FE(INTERNET_STATUS_COOKIE_RECEIVED),
199         FE(INTERNET_STATUS_PRIVACY_IMPACTED),
200         FE(INTERNET_STATUS_P3P_HEADER),
201         FE(INTERNET_STATUS_P3P_POLICYREF),
202         FE(INTERNET_STATUS_COOKIE_HISTORY),
203         FE(INTERNET_STATE_CONNECTED),
204         FE(INTERNET_STATE_DISCONNECTED),
205         FE(INTERNET_STATE_DISCONNECTED_BY_USER),
206         FE(INTERNET_STATE_IDLE),
207         FE(INTERNET_STATE_BUSY)
208 #undef FE
209     };
210     int i;
211
212     for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
213         if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
214     }
215     return "Unknown";
216 }
217
218 VOID SendAsyncCallbackInt(LPWININETAPPINFOW hIC, HINTERNET hHttpSession,
219                              DWORD dwContext, DWORD dwInternetStatus, LPVOID
220                              lpvStatusInfo, DWORD dwStatusInfoLength)
221 {
222     LPVOID lpvNewInfo = NULL;
223     if (! (hIC->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     TRACE("--> Callback %ld (%s)\n",dwInternetStatus, get_callback_name(dwInternetStatus));
232
233     lpvNewInfo = lpvStatusInfo;
234     if(!(hIC->hdr.dwInternalFlags & INET_CALLBACKW)) {
235         switch(dwInternetStatus)
236         {
237         case INTERNET_STATUS_RESOLVING_NAME:
238         case INTERNET_STATUS_REDIRECT:
239             lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
240         }
241     }
242     hIC->lpfnStatusCB(hHttpSession, dwContext, dwInternetStatus,
243                       lpvNewInfo, dwStatusInfoLength);
244     if(lpvNewInfo != lpvStatusInfo)
245         HeapFree(GetProcessHeap(), 0, lpvNewInfo);
246
247     TRACE("<-- Callback %ld (%s)\n",dwInternetStatus, get_callback_name(dwInternetStatus));
248 }
249
250
251
252 VOID SendAsyncCallback(LPWININETAPPINFOW hIC, HINTERNET hHttpSession,
253                              DWORD dwContext, DWORD dwInternetStatus, LPVOID
254                              lpvStatusInfo,  DWORD dwStatusInfoLength)
255 {
256         TRACE("Send Callback %ld (%s)\n",dwInternetStatus, get_callback_name(dwInternetStatus));
257
258         if (! (hIC->lpfnStatusCB))
259             return;
260         if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
261         {
262             WORKREQUEST workRequest;
263             struct WORKREQ_SENDCALLBACK *req;
264
265             workRequest.asyncall = SENDCALLBACK;
266             workRequest.handle = hIC;
267             req = &workRequest.u.SendCallback;
268             req->hHttpSession = hHttpSession;
269             req->dwContext = dwContext;
270             req->dwInternetStatus = dwInternetStatus;
271             req->lpvStatusInfo = lpvStatusInfo;
272             req->dwStatusInfoLength = dwStatusInfoLength;
273
274             INTERNET_AsyncCall(&workRequest);
275         }
276         else
277             SendAsyncCallbackInt(hIC, hHttpSession, dwContext, dwInternetStatus,
278                                   lpvStatusInfo, dwStatusInfoLength);
279 }