Imported Upstream version 2.5.11
[libapache-mod-security.git] / apache2 / msc_multipart.h
1 /*
2  * ModSecurity for Apache 2.x, http://www.modsecurity.org/
3  * Copyright (c) 2004-2009 Breach Security, Inc. (http://www.breach.com/)
4  *
5  * This product is released under the terms of the General Public Licence,
6  * version 2 (GPLv2). Please refer to the file LICENSE (included with this
7  * distribution) which contains the complete text of the licence.
8  *
9  * There are special exceptions to the terms and conditions of the GPL
10  * as it is applied to this software. View the full text of the exception in
11  * file MODSECURITY_LICENSING_EXCEPTION in the directory of this software
12  * distribution.
13  *
14  * If any of the files related to licensing are missing or if you have any
15  * other questions related to licensing please contact Breach Security, Inc.
16  * directly using the email address support@breach.com.
17  *
18  */
19 #ifndef _MSC_MULTIPART_H_
20 #define _MSC_MULTIPART_H_
21
22 #define MULTIPART_BUF_SIZE              4096
23
24 #define MULTIPART_FORMDATA              1
25 #define MULTIPART_FILE                  2
26
27 typedef struct multipart_part multipart_part;
28 typedef struct multipart_data multipart_data;
29
30 #include "apr_general.h"
31 #include "apr_tables.h"
32 #include "modsecurity.h"
33
34 typedef struct value_part_t value_part_t;
35 struct value_part_t {
36     char *data;
37     long int length;
38 };
39
40 struct multipart_part {
41     /* part type, can be MULTIPART_FORMDATA or MULTIPART_FILE */
42     int                      type;
43     /* the name */
44     char                    *name;
45
46     /* variables only, variable value */
47     char                    *value;
48     apr_array_header_t      *value_parts;
49
50     /* files only, the content type (where available) */
51     char                    *content_type;
52
53     /* files only, the name of the temporary file holding data */
54     char                    *tmp_file_name;
55     int                      tmp_file_fd;
56     unsigned int             tmp_file_size;
57     /* files only, filename as supplied by the browser */
58     char                    *filename;
59
60     char                    *last_header_name;
61     apr_table_t             *headers;
62
63     unsigned int             offset;
64     unsigned int             length;
65 };
66
67 struct multipart_data {
68     /* this array keeps parts */
69     apr_array_header_t      *parts;
70
71     /* mime boundary used to detect when
72      * parts end and begin
73      */
74     char                    *boundary;
75     int                      boundary_count;
76
77     /* internal buffer and other variables
78      * used while parsing
79      */
80     char                     buf[MULTIPART_BUF_SIZE + 2];
81     int                      buf_contains_line;
82     char                    *bufptr;
83     int                      bufleft;
84
85     unsigned int             buf_offset;
86
87     /* pointer that keeps track of a part while
88      * it is being built
89      */
90     multipart_part          *mpp;
91
92
93     /* part parsing state; 0 means we are reading
94      * headers, 1 means we are collecting data
95      */
96     int                      mpp_state;
97
98     /* because of the way this parsing algorithm
99      * works we hold back the last two bytes of
100      * each data chunk so that we can discard it
101      * later if the next data chunk proves to be
102      * a boundary; the first byte is an indicator
103      * 0 - no content, 1 - two data bytes available
104      */
105     char                     reserve[4];
106
107     int                      seen_data;
108     int                      is_complete;
109
110     int                      flag_error;
111     int                      flag_data_before;
112     int                      flag_data_after;
113     int                      flag_header_folding;
114     int                      flag_boundary_quoted;
115     int                      flag_lf_line;
116     int                      flag_crlf_line;
117     int                      flag_unmatched_boundary;
118     int                      flag_boundary_whitespace;
119     int                      flag_missing_semicolon;
120     int                      flag_invalid_quoting;
121 };
122
123
124 /* Functions */
125
126 int DSOLOCAL multipart_init(modsec_rec *msr, char **error_msg);
127
128 int DSOLOCAL multipart_complete(modsec_rec *msr, char **error_msg);
129
130 int DSOLOCAL multipart_process_chunk(modsec_rec *msr, const char *buf,
131     unsigned int size, char **error_msg);
132
133 apr_status_t DSOLOCAL multipart_cleanup(modsec_rec *msr);
134
135 int DSOLOCAL multipart_get_arguments(modsec_rec *msr, char *origin, apr_table_t *arguments);
136
137 char DSOLOCAL *multipart_reconstruct_urlencoded_body_sanitise(modsec_rec *msr);
138
139 #endif