Strip the leading slash of the rest string.
[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 #include <unistd.h>
22 #include <sys/socket.h>
23 #include <errno.h>
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 #include <utils.h>
29 #include <log.h>
30 #include <req.h>
31 #include <proc.h>
32
33 struct hthead *mkreq(char *method, char *url, char *ver)
34 {
35     struct hthead *req;
36     
37     omalloc(req);
38     req->method = sstrdup(method);
39     req->url = sstrdup(url);
40     req->ver = sstrdup(ver);
41     req->rest = sstrdup(url);
42     return(req);
43 }
44
45 struct hthead *mkresp(int code, char *msg, char *ver)
46 {
47     struct hthead *resp;
48     
49     omalloc(resp);
50     resp->code = code;
51     resp->msg = sstrdup(msg);
52     resp->ver = sstrdup(ver);
53     return(resp);
54 }
55
56 void freehthead(struct hthead *head)
57 {
58     int i;
59     
60     if(head->method != NULL)
61         free(head->method);
62     if(head->url != NULL)
63         free(head->url);
64     if(head->msg != NULL)
65         free(head->msg);
66     if(head->ver != NULL)
67         free(head->ver);
68     if(head->rest != NULL)
69         free(head->rest);
70     if(head->headers) {
71         for(i = 0; i < head->noheaders; i++) {
72             free(head->headers[i][0]);
73             free(head->headers[i][1]);
74             free(head->headers[i]);
75         }
76         free(head->headers);
77     }
78     free(head);
79 }
80
81 char *getheader(struct hthead *head, char *name)
82 {
83     int i;
84     
85     for(i = 0; i < head->noheaders; i++) {
86         if(!strcasecmp(head->headers[i][0], name))
87             return(head->headers[i][1]);
88     }
89     return(NULL);
90 }
91
92 void replrest(struct hthead *head, char *rest)
93 {
94     char *tmp;
95     
96     /* Do not free the current rest string yet, so that the new one
97      * can a subpart of the old one. */
98     tmp = head->rest;
99     head->rest = sstrdup(rest);
100     free(tmp);
101 }
102
103 void headpreheader(struct hthead *head, const char *name, const char *val)
104 {
105     head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
106     memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
107     head->noheaders++;
108     head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
109     head->headers[0][0] = sstrdup(name);
110     head->headers[0][1] = sstrdup(val);
111 }
112
113 void headappheader(struct hthead *head, const char *name, const char *val)
114 {
115     int i;
116
117     i = head->noheaders++;
118     head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
119     head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
120     head->headers[i][0] = sstrdup(name);
121     head->headers[i][1] = sstrdup(val);
122 }
123
124 int sendreq(int sock, struct hthead *req)
125 {
126     int ret, i;
127     int pfds[2];
128     struct charbuf buf;
129     
130     if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
131         return(-1);
132     bufinit(buf);
133     bufcatstr2(buf, req->method);
134     bufcatstr2(buf, req->url);
135     bufcatstr2(buf, req->ver);
136     bufcatstr2(buf, req->rest);
137     for(i = 0; i < req->noheaders; i++) {
138         bufcatstr2(buf, req->headers[i][0]);
139         bufcatstr2(buf, req->headers[i][1]);
140     }
141     bufcatstr2(buf, "");
142     ret = sendfd(sock, pfds[0], buf.b, buf.d);
143     buffree(buf);
144     close(pfds[0]);
145     if(ret < 0) {
146         close(pfds[1]);
147         return(-1);
148     } else {
149         return(pfds[1]);
150     }
151 }
152
153 int recvreq(int sock, struct hthead **reqp)
154 {
155     int fd;
156     struct charbuf buf;
157     char *p;
158     size_t l;
159     char *name, *val;
160     struct hthead *req;
161     
162     if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
163         return(-1);
164     }
165     buf.s = buf.d;
166     p = buf.b;
167     l = buf.d;
168     
169     *reqp = omalloc(req);
170     if((req->method = sstrdup(decstr(&p, &l))) == NULL)
171         goto fail;
172     if((req->url = sstrdup(decstr(&p, &l))) == NULL)
173         goto fail;
174     if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
175         goto fail;
176     if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
177         goto fail;
178     
179     while(1) {
180         if(!*(name = decstr(&p, &l)))
181             break;
182         val = decstr(&p, &l);
183         headappheader(req, name, val);
184     }
185     
186     buffree(buf);
187     return(fd);
188     
189 fail:
190     close(fd);
191     freehthead(req);
192     errno = EPROTO;
193     return(-1);
194 }