From 337333968e3d06cf5f1fa5198eeba3121aee467a Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Thu, 27 Nov 2008 06:41:11 +0100 Subject: [PATCH] Added a request library component. --- lib/Makefile.am | 2 +- lib/log.c | 3 +++ lib/req.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/req.h | 16 ++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 lib/req.c create mode 100644 lib/req.h diff --git a/lib/Makefile.am b/lib/Makefile.am index fb07224..d234043 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,4 +1,4 @@ noinst_LIBRARIES = libht.a -libht_a_SOURCES = utils.c mt.c log.c +libht_a_SOURCES = utils.c mt.c log.c req.c libht_a_CFLAGS = -fPIC diff --git a/lib/log.c b/lib/log.c index b87d4f6..81f52ba 100644 --- a/lib/log.c +++ b/lib/log.c @@ -21,6 +21,9 @@ #include #include +#ifdef HAVE_CONFIG_H +#include +#endif #include static int tostderr = 1, tosyslog = 0; diff --git a/lib/req.c b/lib/req.c new file mode 100644 index 0000000..47523f3 --- /dev/null +++ b/lib/req.c @@ -0,0 +1,79 @@ +/* + ashd - A Sane HTTP Daemon + Copyright (C) 2008 Fredrik Tolf + + 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 3 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, see . +*/ + +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif +#include +#include + +struct htreq *mkreq(char *method, char *url, char *ver) +{ + struct htreq *req; + + omalloc(req); + req->method = sstrdup(method); + req->url = sstrdup(url); + req->ver = sstrdup(ver); + req->restbuf = sstrdup(url); + req->rest = req->restbuf; + return(req); +} + +void freereq(struct htreq *req) +{ + int i; + + free(req->method); + free(req->url); + free(req->ver); + free(req->restbuf); + if(req->headers) { + for(i = 0; i < req->noheaders; i++) { + free(req->headers[i][0]); + free(req->headers[i][1]); + free(req->headers[i]); + } + free(req->headers); + } + free(req); +} + +void reqpreheader(struct htreq *req, char *name, char *val) +{ + req->headers = srealloc(req->headers, sizeof(*req->headers) * (req->noheaders + 1)); + memmove(req->headers + 1, req->headers, sizeof(*req->headers) * req->noheaders); + req->noheaders++; + req->headers[0] = smalloc(sizeof(*req->headers[0]) * 2); + req->headers[0][0] = sstrdup(name); + req->headers[0][1] = sstrdup(val); +} + +void reqappheader(struct htreq *req, char *name, char *val) +{ + int i; + + i = req->noheaders++; + req->headers = srealloc(req->headers, sizeof(*req->headers) * req->noheaders); + req->headers[i] = smalloc(sizeof(*req->headers[i]) * 2); + req->headers[i][0] = sstrdup(name); + req->headers[i][1] = sstrdup(val); +} diff --git a/lib/req.h b/lib/req.h new file mode 100644 index 0000000..b566dd2 --- /dev/null +++ b/lib/req.h @@ -0,0 +1,16 @@ +#ifndef _LIB_HTREQ_H +#define _LIB_HTREQ_H + +struct htreq { + char *method, *url, *ver; + char *restbuf, *rest; + char ***headers; + int noheaders; +}; + +struct htreq *mkreq(char *method, char *url, char *ver); +void freereq(struct htreq *req); +void reqpreheader(struct htreq *req, char *name, char *val); +void reqappheader(struct htreq *req, char *name, char *val); + +#endif -- 2.11.0