Added test program for the HTTP library
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 22 Jul 2007 01:57:56 +0000 (03:57 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 22 Jul 2007 01:57:56 +0000 (03:57 +0200)
common/.gitignore [new file with mode: 0644]
common/Makefile.am
common/httest.c [new file with mode: 0644]

diff --git a/common/.gitignore b/common/.gitignore
new file mode 100644 (file)
index 0000000..0c44c25
--- /dev/null
@@ -0,0 +1 @@
+/httest
index 45c8390..8030f44 100644 (file)
@@ -1,12 +1,16 @@
 EXTRA_DIST = makegdesc
 
 noinst_LIBRARIES = libcommon.a libhttp.a
+noinst_PROGRAMS = httest
 
 libcommon_a_SOURCES =  tiger.c \
                        utils.c
 
 libhttp_a_SOURCES =    http.c
 
+httest_SOURCES =       httest.c
+httest_LDADD =         libhttp.a libcommon.a
+
 libcommon_a_CFLAGS = -fPIC
 libhttp_a_CFLAGS = -fPIC
 
diff --git a/common/httest.c b/common/httest.c
new file mode 100644 (file)
index 0000000..d04c7a2
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+ *  Dolda Connect - Modular multiuser Direct Connect-style client
+ *  Copyright (C) 2007 Fredrik Tolf (fredrik@dolda2000.com)
+ *  
+ *  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 for more details.
+ *  
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/poll.h>
+#include <errno.h>
+
+#include <utils.h>
+#include <http.h>
+
+void parse(char *url)
+{
+    struct hturlinfo *u;
+    
+    if((u = parseurl(url)) == NULL) {
+       fprintf(stderr, "httest: %s: invalid url\n", url);
+       return;
+    }
+    printf("host: %s\n", u->host);
+    printf("port: %i\n", u->port);
+    printf("path: %s\n", u->path);
+    printf("query: %s\n", u->query);
+    printf("fragment: %s\n", u->fragment);
+    freeurl(u);
+}
+
+void head(char *url)
+{
+    struct hturlinfo *u;
+    struct htconn *c;
+    struct pollfd pfd;
+    struct strpair *p;
+    int ret;
+    
+    if((u = parseurl(url)) == NULL) {
+       fprintf(stderr, "httest: %s: invalid url\n", url);
+       return;
+    }
+    c = htconnect(u);
+    freeurl(u);
+    while(1) {
+       pfd.fd = c->fd;
+       pfd.events = htpollflags(c);
+       if(poll(&pfd, 1, -1) < 0) {
+           fprintf(stderr, "httest: %s: %s\n", url, strerror(errno));
+           freehtconn(c);
+           return;
+       }
+       if((ret = htprocess(c, pfd.revents)) < 0) {
+           fprintf(stderr, "httest: %s: %s\n", url, strerror(errno));
+           freehtconn(c);
+           return;
+       }
+       c->databufdata = 0;
+       if(ret)
+           break;
+    }
+    printf("%i %s\n", c->rescode, c->resstr);
+    for(p = c->headers; p != NULL; p = p->next)
+       printf("%s: %s\n", p->key, p->val);
+    freehtconn(c);
+}
+
+void get(char *url)
+{
+    struct hturlinfo *u;
+    struct htconn *c;
+    struct pollfd pfd;
+    struct strpair *p;
+    int ret, ret2;
+    
+    if((u = parseurl(url)) == NULL) {
+       fprintf(stderr, "httest: %s: invalid url\n", url);
+       return;
+    }
+    c = htconnect(u);
+    freeurl(u);
+    while(1) {
+       pfd.fd = c->fd;
+       pfd.events = htpollflags(c);
+       if(poll(&pfd, 1, -1) < 0) {
+           fprintf(stderr, "httest: %s: %s\n", url, strerror(errno));
+           freehtconn(c);
+           return;
+       }
+       if((ret = htprocess(c, pfd.revents)) < 0) {
+           fprintf(stderr, "httest: %s: %s\n", url, strerror(errno));
+           freehtconn(c);
+           return;
+       }
+       while(c->databufdata > 0) {
+           ret2 = write(1, c->databuf, c->databufdata);
+           memmove(c->databuf, c->databuf + ret2, c->databufdata -= ret2);
+       }
+       if(ret)
+           break;
+    }
+    printf("%i %s\n", c->rescode, c->resstr);
+    for(p = c->headers; p != NULL; p = p->next)
+       printf("%s: %s\n", p->key, p->val);
+    freehtconn(c);
+}
+
+int main(int argc, char **argv) {
+    int i;
+    
+    for(i = 1; i < argc; i++) {
+       if(!strcmp(argv[i], "parse")) {
+           parse(argv[++i]);
+       } else if(!strcmp(argv[i], "head")) {
+           head(argv[++i]);
+       } else if(!strcmp(argv[i], "get")) {
+           get(argv[++i]);
+       }
+    }
+    return(0);
+}