Fixed some issues found by winapi_check.
[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 <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wininet.h"
34 #include "winerror.h"
35
36 #include "wine/debug.h"
37 #include "internet.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
40
41 #define TIME_STRING_LEN  30
42
43 time_t ConvertTimeString(LPCSTR asctime)
44 {
45     char tmpChar[TIME_STRING_LEN];
46     char *tmpChar2;
47     struct tm t;
48     int timelen = strlen(asctime);
49
50     if(!asctime || !timelen)
51         return 0;
52
53     strncpy(tmpChar, asctime, TIME_STRING_LEN);
54
55     /* Assert that the string is the expected length */
56     if (tmpChar[TIME_STRING_LEN] != '\0')
57     {
58         tmpChar[TIME_STRING_LEN] = '\0';
59         FIXME("\n");
60     }
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 = atoi(tmpChar+12) - 1900;
75     t.tm_mday = atoi(tmpChar+5);
76     t.tm_hour = atoi(tmpChar+17);
77     t.tm_min = atoi(tmpChar+20);
78     t.tm_sec = atoi(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(LPCSTR lpszServerName, INTERNET_PORT nServerPort,
129         struct hostent **phe, struct sockaddr_in *psa)
130 {
131     char *found;
132
133     TRACE("%s\n", lpszServerName);
134
135     /* Validate server name first
136      * Check if there is sth. like
137      * pinger.macromedia.com:80
138      * if yes, eliminate the :80....
139      */
140     found = strchr(lpszServerName, ':');
141     if (found)
142     {
143         int len = found - lpszServerName;
144         char *new = HeapAlloc(GetProcessHeap(), 0, len + 1);
145         memcpy( new, lpszServerName, len );
146         new[len] = '\0';
147         TRACE("Found a ':' inside the server name, reparsed name: %s\n", new);
148         *phe = gethostbyname(new);
149         HeapFree( GetProcessHeap(), 0, new );
150     }
151     else *phe = gethostbyname(lpszServerName);
152
153     if (NULL == *phe)
154     {
155         TRACE("Failed to get hostname: (%s)\n", lpszServerName);
156         return FALSE;
157     }
158
159     memset(psa,0,sizeof(struct sockaddr_in));
160     memcpy((char *)&psa->sin_addr, (*phe)->h_addr, (*phe)->h_length);
161     psa->sin_family = (*phe)->h_addrtype;
162     psa->sin_port = htons((u_short)nServerPort);
163
164     return TRUE;
165 }
166
167 /*
168  * Helper function for sending async Callbacks
169  */
170
171 VOID SendAsyncCallbackInt(LPWININETAPPINFOA hIC, HINTERNET hHttpSession,
172                              DWORD dwContext, DWORD dwInternetStatus, LPVOID
173                              lpvStatusInfo, DWORD dwStatusInfoLength)
174 {
175         if (! (hIC->lpfnStatusCB))
176             return;
177
178         TRACE("--> Callback %ld\n",dwInternetStatus);
179
180         hIC->lpfnStatusCB(hHttpSession, dwContext, dwInternetStatus,
181                           lpvStatusInfo, dwStatusInfoLength);
182
183         TRACE("<-- Callback %ld\n",dwInternetStatus);
184 }
185
186
187
188 VOID SendAsyncCallback(LPWININETAPPINFOA hIC, HINTERNET hHttpSession,
189                              DWORD dwContext, DWORD dwInternetStatus, LPVOID
190                              lpvStatusInfo,  DWORD dwStatusInfoLength)
191 {
192         TRACE("Send Callback %ld\n",dwInternetStatus);
193
194         if (! (hIC->lpfnStatusCB))
195             return;
196         if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
197         {
198             WORKREQUEST workRequest;
199
200             workRequest.asyncall = SENDCALLBACK;
201
202             workRequest.param1 = (DWORD)hIC;
203             workRequest.param2 = (DWORD)hHttpSession;
204             workRequest.param3 = dwContext;
205             workRequest.param4 = dwInternetStatus;
206             workRequest.param5 = (DWORD)lpvStatusInfo;
207             workRequest.param6 = dwStatusInfoLength;
208
209             INTERNET_AsyncCall(&workRequest);
210         }
211         else
212             SendAsyncCallbackInt(hIC, hHttpSession, dwContext, dwInternetStatus,
213                                   lpvStatusInfo, dwStatusInfoLength);
214 }