Commit | Line | Data |
---|---|---|
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 | 33 | struct 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 |
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) | |
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 | 81 | char *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 | ||
9e9eca79 FT |
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 | ||
41213112 | 103 | void headpreheader(struct hthead *head, const char *name, const char *val) |
33733396 | 104 | { |
41213112 FT |
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); | |
33733396 FT |
111 | } |
112 | ||
41213112 | 113 | void headappheader(struct hthead *head, const char *name, const char *val) |
33733396 FT |
114 | { |
115 | int i; | |
116 | ||
41213112 FT |
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 | ||
af34331c | 124 | int sendreq(int sock, struct hthead *req, int fd) |
41213112 FT |
125 | { |
126 | int ret, i; | |
41213112 FT |
127 | struct charbuf buf; |
128 | ||
41213112 FT |
129 | bufinit(buf); |
130 | bufcatstr2(buf, req->method); | |
131 | bufcatstr2(buf, req->url); | |
132 | bufcatstr2(buf, req->ver); | |
133 | bufcatstr2(buf, req->rest); | |
134 | for(i = 0; i < req->noheaders; i++) { | |
135 | bufcatstr2(buf, req->headers[i][0]); | |
136 | bufcatstr2(buf, req->headers[i][1]); | |
137 | } | |
138 | bufcatstr2(buf, ""); | |
af34331c | 139 | ret = sendfd(sock, fd, buf.b, buf.d); |
41213112 | 140 | buffree(buf); |
af34331c | 141 | if(ret < 0) |
41213112 | 142 | return(-1); |
af34331c FT |
143 | else |
144 | return(0); | |
41213112 FT |
145 | } |
146 | ||
147 | int recvreq(int sock, struct hthead **reqp) | |
148 | { | |
149 | int fd; | |
150 | struct charbuf buf; | |
151 | char *p; | |
152 | size_t l; | |
153 | char *name, *val; | |
154 | struct hthead *req; | |
155 | ||
156 | if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) { | |
157 | return(-1); | |
158 | } | |
159 | buf.s = buf.d; | |
160 | p = buf.b; | |
161 | l = buf.d; | |
162 | ||
163 | *reqp = omalloc(req); | |
164 | if((req->method = sstrdup(decstr(&p, &l))) == NULL) | |
165 | goto fail; | |
166 | if((req->url = sstrdup(decstr(&p, &l))) == NULL) | |
167 | goto fail; | |
168 | if((req->ver = sstrdup(decstr(&p, &l))) == NULL) | |
169 | goto fail; | |
170 | if((req->rest = sstrdup(decstr(&p, &l))) == NULL) | |
171 | goto fail; | |
172 | ||
173 | while(1) { | |
174 | if(!*(name = decstr(&p, &l))) | |
175 | break; | |
176 | val = decstr(&p, &l); | |
177 | headappheader(req, name, val); | |
178 | } | |
179 | ||
180 | buffree(buf); | |
181 | return(fd); | |
182 | ||
183 | fail: | |
184 | close(fd); | |
185 | freehthead(req); | |
186 | errno = EPROTO; | |
187 | return(-1); | |
33733396 | 188 | } |