OSDN Git Service

Merge branch 'packet_edit' into autotools-fix(releng)
[ultramonkey-l7/sslproxy.git] / src / http_message.cpp
1 /*
2  * @file  http_message.cpp
3  * @brief module of HTTP Message
4  * @brief HTTP Message parser
5  *
6  * Copyright (C) 2009  NTT COMWARE Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  **********************************************************************/
24
25 #include "http_message.h"
26
27 /*!
28  * HTTP Message constructor.
29  */
30 http_message::http_message()
31     :
32     modified(false)
33 {
34     /*-------- DEBUG LOG --------*/
35     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
36         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 1,
37         "in/out_function : Constructor http_message::http_message(void)");
38     }
39     /*------ DEBUG LOG END ------*/
40 }
41
42 /*!
43  * HTTP Message constructor.
44  * Parse HTTP message header.
45  *
46  * @param[in]   header  full http message header string
47  */
48 http_message::http_message(std::string header)
49     :
50     modified(false)
51 {
52     /*-------- DEBUG LOG --------*/
53     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
54         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 2,
55         "in/out_function : Constructor http_message::http_message(std::string header) : "
56         "header(%s)", header.c_str());
57     }
58     /*------ DEBUG LOG END ------*/
59     this->parse(header);
60 }
61
62 /*!
63  * HTTP Message destructor.
64  */
65 http_message::~http_message()
66 {
67     /*-------- DEBUG LOG --------*/
68     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
69         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 3,
70         "in/out_function : Destructor http_message::~http_message(void)");
71     }
72     /*------ DEBUG LOG END ------*/
73 }
74
75 /*!
76  * Get HTTP header field function.
77  *
78  * @param[in]   field_name  lookup field name
79  * @return      header field value
80  */
81 field_range http_message::header(std::string field_name) const
82 {
83     /*-------- DEBUG LOG --------*/
84     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
85         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 4,
86         "in_function : field_range http_message::header(std::string field_name) : "
87         "field_name(%s)", field_name.c_str());
88     }
89     /*------ DEBUG LOG END ------*/
90
91     std::string name = convert_upper_camel_case(field_name);
92     field_range ret = this->_header.get<field_map>().equal_range(name);
93
94     /*-------- DEBUG LOG --------*/
95     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
96         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 5,
97         "out_function : field_range http_message::header(std::string field_name)");
98     }
99     /*------ DEBUG LOG END ------*/
100
101     return ret;
102 }
103
104 /*!
105  * Set HTTP header field function.
106  * Set new HTTP header field and return old HTTP header field.
107  *
108  * @param[in]   field_name  lookup field name
109  * @param[in]   field_value field value
110  */
111 void http_message::header(std::string field_name, std::string field_value)
112 {
113     /*-------- DEBUG LOG --------*/
114     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
115         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 6,
116         "in_function : field_range http_message::header(std::string field_name, std::string field_value) : "
117         "field_name(%s), field_value(%s)", field_name.c_str(), field_value.c_str());
118     }
119     /*------ DEBUG LOG END ------*/
120
121     bool changed = false;
122     std::string name = convert_upper_camel_case(field_name);
123     field_range ret = this->_header.get<field_map>().equal_range(name);
124     field_map_iterator it = ret.first;
125     field_map_iterator it_end = ret.second;
126     try {
127         for (;it != it_end; ++it) {
128             if (field_value != "") {
129                 if ( _header.get<field_map>().replace(it, field(name, field_value)) ) {
130                     changed = true;
131                     this->modified = true;
132                 }
133             }
134             else {
135                 _header.get<field_map>().erase(it);
136                 changed = true;
137                 this->modified = true;
138             }
139         }
140         if (!changed && field_value != "") {
141             _header.get<field_map>().insert( field(name, field_value) );
142             this->modified = true;
143         }
144     }
145     catch (...) {
146         LOGGER_PUT_LOG_ERROR(LOG_CAT_PACKET_EDIT_HTTP, 1,
147         "Exception occured by inserting or replacing boost::multi_index.");
148     }
149
150     /*-------- DEBUG LOG --------*/
151     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
152         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 7,
153         "out_function : field_range http_message::header(std::string field_name, std::string field_value)");
154     }
155     /*------ DEBUG LOG END ------*/
156 }
157
158 /*!
159  * Get message body function.
160  *
161  * @return    message body
162  */
163 std::string http_message::body() const
164 {
165     /*-------- DEBUG LOG --------*/
166     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
167         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 8,
168         "in_function : std::string http_message::body(void)");
169         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 9,
170         "out_function : std::string http_message::body(void) : "
171         "return(%s)", this->_body.c_str());
172     }
173     /*------ DEBUG LOG END ------*/
174
175     return this->_body;
176 }
177
178 /*!
179  * Set message body function.
180  * Set new message body and return old message body.
181  *
182  * @param[in]   _body   new message body
183  * @return  old message body
184  */
185 std::string http_message::body(std::string _body)
186 {
187     /*-------- DEBUG LOG --------*/
188     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
189         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 10,
190         "in_function : std::string http_message::http_version(std::string _message) : "
191         "_body(%s)", _body.c_str());
192     }
193     /*------ DEBUG LOG END ------*/
194
195     std::string ret = this->_body;
196     this->_body = _body;
197     this->modified = true;
198
199     /*-------- DEBUG LOG --------*/
200     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
201         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 11,
202         "out_function : std::string http_message::body(std::string _body) : "
203         "return(%s)", ret.c_str());
204     }
205     /*------ DEBUG LOG END ------*/
206
207     return ret;
208 }
209
210 /*!
211  * Get full HTTP message function.
212  *
213  * @return    HTTP message
214  */
215 std::string http_message::as_string()
216 {
217     /*-------- DEBUG LOG --------*/
218     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
219         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 12,
220         "in_function : std::string http_message::as_string(void)");
221     }
222     /*------ DEBUG LOG END ------*/
223
224     if (this->modified)
225         this->rebuild();
226
227     /*-------- DEBUG LOG --------*/
228     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
229         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 13,
230         "out_function : std::string http_message::as_string(void) : "
231         "return(%s)", this->raw_message.c_str());
232     }
233     /*------ DEBUG LOG END ------*/
234
235     return this->raw_message;
236 }
237
238 /*!
239  * Parse HTTP header function.
240  *
241  * @param[in]   message     full HTTP message header
242  */
243 void http_message::parse(std::string message)
244 {
245     /*-------- DEBUG LOG --------*/
246     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
247         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 14,
248         "in_function : void http_message::parse(std::string message) : "
249         "message(%s)", message.c_str());
250     }
251     /*------ DEBUG LOG END ------*/
252
253     // save raw message
254     if (this->raw_message.length() == 0)
255         this->raw_message = message;
256
257     // parse message
258     HTTP_MESSAGE_POSITION pos = MESSAGE_TOP;
259
260     /*
261      * RFC2616
262      *  OCTET       : 8bit data
263      *  CHAR        : US-ASCII(0-127)
264      *  UPALPHA     : A-Z
265      *  LOALPHA     : a-z
266      *  ALPHA       : UPALPHA | LOALPHA
267      *  DIGIT       : 0-9
268      *  HEXDIG      : A-F | a-f | DIGIT
269      *  SP          : SPace(32)
270      *  HT          : Horizontal Tab(9)
271      *  CR          : Carriage Return(13)
272      *  LF          : Line Feed(10)
273      *  CTL         : ConTLol char(0-31,127)
274      *  LWS         : [CRLF] 1*(SP|HT)
275      *  separators  : ()<>@,;:\"/[]?={} and SP, HT
276      *  token       : 1*(CHAR not CTL, separators)
277      */
278     std::string::iterator ptr = message.begin();
279     std::string::iterator end = message.end();
280     std::string::iterator start = ptr;
281     std::pair<std::string, std::string> field_pair;
282     try {
283         while (ptr != end) {
284             switch(pos) {
285             /*
286              * MESSAGE-HEADER   : field-name ":" [ field-value ]
287              * field-name       : token
288              * field-value      : *( field-content | LWS )
289              * field-content    : <the OCTETs making up the field-value and
290              *                    consisting of either *TEXT or combinations
291              *                    of token, separators, and quoted-string>
292              * TEXT             : <any OCTET except CTLs, but including LWS>
293              * quoted-string    : ( <"> *(qdtext | quoted-pair ) <"> )
294              * qdtext           : <any TEXT except <">>
295              * quoted-pair      : "\" CHAR
296              */
297             case MESSAGE_TOP:
298                 if (isalpha(*ptr) || *ptr == '-' || isdigit(*ptr) || 
299                     *ptr == '.' || *ptr == '_' || *ptr == '~' || *ptr == '!' ||
300                     *ptr == '$' || *ptr == '&' || *ptr == '*' || *ptr == '+' ||
301                     *ptr == '%') {
302                     start = ptr;
303                     pos = MESSAGE_FIELD_NAME;
304                 } else if (*ptr == '\r') { // CRLF + CRLF
305                     pos = MESSAGE_LAST_CR;
306                 } else {
307                     LOGGER_PUT_LOG_ERROR(LOG_CAT_PACKET_EDIT_HTTP, 2,
308                     "Parse error: Invalid header field name.(%c)", *ptr);
309                     throw -1;
310                 }
311                 break;
312     
313             case MESSAGE_CR:
314                 // LF only
315                 if (*ptr == '\n') {
316                     pos = MESSAGE_LF;
317                 } else {
318                     LOGGER_PUT_LOG_ERROR(LOG_CAT_PACKET_EDIT_HTTP, 3,
319                     "Parse error: No LF.(%c)", *ptr);
320                     throw -1;
321                 }
322                 break;
323     
324             case MESSAGE_LF:
325                 if (isalpha(*ptr) || *ptr == '-' || isdigit(*ptr) || 
326                     *ptr == '.' || *ptr == '_' || *ptr == '~' || *ptr == '!' ||
327                     *ptr == '$' || *ptr == '&' || *ptr == '*' || *ptr == '+' ||
328                     *ptr == '%') {
329                     if (field_pair.first.length()) {
330                         field_pair.first = convert_upper_camel_case(field_pair.first);
331                         boost::trim(field_pair.second);
332                         _header.get<field_map>().insert(field_pair);
333                         field_pair.first.clear();
334                     }
335                     start = ptr;
336                     pos = MESSAGE_FIELD_NAME;
337                 } else if (*ptr == ' ' || *ptr == '\t') {
338                     pos = MESSAGE_FIELD_VALUE;
339                 } else if (*ptr == '\r') { // CRLF + CRLF
340                     if (field_pair.first.length()) {
341                         field_pair.first = convert_upper_camel_case(field_pair.first);
342                         boost::trim(field_pair.second);
343                         _header.get<field_map>().insert(field_pair);
344                         field_pair.first.clear();
345                     }
346                     pos = MESSAGE_LAST_CR;
347                 } else {
348                     LOGGER_PUT_LOG_ERROR(LOG_CAT_PACKET_EDIT_HTTP, 4,
349                     "Parse error: Invalid header field name.(%c)", *ptr);
350                     throw -1;
351                 }
352                 break;
353     
354             case MESSAGE_FIELD_NAME:
355                 // field-name end with ':'
356                 if (*ptr == ':') {
357                     pos = MESSAGE_FIELD_NAME_COLON;
358                     field_pair.first.assign(start, ptr);
359                 } else if (!isalpha(*ptr) && *ptr != '-' && !isdigit(*ptr) && 
360                     *ptr != '.' && *ptr != '_' && *ptr != '~' && *ptr != '!' &&
361                     *ptr != '$' && *ptr != '&' && *ptr != '*' && *ptr != '+' &&
362                     *ptr != '%') {
363                     LOGGER_PUT_LOG_ERROR(LOG_CAT_PACKET_EDIT_HTTP, 5,
364                     "Parse error: Invalid header field name.(%c)", *ptr);
365                     throw -1;
366                 }
367                 break;
368     
369             case MESSAGE_FIELD_NAME_COLON:
370                 if (*ptr == ' ' || isalpha(*ptr) || isdigit(*ptr) || *ptr == '-' ||
371                     *ptr == '.' || *ptr == '_' || *ptr == '~' || *ptr == ':' || 
372                     *ptr == '@' || *ptr == '!' || *ptr == '$' || *ptr == '&' ||
373                     *ptr == '(' || *ptr == ')' || *ptr == '*' || *ptr == '+' ||
374                     *ptr == ',' || *ptr == ';' || *ptr == '=' || *ptr == '%' ||
375                     *ptr == '<' || *ptr == '>' || *ptr == '[' || *ptr == ']' ||
376                     *ptr == '{' || *ptr == '}' || *ptr == '?' || *ptr == '"' ||
377                     *ptr == '|' || *ptr == '/' || *ptr == '\\' || *ptr == '\t') {
378                     start = ptr;
379                     pos = MESSAGE_FIELD_VALUE;
380                 } else if (*ptr == '\r') { // omit field value
381                     field_pair.second.clear();
382                 } else {
383                     LOGGER_PUT_LOG_ERROR(LOG_CAT_PACKET_EDIT_HTTP, 6,
384                     "Parse error: Invalid header field value.(%c)", *ptr);
385                     throw -1;
386                 }
387                 break;
388     
389             case MESSAGE_FIELD_VALUE:
390                 // field-value end with CR
391                 if (*ptr == '\r') {
392                     pos = MESSAGE_CR;
393                     field_pair.second.assign(start, ptr);
394                 } else if (*ptr != ' ' && !isalpha(*ptr) && !isdigit(*ptr) && *ptr != '-' &&
395                     *ptr != '.' && *ptr != '_' && *ptr != '~' && *ptr != ':' && 
396                     *ptr != '@' && *ptr != '!' && *ptr != '$' && *ptr != '&' &&
397                     *ptr != '(' && *ptr != ')' && *ptr != '*' && *ptr != '+' &&
398                     *ptr != ',' && *ptr != ';' && *ptr != '=' && *ptr != '%' &&
399                     *ptr != '<' && *ptr != '>' && *ptr != '[' && *ptr != ']' &&
400                     *ptr != '{' && *ptr != '}' && *ptr != '?' && *ptr != '"' &&
401                     *ptr != '|' && *ptr != '/' && *ptr != '\\'&& *ptr != '\t' ) {
402                     LOGGER_PUT_LOG_ERROR(LOG_CAT_PACKET_EDIT_HTTP, 7,
403                     "Parse error: Invalid header field value.(%c)", *ptr);
404                     throw -1;
405                 }
406                 break;
407     
408             case MESSAGE_LAST_CR:
409                 // LF only
410                 if (*ptr == '\n') {
411                     pos = MESSAGE_LAST_LF;
412                 } else {
413                     LOGGER_PUT_LOG_ERROR(LOG_CAT_PACKET_EDIT_HTTP, 8,
414                     "Parse error: No LF.(%c)", *ptr);
415                     throw -1;
416                 }
417                 break;
418     
419             /*
420              * MESSAGE-BODY     : *OCTET
421              */
422             case MESSAGE_LAST_LF:
423                 pos = MESSAGE_BODY;
424                 start = ptr;
425                 break;
426     
427             case MESSAGE_BODY:
428                 break;
429             }
430             ptr++;
431         }
432     
433         switch (pos) {
434         case MESSAGE_BODY:
435             this->_body.assign(start, ptr);
436         }
437     }
438     catch (...) {
439         LOGGER_PUT_LOG_ERROR(LOG_CAT_PACKET_EDIT_HTTP, 9,
440         "Exception occured by parsing HTTP message.");
441     }
442
443     /*-------- DEBUG LOG --------*/
444     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
445         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 15,
446         "out_function : void http_message::parse(std::string message)");
447     }
448     /*------ DEBUG LOG END ------*/
449 }
450
451 /*!
452  * Rebuild HTTP header function.
453  */
454 void http_message::rebuild()
455 {
456     /*-------- DEBUG LOG --------*/
457     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
458         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 16,
459         "in_function : void http_message::rebuild()");
460     }
461     /*------ DEBUG LOG END ------*/
462
463     // insertion order
464     header_container::iterator it = this->_header.begin();
465     header_container::iterator it_end = this->_header.end();
466
467     while (it != it_end) {
468         this->raw_message += it->first + ": " + it->second + "\r\n";
469         it++;
470     }
471
472     this->raw_message += "\r\n" + this->body();
473
474     /*-------- DEBUG LOG --------*/
475     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
476         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 17,
477         "out_function : void http_message::rebuild()");
478     }
479     /*------ DEBUG LOG END ------*/
480 }
481
482 /*!
483  * Field name convert function.
484  * Convert upper camelcase
485  *     ex. connecTION => Connection
486  *         usEr-aGeNT => User-Agent
487  *         p3p => P3P
488  *
489  * @param[in]   field_name  field name
490  * @return  converted to camel case
491  */
492 std::string http_message::convert_upper_camel_case(std::string field_name) const
493 {
494     /*-------- DEBUG LOG --------*/
495     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
496         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 18,
497         "in_function : std::string http_message::upper_camel_case(std::string field_name) : "
498         "field_name(%s)", field_name.c_str());
499     }
500     /*------ DEBUG LOG END ------*/
501
502     std::string ret;
503     boost::char_separator<char> sep("-_0123456789", "-_0123456789", boost::keep_empty_tokens);
504     boost::tokenizer<boost::char_separator<char> > tokens(field_name, sep);
505     boost::tokenizer<boost::char_separator<char> >::iterator tok_it  = tokens.begin();
506     boost::tokenizer<boost::char_separator<char> >::iterator tok_end = tokens.end();
507     for (; tok_it != tok_end; ++tok_it) {
508         std::string token(*tok_it);
509         boost::to_lower(token);
510         token.at(0) = std::toupper(token.at(0));
511         ret += token;
512     }
513
514     /*-------- DEBUG LOG --------*/
515     if (LOG_LV_DEBUG == logger_get_log_level(LOG_CAT_PACKET_EDIT_HTTP)) {
516         LOGGER_PUT_LOG_DEBUG(LOG_CAT_PACKET_EDIT_HTTP, 19,
517         "out_function : std::string http_message::upper_camel_case(std::string field_name) : "
518         "return(%s)", ret.c_str());
519     }
520     /*------ DEBUG LOG END ------*/
521     return ret;
522 }