akaros/tests/get_html.c
<<
>>
Prefs
   1/* Copyright (c) 2014 The Regents of the University of California
   2 * Barret Rhoden <brho@cs.berkeley.edu>
   3 * See LICENSE for details.
   4 *
   5 * Simple web client, gets a single page.  Used for debugging mostly. */
   6
   7#include <stdlib.h>
   8#include <stdio.h>
   9#include <string.h>
  10#include <unistd.h>
  11#include <assert.h>
  12#include <iplib/iplib.h>
  13#include <parlib/net.h>
  14
  15/* simple test, gets a single web page.  no url parsing, no timeout detection,
  16 * etc.  pass it the IP addr and page to fetch.
  17 *
  18 * check out http://www.d.umn.edu/~gshute/net/http-script.html for some info. */
  19int main(int argc, char *argv[])
  20{
  21        char *host, *page, *port;
  22        int dfd, ret;
  23        char buf[256];
  24        char addr[256];
  25        host = "146.148.59.222";
  26        page = "files/test.html";
  27        port = "80";
  28
  29        if (argc > 1)
  30                host = argv[1];
  31        if (argc > 2)
  32                port = argv[2];
  33        if (argc > 3)
  34                page = argv[3];
  35
  36        printf("FYI, Usage: %s [HOST [PORT [PAGE]]]\n", argv[0]);
  37
  38        printf("Trying to access http://%s:%s/%s\n", host, port, page);
  39        /* manually making our own addr (no mkaddr, which was racy anyway) */
  40        ret = snprintf(addr, sizeof(addr), "tcp!%s!%s", host, port);
  41        if (snprintf_error(ret, sizeof(addr))) {
  42                perror("Addr string too long");
  43                exit(-1);
  44        }
  45        dfd = dial9(addr, 0, 0, 0, 0);
  46        if (dfd < 0) {
  47                perror("Bad Data FD");
  48                exit(-1);
  49        }
  50        /* short get style */
  51        snprintf(buf, sizeof(buf), "GET /%s\r\nConnection: close\r\n\r\n",
  52                 page);
  53        ret = write(dfd, buf, strlen(buf));
  54        if (ret < 0) {
  55                perror("Write");
  56                exit(-1);
  57        }
  58        /* buf - 1, to leave room for a \0 when we print */
  59        while ((ret = read(dfd, buf, sizeof(buf) - 1)) > 0) {
  60                assert(ret < sizeof(buf));
  61                /* trim to print only what we received */
  62                buf[ret] = 0;
  63                printf("%s", buf);
  64        }
  65}
  66