X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=common%2Fhttest.c;fp=common%2Fhttest.c;h=d04c7a2c88b499016499b8ee77dfabb3a8cf9821;hb=de760c32656215930e4fc83830a5bdedbb185d73;hp=0000000000000000000000000000000000000000;hpb=2b6703ac37a2c0f17923535d68fdf0ac2debedea;p=doldaconnect.git diff --git a/common/httest.c b/common/httest.c new file mode 100644 index 0000000..d04c7a2 --- /dev/null +++ b/common/httest.c @@ -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 +#include +#include +#include +#include +#include + +#include +#include + +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); +}