File: urlistream.C

package info (click to toggle)
html2text 1.3.2a-28
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 2,008 kB
  • sloc: cpp: 9,605; yacc: 830; sh: 226; makefile: 114
file content (399 lines) | stat: -rw-r--r-- 9,658 bytes parent folder | download | duplicates (7)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399

 /***************************************************************************/

/*
 * Portions Copyright (c) 1999 GMRS Software GmbH
 * Carl-von-Linde-Str. 38, D-85716 Unterschleissheim, http://www.gmrs.de
 * All rights reserved.
 *
 * Author: Arno Unkrig <arno@unkrig.de>
 */
 
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License in the file COPYING for more details.
 */

 /***************************************************************************/

/*
 * Changes to version 1.2.2 were made by Martin Bayer <mbayer@zedat.fu-berlin.de>
 * Dates and reasons of modifications:
 * Thu Oct  4 21:49:09 CEST 2001: ported to g++ 3.0
 * Sun Apr  7 11:59:03 CEST 2002: Handle URLs with missing node
 * Mon Jul 22 13:53:02 CEST 2002: Made finaly reading from STDIN work.
 */
  
 /***************************************************************************/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef SYS_POLL_MISSING /* { */
struct pollfd {
  int fd;           /* file descriptor */
  short events;     /* requested events */
  short revents;    /* returned events */
};
extern "C" int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
#define POLLIN      0x0001    /* There is data to read */
#define POLLPRI     0x0002    /* There is urgent data to read */
#define POLLOUT     0x0004    /* Writing now will not block */
#define POLLERR     0x0008    /* Error condition */
#define POLLHUP     0x0010    /* Hung up */
#define POLLNVAL    0x0020    /* Invalid request: fd not open */
#else /* } { */
#include <sys/poll.h>
#endif /* } */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>

#include "urlistream.h"

using std::ios;

/* ------------------------------------------------------------------------- */

/*
 * Compile with
 *
 *     CC -DTESTING urlistream.C -o urlcat
 */

/* ------------------------------------------------------------------------- */

void
urlistream::open(
  const char *url,
  int        timeout /* = default_timeout */  // Milliseconds
)
{
  if (is_open()) close();

  /*
   * "http:" <address> URL?
   */
  if (!memcmp(url, "http:", 5)) {
    http_open(url + 5, timeout);
    return;
  }

  /*
   * "file:" <file-name> URL?
   */
  if (!memcmp(url, "file:", 5)) {
    file_open(url + 5);
    return;
  }

  /*
   * is the URL a bare file name?
   */
  if (strchr(url, ':') == NULL) {
    file_open(url);
    return;
  }

  open_error_ = "Unknown protocol (only \"file:\" and \"http:\" allowed)";
}

void
urlistream::open(
  const string &url,
  int          timeout /* = default_timeout */  // Milliseconds
)
{
  open(url.c_str(), timeout);
}

/* ------------------------------------------------------------------------- */

void
urlistream::http_open(
  const char *address,    // The URL portion after "http:"
  int        timeout      // Milliseconds
)
{

  /*
   * Break up the HTTP address:
   *
   *   "//" <host> [ ":" <port> ] <node>
   *
   * A missing node is interpreted as node "/" - Arno
   */
  char host_name[100];
  char port_name[100];
  char node_name[1000];
  {
    const char *p = address;
    char       *q;

    if (*p++ != '/' || *p++ != '/') {
      open_error_ = "HTTP address does not begin with \"//\"";
      return;
    }

    for (q = host_name; *p && *p != ':' && *p != '/'; ++p) {
      if (q < host_name + sizeof(host_name) - 1) *q++ = *p;
    }
    *q = '\0';

    if (*p == ':') {
      ++p;
      for (q = port_name; *p && *p != '/'; ++p) {
	if (q < port_name + sizeof(port_name) - 1) *q++ = *p;
      }
      *q = '\0';
    } else {
      strcpy(port_name, "80");
    }

    for (q = node_name; *p && *p != '#'; ++p) {
      if (q < node_name + sizeof(node_name) - 1) *q++ = *p;
    }
    *q = '\0';
    if (!node_name[0]) {
      strcpy(node_name, "/");
    }
  }

  struct sockaddr_in soc_address;
  soc_address.sin_family = AF_INET;

  /*
   * Parse the host name.
   */
  {
    const char *p;
    int dot_count = 0;
    for (p = host_name; *p; ++p) {
      if (*p == '.') { ++dot_count; } else if (!isdigit(*p)) break;
    }
    if (*p == '\0' && dot_count == 3) {
      soc_address.sin_addr.s_addr = inet_addr(host_name);
    } else {
      struct hostent *h = gethostbyname(host_name);
      if (
        h == 0 ||
        h->h_addrtype != AF_INET ||
        h->h_length != sizeof(struct in_addr)
      ) {
	open_error_ = "Could not resolve host name";
	return;
      }
      soc_address.sin_addr = *(struct in_addr *) h->h_addr;
    }
  }

  /*
   * Parse the port name.
   */
  if (isdigit(port_name[0])) {
    soc_address.sin_port = htons(atoi(port_name));
  } else {
    struct servent *s = getservbyname(port_name, 0);
    soc_address.sin_port = htons(s ? s->s_port : 80);
  }

  /*
   * Strip the "#anchor" suffix from the node name.
   */
  { char *p = strchr(node_name, '#'); if (p) *p = '\0'; }

  /*
   * On-the-fly definition of "FileHandle" which closes a UNIX file descriptor
   * on destruction.
   */
  class FileHandle {
  public:
    FileHandle() : fd(-1) {}
    ~FileHandle() { if (fd != -1) ::close(fd); }
    operator int() { return fd; }
    int operator=(int x) { return (fd = x); }
  private:
    int fd;
  } fd;

  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (fd == -1) {
    open_error_ = strerror(errno);
    return;
  }

  /*
   * Make the socket non-blocking, so the "connect()" can be canceled. This
   * means that when we issue the "connect()" we should NOT have to wait for
   * the accept on the other end.
   */
  if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
    open_error_ = strerror(errno);
    return;
  }

  /*
   * Issue the "connect()". Since the server can't do an instantaneous
   * "accept()" and we are non-blocking, this will almost certainly return
   * a negative status.
   */
  if (connect(
    fd,
    (struct sockaddr *) &soc_address, sizeof(soc_address)
  ) == -1) {
    if (errno != EINPROGRESS && errno != EAGAIN) {
      open_error_ = strerror(errno);
      return;
    }

    for (;;) {
      struct pollfd p;
      int           res;

      p.fd     = fd;
      p.events = POLLOUT;
      res = poll(&p, 1, timeout);

      /*
       * Interrupted "poll()"?
       */
      if (res == -1 && errno == EINTR) continue;

      /*
       * Check for errors.
       */
      if (res == -1 && errno != EALREADY) {
        open_error_ = strerror(errno);
	return;
      }

      /*
       * Check for timeout.
       */
      if (res == 0) {
	open_error_ = "\"connect()\" timed out";
	return;
      }

      /*
       * Extra check here for connection success, if we try to
       * connect again, and get EISCONN, it means we have a
       * successful connection.
       *
       * Notice: On SINIX 5.43 B2000, the "poll()" returns "1" when the
       * timeout occurs (!?). When we call "poll()", we encounter a SIGPIPE
       * (!?). If we ignore it, "poll()" returns EINVAL (22) (!?).
       */
      void (*sigpipe_handler)(int) = signal(SIGPIPE, SIG_IGN); /* { */
      res = connect(fd, (struct sockaddr *) &soc_address, sizeof(soc_address));
      (void) signal(SIGPIPE, sigpipe_handler); /* } */

      if (res == -1 && errno == EISCONN) break;
      if (res == -1 && errno != EALREADY) {
        open_error_ = strerror(errno);
	return;
      }
    }
  }

  /*
   * Make the socket blocking again on good "connect()".
   */
  if (fcntl(fd, F_SETFL, 0) == -1) {
    open_error_ = strerror(errno);
    return;
  }

  /*
   * Issue the HTTP request.
   *
   * Notice: "GET xyz" means "return the document without a header".
   */
  char command[4 + (sizeof(node_name) - 1) + 4 + 1];
  sprintf(command, "GET %s\r\n\r\n", node_name);
  ssize_t command_length = strlen(command);

  if (::write(fd, command, command_length) != command_length) {
    open_error_ = "Error sending HTTP GET request";
    return;
  }

  /*
   * Attach the file descriptor to the ifstream.
   */
  fd_ = fd;

  fd = -1;    // ...so that it is not implicitly "::close()"'d.
}

/* ------------------------------------------------------------------------- */

void
urlistream::file_open(const char *file_name)
{
  fd_ = !strcmp(file_name, "-") ? ::dup(0) : ::open(file_name, O_RDONLY);
  open_error_ = strerror(errno);
}

/* ------------------------------------------------------------------------- */

const char *
urlistream::open_error() const
{
  return open_error_ ? open_error_ : "No error";
}

/* ------------------------------------------------------------------------- */

int
urlistream::get()
{
  char ch;
  int ret = ::read(fd_, &ch, 1);
  return (ret > 0 ? ch : -1);
}

#ifdef TESTING /* { */

int
main(int argc, char **argv)
{
  if (argc < 2) {
    cerr << "Usage:  urlcat <url> [ ... ]" << endl;
    exit(1);
  }

  for (int i = 1; i < argc; ++i) {
    urlistream uis(argv[i]);
    if (!uis.is_open()) {
      cerr << "Opening \"" << argv[i] << "\": " << uis.open_error() << endl;
      exit(1);
    }

    for (;;) {
      int c = uis.get();
      if (c == EOF) break;
      cout << (char) c;
    }
  }

  return 0;
}

#endif /* } */

/* ------------------------------------------------------------------------- */