Fix subclassing to support nested messages.
[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
37 #include "wine/debug.h"
38 #include "internet.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
41
42 #define TIME_STRING_LEN  30
43
44 time_t ConvertTimeString(LPCSTR asctime)
45 {
46     char tmpChar[TIME_STRING_LEN];
47     char *tmpChar2;
48     struct tm t;
49     int timelen = strlen(asctime);
50
51     if(!asctime || !timelen)
52         return 0;
53
54     strncpy(tmpChar, asctime, TIME_STRING_LEN);
55
56     /* Assert that the string is the expected length */
57     if (tmpChar[TIME_STRING_LEN] != '\0')
58     {
59         tmpChar[TIME_STRING_LEN] = '\0';
60         FIXME("\n");
61     }
62
63     /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
64      * We assume the time is in this format
65      * and divide it into easy to swallow chunks
66      */
67     tmpChar[3]='\0';
68     tmpChar[7]='\0';
69     tmpChar[11]='\0';
70     tmpChar[16]='\0';
71     tmpChar[19]='\0';
72     tmpChar[22]='\0';
73     tmpChar[25]='\0';
74
75     t.tm_year = atoi(tmpChar+12) - 1900;
76     t.tm_mday = atoi(tmpChar+5);
77     t.tm_hour = atoi(tmpChar+17);
78     t.tm_min = atoi(tmpChar+20);
79     t.tm_sec = atoi(tmpChar+23);
80
81     /* and month */
82     tmpChar2 = tmpChar + 8;
83     switch(tmpChar2[2])
84     {
85         case 'n':
86             if(tmpChar2[1]=='a')
87                 t.tm_mon = 0;
88             else
89                 t.tm_mon = 5;
90             break;
91         case 'b':
92             t.tm_mon = 1;
93             break;
94         case 'r':
95             if(tmpChar2[1]=='a')
96                 t.tm_mon = 2;
97             else
98                 t.tm_mon = 3;
99             break;
100         case 'y':
101             t.tm_mon = 4;
102             break;
103         case 'l':
104             t.tm_mon = 6;
105             break;
106         case 'g':
107             t.tm_mon = 7;
108             break;
109         case 'p':
110             t.tm_mon = 8;
111             break;
112         case 't':
113             t.tm_mon = 9;
114             break;
115         case 'v':
116             t.tm_mon = 10;
117             break;
118         case 'c':
119             t.tm_mon = 11;
120             break;
121         default:
122             FIXME("\n");
123     }
124
125     return mktime(&t);
126 }
127
128
129 BOOL GetAddress(LPCSTR lpszServerName, INTERNET_PORT nServerPort,
130         struct hostent **phe, struct sockaddr_in *psa)
131 {
132     char *found;
133
134     TRACE("%s\n", lpszServerName);
135
136     /* Validate server name first
137      * Check if there is sth. like
138      * pinger.macromedia.com:80
139      * if yes, eliminate the :80....
140      */
141     found = strchr(lpszServerName, ':');
142     if (found)
143     {
144         int len = found - lpszServerName;
145         char *new = HeapAlloc(GetProcessHeap(), 0, len + 1);
146         memcpy( new, lpszServerName, len );
147         new[len] = '\0';
148         TRACE("Found a ':' inside the server name, reparsed name: %s\n", new);
149         *phe = gethostbyname(new);
150         HeapFree( GetProcessHeap(), 0, new );
151     }
152     else *phe = gethostbyname(lpszServerName);
153
154     if (NULL == *phe)
155     {
156         TRACE("Failed to get hostname: (%s)\n", lpszServerName);
157         return FALSE;
158     }
159
160     memset(psa,0,sizeof(struct sockaddr_in));
161     memcpy((char *)&psa->sin_addr, (*phe)->h_addr, (*phe)->h_length);
162     psa->sin_family = (*phe)->h_addrtype;
163     psa->sin_port = htons((u_short)nServerPort);
164
165     return TRUE;
166 }
167
168 /*
169  * Helper function for sending async Callbacks
170  */
171
172 static const char *get_callback_name(DWORD dwInternetStatus) {
173     static const wininet_flag_info internet_status[] = {
174 #define FE(x) { x, #x }
175         FE(INTERNET_STATUS_RESOLVING_NAME),
176         FE(INTERNET_STATUS_NAME_RESOLVED),
177         FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
178         FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
179         FE(INTERNET_STATUS_SENDING_REQUEST),
180         FE(INTERNET_STATUS_REQUEST_SENT),
181         FE(INTERNET_STATUS_RECEIVING_RESPONSE),
182         FE(INTERNET_STATUS_RESPONSE_RECEIVED),
183         FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
184         FE(INTERNET_STATUS_PREFETCH),
185         FE(INTERNET_STATUS_CLOSING_CONNECTION),
186         FE(INTERNET_STATUS_CONNECTION_CLOSED),
187         FE(INTERNET_STATUS_HANDLE_CREATED),
188         FE(INTERNET_STATUS_HANDLE_CLOSING),
189         FE(INTERNET_STATUS_REQUEST_COMPLETE),
190         FE(INTERNET_STATUS_REDIRECT),
191         FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
192         FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
193         FE(INTERNET_STATUS_STATE_CHANGE),
194         FE(INTERNET_STATUS_COOKIE_SENT),
195         FE(INTERNET_STATUS_COOKIE_RECEIVED),
196         FE(INTERNET_STATUS_PRIVACY_IMPACTED),
197         FE(INTERNET_STATUS_P3P_HEADER),
198         FE(INTERNET_STATUS_P3P_POLICYREF),
199         FE(INTERNET_STATUS_COOKIE_HISTORY),
200         FE(INTERNET_STATE_CONNECTED),
201         FE(INTERNET_STATE_DISCONNECTED),
202         FE(INTERNET_STATE_DISCONNECTED_BY_USER),
203         FE(INTERNET_STATE_IDLE),
204         FE(INTERNET_STATE_BUSY)
205 #undef FE
206     };
207     int i;
208
209     for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
210         if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
211     }
212     return "Unknown";
213 }
214
215 VOID SendAsyncCallbackInt(LPWININETAPPINFOA hIC, HINTERNET hHttpSession,
216                              DWORD dwContext, DWORD dwInternetStatus, LPVOID
217                              lpvStatusInfo, DWORD dwStatusInfoLength)
218 {
219         if (! (hIC->lpfnStatusCB))
220             return;
221
222         /* the IE5 version of wininet does not
223            send callbacks if dwContext is zero */
224         if( !dwContext )
225             return;
226
227         TRACE("--> Callback %ld (%s)\n",dwInternetStatus, get_callback_name(dwInternetStatus));
228
229         hIC->lpfnStatusCB(hHttpSession, dwContext, dwInternetStatus,
230                           lpvStatusInfo, dwStatusInfoLength);
231
232         TRACE("<-- Callback %ld (%s)\n",dwInternetStatus, get_callback_name(dwInternetStatus));
233 }
234
235
236
237 VOID SendAsyncCallback(LPWININETAPPINFOA hIC, HINTERNET hHttpSession,
238                              DWORD dwContext, DWORD dwInternetStatus, LPVOID
239                              lpvStatusInfo,  DWORD dwStatusInfoLength)
240 {
241         TRACE("Send Callback %ld (%s)\n",dwInternetStatus, get_callback_name(dwInternetStatus));
242
243         if (! (hIC->lpfnStatusCB))
244             return;
245         if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
246         {
247             WORKREQUEST workRequest;
248             struct WORKREQ_SENDCALLBACK *req;
249
250             workRequest.asyncall = SENDCALLBACK;
251             workRequest.handle = hIC;
252             req = &workRequest.u.SendCallback;
253             req->hHttpSession = hHttpSession;
254             req->dwContext = dwContext;
255             req->dwInternetStatus = dwInternetStatus;
256             req->lpvStatusInfo = lpvStatusInfo;
257             req->dwStatusInfoLength = dwStatusInfoLength;
258
259             INTERNET_AsyncCall(&workRequest);
260         }
261         else
262             SendAsyncCallbackInt(hIC, hHttpSession, dwContext, dwInternetStatus,
263                                   lpvStatusInfo, dwStatusInfoLength);
264 }