File: httpget.c

package info (click to toggle)
wcstools 3.9.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,684 kB
  • sloc: ansic: 113,336; sh: 553; makefile: 245; lisp: 86; sed: 1
file content (378 lines) | stat: -rw-r--r-- 8,886 bytes parent folder | download | duplicates (5)
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
371
372
373
374
375
376
377
378
/* File httpget.c
 * February 6, 2015
 * By Jessica Mink and John Roll

 * Test http access to scat
 * Load with -lnsl -lsocket
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define CHUNK	8192
#define LINE	1024

FILE *gethttp();
static int all = 0;
static int verbose = 0;

main (ac, av)
int ac;
char **av;
{
    FILE *fp;
    int  red;
    char *url;

    if (ac == 1) {
	printf ("HTTPGET: Print contents of URL\n");
	printf ("If -a, all to standard out, else non-data to stderr\n");
	exit (0);
	}

    url = *(av+1);
    if (url[0] == '-' && url[1] == 'a') {
	all = 1;
	url = *(av+2);
	}
    if (url[0] == '-' && url[1] == 'v') {
	verbose = 1;
	url = *(av+2);
	}
    if ( !(fp = gethttp (url)) ) {
	fprintf(stderr, "Can't read URL %s\n", url);
	exit (1);
	}
    exit (0);
}


#define NO_XFILE_H

FILE *SokOpen();
#define XFREAD  1
#define XFWRITE 2
#define XFCREAT 4

#define File    FILE *
#define OpenFd(fd, mode)        fdopen(fd, mode)
#define FileFd(fd)              fileno(fd)
#define Malloc(space, size)     ( (space) = (void *) malloc(size) )


File
gethttp (url)
char	*url;
{
    File sok;

    char hosturl[LINE];
    char linebuf[CHUNK];
    char page[LINE];
    char *buffer;
    char *fpage;
    char *port;
    char *cbcont;
    char *newpage;
    int  nport = 80;
    int  chunked = 0;
    int  lchunk;
    int  status;
    int  red;
    int  nbcont = 0;
    int  nbr;
    int  i;
    char *strcsrch();
    char *encodeURL();

    port = NULL;
    strcpy (hosturl, url);
 
    /* Extract server name and path from URL */
    if ( !strncmp(hosturl, "http://", 7) ) {
	strcpy(hosturl, url+7);
	}
    fpage = strchr (hosturl, '/');
    if (fpage != NULL) {
	strcpy (page, fpage);
	*fpage = (char) 0;
	if (port = strchr (hosturl, ':') ) {
	    *port = '\0';
	    port++;
	    nport = atoi (port);
	    }
	}
    else {
	page[0] = '/';
	page[1] = '\0';
	}

    /* Open port to HTTP server */
    if ( !(sok = SokOpen (hosturl, nport, XFREAD | XFWRITE)) ) {
	if (port != NULL)
	    fprintf(stderr, "Can't read URL %s:%s\n", hosturl, port);
	else
	    fprintf(stderr, "Can't read URL %s\n", hosturl);
	abort();
	}

    /* Make sure that URL contains only legal characters */
    /* newpage = encodeURL (page); */
    newpage = page;

    fprintf(sok, "GET %s HTTP/1.1\nHost: %s\n\n", newpage, hosturl);
    fflush(sok);

    fscanf(sok, "%*s %d %*s\n", &status);

    if ( status != 200 ) return NULL;

    nbcont = 0;
    while ( fgets(linebuf, LINE, sok) ) {
	if (all)
	    fprintf (stdout, "%s", linebuf);
	else
	    fprintf (stderr, "%s", linebuf);
	if (strcsrch (linebuf, "chunked") != NULL) {
	    chunked = 1;
	    if (all)
		fprintf (stdout, "chunked return\n");
	    else if (verbose)
		fprintf (stderr, "chunked return\n");
	    }
	if (strcsrch (linebuf, "Content-length") != NULL) {
	    if ((cbcont = strchr (linebuf, ':')) != NULL) {
		nbcont = atoi (cbcont+1);
		if (all)
		    fprintf (stdout, "%d bytes\n", nbcont);
		else if (verbose)
		    fprintf (stderr, "%d bytes\n", nbcont);
		}
	    }
	if ( *linebuf == '\n' ) break;
	if ( *linebuf == '\r' ) break;
	}

    if (nbcont == 0) {
	fgets (linebuf, LINE, sok);
	if (all)
	    fprintf (stdout, "%s", linebuf);
	else if (verbose)
	    fprintf (stderr, "%s", linebuf);
	}

    /* Print result a chunk at a time */
    if (chunked) {
	lchunk = (int) strtol (linebuf, NULL, 16);
	buffer = (char *) calloc (1, CHUNK);
	while (lchunk > 0) {
	    for (i = 0; i < CHUNK; i++)
		buffer[i] = (char) 0;
	    nbr = fread (buffer, 1, lchunk, sok);
	    fprintf (stdout, "%s", buffer);
	    fgets (linebuf, LINE, sok);
	    if (all)
		fprintf (stdout, "%s", linebuf);
	    else if (verbose)
		fprintf (stderr, "%s", linebuf);
	    fgets (linebuf, LINE, sok);
	    if (all)
		fprintf (stdout, "%s", linebuf);
	    else if (verbose)
		fprintf (stderr, "%s", linebuf);
	    if (strlen (linebuf) < 1)
		break;
	    lchunk = (int) strtol (linebuf, NULL, 16);
	    if (lchunk < 1)
		break;
	    }
	}

    /* Print result all at once */
    else if (nbcont > 0) {
	buffer = (char *) calloc (1, nbcont);
	if ((red = fread (buffer, 1, nbcont, sok)) > 0)
	    fwrite (buffer, 1, red, stdout);
	}

    /* Print result a line at at time */
    else {
	while ( (red = fread (linebuf, 1, CHUNK, sok)) > 0 ) {
	    fwrite (linebuf, 1, red, stdout);
	    }
	}

    return sok;
}


/* sokFile.c
 */
/* copyright 1991, 1993, 1995, 1999 John B. Roll jr.
 */


#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#else
#include <sys/fcntl.h>
#endif

#include <errno.h>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <netdb.h>

#ifdef BERKLEY
#include <sys/time.h>
#endif

#ifndef NO_XFILE_H
#include "xos.h"
#include "xfile.h"
#endif

#ifdef __STDC__
static int FileINetParse(char *file, int port, struct sockaddr_in *adrinet);
#endif

File SokOpen(name, port, mode)
        char *name;             /* "host:port" socket to open */
        int   port;
        int   mode;             /* mode of socket to open */
{
    int             xfd;        /* socket descriptor */
    int             type;       /* type returned from FileINetParse */
    struct sockaddr_in adrinet; /* socket structure parsed from name */
    int             reuse_addr = 1;


    File            f;          /* returned file descriptor */

    if (!(type = FileINetParse(name, port, &adrinet)))
        return NULL;

    if ( type == 1 
     || (mode & XFCREAT && mode & XFREAD && !(mode & XFWRITE)) ) {
        if ( ((xfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
          ||  setsockopt(xfd, SOL_SOCKET, SO_REUSEADDR,
                     (char *) &reuse_addr, sizeof(reuse_addr)) < 0
          || (bind(xfd, (struct sockaddr *) & adrinet
                         ,sizeof(adrinet)) != 0)
          ||  listen(xfd, 5) ) {
            close(xfd);
            return NULL;
        }
      } else {
        if (((xfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
                   || (connect(xfd, (struct sockaddr *) & adrinet
                               ,sizeof(adrinet)) != 0)) {
            close(xfd);
            return NULL;
        }
    }

    f = OpenFd(xfd, "r+");

    return f;
}


static int FileINetParse(file, port, adrinet)
        char *file;             /* host/socket pair to parse? */
        int   port;
        struct sockaddr_in *adrinet; /* socket info structure to fill? */
{
    struct hostent *hp;         /* -> hostent structure for host */
    char            hostname[MAXHOSTNAMELEN + 12]; /* name of host */
    char           *portstr;    /* internet port number (ascii) */
    int             type = 2;   /* return code */

    char *strchr();

    if ( !strncmp(file, "http://", 7) ) {
        file += 7;
        if ( port == -1 ) port  = 80;
    }

    strcpy(hostname, file);

#ifdef msdos
    /* This is a DOS disk discriptor, not a machine name */
    if ((!(file[0] == '.')) && file[1] == ':')
        return 0;
#endif

    if ( portstr = strchr(hostname, '/') ) {
        *portstr = '\0';
    }

    if ( portstr = strchr(hostname, ':') ) {
        *portstr++ = '\0';

        if ((port = strtol(portstr, NULL, 0)) == 0) {
            struct servent *getservbyname();
            struct servent *service;

            if ((service = getservbyname(portstr, NULL)) == NULL)
                return 0;
            port = service->s_port;
        }
    }

    if ( port == -1 ) return 0;

    if (hostname[0] == '\0')
        type = 1;
    if (hostname[0] == '\0' || hostname[0] == '.')
        if (gethostname(hostname, MAXHOSTNAMELEN) == -1)
            return 0;

    if ((hp = gethostbyname(hostname)) == NULL)
        return 0;

    memset(adrinet, 0, sizeof(struct sockaddr_in));
    adrinet->sin_family = AF_INET;
    adrinet->sin_port = htons(port);
    memcpy(&adrinet->sin_addr, hp->h_addr, hp->h_length);

    return type;
}

static char *iaddrstr(addr, addrlen)
        struct sockaddr_in *addr; /* socket info structure to fill? */
        int     addrlen;
 {
     struct hostent *hp = NULL;
     char           *name;
 
    if (!(hp = gethostbyaddr((char *) &addr->sin_addr, sizeof(addr->sin_addr)
                        , AF_INET))) {
        unsigned char  *a = (unsigned char *) &addr->sin_addr;

        Malloc(name, 32);
        sprintf(name, "%d.%d.%d.%d:%d", a[0], a[1], a[2], a[3]
                , ntohs(addr->sin_port));
    } else {
        Malloc(name, strlen(hp->h_name) + 10);
        sprintf(name, "%s:%d", hp->h_name, ntohs(addr->sin_port));
    }

    return name;
}

/* Dec  4 2000	New program
 *
 * Mar 27 2001	Fix so colons can be in query part of URL for coordinates
 * Mar 27 2001	Add option to read entire contents at once
 * Jul 12 2001	Make line buffer large enough to read a chunk of data
 *
 * Feb 03 2015	Add more diagnostics
 * Feb 06 2015	Add more comments and character translation
 */