Refactoring mk_request_error()
[MonkeyD.git] / src / method.c
blob066b1def52b0fe9a4b59b513a03bfe30dae69dba
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/socket.h>
26 #include <sys/wait.h>
27 #include <netinet/in.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <time.h>
33 #include "monkey.h"
35 #include "str.h"
36 #include "memory.h"
37 #include "http.h"
38 #include "http_status.h"
39 #include "socket.h"
40 #include "config.h"
41 #include "utils.h"
42 #include "file.h"
43 #include "cache.h"
45 long int mk_method_post_content_length(char *body)
47 struct header_toc *toc = NULL;
48 long int len;
49 mk_pointer tmp;
51 /* obs: Table of Content (toc) is created when the full
52 * request has arrived, this function cannot be used from
53 * mk_http_pending_request().
55 toc = mk_cache_get(mk_cache_header_toc);
56 tmp = mk_request_header_find(toc, MK_KNOWN_HEADERS, body,
57 mk_rh_content_length);
59 if (!tmp.data) {
60 return -1;
63 len = atoi(tmp.data);
65 return len;
69 /* POST METHOD */
70 int mk_method_post(struct client_request *cr, struct request *sr)
72 struct header_toc *toc = NULL;
73 mk_pointer tmp;
74 char buffer[MAX_REQUEST_BODY];
75 long content_length_post = 0;
77 content_length_post = mk_method_post_content_length(cr->body);
79 /* Length Required */
80 if (content_length_post == -1) {
81 mk_request_error(M_CLIENT_LENGTH_REQUIRED, cr, sr);
82 return -1;
85 /* Bad request */
86 if (content_length_post <= 0) {
87 mk_request_error(M_CLIENT_BAD_REQUEST, cr, sr);
88 return -1;
91 /* Content length too large */
92 if (content_length_post >= MAX_REQUEST_BODY) {
93 mk_request_error(M_CLIENT_REQUEST_ENTITY_TOO_LARGE, cr, sr);
94 return -1;
97 toc = mk_cache_get(mk_cache_header_toc);
98 tmp = mk_request_header_find(toc, MK_KNOWN_HEADERS, sr->body.data,
99 mk_rh_content_type);
101 if (!tmp.data) {
102 mk_request_error(M_CLIENT_BAD_REQUEST, cr, sr);
103 return -1;
105 sr->content_type = tmp;
107 if (sr->post_variables.len < content_length_post) {
108 content_length_post = strlen(buffer);
111 sr->content_length = content_length_post;
113 return 0;
116 /* Return POST variables sent in request */
117 mk_pointer mk_method_post_get_vars(char *body, int index)
119 long len = 1;
120 char *str = 0;
121 mk_pointer p;
123 str = mk_string_copy_substr(body, index, strlen(body));
124 if (str) {
125 len = strlen(str);
128 p.data = str;
129 p.len = len;
131 return p;