Added a request library component.
[ashd.git] / lib / req.c
1 /*
2     ashd - A Sane HTTP Daemon
3     Copyright (C) 2008  Fredrik Tolf <fredrik@dolda2000.com>
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <utils.h>
26 #include <req.h>
27
28 struct htreq *mkreq(char *method, char *url, char *ver)
29 {
30     struct htreq *req;
31     
32     omalloc(req);
33     req->method = sstrdup(method);
34     req->url = sstrdup(url);
35     req->ver = sstrdup(ver);
36     req->restbuf = sstrdup(url);
37     req->rest = req->restbuf;
38     return(req);
39 }
40
41 void freereq(struct htreq *req)
42 {
43     int i;
44     
45     free(req->method);
46     free(req->url);
47     free(req->ver);
48     free(req->restbuf);
49     if(req->headers) {
50         for(i = 0; i < req->noheaders; i++) {
51             free(req->headers[i][0]);
52             free(req->headers[i][1]);
53             free(req->headers[i]);
54         }
55         free(req->headers);
56     }
57     free(req);
58 }
59
60 void reqpreheader(struct htreq *req, char *name, char *val)
61 {
62     req->headers = srealloc(req->headers, sizeof(*req->headers) * (req->noheaders + 1));
63     memmove(req->headers + 1, req->headers, sizeof(*req->headers) * req->noheaders);
64     req->noheaders++;
65     req->headers[0] = smalloc(sizeof(*req->headers[0]) * 2);
66     req->headers[0][0] = sstrdup(name);
67     req->headers[0][1] = sstrdup(val);
68 }
69
70 void reqappheader(struct htreq *req, char *name, char *val)
71 {
72     int i;
73
74     i = req->noheaders++;
75     req->headers = srealloc(req->headers, sizeof(*req->headers) * req->noheaders);
76     req->headers[i] = smalloc(sizeof(*req->headers[i]) * 2);
77     req->headers[i][0] = sstrdup(name);
78     req->headers[i][1] = sstrdup(val);
79 }