File: wininet.c

package info (click to toggle)
k2pdfopt 2.55%2Bds-3.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,896 kB
  • sloc: ansic: 87,945; cpp: 5,915; makefile: 5
file content (370 lines) | stat: -rw-r--r-- 9,777 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
** wininet.c      Windows specific calls (they do nothing in other platforms)
**
** Part of willus.com general purpose C code library.
**
** Copyright (C) 2020  http://willus.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
*/

#include "willus.h"

#ifdef HAVE_WIN32_API

#include <windows.h>
#include <wininet.h>

#endif

/* #include <process.h> */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <time.h>

#ifdef HAVE_WIN32_API

static int  wininet_timeout_ms=-1;
static int  wininet_retries=-1;
static void wininet_set_timeouts_internal(wfile *wf);



static int wininet_httpget_to_file_simple(char *dstname,char *lurl);
static int wininet_httpstart_simple(wfile *wf,char *host0);
static void *wininet_httpinituri_simple(wfile *wf,char *uri,int *error);

/*
** ret value of 0 indicates error.
*/
int wininet_query_option(wfile *wf,int option,void *buf,int maxlen)

    {
    return(InternetQueryOption((void *)wf->ihandle,option,buf,(void *)&maxlen));
    }


/*
** ret value of 0 indicates error.
*/
int wininet_set_option(wfile *wf,int option,void *value,int nbytes)

    {
    return(InternetSetOption((void *)wf->ihandle,option,value,nbytes));
    }


void wininet_set_timeout(double secs,int retries)

    {
    wininet_timeout_ms = 1000.*secs+0.5;
    wininet_retries = retries;
    }


void wininet_timeout_settings(wfile *wf)

    {
    static char *label[] =
        { "Connect retries","Connect timeout","Send timeout",
          "Receive timeout","Data receive timeout",
          "Data send timeout","Control receive timeout",
          "Control send timeout","" };
    static int units[8]={1,1000,1000,1000,1000,1000,1000,1000};
    static int optno[8]=
        {
        INTERNET_OPTION_CONNECT_RETRIES,
        INTERNET_OPTION_CONNECT_TIMEOUT,
        INTERNET_OPTION_SEND_TIMEOUT,
        INTERNET_OPTION_RECEIVE_TIMEOUT,
        INTERNET_OPTION_DATA_RECEIVE_TIMEOUT,
        INTERNET_OPTION_DATA_SEND_TIMEOUT,
        INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT,
        INTERNET_OPTION_CONTROL_SEND_TIMEOUT
        };
    int     i,n;

    for (i=0;label[i][0]!='\0';i++)
        {
        if (wininet_query_option(wf,optno[i],&n,4))
            if (units[i]==1)
                printf("%-25s  %d\n",label[i],n);
            else
                printf("%-25s  %.2f s\n",label[i],(double)n/units[i]);
        else
            printf("Query error:  %s\n",win_lasterror());
        }
    }


static void wininet_set_timeouts_internal(wfile *wf)

    {
    static int optno[12] =
        {
        INTERNET_OPTION_CONNECT_RETRIES,
        INTERNET_OPTION_CONNECT_TIMEOUT,
        INTERNET_OPTION_SEND_TIMEOUT,
        INTERNET_OPTION_RECEIVE_TIMEOUT,
        INTERNET_OPTION_DATA_RECEIVE_TIMEOUT,
        INTERNET_OPTION_DATA_SEND_TIMEOUT,
        INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT,
        INTERNET_OPTION_CONTROL_SEND_TIMEOUT,
        -1,-1,-1,-1
        };
    int     i;

    if (wininet_retries>=0)
        wininet_set_option(wf,optno[0],&wininet_retries,4);
    if (wininet_timeout_ms>=0)
        for (i=1;optno[i]!=-1;i++)
            wininet_set_option(wf,optno[i],&wininet_timeout_ms,4);
    }


static int wininet_httpget_to_file_simple(char *dstname,char *lurl)

    {
    static char url[MAXFILENAMELEN],host[MAXFILENAMELEN],filename[MAXFILENAMELEN];
    wfile wf;
    int   status,i,errcode;
    void *handle;
    static char buffer[4096];
    int    maxsize;
    int    nbytes,tot1,tot2;
    FILE  *out;

    maxsize=4096;
    if (!strnicmp(lurl,"http://",7))
        strcpy(host,&lurl[7]);
    else if (!strnicmp(lurl,"https://",8))
        strcpy(host,&lurl[8]);
    else
        strcpy(host,lurl);
    strcpy(filename,dstname);
    for (i=0;host[i]!='?' && host[i]!='/' && host[i]!='\0';i++);
    if (host[i]=='\0')
        strcpy(url,"/");
    else
        strcpy(url,&host[i]);
    host[i]='\0';
    status=wininet_httpstart_simple(&wf,host);
    if (status<0)
        return(-2);
    errcode=0;
    handle = wininet_httpinituri_simple(&wf,url,&errcode);
    if (handle==NULL)
        {
        wininet_httpend(&wf);
        return(errcode==HTTP_STATUS_DENIED ? -6: -3);
        }
    out=fopen(filename,"wb");
    if (out==NULL)
        {
        wlprintf("Cannot open file %s for writing.\n",filename);
        wininet_httpenduri(handle);
        wininet_httpend(&wf);
        return(-4);
        }
    tot1 = tot2 = 0;
    while ((nbytes=wininet_httpgetdata(handle,buffer,maxsize))>0)
        {
        tot1 += nbytes;
        tot2 += fwrite(buffer,sizeof(char),nbytes,out);
        }
    fclose(out);
    wininet_httpenduri(handle);
    wininet_httpend(&wf);
    if (tot1!=tot2)
        return(-5);
    return(0);
    }


static int wininet_httpstart_simple(wfile *wf,char *host0)

    {
    static char host[MAXFILENAMELEN];
    int i,port;

    port=INTERNET_INVALID_PORT_NUMBER;
    strncpy(host,host0,255);
    host[255]='\0';
    for (i=strlen(host)-1;i>=0;i--)
        if (host[i]==':')
            break;
    if (i>=0 && host[i]==':' && is_an_integer(&host[i+1]))
        {
        port=atoi(&host[i+1]);
        host[i]='\0';
        }
    wf->ihandle=wf->ftphandle=wf->winhandle=NULL;
    wf->ihandle=InternetOpen("whttp",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
    if (wf->ihandle==NULL)
        return(-1);
    wininet_set_timeouts_internal(wf);
    wf->ftphandle=InternetConnect((HANDLE)wf->ihandle,host,port,NULL,NULL,
                                  INTERNET_SERVICE_HTTP,0,0);
    if (wf->ftphandle==NULL)
        return(-2);
    return(0);
    }


static void *wininet_httpinituri_simple(wfile *wf,char *uri,int *error)

    {
    HINTERNET   handle;
    int     tries;
    int     dwStatus;
    int     dwStatusSize = sizeof(dwStatus);

    handle=HttpOpenRequest((HINTERNET)wf->ftphandle,"GET",uri,NULL,NULL,
                            NULL,INTERNET_FLAG_KEEP_CONNECTION
                             | INTERNET_FLAG_PRAGMA_NOCACHE
                             | INTERNET_FLAG_RELOAD,0);
    if (handle==NULL)
        return(handle);
    (*error)=0;
    for (tries=0;tries<10;tries++)
        {
        int status;
        status=HttpSendRequest(handle,NULL,0,NULL,0);
        if (!status)
            {
            (*error)=GetLastError();
            break;
            }
        status=HttpQueryInfo(handle, HTTP_QUERY_FLAG_NUMBER |
                HTTP_QUERY_STATUS_CODE, &dwStatus,(void *)&dwStatusSize, NULL);
        if (!status)
            {
            (*error)=GetLastError();
            break;
            }
        if (dwStatus==HTTP_STATUS_DENIED)
            {
            if (tries>=5)
                {
                (*error)=HTTP_STATUS_DENIED;
                wininet_httpenduri(handle);
                return(NULL);
                }
            }
        if (dwStatus==HTTP_STATUS_OK)
            break;
        }
    return((void *)handle);
    }


int wininet_httpgetdata(void *handle,char *data,int maxlen)

    {
    int     index,status,bytesread;

    index=0;
    while (1)
        {
        bytesread=0;
        status=InternetReadFile((HINTERNET)handle,&data[index],maxlen-index,(void *)&bytesread);
/* printf("IRF index=%d, status=%d, bread=%d\n",index,status,bytesread); */
        if (!status)
            {
            if (index>0)
                return(index);
            return(-1);
            }
        if (bytesread>0)
            {
            if (maxlen-index==bytesread)
                return(maxlen);
            index+=bytesread;
            continue;
            }
        break;
        }
    return(index);
    }


int wininet_httpenduri(void *handle)

    {
    /* TRUE for success */
    return(InternetCloseHandle((HINTERNET)handle));
    }


void wininet_httpend(wfile *wf)

    {
    /*
    if (wf->ftphandle!=0)
        {
        InternetCloseHandle((HANDLE)wf->ftphandle);
        wf->ftphandle=0;
        }
    */
    if (wf->ihandle!=0)
        {
        InternetCloseHandle((HANDLE)wf->ihandle);
        wf->ihandle=0;
        }
    }
#endif /* HAVE_WIN32_API */

int inet_httpget(char *dstname,char *lurl)

    {
    static char tmpbuf[512];
    int status;

#ifdef HAVE_WIN32_API
    status=wininet_httpget_to_file_simple(dstname,lurl);
    if (wfile_size(dstname)<0.5)
        remove(dstname);
    /* Try wget and curl if internal method didn't work */
    if (status || wfile_status(dstname)==0)
        {
#endif /* HAVE_WIN32_API */
    sprintf(tmpbuf,"wget --no-check-certificate --tries=1 -O \"%s\" \"%s\"",dstname,lurl);
// printf("\n\n\ncommand='%s'\n\n\n",tmpbuf);
    remove(dstname);
    status=wsys_shell_command(tmpbuf,NULL,NULL);
    if (status)
        {
        sprintf(tmpbuf,"curl --connect-timeout 15 -o \"%s\" \"%s\"",dstname,lurl);
        remove(dstname);
        status=wsys_shell_command(tmpbuf,NULL,NULL);
        }
    if (status || wfile_status(dstname)!=1)
        {
        /*
        wlprintf("Could not get URL %s to local file %s (ret status=%d).\n",lurl,dstname,status);
        */
        if (!status)
            status=-1;
        }
    else
        status=0;
#ifdef HAVE_WIN32_API
        }
#endif
    return(status);
    }