Send request data to multiplexer.
[ashd.git] / lib / req.c
CommitLineData
33733396
FT
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>
41213112
FT
21#include <unistd.h>
22#include <sys/socket.h>
23#include <errno.h>
33733396
FT
24
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28#include <utils.h>
41213112 29#include <log.h>
33733396 30#include <req.h>
41213112 31#include <proc.h>
33733396 32
41213112 33struct hthead *mkreq(char *method, char *url, char *ver)
33733396 34{
41213112 35 struct hthead *req;
33733396
FT
36
37 omalloc(req);
38 req->method = sstrdup(method);
39 req->url = sstrdup(url);
40 req->ver = sstrdup(ver);
41213112 41 req->rest = sstrdup(url);
33733396
FT
42 return(req);
43}
44
41213112
FT
45struct 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
56void freehthead(struct hthead *head)
33733396
FT
57{
58 int i;
59
41213112
FT
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]);
33733396 75 }
41213112 76 free(head->headers);
33733396 77 }
41213112 78 free(head);
33733396
FT
79}
80
41213112 81char *getheader(struct hthead *head, char *name)
66987955
FT
82{
83 int i;
84
41213112
FT
85 for(i = 0; i < head->noheaders; i++) {
86 if(!strcasecmp(head->headers[i][0], name))
87 return(head->headers[i][1]);
66987955
FT
88 }
89 return(NULL);
90}
91
41213112 92void headpreheader(struct hthead *head, const char *name, const char *val)
33733396 93{
41213112
FT
94 head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
95 memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
96 head->noheaders++;
97 head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
98 head->headers[0][0] = sstrdup(name);
99 head->headers[0][1] = sstrdup(val);
33733396
FT
100}
101
41213112 102void headappheader(struct hthead *head, const char *name, const char *val)
33733396
FT
103{
104 int i;
105
41213112
FT
106 i = head->noheaders++;
107 head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
108 head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
109 head->headers[i][0] = sstrdup(name);
110 head->headers[i][1] = sstrdup(val);
111}
112
113int sendreq(int sock, struct hthead *req)
114{
115 int ret, i;
116 int pfds[2];
117 struct charbuf buf;
118
8f55ddd7 119 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
41213112
FT
120 return(-1);
121 bufinit(buf);
122 bufcatstr2(buf, req->method);
123 bufcatstr2(buf, req->url);
124 bufcatstr2(buf, req->ver);
125 bufcatstr2(buf, req->rest);
126 for(i = 0; i < req->noheaders; i++) {
127 bufcatstr2(buf, req->headers[i][0]);
128 bufcatstr2(buf, req->headers[i][1]);
129 }
130 bufcatstr2(buf, "");
131 ret = sendfd(sock, pfds[0], buf.b, buf.d);
132 buffree(buf);
133 close(pfds[0]);
134 if(ret < 0) {
135 close(pfds[1]);
136 return(-1);
137 } else {
138 return(pfds[1]);
139 }
140}
141
142int recvreq(int sock, struct hthead **reqp)
143{
144 int fd;
145 struct charbuf buf;
146 char *p;
147 size_t l;
148 char *name, *val;
149 struct hthead *req;
150
151 if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
152 return(-1);
153 }
154 buf.s = buf.d;
155 p = buf.b;
156 l = buf.d;
157
158 *reqp = omalloc(req);
159 if((req->method = sstrdup(decstr(&p, &l))) == NULL)
160 goto fail;
161 if((req->url = sstrdup(decstr(&p, &l))) == NULL)
162 goto fail;
163 if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
164 goto fail;
165 if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
166 goto fail;
167
168 while(1) {
169 if(!*(name = decstr(&p, &l)))
170 break;
171 val = decstr(&p, &l);
172 headappheader(req, name, val);
173 }
174
175 buffree(buf);
176 return(fd);
177
178fail:
179 close(fd);
180 freehthead(req);
181 errno = EPROTO;
182 return(-1);
33733396 183}