Subversion Repositories SvarDOS

Rev

Rev 1131 | Rev 1501 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1131 Rev 1132
Line 244... Line 244...
244
 * write result to file outfname if not null, or print to stdout otherwise
244
 * write result to file outfname if not null, or print to stdout otherwise
245
 * fills bsum with the BSD sum of the data
245
 * fills bsum with the BSD sum of the data
246
 * is ispost is non-zero, then the request is a POST and its body data is
246
 * is ispost is non-zero, then the request is a POST and its body data is
247
 * obtained through repeated calls to checkupdata()
247
 * obtained through repeated calls to checkupdata()
248
 * returns the length of data obtained, or neg value on error */
248
 * returns the length of data obtained, or neg value on error */
249
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum, int ispost, unsigned char *buffer, size_t buffersz) {
249
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum, int ispost, unsigned char *buffer, size_t buffersz, unsigned short tcpbuflen) {
250
  struct net_tcpsocket *sock;
250
  struct net_tcpsocket *sock;
251
  time_t lastactivity, lastprogressoutput = 0;
251
  time_t lastactivity, lastprogressoutput = 0;
252
  int httpcode = -1, ischunked = 0;
252
  int httpcode = -1, ischunked = 0;
253
  int byteread;
253
  int byteread;
254
  long flen = 0, lastflen = 0;
254
  long flen = 0, lastflen = 0;
Line 258... Line 258...
258
  struct unchunk_state *unchstate = (void *)buffer;
258
  struct unchunk_state *unchstate = (void *)buffer;
259
  buffer += sizeof(*unchstate);
259
  buffer += sizeof(*unchstate);
260
  buffersz -= sizeof(*unchstate);
260
  buffersz -= sizeof(*unchstate);
261
  memset(unchstate, 0, sizeof(*unchstate));
261
  memset(unchstate, 0, sizeof(*unchstate));
262
 
262
 
263
  sock = net_connect(ipaddr, 80);
263
  sock = net_connect(ipaddr, 80, tcpbuflen);
264
  if (sock == NULL) {
264
  if (sock == NULL) {
265
    printf(svarlang_strid(0x0902), HOSTADDR); /* "ERROR: failed to connect to " HOSTADDR */
265
    printf(svarlang_strid(0x0902), HOSTADDR); /* "ERROR: failed to connect to " HOSTADDR */
266
    puts("");
266
    puts("");
267
    goto SHITQUIT;
267
    goto SHITQUIT;
268
  }
268
  }
Line 411... Line 411...
411
 
411
 
412
 
412
 
413
int main(int argc, char **argv) {
413
int main(int argc, char **argv) {
414
  unsigned short bsum = 0;
414
  unsigned short bsum = 0;
415
  long flen;
415
  long flen;
416
  size_t membufsz = 5000;
416
  unsigned short tcpbufsz = (16 * 1024);
417
  int ispost; /* is the request a POST? */
417
  int ispost; /* is the request a POST? */
418
 
418
 
419
  struct {
419
  struct {
-
 
420
    unsigned char buffer[5000];
420
    char ipaddr[64];
421
    char ipaddr[64];
421
    char url[64];
422
    char url[64];
422
    char outfname[16];
423
    char outfname[16];
423
    unsigned char buffer[1];
-
 
424
  } *mem;
424
  } *mem;
425
 
425
 
426
  svarlang_autoload("PKGNET");
426
  svarlang_autoload("PKGNET");
427
 
427
 
428
  /* look for PKGNETBUFSZ env var to size up the buffer (default=5000 bytes) */
428
  /* look for PKGNETBUFSZ env var to size up the buffer (default=5000 bytes) */
429
  {
429
  {
430
    const char *ptr = getenv("PKGNETBUFSZ");
430
    const char *ptr = getenv("PKGNETBUFSZ");
431
    if (ptr != NULL) {
431
    if (ptr != NULL) {
432
      long newsz = atol(ptr);
432
      long newsz = atol(ptr);
433
      if ((newsz > 0) && (newsz < 65500)) {
433
      if ((newsz >= 0) && (newsz < 65500)) {
434
        membufsz = newsz;
434
        tcpbufsz = newsz;
435
        printf("WILL USE CUSTOM RECV BUFF SIZE = %u\r\n", membufsz);
435
        printf("WILL USE CUSTOM TCP BUFF SIZE = %u\r\n", tcpbufsz);
436
      }
436
      }
437
    }
437
    }
438
  }
438
  }
439
 
439
 
440
  /* allocate memory */
440
  /* allocate memory */
441
  mem = malloc(sizeof(*mem) + membufsz);
441
  mem = malloc(sizeof(*mem));
442
  if (mem == NULL) {
442
  if (mem == NULL) {
443
    putsnls(9, 9); /* "ERROR: out of memory" */
443
    putsnls(9, 9); /* "ERROR: out of memory" */
444
    return(1);
444
    return(1);
445
  }
445
  }
446
 
446
 
Line 465... Line 465...
465
  if (net_dnsresolve(mem->ipaddr, HOSTADDR, 2) != 0) {
465
  if (net_dnsresolve(mem->ipaddr, HOSTADDR, 2) != 0) {
466
    putsnls(9, 12); /* "ERROR: DNS resolution failed" */
466
    putsnls(9, 12); /* "ERROR: DNS resolution failed" */
467
    return(1);
467
    return(1);
468
  }
468
  }
469
 
469
 
470
  flen = htget(mem->ipaddr, mem->url, mem->outfname, &bsum, ispost, mem->buffer, membufsz);
470
  flen = htget(mem->ipaddr, mem->url, mem->outfname, &bsum, ispost, mem->buffer, sizeof(mem->buffer), tcpbufsz);
471
  if (flen < 1) return(1);
471
  if (flen < 1) return(1);
472
 
472
 
473
  if (mem->outfname[0] != 0) {
473
  if (mem->outfname[0] != 0) {
474
    /* print bsum, size, filename */
474
    /* print bsum, size, filename */
475
    printf(svarlang_strid(0x0200), flen >> 10, mem->outfname, bsum); /* Downloaded %ld KiB into %s (BSUM: %04X) */
475
    printf(svarlang_strid(0x0200), flen >> 10, mem->outfname, bsum); /* Downloaded %ld KiB into %s (BSUM: %04X) */