diff options
author | Christian Grothoff <christian@grothoff.org> | 2007-06-13 07:00:59 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2007-06-13 07:00:59 +0000 |
commit | d05460508fadfe8bc7370c3e2d5b23facf686e0e (patch) | |
tree | 7407caa9ad2991695eb8e154106fbd912d6bf5ad | |
parent | be3489e477fd43a69124dffd55f39efaa41f6e99 (diff) | |
download | libmicrohttpd-d05460508fadfe8bc7370c3e2d5b23facf686e0e.tar.gz libmicrohttpd-d05460508fadfe8bc7370c3e2d5b23facf686e0e.zip |
making it compile
-rw-r--r-- | src/daemon/daemon.c | 9 | ||||
-rw-r--r-- | src/daemon/internal.h | 6 | ||||
-rw-r--r-- | src/daemon/session.c | 200 |
3 files changed, 133 insertions, 82 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 0ebf21d0..4b743aad 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c | |||
@@ -294,6 +294,15 @@ MHD_cleanup_sessions(struct MHD_Daemon * daemon) { | |||
294 | else | 294 | else |
295 | prev->next = pos->next; | 295 | prev->next = pos->next; |
296 | free(pos->addr); | 296 | free(pos->addr); |
297 | if (pos->url != NULL) | ||
298 | free(pos->url); | ||
299 | if (pos->method != NULL) | ||
300 | free(pos->method); | ||
301 | if (pos->write_buffer != NULL) | ||
302 | free(pos->write_buffer); | ||
303 | if (pos->read_buffer != NULL) | ||
304 | free(pos->read_buffer); | ||
305 | /* FIXME: free headers_received here! */ | ||
297 | /* FIXME: more to free here! */ | 306 | /* FIXME: more to free here! */ |
298 | free(pos); | 307 | free(pos); |
299 | } | 308 | } |
diff --git a/src/daemon/internal.h b/src/daemon/internal.h index 61653d8e..4beff728 100644 --- a/src/daemon/internal.h +++ b/src/daemon/internal.h | |||
@@ -150,8 +150,14 @@ struct MHD_Session { | |||
150 | 150 | ||
151 | struct MHD_Response * response; | 151 | struct MHD_Response * response; |
152 | 152 | ||
153 | /** | ||
154 | * Request method. Should be GET/POST/etc. | ||
155 | */ | ||
153 | char * method; | 156 | char * method; |
154 | 157 | ||
158 | /** | ||
159 | * Requested URL (everything after "GET" only). | ||
160 | */ | ||
155 | char * url; | 161 | char * url; |
156 | 162 | ||
157 | /** | 163 | /** |
diff --git a/src/daemon/session.c b/src/daemon/session.c index fa3864ca..a719c51a 100644 --- a/src/daemon/session.c +++ b/src/daemon/session.c | |||
@@ -142,37 +142,65 @@ MHD_session_get_fdset(struct MHD_Session * session, | |||
142 | return MHD_YES; | 142 | return MHD_YES; |
143 | } | 143 | } |
144 | 144 | ||
145 | |||
146 | |||
147 | /* FIXME: implement/fix code below this line! */ | ||
148 | |||
149 | |||
150 | /** | 145 | /** |
151 | * This function needs to do a lot more (i.e. break up get arguments) | 146 | * Parse a single line of the HTTP header. Remove it |
152 | * but for now just seperates the prefix of the url from the document | 147 | * from the read buffer. If the current line does not |
153 | * portion. | 148 | * fit, consider growing the buffer. If the line is |
149 | * far too long, close the connection. If no line is | ||
150 | * found (incomplete, buffer too small, line too long), | ||
151 | * return NULL. Otherwise return a copy of the line. | ||
154 | */ | 152 | */ |
155 | static void | 153 | static char * |
156 | MHD_parse_URL(struct MHD_Session * session) { | 154 | MHD_get_next_header_line(struct MHD_Session * session) { |
157 | #if 0 | 155 | char * rbuf; |
158 | char * working; | 156 | size_t pos; |
159 | int pos,i; | 157 | size_t start; |
160 | 158 | ||
161 | working = session->headers[0]->value; | 159 | start = 0; |
162 | 160 | pos = 0; | |
163 | pos = 0; | 161 | rbuf = session->read_buffer; |
164 | for(i = 0; i < strlen(working); i++) { | 162 | while ( (pos < session->readLoc - 1) && |
165 | if(working[i] == '/') | 163 | (rbuf[pos] != '\r') && |
166 | pos = i+1; | 164 | (rbuf[pos] != '\n') ) |
167 | } | 165 | pos++; |
168 | if(pos >= strlen(working)) | 166 | if (pos == session->readLoc) { |
169 | pos = 0; | 167 | /* not found, consider growing... */ |
170 | 168 | if (session->readLoc == session->read_buffer_size) { | |
171 | session->documentName = session->headers[0]->value+pos; | 169 | /* grow buffer to read larger header or die... */ |
172 | #endif | 170 | if (session->read_buffer_size < 4 * MHD_MAX_BUF_SIZE) { |
171 | rbuf = malloc(session->read_buffer_size * 2); | ||
172 | memcpy(rbuf, | ||
173 | session->read_buffer, | ||
174 | session->readLoc); | ||
175 | free(session->read_buffer); | ||
176 | session->read_buffer = rbuf; | ||
177 | session->read_buffer_size *= 2; | ||
178 | } else { | ||
179 | /* die, header far too long to be reasonable */ | ||
180 | /* FIXME: log */ | ||
181 | close(session->socket_fd); | ||
182 | session->socket_fd = -1; | ||
183 | } | ||
184 | } | ||
185 | return NULL; | ||
186 | } | ||
187 | /* found, check if we have proper CRLF */ | ||
188 | rbuf = malloc(pos); | ||
189 | memcpy(rbuf, | ||
190 | session->read_buffer, | ||
191 | pos-1); | ||
192 | rbuf[pos-1] = '\0'; | ||
193 | if ( (rbuf[pos] == '\r') && | ||
194 | (rbuf[pos+1] == '\n') ) | ||
195 | pos++; /* skip both r and n */ | ||
196 | pos++; | ||
197 | memmove(session->read_buffer, | ||
198 | &session->read_buffer[pos], | ||
199 | session->readLoc - pos); | ||
200 | session->readLoc -= pos; | ||
201 | return rbuf; | ||
173 | } | 202 | } |
174 | 203 | ||
175 | |||
176 | /** | 204 | /** |
177 | * This function is designed to parse the input buffer of a given session. | 205 | * This function is designed to parse the input buffer of a given session. |
178 | * | 206 | * |
@@ -185,62 +213,66 @@ MHD_parse_URL(struct MHD_Session * session) { | |||
185 | */ | 213 | */ |
186 | static void | 214 | static void |
187 | MHD_parse_session_headers(struct MHD_Session * session) { | 215 | MHD_parse_session_headers(struct MHD_Session * session) { |
188 | #if 0 | 216 | char * line; |
189 | const char * crlfcrlf = "\r\n\r\n"; | 217 | char * colon; |
190 | const char * crlf = "\r\n"; | 218 | char * uri; |
191 | 219 | char * httpType; | |
192 | char * saveptr; | 220 | struct MHD_HTTP_Header * hdr; |
193 | char * saveptr1; | ||
194 | |||
195 | struct MHD_HTTP_Header * newHeader; | ||
196 | char * curTok; | ||
197 | char * curTok1; | ||
198 | 221 | ||
199 | int numBytes; | 222 | if (session->bodyReceived == 1) |
200 | 223 | abort(); | |
201 | curTok = strstr(session->inbuf, crlfcrlf); | 224 | while (NULL != (line = MHD_get_next_header_line(session))) { |
202 | 225 | if (session->url == NULL) { | |
203 | if(curTok == NULL) { | 226 | /* line must be request line */ |
204 | return -1; | 227 | uri = strstr(line, " "); |
205 | } | 228 | if (uri == NULL) |
206 | 229 | goto DIE; | |
207 | memset(curTok+2, 0, 2); | 230 | uri[0] = '\0'; |
208 | 231 | session->method = strdup(line); | |
209 | numBytes = strlen(session->inbuf) + 2; | 232 | uri++; |
210 | 233 | httpType = strstr(uri, " "); | |
211 | curTok = strtok_r(session->inbuf, crlf, &saveptr); | 234 | if (httpType != NULL) |
212 | 235 | httpType[0] = '\0'; | |
213 | session->requestType = strtok_r(curTok, " ", &saveptr1); | 236 | session->url = strdup(uri); |
214 | 237 | /* do we want to do anything with httpType? */ | |
215 | newHeader = (struct MHD_HTTP_Header *)malloc(sizeof(struct MHD_HTTP_Header)); | 238 | free(line); |
216 | if(newHeader == NULL) { | 239 | continue; |
217 | if(session->daemon->options & MHD_USE_DEBUG) | 240 | } |
218 | fprintf(stderr, "Error allocating memory!\n"); | 241 | /* check if this is the end of the header */ |
219 | return -1; | 242 | if (strlen(line) == 0) { |
220 | } | 243 | /* end of header */ |
221 | newHeader->kind = MHD_GET_ARGUMENT_KIND; | 244 | session->headersReceived = 1; |
222 | newHeader->header = session->requestType; | 245 | /* FIXME: check with methods may have a body, |
223 | newHeader->value = strtok_r(NULL, " ", &saveptr1); | 246 | check headers to find out upload size */ |
224 | 247 | session->uploadSize = 0; | |
225 | session->headers[session->firstFreeHeader++] = newHeader; | 248 | session->bodyReceived = 1; |
226 | 249 | free(line); | |
227 | curTok = strtok_r(NULL, crlf, &saveptr); | 250 | break; |
228 | while(curTok != NULL && session->firstFreeHeader < MHD_MAX_HEADERS) { | 251 | } |
229 | curTok1 = strtok_r(curTok, ":", &saveptr1); | 252 | /* ok, line should be normal header line, find colon */ |
230 | newHeader = (struct MHD_HTTP_Header *)malloc(sizeof(struct MHD_HTTP_Header)); | 253 | colon = strstr(line, ": "); |
231 | if(newHeader == NULL) { | 254 | if (colon == NULL) { |
232 | if(session->daemon->options & MHD_USE_DEBUG) | 255 | /* error in header line, die hard */ |
233 | fprintf(stderr, "Error allocating memory!\n"); | 256 | /* FIXME: log */ |
234 | return -1; | 257 | goto DIE; |
235 | } | 258 | } |
236 | newHeader->header = curTok1; | 259 | /* zero-terminate header */ |
237 | newHeader->value = curTok + strlen(curTok1) + 2; | 260 | colon[0] = '\0'; |
238 | //For now, everything is a get! | 261 | colon += 2; /* advance to value */ |
239 | newHeader->kind = MHD_GET_ARGUMENT_KIND; | 262 | hdr = malloc(sizeof(struct MHD_HTTP_Header)); |
240 | session->headers[session->firstFreeHeader++] = newHeader; | 263 | hdr->next = session->headers_received; |
241 | curTok = strtok_r(NULL, crlf, &saveptr); | 264 | hdr->header = line; |
242 | } | 265 | hdr->value = strdup(colon); |
243 | #endif | 266 | hdr->kind = MHD_HEADER_KIND; |
267 | session->headers_received = hdr; | ||
268 | } | ||
269 | if (session->bodyReceived == 0) | ||
270 | return; | ||
271 | /* FIXME: here: find cookie header and parse that! */ | ||
272 | return; | ||
273 | DIE: | ||
274 | close(session->socket_fd); | ||
275 | session->socket_fd = -1; | ||
244 | } | 276 | } |
245 | 277 | ||
246 | 278 | ||
@@ -463,6 +495,10 @@ MHD_session_handle_write(struct MHD_Session * session) { | |||
463 | session->headersSent = 0; | 495 | session->headersSent = 0; |
464 | session->bodyReceived = 0; | 496 | session->bodyReceived = 0; |
465 | session->messagePos = 0; | 497 | session->messagePos = 0; |
498 | free(session->method); | ||
499 | session->method = NULL; | ||
500 | free(session->url); | ||
501 | session->url = NULL; | ||
466 | free(session->write_buffer); | 502 | free(session->write_buffer); |
467 | session->write_buffer = NULL; | 503 | session->write_buffer = NULL; |
468 | session->write_buffer_size = 0; | 504 | session->write_buffer_size = 0; |