Added a request library component.
authorFredrik Tolf <fredrik@dolda2000.com>
Thu, 27 Nov 2008 05:41:11 +0000 (06:41 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Thu, 27 Nov 2008 05:41:11 +0000 (06:41 +0100)
lib/Makefile.am
lib/log.c
lib/req.c [new file with mode: 0644]
lib/req.h [new file with mode: 0644]

index fb07224..d234043 100644 (file)
@@ -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
index b87d4f6..81f52ba 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -21,6 +21,9 @@
 #include <stdio.h>
 #include <syslog.h>
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <utils.h>
 
 static int tostderr = 1, tosyslog = 0;
diff --git a/lib/req.c b/lib/req.c
new file mode 100644 (file)
index 0000000..47523f3
--- /dev/null
+++ b/lib/req.c
@@ -0,0 +1,79 @@
+/*
+    ashd - A Sane HTTP Daemon
+    Copyright (C) 2008  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 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 <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <utils.h>
+#include <req.h>
+
+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 (file)
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