Security Plugin: set http status for URL rule match
[MonkeyD.git] / src / method.c
blob4c799b561affb6950e60d2c8d1c44e5ab826a2c8
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 long content_length_post = 0;
76 content_length_post = mk_method_post_content_length(cr->body);
78 /* Length Required */
79 if (content_length_post == -1) {
80 mk_request_error(M_CLIENT_LENGTH_REQUIRED, cr, sr);
81 return -1;
84 /* Bad request */
85 if (content_length_post <= 0) {
86 mk_request_error(M_CLIENT_BAD_REQUEST, cr, sr);
87 return -1;
90 /* Content length too large */
91 if (content_length_post >= cr->body_size) {
92 mk_request_error(M_CLIENT_REQUEST_ENTITY_TOO_LARGE, cr, sr);
93 return -1;
96 toc = mk_cache_get(mk_cache_header_toc);
97 tmp = mk_request_header_find(toc, MK_KNOWN_HEADERS, sr->body.data,
98 mk_rh_content_type);
100 if (!tmp.data) {
101 mk_request_error(M_CLIENT_BAD_REQUEST, cr, sr);
102 return -1;
104 sr->content_type = tmp;
105 sr->content_length = content_length_post;
107 return 0;
110 /* Return POST variables sent in request */
111 mk_pointer mk_method_post_get_vars(char *body, int index)
113 long len = 1;
114 char *str = 0;
115 mk_pointer p;
117 str = mk_string_copy_substr(body, index, strlen(body));
118 if (str) {
119 len = strlen(str);
122 p.data = str;
123 p.len = len;
125 return p;