OSDN Git Service

DHA20091203更新分のTAB、改行一括変換
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / module / protocol / protocol_module_ip.cpp
1 /*
2  * @file  protocol_module_ip.cpp
3  * @brief protocol module of any protocol.
4  * @brief this module never keep session persistence.
5  *
6  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
7  * Copyright (C) 2009  NTT COMWARE Corporation.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  *
24  **********************************************************************/
25
26 #include <vector>
27 #include <list>
28 #include <algorithm>
29 #include <iostream>
30 #include <boost/asio/ip/tcp.hpp>
31 #include <boost/format.hpp>
32 #include <boost/xpressive/xpressive.hpp>
33 #include "protocol_module_ip.h"
34 #include "http_utility.h"
35 #include "utility.h"
36
37 namespace l7vs
38 {
39
40     const std::string protocol_module_ip::MODULE_NAME = "ip";
41     const int protocol_module_ip::THREAD_DIVISION_UP_STREAM = 0;
42     const int protocol_module_ip::THREAD_DIVISION_DOWN_STREAM = 1;
43
44     const int protocol_module_ip::END_FLAG_OFF = 0;
45     const int protocol_module_ip::END_FLAG_ON = 1;
46
47     const int protocol_module_ip::ACCEPT_END_FLAG_OFF = 0;
48     const int protocol_module_ip::ACCEPT_END_FLAG_ON = 1;
49
50     const int protocol_module_ip::SORRY_FLAG_ON = 1;
51     const int protocol_module_ip::SORRY_FLAG_OFF = 0;
52
53     const int protocol_module_ip::SWITCH_FLAG_OFF = 0;
54     const int protocol_module_ip::SWITCH_FLAG_ON = 1;
55
56     const int protocol_module_ip::FORWARDED_FOR_OFF = 0;
57     const int protocol_module_ip::FORWARDED_FOR_ON = 1;
58
59     using namespace boost::xpressive;
60     //! constractor
61     protocol_module_ip::protocol_module_ip() :
62             ip_protocol_module_base(MODULE_NAME), forwarded_for(FORWARDED_FOR_OFF)
63     {
64         sorry_uri.assign('\0');
65         sorry_uri[0] = '/';
66     }
67     //! destractor
68     protocol_module_ip::~protocol_module_ip()
69     {
70     }
71     //! tcp protocol support check
72     //! @return tcp support is true
73     //! @return tcp not-support is false
74     bool protocol_module_ip::is_tcp()
75     {
76         /*-------- DEBUG LOG --------*/
77         if (unlikely(LOG_LV_DEBUG == getloglevel()))
78         {
79             putLogDebug(600000, "in/out_function : bool protocol_module_ip::is_tcp() : return_value = true.",
80                         __FILE__, __LINE__);
81         }
82         /*------DEBUG LOG END------*/
83         return true;
84     }
85
86     //! udp protocol support check
87     //! @return udp support is true
88     //! @return udp not-support is false
89     bool protocol_module_ip::is_udp()
90     {
91         /*-------- DEBUG LOG --------*/
92         if (unlikely(LOG_LV_DEBUG == getloglevel()))
93         {
94             putLogDebug(600001, "in/out_function : bool protocol_module_ip::is_udp() : return_value = false.",
95                         __FILE__, __LINE__);
96         }
97         /*------DEBUG LOG END------*/
98         return false;
99     }
100
101     //! replication interval interrrupt
102     //! timer thread call this function. from virtualservice.
103     void protocol_module_ip::replication_interrupt()
104     {
105         /*-------- DEBUG LOG --------*/
106         if (unlikely(LOG_LV_DEBUG == getloglevel()))
107         {
108             putLogDebug(600002, "in/out_function : void protocol_module_ip::"
109                         "replication_interrupt().", __FILE__, __LINE__ );
110         }
111         /*------DEBUG LOG END------*/
112     }
113     //! initialize function. called from module control. module loaded call
114     //! @param[in]    realserver list iterator begin function object type
115     //!    @param[in]    realserver list iterator end function object type
116     //! @param[in]    realserver list iterator next function object type
117     //! @param[in]    realserver list mutex lock function object type.
118     //! @param[in]    realserver list mutex unlock function object type
119     void protocol_module_ip::initialize(rs_list_itr_func_type    inlist_begin,
120                                         rs_list_itr_func_type    inlist_end,
121                                         rs_list_itr_next_func_type    inlist_next,
122                                         boost::function< void( void ) >    inlist_lock,
123                                         boost::function< void( void ) >    inlist_unlock)
124     {
125         /*-------- DEBUG LOG --------*/
126         if (unlikely(LOG_LV_DEBUG == getloglevel()))
127         {
128             putLogDebug(600003, "in_function : void protocol_module_ip::initialize("
129                         "rs_list_itr_func_type inlist_begin, rs_list_itr_func_type inlist_end, "
130                         "rs_list_itr_next_func_type inlist_next, boost::function< void(void) > "
131                         "inlist_lock, boost::function< void(void) > inlist_unlock).", __FILE__, __LINE__);
132         }
133         /*------DEBUG LOG END------*/
134
135         //RealServer list begin function
136         rs_list_begin = inlist_begin;
137         //RealServer list end function
138         rs_list_end = inlist_end;
139         //RealServer list next function
140         rs_list_next = inlist_next;
141         //RealServer list lock funtion
142         rs_list_lock = inlist_lock;
143         //RealServer list unlock funtion
144         rs_list_unlock = inlist_unlock;
145
146         /*-------- DEBUG LOG --------*/
147         if (unlikely(LOG_LV_DEBUG == getloglevel()))
148         {
149             putLogDebug(600004, "out_function : void protocol_module_ip::initialize("
150                         "rs_list_itr_func_type inlist_begin, rs_list_itr_func_type inlist_end, "
151                         "rs_list_itr_next_func_type inlist_next, boost::function< void(void) > "
152                         "inlist_lock, boost::function< void(void) > inlist_unlock).", __FILE__, __LINE__);
153         }
154         /*------DEBUG LOG END------*/
155     }
156
157     //! finalize called from module control. module unloaded call.
158     void protocol_module_ip::finalize()
159     {
160         /*-------- DEBUG LOG --------*/
161         if (unlikely(LOG_LV_DEBUG == getloglevel()))
162         {
163             putLogDebug(600005, "in_function : void protocol_module_ip::finalize().", __FILE__, __LINE__);
164         }
165         /*------DEBUG LOG END------*/
166
167         //RealServer list functions initializtion
168         //RealServer list begin function
169         rs_list_begin.clear();
170         //RealServer list end function
171         rs_list_end.clear();
172         //RealServer list next function
173         rs_list_next.clear();
174         //RealServer list lock function
175         rs_list_lock.clear();
176         //RealServer list unlock function
177         rs_list_unlock.clear();
178
179         //Replication functions initializtion
180         //component memory allcate function
181         replication_pay_memory.clear();
182         //component memory lock function
183         replication_area_lock.clear();
184         //component memory unlock function
185         replication_area_unlock.clear();
186
187         /*-------- DEBUG LOG --------*/
188         if (unlikely(LOG_LV_DEBUG == getloglevel()))
189         {
190             putLogDebug(600006, "function : void protocol_module_ip::finalize() : "
191                         "rs_list_begin.clear(), rs_list_end.clear(), rs_list_next.clear(), "
192                         "rs_list_lock.clear(), rs_list_unlock.clear() end.", __FILE__, __LINE__);
193         }
194         /*------DEBUG LOG END------*/
195
196         //ScheduleModule's functions initializtion
197         schedule_tcp.clear();
198
199         /*-------- DEBUG LOG --------*/
200         if (unlikely(LOG_LV_DEBUG == getloglevel()))
201         {
202             putLogDebug(600007, "function : void protocol_module_ip::finalize() : "
203                         "schedule_tcp.clear() end.", __FILE__, __LINE__);
204         }
205         /*------DEBUG LOG END------*/
206
207         //Module's option initializtion
208         //forwarded_for
209         forwarded_for = FORWARDED_FOR_OFF;
210         //sorry-uri
211         sorry_uri.assign('\0');
212         //time_out
213         timeout = 0;
214         //reschedule flag
215         reschedule = 0;
216
217         // replication initialize
218         if (replication_data_processor)
219         {
220             /*-------- DEBUG LOG --------*/
221             if (unlikely(LOG_LV_DEBUG == getloglevel()))
222             {
223                 boost::format formatter("delete : address = &(%d).");
224                 formatter % static_cast<void*>(replication_data_processor);
225                 putLogDebug(600008, formatter.str(), __FILE__, __LINE__);
226             }
227             /*------DEBUG LOG END------*/
228
229             delete replication_data_processor;
230             replication_data_processor = NULL;
231         }
232
233         // session initialize
234         if (ip_data_processor)
235         {
236             /*-------- DEBUG LOG --------*/
237             if (unlikely(LOG_LV_DEBUG == getloglevel()))
238             {
239                 boost::format formatter("delete : address = &(%d).");
240                 formatter % static_cast<void*>(ip_data_processor);
241                 putLogDebug(600009, formatter.str(), __FILE__, __LINE__);
242             }
243             /*------DEBUG LOG END------*/
244
245             delete ip_data_processor;
246             ip_data_processor = NULL;
247         }
248
249         /*-------- DEBUG LOG --------*/
250         if (unlikely(LOG_LV_DEBUG == getloglevel()))
251         {
252             putLogDebug(600010, "out_function : void protocol_module_ip::finalize().", __FILE__, __LINE__);
253         }
254         /*------DEBUG LOG END------*/
255
256         //logger functions initializtion
257         //log level getting function
258         getloglevel.clear();
259         //logger(Fatal)
260         putLogFatal.clear();
261         //logger(Error)
262         putLogError.clear();
263         //logger(Warn)
264         putLogWarn.clear();
265         //logger(Info)
266         putLogInfo.clear();
267         //logger(Debug)
268         putLogDebug.clear();
269     }
270
271     //! sorry support check
272     //! @return true sorry mode is supported.
273     //! @return false sorry mode is unsupported.
274     bool protocol_module_ip::is_use_sorry()
275     {
276         /*-------- DEBUG LOG --------*/
277         if (unlikely(LOG_LV_DEBUG == getloglevel()))
278         {
279             putLogDebug(600011, "in/out_function : bool protocol_module_ip::is_use_sorry() : return_value = true.",
280                         __FILE__, __LINE__);
281         }
282         /*------DEBUG LOG END------*/
283         return true;
284     }
285
286     //! realserver list update event
287     void protocol_module_ip::handle_rslist_update()
288     {
289         /*-------- DEBUG LOG --------*/
290         if (unlikely(LOG_LV_DEBUG == getloglevel()))
291         {
292             putLogDebug(600012, "in/out_function : void protocol_module_ip::handle_rslist_update().",
293                         __FILE__, __LINE__);
294         }
295         /*------DEBUG LOG END------*/
296     }
297
298     //! module parameter check.used by l7vsadm
299     //! @param[in]    module paramter string list
300     //! @return    result.flag true is parameter is noproblem.
301     //! @return result.flag false is paramter is problem.
302     protocol_module_base::check_message_result protocol_module_ip::check_parameter(const std::vector<
303             std::string>& args)
304     {
305         /*-------- DEBUG LOG --------*/
306         if (unlikely(LOG_LV_DEBUG == getloglevel()))
307         {
308             boost::format formatter("in_function : protocol_module_ip::check_message_result "
309                                     "protocol_module_ip::check_parameter("
310                                     "const std::vector<std::string>& args) : args = %s.");
311             std::string argsdump;
312             for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
313             {
314                 argsdump += *it;
315             }
316             formatter % argsdump;
317             putLogDebug(600013, formatter.str(), __FILE__, __LINE__);
318         }
319         /*------DEBUG LOG END------*/
320
321         //set check result true
322         // set check result flag true
323         check_message_result check_result;
324         check_result.flag = true;
325         bool timeout_flag = false;
326         bool reschedule_flag = false;
327         bool no_reschedule_flag = false;
328         bool forward_checked = false;
329         bool sorryuri_checked = false;
330         sregex    sorry_uri_regex
331         =    +(    '/' >>
332              *(    alpha |
333                 digit |
334                 ( set = ';', ':', '@', '&', '=' ) |
335                 ( set = '$', '-', '_', '.', '+' ) |
336                 ( set = '!', '*', '\'', '\(', ')', ',' ) |
337                 '%' >> repeat<2>(xdigit)));
338
339         typedef std::vector<std::string>::const_iterator vec_str_it;
340
341         try
342         {
343             vec_str_it it = args.begin();
344             vec_str_it it_end = args.end();
345             boost::format formatter;
346
347             //loop option strings
348             for (; it != it_end;)
349             {
350                 if (*it == "-T" || *it == "--timeout")
351                 {
352                     // timeout
353                     if (!timeout_flag)
354                     {
355                         // not set timeout option
356                         ++it;
357                         // next parameter exist check
358                         if (it != it_end)
359                         {
360                             // next parameter exist
361                             if ((*it).substr(0, 1) == "-" || (*it).substr(0, 2) == "--")
362                             {
363                                 continue;
364                             }
365
366                             try
367                             {
368                                 unsigned long ultimeout = 0;
369                                 ultimeout = boost::lexical_cast<unsigned long>(*it);
370                                 // int maxvalue check
371                                 if (ultimeout > INT_MAX)
372                                 {
373                                     check_result.flag = false;
374                                     formatter.parse("'-T/--timeout' option value '%s' is too large.");
375                                     formatter % *it;
376                                     check_result.message = formatter.str();
377                                     putLogError(600000, check_result.message, __FILE__, __LINE__);
378                                     break;
379                                 }
380                                 else
381                                 {
382                                     timeout_flag = true;
383                                     ++it;
384                                     continue;
385                                 }
386                             }
387                             catch (boost::bad_lexical_cast& e)
388                             {
389                                 // not numeric character
390                                 check_result.flag = false;
391                                 formatter.parse("'-T/--timeout' option value '%s' is not numeric character.");
392                                 formatter % *it;
393                                 check_result.message = formatter.str();
394                                 putLogError(600001, check_result.message, __FILE__, __LINE__);
395                                 break;
396                             }
397                         }
398                         else
399                         {
400                             break;
401                         }
402                     }
403                     else
404                     {
405                         // already set timeout
406                         check_result.flag = false;
407                         check_result.message = "Cannot set multiple option '-T/--timeout'.";
408                         putLogError(600002, check_result.message, __FILE__, __LINE__);
409                         break;
410                     }
411                 }
412                 else if (*it == "-R" || *it == "--reschedule")
413                 {
414                     // reschedule
415                     if (!no_reschedule_flag)
416                     {
417                         // not set no-reschedule flag
418                         reschedule_flag = true;
419                     }
420                     else
421                     {
422                         // already set no-reschedule flag
423                         check_result.flag = false;
424                         check_result.message = "You have to choose either of reschedule or no-reschedule.";
425                         putLogError(600003, check_result.message, __FILE__, __LINE__);
426                         break;
427                     }
428                 }
429                 else if  (*it == "-N" || *it == "--no-reschedule")
430                 {
431                     // no-reschedule
432                     if (!reschedule_flag)
433                     {
434                         // not set reschedule flag
435                         no_reschedule_flag = true;
436                     }
437                     else
438                     {
439                         // already set reshcedule flag
440                         check_result.flag = false;
441                         check_result.message = "You have to choose either of reschedule or no-reschedule.";
442                         putLogError(600004, check_result.message, __FILE__, __LINE__);
443                         break;
444                     }
445                 }
446                 //option string = "-F"
447                 else if (*it == "-F" || *it == "--forwarded-for")
448                 {
449                     //set forward flag  ON
450                     forward_checked = true;
451                 }
452                 //option string = "-S"
453                 else if (*it == "-S" || *it == "--sorry-uri")
454                 {
455                     //set sorryURI flag OFF
456                     if (!sorryuri_checked)
457                     {
458                         //next item exist
459                         if (++it != it_end)
460                         {
461                             if (!it->empty() && (it->substr(0, 1) == "-" || it->substr(0, 2) == "--"))
462                             {
463                                 //set check result flag false
464                                 check_result.flag = false;
465                                 //set check result message
466                                 check_result.message = "You have to set option value '-S/--sorry-uri'.";
467                                 putLogError(600005, check_result.message, __FILE__, __LINE__ );
468                                 //loop break;
469                                 break;
470                             }
471                             //next option string's length > 127
472                             if (it->size() > MAX_OPTION_SIZE - 1)
473                             {
474                                 std::ostringstream ostr;
475                                 ostr << "'-S/--sorry-uri' option value '" << *it << "' is too long.";
476
477                                 //set check result flag false
478                                 check_result.flag = false;
479                                 //set check result message
480                                 check_result.message = ostr.str();
481                                 putLogError(600006, check_result.message, __FILE__, __LINE__ );
482                                 //loop break;
483                                 break;
484                             }
485                             //next option string's length <= 127
486                             else
487                             {
488                                 //regex check
489                                 if (regex_match(*it, sorry_uri_regex ))
490                                 {
491                                     //check OK
492                                     //set sorryURI flag ON
493                                     sorryuri_checked = true;
494                                 }
495                                 //check NG
496                                 else
497                                 {
498                                     std::ostringstream ostr;
499                                     ostr << "'-S/--sorry-uri' option value '" << *it << "' is not a valid URI.";
500
501                                     //set check result flag false
502                                     check_result.flag = false;
503                                     //set check result message
504                                     check_result.message = ostr.str();
505                                     putLogError(600007, check_result.message, __FILE__, __LINE__ );
506                                     break;
507                                 }
508                             }
509                         }
510                         //next item is not exist
511                         else
512                         {
513                             //set check flag false
514                             check_result.flag = false;
515                             //set check result message
516                             check_result.message = "You have to set option value '-S/--sorry-uri'.";
517                             putLogError(600008, check_result.message, __FILE__,
518                                         __LINE__ );
519                             //loop break
520                             break;
521                         }
522                     }
523                     //sorryURI flag = ON
524                     else
525                     {
526                         //set check result flag false
527                         check_result.flag = false;
528                         //set check result message
529                         check_result.message = "Cannot set multiple option '-S/--sorry-uri'.";
530                         putLogError(600009, check_result.message, __FILE__,
531                                     __LINE__ );
532                         //loop break
533                         break;
534                     }
535                 }
536                 //other option string
537                 else
538                 {
539                     //set check result flag false
540                     check_result.flag = false;
541                     //set check result message
542                     check_result.message = "Option error.";
543                     putLogError(600010, check_result.message, __FILE__, __LINE__ );
544                     //loop break
545                     break;
546                 }
547
548                 ++it;
549             }
550         }
551         catch (const std::exception& ex)
552         {
553             check_result.flag = false;
554             std::cerr << "protocol_module_ip::check_parameter() : exception : error = " << ex.what() << "." << std::endl;
555             boost::format formatter("function : protocol_module_base::check_message_result "
556                                     "protocol_module_ip::check_parameter() exception : "
557                                     "error = %s.");
558             formatter % ex.what();
559             putLogError(600011, formatter.str(), __FILE__, __LINE__ );
560         }
561         catch (...)
562         {
563             check_result.flag = false;
564             std::cerr << "protocol_module_ip::check_parameter() : Unknown exception." << std::endl;
565             putLogError(600012, "function : protocol_module_base::check_message_result "
566                         "protocol_module_ip::check_parameter() : "
567                         "Unknown exception.", __FILE__, __LINE__ );
568         }
569
570         /*-------- DEBUG LOG --------*/
571         if (unlikely(LOG_LV_DEBUG == getloglevel()))
572         {
573             boost::format formatter("out_function : protocol_module_base::check_message_result "
574                                     "protocol_module_ip::check_parameter("
575                                     "const std::vector<std::string>& args) : return_value = ("
576                                     "check_message_result.flag = %d, check_message_result.message = %s).");
577             formatter % check_result.flag % check_result.message;
578             putLogDebug(600014, formatter.str(), __FILE__, __LINE__ );
579         }
580         /*------DEBUG LOG END------*/
581
582         return check_result;
583     }
584
585     //! parameter set
586     //! @param[in] module paramter string list
587     //! @return    result.flag true is parameter is noproblem.
588     //! @return result.flag false is paramter is problem.
589     protocol_module_base::check_message_result protocol_module_ip::set_parameter(const std::vector<
590             std::string>& args)
591     {
592         /*-------- DEBUG LOG --------*/
593         if (unlikely(LOG_LV_DEBUG == getloglevel()))
594         {
595             boost::format formatter("in_function : protocol_module_base::check_message_result "
596                                     "protocol_module_ip::set_parameter("
597                                     "const std::vector<std::string>& args) : args = %s.");
598             std::string argsdump;
599             for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
600             {
601                 argsdump += *it;
602                 argsdump += " ";
603             }
604             formatter % argsdump;
605             putLogDebug(600015, formatter.str(), __FILE__, __LINE__ );
606         }
607         /*------DEBUG LOG END------*/
608
609         //set check result flag true
610         check_message_result check_result;
611         check_result.flag = true;
612         bool timeout_flag = false;
613         bool reschedule_flag = false;
614         bool no_reschedule_flag = false;
615         bool forward_checked = false;
616         bool sorryuri_checked = false;
617         boost::format formatter;
618         sregex    sorry_uri_regex
619         =    +(    '/' >>
620              *(    alpha |
621                 digit |
622                 ( set = ';', ':', '@', '&', '=' ) |
623                 ( set = '$', '-', '_', '.', '+' ) |
624                 ( set = '!', '*', '\'', '\(', ')', ',' ) |
625                 '%' >> repeat<2>(xdigit)));
626
627         typedef std::vector<std::string>::const_iterator vec_str_it;
628
629         //set forwarded flag true
630         forwarded_for = 1;
631
632         try
633         {
634             vec_str_it it = args.begin();
635             vec_str_it it_end = args.end();
636
637             for (; it != it_end;)
638             {
639
640                 if (*it == "-T" || *it == "--timeout")
641                 {
642                     // timeout
643                     if (!timeout_flag)
644                     {
645                         // not set timeout option
646                         ++it;
647                         // next parameter exist check
648                         if (it != it_end)
649                         {
650                             // next parameter exist
651                             if ((*it).substr(0, 1) == "-" || (*it).substr(0, 2) == "--")
652                             {
653                                 continue;
654                             }
655
656                             try
657                             {
658                                 unsigned long ultimeout = 0;
659                                 ultimeout = boost::lexical_cast<unsigned long>(*it);
660                                 // int maxvalue check
661                                 if (ultimeout > INT_MAX)
662                                 {
663                                     check_result.flag = false;
664                                     formatter.parse("'-T/--timeout' option value '%s' is too large.");
665                                     formatter % *it;
666                                     check_result.message = formatter.str();
667                                     putLogError(600013, check_result.message, __FILE__, __LINE__);
668                                     break;
669                                 }
670                                 else
671                                 {
672                                     timeout_flag = true;
673                                     timeout = ultimeout;
674                                     ++it;
675                                     continue;
676                                 }
677                             }
678                             catch (boost::bad_lexical_cast& e)
679                             {
680                                 // not numeric character
681                                 check_result.flag = false;
682                                 formatter.parse("'-T/--timeout' option value '%s' is not numeric character.");
683                                 formatter % *it;
684                                 check_result.message = formatter.str();
685                                 putLogError(600014, check_result.message, __FILE__, __LINE__);
686                                 break;
687                             }
688                         }
689                         else
690                         {
691                             break;
692                         }
693                     }
694                     else
695                     {
696                         // already set timeout
697                         check_result.flag = false;
698                         check_result.message = "Cannot set multiple option '-T/--timeout'.";
699                         putLogError(600015, check_result.message, __FILE__, __LINE__);
700                         break;
701
702                     }
703                 }
704                 else if (*it == "-R" || *it == "--reschedule")
705                 {
706                     // reschedule
707                     if (!no_reschedule_flag)
708                     {
709                         // not set no-reschedule flag
710                         reschedule_flag = true;
711             reschedule = 1;
712                     }
713                     else
714                     {
715                         // already set no-reschedule flag
716                         check_result.flag = false;
717                         check_result.message = "You have to choose either of reschedule or no-reschedule.";
718                         putLogError(600016, check_result.message, __FILE__, __LINE__);
719                         break;
720                     }
721                 }
722                 else if  (*it == "-N" || *it == "--no-reschedule")
723                 {
724                     // no-reschedule
725                     if (!reschedule_flag)
726                     {
727                         // not set reschedule flag
728                         no_reschedule_flag = true;
729             reschedule = 0;
730
731                     }
732                     else
733                     {
734                         // already set reshcedule flag
735                         check_result.flag = false;
736                         check_result.message = "You have to choose either of reschedule or no-reschedule.";
737                         putLogError(600017, check_result.message, __FILE__, __LINE__);
738                         break;
739                     }
740                 }
741                 //option string = "-F"
742                 else if (*it == "-F" || *it == "--forwarded-for")
743                 {
744                     //set forwarded flag ON
745                     forward_checked = true;
746             forwarded_for = FORWARDED_FOR_ON;
747                 }
748                 //option string  = "-S"
749                 else if (*it == "-S" || *it == "--sorry-uri")
750                 {
751                     //sorryURI flag = OFF
752                     if (!sorryuri_checked)
753                     {
754                         //next item exist
755                         if (++it != it_end)
756                         {
757                             if (!it->empty() && (it->substr(0, 1) == "-" || it->substr(0, 2) == "--"))
758                             {
759                                 //set check result flag false
760                                 check_result.flag = false;
761                                 //set check result message
762                                 check_result.message = "You have to set option value '-S/--sorry-uri'.";
763                                 //loop break
764                                 break;
765                             }
766                             //next option string's length > 127
767                             if (it->size() > MAX_OPTION_SIZE - 1)
768                             {
769                                 std::ostringstream ostr;
770                                 ostr << "'-S/--sorry-uri' option value '" << *it << "' is too long.";
771
772                                 //set check result flag false
773                                 check_result.flag = false;
774                                 //set check result message
775                                 check_result.message = ostr.str();
776                                 putLogError(600018, check_result.message, __FILE__,
777                                             __LINE__ );
778                                 //loop break
779                                 break;
780                             }
781                             //next option string's length <= 127
782                             else
783                             {
784                                 //regex check
785                                 //check OK
786                                 if (regex_match(*it, sorry_uri_regex))
787                                 {
788                                     sorryuri_checked = true;
789                     memcpy(sorry_uri.data(), it->c_str(), it->size());
790                                 }
791                                 //check NG
792                                 else
793                                 {
794                                     std::ostringstream ostr;
795                                     ostr << "'-S/--sorry-uri' option value '" << *it << "' is not a valid URI.";
796
797                                     //set check result flag false
798                                     check_result.flag = false;
799                                     //set check result message
800                                     check_result.message = ostr.str();
801                                     putLogError(600019, check_result.message, __FILE__,
802                                                 __LINE__ );
803                                     break;
804                                 }
805                             }
806                         }
807                         //next item not exist
808                         else
809                         {
810                             //set check result flag false
811                             check_result.flag = false;
812                             //set check result message
813                             check_result.message = "You have to set option value '-S/--sorry-uri'.";
814                             putLogError(600020, check_result.message, __FILE__,
815                                         __LINE__ );
816                             break;
817                         }
818                     }
819                     //sorryURI flag = ON
820                     else
821                     {
822                         //set check result flag false
823                         check_result.flag = false;
824                         //set check result message
825                         check_result.message = "Cannot set multiple option '-S/--sorry-uri'.";
826                         putLogError(600021, check_result.message, __FILE__,
827                                     __LINE__ );
828                         break;
829                     }
830                 }
831                 //others
832                 else
833                 {
834                     //set check result flag false
835                     check_result.flag = false;
836                     //set check result message
837                     check_result.message = "Option error.";
838                     putLogError(600022, check_result.message, __FILE__, __LINE__);
839
840                     break;
841                 }
842                 ++it;
843             }
844
845             // result check
846             if (check_result.flag)
847             {
848                 // set timeout's default value
849                 if (!timeout_flag)
850                 {
851                     timeout = 3600;
852                 }
853
854                 // set reschedule's default value
855                 if (!reschedule_flag)
856                 {
857                     reschedule = 0;
858                 }
859                 //forward flag = OFF
860                 if (!forward_checked)
861                 {
862                     forwarded_for = 0;
863                 }
864
865             }
866             /*-------- DEBUG LOG --------*/
867             if (unlikely(LOG_LV_DEBUG == getloglevel()))
868             {
869                 boost::format formatter("function : protocol_module_ip::check_message_result "
870                                         "protocol_module_ip::set_parameter(const std::vector<std::string>& args) : "
871                                         "timeout = %d, reschedule = %d.");
872                 formatter % timeout  % reschedule;
873                 putLogDebug(600016, formatter.str(), __FILE__, __LINE__);
874             }
875             /*------DEBUG LOG END------*/
876
877             unsigned int data_size = 0;
878             void* data_addr = NULL;
879             data_addr = replication_pay_memory(get_name(), &data_size);
880
881             /*-------- DEBUG LOG --------*/
882             if (unlikely(LOG_LV_DEBUG == getloglevel()))
883             {
884                 putLogDebug(600017, "function : protocol_module_ip::check_message_result protocol_module_ip::"
885                             "set_parameter() : replication_pay_memory() end.", __FILE__, __LINE__);
886                 boost::format formatter("function : protocol_module_ip::check_message_result protocol_module_ip::"
887                                         "set_parameter() : data_addr = &(%d), data_size = %d.");
888                 formatter % data_addr % data_size;
889                 putLogDebug(600018, formatter.str(), __FILE__, __LINE__);
890             }
891
892             /*------DEBUG LOG END------*/
893             if (data_addr == NULL || data_size <= 0)
894             {
895                 // replication area is null
896                 putLogError(600023, "Replication area is null.", __FILE__, __LINE__);
897
898                 /*-------- DEBUG LOG --------*/
899                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
900                 {
901                     putLogDebug(600019, "function : protocol_module_ip::check_message_result "
902                                 "protocol_module_ip::set_parameter() : "
903                                 "Replication area is null.", __FILE__, __LINE__);
904                 }
905                 /*------DEBUG LOG END------*/
906             }
907
908             // create ip_replication_data_processor
909             replication_data_processor = new ip_replication_data_processor(
910                 static_cast<char*>(data_addr),
911                 data_size,
912                 virtual_service_endpoint_tcp,
913                 getloglevel,
914                 putLogFatal,
915                 putLogError,
916                 putLogWarn,
917                 putLogInfo,
918                 putLogDebug );
919
920             /*-------- DEBUG LOG --------*/
921             if (unlikely(LOG_LV_DEBUG == getloglevel()))
922             {
923                 boost::format formatter("new : address = &(%d), size = %lu.");
924                 formatter % static_cast<void*>(replication_data_processor)
925                 % sizeof(ip_replication_data_processor);
926                 putLogDebug(600020, formatter.str(), __FILE__, __LINE__);
927             }
928             /*------DEBUG LOG END------*/
929
930             replication_data_processor->register_replication_area_lock(replication_area_lock);
931
932             /*-------- DEBUG LOG --------*/
933             if (unlikely(LOG_LV_DEBUG == getloglevel()))
934             {
935                 putLogDebug(600021, "function : protocol_module_ip::check_message_result protocol_module_ip::"
936                             "set_parameter() : register_replication_area_lock() end.", __FILE__, __LINE__);
937             }
938             /*------DEBUG LOG END------*/
939
940             replication_data_processor->register_replication_area_unlock(replication_area_unlock);
941
942             /*-------- DEBUG LOG --------*/
943             if (unlikely(LOG_LV_DEBUG == getloglevel()))
944             {
945                 putLogDebug(600022, "function : protocol_module_ip::check_message_result protocol_module_ip::"
946                             "set_parameter() : register_replication_area_unlock() end.", __FILE__, __LINE__);
947             }
948             /*------DEBUG LOG END------*/
949
950             // create ip_session_data_processor
951             ip_data_processor = new ip_session_data_processor(
952                 timeout,
953                 replication_data_processor,
954                 getloglevel,
955                 putLogFatal,
956                 putLogError,
957                 putLogWarn,
958                 putLogInfo,
959                 putLogDebug);
960
961             /*-------- DEBUG LOG --------*/
962             if (unlikely(LOG_LV_DEBUG == getloglevel()))
963             {
964                 boost::format formatter("new : address = &(%d), size = %lu.");
965                 formatter % static_cast<void*>(ip_data_processor)
966                 % sizeof(ip_session_data_processor);
967                 putLogDebug(600023, formatter.str(), __FILE__, __LINE__);
968             }
969             /*------DEBUG LOG END------*/
970
971             // restore data from replication area
972             ip_replication_data* redata = replication_data_processor->get_replication_area();
973
974             /*-------- DEBUG LOG --------*/
975             if (unlikely(LOG_LV_DEBUG == getloglevel()))
976             {
977                 putLogDebug(600024, "function : protocol_module_ip::check_message_result protocol_module_ip::"
978                             "set_parameter() : get_replication_area() end.", __FILE__, __LINE__);
979             }
980             /*------DEBUG LOG END------*/
981
982             replication_area_lock();
983
984             /*-------- DEBUG LOG --------*/
985             if (unlikely(LOG_LV_DEBUG == getloglevel()))
986             {
987                 putLogDebug(600025, "function : protocol_module_ip::check_message_result protocol_module_ip::"
988                             "set_parameter() : replication_area_lock() end.", __FILE__, __LINE__);
989             }
990             /*------DEBUG LOG END------*/
991
992             ip_data_processor->read_session_data_from_replication_area(redata);
993
994             /*-------- DEBUG LOG --------*/
995             if (unlikely(LOG_LV_DEBUG == getloglevel()))
996             {
997                 putLogDebug(600026, "function : protocol_module_ip::check_message_result protocol_module_ip::"
998                             "set_parameter() : read_session_data_from_replication_area() end.", __FILE__, __LINE__);
999             }
1000             /*------DEBUG LOG END------*/
1001
1002             replication_area_unlock();
1003
1004             /*-------- DEBUG LOG --------*/
1005             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1006             {
1007                 putLogDebug(600027, "function : protocol_module_ip::check_message_result protocol_module_ip::"
1008                             "set_parameter() : replication_area_unlock() end.", __FILE__, __LINE__);
1009             }
1010             /*------DEBUG LOG END------*/
1011
1012         }
1013         catch (const std::bad_alloc& ba)
1014         {
1015             if (replication_data_processor)
1016             {
1017                 delete replication_data_processor;
1018                 replication_data_processor = NULL;
1019             }
1020
1021             if (ip_data_processor)
1022             {
1023                 delete ip_data_processor;
1024                 ip_data_processor = NULL;
1025             }
1026
1027             std::cerr << "protocol_module_ip::set_parameter() : exception : Could not allocate memory." << std::endl;
1028             check_result.flag = false;
1029             check_result.message = "Could not allocate memory.";
1030             putLogError(600024, check_result.message, __FILE__, __LINE__);
1031         }
1032         catch (const std::exception& ex)
1033         {
1034             if (replication_data_processor)
1035             {
1036                 delete replication_data_processor;
1037                 replication_data_processor = NULL;
1038             }
1039
1040             if (ip_data_processor)
1041             {
1042                 delete ip_data_processor;
1043                 ip_data_processor = NULL;
1044             }
1045
1046             check_result.flag = false;
1047             std::cerr << "protocol_module_ip::set_parameter() : exception : error = " << ex.what() << std::endl;
1048             boost::format formatter("function : protocol_module_ip::check_message_result "
1049                                     "protocol_module_ip::set_parameter() : exception : error = %s.");
1050             formatter % ex.what();
1051             putLogError(600025, formatter.str(), __FILE__, __LINE__);
1052         }
1053         catch (...)
1054         {
1055             if (replication_data_processor)
1056             {
1057                 delete replication_data_processor;
1058                 replication_data_processor = NULL;
1059             }
1060
1061             if (ip_data_processor)
1062             {
1063                 delete ip_data_processor;
1064                 ip_data_processor = NULL;
1065             }
1066
1067             check_result.flag = false;
1068             std::cerr << "protocol_module_ip::set_parameter() : Unknown exception." << std::endl;
1069             putLogError(600026, "function : protocol_module_ip::check_message_result protocol_module_ip::"
1070                         "set_parameter() : Unknown exception.", __FILE__, __LINE__);
1071         }
1072         /*-------- DEBUG LOG --------*/
1073         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1074         {
1075             boost::format formatter("out_function : protocol_module_ip::check_message_result "
1076                                     "protocol_module_ip::set_parameter("
1077                                     "const std::vector<std::string>& args) : return_value = ("
1078                                     "check_message_result.flag = %d, check_message_result.message = %s).");
1079             formatter % check_result.flag % check_result.message;
1080             putLogDebug(600028, formatter.str(), __FILE__, __LINE__);
1081         }
1082         /*------DEBUG LOG END------*/
1083         return check_result;
1084     }
1085
1086     //! parameter add
1087     //! @param[in] module paramter string list
1088     //! @return    result.flag true is parameter is noproblem.
1089     //! @return result.flag false is paramter is problem.
1090     protocol_module_base::check_message_result protocol_module_ip::add_parameter(const std::vector<
1091             std::string>& args)
1092     {
1093         /*-------- DEBUG LOG --------*/
1094         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1095         {
1096             boost::format formatter("in_function : protocol_module_ip::check_message_result protocol_module_ip::"
1097                                     "add_parameter(const std::vector<std::string>& args) : args = %s.");
1098             std::string argsdump;
1099             for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
1100             {
1101                 argsdump += *it;
1102             }
1103             formatter % argsdump;
1104             putLogDebug(600029, formatter.str(), __FILE__, __LINE__);
1105         }
1106         /*------DEBUG LOG END------*/
1107         check_message_result check_result;
1108         //set check result flag true
1109         check_result.flag = true;
1110
1111         //param list is not empty
1112         if (!args.empty())
1113         {
1114             //set check result flag false
1115             check_result.flag = false;
1116             //set check result message
1117             check_result.message = "Cannot add option.";
1118             putLogError(600027, check_result.message, __FILE__, __LINE__ );
1119         }
1120
1121         /*-------- DEBUG LOG --------*/
1122         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1123         {
1124             boost::format formatter("out_function : protocol_module_ip::check_message_result "
1125                                     "protocol_module_ip::add_parameter(const std::vector<std::string>& args) : "
1126                                     "return_value = (check_message_result.flag = %d, check_message_result.message = %s).");
1127             formatter % check_result.flag % check_result.message;
1128             putLogDebug(600030, formatter.str(), __FILE__, __LINE__);
1129         }
1130         /*------DEBUG LOG END------*/
1131         return check_result;
1132     }
1133
1134     //! get option info
1135     //! @param[out] module paramter string
1136     void protocol_module_ip::get_option_info(std::string& option)
1137     {
1138         /*-------- DEBUG LOG --------*/
1139         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1140         {
1141             putLogDebug(300031, "in_function : void protocol_module_ip::get_option_info("
1142                         "std::string& option).", __FILE__, __LINE__);
1143         }
1144         /*------DEBUG LOG END------*/
1145
1146         boost::format option_formatter("--timeout %d%s %s --sorry-uri '%s'");
1147         option_formatter % timeout % (forwarded_for ? " --forwarded-for" : "") % (reschedule ? "--reschedule" : "--no-reschedule") % sorry_uri.c_array();
1148         option.assign(option_formatter.str());     
1149
1150         /*-------- DEBUG LOG --------*/
1151         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1152         {
1153             boost::format formatter("out_function : void protocol_module_ip::get_option_info("
1154                                     "std::string& option) : option = %s.");
1155             formatter % option;
1156             putLogDebug(300039, formatter.str(), __FILE__, __LINE__);
1157         }
1158         /*------DEBUG LOG END------*/
1159     }
1160
1161     //! TCP/IP scheduled function registation.
1162     //! @param[in] schedule module TCP/IP scheduled function object type
1163     void protocol_module_ip::register_schedule(tcp_schedule_func_type inschedule)
1164     {
1165         /*-------- DEBUG LOG --------*/
1166         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1167         {
1168             putLogDebug(600031, "in_function : void protocol_module_ip::register_schedule("
1169                         "tcp_schedule_func_type inschedule).", __FILE__, __LINE__);
1170         }
1171         /*------DEBUG LOG END------*/
1172         schedule_tcp = inschedule;
1173         /*-------- DEBUG LOG --------*/
1174         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1175         {
1176             putLogDebug(600032, "out_function : void protocol_module_ip::register_schedule("
1177                         "tcp_schedule_func_type inschedule).", __FILE__, __LINE__);
1178         }
1179         /*------DEBUG LOG END------*/
1180     }
1181
1182     //! UDP scheduled function registation
1183     //! @param[in] schedule module UDP scheduled funtion object type
1184     void protocol_module_ip::register_schedule(udp_schedule_func_type inschedule)
1185     {
1186         /*-------- DEBUG LOG --------*/
1187         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1188         {
1189             putLogDebug(600033, "in/out_function : void protocol_module_ip::register_schedule("
1190                         "udp_schedule_func_type inschedule).", __FILE__, __LINE__);
1191         }
1192         /*------DEBUG LOG END------*/
1193     }
1194
1195     //! called from session initialzie use in upstream_thread
1196     //! @param[in]    upstream thread id.
1197     //! @param[in]    downstream thread id
1198     //! @return        session use EVENT mode.
1199     protocol_module_base::EVENT_TAG protocol_module_ip::handle_session_initialize(
1200         const boost::thread::id up_thread_id, const boost::thread::id down_thread_id,
1201         const boost::asio::ip::tcp::endpoint& client_endpoint_tcp,
1202         const boost::asio::ip::udp::endpoint& client_endpoint_udp)
1203     {
1204         /*-------- DEBUG LOG --------*/
1205         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1206         {
1207             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1208                                     "handle_session_initialize(const boost::thread::id up_thread_id, "
1209                                     "const boost::thread::id down_thread_id, "
1210                                     "const boost::asio::ip::tcp::endpoint& client_endpoint_tcp, "
1211                                     "const boost::asio::ip::udp::endpoint& client_endpoint_udp) : "
1212                                     "up_thread_id = %d, down_thread_id = %d.");
1213             formatter % up_thread_id % down_thread_id;
1214             putLogDebug(600034, formatter.str(), __FILE__, __LINE__);
1215         }
1216         /*------DEBUG LOG END------*/
1217
1218         EVENT_TAG status = FINALIZE;
1219         unsigned int ip_hash = 0;
1220
1221         //session thread initialization
1222         try
1223         {
1224             thread_data_ptr p_up(new session_thread_data_ip);
1225             /*-------- DEBUG LOG --------*/
1226             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1227             {
1228                 boost::format formatter("new : address = &(%d), size = %lu.");
1229                 formatter % static_cast<void*>(p_up.get()) % sizeof(session_thread_data_ip);
1230                 putLogDebug(600035, formatter.str(), __FILE__, __LINE__);
1231             }
1232             /*------DEBUG LOG END------*/
1233
1234             //calculate ip address's hash
1235             ip_hash = l7vs_ip_service_calc_hash(client_endpoint_tcp);
1236
1237             p_up->thread_id                = up_thread_id;
1238             p_up->thread_division            = THREAD_DIVISION_UP_STREAM;
1239             p_up->pair_thread_id            = down_thread_id;
1240             p_up->accept_end_flag            = ACCEPT_END_FLAG_OFF;
1241             p_up->end_flag                = END_FLAG_OFF;
1242             p_up->sorry_flag                = SORRY_FLAG_OFF;
1243             p_up->switch_flag                = SWITCH_FLAG_OFF;
1244             p_up->last_status                = INITIALIZE;
1245             p_up->client_endpoint            = client_endpoint_tcp;
1246             p_up->data_buffer                = new char[MAX_BUFFER_SIZE];
1247             p_up->data_buffer_size            = MAX_BUFFER_SIZE;
1248             p_up->data_length                = 0;
1249             p_up->data_offset                = 0;
1250             p_up->current_message_rest_size        = 0;
1251             p_up->data_state                = HTTP_START;
1252             p_up->ip_hash                = ip_hash;
1253
1254             /*-------- DEBUG LOG --------*/
1255             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1256             {
1257                 // data dump
1258                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1259                                         "handle_session_initialize() : session_thread_data_ip(upthread) : "
1260                                         "thread_id = %d, thread_division = %d, "
1261                                         "pair_thread_id = %d, accept_end_flag = %d, end_flag = %d, "
1262                                         "sorry_flag = %d, switch_flag = %d, last_status = %d, client_endpoint = [%s]:%d, data_buffer = &(%d), "
1263                                         "data_buffer_size = %d, data_length = %d, data_offset = %d, current_message_rest_size = %d, data_state = %d, "
1264                                         "ip_hash = %d.");
1265                 formatter % p_up->thread_id
1266                 % p_up->thread_division
1267                 % p_up->pair_thread_id
1268                 % p_up->accept_end_flag
1269                 % p_up->end_flag
1270                 % p_up->sorry_flag
1271                 % p_up->switch_flag
1272                 % p_up->last_status
1273                 % p_up->client_endpoint.address().to_string()
1274                 % p_up->client_endpoint.port()
1275                 % p_up->data_buffer
1276                 % p_up->data_buffer_size
1277                 % p_up->data_length
1278                 % p_up->data_offset
1279                 % p_up->current_message_rest_size
1280                 % p_up->data_state
1281                 % p_up->ip_hash;
1282
1283
1284                 putLogDebug(600036, formatter.str(), __FILE__, __LINE__);
1285             }
1286             /*------DEBUG LOG END------*/
1287
1288             thread_data_ptr p_down(new session_thread_data_ip);
1289
1290             /*-------- DEBUG LOG --------*/
1291             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1292             {
1293                 boost::format formatter("new : address = &(%d), size = %lu.");
1294                 formatter % static_cast<void*>(p_down.get()) % sizeof(session_thread_data_ip);
1295                 putLogDebug(600037, formatter.str(), __FILE__, __LINE__);
1296             }
1297             /*------DEBUG LOG END------*/
1298
1299             p_down->thread_id                = down_thread_id;
1300             p_down->thread_division            = THREAD_DIVISION_DOWN_STREAM;
1301             p_down->pair_thread_id            = up_thread_id;
1302             p_down->accept_end_flag            = ACCEPT_END_FLAG_OFF;
1303             p_down->end_flag                = END_FLAG_OFF;
1304             p_down->sorry_flag                = SORRY_FLAG_OFF;
1305             p_down->switch_flag                = SWITCH_FLAG_OFF;
1306             p_down->last_status                = INITIALIZE;
1307             p_down->client_endpoint            = client_endpoint_tcp;
1308             p_down->data_buffer                = new char[MAX_BUFFER_SIZE];
1309             p_down->data_buffer_size            = MAX_BUFFER_SIZE;
1310             p_down->data_length                = 0;
1311             p_down->data_offset                = 0;
1312             p_down->current_message_rest_size        = 0;
1313             p_down->data_state                = HTTP_START;
1314             p_down->ip_hash                = ip_hash;
1315
1316             /*-------- DEBUG LOG --------*/
1317             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1318             {
1319                 // data_dump
1320                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1321                                         "handle_session_initialize() : session_thread_data_ip(downthread) : "
1322                                         "thread_id = %d, thread_division = %d, "
1323                                         "pair_thread_id = %d, accept_end_flag = %d, end_flag = %d, "
1324                                         "sorry_flag = %d, switch_flag = %d, last_status = %d, client_endpoint = [%s]:%d, data_buffer = &(%d), "
1325                                         "data_buffer_size = %d, data_length = %d, data_offset = %d, current_message_rest_size = %d, data_state = %d, "
1326                                         "ip_hash = %d.");
1327                 formatter % p_down->thread_id
1328                 % p_down->thread_division
1329                 % p_down->pair_thread_id
1330                 % p_down->accept_end_flag
1331                 % p_down->end_flag
1332                 % p_down->sorry_flag
1333                 % p_down->switch_flag
1334                 % p_down->last_status
1335                 % p_down->client_endpoint.address().to_string()
1336                 % p_down->client_endpoint.port()
1337                 % p_down->data_buffer
1338                 % p_down->data_buffer_size
1339                 % p_down->data_length
1340                 % p_down->data_offset
1341                 % p_down->current_message_rest_size
1342                 % p_down->data_state
1343                 % p_down->ip_hash;
1344                 putLogDebug(600038, formatter.str(), __FILE__, __LINE__);
1345             }
1346             /*------DEBUG LOG END------*/
1347
1348
1349             boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
1350
1351             session_thread_data_map[up_thread_id] = p_up;
1352             session_thread_data_map[down_thread_id] = p_down;
1353
1354             //set return status
1355             status = ACCEPT;
1356             //save last status
1357             session_thread_data_map[up_thread_id]->last_status = status;
1358             session_thread_data_map[down_thread_id]->last_status = REALSERVER_RECV;
1359         }
1360         catch (const std::bad_alloc&)
1361         {
1362             std::cerr << "protocol_module_ip::handle_session_initialize() : exception : Could not allocate memory." << std::endl;
1363             boost::format formatter("Could not allocate memory. thread id : %d.");
1364             formatter % boost::this_thread::get_id();
1365             putLogError(600028, formatter.str(), __FILE__, __LINE__);
1366             status = FINALIZE;
1367         }
1368         catch (const std::exception& ex)
1369         {
1370             std::cerr << "protocol_module_ip::handle_session_initialize() : exception : error = " << ex.what() << "." << std::endl;
1371             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1372                                     "handle_session_initialize() : exception : error = %s. thread id : %d.");
1373             formatter % ex.what() % boost::this_thread::get_id();
1374             putLogError(600029, formatter.str(), __FILE__, __LINE__);
1375             status = FINALIZE;
1376         }
1377         catch (...)
1378         {
1379             std::cerr << "protocol_module_ip::handle_session_initialize() : Unknown exception." << std::endl;
1380             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1381                                     "handle_session_initialize() : Unknown exception. thread id : %d.");
1382             formatter % boost::this_thread::get_id();
1383             putLogError(600030, formatter.str(), __FILE__, __LINE__);
1384             status = FINALIZE;
1385         }
1386
1387         /*-------- DEBUG LOG --------*/
1388         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1389         {
1390             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1391                                     "handle_session_initialize(const boost::thread::id up_thread_id, "
1392                                     "const boost::thread::id down_thread_id, "
1393                                     "const boost::asio::ip::tcp::endpoint& client_endpoint_tcp, "
1394                                     "const boost::asio::ip::udp::endpoint& client_endpoint_udp) : return_value = %d. "
1395                                     "thread id : %d.");
1396             formatter % status % boost::this_thread::get_id();
1397             putLogDebug(600039, formatter.str(), __FILE__, __LINE__);
1398         }
1399         /*------DEBUG LOG END------*/
1400
1401         return status;
1402     }
1403     //! called from session finalize use in upstream thread.
1404     //! @param[in]    upstream thread id.
1405     //! @param[in]    downstream thread id
1406     //! @return        session use EVENT mode.
1407     protocol_module_base::EVENT_TAG protocol_module_ip::handle_session_finalize(
1408         const boost::thread::id up_thread_id, const boost::thread::id down_thread_id)
1409     {
1410         /*-------- DEBUG LOG --------*/
1411         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1412         {
1413             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1414                                     "handle_session_finalize(const boost::thread::id up_thread_id, "
1415                                     "const boost::thread::id down_thread_id) : "
1416                                     "up_thread_id = %d, down_thread_id = %d.");
1417             formatter % up_thread_id % down_thread_id;
1418             putLogDebug(600040, formatter.str(), __FILE__, __LINE__);
1419         }
1420         /*------DEBUG LOG END------*/
1421         EVENT_TAG status = STOP;
1422         thread_data_ptr p_up;
1423         thread_data_ptr p_down;
1424         session_thread_data_map_it session_thread_data_it;
1425
1426         //session thread free
1427         try
1428         {
1429             boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
1430
1431             session_thread_data_it = session_thread_data_map.find(up_thread_id);
1432             if (session_thread_data_it != session_thread_data_map.end())
1433             {
1434                 p_up = session_thread_data_it->second;
1435                 /*-------- DEBUG LOG --------*/
1436                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
1437                 {
1438                     boost::format formatter("delete : address = &(%d).");
1439                     formatter % static_cast<void*>(p_up->data_buffer);
1440                     putLogDebug(600041, formatter.str(), __FILE__, __LINE__);
1441                 }
1442                 /*------DEBUG LOG END------*/
1443
1444                 delete p_up->data_buffer;
1445                 /*-------- DEBUG LOG --------*/
1446                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
1447                 {
1448                     boost::format formatter("delete : address = &(%d).");
1449                     formatter % static_cast<void*>(p_up.get());
1450                     putLogDebug(600042, formatter.str(), __FILE__, __LINE__);
1451                 }
1452                 /*------DEBUG LOG END------*/
1453
1454                 session_thread_data_map.erase(up_thread_id);
1455             }
1456
1457             session_thread_data_it = session_thread_data_map.find(down_thread_id);
1458             if (session_thread_data_it != session_thread_data_map.end())
1459             {
1460
1461                 p_down = session_thread_data_it->second;
1462                 /*-------- DEBUG LOG --------*/
1463                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
1464                 {
1465                     boost::format formatter("delete : address = &(%d).");
1466                     formatter % static_cast<void*>(p_up->data_buffer);
1467                     putLogDebug(600043, formatter.str(), __FILE__, __LINE__);
1468                 }
1469                 /*------DEBUG LOG END------*/
1470
1471                 delete p_down->data_buffer;
1472                 /*-------- DEBUG LOG --------*/
1473                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
1474                 {
1475                     boost::format formatter("delete : address = &(%d).");
1476                     formatter % static_cast<void*>(p_down.get());
1477                     putLogDebug(600044, formatter.str(), __FILE__, __LINE__);
1478                 }
1479                 /*------DEBUG LOG END------*/
1480
1481                 session_thread_data_map.erase(down_thread_id);
1482             }
1483
1484             //set return status
1485             status = STOP;
1486         }
1487         catch (const std::exception& ex)
1488         {
1489             std::cerr << "protocol_module_ip::handle_session_finalize() : exception : error = " << ex.what() << "." << std::endl;
1490             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1491                                     "handle_session_finalize() : exception : error = %s. thread id : %d.");
1492             formatter % ex.what() % boost::this_thread::get_id();
1493             putLogError(600031, formatter.str(), __FILE__, __LINE__ );
1494             status = STOP;
1495         }
1496         catch (...)
1497         {
1498             std::cerr << "protocol_module_ip::handle_session_finalize() : Unknown exception." << std::endl;
1499             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1500                                     "handle_session_finalize() : "
1501                                     "Unknown exception. thread id : %d.");
1502             formatter % boost::this_thread::get_id();
1503             putLogError(600032, formatter.str(), __FILE__, __LINE__ );
1504             status = STOP;
1505         }
1506         /*-------- DEBUG LOG --------*/
1507         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1508         {
1509             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1510                                     "handle_session_finalize(const boost::thread::id up_thread_id, "
1511                                     "const boost::thread::id down_thread_id) : return_value = %d. thread id : %d.");
1512             formatter % status % boost::this_thread::get_id();
1513             putLogDebug(600045, formatter.str(), __FILE__, __LINE__ );
1514         }
1515         /*------DEBUG LOG END------*/
1516         return status;
1517     }
1518
1519     //! called from after session accept.in client socket use in upstream thread.
1520     //! @param[in]    upstream thread id.
1521     //! @return        session use EVENT mode.
1522     protocol_module_base::EVENT_TAG protocol_module_ip::handle_accept(const boost::thread::id thread_id)
1523     {
1524         /*-------- DEBUG LOG --------*/
1525         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1526         {
1527             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1528                                     "handle_accept(const boost::thread::id thread_id) : thread_id = %d.");
1529             formatter % thread_id;
1530             putLogDebug(600046, formatter.str(), __FILE__, __LINE__ );
1531         }
1532         /*------DEBUG LOG END------*/
1533
1534         EVENT_TAG status = FINALIZE;
1535         thread_data_ptr session_data_ptr;
1536         session_thread_data_map_it session_thread_it;
1537
1538         try
1539         {
1540             {
1541                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
1542
1543                 session_thread_it = session_thread_data_map.find(thread_id);
1544                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
1545                 {
1546                     boost::format formatter("Invalid thread id. thread id : %d.");
1547                     formatter % boost::this_thread::get_id();
1548                     putLogError(600033, formatter.str(), __FILE__, __LINE__ );
1549                     throw -1;
1550                 }
1551
1552                 session_data_ptr = session_thread_it->second;
1553             }
1554
1555             //set accept end flag ON
1556             session_data_ptr->accept_end_flag = ACCEPT_END_FLAG_ON;
1557
1558             /*-------- DEBUG LOG --------*/
1559             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1560             {
1561                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1562                                         "handle_accept(const boost::thread::id thread_id) : ACCEPT_END_FLAG_ON. thread id : %d.");
1563                 formatter % boost::this_thread::get_id();
1564                 putLogDebug(600047, formatter.str(), __FILE__, __LINE__ );
1565             }
1566             /*------DEBUG LOG END------*/
1567
1568             //sorry flag on
1569             if (session_data_ptr->sorry_flag == SORRY_FLAG_ON)
1570             {
1571                 //set return status
1572                 status = SORRYSERVER_SELECT;
1573             }
1574             //sorry flag off
1575             else
1576             {
1577                 //set return status
1578                 status = REALSERVER_SELECT;
1579             }
1580
1581             //set last status
1582             session_data_ptr->last_status = status;
1583         }
1584         catch (int e)
1585         {
1586             /*-------- DEBUG LOG --------*/
1587             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1588             {
1589                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1590                                         "handle_accept() : catch exception e = %d. thread id : %d.");
1591                 formatter % e % boost::this_thread::get_id();
1592                 putLogDebug(600048, formatter.str(), __FILE__, __LINE__ );
1593             }
1594             /*------DEBUG LOG END------*/
1595             status = FINALIZE;
1596         }
1597         catch (const std::exception& ex)
1598         {
1599             std::cerr << "protocol_module_ip::handle_accept() : exception : error = " << ex.what() << "." << std::endl;
1600             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1601                                     "handle_accept() : exception : error = %s. thread id : %d.");
1602             formatter % ex.what() % boost::this_thread::get_id();
1603             putLogError(600034, formatter.str(), __FILE__, __LINE__ );
1604             status = FINALIZE;
1605         }
1606         catch (...)
1607         {
1608             std::cerr << "protocol_module_ip::handle_accept() : Unknown exception." << std::endl;
1609             boost::format formatter("function : protocol_module_base::EVENT_TAG "
1610                                     "protocol_module_ip::handle_accept() : "
1611                                     "Unknown exception. thread id : %d.");
1612             formatter % boost::this_thread::get_id();
1613             putLogError(600035, formatter.str(), __FILE__, __LINE__ );
1614             status = FINALIZE;
1615         }
1616
1617         /*-------- DEBUG LOG --------*/
1618         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1619         {
1620             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1621                                     "handle_accept(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
1622             formatter % status % boost::this_thread::get_id();
1623             putLogDebug(600049, formatter.str(), __FILE__, __LINE__ );
1624         }
1625         /*------DEBUG LOG END------*/
1626
1627         return status;
1628     }
1629
1630     //! called from after session recv in client socket. use in upstream thread.
1631     //! @param[in]    upstream thread id
1632     //! @param[in]    recive buffer refarence.
1633     //! @param[in]    recive length
1634     //! @return        session use EVENT mode.
1635     protocol_module_base::EVENT_TAG protocol_module_ip::handle_client_recv(const boost::thread::id thread_id,
1636             const boost::array<char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
1637     {
1638
1639         /*-------- DEBUG LOG --------*/
1640         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1641         {
1642             size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
1643             std::string buffer;
1644             dump_memory(recvbuffer.data(), buffer_size, buffer);
1645             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1646                                     "handle_client_recv(const boost::thread::id thread_id, "
1647                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
1648                                     "const size_t recvlen) : thread_id = %d, recvbuffer = %s, recvlen = %d.");
1649             formatter % thread_id % buffer % recvlen;
1650             putLogDebug(600050, formatter.str(), __FILE__, __LINE__ );
1651         }
1652         /*------DEBUG LOG END------*/
1653
1654         EVENT_TAG status                = FINALIZE;
1655         bool find_ret                    = false;
1656         size_t http_header_offset            = 0;
1657         size_t http_header_len                = 0;
1658         size_t http_header_content_length_offset    = 0;
1659         size_t http_header_content_length_len        = 0;
1660         int content_length_value            = 0;
1661         const size_t CR_LF_LEN                = 2; //length of "\r\n"
1662         const size_t CR_LF_CR_LF_LEN            = 4; //length of "\r\n\r\n"
1663         session_thread_data_map_it            session_thread_it;
1664         thread_data_ptr                    session_data_ptr;
1665         http_utility::CHECK_RESULT_TAG             check_ret;
1666
1667         std::string content_length;
1668         cmatch regex_ret;
1669         cregex content_length_regex = icase("Content-Length") >> ":" >> *~_d >> (s1 = +_d) >> *~_d;
1670
1671         //parameter check
1672         if (unlikely(recvlen > recvbuffer.size()))
1673         {
1674             std::cerr << "protocol_module_ip::handle_client_recv() : Data size bigger than buffer size." << std::endl;
1675             boost::format formatter("Data size bigger than buffer size. thread id : %d.");
1676             formatter % boost::this_thread::get_id();
1677             putLogError(600036, formatter.str(), __FILE__, __LINE__ );
1678
1679             /*-------- DEBUG LOG --------*/
1680             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1681             {
1682                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1683                                         "handle_client_recv(const boost::thread::id thread_id, "
1684                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
1685                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
1686                 formatter % FINALIZE % boost::this_thread::get_id();
1687                 putLogDebug(600051, formatter.str(), __FILE__, __LINE__ );
1688             }
1689             /*------DEBUG LOG END------*/
1690             return status;
1691         }
1692
1693         try
1694         {
1695             {
1696                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
1697
1698                 session_thread_it = session_thread_data_map.find(thread_id);
1699                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
1700                 {
1701
1702                     boost::format formatter("Invalid thread id. thread id : %d.");
1703                     formatter % boost::this_thread::get_id();
1704                     putLogError(600037, formatter.str(), __FILE__, __LINE__ );
1705                     throw -1;
1706                 }
1707
1708                 session_data_ptr = session_thread_it->second;
1709             }
1710
1711             //end flag on
1712             if (session_data_ptr->end_flag == END_FLAG_ON)
1713             {
1714                 //set return status
1715                 status = CLIENT_RECV;
1716             }
1717             //end flag off
1718             else
1719             {
1720                 //copy data from recvbuffer
1721                 if (!get_data_from_recvbuffer(session_data_ptr, recvbuffer, recvlen))
1722                 {
1723                     //copy failed
1724                     std::cerr << "protocol_module_ip::handle_client_recv() : Data size bigger than buffer size." << std::endl;
1725                     boost::format formatter("Data size bigger than buffer size. thread id : % id.");
1726                     formatter % boost::this_thread::get_id();
1727                     putLogError(600038, formatter.str(), __FILE__, __LINE__);
1728                     status = FINALIZE;
1729                 }
1730                 else
1731                 {
1732                     //data state is HTTP_START
1733                     if (session_data_ptr->data_state == HTTP_START)
1734                     {
1735                         //search http header
1736                         find_ret = http_utility::find_http_header_all(session_data_ptr->data_buffer + session_data_ptr->data_offset,
1737                                                         session_data_ptr->data_length,
1738                                                         http_header_offset,
1739                                                         http_header_len
1740                                                        );
1741
1742                         /*-------- DEBUG LOG --------*/
1743                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1744                         {
1745                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1746                                                     "handle_client_recv() : call find_http_header_all : "
1747                                                     "return_value = %d. thread id : %d.");
1748                             formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
1749                             putLogDebug(600052, formatter.str(), __FILE__, __LINE__ );
1750                         }
1751                         /*------DEBUG LOG END------*/
1752
1753                         //search http header result is NG
1754                         if (!find_ret)
1755                         {
1756                             //data size bigger than max buffer size
1757                             if (session_data_ptr->data_length >= MAX_IP_MODULE_BUFFER_SIZE - recvbuffer.size())
1758                             {
1759                                 //set data state UNKNOWN
1760                                 session_data_ptr->data_state = UNKNOWN;
1761                                 //set current message rest size
1762                                 session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
1763                             }
1764                         }
1765                         //search http header result is OK
1766                         else
1767                         {
1768                             //check http method and version
1769                             check_ret = http_utility::check_http_method_and_version(session_data_ptr->data_buffer,
1770                                         session_data_ptr->data_length);
1771                             /*-------- DEBUG LOG --------*/
1772                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1773                             {
1774                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1775                                                         "handle_client_recv() : call check_http_method_and_version : "
1776                                                         "return_value = %d. thread id : %d.");
1777                                 formatter % check_ret % boost::this_thread::get_id();
1778                                 putLogDebug(600053, formatter.str(), __FILE__, __LINE__ );
1779                             }
1780                             /*------DEBUG LOG END------*/
1781
1782                             //check http method and version result is NG
1783                             if (check_ret == http_utility::CHECK_NG)
1784                             {
1785                                 //set data state UNKNOWN
1786                                 session_data_ptr->data_state = UNKNOWN;
1787                                 //set current message rest size
1788                                 session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
1789                             }
1790                             //check http method and version result is OK
1791                             else
1792                             {
1793                                 //search Content_Length header
1794                                 find_ret = http_utility::find_http_header_content_length(session_data_ptr->data_buffer+session_data_ptr->data_offset,
1795                                            session_data_ptr->data_length,
1796                                            http_header_content_length_offset,
1797                                            http_header_content_length_len);
1798                                 /*-------- DEBUG LOG --------*/
1799                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
1800                                 {
1801                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1802                                                             "handle_client_recv() : call find_http_header_content_length : "
1803                                                             "return_value = %d. thread id : %d.");
1804                                     formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
1805                                     putLogDebug(600054, formatter.str(), __FILE__, __LINE__ );
1806                                 }
1807                                 /*------DEBUG LOG END------*/
1808
1809                                 //search Content_Length result is OK
1810                                 if (find_ret)
1811                                 {
1812                                     //set content length string
1813                                     content_length.assign(session_data_ptr->data_buffer+session_data_ptr->data_offset + http_header_content_length_offset,
1814                                                           http_header_content_length_len );
1815                                     find_ret = regex_search(content_length.c_str(), regex_ret, content_length_regex);
1816
1817                                     //"content-length: ddd\r\n"
1818                                     if ( find_ret)
1819                                     {
1820                                         content_length = content_length.substr(
1821                                                              regex_ret.position(1),
1822                                                              regex_ret.length(1));
1823
1824                                         //set content length value
1825                                         content_length_value = boost::lexical_cast<int>(content_length);
1826                                     }
1827
1828                                     //http_header context is "\r\n\r\n" only
1829                                     if (http_header_len == 0)
1830                                     {
1831                                         //set current message rest size
1832                                         session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + content_length_value + CR_LF_LEN;
1833                                     }
1834                                     else
1835                                     {
1836                                         //set current message rest size
1837                                         session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + content_length_value + CR_LF_CR_LF_LEN;
1838                                     }
1839                                 }
1840                                 //search Content_Length result is NG
1841                                 else
1842                                 {
1843                                     //http_header context is "\r\n\r\n" only
1844                                     if (http_header_len == 0)
1845                                     {
1846                                         //set current message rest size
1847                                         session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + CR_LF_LEN;
1848                                     }
1849                                     else
1850                                     {
1851                                         //set current message rest size
1852                                         session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + CR_LF_CR_LF_LEN;
1853                                     }
1854
1855                                 }
1856
1857                                 //set data state HTTP_HEADER
1858                                 session_data_ptr->data_state = HTTP_HEADER;
1859
1860                             }
1861                         }
1862                     }
1863                     //data state is UNKNOWN
1864                     else if (session_data_ptr->data_state == UNKNOWN)
1865                     {
1866                         //set current message rest size
1867                         session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
1868                     }
1869                     else
1870                     {
1871                         //none
1872                     }
1873
1874                     //data state is HTTP_START
1875                     if (session_data_ptr->data_state == HTTP_START )
1876                     {
1877                         status = CLIENT_RECV;
1878                     }
1879                     //data state is not HTTP_START
1880                     else
1881                     {
1882                         //sorry flag is on
1883                         if (session_data_ptr->sorry_flag == SORRY_FLAG_ON)
1884                         {
1885                             //set return status
1886                             status = SORRYSERVER_CONNECT;
1887                         }
1888                         //sorry flag is off
1889                         else
1890                         {
1891                             //set return status
1892                             status = REALSERVER_CONNECT;
1893                         }
1894                     }
1895                 }
1896             }
1897
1898             //set last status
1899             session_data_ptr->last_status = status;
1900         }
1901         catch (int e)
1902         {
1903             /*-------- DEBUG LOG --------*/
1904             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1905             {
1906                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1907                                         "handle_client_recv() : catch exception e = %d. thread id : %d.");
1908                 formatter % e % boost::this_thread::get_id();
1909                 putLogDebug(600055, formatter.str(), __FILE__, __LINE__ );
1910             }
1911             /*------DEBUG LOG END------*/
1912             status = FINALIZE;
1913         }
1914         catch (const boost::bad_lexical_cast& )
1915         {
1916             std::cerr << "protocol_module_ip::handle_client_recv() : exception : " <<  "Content_Length field's value is invalid." << std::endl;
1917             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::handle_client_recv() : "
1918                                     "Content_Length field's value is invalid. thread id : %d.");
1919             formatter % boost::this_thread::get_id();
1920             putLogError(600039, formatter.str(), __FILE__, __LINE__ );
1921             status = FINALIZE;
1922         }
1923         catch (const std::exception& ex)
1924         {
1925             std::cerr << "protocol_module_ip::handle_client_recv() : exception : error = " << ex.what() << "." << std::endl;
1926             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1927                                     "handle_client_recv() : exception : error = %s. thread id : %d.");
1928             formatter % ex.what() % boost::this_thread::get_id();
1929             putLogError(600040, formatter.str(), __FILE__, __LINE__ );
1930             status = FINALIZE;
1931         }
1932         catch (...)
1933         {
1934             std::cerr << "protocol_module_ip::handle_client_recv() : Unknown exception." << std::endl;
1935             boost::format formatter("function : protocol_module_base::EVENT_TAG "
1936                                     "protocol_module_ip::handle_client_recv() : "
1937                                     "Unknown exception. thread id : %d.");
1938             formatter % boost::this_thread::get_id();
1939             putLogError(600041, formatter.str(), __FILE__, __LINE__ );
1940             status = FINALIZE;
1941         }
1942
1943         /*-------- DEBUG LOG --------*/
1944         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1945         {
1946             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1947                                     "handle_client_recv(const boost::thread::id thread_id, "
1948                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
1949                                     "const size_t recvlen) : return_value = %d. thread id : %d.");
1950             formatter % status % boost::this_thread::get_id();
1951             putLogDebug(600056, formatter.str(), __FILE__, __LINE__ );
1952         }
1953         /*------DEBUG LOG END------*/
1954
1955         return status;
1956     }
1957
1958     //! called from after realserver select.use in upstream thread.
1959     //! @param[in]    upstream thread id
1960     //! @param[out]    realserver TCP endpoint
1961     //! @return        session use EVENT mode.
1962     protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_select(
1963         const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint & rs_endpoint)
1964     {
1965
1966         /*-------- DEBUG LOG --------*/
1967         if (unlikely(LOG_LV_DEBUG == getloglevel()))
1968         {
1969             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1970                                     "handle_realserver_select(const boost::thread::id thread_id, "
1971                                     "boost::asio::ip::tcp::endpoint& rs_endpoint) : "
1972                                     "thread_id = %d, rs_endpoint = [%s]:%d.");
1973             formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
1974             putLogDebug(600057, formatter.str(), __FILE__, __LINE__ );
1975         }
1976         /*------DEBUG LOG END------*/
1977
1978         EVENT_TAG status = FINALIZE;
1979         boost::asio::ip::tcp::endpoint init_endpoint;
1980         thread_data_ptr session_data_ptr;
1981         session_thread_data_map_it session_thread_it;
1982         session_thread_data_map_it session_thread_it_end;
1983         realserverlist_type::iterator    rs_list_itr;
1984
1985         if (unlikely(schedule_tcp.empty()))
1986         {
1987             std::cerr << "protocol_module_ip::handle_realserver_select() : Schedule_tcp function is empty." << std::endl;
1988             boost::format formatter("Schedule_tcp function is empty. thread id : %d.");
1989             formatter % boost::this_thread::get_id();
1990             putLogError(600042, formatter.str(), __FILE__, __LINE__ );
1991
1992             /*-------- DEBUG LOG --------*/
1993             if (unlikely(LOG_LV_DEBUG == getloglevel()))
1994             {
1995                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
1996                                         "handle_realserver_select(const boost::thread::id thread_id, "
1997                                         "boost::asio::ip::tcp::endpoint& rs_endpoint)"
1998                                         " : return_value = %d. thread id : %d.");
1999                 formatter % FINALIZE % boost::this_thread::get_id();
2000                 putLogDebug(600058, formatter.str(), __FILE__, __LINE__ );
2001             }
2002             /*------DEBUG LOG END------*/
2003             return FINALIZE;
2004         }
2005
2006
2007         try
2008         {
2009             {
2010                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
2011
2012                 session_thread_it = session_thread_data_map.find(thread_id);
2013                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
2014                 {
2015                     boost::format formatter("Invalid thread id. thread id : %d.");
2016                     formatter % boost::this_thread::get_id();
2017                     putLogError(600043, formatter.str(), __FILE__, __LINE__ );
2018                     throw -1;
2019                 }
2020
2021                 session_data_ptr = session_thread_it->second;
2022             }
2023
2024             //initialize realserver endpoint
2025             rs_endpoint = init_endpoint;
2026
2027             boost::mutex::scoped_lock lock(session_data_mutex);
2028
2029             //get endpoint data from the ip data
2030             int ret = ip_data_processor->get_endpoint_from_session_data(session_data_ptr->ip_hash,
2031                       rs_endpoint);
2032
2033             /*-------- DEBUG LOG --------*/
2034             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2035             {
2036                 boost::format formatter("function : protocol_module_base::EVENT_TAG "
2037                                         "protocol_module_ip::handle_realserver_select() : "
2038                                         "get_endpoint_from_session_data() end. thread id : %d.");
2039                 formatter % boost::this_thread::get_id();
2040                 putLogDebug(600059, formatter.str(), __FILE__, __LINE__);
2041             }
2042             /*------DEBUG LOG END------*/
2043
2044             //endpoint is matched
2045             if (ret == 0)
2046             {
2047                 //find the rs_endpoint in rs_list
2048                 rs_list_lock();
2049
2050                 rs_list_itr = rs_list_begin();
2051                 int is_match = 0;
2052
2053                 while ( rs_list_itr != rs_list_end())
2054                 {
2055
2056                     if ( rs_list_itr->tcp_endpoint == rs_endpoint )
2057                     {
2058                         is_match = 1;
2059                         break;
2060                     }
2061
2062                     rs_list_itr = rs_list_next( rs_list_itr );
2063                 }
2064
2065                 rs_list_unlock();
2066
2067                 //endpoint is matched in the list
2068                 if (is_match)
2069                 {
2070                     //data state is HTTP_START
2071                     if (session_data_ptr->data_state == HTTP_START)
2072                     {
2073                         //set return status
2074                         status = CLIENT_RECV;
2075                     }
2076                     //data state is not HTTP_START
2077                     else
2078                     {
2079                         //set return status
2080                         status = REALSERVER_CONNECT;
2081                     }
2082                 }
2083                 //endpoint is not matched in the list
2084                 else
2085                 {
2086                     //if reschedule is on then try multi times connect
2087                     if (reschedule == 1)
2088                     {
2089                         // init rs_endpoint
2090                         rs_endpoint = init_endpoint;
2091
2092                         schedule_tcp(thread_id, rs_list_begin, rs_list_end, rs_list_next, rs_endpoint);
2093                         /*-------- DEBUG LOG --------*/
2094                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2095                         {
2096                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2097                                                     "handle_realserver_select() : call schedule_tcp : "
2098                                                     "rs_endpoint = [%s]:%d. thread id : %d.");
2099                             formatter % rs_endpoint.address().to_string() % rs_endpoint.port() % boost::this_thread::get_id();
2100                             putLogDebug(600060, formatter.str(), __FILE__, __LINE__ );
2101                         }
2102                         /*------DEBUG LOG END------*/
2103
2104                         //get the endpoint by schedule successfully
2105                         if (init_endpoint != rs_endpoint)
2106                         {
2107                             time_t init_time = 0;
2108
2109                             //write data to session table
2110                             ip_data_processor->write_session_data(session_data_ptr->ip_hash, rs_endpoint, init_time);
2111                             /*-------- DEBUG LOG --------*/
2112                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2113                             {
2114                                 boost::format formatter("function : protocol_module_base::EVENT_TAG "
2115                                                         "protocol_module_ip::handle_realserver_select() : "
2116                                                         "write_session_data() end. thread id : %d.");
2117                                 formatter % boost::this_thread::get_id();
2118                                 putLogDebug(600061, formatter.str(), __FILE__, __LINE__);
2119                             }
2120                             /*------DEBUG LOG END------*/
2121
2122                             //data state is HTTP_START
2123                             if (session_data_ptr->data_state == HTTP_START)
2124                             {
2125                                 //set return status
2126                                 status = CLIENT_RECV;
2127                             }
2128                             //data state is not HTTP_START
2129                             else
2130                             {
2131                                 //set return status
2132                                 status = REALSERVER_CONNECT;
2133                             }
2134                         }
2135                         //get the endpoint by schedule unsuccessfully
2136                         else
2137                         {
2138                             //set return status
2139                             status = CLIENT_DISCONNECT;
2140                         }
2141                     }
2142                     //reschedule is off
2143                     else
2144                     {
2145                         //set return status
2146                         status = CLIENT_DISCONNECT;
2147                     }
2148                 }
2149             }
2150             //endpoint is not matched
2151             else
2152             {
2153                 // init rs_endpoint
2154                 rs_endpoint = init_endpoint;
2155
2156                 //call schedule_module's schedule function, get realserver endpoint
2157                 schedule_tcp(thread_id, rs_list_begin, rs_list_end, rs_list_next, rs_endpoint);
2158
2159                 /*-------- DEBUG LOG --------*/
2160                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2161                 {
2162                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2163                                             "handle_realserver_select() : call schedule_tcp : "
2164                                             "rs_endpoint = [%s]:%d. thread id : %d.");
2165                     formatter % rs_endpoint.address().to_string() % rs_endpoint.port() % boost::this_thread::get_id();
2166                     putLogDebug(600062, formatter.str(), __FILE__, __LINE__ );
2167                 }
2168                 /*------DEBUG LOG END------*/
2169
2170                 //get the endpoint by schedule successfully
2171                 if (init_endpoint != rs_endpoint)
2172                 {
2173                     time_t init_time = 0;
2174
2175                     //write data to session table
2176                     ip_data_processor->write_session_data(session_data_ptr->ip_hash, rs_endpoint, init_time);
2177                     /*-------- DEBUG LOG --------*/
2178                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
2179                     {
2180                         boost::format formatter("function : protocol_module_base::EVENT_TAG "
2181                                                 "protocol_module_ip::handle_realserver_select() : "
2182                                                 "write_session_data() end. thread id : %d.");
2183                         formatter % boost::this_thread::get_id();
2184                         putLogDebug(600063, formatter.str(), __FILE__, __LINE__);
2185                     }
2186                     /*------DEBUG LOG END------*/
2187
2188                     //data state is HTTP_START
2189                     if (session_data_ptr->data_state == HTTP_START)
2190                     {
2191                         //set return status
2192                         status = CLIENT_RECV;
2193                     }
2194                     //data state is not HTTP_START
2195                     else
2196                     {
2197                         //set return status
2198                         status = REALSERVER_CONNECT;
2199                     }
2200                 }
2201                 //get the endpoint by schedule unsuccessfully
2202                 else
2203                 {
2204                     //set return status
2205                     status = CLIENT_DISCONNECT;
2206                 }
2207             }
2208
2209             //set last status
2210             session_data_ptr->last_status = status;
2211         }
2212         catch (int e)
2213         {
2214             /*-------- DEBUG LOG --------*/
2215             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2216             {
2217                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2218                                         "handle_realserver_select() : catch exception e = %d. thread id : %d.");
2219                 formatter % e % boost::this_thread::get_id();
2220                 putLogDebug(600066, formatter.str(), __FILE__, __LINE__ );
2221             }
2222             /*------DEBUG LOG END------*/
2223             status = FINALIZE;
2224         }
2225         catch (const std::exception& ex)
2226         {
2227             std::cerr << "protocol_module_ip::handle_realserver_select() : exception : error = " << ex.what() << "." << std::endl;
2228             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2229                                     "handle_realserver_select() : exception : error = %s. thread id : %d.");
2230             formatter % ex.what() % boost::this_thread::get_id();
2231             putLogError(600044, formatter.str(), __FILE__, __LINE__ );
2232             status = FINALIZE;
2233         }
2234         catch (...)
2235         {
2236             std::cerr << "protocol_module_ip::handle_realserver_select() : Unknown exception." << std::endl;
2237             boost::format formatter("function : protocol_module_base::EVENT_TAG "
2238                                     "protocol_module_ip::handle_realserver_select() : "
2239                                     "Unknown exception. thread id : %d.");
2240             formatter % boost::this_thread::get_id();
2241             putLogError(600045, formatter.str(), __FILE__, __LINE__ );
2242             status = FINALIZE;
2243         }
2244
2245         /*-------- DEBUG LOG --------*/
2246         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2247         {
2248             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2249                                     "handle_realserver_select(const boost::thread::id thread_id, "
2250                                     "boost::asio::ip::tcp::endpoint& rs_endpoint)"
2251                                     " : return_value = %d. thread id : %d.");
2252             formatter % status % boost::this_thread::get_id();
2253             putLogDebug(600067, formatter.str(), __FILE__, __LINE__ );
2254         }
2255         /*------DEBUG LOG END------*/
2256         return status;
2257     }
2258
2259     //! called from after realserver select
2260     //! @param[in]    upstream thread id
2261     //! @param[out]    realserver UDP endpoint
2262     //! @param[out]    sendbuffer reference
2263     //! @param[out]    send data length
2264     //! @return        session use EVENT mode.
2265     protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_select(
2266         const boost::thread::id thread_id, boost::asio::ip::udp::endpoint& rs_endpoint, boost::array<char,
2267         MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
2268     {
2269         /*-------- DEBUG LOG --------*/
2270         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2271         {
2272             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2273                                     "handle_realserver_select(const boost::thread::id thread_id, "
2274                                     "boost::asio::ip::udp::endpoint& rs_endpoint, boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
2275                                     "size_t& datalen) : "
2276                                     "return_value = %d. thread id : %d.");
2277             formatter % STOP % boost::this_thread::get_id();
2278             putLogDebug(600068, formatter.str(), __FILE__, __LINE__ );
2279         }
2280         /*------DEBUG LOG END------*/
2281         return STOP;
2282     }
2283     //! called from after realserver connect
2284     //! @param[in]    upstream thread id
2285     //! @param[out]    sendbuffer reference
2286     //! @param[out]    send data length
2287     //! @return        session use EVENT mode.
2288     protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_connect(
2289         const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
2290     {
2291         /*-------- DEBUG LOG --------*/
2292         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2293         {
2294             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2295                                     "handle_realserver_connect(const boost::thread::id thread_id, "
2296                                     "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : "
2297                                     "thread_id = %d.");
2298             formatter % thread_id;
2299             putLogDebug(600069, formatter.str(), __FILE__, __LINE__ );
2300         }
2301         /*------DEBUG LOG END------*/
2302
2303         EVENT_TAG status            = FINALIZE;
2304         size_t send_possible_size        = 0;
2305         size_t x_forwarded_for_insert_pos   = 0;
2306         thread_data_ptr                session_data_ptr;
2307         session_thread_data_map_it        session_thread_it;
2308         std::pair<char*, size_t>        buffer_element;
2309         std::string                x_forwarded_for_context;
2310
2311         try
2312         {
2313             {
2314                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
2315
2316                 //thread id check
2317                 session_thread_it = session_thread_data_map.find(thread_id);
2318                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
2319                 {
2320                     boost::format formatter("Invalid thread id. thread id : %d.");
2321                     formatter % boost::this_thread::get_id();
2322                     putLogError(600046, formatter.str(), __FILE__, __LINE__ );
2323                     throw -1;
2324                 }
2325
2326                 session_data_ptr = session_thread_it->second;
2327             }
2328
2329             send_possible_size = std::min(session_data_ptr->current_message_rest_size,
2330                                           session_data_ptr->data_length
2331                                          );
2332
2333             //buffer sequence is empty
2334             if (session_data_ptr->buffer_sequence.empty())
2335             {
2336                 //data state is HTTP_HEADER
2337                 if (session_data_ptr->data_state == HTTP_HEADER)
2338                 {
2339                     //forwarded_for flag is on
2340                     if (forwarded_for == FORWARDED_FOR_ON)
2341                     {
2342                         //search X-Forwarded-For header
2343                         create_x_forwarded_for(session_data_ptr->client_endpoint.address().to_string(),
2344                                                session_data_ptr->data_buffer + session_data_ptr->data_offset,
2345                                                session_data_ptr->data_length,
2346                                                x_forwarded_for_insert_pos,
2347                                                x_forwarded_for_context);
2348
2349                         //put buffer data to buffer_sequence
2350                         buffer_element.first = session_data_ptr->data_buffer + session_data_ptr->data_offset;
2351                         buffer_element.second = x_forwarded_for_insert_pos;
2352                         session_data_ptr->buffer_sequence.push_back(buffer_element);
2353
2354                         session_data_ptr->forwarded_for_buffer.assign(0);
2355
2356                         /*-------- DEBUG LOG --------*/
2357                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2358                         {
2359                             std::string datadump;
2360                             dump_memory(x_forwarded_for_context.c_str(),
2361                                         x_forwarded_for_context.size(), datadump);
2362                             boost::format formatter(
2363                                 "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2364                                 "handle_realserver_connect() : before memcpy (data dump) : "
2365                                 "data begin = 0, data_size = %d, data = %s");
2366                             formatter %  x_forwarded_for_context.size() % datadump;
2367                             putLogDebug(600070, formatter.str(), __FILE__, __LINE__ );
2368                         }
2369                         /*------DEBUG LOG END------*/
2370
2371                         memcpy(session_data_ptr->forwarded_for_buffer.data(),
2372                                x_forwarded_for_context.c_str(),
2373                                x_forwarded_for_context.size());
2374
2375                         /*-------- DEBUG LOG --------*/
2376                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2377                         {
2378                             std::string datadump;
2379                             dump_memory(x_forwarded_for_context.c_str(),
2380                                         x_forwarded_for_context.size(), datadump);
2381                             boost::format formatter(
2382                                 "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2383                                 "handle_realserver_connect() : after memcpy (data dump) : "
2384                                 "data begin = 0, data_size = %d, data = %s");
2385                             formatter % x_forwarded_for_context.size() % datadump;
2386                             putLogDebug(600071, formatter.str(), __FILE__, __LINE__ );
2387                         }
2388                         /*------DEBUG LOG END------*/
2389
2390                         //set buffer's position
2391                         buffer_element.first = session_data_ptr->forwarded_for_buffer.data();
2392                         buffer_element.second = x_forwarded_for_context.size();
2393
2394                         //set data to buffer_sequence
2395                         session_data_ptr->buffer_sequence.push_back(buffer_element);
2396
2397                         //set buffer's position
2398                         buffer_element.first = session_data_ptr->data_buffer + session_data_ptr->data_offset + x_forwarded_for_insert_pos;
2399                         buffer_element.second = send_possible_size - x_forwarded_for_insert_pos;
2400
2401                         //set data to buffer_sequence
2402                         session_data_ptr->buffer_sequence.push_back(buffer_element);
2403                     }
2404                     //forwarded_for flag is off
2405                     else
2406                     {
2407                         //set buffer's position
2408                         buffer_element.first = session_data_ptr->data_buffer + session_data_ptr->data_offset;
2409                         buffer_element.second = send_possible_size;
2410
2411                         //set data to buffer_sequence
2412                         session_data_ptr->buffer_sequence.push_back(buffer_element);
2413                     }
2414                     session_data_ptr->data_state = HTTP_BODY;
2415                 }
2416                 //data state is not HTTP_HEADER
2417                 else
2418                 {
2419
2420                     buffer_element.first = session_data_ptr->data_buffer + session_data_ptr->data_offset;
2421                     buffer_element.second = send_possible_size;
2422
2423                     //set data to buffer_sequence
2424                     session_data_ptr->buffer_sequence.push_back(buffer_element);
2425
2426                 }
2427             }
2428
2429             //put buffer_sequence data into sendbuffer
2430             put_data_into_sendbuffer(session_data_ptr, sendbuffer, datalen);
2431
2432             //set return status
2433             status = REALSERVER_SEND;
2434
2435             //set last status
2436             session_data_ptr->last_status = status;
2437         }
2438         catch (int e)
2439         {
2440             /*-------- DEBUG LOG --------*/
2441             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2442             {
2443                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2444                                         "handle_realserver_connect() : catch exception e = %d. thread id : %d.");
2445                 formatter % e % boost::this_thread::get_id();
2446                 putLogDebug(600072, formatter.str(), __FILE__, __LINE__ );
2447             }
2448             /*------DEBUG LOG END------*/
2449
2450             //set return status
2451             status = FINALIZE;
2452         }
2453         catch (std::exception& ex)
2454         {
2455             std::cerr << "protocol_module_ip::handle_realserver_connect() : exception : error = " << ex.what() << "." << std::endl;
2456             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2457                                     "handle_realserver_connect() : exception : error = %s. thread id : %d.");
2458             formatter % ex.what() % boost::this_thread::get_id();
2459             putLogError(600047, formatter.str(), __FILE__, __LINE__ );
2460
2461             //set return status
2462             status = FINALIZE;
2463         }
2464         catch (...)
2465         {
2466             std::cerr << "protocol_module_ip::handle_realserver_connect() : Unknown exception." << std::endl;
2467             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2468                                     "handle_realserver_connect() : Unknown exception. thread id : %d.");
2469             formatter % boost::this_thread::get_id();
2470             putLogError(600048, formatter.str(), __FILE__, __LINE__ );
2471
2472             //set return status
2473             status = FINALIZE;
2474         }
2475
2476         /*-------- DEBUG LOG --------*/
2477         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2478         {
2479             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2480                                     "handle_realserver_connect(const boost::thread::id thread_id, "
2481                                     "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
2482                                     "size_t& datalen) : return_value = %d. thread id : %d.");
2483             formatter % status % boost::this_thread::get_id();
2484             putLogDebug(600073, formatter.str(), __FILE__, __LINE__ );
2485         }
2486         /*------DEBUG LOG END------*/
2487
2488         return status;
2489     }
2490
2491     //! called from after realserver connection fail
2492     //! @param[in]    upstream thread id
2493     //! @param[in]    fail realserver endpoint reference
2494     //! @return        session use EVENT mode.
2495     protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_connection_fail(
2496         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & rs_endpoint)
2497     {
2498         /*-------- DEBUG LOG --------*/
2499         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2500         {
2501             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2502                                     "handle_realserver_connection_fail(const boost::thread::id thread_id, "
2503                                     "const boost::asio::ip::tcp::endpoint& rs_endpoint) : "
2504                                     "thread_id = %d, rs_endpoint = [%s]:%d.");
2505             formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
2506             putLogDebug(600074, formatter.str(), __FILE__, __LINE__ );
2507         }
2508         /*------DEBUG LOG END------*/
2509
2510         EVENT_TAG status = FINALIZE;
2511         thread_data_ptr session_data_ptr;
2512         session_thread_data_map_it session_thread_it;
2513
2514         try
2515         {
2516             boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
2517
2518             session_thread_it = session_thread_data_map.find(thread_id);
2519             if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
2520             {
2521                 boost::format formatter("Invalid thread id. thread id : %d.");
2522                 formatter % boost::this_thread::get_id();
2523                 putLogError(600049, formatter.str(), __FILE__, __LINE__ );
2524                 throw -1;
2525             }
2526
2527             session_data_ptr = session_thread_it->second;
2528
2529             //set end flag on
2530             session_data_ptr->end_flag = END_FLAG_ON;
2531
2532             /*-------- DEBUG LOG --------*/
2533             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2534             {
2535                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2536                                         "handle_realserver_connection_fail(const boost::thread::id thread_id, "
2537                                         "const boost::asio::ip::tcp::endpoint& rs_endpoint) : END_FLAG_ON. "
2538                                         "thread_id = %d, rs_endpoint = [%s]:%d.");
2539                 formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
2540                 putLogDebug(600075, formatter.str(), __FILE__, __LINE__ );
2541             }
2542             /*------DEBUG LOG END------*/
2543
2544             //set return status
2545             status = CLIENT_DISCONNECT;
2546
2547             //set last status
2548             session_data_ptr->last_status = status;
2549         }
2550         catch (int e)
2551         {
2552             /*-------- DEBUG LOG --------*/
2553             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2554             {
2555                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2556                                         "handle_realserver_connection_fail() : catch exception e = %d. thread id : %d.");
2557                 formatter % e % boost::this_thread::get_id();
2558                 putLogDebug(600076, formatter.str(), __FILE__, __LINE__ );
2559             }
2560             /*------DEBUG LOG END------*/
2561
2562             //set return status
2563             status = FINALIZE;
2564         }
2565         catch (const std::exception& ex)
2566         {
2567             std::cerr << "protocol_module_ip::handle_realserver_connection_fail() : exception : error = " << ex.what() << "." << std::endl;
2568             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2569                                     "handle_realserver_connection_fail() : exception : error = %s. thread id : %d.");
2570             formatter % ex.what() % boost::this_thread::get_id();
2571             putLogError(600050, formatter.str(), __FILE__, __LINE__ );
2572
2573             //set return status
2574             status = FINALIZE;
2575         }
2576         catch (...)
2577         {
2578             std::cerr << "protocol_module_ip::handle_realserver_connection_fail() : Unknown exception." << std::endl;
2579             boost::format formatter("function : protocol_module_base::EVENT_TAG "
2580                                     "protocol_module_ip::handle_realserver_connection_fail() : "
2581                                     "Unknown exception. thread id : %d.");
2582             formatter % boost::this_thread::get_id();
2583             putLogError(600051, formatter.str(), __FILE__, __LINE__ );
2584
2585             //set return status
2586             status = FINALIZE;
2587         }
2588
2589         /*-------- DEBUG LOG --------*/
2590         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2591         {
2592             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2593                                     "handle_realserver_connection_fail(const boost::thread::id thread_id, "
2594                                     "const boost::asio::ip::tcp::endpoint& rs_endpoint) : return_value = %d. thread id : %d.");
2595             formatter % status % boost::this_thread::get_id();
2596             putLogDebug(600077, formatter.str(), __FILE__, __LINE__ );
2597         }
2598         /*------DEBUG LOG END------*/
2599
2600         return status;
2601     }
2602     //! called from after realserver send.
2603     //! @param[in]    upstream thread id
2604     //! @return        session use EVENT mode.
2605     protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_send(
2606         const boost::thread::id thread_id)
2607     {
2608         /*-------- DEBUG LOG --------*/
2609         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2610         {
2611             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2612                                     "handle_realserver_send(const boost::thread::id thread_id) : thread_id = %d.");
2613             formatter % thread_id;
2614             putLogDebug(600078, formatter.str(), __FILE__, __LINE__ );
2615         }
2616         /*------DEBUG LOG END------*/
2617
2618         EVENT_TAG status                = FINALIZE;
2619         size_t http_header_all_offset            = 0;
2620         size_t http_header_all_len            = 0;
2621         size_t http_header_content_length_offset    = 0;
2622         size_t http_header_content_length_len        = 0;
2623         const size_t CR_LF_LEN                = 2;
2624         const size_t CR_LF_CR_LF_LEN            = 4;
2625         int content_length_value            = 0;
2626
2627         std::string content_length;
2628         cmatch regex_ret;
2629         cregex content_length_regex = icase("Content-Length") >> ":" >> *~_d >> (s1 = +_d) >> *~_d;
2630
2631
2632         bool find_ret = false;
2633         http_utility::CHECK_RESULT_TAG check_ret;
2634
2635         thread_data_ptr session_data_ptr;
2636         session_thread_data_map_it session_thread_it;
2637
2638         try
2639         {
2640             {
2641                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
2642
2643                 //thread id check
2644                 session_thread_it = session_thread_data_map.find(thread_id);
2645                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
2646                 {
2647                     boost::format formatter("Invalid thread id. thread id : %d.");
2648                     formatter % boost::this_thread::get_id();
2649                     putLogError(600052, formatter.str(), __FILE__, __LINE__ );
2650                     throw -1;
2651                 }
2652
2653                 session_data_ptr = session_thread_it->second;
2654             }
2655
2656             //current_message_rest_size > 0
2657             if (session_data_ptr->current_message_rest_size > 0)
2658             {
2659                 //data size > 0
2660                 if (session_data_ptr->data_length > 0)
2661                 {
2662                     //set return status
2663                     status = REALSERVER_CONNECT;
2664                 }
2665                 //data size is 0
2666                 else
2667                 {
2668                     //data offset is 0
2669                     session_data_ptr->data_offset = 0;
2670
2671                     //set return status
2672                     status = CLIENT_RECV;
2673                 }
2674             }
2675             //current_message_rest_size is 0
2676             else
2677             {
2678                 //data size > 0
2679                 if (session_data_ptr->data_length > 0)
2680                 {
2681                     //data state is HTTP_BODY
2682                     if (session_data_ptr->data_state == HTTP_BODY)
2683                     {
2684                         //search whole http header, get whole http header's offset and length
2685                         find_ret = http_utility::find_http_header_all(session_data_ptr->data_buffer + session_data_ptr->data_offset,
2686                                                         session_data_ptr->data_length,
2687                                                         http_header_all_offset,
2688                                                         http_header_all_len
2689                                                        );
2690
2691                         /*-------- DEBUG LOG --------*/
2692                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2693                         {
2694                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2695                                                     "handle_realserver_send() : call find_http_header_all : "
2696                                                     "return_value = %d. thread id : %d.");
2697                             formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
2698                             putLogDebug(600079, formatter.str(), __FILE__, __LINE__ );
2699                         }
2700                         /*------DEBUG LOG END------*/
2701
2702                         //search http header result is NG
2703                         if (!find_ret)
2704                         {
2705                             //set data state HTTP_START
2706                             session_data_ptr->data_state = HTTP_START;
2707
2708                             //set return status
2709                             status = CLIENT_RECV;
2710                         }
2711                         //search http header result is OK
2712                         else
2713                         {
2714                             //check http method and version
2715                             check_ret = http_utility::check_http_method_and_version(session_data_ptr->data_buffer + session_data_ptr->data_offset,
2716                                         session_data_ptr->data_length);
2717
2718                             /*-------- DEBUG LOG --------*/
2719                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2720                             {
2721                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2722                                                         "handle_realserver_send() : call check_http_method_and_version : "
2723                                                         "return_value = %d. thread id : %d.");
2724                                 formatter % check_ret % boost::this_thread::get_id();
2725                                 putLogDebug(600080, formatter.str(), __FILE__, __LINE__ );
2726                             }
2727                             /*------DEBUG LOG END------*/
2728
2729                             //check method and version result is NG
2730                             if (check_ret == http_utility::CHECK_NG)
2731                             {
2732                                 //set current message rest size
2733                                 session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
2734
2735                                 //set data state UNKNOWN
2736                                 session_data_ptr->data_state = UNKNOWN;
2737                             }
2738                             //check method and version result is OK
2739                             else
2740                             {
2741                                 //search Content_Length header
2742                                 find_ret = http_utility::find_http_header_content_length(session_data_ptr->data_buffer+session_data_ptr->data_offset,
2743                                            session_data_ptr->data_length,
2744                                            http_header_content_length_offset,
2745                                            http_header_content_length_len);
2746
2747                                 /*-------- DEBUG LOG --------*/
2748                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
2749                                 {
2750                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2751                                                             "handle_realserver_send() : call find_http_header_content_length : "
2752                                                             "return_value = %d. thread id : %d.");
2753                                     formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
2754                                     putLogDebug(600081, formatter.str(), __FILE__, __LINE__ );
2755                                 }
2756                                 /*------DEBUG LOG END------*/
2757
2758                                 //search Content_Length result is OK
2759                                 if (find_ret)
2760                                 {
2761                                     content_length.assign(session_data_ptr->data_buffer+session_data_ptr->data_offset + http_header_content_length_offset,
2762                                                           http_header_content_length_len );
2763                                     find_ret = regex_search(content_length.c_str(), regex_ret, content_length_regex);
2764
2765                                     //"content-length: ddd\r\n"
2766                                     if ( find_ret)
2767                                     {
2768                                         content_length = content_length.substr(
2769                                                              regex_ret.position(1),
2770                                                              regex_ret.length(1));
2771
2772                                         //set content length value
2773                                         content_length_value = boost::lexical_cast<int>(content_length);
2774                                     }
2775
2776                                     //http_header context is "\r\n\r\n" only
2777                                     if (http_header_all_len == 0)
2778                                     {
2779                                         //set current message rest size
2780                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + content_length_value + CR_LF_LEN;
2781                                     }
2782                                     else
2783                                     {
2784                                         //set current message rest size
2785                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + content_length_value + CR_LF_CR_LF_LEN;
2786                                     }
2787                                 }
2788                                 //search Content_Length result is OK
2789                                 else
2790                                 {
2791                                     //http_header context is "\r\n\r\n" only
2792                                     if (http_header_all_len == 0)
2793                                     {
2794                                         //set current message rest size
2795                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + CR_LF_LEN;
2796                                     }
2797                                     else
2798                                     {
2799                                         //set current message rest size
2800                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + CR_LF_CR_LF_LEN;
2801                                     }
2802
2803                                 }
2804
2805                                 //set data state HTTP_HEADER
2806                                 session_data_ptr->data_state = HTTP_HEADER;
2807                             }
2808
2809                             //set return status
2810                             status = REALSERVER_CONNECT;
2811                         }
2812                     }
2813                     //data state is UNKNOWN
2814                     else if (session_data_ptr->data_state == UNKNOWN)
2815                     {
2816                         //set return status
2817                         status = REALSERVER_CONNECT;
2818                     }
2819
2820                 }
2821                 //data size is 0
2822                 else
2823                 {
2824                     //data state is HTTP_BODY
2825                     if (session_data_ptr->data_state == HTTP_BODY)
2826                     {
2827                         //set data state HTTP_START
2828                         session_data_ptr->data_state = HTTP_START;
2829                     }
2830
2831                     //set data offset 0
2832                     session_data_ptr->data_offset = 0;
2833
2834                     //set return status
2835                     status = CLIENT_RECV;
2836                 }
2837             }
2838             //set last status
2839             session_data_ptr->last_status = status;
2840         }
2841         catch (int e)
2842         {
2843             /*-------- DEBUG LOG --------*/
2844             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2845             {
2846                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2847                                         "handle_realserver_send() : catch exception e = %d. thread id : %d.");
2848                 formatter % e % boost::this_thread::get_id();
2849                 putLogDebug(600082, formatter.str(), __FILE__, __LINE__ );
2850             }
2851             /*------DEBUG LOG END------*/
2852
2853             //set return status
2854             status = FINALIZE;
2855         }
2856         catch (const boost::bad_lexical_cast& )
2857         {
2858             std::cerr << "protocol_module_ip::handle_realserver_send() : exception : " <<  "Content_Length field's value is invalid." << std::endl;
2859             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_send() : "
2860                                     "Content_Length field's value is invalid. thread id : %d.");
2861             formatter % boost::this_thread::get_id();
2862             putLogError(600053, formatter.str(), __FILE__, __LINE__ );
2863
2864             //set return status
2865             status = FINALIZE;
2866         }
2867         catch (const std::exception& ex)
2868         {
2869             std::cerr << "protocol_module_ip::handle_realserver_send() : exception : error = " << ex.what() << "." << std::endl;                //set data state HTTP_HEADER
2870             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2871                                     "handle_realserver_send() : exception : error = %s. thread id : %d.");
2872             formatter % ex.what() % boost::this_thread::get_id();
2873             putLogError(600054, formatter.str(), __FILE__, __LINE__ );
2874
2875             //set return status
2876             status = FINALIZE;
2877         }
2878         catch (...)
2879         {
2880             std::cerr << "protocol_module_ip::handle_realserver_send() : Unknown exception." << std::endl;
2881             boost::format formatter("function : protocol_module_base::EVENT_TAG "
2882                                     "protocol_module_ip::handle_realserver_send() : "
2883                                     "Unknown exception. thread id : %d.");
2884             formatter % boost::this_thread::get_id();
2885             putLogError(600055, formatter.str(), __FILE__, __LINE__ );
2886
2887             //set return status
2888             status = FINALIZE;
2889         }
2890
2891         /*-------- DEBUG LOG --------*/
2892         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2893         {
2894             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2895                                     "handle_realserver_send(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
2896             formatter % status % boost::this_thread::get_id();
2897             putLogDebug(600083, formatter.str(), __FILE__, __LINE__ );
2898         }
2899         /*------DEBUG LOG END------*/
2900
2901         return status;
2902     }
2903
2904     //! called from after sorryserver select
2905     //! @param[in]    upstream thread id
2906     //! @param[in]    sorryserver endpiont reference
2907     //! @return        session use EVENT mode.
2908     protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorryserver_select(
2909         const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint& sorry_endpoint)
2910     {
2911         /*-------- DEBUG LOG --------*/
2912         if (unlikely(LOG_LV_DEBUG == getloglevel()))
2913         {
2914             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2915                                     "handle_sorryserver_select(const boost::thread::id thread_id, "
2916                                     "boost::asio::ip::tcp::endpoint& sorry_endpoint) : "
2917                                     "thread_id = %d, sorry_endpoint = [%s]:%d.");
2918             formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port();
2919             putLogDebug(600084, formatter.str(), __FILE__, __LINE__ );
2920         }
2921         /*------DEBUG LOG END------*/
2922
2923         EVENT_TAG status = FINALIZE;
2924         boost::asio::ip::tcp::endpoint tmp_endpoint;
2925         thread_data_ptr session_data_ptr;
2926         session_thread_data_map_it session_thread_it;
2927         session_thread_data_map_it session_thread_it_end;
2928
2929         try
2930         {
2931             {
2932                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
2933
2934                 session_thread_it = session_thread_data_map.find(thread_id);
2935                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
2936                 {
2937                     boost::format formatter("Invalid thread id. thread id : %d.");
2938                     formatter % boost::this_thread::get_id();
2939                     putLogError(600056, formatter.str(), __FILE__, __LINE__ );
2940                     throw -1;
2941                 }
2942
2943                 session_data_ptr = session_thread_it->second;
2944             }
2945
2946             //data state is HTTP_START
2947             if (session_data_ptr->data_state == HTTP_START)
2948             {
2949                 //set return status
2950                 status = CLIENT_RECV;
2951             }
2952             //data state is not HTTP_START
2953             else
2954             {
2955                 //set return status
2956                 status = SORRYSERVER_CONNECT;
2957             }
2958
2959             //set last status
2960             session_data_ptr->last_status = status;
2961         }
2962         catch (int e)
2963         {
2964             /*-------- DEBUG LOG --------*/
2965             if (unlikely(LOG_LV_DEBUG == getloglevel()))
2966             {
2967                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2968                                         "handle_sorryserver_select() : catch exception e = %d. thread id : %d.");
2969                 formatter % e % boost::this_thread::get_id();
2970                 putLogDebug(600085, formatter.str(), __FILE__, __LINE__ );
2971             }
2972             /*------DEBUG LOG END------*/
2973
2974             //set return status
2975             status = FINALIZE;
2976         }
2977         catch (const std::exception& ex)
2978         {
2979             std::cerr << "protocol_module_ip::handle_sorryserver_select() : exception : error = " << ex.what() << "." << std::endl;
2980             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
2981                                     "handle_sorryserver_select() : exception : error = %s. thread id : %d.");
2982             formatter % ex.what() % boost::this_thread::get_id();
2983             putLogError(600057, formatter.str(), __FILE__, __LINE__ );
2984
2985             //set return status
2986             status = FINALIZE;
2987         }
2988         catch (...)
2989         {
2990             std::cerr << "protocol_module_ip::handle_sorryserver_select() : Unknown exception." << std::endl;
2991             boost::format formatter("function : protocol_module_base::EVENT_TAG "
2992                                     "protocol_module_ip::handle_sorryserver_select() : "
2993                                     "Unknown exception. thread id : %d.");
2994             formatter % boost::this_thread::get_id();
2995             putLogError(600058, formatter.str(), __FILE__, __LINE__ );
2996
2997             //set return status
2998             status = FINALIZE;
2999         }
3000
3001         /*-------- DEBUG LOG --------*/
3002         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3003         {
3004             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3005                                     "handle_sorryserver_select(const boost::thread::id thread_id, "
3006                                     "boost::asio::ip::tcp::endpoint& sorry_endpoint)"
3007                                     " : return_value = %d. thread id : %d.");
3008             formatter % status % boost::this_thread::get_id();
3009             putLogDebug(600086, formatter.str(), __FILE__, __LINE__ );
3010         }
3011         /*------DEBUG LOG END------*/
3012
3013         return status;
3014     }
3015
3016     //! called from after sorryserver connect
3017     //!    @param[in]    upstream thread id
3018     //! @param[out]    send buffer reference.
3019     //! @param[out]    send length
3020     //! @return        session use EVENT mode.
3021     protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorryserver_connect(
3022         const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
3023     {
3024         /*-------- DEBUG LOG --------*/
3025         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3026         {
3027             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3028                                     "handle_sorryserver_connect(const boost::thread::id thread_id, "
3029                                     "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : "
3030                                     "thread_id = %d.");
3031             formatter % thread_id;
3032             putLogDebug(600087, formatter.str(), __FILE__, __LINE__ );
3033         }
3034         /*------DEBUG LOG END------*/
3035
3036         EVENT_TAG status            = FINALIZE;
3037         size_t send_possible_size        = 0;
3038         size_t uri_offset            = 0;
3039         size_t uri_len                = 0;
3040         size_t x_forwarded_for_insert_pos   = 0;
3041         thread_data_ptr                session_data_ptr;
3042         session_thread_data_map_it        session_thread_it;
3043         std::pair<char*, size_t>        buffer_element;
3044         std::string                x_forwarded_for_context;
3045
3046         try
3047         {
3048             {
3049                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
3050
3051                 //thread id check
3052                 session_thread_it = session_thread_data_map.find(thread_id);
3053                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
3054                 {
3055                     boost::format formatter("Invalid thread id. thread id : %d.");
3056                     formatter % boost::this_thread::get_id();
3057                     putLogError(600059, formatter.str(), __FILE__, __LINE__ );
3058                     throw -1;
3059                 }
3060
3061                 session_data_ptr = session_thread_it->second;
3062             }
3063             //set send possible data size
3064             send_possible_size = std::min(session_data_ptr->current_message_rest_size,
3065                                           session_data_ptr->data_length
3066                                          );
3067
3068             if (session_data_ptr->buffer_sequence.empty())
3069             {
3070                 //data state is HTTP_HEADER
3071                 if (session_data_ptr->data_state == HTTP_HEADER)
3072                 {
3073                     //search uri
3074                     http_utility::find_uri(session_data_ptr->data_buffer + session_data_ptr->data_offset,
3075                              session_data_ptr->data_length, uri_offset, uri_len);
3076
3077                     //set buffer's position
3078                     buffer_element.first = session_data_ptr->data_buffer + session_data_ptr->data_offset;
3079                     buffer_element.second = uri_offset;
3080
3081                     //put buffer data to buffer_sequence
3082                     session_data_ptr->buffer_sequence.push_back(buffer_element);
3083
3084                     //set buffer's position
3085                     buffer_element.first = sorry_uri.data();
3086                     buffer_element.second = strlen(sorry_uri.data());
3087
3088                     //put buffer data to buffer_sequence
3089                     session_data_ptr->buffer_sequence.push_back(buffer_element);
3090
3091                     //forwarded_for flag is on
3092                     if (forwarded_for == FORWARDED_FOR_ON)
3093                     {
3094                         create_x_forwarded_for(session_data_ptr->client_endpoint.address().to_string(),
3095                                                session_data_ptr->data_buffer + session_data_ptr->data_offset,
3096                                                session_data_ptr->data_length,
3097                                                x_forwarded_for_insert_pos,
3098                                                x_forwarded_for_context);
3099
3100                         //set buffer's position
3101                         buffer_element.first = session_data_ptr->data_buffer
3102                                                + session_data_ptr->data_offset
3103                                                + uri_offset
3104                                                + uri_len;
3105                         buffer_element.second = x_forwarded_for_insert_pos - uri_offset - uri_len;
3106
3107                         //put buffer data to buffer_sequence
3108                         session_data_ptr->buffer_sequence.push_back(buffer_element);
3109
3110                         session_data_ptr->forwarded_for_buffer.assign(0);
3111
3112                         /*-------- DEBUG LOG --------*/
3113                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3114                         {
3115                             std::string datadump;
3116                             dump_memory(x_forwarded_for_context.c_str(),
3117                                         x_forwarded_for_context.size(), datadump);
3118                             boost::format formatter(
3119                                 "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3120                                 "handle_sorryserver_connect() : before memcpy (data dump) : "
3121                                 "data begin = 0, data_size = %d, data = %s");
3122                             formatter %  x_forwarded_for_context.size() % datadump;
3123                             putLogDebug(600088, formatter.str(), __FILE__, __LINE__ );
3124                         }
3125                         /*------DEBUG LOG END------*/
3126
3127                         memcpy(session_data_ptr->forwarded_for_buffer.data(),
3128                                x_forwarded_for_context.c_str(),
3129                                x_forwarded_for_context.size());
3130
3131                         /*-------- DEBUG LOG --------*/
3132                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3133                         {
3134                             std::string datadump;
3135                             dump_memory(x_forwarded_for_context.c_str(),
3136                                         x_forwarded_for_context.size(), datadump);
3137                             boost::format formatter(
3138                                 "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3139                                 "handle_sorryserver_connect() : after memcpy (data dump) : "
3140                                 "data begin = 0, data_size = %d, data = %s");
3141                             formatter % x_forwarded_for_context.size() % datadump;
3142                             putLogDebug(600089, formatter.str(), __FILE__, __LINE__ );
3143                         }
3144                         /*------DEBUG LOG END------*/
3145
3146                         //set buffer's position
3147                         buffer_element.first = session_data_ptr->forwarded_for_buffer.data();
3148                         buffer_element.second = x_forwarded_for_context.size();
3149                         //put buffer data to buffer_sequence
3150                         session_data_ptr->buffer_sequence.push_back(buffer_element);
3151
3152                         //set buffer's position
3153                         buffer_element.first = session_data_ptr->data_buffer + session_data_ptr->data_offset + x_forwarded_for_insert_pos;
3154                         buffer_element.second = send_possible_size - x_forwarded_for_insert_pos;
3155                         //put buffer data to buffer_sequence
3156                         session_data_ptr->buffer_sequence.push_back(buffer_element);
3157                     }
3158                     //forwarded_for flag is on
3159                     else
3160                     {
3161                         //set buffer's position
3162                         buffer_element.first = session_data_ptr->data_buffer
3163                                                + session_data_ptr->data_offset
3164                                                + uri_offset
3165                                                + uri_len;
3166                         buffer_element.second = send_possible_size - uri_offset - uri_len;
3167                         //put buffer data to buffer_sequence
3168                         session_data_ptr->buffer_sequence.push_back(buffer_element);
3169                     }
3170
3171                     //set data state HTTP_BODY
3172                     session_data_ptr->data_state = HTTP_BODY;
3173                 }
3174                 //data state is not HTTP_HEADER
3175                 else
3176                 {
3177                     //set buffer's position
3178                     buffer_element.first = session_data_ptr->data_buffer + session_data_ptr->data_offset;
3179                     buffer_element.second = send_possible_size;
3180                     //put buffer data to buffer_sequence
3181                     session_data_ptr->buffer_sequence.push_back(buffer_element);
3182                 }
3183             }
3184
3185             //put buffer_sequence data into sendbuffer
3186             put_data_into_sendbuffer(session_data_ptr, sendbuffer, datalen);
3187
3188             //set return status
3189             status = SORRYSERVER_SEND;
3190
3191             //set last status
3192             session_data_ptr->last_status = status;
3193
3194         }
3195         catch (int e)
3196         {
3197             /*-------- DEBUG LOG --------*/
3198             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3199             {
3200                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3201                                         "handle_sorryserver_connect() : catch exception e = %d. thread id : %d.");
3202                 formatter % e % boost::this_thread::get_id();
3203                 putLogDebug(600090, formatter.str(), __FILE__, __LINE__ );
3204             }
3205             /*------DEBUG LOG END------*/
3206
3207             //set return status
3208             status = FINALIZE;
3209         }
3210         catch (const std::exception& ex)
3211         {
3212             std::cerr << "protocol_module_ip::handle_sorryserver_connect() : exception : error = " << ex.what() << "." << std::endl;
3213             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3214                                     "handle_sorryserver_connect() : exception : error = %s. thread id : %d.");
3215             formatter % ex.what() % boost::this_thread::get_id();
3216             putLogError(600060, formatter.str(), __FILE__, __LINE__ );
3217
3218             //set return status
3219             status = FINALIZE;
3220         }
3221         catch (...)
3222         {
3223             std::cerr << "protocol_module_ip::handle_sorryserver_connect() : Unknown exception." << std::endl;
3224             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3225                                     "handle_sorryserver_connect() : Unknown exception. thread id : %d.");
3226             formatter % boost::this_thread::get_id();
3227             putLogError(600061, formatter.str(), __FILE__, __LINE__ );
3228
3229             //set return status
3230             status = FINALIZE;
3231         }
3232
3233         /*-------- DEBUG LOG --------*/
3234         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3235         {
3236             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3237                                     "handle_sorryserver_connect(const boost::thread::id thread_id, "
3238                                     "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
3239                                     "size_t& datalen) : return_value = %d. thread id : %d.");
3240             formatter % status % boost::this_thread::get_id();
3241             putLogDebug(600091, formatter.str(), __FILE__, __LINE__ );
3242         }
3243         /*------DEBUG LOG END------*/
3244
3245         return status;
3246     }
3247     //! called from after sorryserver connection fail
3248     //! @param[in]    upstream thread id
3249     //! @param[in]    sorryserver endpoint reference.
3250     //! @return        session use EVENT mode.
3251     protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorryserver_connection_fail(
3252         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & sorry_endpoint)
3253     {
3254         /*-------- DEBUG LOG --------*/
3255         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3256         {
3257             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3258                                     "handle_sorryserver_connection_fail(const boost::thread::id thread_id, "
3259                                     "const boost::asio::ip::tcp::endpoint& sorry_endpoint) : "
3260                                     "thread_id = %d, sorry_endpoint = [%s]:%d.");
3261             formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port();
3262             putLogDebug(600092, formatter.str(), __FILE__, __LINE__ );
3263         }
3264         /*------DEBUG LOG END------*/
3265
3266         EVENT_TAG status = FINALIZE;
3267         thread_data_ptr session_data_ptr;
3268         session_thread_data_map_it session_thread_it;
3269
3270         try
3271         {
3272             boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
3273
3274             session_thread_it = session_thread_data_map.find(thread_id);
3275             if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
3276             {
3277                 boost::format formatter("Invalid thread id. thread id : %d.");
3278                 formatter % boost::this_thread::get_id();
3279                 putLogError(600062, formatter.str(), __FILE__, __LINE__ );
3280                 throw -1;
3281             }
3282
3283             session_data_ptr = session_thread_it->second;
3284
3285             //set return status
3286             status = CLIENT_DISCONNECT;
3287
3288             //set last status
3289             session_data_ptr->last_status = status;
3290         }
3291         catch (int e)
3292         {
3293             /*-------- DEBUG LOG --------*/
3294             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3295             {
3296                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3297                                         "handle_sorryserver_connection_fail() : catch exception e = %d. thread id : %d.");
3298                 formatter % e % boost::this_thread::get_id();
3299                 putLogDebug(600093, formatter.str(), __FILE__, __LINE__ );
3300             }
3301             /*------DEBUG LOG END------*/
3302
3303             //set return status
3304             status = FINALIZE;
3305         }
3306         catch (const std::exception& ex)
3307         {
3308             std::cerr << "protocol_module_ip::handle_sorryserver_connection_fail() : exception : error = " << ex.what() << "." << std::endl;
3309             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3310                                     "handle_sorryserver_connection_fail() : exception : error = %s. thread id : %d.");
3311             formatter % ex.what() % boost::this_thread::get_id();
3312             putLogError(600063, formatter.str(), __FILE__, __LINE__ );
3313
3314             //set return status
3315             status = FINALIZE;
3316         }
3317         catch (...)
3318         {
3319             std::cerr << "protocol_module_ip::handle_sorryserver_connection_fail() : Unknown exception." << std::endl;
3320             boost::format formatter("function : protocol_module_base::EVENT_TAG "
3321                                     "protocol_module_ip::handle_sorryserver_connection_fail() : "
3322                                     "Unknown exception. thread id : %d.");
3323             formatter % boost::this_thread::get_id();
3324             putLogError(600064, formatter.str(), __FILE__, __LINE__ );
3325
3326             //set return status
3327             status = FINALIZE;
3328         }
3329
3330         /*-------- DEBUG LOG --------*/
3331         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3332         {
3333             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3334                                     "handle_sorryserver_connection_fail(const boost::thread::id thread_id, "
3335                                     "const boost::asio::ip::tcp::endpoint& sorry_endpoint) : return_value = %d. thread id : %d.");
3336             formatter % status % boost::this_thread::get_id();
3337             putLogDebug(600094, formatter.str(), __FILE__, __LINE__ );
3338         }
3339         /*------DEBUG LOG END------*/
3340
3341         return status;
3342     }
3343
3344     //! called from after sorryserver send
3345     //! @param[in]    upstream thread id
3346     //! @return        session use EVENT mode.
3347     protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorryserver_send(
3348         const boost::thread::id thread_id)
3349     {
3350         /*-------- DEBUG LOG --------*/
3351         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3352         {
3353             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3354                                     "handle_sorryserver_send(const boost::thread::id thread_id) : thread_id = %d.");
3355             formatter % thread_id;
3356             putLogDebug(600095, formatter.str(), __FILE__, __LINE__ );
3357         }
3358         /*------DEBUG LOG END------*/
3359
3360         EVENT_TAG status                = FINALIZE;
3361         size_t http_header_all_offset            = 0;
3362         size_t http_header_all_len            = 0;
3363         size_t http_header_content_length_offset    = 0;
3364         size_t http_header_content_length_len        = 0;
3365         const size_t CR_LF_LEN                = 2;
3366         const size_t CR_LF_CR_LF_LEN            = 4;
3367         int content_length_value            = 0;
3368
3369         std::string content_length;
3370         cmatch regex_ret;
3371         cregex content_length_regex = icase("Content-Length") >> ":" >> *~_d >> (s1 = +_d) >> *~_d;
3372
3373
3374         bool find_ret = false;
3375         http_utility::CHECK_RESULT_TAG check_ret;
3376
3377         thread_data_ptr session_data_ptr;
3378         session_thread_data_map_it session_thread_it;
3379
3380         try
3381         {
3382             {
3383                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
3384
3385                 //thread id check
3386                 session_thread_it = session_thread_data_map.find(thread_id);
3387                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
3388                 {
3389                     boost::format formatter("Invalid thread id. thread id : %d.");
3390                     formatter % boost::this_thread::get_id();
3391                     putLogError(600065, formatter.str(), __FILE__, __LINE__ );
3392                     throw -1;
3393                 }
3394
3395                 session_data_ptr = session_thread_it->second;
3396             }
3397
3398             //current_message_rest_size > 0
3399             if (session_data_ptr->current_message_rest_size > 0)
3400             {
3401                 //data size > 0
3402                 if (session_data_ptr->data_length > 0)
3403                 {
3404                     //set return status
3405                     status = SORRYSERVER_CONNECT;
3406                 }
3407                 //data size is 0
3408                 else
3409                 {
3410                     //data offset is 0
3411                     session_data_ptr->data_offset = 0;
3412
3413                     //set return status
3414                     status =  CLIENT_RECV;
3415                 }
3416             }
3417             //current_message_rest_size is 0
3418             else
3419             {
3420                 //data size > 0
3421                 if (session_data_ptr->data_length> 0)
3422                 {
3423                     //data state is HTTP_BODY
3424                     if (session_data_ptr->data_state == HTTP_BODY)
3425                     {
3426                         //search whole http header, get whole http header's offset and length
3427                         find_ret = http_utility::find_http_header_all(session_data_ptr->data_buffer + session_data_ptr->data_offset,
3428                                                         session_data_ptr->data_length,
3429                                                         http_header_all_offset,
3430                                                         http_header_all_len
3431                                                        );
3432
3433                         /*-------- DEBUG LOG --------*/
3434                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3435                         {
3436                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3437                                                     "handle_sorryserver_send() : call find_http_header_all : "
3438                                                     "return_value = %d. thread id : %d.");
3439                             formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
3440                             putLogDebug(600096, formatter.str(), __FILE__, __LINE__ );
3441                         }
3442                         /*------DEBUG LOG END------*/
3443
3444                         //search http header result is NG
3445                         if (!find_ret)
3446                         {
3447                             //set data state HTTP_START
3448                             session_data_ptr->data_state = HTTP_START;
3449
3450                             //set return status
3451                             status = CLIENT_RECV;
3452                         }
3453                         //check method and version result is OK
3454                         else
3455                         {
3456                             //search Content_Length header
3457                             check_ret = http_utility::check_http_method_and_version(session_data_ptr->data_buffer + session_data_ptr->data_offset,
3458                                         session_data_ptr->data_length);
3459
3460                             /*-------- DEBUG LOG --------*/
3461                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3462                             {
3463                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3464                                                         "handle_sorryserver_send() : call check_http_method_and_version : "
3465                                                         "return_value = %d. thread id : %d.");
3466                                 formatter % check_ret % boost::this_thread::get_id();
3467                                 putLogDebug(600097, formatter.str(), __FILE__, __LINE__ );
3468                             }
3469                             /*------DEBUG LOG END------*/
3470
3471                             //check method and version result is NG
3472                             if (check_ret == http_utility::CHECK_NG)
3473                             {
3474                                 //set current message rest size
3475                                 session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
3476
3477                                 //set data state UNKNOWN
3478                                 session_data_ptr->data_state = UNKNOWN;
3479                             }
3480                             //check method and version result is OK
3481                             else
3482                             {
3483                                 //search Content_Length header
3484                                 find_ret = http_utility::find_http_header_content_length(session_data_ptr->data_buffer+session_data_ptr->data_offset,
3485                                            session_data_ptr->data_length,
3486                                            http_header_content_length_offset,
3487                                            http_header_content_length_len);
3488
3489                                 /*-------- DEBUG LOG --------*/
3490                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
3491                                 {
3492                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3493                                                             "handle_sorryserver_send() : call find_http_header_content_length : "
3494                                                             "return_value = %d. thread id : %d.");
3495                                     formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
3496                                     putLogDebug(600098, formatter.str(), __FILE__, __LINE__ );
3497                                 }
3498                                 /*------DEBUG LOG END------*/
3499
3500                                 //search Content_Length result is OK
3501                                 if (find_ret)
3502                                 {
3503                                     content_length.assign(session_data_ptr->data_buffer+session_data_ptr->data_offset + http_header_content_length_offset,
3504                                                           http_header_content_length_len );
3505                                     find_ret = regex_search(content_length.c_str(), regex_ret, content_length_regex);
3506
3507                                     //"content-length: ddd\r\n"
3508                                     if ( find_ret)
3509                                     {
3510                                         content_length = content_length.substr(
3511                                                              regex_ret.position(1),
3512                                                              regex_ret.length(1));
3513
3514                                         //set content length value
3515                                         content_length_value = boost::lexical_cast<int>(content_length);
3516                                     }
3517
3518                                     //http_header context is "\r\n\r\n" only
3519                                     if (http_header_all_len == 0)
3520                                     {
3521                                         //set current message rest size
3522                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + content_length_value + CR_LF_LEN;
3523                                     }
3524                                     else
3525                                     {
3526                                         //set current message rest size
3527                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + content_length_value + CR_LF_CR_LF_LEN;
3528                                     }
3529                                 }
3530                                 //search Content_Length result is OK
3531                                 else
3532                                 {
3533                                     //http_header context is "\r\n\r\n" only
3534                                     if (http_header_all_len == 0)
3535                                     {
3536                                         //set current message rest size
3537                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + CR_LF_LEN;
3538                                     }
3539                                     else
3540                                     {
3541                                         //set current message rest size
3542                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + CR_LF_CR_LF_LEN;
3543                                     }
3544
3545                                 }
3546
3547                                 //set data state HTTP_HEADER
3548                                 session_data_ptr->data_state = HTTP_HEADER;
3549
3550                             }
3551                             //set return status
3552                             status = SORRYSERVER_CONNECT;
3553                         }
3554                     }
3555                     //data state is UNKNOWN
3556                     else if (session_data_ptr->data_state == UNKNOWN)
3557                     {
3558                         //set return status
3559                         status = SORRYSERVER_CONNECT;
3560                     }
3561
3562                 }
3563                 //data size is 0
3564                 else
3565                 {
3566                     //data state is HTTP_BODY
3567                     if (session_data_ptr->data_state == HTTP_BODY)
3568                     {
3569                         //set data state HTTP_START
3570                         session_data_ptr->data_state = HTTP_START;
3571                     }
3572
3573                     //set data offset 0
3574                     session_data_ptr->data_offset = 0;
3575
3576                     //set return status
3577                     status = CLIENT_RECV;
3578                 }
3579             }
3580
3581             //set last status
3582             session_data_ptr->last_status = status;
3583         }
3584         catch (int e)
3585         {
3586             /*-------- DEBUG LOG --------*/
3587             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3588             {
3589                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3590                                         "handle_sorryserver_send() : catch exception e = %d. thread id : %d.");
3591                 formatter % e % boost::this_thread::get_id();
3592                 putLogDebug(600099, formatter.str(), __FILE__, __LINE__ );
3593             }
3594             /*------DEBUG LOG END------*/
3595
3596             //set return status
3597             status = FINALIZE;
3598         }
3599         catch (const boost::bad_lexical_cast& )
3600         {
3601             std::cerr << "protocol_module_ip::handle_sorryserver_send() : exception : " <<  "Content_Length field's value is invalid." << std::endl;
3602             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorryserver_send() : "
3603                                     "Content_Length field's value is invalid. thread id : %d.");
3604             formatter % boost::this_thread::get_id();
3605             putLogError(600066, formatter.str(), __FILE__, __LINE__ );
3606
3607             //set return status
3608             status = FINALIZE;
3609         }
3610         catch (const std::exception& ex)
3611         {
3612             std::cerr << "protocol_module_ip::handle_sorryserver_send() : exception : error = " << ex.what() << "." << std::endl;
3613             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3614                                     "handle_sorryserver_send() : exception : error = %s. thread id : %d.");
3615             formatter % ex.what() % boost::this_thread::get_id();
3616             putLogError(600067, formatter.str(), __FILE__, __LINE__ );
3617
3618             //set return status
3619             status = FINALIZE;
3620         }
3621         catch (...)
3622         {
3623             std::cerr << "protocol_module_ip::handle_sorryserver_send() : Unknown exception." << std::endl;
3624             boost::format formatter("function : protocol_module_base::EVENT_TAG "
3625                                     "protocol_module_ip::handle_sorryserver_send() : "
3626                                     "Unknown exception. thread id : %d.");
3627             formatter % boost::this_thread::get_id();
3628             putLogError(600068, formatter.str(), __FILE__, __LINE__ );
3629
3630             //set return status
3631             status = FINALIZE;
3632         }
3633
3634         /*-------- DEBUG LOG --------*/
3635         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3636         {
3637             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3638                                     "handle_sorryserver_send(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
3639             formatter % status % boost::this_thread::get_id();
3640             putLogDebug(600100, formatter.str(), __FILE__, __LINE__ );
3641         }
3642         /*------DEBUG LOG END------*/
3643
3644         return status;
3645     }
3646
3647     //! called from after realserver recive.for UDP
3648     //! @param[in]    downstream thread id
3649     //! @param[in]    realserver UDP endpoint reference
3650     //! @param[in]    recive from realserver buffer reference
3651     //! @param[in]    recv data length
3652     //! @return        session use EVENT mode.
3653     protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_recv(
3654         const boost::thread::id thread_id, const boost::asio::ip::udp::endpoint& rs_endpoint, const boost::array<char,
3655         MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
3656     {
3657         /*-------- DEBUG LOG --------*/
3658         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3659         {
3660             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3661                                     "handle_realserver_recv(const boost::thread::id thread_id, "
3662                                     "const boost::asio::ip::udp::endpoint& rs_endpoint, const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
3663                                     "const size_t& recvlen) : "
3664                                     "return_value = %d. thread id : %d.");
3665             formatter % STOP % boost::this_thread::get_id();
3666             putLogDebug(600101, formatter.str(), __FILE__, __LINE__ );
3667         }
3668         /*------DEBUG LOG END------*/
3669         return STOP;
3670     }
3671
3672     //! called from after realserver recvive for TCP/IP
3673     //! @param[in]    downstream thread id
3674     //! @param[in]    realserver TCP/IP endpoint reference
3675     //! @param[in]    realserver recive buffer reference.
3676     //! @param[in]    recv data length
3677     //! @return        session use EVENT mode.
3678     protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_recv(
3679         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint& rs_endpoint, const boost::array<char,
3680         MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
3681     {
3682         /*-------- DEBUG LOG --------*/
3683         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3684         {
3685             size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
3686             std::string buffer;
3687             dump_memory(recvbuffer.data(), buffer_size, buffer);
3688             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3689                                     "handle_realserver_recv(const boost::thread::id thread_id, "
3690                                     "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
3691                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
3692                                     "const size_t recvlen) : thread_id = %d, rs_endpoint = [%s]:%d, recvbuffer = %s, recvlen = %d.");
3693             formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port()
3694             % buffer % recvlen;
3695             putLogDebug(600102, formatter.str(), __FILE__, __LINE__ );
3696         }
3697         /*------DEBUG LOG END------*/
3698
3699         EVENT_TAG status                = FINALIZE;
3700
3701         bool find_ret                    = false;
3702         size_t http_header_offset            = 0;
3703         size_t http_header_len                = 0;
3704         size_t http_header_content_length_offset    = 0;
3705         size_t http_header_content_length_len        = 0;
3706         int content_length_value            = 0;
3707         const size_t CR_LF_LEN                = 2; //length of "\r\n"
3708         const size_t CR_LF_CR_LF_LEN            = 4; //length of "\r\n\r\n"
3709
3710         session_thread_data_map_it            session_thread_it;
3711         thread_data_ptr                    session_data_ptr;
3712         http_utility::CHECK_RESULT_TAG            check_ret;
3713
3714         std::string                    content_length;
3715         cmatch                        regex_ret;
3716         cregex content_length_regex = icase("Content-Length") >> ":" >> *~_d >> (s1 = +_d) >> *~_d;
3717
3718         if (unlikely(recvlen > recvbuffer.size()))
3719         {
3720             std::cerr << "protocol_module_ip::handle_realserver_recv() : Data size bigger than buffer size." << std::endl;
3721             boost::format formatter("Data size bigger than buffer size. thread id : %d.");
3722             formatter % boost::this_thread::get_id();
3723             putLogError(600069, formatter.str(), __FILE__, __LINE__ );
3724
3725             /*-------- DEBUG LOG --------*/
3726             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3727             {
3728                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3729                                         "handle_realserver_recv(const boost::thread::id thread_id, "
3730                                         "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
3731                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
3732                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
3733                 formatter % FINALIZE % boost::this_thread::get_id();
3734                 putLogDebug(600103, formatter.str(), __FILE__, __LINE__ );
3735             }
3736             /*------DEBUG LOG END------*/
3737
3738             return status;
3739         }
3740
3741         try
3742         {
3743             {
3744                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
3745
3746                 session_thread_it = session_thread_data_map.find(thread_id);
3747                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
3748                 {
3749                     boost::format formatter("Invalid thread id. thread id : %d.");
3750                     formatter % boost::this_thread::get_id();
3751                     putLogError(600070, formatter.str(), __FILE__, __LINE__ );
3752                     throw -1;
3753                 }
3754
3755                 session_data_ptr = session_thread_it->second;
3756             }
3757
3758             //set switch flag off
3759             session_data_ptr->switch_flag = SWITCH_FLAG_OFF;
3760
3761             /*-------- DEBUG LOG --------*/
3762             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3763             {
3764                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3765                                         "handle_realserver_recv(const boost::thread::id thread_id, "
3766                                         "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
3767                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
3768                                         "const size_t recvlen) : SWITCH_FLAG_OFF. "
3769                                         "thread_id = %d, rs_endpoint = [%s]:%d.");
3770                 formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
3771                 putLogDebug(600104, formatter.str(), __FILE__, __LINE__ );
3772             }
3773             /*------DEBUG LOG END------*/
3774
3775             //copy data from recvbuffer
3776             if (!get_data_from_recvbuffer(session_data_ptr, recvbuffer, recvlen))
3777             {
3778                 //copy failed
3779                 std::cerr << "protocol_module_ip::handle_realserver_recv() : Data size bigger than buffer size." << std::endl;
3780                 boost::format formatter("Data size bigger than buffer size. thread id : % id.");
3781                 formatter % boost::this_thread::get_id();
3782                 putLogError(600071, formatter.str(), __FILE__, __LINE__);
3783                 status = FINALIZE;
3784             }
3785             else
3786             {
3787                 //data state is HTTP_START
3788                 if (session_data_ptr->data_state == HTTP_START)
3789                 {
3790                     //search http header
3791                     find_ret = http_utility::find_http_header_all(session_data_ptr->data_buffer + session_data_ptr->data_offset,
3792                                                     session_data_ptr->data_length,
3793                                                     http_header_offset,
3794                                                     http_header_len
3795                                                    );
3796
3797                     /*-------- DEBUG LOG --------*/
3798                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
3799                     {
3800                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3801                                                 "handle_realserver_recv() : call find_http_header_all : "
3802                                                 "return_value = %d. thread id : %d.");
3803                         formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
3804                         putLogDebug(600105, formatter.str(), __FILE__, __LINE__ );
3805                     }
3806                     /*------DEBUG LOG END------*/
3807
3808                     //search http header result is NG
3809                     if (!find_ret)
3810                     {
3811                         //data size bigger than max buffer size
3812                         if (session_data_ptr->data_length >= MAX_IP_MODULE_BUFFER_SIZE - recvbuffer.size())
3813                         {
3814                             //set data state UNKNOWN
3815                             session_data_ptr->data_state = UNKNOWN;
3816                             //set current message rest size
3817                             session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
3818                         }
3819                     }
3820                     //search http header result is OK
3821                     else
3822                     {
3823                         //check http version and status code
3824                         check_ret = http_utility::check_http_version_and_status_code(session_data_ptr->data_buffer,
3825                                     session_data_ptr->data_length);
3826
3827                         /*-------- DEBUG LOG --------*/
3828                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3829                         {
3830                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3831                                                     "handle_realserver_recv() : call check_http_version_and_status_code : "
3832                                                     "return_value = %d. thread id : %d.");
3833                             formatter % check_ret % boost::this_thread::get_id();
3834                             putLogDebug(600106, formatter.str(), __FILE__, __LINE__ );
3835                         }
3836                         /*------DEBUG LOG END------*/
3837
3838                         //check http version and status code result is NG
3839                         if (check_ret == http_utility::CHECK_NG)
3840                         {
3841                             //set data state UNKNOWN
3842                             session_data_ptr->data_state = UNKNOWN;
3843                             //set current message rest size
3844                             session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
3845                         }
3846                         //check http version and status code result is OK
3847                         else
3848                         {
3849                             //search Content_Length header
3850                             find_ret = http_utility::find_http_header_content_length(session_data_ptr->data_buffer+session_data_ptr->data_offset,
3851                                        session_data_ptr->data_length,
3852                                        http_header_content_length_offset,
3853                                        http_header_content_length_len);
3854
3855                             /*-------- DEBUG LOG --------*/
3856                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3857                             {
3858                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3859                                                         "handle_realserver_recv() : call find_http_header_content_length : "
3860                                                         "return_value = %d. thread id : %d.");
3861                                 formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
3862                                 putLogDebug(600107, formatter.str(), __FILE__, __LINE__ );
3863                             }
3864                             /*------DEBUG LOG END------*/
3865
3866                             //search Content_Length result is OK
3867                             if (find_ret)
3868                             {
3869                                 //set content length string
3870                                 content_length.assign(session_data_ptr->data_buffer+session_data_ptr->data_offset + http_header_content_length_offset,
3871                                                       http_header_content_length_len );
3872                                 find_ret = regex_search(content_length.c_str(), regex_ret, content_length_regex);
3873
3874                                 //"content-length: ddd\r\n"
3875                                 if ( find_ret)
3876                                 {
3877                                     content_length = content_length.substr(
3878                                                          regex_ret.position(1),
3879                                                          regex_ret.length(1));
3880
3881                                     //set content length value
3882                                     content_length_value = boost::lexical_cast<int>(content_length);
3883                                 }
3884
3885                                 //http_header context is "\r\n\r\n" only
3886                                 if (http_header_len == 0)
3887                                 {
3888                                     //set current message rest size
3889                                     session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + content_length_value + CR_LF_LEN;
3890                                 }
3891                                 else
3892                                 {
3893                                     //set current message rest size
3894                                     session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + content_length_value + CR_LF_CR_LF_LEN;
3895                                 }
3896                             }
3897                             //search Content_Length result is NG
3898                             else
3899                             {
3900                                 //http_header context is "\r\n\r\n" only
3901                                 if (http_header_len == 0)
3902                                 {
3903                                     //set current message rest size
3904                                     session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + CR_LF_LEN;
3905                                 }
3906                                 else
3907                                 {
3908                                     //set current message rest size
3909                                     session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + CR_LF_CR_LF_LEN;
3910                                 }
3911
3912                             }
3913
3914                             //set data state HTTP_HEADER
3915                             session_data_ptr->data_state = HTTP_HEADER;
3916                         }
3917                     }
3918                 }
3919                 //data state is UNKNOWN
3920                 else if (session_data_ptr->data_state == UNKNOWN)
3921                 {
3922                     //set current message rest size
3923                     session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
3924                 }
3925                 else
3926                 {
3927                     //none
3928                 }
3929
3930                 //data state is HTTP_START
3931                 if (session_data_ptr->data_state == HTTP_START)
3932                 {
3933                     //set return status
3934                     status = REALSERVER_RECV;
3935                 }
3936                 //data state is not HTTP_START
3937                 else
3938                 {
3939                     //set return status
3940                     status = CLIENT_CONNECTION_CHECK;
3941                 }
3942             }
3943
3944             //set last status
3945             session_data_ptr->last_status = status;
3946         }
3947         catch (int e)
3948         {
3949             /*-------- DEBUG LOG --------*/
3950             if (unlikely(LOG_LV_DEBUG == getloglevel()))
3951             {
3952                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3953                                         "handle_realserver_recv() : catch exception e = %d. thread id : %d.");
3954                 formatter % e % boost::this_thread::get_id();
3955                 putLogDebug(600108, formatter.str(), __FILE__, __LINE__ );
3956             }
3957             /*------DEBUG LOG END------*/
3958
3959             //set return status
3960             status = FINALIZE;
3961         }
3962         catch (const boost::bad_lexical_cast& )
3963         {
3964             std::cerr << "protocol_module_ip::handle_realserver_recv() : exception : " <<  "Content_Length field's value is invalid." << std::endl;
3965             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_recv() : "
3966                                     "Content_Length field's value is invalid. thread id : %d.");
3967             formatter % boost::this_thread::get_id();
3968             putLogError(600072, formatter.str(), __FILE__, __LINE__ );
3969
3970             //set return status
3971             status = FINALIZE;
3972         }
3973         catch (const std::exception& ex)
3974         {
3975             std::cerr << "protocol_module_ip::handle_realserver_recv() : exception : error = " << ex.what() << "." << std::endl;
3976             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
3977                                     "handle_realserver_recv() : exception : error = %s. thread id : %d.");
3978             formatter % ex.what() % boost::this_thread::get_id();
3979             putLogError(600073, formatter.str(), __FILE__, __LINE__ );
3980
3981             //set return status
3982             status = FINALIZE;
3983         }
3984         catch (...)
3985         {
3986             std::cerr << "protocol_module_ip::handle_realserver_recv() : Unknown exception." << std::endl;
3987             boost::format formatter("function : protocol_module_base::EVENT_TAG "
3988                                     "protocol_module_ip::handle_realserver_recv() : "
3989                                     "Unknown exception. thread id : %d.");
3990             formatter % boost::this_thread::get_id();
3991             putLogError(600074, formatter.str(), __FILE__, __LINE__ );
3992
3993             //set return status
3994             status = FINALIZE;
3995         }
3996
3997         /*-------- DEBUG LOG --------*/
3998         if (unlikely(LOG_LV_DEBUG == getloglevel()))
3999         {
4000             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4001                                     "handle_realserver_recv(const boost::thread::id thread_id, "
4002                                     "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
4003                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4004                                     "const size_t recvlen) : return_value = %d. thread id : %d.");
4005             formatter % FINALIZE % boost::this_thread::get_id();
4006             putLogDebug(600109, formatter.str(), __FILE__, __LINE__ );
4007         }
4008         /*------DEBUG LOG END------*/
4009
4010         return status;
4011     }
4012
4013
4014
4015     //! called from after sorryserver recive
4016     //! @param[in]    downstream thread id
4017     //! @param[in]    sorryserver endpoint reference
4018     //! @param[in]    recive from realserver buffer reference.
4019     //! @param[in]    recv data length
4020     //! @return     session use EVENT mode
4021     protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorryserver_recv(
4022         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint& sorry_endpoint, const boost::array<
4023         char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
4024     {
4025         /*-------- DEBUG LOG --------*/
4026         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4027         {
4028             size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
4029             std::string buffer;
4030             dump_memory(recvbuffer.data(), buffer_size, buffer);
4031             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4032                                     "handle_sorryserver_recv(const boost::thread::id thread_id, "
4033                                     "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
4034                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4035                                     "const size_t recvlen) : thread_id = %d, sorry_endpoint = [%s]:%d, recvbuffer = %s, recvlen = %d.");
4036             formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port()
4037             % buffer % recvlen;
4038             putLogDebug(600110, formatter.str(), __FILE__, __LINE__ );
4039         }
4040         /*------DEBUG LOG END------*/
4041
4042         EVENT_TAG status                = FINALIZE;
4043         bool find_ret                    = false;
4044         size_t http_header_offset            = 0;
4045         size_t http_header_len                = 0;
4046         size_t http_header_content_length_offset    = 0;
4047         size_t http_header_content_length_len        = 0;
4048         int content_length_value            = 0;
4049         const size_t CR_LF_LEN                = 2; //length of "\r\n"
4050         const size_t CR_LF_CR_LF_LEN            = 4; //length of "\r\n\r\n"
4051
4052         session_thread_data_map_it            session_thread_it;
4053         thread_data_ptr                    session_data_ptr;
4054         http_utility::CHECK_RESULT_TAG            check_ret;
4055         std::string                    content_length;
4056         cmatch                        regex_ret;
4057         cregex content_length_regex = icase("Content-Length") >> ":" >> *~_d >> (s1 = +_d) >> *~_d;
4058
4059         if (unlikely(recvlen > recvbuffer.size()))
4060         {
4061             std::cerr << "protocol_module_ip::handle_sorryserver_recv() : Data size bigger than buffer size." << std::endl;
4062             boost::format formatter("Data size bigger than buffer size. thread id : %d.");
4063             formatter % boost::this_thread::get_id();
4064             putLogError(600075, formatter.str(), __FILE__, __LINE__ );
4065
4066             /*-------- DEBUG LOG --------*/
4067             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4068             {
4069                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4070                                         "handle_sorryserver_recv(const boost::thread::id thread_id, "
4071                                         "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
4072                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4073                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
4074                 formatter % FINALIZE % boost::this_thread::get_id();
4075                 putLogDebug(600111, formatter.str(), __FILE__, __LINE__ );
4076             }
4077             /*------DEBUG LOG END------*/
4078
4079             return status;
4080         }
4081
4082         try
4083         {
4084             {
4085                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
4086
4087                 session_thread_it = session_thread_data_map.find(thread_id);
4088                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
4089                 {
4090                     boost::format formatter("Invalid thread id. thread id : %d.");
4091                     formatter % boost::this_thread::get_id();
4092                     putLogError(600076, formatter.str(), __FILE__, __LINE__ );
4093                     throw -1;
4094                 }
4095
4096                 session_data_ptr = session_thread_it->second;
4097             }
4098
4099             //set switch flag off
4100             session_data_ptr->switch_flag = SWITCH_FLAG_OFF;
4101
4102             /*-------- DEBUG LOG --------*/
4103             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4104             {
4105                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4106                                         "handle_sorryserver_recv(const boost::thread::id thread_id, "
4107                                         "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
4108                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4109                                         "const size_t recvlen) : SWITCH_FLAG_OFF. "
4110                                         "thread_id = %d, rs_endpoint = [%s]:%d.");
4111                 formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port();
4112                 putLogDebug(600112, formatter.str(), __FILE__, __LINE__ );
4113             }
4114             /*------DEBUG LOG END------*/
4115
4116             //copy data from recvbuffer
4117             if (!get_data_from_recvbuffer(session_data_ptr, recvbuffer, recvlen))
4118             {
4119                 //copy failed
4120                 std::cerr << "protocol_module_ip::handle_sorryserver_recv() : Data size bigger than buffer size." << std::endl;
4121                 boost::format formatter("Data size bigger than buffer size. thread id : % id.");
4122                 formatter % boost::this_thread::get_id();
4123                 putLogError(600077, formatter.str(), __FILE__, __LINE__);
4124
4125                 status = FINALIZE;
4126             }
4127             else
4128             {
4129                 //data state is HTTP_START
4130                 if (session_data_ptr->data_state == HTTP_START)
4131                 {
4132                     //search http header
4133                     find_ret = http_utility::find_http_header_all(session_data_ptr->data_buffer + session_data_ptr->data_offset,
4134                                                     session_data_ptr->data_length,
4135                                                     http_header_offset,
4136                                                     http_header_len
4137                                                    );
4138
4139                     /*-------- DEBUG LOG --------*/
4140                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
4141                     {
4142                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4143                                                 "handle_sorryserver_recv() : call find_http_header_all : "
4144                                                 "return_value = %d. thread id : %d.");
4145                         formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
4146                         putLogDebug(600113, formatter.str(), __FILE__, __LINE__ );
4147                     }
4148                     /*------DEBUG LOG END------*/
4149
4150                     //search http header result is NG
4151                     if (!find_ret)
4152                     {
4153                         //data size bigger than max buffer size
4154                         if (session_data_ptr->data_length >= MAX_IP_MODULE_BUFFER_SIZE - recvbuffer.size())
4155                         {
4156                             //set data state UNKNOWN
4157                             session_data_ptr->data_state = UNKNOWN;
4158                             //set current message rest size
4159                             session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
4160                         }
4161                     }
4162                     //search http header result is OK
4163                     else
4164                     {
4165                         //check http version and status code
4166                         check_ret = http_utility::check_http_version_and_status_code(session_data_ptr->data_buffer,
4167                                     session_data_ptr->data_length);
4168
4169                         /*-------- DEBUG LOG --------*/
4170                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4171                         {
4172                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4173                                                     "handle_sorryserver_recv() : call check_http_version_and_status_code : "
4174                                                     "return_value = %d. thread id : %d.");
4175                             formatter % check_ret % boost::this_thread::get_id();
4176                             putLogDebug(600114, formatter.str(), __FILE__, __LINE__ );
4177                         }
4178                         /*------DEBUG LOG END------*/
4179
4180                         //check http version and status code result is NG
4181                         if (check_ret == http_utility::CHECK_NG)
4182                         {
4183                             //set data state UNKNOWN
4184                             session_data_ptr->data_state = UNKNOWN;
4185                             //set current message rest size
4186                             session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
4187                         }
4188                         //check http version and status code result is OK
4189                         else
4190                         {
4191                             //search Content_Length header
4192                             find_ret = http_utility::find_http_header_content_length(session_data_ptr->data_buffer+session_data_ptr->data_offset,
4193                                        session_data_ptr->data_length,
4194                                        http_header_content_length_offset,
4195                                        http_header_content_length_len);
4196
4197                             /*-------- DEBUG LOG --------*/
4198                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4199                             {
4200                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4201                                                         "handle_sorryserver_recv() : call find_http_header_content_length : "
4202                                                         "return_value = %d. thread id : %d.");
4203                                 formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
4204                                 putLogDebug(600115, formatter.str(), __FILE__, __LINE__ );
4205                             }
4206                             /*------DEBUG LOG END------*/
4207
4208                             //search Content_Length result is OK
4209                             if (find_ret)
4210                             {
4211                                 ////set content length string
4212                                 content_length.assign(session_data_ptr->data_buffer+session_data_ptr->data_offset + http_header_content_length_offset,
4213                                                       http_header_content_length_len );
4214                                 find_ret = regex_search(content_length.c_str(), regex_ret, content_length_regex);
4215
4216                                 //"content-length: ddd\r\n"
4217                                 if ( find_ret)
4218                                 {
4219                                     content_length = content_length.substr(
4220                                                          regex_ret.position(1),
4221                                                          regex_ret.length(1));
4222
4223                                     //set content length value
4224                                     content_length_value = boost::lexical_cast<int>(content_length);
4225                                 }
4226
4227                                 //http_header context is "\r\n\r\n" only
4228                                 if (http_header_len == 0)
4229                                 {
4230                                     //set current message rest size
4231                                     session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + content_length_value + CR_LF_LEN;
4232                                 }
4233                                 else
4234                                 {
4235                                     //set current message rest size
4236                                     session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + content_length_value + CR_LF_CR_LF_LEN;
4237                                 }
4238                             }
4239                             //search Content_Length result is NG
4240                             else
4241                             {
4242                                 //http_header context is "\r\n\r\n" only
4243                                 if (http_header_len == 0)
4244                                 {
4245                                     //set current message rest size
4246                                     session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + CR_LF_LEN;
4247                                 }
4248                                 else
4249                                 {
4250                                     //set current message rest size
4251                                     session_data_ptr->current_message_rest_size = http_header_offset + http_header_len + CR_LF_CR_LF_LEN;
4252                                 }
4253
4254                             }
4255
4256                             //set data state HTTP_HEADER
4257                             session_data_ptr->data_state = HTTP_HEADER;
4258
4259                         }
4260                     }
4261                 }
4262                 //data state is UNKNOWN
4263                 else if (session_data_ptr->data_state == UNKNOWN)
4264                 {
4265                     //set current message rest size
4266                     session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
4267                 }
4268                 else
4269                 {
4270                     //none
4271                 }
4272
4273                 //data state is HTTP_START
4274                 if (session_data_ptr->data_state == HTTP_START)
4275                 {
4276                     //set return status
4277                     status = SORRYSERVER_RECV;
4278                 }
4279                 //data state is not HTTP_START
4280                 else
4281                 {
4282                     //set return status
4283                     status = CLIENT_CONNECTION_CHECK;
4284                 }
4285             }
4286
4287             //set last status
4288             session_data_ptr->last_status = status;
4289         }
4290         catch (int e)
4291         {
4292             /*-------- DEBUG LOG --------*/
4293             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4294             {
4295                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4296                                         "handle_sorryserver_recv() : catch exception e = %d. thread id : %d.");
4297                 formatter % e % boost::this_thread::get_id();
4298                 putLogDebug(600116, formatter.str(), __FILE__, __LINE__ );
4299             }
4300             /*------DEBUG LOG END------*/
4301
4302             //set return status
4303             status = FINALIZE;
4304         }
4305         catch (const boost::bad_lexical_cast& )
4306         {
4307             std::cerr << "protocol_module_ip::handle_sorryserver_recv() : exception : " <<  "Content_Length field's value is invalid." << std::endl;
4308             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorryserver_recv() : "
4309                                     "Content_Length field's value is invalid. thread id : %d.");
4310             formatter % boost::this_thread::get_id();
4311             putLogError(600078, formatter.str(), __FILE__, __LINE__ );
4312
4313             //set return status
4314             status = FINALIZE;
4315         }
4316         catch (const std::exception& ex)
4317         {
4318             std::cerr << "protocol_module_ip::handle_sorryserver_recv() : exception : error = " << ex.what() << "." << std::endl;
4319             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4320                                     "handle_sorryserver_recv() : exception : error = %s. thread id : %d.");
4321             formatter % ex.what() % boost::this_thread::get_id();
4322             putLogError(600079, formatter.str(), __FILE__, __LINE__ );
4323
4324             //set return status
4325             status = FINALIZE;
4326         }
4327         catch (...)
4328         {
4329             std::cerr << "protocol_module_ip::handle_sorryserver_recv() : Unknown exception." << std::endl;
4330             boost::format formatter("function : protocol_module_base::EVENT_TAG "
4331                                     "protocol_module_ip::handle_sorryserver_recv() : "
4332                                     "Unknown exception. thread id : %d.");
4333             formatter % boost::this_thread::get_id();
4334             putLogError(600080, formatter.str(), __FILE__, __LINE__ );
4335
4336             //set return status
4337             status = FINALIZE;
4338         }
4339
4340         /*-------- DEBUG LOG --------*/
4341         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4342         {
4343             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4344                                     "handle_sorryserver_recv(const boost::thread::id thread_id, "
4345                                     "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
4346                                     "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4347                                     "const size_t recvlen) : return_value = %d. thread id : %d.");
4348             formatter % FINALIZE % boost::this_thread::get_id();
4349             putLogDebug(600117, formatter.str(), __FILE__, __LINE__ );
4350         }
4351         /*------DEBUG LOG END------*/
4352
4353         return status;
4354
4355     }
4356
4357     //! called from UPSTEEARM thread. make module original message.
4358     //! @param[in]    downstream thread id.
4359     //! @return     session use EVENT mode
4360     protocol_module_base::EVENT_TAG protocol_module_ip::handle_response_send_inform(
4361         const boost::thread::id thread_id)
4362     {
4363         /*-------- DEBUG LOG --------*/
4364         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4365         {
4366             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4367                                     "handle_response_send_inform(const boost::thread::id thread_id) : "
4368                                     "return_value = %d. thread id : %d.");
4369             formatter % STOP % boost::this_thread::get_id();
4370             putLogDebug(600118, formatter.str(), __FILE__, __LINE__ );
4371         }
4372         /*------DEBUG LOG END------*/
4373
4374         return STOP;
4375     }
4376
4377     //! called from after client connection check. use TCP/IP only. create client send message.
4378     //! @param[in]    downstream thread id
4379     //! @param[out]    send budffer reference
4380     //! @param[out]    send data length
4381     //! @return     session use EVENT mode
4382     protocol_module_base::EVENT_TAG protocol_module_ip::handle_client_connection_check(
4383         const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
4384     {
4385         /*-------- DEBUG LOG --------*/
4386         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4387         {
4388             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4389                                     "handle_client_connection_check(const boost::thread::id thread_id, "
4390                                     "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : "
4391                                     "thread_id = %d.");
4392             formatter % thread_id;
4393             putLogDebug(600119, formatter.str(), __FILE__, __LINE__ );
4394         }
4395         /*------DEBUG LOG END------*/
4396
4397         EVENT_TAG status = FINALIZE;
4398         thread_data_ptr session_data_ptr;
4399         size_t send_possible_size = 0;
4400
4401         try
4402         {
4403             {
4404                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
4405
4406                 session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
4407                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
4408                 {
4409                     boost::format formatter("Invalid thread id. thread id : %d.");
4410                     formatter % boost::this_thread::get_id();
4411                     putLogError(600081, formatter.str(), __FILE__, __LINE__ );
4412                     throw -1;
4413                 }
4414
4415                 session_data_ptr = session_thread_it->second;
4416             }
4417
4418             //data state is HTTP_HEADER
4419             if (session_data_ptr->data_state == HTTP_HEADER)
4420             {
4421                 //set data HTTP_BODY
4422                 session_data_ptr->data_state = HTTP_BODY;
4423             }
4424
4425             //set send possible data size
4426             send_possible_size = std::min(std::min(sendbuffer.size(), session_data_ptr->current_message_rest_size),
4427                                           session_data_ptr->data_length
4428                                          );
4429             //set send data size
4430             datalen = send_possible_size;
4431
4432             /*-------- DEBUG LOG --------*/
4433             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4434             {
4435                 std::string datadump;
4436                 dump_memory(session_data_ptr->data_buffer + session_data_ptr->data_offset, send_possible_size, datadump);
4437
4438                 boost::format formatter(
4439                     "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4440                     "handle_client_connection_check() : before memcpy (data dump) : "
4441                     "data begin = %d, data_size = %d, data = %s");
4442                 formatter % session_data_ptr->data_offset % send_possible_size % datadump;
4443                 putLogDebug(600120, formatter.str(), __FILE__, __LINE__ );
4444             }
4445             /*------DEBUG LOG END------*/
4446
4447             //copy send possible data to sendbuffer
4448             memcpy(sendbuffer.data(), session_data_ptr->data_buffer + session_data_ptr->data_offset,
4449                    send_possible_size);
4450
4451             /*-------- DEBUG LOG --------*/
4452             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4453             {
4454                 std::string datadump;
4455                 dump_memory(sendbuffer.data(), send_possible_size, datadump);
4456
4457                 boost::format formatter(
4458                     "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4459                     "handle_client_connection_check() : after memcpy (data dump) : "
4460                     "data begin = 0, data_size = %d, data = %s");
4461                 formatter % send_possible_size % datadump;
4462                 putLogDebug(600121, formatter.str(), __FILE__, __LINE__ );
4463             }
4464             /*------DEBUG LOG END------*/
4465
4466             //set current message rest size
4467             session_data_ptr->current_message_rest_size -= send_possible_size;
4468
4469             //set buffer's position
4470             session_data_ptr->data_offset += send_possible_size;
4471             session_data_ptr->data_length -= send_possible_size;
4472
4473             //current message rest size is 0
4474             if (session_data_ptr->current_message_rest_size == 0)
4475             {
4476                 boost::mutex::scoped_lock lock(session_data_mutex);
4477                 time_t now;
4478                 time(&now);
4479                 boost::asio::ip::tcp::endpoint init_endpoint;
4480
4481                 //write session data to session table
4482                 ip_data_processor->write_session_data(session_data_ptr->ip_hash,
4483                                                       init_endpoint,
4484                                                       now);
4485
4486             }
4487
4488             //set return status
4489             status = CLIENT_SEND;
4490
4491             //set last status
4492             session_data_ptr->last_status = status;
4493         }
4494         catch (int e)
4495         {
4496             /*-------- DEBUG LOG --------*/
4497             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4498             {
4499                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4500                                         "handle_client_connection_check() : catch exception e = %d. thread id : %d.");
4501                 formatter % e % boost::this_thread::get_id();
4502                 putLogDebug(600122, formatter.str(), __FILE__, __LINE__ );
4503             }
4504             /*------DEBUG LOG END------*/
4505
4506             //set return status
4507             status = FINALIZE;
4508         }
4509         catch (const std::exception& ex)
4510         {
4511             std::cerr << "protocol_module_ip::handle_client_connection_check() : exception : error = " << ex.what() << "." << std::endl;
4512             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4513                                     "handle_client_connection_check() : exception : error = %s. thread id : %d.");
4514             formatter % ex.what() % boost::this_thread::get_id();
4515             putLogError(600082, formatter.str(), __FILE__, __LINE__ );
4516
4517             //set return status
4518             status = FINALIZE;
4519         }
4520         catch (...)
4521         {
4522             std::cerr << "protocol_module_ip::handle_client_connection_check() : Unknown exception." << std::endl;
4523             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4524                                     "handle_client_connection_check() : Unknown exception. thread id : %d.");
4525             formatter % boost::this_thread::get_id();
4526             putLogError(600083, formatter.str(), __FILE__, __LINE__ );
4527
4528             //set return status
4529             status = FINALIZE;
4530         }
4531
4532         /*-------- DEBUG LOG --------*/
4533         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4534         {
4535             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4536                                     "handle_client_connection_check(const boost::thread::id thread_id, "
4537                                     "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : return_value = %d. thread id : %d.");
4538             formatter % status % boost::this_thread::get_id();
4539             putLogDebug(600123, formatter.str(), __FILE__, __LINE__ );
4540         }
4541         /*------DEBUG LOG END------*/
4542
4543         return status;
4544     }
4545
4546     //! called from after client select. use UDP only
4547     //! @param[in]    downstream thread id
4548     //!    @param[in]    client udp endpoint
4549     //! @param[out]    send buffer reference
4550     //! @param[out]    send data length
4551     //! @return     session use EVENT mode
4552     protocol_module_base::EVENT_TAG protocol_module_ip::handle_client_select(
4553         const boost::thread::id thread_id, boost::asio::ip::udp::endpoint& cl_endpoint, boost::array<char,
4554         MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
4555     {
4556         /*-------- DEBUG LOG --------*/
4557         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4558         {
4559             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4560                                     "handle_client_select(const boost::thread::id thread_id, "
4561                                     "boost::asio::ip::udp::endpoint& cl_endpoint, boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
4562                                     "const size_t& datalen) : "
4563                                     "return_value = %d. thread id : %d.");
4564             formatter % STOP % boost::this_thread::get_id();
4565             putLogDebug(600124, formatter.str(), __FILE__, __LINE__ );
4566         }
4567         /*------DEBUG LOG END------*/
4568         return STOP;
4569     }
4570
4571     //!    called from after client send
4572     //!    @param[in]    downstream thread id
4573     //! @return     session use EVENT mode
4574     protocol_module_base::EVENT_TAG protocol_module_ip::handle_client_send(
4575         const boost::thread::id thread_id)
4576     {
4577         /*-------- DEBUG LOG --------*/
4578         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4579         {
4580             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4581                                     "handle_client_send(const boost::thread::id thread_id) : thread_id = %d.");
4582             formatter % thread_id;
4583             putLogDebug(600125, formatter.str(), __FILE__, __LINE__ );
4584         }
4585         /*------DEBUG LOG END------*/
4586
4587         EVENT_TAG status = FINALIZE;
4588         size_t http_header_all_offset        = 0;
4589         size_t http_header_all_len        = 0;
4590         size_t http_header_content_length_offset= 0;
4591         size_t http_header_content_length_len    = 0;
4592         const size_t CR_LF_LEN             = 2;
4593         const size_t CR_LF_CR_LF_LEN        = 4;
4594         int content_length_value        = 0;
4595
4596         std::string content_length;
4597         cmatch regex_ret;
4598         cregex content_length_regex = icase("Content-Length") >> ":" >> *~_d >> (s1 = +_d) >> *~_d;
4599
4600
4601         bool find_ret = false;
4602         http_utility::CHECK_RESULT_TAG check_ret;
4603
4604         thread_data_ptr session_data_ptr;
4605         session_thread_data_map_it session_thread_it;
4606
4607         try
4608         {
4609             {
4610                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
4611
4612                 //thread id check
4613                 session_thread_it = session_thread_data_map.find(thread_id);
4614                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
4615                 {
4616                     boost::format formatter("Invalid thread id. thread id : %d.");
4617                     formatter % boost::this_thread::get_id();
4618                     putLogError(600084, formatter.str(), __FILE__, __LINE__ );
4619                     throw -1;
4620                 }
4621
4622                 session_data_ptr = session_thread_it->second;
4623             }
4624
4625             //current_message_rest_size > 0
4626             if (session_data_ptr->current_message_rest_size > 0)
4627             {
4628                 //data size > 0
4629                 if (session_data_ptr->data_length > 0)
4630                 {
4631                     //set return status
4632                     status = CLIENT_CONNECTION_CHECK;
4633                 }
4634                 //data size is 0
4635                 else
4636                 {
4637                     //data offset is 0
4638                     session_data_ptr->data_offset = 0;
4639
4640                     //set return status
4641                     status = REALSERVER_RECV;
4642                 }
4643             }
4644             //current_message_rest_size is 0
4645             else
4646             {
4647                 //data size > 0
4648                 if (session_data_ptr->data_length > 0)
4649                 {
4650                     //data state is HTTP_BODY
4651                     if (session_data_ptr->data_state == HTTP_BODY)
4652                     {
4653                         //search whole http header, get whole http header's offset and length
4654                         find_ret = http_utility::find_http_header_all(session_data_ptr->data_buffer + session_data_ptr->data_offset,
4655                                                         session_data_ptr->data_length,
4656                                                         http_header_all_offset,
4657                                                         http_header_all_len
4658                                                        );
4659
4660                         /*-------- DEBUG LOG --------*/
4661                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4662                         {
4663                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4664                                                     "handle_client_send() : call find_http_header_all : "
4665                                                     "return_value = %d. thread id : %d.");
4666                             formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
4667                             putLogDebug(600126, formatter.str(), __FILE__, __LINE__ );
4668                         }
4669                         /*------DEBUG LOG END------*/
4670
4671                         //search http header result is NG
4672                         if (!find_ret)
4673                         {
4674                             //set data state HTTP_START
4675                             session_data_ptr->data_state = HTTP_START;
4676
4677                             //set return status
4678                             status = REALSERVER_RECV;
4679                         }
4680                         //search http header result is OK
4681                         else
4682                         {
4683                             //check http version and status code
4684                             check_ret = http_utility::check_http_version_and_status_code(session_data_ptr->data_buffer + session_data_ptr->data_offset,
4685                                         session_data_ptr->data_length);
4686
4687                             /*-------- DEBUG LOG --------*/
4688                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4689                             {
4690                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4691                                                         "handle_client_send() : call check_http_version_and_status_code : "
4692                                                         "return_value = %d. thread id : %d.");
4693                                 formatter % check_ret % boost::this_thread::get_id();
4694                                 putLogDebug(600127, formatter.str(), __FILE__, __LINE__ );
4695                             }
4696                             /*------DEBUG LOG END------*/
4697
4698                             //check version and status code result is NG
4699                             if (check_ret == http_utility::CHECK_NG)
4700                             {
4701                                 //set current message rest size
4702                                 session_data_ptr->current_message_rest_size = session_data_ptr->data_length;
4703
4704                                 //set data state UNKNOWN
4705                                 session_data_ptr->data_state = UNKNOWN;
4706                             }
4707                             //check version and status code result is OK
4708                             else
4709                             {
4710                                 //search Content_Length header
4711                                 find_ret = http_utility::find_http_header_content_length(session_data_ptr->data_buffer+session_data_ptr->data_offset,
4712                                            session_data_ptr->data_length,
4713                                            http_header_content_length_offset,
4714                                            http_header_content_length_len);
4715
4716                                 /*-------- DEBUG LOG --------*/
4717                                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
4718                                 {
4719                                     boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4720                                                             "handle_client_send() : call find_http_header_content_length : "
4721                                                             "return_value = %d. thread id : %d.");
4722                                     formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
4723                                     putLogDebug(600128, formatter.str(), __FILE__, __LINE__ );
4724                                 }
4725                                 /*------DEBUG LOG END------*/
4726
4727                                 //search Content_Length result is OK
4728                                 if (find_ret)
4729                                 {
4730                                     content_length.assign(session_data_ptr->data_buffer+session_data_ptr->data_offset + http_header_content_length_offset,
4731                                                           http_header_content_length_len );
4732                                     find_ret = regex_search(content_length.c_str(), regex_ret, content_length_regex);
4733
4734                                     //"content-length: ddd\r\n"
4735                                     if ( find_ret)
4736                                     {
4737                                         content_length = content_length.substr(
4738                                                              regex_ret.position(1),
4739                                                              regex_ret.length(1));
4740
4741                                         //set content length value
4742                                         content_length_value = boost::lexical_cast<int>(content_length);
4743                                     }
4744
4745                                     //http_header context is "\r\n\r\n" only
4746                                     if (http_header_all_len == 0)
4747                                     {
4748                                         //set current message rest size
4749                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + content_length_value + CR_LF_LEN;
4750                                     }
4751                                     else
4752                                     {
4753                                         //set current message rest size
4754                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + content_length_value + CR_LF_CR_LF_LEN;
4755                                     }
4756                                 }
4757                                 //search Content_Length result is OK
4758                                 else
4759                                 {
4760                                     //http_header context is "\r\n\r\n" only
4761                                     if (http_header_all_len == 0)
4762                                     {
4763                                         //set current message rest size
4764                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + CR_LF_LEN;
4765                                     }
4766                                     else
4767                                     {
4768                                         //set current message rest size
4769                                         session_data_ptr->current_message_rest_size = http_header_all_offset + http_header_all_len + CR_LF_CR_LF_LEN;
4770                                     }
4771
4772                                 }
4773
4774                                 //set data state HTTP_HEADER
4775                                 session_data_ptr->data_state = HTTP_HEADER;
4776                             }
4777
4778                             //set return status
4779                             status = CLIENT_CONNECTION_CHECK;
4780                         }
4781                     }
4782                     //data state is UNKNOWN
4783                     else if (session_data_ptr->data_state == UNKNOWN)
4784                     {
4785                         //set return status
4786                         status = CLIENT_CONNECTION_CHECK;
4787                     }
4788                 }
4789                 //data size is 0
4790                 else
4791                 {
4792                     //data state is HTTP_BODY
4793                     if (session_data_ptr->data_state == HTTP_BODY)
4794                     {
4795                         //set data state HTTP_START
4796                         session_data_ptr->data_state = HTTP_START;
4797                     }
4798
4799                     //set data offset 0
4800                     session_data_ptr->data_offset = 0;
4801
4802                     //set return status
4803                     status = REALSERVER_RECV;
4804                 }
4805             }
4806
4807             //switch flag is on and status is REALSERVER_RECV
4808             if (session_data_ptr->switch_flag == SWITCH_FLAG_ON
4809                     && status == REALSERVER_RECV)
4810             {
4811                 //set return status
4812                 status = CLIENT_DISCONNECT;
4813             }
4814             //sorry flag is on and and status is REALSERVER_RECV
4815             else if (session_data_ptr->sorry_flag == SORRY_FLAG_ON
4816                      && status == REALSERVER_RECV)
4817             {
4818                 //set return status
4819                 status = SORRYSERVER_RECV;
4820             }
4821             else
4822             {
4823                 //none
4824             }
4825
4826             //set last status
4827             session_data_ptr->last_status = status;
4828         }
4829         catch (int e)
4830         {
4831             /*-------- DEBUG LOG --------*/
4832             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4833             {
4834                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4835                                         "handle_client_send() : catch exception e = %d. thread id : %d.");
4836                 formatter % e % boost::this_thread::get_id();
4837                 putLogDebug(600129, formatter.str(), __FILE__, __LINE__ );
4838             }
4839             /*------DEBUG LOG END------*/
4840
4841             //set last status
4842             status = FINALIZE;
4843         }
4844         catch (const boost::bad_lexical_cast& )
4845         {
4846             std::cerr << "protocol_module_ip::handle_client_send() : exception : " <<  "Content_Length field's value is invalid." << std::endl;
4847             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::handle_client_send() : "
4848                                     "Content_Length field's value is invalid. thread id : %d.");
4849             formatter % boost::this_thread::get_id();
4850             putLogError(600085, formatter.str(), __FILE__, __LINE__ );
4851
4852             //set last status
4853             status = FINALIZE;
4854         }
4855         catch (const std::exception& ex)
4856         {
4857             std::cerr << "protocol_module_ip::handle_client_send() : exception : error = " << ex.what() << "." << std::endl;
4858             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4859                                     "handle_client_send() : exception : error = %s. thread id : %d.");
4860             formatter % ex.what() % boost::this_thread::get_id();
4861             putLogError(600086, formatter.str(), __FILE__, __LINE__ );
4862
4863             //set last status
4864             status = FINALIZE;
4865         }
4866         catch (...)
4867         {
4868             std::cerr << "protocol_module_ip::handle_client_send() : Unknown exception." << std::endl;
4869             boost::format formatter("function : protocol_module_base::EVENT_TAG "
4870                                     "protocol_module_ip::handle_client_send() : "
4871                                     "Unknown exception. thread id : %d.");
4872             formatter % boost::this_thread::get_id();
4873             putLogError(600087, formatter.str(), __FILE__, __LINE__ );
4874
4875             //set last status
4876             status = FINALIZE;
4877         }
4878
4879         /*-------- DEBUG LOG --------*/
4880         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4881         {
4882             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4883                                     "handle_client_send(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
4884             formatter % status % boost::this_thread::get_id();
4885             putLogDebug(600130, formatter.str(), __FILE__, __LINE__ );
4886         }
4887         /*------DEBUG LOG END------*/
4888
4889         return status;
4890     }
4891
4892     //! call from client disconnect event. use upstream thread and downstream thread.
4893     //! @param[in]    upstream and downstream thread id( check! one thread one event! )
4894     //! @return     session use EVENT mode
4895     protocol_module_base::EVENT_TAG protocol_module_ip::handle_client_disconnect(
4896         const boost::thread::id thread_id)
4897     {
4898         /*-------- DEBUG LOG --------*/
4899         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4900         {
4901             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4902                                     "handle_client_disconnect(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
4903             formatter % FINALIZE % boost::this_thread::get_id();
4904             putLogDebug(600131, formatter.str(), __FILE__, __LINE__ );
4905         }
4906         /*------DEBUG LOG END------*/
4907         return FINALIZE;
4908     }
4909
4910     //! call from sorry mode event. use upstream thread and downstream thread
4911     //! @param[in]    upstream and downstream thread id( check! one thread one event and first time call pattern )
4912     //! @return     session use EVENT mode
4913     protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorry_enable(
4914         const boost::thread::id thread_id)
4915     {
4916         /*-------- DEBUG LOG --------*/
4917         if (unlikely(LOG_LV_DEBUG == getloglevel()))
4918         {
4919             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4920                                     "handle_sorry_enable(const boost::thread::id thread_id) : thread_id = %d.");
4921             formatter % boost::this_thread::get_id();
4922             putLogDebug(600132, formatter.str(), __FILE__, __LINE__ );
4923         }
4924         /*------DEBUG LOG END------*/
4925
4926         EVENT_TAG status = FINALIZE;
4927         thread_data_ptr session_data_ptr;
4928
4929         try
4930         {
4931             {
4932                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
4933
4934                 session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
4935                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
4936                 {
4937                     boost::format formatter("Invalid thread id. thread id : %d.");
4938                     formatter % boost::this_thread::get_id();
4939                     putLogError(600088, formatter.str(), __FILE__, __LINE__ );
4940                     throw -1;
4941                 }
4942
4943                 session_data_ptr = session_thread_it->second;
4944             }
4945
4946
4947             //up thread
4948             if (session_data_ptr->thread_division == THREAD_DIVISION_UP_STREAM)
4949             {
4950                 //accept_end_flag is off
4951                 if (session_data_ptr->accept_end_flag == ACCEPT_END_FLAG_OFF)
4952                 {
4953                     //set return status
4954                     status = ACCEPT;
4955                 }
4956                 //accept_end_flag is on
4957                 else
4958                 {
4959                     //sorry flag is on
4960                     if (session_data_ptr->sorry_flag == SORRY_FLAG_ON)
4961                     {
4962                         //data state is HTTP_START or HTTP_HEADER
4963                         if (session_data_ptr->data_state == HTTP_START
4964                                 || session_data_ptr->data_state ==HTTP_HEADER)
4965                         {
4966                             //set switch flag on
4967                             session_data_ptr->switch_flag = SWITCH_FLAG_ON;
4968
4969                             /*-------- DEBUG LOG --------*/
4970                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
4971                             {
4972                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
4973                                                         "handle_sorry_enable(const boost::thread::id thread_id) : SWITCH_FLAG_ON. thread id : %d.");
4974                                 formatter % boost::this_thread::get_id();
4975                                 putLogDebug(600133, formatter.str(), __FILE__, __LINE__ );
4976                             }
4977                             /*------DEBUG LOG END------*/
4978
4979                             //set return status
4980                             status = SORRYSERVER_DISCONNECT;
4981                         }
4982                         //data state is HTTP_BODY or UNKNOWN
4983                         else
4984                         {
4985                             //set return status
4986                             status = session_data_ptr->last_status;
4987                         }
4988                     }
4989                     //sorry flag is off
4990                     else
4991                     {
4992                         //data state is HTTP_START or HTTP_HEADER
4993                         if (session_data_ptr->data_state == HTTP_START
4994                                 || session_data_ptr->data_state ==HTTP_HEADER)
4995                         {
4996                             //set switch flag on
4997                             session_data_ptr->switch_flag = SWITCH_FLAG_ON;
4998
4999                             /*-------- DEBUG LOG --------*/
5000                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5001                             {
5002                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5003                                                         "handle_sorry_enable(const boost::thread::id thread_id) : SWITCH_FLAG_ON. thread id : %d.");
5004                                 formatter % boost::this_thread::get_id();
5005                                 putLogDebug(600134, formatter.str(), __FILE__, __LINE__ );
5006                             }
5007                             /*------DEBUG LOG END------*/
5008                         }
5009                         //data state is HTTP_BODY or UNKNOWN
5010                         else
5011                         {
5012                             //set ebd flag on
5013                             session_data_ptr->end_flag = END_FLAG_ON;
5014
5015                             /*-------- DEBUG LOG --------*/
5016                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5017                             {
5018                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5019                                                         "handle_sorry_enable(const boost::thread::id thread_id) : END_FLAG_ON. thread id : %d.");
5020                                 formatter % boost::this_thread::get_id();
5021                                 putLogDebug(600135, formatter.str(), __FILE__, __LINE__ );
5022                             }
5023                             /*------DEBUG LOG END------*/
5024                         }
5025
5026                         //set return status
5027                         status = REALSERVER_DISCONNECT;
5028                     }
5029                 }
5030             }
5031             //down thread
5032             else
5033             {
5034                 //sorry flag is on
5035                 if (session_data_ptr->sorry_flag == SORRY_FLAG_ON)
5036                 {
5037                     //set return status
5038                     status = session_data_ptr->last_status;
5039                 }
5040                 //sorry flag is off
5041                 else
5042                 {
5043                     //data state is HTTP_START and data size is 0
5044                     if (session_data_ptr->data_state == HTTP_START
5045                             && session_data_ptr->data_length == 0)
5046                     {
5047                         //set return status
5048                         status = SORRYSERVER_RECV;
5049                     }
5050                     //data state is HTTP_START and data size > 0
5051                     else if (session_data_ptr->data_state == HTTP_START
5052                              && session_data_ptr->data_length > 0)
5053                     {
5054                         //set return status
5055                         status = REALSERVER_DISCONNECT;
5056                     }
5057                     //data state is HTTP_HEADER or HTTP_BODY
5058                     else if (session_data_ptr->data_state == HTTP_HEADER
5059                              || session_data_ptr->data_state == HTTP_BODY)
5060                     {
5061                         //set switch flag on
5062                         session_data_ptr->switch_flag = SWITCH_FLAG_ON;
5063
5064                         /*-------- DEBUG LOG --------*/
5065                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5066                         {
5067                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5068                                                     "handle_sorry_enable(const boost::thread::id thread_id) : SWITCH_FLAG_ON. thread id : %d.");
5069                             formatter % boost::this_thread::get_id();
5070                             putLogDebug(600136, formatter.str(), __FILE__, __LINE__ );
5071                         }
5072                         /*------DEBUG LOG END------*/
5073
5074                         //set return status
5075                         status = session_data_ptr->last_status;
5076                     }
5077                     //other
5078                     else
5079                     {
5080                         //set return status
5081                         status = REALSERVER_DISCONNECT;
5082                     }
5083                 }
5084             }
5085
5086             //set sorry flag on
5087             session_data_ptr->sorry_flag = SORRY_FLAG_ON;
5088
5089             /*-------- DEBUG LOG --------*/
5090             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5091             {
5092                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5093                                         "handle_sorry_enable(const boost::thread::id thread_id) : SORRY_FLAG_ON. thread id : %d.");
5094                 formatter % boost::this_thread::get_id();
5095                 putLogDebug(600137, formatter.str(), __FILE__, __LINE__ );
5096             }
5097             /*------DEBUG LOG END------*/
5098
5099             //set last status
5100             session_data_ptr->last_status = status;
5101
5102         }
5103         catch (int e)
5104         {
5105             /*-------- DEBUG LOG --------*/
5106             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5107             {
5108                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5109                                         "handle_sorry_enable() : catch exception e = %d. thread id : %d.");
5110                 formatter % e % boost::this_thread::get_id();
5111                 putLogDebug(600138, formatter.str(), __FILE__, __LINE__ );
5112             }
5113             /*------DEBUG LOG END------*/
5114
5115             //set return status
5116             status = FINALIZE;
5117         }
5118         catch (std::exception& ex)
5119         {
5120             std::cerr << "protocol_module_ip::handle_sorry_enable() : exception : error = " << ex.what() << "." << std::endl;
5121             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5122                                     "handle_sorry_enable() : exception : error = %s. thread id : %d.");
5123             formatter % ex.what() % boost::this_thread::get_id();
5124             putLogError(600089, formatter.str(), __FILE__, __LINE__ );
5125
5126             //set return status
5127             status = FINALIZE;
5128         }
5129         catch (...)
5130         {
5131             std::cerr << "protocol_module_ip::handle_sorry_enable() : Unknown exception." << std::endl;
5132             boost::format formatter("function : protocol_module_base::EVENT_TAG "
5133                                     "protocol_module_ip::handle_sorry_enable() : "
5134                                     "Unknown exception. thread id : %d.");
5135             formatter % boost::this_thread::get_id();
5136             putLogError(600090, formatter.str(), __FILE__, __LINE__ );
5137
5138             //set return status
5139             status = FINALIZE;
5140         }
5141
5142         /*-------- DEBUG LOG --------*/
5143         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5144         {
5145             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5146                                     "handle_sorry_enable(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
5147             formatter % status % boost::this_thread::get_id();
5148             putLogDebug(600139, formatter.str(), __FILE__, __LINE__ );
5149         }
5150         /*------DEBUG LOG END------*/
5151
5152         return status;
5153     }
5154
5155     //! call from sorry mode disable. use upstream thread and downstream thread.
5156     //! @param[in]    upstream and downstream thread id( check! one thread one event )
5157     //! @return     session use EVENT mode
5158     protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorry_disable(
5159         const boost::thread::id thread_id)
5160     {
5161         /*-------- DEBUG LOG --------*/
5162         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5163         {
5164             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5165                                     "handle_sorry_disable(const boost::thread::id thread_id) : thread_id = %d.");
5166             formatter % boost::this_thread::get_id();
5167             putLogDebug(600140, formatter.str(), __FILE__, __LINE__ );
5168         }
5169         /*------DEBUG LOG END------*/
5170
5171         EVENT_TAG status = FINALIZE;
5172         thread_data_ptr session_data_ptr;
5173
5174         try
5175         {
5176             {
5177                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
5178
5179                 session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
5180                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
5181                 {
5182                     boost::format formatter("Invalid thread id. thread id : %d.");
5183                     formatter % boost::this_thread::get_id();
5184                     putLogError(600091, formatter.str(), __FILE__, __LINE__ );
5185                     throw -1;
5186                 }
5187
5188                 session_data_ptr = session_thread_it->second;
5189             }
5190
5191             //up thread
5192             if (session_data_ptr->thread_division == THREAD_DIVISION_UP_STREAM)
5193             {
5194                 //accept_end_flag is off
5195                 if (session_data_ptr->accept_end_flag == ACCEPT_END_FLAG_OFF)
5196                 {
5197                     //set return status
5198                     status =ACCEPT;
5199                 }
5200                 //accept_end_flag is on
5201                 else
5202                 {
5203                     //sorry flag is on
5204                     if (session_data_ptr->sorry_flag == SORRY_FLAG_ON)
5205                     {
5206                         ///data state is HTTP_START or HTTP_HEADER
5207                         if (session_data_ptr->data_state == HTTP_START
5208                                 || session_data_ptr->data_state ==HTTP_HEADER)
5209                         {
5210                             //set switch flag on
5211                             session_data_ptr->switch_flag = SWITCH_FLAG_ON;
5212
5213                             /*-------- DEBUG LOG --------*/
5214                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5215                             {
5216                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5217                                                         "handle_sorry_disable(const boost::thread::id thread_id) : SWITCH_FLAG_ON. thread id : %d.");
5218                                 formatter % boost::this_thread::get_id();
5219                                 putLogDebug(600141, formatter.str(), __FILE__, __LINE__ );
5220                             }
5221                             /*------DEBUG LOG END------*/
5222
5223                         }
5224                         //data state is HTTP_BODY or UNKNOWN
5225                         else
5226                         {
5227                             //set end flag on
5228                             session_data_ptr->end_flag = END_FLAG_ON;
5229
5230                             /*-------- DEBUG LOG --------*/
5231                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5232                             {
5233                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5234                                                         "handle_sorry_disable(const boost::thread::id thread_id) : END_FLAG_ON. thread id : %d.");
5235                                 formatter % boost::this_thread::get_id();
5236                                 putLogDebug(600142, formatter.str(), __FILE__, __LINE__ );
5237                             }
5238                             /*------DEBUG LOG END------*/
5239                         }
5240
5241                         //set return status
5242                         status = SORRYSERVER_DISCONNECT;
5243
5244                     }
5245                     //sorry flag is off
5246                     else
5247                     {
5248                         //data state is HTTP_START or HTTP_HEADER
5249                         if (session_data_ptr->data_state == HTTP_START
5250                                 || session_data_ptr->data_state ==HTTP_HEADER)
5251                         {
5252                             //set switch flag on
5253                             session_data_ptr->switch_flag = SWITCH_FLAG_ON;
5254
5255                             /*-------- DEBUG LOG --------*/
5256                             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5257                             {
5258                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5259                                                         "handle_sorry_disable(const boost::thread::id thread_id) : SWITCH_FLAG_ON. thread id : %d.");
5260                                 formatter % boost::this_thread::get_id();
5261                                 putLogDebug(600143, formatter.str(), __FILE__, __LINE__ );
5262                             }
5263                             /*------DEBUG LOG END------*/
5264
5265                             //set return status
5266                             status = REALSERVER_DISCONNECT;
5267                         }
5268                         //data state is HTTP_BODY or UNKNOWN
5269                         else
5270                         {
5271                             //set return status
5272                             status = session_data_ptr->last_status;
5273                         }
5274                     }
5275                 }
5276             }
5277             //down thread
5278             else
5279             {
5280                 //sorry flag is off
5281                 if (session_data_ptr->sorry_flag == SORRY_FLAG_OFF)
5282                 {
5283                     //set return status
5284                     status = session_data_ptr->last_status;
5285                 }
5286                 //sorry flag is on
5287                 else
5288                 {
5289                     //data state is HTTP_START and data size is 0
5290                     if (session_data_ptr->data_state == HTTP_START
5291                             && session_data_ptr->data_length == 0)
5292                     {
5293                         //set return status
5294                         status = REALSERVER_RECV;
5295                     }
5296                     //data state is HTTP_START and data size > 0
5297                     else if (session_data_ptr->data_state == HTTP_START
5298                              && session_data_ptr->data_length > 0)
5299                     {
5300                         //set return status
5301                         status = SORRYSERVER_DISCONNECT;
5302                     }
5303                     //data state is HTTP_HEADER or HTTP_BODY
5304                     else if (session_data_ptr->data_state == HTTP_HEADER
5305                              || session_data_ptr->data_state == HTTP_BODY)
5306                     {
5307                         //set switch flag on
5308                         session_data_ptr->switch_flag = SWITCH_FLAG_ON;
5309
5310                         /*-------- DEBUG LOG --------*/
5311                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5312                         {
5313                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5314                                                     "handle_sorry_disable(const boost::thread::id thread_id) : SWITCH_FLAG_ON. thread id : %d.");
5315                             formatter % boost::this_thread::get_id();
5316                             putLogDebug(600144, formatter.str(), __FILE__, __LINE__ );
5317                         }
5318                         /*------DEBUG LOG END------*/
5319
5320                         //set return status
5321                         status = session_data_ptr->last_status;
5322                     }
5323                     //other
5324                     else
5325                     {
5326                         //set return status
5327                         status = SORRYSERVER_DISCONNECT;
5328                     }
5329                 }
5330             }
5331
5332             //set sorry flag off
5333             session_data_ptr->sorry_flag = SORRY_FLAG_OFF;
5334
5335             /*-------- DEBUG LOG --------*/
5336             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5337             {
5338                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5339                                         "handle_sorry_disable(const boost::thread::id thread_id) : SORRY_FLAG_OFF. thread id : %d.");
5340                 formatter % boost::this_thread::get_id();
5341                 putLogDebug(600145, formatter.str(), __FILE__, __LINE__ );
5342             }
5343             /*------DEBUG LOG END------*/
5344
5345             //set last status
5346             session_data_ptr->last_status = status;
5347         }
5348         catch (int e)
5349         {
5350             /*-------- DEBUG LOG --------*/
5351             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5352             {
5353                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5354                                         "handle_sorry_disable() : catch exception e = %d. thread id : %d.");
5355                 formatter % e % boost::this_thread::get_id();
5356                 putLogDebug(600146, formatter.str(), __FILE__, __LINE__ );
5357             }
5358             /*------DEBUG LOG END------*/
5359
5360             //set last status
5361             status = FINALIZE;
5362         }
5363         catch (std::exception& ex)
5364         {
5365             std::cerr << "protocol_module_ip::handle_sorry_disable() : exception : error = " << ex.what() << "." << std::endl;
5366             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5367                                     "handle_sorry_disable() : exception : error = %s. thread id : %d.");
5368             formatter % ex.what() % boost::this_thread::get_id();
5369             putLogError(600092, formatter.str(), __FILE__, __LINE__ );
5370
5371             //set last status
5372             status = FINALIZE;
5373         }
5374         catch (...)
5375         {
5376             std::cerr << "protocol_module_ip::handle_sorry_disable() : Unknown exception." << std::endl;
5377             boost::format formatter("function : protocol_module_base::EVENT_TAG "
5378                                     "protocol_module_ip::handle_sorry_disable() : "
5379                                     "Unknown exception. thread id : %d.");
5380             formatter % boost::this_thread::get_id();
5381             putLogError(600093, formatter.str(), __FILE__, __LINE__ );
5382
5383             //set last status
5384             status = FINALIZE;
5385         }
5386
5387         /*-------- DEBUG LOG --------*/
5388         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5389         {
5390             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5391                                     "handle_sorry_disable(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
5392             formatter % status % boost::this_thread::get_id();
5393             putLogDebug(600147, formatter.str(), __FILE__, __LINE__ );
5394         }
5395         /*------DEBUG LOG END------*/
5396
5397         return status;
5398     }
5399
5400     //! call from realserver disconnect. use upstream thread and downstream thread
5401     //! @param[in]    upstream and downstream thread id( check! one thread one event )
5402     //! @param[in]    disconnected realserver endpoint.
5403     //! @return     session use EVENT mode
5404     protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_disconnect(
5405         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & rs_endpoint)
5406     {
5407         /*-------- DEBUG LOG --------*/
5408         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5409         {
5410             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5411                                     "handle_realserver_disconnect(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint& rs_endpoint) : "
5412                                     "thread_id = %d, rs_endpoint = [%s]:%d.");
5413             formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
5414             putLogDebug(600148, formatter.str(), __FILE__, __LINE__ );
5415         }
5416         /*------DEBUG LOG END------*/
5417
5418         EVENT_TAG status = FINALIZE;
5419         thread_data_ptr session_data_ptr;
5420
5421         try
5422         {
5423             {
5424                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
5425
5426                 session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
5427                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
5428                 {
5429                     boost::format formatter("Invalid thread id. thread id : %d.");
5430                     formatter % boost::this_thread::get_id();
5431                     putLogError(600094, formatter.str(), __FILE__, __LINE__ );
5432                     throw -1;
5433                 }
5434
5435                 session_data_ptr = session_thread_it->second;
5436             }
5437
5438
5439             //up thread
5440             if (session_data_ptr->thread_division == THREAD_DIVISION_UP_STREAM)
5441             {
5442                 //end flag is on
5443                 if (session_data_ptr->end_flag == END_FLAG_ON)
5444                 {
5445                     //set return status
5446                     status = CLIENT_RECV;
5447                 }
5448                 //end flag is off
5449                 else
5450                 {
5451                     //switch flag is on
5452                     if (session_data_ptr->switch_flag == SWITCH_FLAG_ON)
5453                     {
5454                         //sorry flag is on
5455                         if (session_data_ptr->sorry_flag == SORRY_FLAG_ON)
5456                         {
5457                             //set return status
5458                             status = SORRYSERVER_SELECT;
5459                         }
5460                         //sorry flag is off
5461                         else
5462                         {
5463                             //set return status
5464                             status = REALSERVER_SELECT;
5465                         }
5466
5467                         //set switch flag off
5468                         session_data_ptr->switch_flag = SWITCH_FLAG_OFF;
5469
5470                         /*-------- DEBUG LOG --------*/
5471                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5472                         {
5473                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5474                                                     "handle_realserver_disconnect(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &"                         "rs_endpoint) : SWITCH_FLAG_OFF. thread id : %d.");
5475                             formatter % boost::this_thread::get_id();
5476                             putLogDebug(600149, formatter.str(), __FILE__, __LINE__ );
5477                         }
5478                         /*------DEBUG LOG END------*/
5479                     }
5480                     //switch flag is off
5481                     else
5482                     {
5483                         //set return status
5484                         status = CLIENT_RECV;
5485                     }
5486                 }
5487             }
5488             //down thread
5489             else
5490             {
5491                 //set return status
5492                 status = CLIENT_DISCONNECT;
5493             }
5494
5495             //set last status
5496             session_data_ptr->last_status = status;
5497         }
5498         catch (int e)
5499         {
5500             /*-------- DEBUG LOG --------*/
5501             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5502             {
5503                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5504                                         "handle_realserver_disconnect() : catch exception e = %d. thread id : %d.");
5505                 formatter % e % boost::this_thread::get_id();
5506                 putLogDebug(600150, formatter.str(), __FILE__, __LINE__ );
5507             }
5508             /*------DEBUG LOG END------*/
5509
5510             //set return status
5511             status = FINALIZE;
5512         }
5513         catch (std::exception& ex)
5514         {
5515             std::cerr << "protocol_module_ip::handle_realserver_disconnect() : exception : error = " << ex.what() << "." << std::endl;
5516             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5517                                     "handle_realserver_disconnect() : exception : error = %s. thread id : %d.");
5518             formatter % ex.what() % boost::this_thread::get_id();
5519             putLogError(600095, formatter.str(), __FILE__, __LINE__ );
5520
5521             //set return status
5522             status = FINALIZE;
5523         }
5524         catch (...)
5525         {
5526             std::cerr << "protocol_module_ip::handle_realserver_disconnect() : Unknown exception." << std::endl;
5527             boost::format formatter("function : protocol_module_base::EVENT_TAG "
5528                                     "protocol_module_ip::handle_realserver_disconnect() : "
5529                                     "Unknown exception. thread id : %d.");
5530             formatter % boost::this_thread::get_id();
5531             putLogError(600096, formatter.str(), __FILE__, __LINE__ );
5532
5533             //set return status
5534             status = FINALIZE;
5535         }
5536
5537         /*-------- DEBUG LOG --------*/
5538         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5539         {
5540             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5541                                     "handle_realserver_disconnect(const boost::thread::id thread_id, "
5542                                     "const boost::asio::ip::tcp::endpoint& rs_endpoint) : return_value = %d. thread id : %d.");
5543             formatter % status % boost::this_thread::get_id();
5544             putLogDebug(600151, formatter.str(), __FILE__, __LINE__ );
5545         }
5546         /*------DEBUG LOG END------*/
5547
5548         return status;
5549     }
5550
5551     //! call from sorry server disconnect. use upstraem thread and downstream thread
5552     //! @param[in]    upstream and downstream thread id( check! one thread one event )
5553     //! @param[in]    disconnect sorryserver endpoint
5554     //! @return        session use EVENT mode
5555     //! @return        session use EVENT mode
5556     protocol_module_base::EVENT_TAG protocol_module_ip::handle_sorryserver_disconnect(
5557         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & sorry_endpoint)
5558     {
5559
5560         /*-------- DEBUG LOG --------*/
5561         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5562         {
5563             boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5564                                     "handle_sorryserver_disconnect(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint& sorry_endpoint) : "
5565                                     "thread_id = %d, sorry_endpoint = [%s]:%d.");
5566             formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port();
5567             putLogDebug(600152, formatter.str(), __FILE__, __LINE__ );
5568         }
5569         /*------DEBUG LOG END------*/
5570
5571         EVENT_TAG status = FINALIZE;
5572         thread_data_ptr session_data_ptr;
5573
5574         try
5575         {
5576             {
5577                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
5578
5579                 session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
5580                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL))
5581                 {
5582                     boost::format formatter("Invalid thread id. thread id : %d.");
5583                     formatter % boost::this_thread::get_id();
5584                     putLogError(600097, formatter.str(), __FILE__, __LINE__ );
5585                     throw -1;
5586                 }
5587
5588                 session_data_ptr = session_thread_it->second;
5589             }
5590
5591             //up thread
5592             if (session_data_ptr->thread_division == THREAD_DIVISION_UP_STREAM)
5593             {
5594                 //end flag is on
5595                 if (session_data_ptr->end_flag == END_FLAG_ON)
5596                 {
5597                     //set return status
5598                     status = CLIENT_RECV;
5599                 }
5600                 //end flag is off
5601                 else
5602                 {
5603                     //switch flag is on
5604                     if (session_data_ptr->switch_flag == SWITCH_FLAG_ON)
5605                     {
5606                         //sorry flag is on
5607                         if (session_data_ptr->sorry_flag == SORRY_FLAG_ON)
5608                         {
5609                             //set return status
5610                             status = SORRYSERVER_SELECT;
5611                         }
5612                         //sorry flag is off
5613                         else
5614                         {
5615                             //set return status
5616                             status = REALSERVER_SELECT;
5617                         }
5618
5619                         //set switch flag off
5620                         session_data_ptr->switch_flag = SWITCH_FLAG_OFF;
5621
5622                         /*-------- DEBUG LOG --------*/
5623                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5624                         {
5625                             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5626                                                     "handle_sorryserver_disconnect(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &"                         "sorry_endpoint) : SWITCH_FLAG_OFF. thread id : %d.");
5627                             formatter % boost::this_thread::get_id();
5628                             putLogDebug(600153, formatter.str(), __FILE__, __LINE__ );
5629                         }
5630                         /*------DEBUG LOG END------*/
5631
5632                     }
5633                     //switch flag is off
5634                     else
5635                     {
5636                         //set return status
5637                         status = CLIENT_RECV;
5638                     }
5639                 }
5640             }
5641             //down thread
5642             else
5643             {
5644                 //set return status
5645                 status = CLIENT_DISCONNECT;
5646             }
5647
5648             //set last status
5649             session_data_ptr->last_status = status;
5650
5651         }
5652         catch (int e)
5653         {
5654             /*-------- DEBUG LOG --------*/
5655             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5656             {
5657                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5658                                         "handle_sorryserver_disconnect() : catch exception e = %d. thread id : %d.");
5659                 formatter % e % boost::this_thread::get_id();
5660                 putLogDebug(600154, formatter.str(), __FILE__, __LINE__ );
5661             }
5662             /*------DEBUG LOG END------*/
5663
5664             //set return status
5665             status = FINALIZE;
5666         }
5667
5668         catch (std::exception& ex)
5669         {
5670             std::cerr << "protocol_module_ip::handle_sorryserver_disconnect() : exception : error = " << ex.what() << "." << std::endl;
5671             boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5672                                     "handle_sorryserver_disconnect() : exception : error = %s. thread id : %d.");
5673             formatter % ex.what() % boost::this_thread::get_id();
5674             putLogError(600098, formatter.str(), __FILE__, __LINE__ );
5675
5676             //set return status
5677             status = FINALIZE;
5678         }
5679         catch (...)
5680         {
5681             std::cerr << "protocol_module_ip::handle_sorryserver_disconnect() : Unknown exception." << std::endl;
5682             boost::format formatter("function : protocol_module_base::EVENT_TAG "
5683                                     "protocol_module_ip::handle_sorryserver_disconnect() : "
5684                                     "Unknown exception. thread id : %d.");
5685             formatter % boost::this_thread::get_id();
5686             putLogError(600099, formatter.str(), __FILE__, __LINE__ );
5687
5688             //set return status
5689             status = FINALIZE;
5690         }
5691
5692         /*-------- DEBUG LOG --------*/
5693         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5694         {
5695             boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5696                                     "handle_sorryserver_disconnect(const boost::thread::id thread_id, "
5697                                     "const boost::asio::ip::tcp::endpoint& sorry_endpoint) : return_value = %d. thread id : %d.");
5698             formatter % status % boost::this_thread::get_id();
5699             putLogDebug(600155, formatter.str(), __FILE__, __LINE__ );
5700         }
5701         /*------DEBUG LOG END------*/
5702
5703         return status;
5704     }
5705
5706     //! call from realserver disconnect. use upstream thread and downstream thread.
5707     //! @param[in]    upstream and downstream thread id( check! one thread one event )
5708     //! @param[in]    disconnect realserver endpoint
5709     //! @return        session use EVENT mode.
5710     protocol_module_base::EVENT_TAG protocol_module_ip::handle_realserver_close(
5711         const boost::thread::id thread_id, const boost::asio::ip::udp::endpoint & rs_endpoint)
5712     {
5713         /*-------- DEBUG LOG --------*/
5714         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5715         {
5716             boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5717                                     "handle_realserver_close(const boost::thread::id thread_id, "
5718                                     "boost::asio::ip::udp::endpoint& rs_endpoint) : "
5719                                     "return_value = %d. thread id : %d.");
5720             formatter % STOP % boost::this_thread::get_id();
5721             putLogDebug(600156, formatter.str(), __FILE__, __LINE__ );
5722         }
5723         /*------DEBUG LOG END------*/
5724
5725         return STOP;
5726     }
5727
5728
5729     bool  protocol_module_ip::get_data_from_recvbuffer(
5730         thread_data_ptr data_ptr, const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
5731     {
5732         /*-------- DEBUG LOG --------*/
5733         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5734         {
5735             boost::format formatter("in_function : bool protocol_module_ip::get_data_from_recvbuffer("
5736                                     "thread_data_ptr data_ptr, const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5737                                     "const size_t& recvlen) : thread_id = %d.");
5738             formatter % boost::this_thread::get_id();
5739             putLogDebug(600157, formatter.str(), __FILE__, __LINE__);
5740         }
5741         /*------DEBUG LOG END------*/
5742
5743         char* tmpbuffer = NULL;
5744
5745         //pointer volidate check
5746         if (unlikely(data_ptr == NULL || data_ptr->data_buffer == NULL))
5747         {
5748             boost::format formatter("Invalid pointer. thread id : %d.");
5749             formatter % boost::this_thread::get_id();
5750             putLogError(600100, formatter.str(), __FILE__, __LINE__ );
5751
5752             /*-------- DEBUG LOG --------*/
5753             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5754             {
5755                 boost::format formatter("out_function : bool protocol_module_ip::get_data_from_recvbuffer("
5756                                         "thread_data_ptr data_ptr, const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5757                                         "const size_t& recvlen) : return_value = false. thread id : %d.");
5758                 formatter % boost::this_thread::get_id();
5759                 putLogDebug(600158, formatter.str(), __FILE__, __LINE__ );
5760             }
5761             /*------DEBUG LOG END------*/
5762
5763             return false;
5764         }
5765
5766
5767         //the new data can append to buffer directly
5768         if (data_ptr->data_buffer_size - data_ptr->data_length - data_ptr->data_offset >= recvlen)
5769         {
5770
5771             /*-------- DEBUG LOG --------*/
5772             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5773             {
5774                 std::string datadump;
5775                 dump_memory(recvbuffer.data(), recvlen, datadump);
5776                 boost::format formatter(
5777                     "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5778                     "get_data_from_recvbuffer() : before memcpy (data dump) : "
5779                     "data begin = 0, data_size = %d, data = %s");
5780                 formatter % recvlen % datadump;
5781                 putLogDebug(600159, formatter.str(), __FILE__, __LINE__ );
5782             }
5783             /*------DEBUG LOG END------*/
5784
5785             memcpy(data_ptr->data_buffer + data_ptr->data_offset + data_ptr->data_length, recvbuffer.data(), recvlen);
5786
5787             /*-------- DEBUG LOG --------*/
5788             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5789             {
5790                 std::string datadump;
5791                 dump_memory(data_ptr->data_buffer + data_ptr->data_offset + data_ptr->data_length, recvlen, datadump);
5792                 boost::format formatter(
5793                     "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5794                     "get_data_from_recvbuffer() : after memcpy (data dump) : "
5795                     "data begin = 0, data_size = %d, data = %s");
5796                 formatter % recvlen % datadump;
5797                 putLogDebug(600160, formatter.str(), __FILE__, __LINE__ );
5798             }
5799             /*------DEBUG LOG END------*/
5800
5801             data_ptr->data_length += recvlen;
5802
5803             /*-------- DEBUG LOG --------*/
5804             if (unlikely(LOG_LV_DEBUG == getloglevel()))
5805             {
5806                 boost::format formatter("out_function : bool protocol_module_ip::get_data_from_recvbuffer("
5807                                         "thread_data_ptr data_ptr, const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5808                                         "const size_t& recvlen) : return_value = true. thread id : %d.");
5809                 formatter % boost::this_thread::get_id();
5810                 putLogDebug(600161, formatter.str(), __FILE__, __LINE__ );
5811             }
5812             /*------DEBUG LOG END------*/
5813             return true;
5814         }
5815         else
5816         {
5817             //the new data can append to buffer through moving orignal data
5818             if (data_ptr->data_buffer_size - data_ptr->data_length >= recvlen)
5819             {
5820                 memmove(data_ptr->data_buffer, data_ptr->data_buffer + data_ptr->data_offset, data_ptr->data_length);
5821                 /*-------- DEBUG LOG --------*/
5822                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
5823                 {
5824                     std::string datadump;
5825                     dump_memory(recvbuffer.data(), recvlen, datadump);
5826                     boost::format formatter(
5827                         "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5828                         "get_data_from_recvbuffer() : before memcpy (data dump) : "
5829                         "data begin = 0, data_size = %d, data = %s");
5830                     formatter % recvlen % datadump;
5831                     putLogDebug(600162, formatter.str(), __FILE__, __LINE__ );
5832                 }
5833                 /*------DEBUG LOG END------*/
5834
5835                 memcpy(data_ptr->data_buffer + data_ptr->data_length, recvbuffer.data(), recvlen);
5836
5837                 /*-------- DEBUG LOG --------*/
5838                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
5839                 {
5840                     std::string datadump;
5841                     dump_memory(data_ptr->data_buffer + data_ptr->data_length, recvlen, datadump);
5842                     boost::format formatter(
5843                         "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5844                         "get_data_from_recvbuffer() : after memcpy (data dump) : "
5845                         "data begin = 0, data_size = %d, data = %s");
5846                     formatter % recvlen % datadump;
5847                     putLogDebug(600163, formatter.str(), __FILE__, __LINE__ );
5848                 }
5849                 /*------DEBUG LOG END------*/
5850
5851                 data_ptr->data_offset = 0;
5852                 data_ptr->data_length += recvlen;
5853
5854                 /*-------- DEBUG LOG --------*/
5855                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
5856                 {
5857                     boost::format formatter("out_function : bool protocol_module_ip::get_data_from_recvbuffer("
5858                                             "thread_data_ptr data_ptr, const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5859                                             "const size_t& recvlen) : return_value = true. thread id : %d.");
5860                     formatter % boost::this_thread::get_id();
5861                     putLogDebug(600164, formatter.str(), __FILE__, __LINE__ );
5862                 }
5863                 /*------DEBUG LOG END------*/
5864
5865                 return true;
5866             }
5867             else
5868             {
5869                 //not allowed to realloc new buffer who's size is larger than the max size
5870                 if (data_ptr->data_buffer_size >= MAX_IP_MODULE_BUFFER_SIZE
5871                         || data_ptr->data_buffer_size + recvlen >= MAX_IP_MODULE_BUFFER_SIZE)
5872                 {
5873                     boost::format formatter("Buffer size is bigger than Max size. thread id : %d.");
5874                     formatter % boost::this_thread::get_id();
5875                     putLogError(600101, formatter.str(), __FILE__, __LINE__ );
5876
5877                     /*-------- DEBUG LOG --------*/
5878                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5879                     {
5880                         boost::format formatter("out_function : bool protocol_module_ip::get_data_from_recvbuffer("
5881                                                 "thread_data_ptr data_ptr, const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5882                                                 "const size_t& recvlen) : return_value = false. thread id : %d.");
5883                         formatter % boost::this_thread::get_id();
5884                         putLogDebug(600165, formatter.str(), __FILE__, __LINE__ );
5885                     }
5886                     /*------DEBUG LOG END------*/
5887
5888                     return false;
5889                 }
5890                 else
5891                 {
5892                     try
5893                     {
5894                         /*-------- DEBUG LOG --------*/
5895                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5896                         {
5897                             boost::format formatter("new : address = &(%d), size = %lu.");
5898                             formatter % static_cast<void*>(tmpbuffer) % MAX_IP_MODULE_BUFFER_SIZE;
5899                             putLogDebug(600166, formatter.str(), __FILE__, __LINE__);
5900                         }
5901                         /*------DEBUG LOG END------*/
5902
5903                         //alloc a new buffer who's size is max
5904                         tmpbuffer = new char[MAX_IP_MODULE_BUFFER_SIZE];
5905                     }
5906                     catch (const std::bad_alloc&) //memory alloc fail
5907                     {
5908                         std::cerr << "protocol_module_ip::get_data_from_recvbuffer() : exception : Could not allocate memory." << std::endl;
5909                         boost::format formatter("Could not allocate memory. thread id : %d.");
5910                         formatter % boost::this_thread::get_id();
5911                         putLogError(600102, formatter.str(), __FILE__, __LINE__);
5912
5913                         /*-------- DEBUG LOG --------*/
5914                         if (unlikely(LOG_LV_DEBUG == getloglevel()))
5915                         {
5916                             boost::format formatter("out_function : bool protocol_module_ip::get_data_from_recvbuffer("
5917                                                     "thread_data_ptr data_ptr, const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5918                                                     "const size_t& recvlen) : return_value = false. thread id : %d.");
5919                             formatter % boost::this_thread::get_id();
5920                             putLogDebug(600167, formatter.str(), __FILE__, __LINE__ );
5921                         }
5922                         /*------DEBUG LOG END------*/
5923
5924                         return false;
5925                     }
5926
5927                     memset(tmpbuffer, 0, MAX_IP_MODULE_BUFFER_SIZE);
5928
5929                     /*-------- DEBUG LOG --------*/
5930                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5931                     {
5932                         std::string datadump;
5933                         dump_memory(data_ptr->data_buffer+data_ptr->data_offset, data_ptr->data_length, datadump);
5934                         boost::format formatter(
5935                             "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5936                             "get_data_from_recvbuffer() : before memcpy (data dump) : "
5937                             "data begin = 0, data_size = %d, data = %s");
5938                         formatter % data_ptr->data_length % datadump;
5939                         putLogDebug(600168, formatter.str(), __FILE__, __LINE__ );
5940                     }
5941                     /*------DEBUG LOG END------*/
5942
5943                     //copy old data to new buffer
5944                     memcpy(tmpbuffer, data_ptr->data_buffer+data_ptr->data_offset, data_ptr->data_length);
5945
5946                     /*-------- DEBUG LOG --------*/
5947                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5948                     {
5949                         std::string datadump;
5950                         dump_memory(tmpbuffer, data_ptr->data_length, datadump);
5951                         boost::format formatter(
5952                             "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5953                             "get_data_from_recvbuffer() : after memcpy (data dump) : "
5954                             "data begin = 0, data_size = %d, data = %s");
5955                         formatter % data_ptr->data_length % datadump;
5956                         putLogDebug(600169, formatter.str(), __FILE__, __LINE__ );
5957                     }
5958                     /*------DEBUG LOG END------*/
5959
5960                     /*-------- DEBUG LOG --------*/
5961                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5962                     {
5963                         boost::format formatter("delete : address = &(%d).");
5964                         formatter % static_cast<void*>(data_ptr->data_buffer);
5965                         putLogDebug(600170, formatter.str(), __FILE__, __LINE__);
5966                     }
5967                     /*------DEBUG LOG END------*/
5968
5969                     //release old memory
5970                     delete [] data_ptr->data_buffer;
5971                     data_ptr->data_buffer = tmpbuffer;
5972                     data_ptr->data_offset = 0;
5973
5974                     /*-------- DEBUG LOG --------*/
5975                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5976                     {
5977                         std::string datadump;
5978                         dump_memory(recvbuffer.data(), recvlen, datadump);
5979                         boost::format formatter(
5980                             "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5981                             "get_data_from_recvbuffer() : before memcpy (data dump) : "
5982                             "data begin = 0, data_size = %d, data = %s");
5983                         formatter % recvlen % datadump;
5984                         putLogDebug(600171, formatter.str(), __FILE__, __LINE__ );
5985                     }
5986                     /*------DEBUG LOG END------*/
5987
5988                     //append new data
5989                     memcpy(data_ptr->data_buffer+data_ptr->data_length, recvbuffer.data(), recvlen);
5990
5991                     /*-------- DEBUG LOG --------*/
5992                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
5993                     {
5994                         std::string datadump;
5995                         dump_memory(data_ptr->data_buffer+data_ptr->data_length, recvlen, datadump);
5996                         boost::format formatter(
5997                             "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
5998                             "get_data_from_recvbuffer() : after memcpy (data dump) : "
5999                             "data begin = 0, data_size = %d, data = %s");
6000                         formatter % recvlen % datadump;
6001                         putLogDebug(600172, formatter.str(), __FILE__, __LINE__ );
6002                     }
6003                     /*------DEBUG LOG END------*/
6004
6005                     data_ptr->data_length += recvlen;
6006                     data_ptr->data_buffer_size = MAX_IP_MODULE_BUFFER_SIZE ;
6007
6008                     /*-------- DEBUG LOG --------*/
6009                     if (unlikely(LOG_LV_DEBUG == getloglevel()))
6010                     {
6011                         boost::format formatter("out_function : bool protocol_module_ip::get_data_from_recvbuffer("
6012                                                 "thread_data_ptr data_ptr, const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
6013                                                 "const size_t& recvlen) : return_value = true. thread id : %d.");
6014                         formatter % boost::this_thread::get_id();
6015                         putLogDebug(600173, formatter.str(), __FILE__, __LINE__ );
6016                     }
6017                     /*------DEBUG LOG END------*/
6018
6019                     return true;
6020
6021                 }
6022             }
6023         }
6024     }
6025
6026     bool protocol_module_ip::put_data_into_sendbuffer(
6027         thread_data_ptr data_ptr, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)
6028     {
6029         /*-------- DEBUG LOG --------*/
6030         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6031         {
6032             boost::format formatter("in_function : bool protocol_module_ip::put_data_to_sendbuffer("
6033                                     "thread_data_ptr data_ptr, boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
6034                                     "size_t& datalen) : thread_id = %d.");
6035             formatter % boost::this_thread::get_id();
6036             putLogDebug(600174, formatter.str(), __FILE__, __LINE__);
6037         }
6038         /*------DEBUG LOG END------*/
6039
6040         size_t sendbuffer_rest_size = 0;
6041         size_t new_offset        = 0;
6042
6043         if (unlikely(data_ptr == NULL || data_ptr->data_buffer == NULL || data_ptr->buffer_sequence.empty()))
6044         {
6045             boost::format formatter("Invalid pointer. thread id : %d.");
6046             formatter % boost::this_thread::get_id();
6047             putLogError(600103, formatter.str(), __FILE__, __LINE__ );
6048
6049             /*-------- DEBUG LOG --------*/
6050             if (unlikely(LOG_LV_DEBUG == getloglevel()))
6051             {
6052                 boost::format formatter("out_function : bool protocol_module_ip::put_data_to_sendbuffer("
6053                                         "thread_data_ptr data_ptr, boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
6054                                         "size_t& datalen) : return_value = false. thread_id = %d.");
6055                 formatter % boost::this_thread::get_id();
6056                 putLogDebug(600175, formatter.str(), __FILE__, __LINE__);
6057             }
6058             /*------DEBUG LOG END------*/
6059
6060             return false;
6061         }
6062
6063         sendbuffer_rest_size = sendbuffer.size();
6064         datalen = 0;
6065
6066         //buffer_sequence loop
6067         //copy data to send buffer until send buffer is full
6068         while (data_ptr->buffer_sequence.size() > 0)
6069         {
6070             std::pair<char*, size_t> buffer_element = data_ptr->buffer_sequence.front();
6071             //sendbuffer rest size id bigger than copy possible size
6072             if (buffer_element.second <= sendbuffer_rest_size)
6073             {
6074                 /*-------- DEBUG LOG --------*/
6075                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6076                 {
6077                     std::string datadump;
6078                     dump_memory(buffer_element.first, buffer_element.second, datadump);
6079                     boost::format formatter(
6080                         "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
6081                         "put_data_to_sendbuffer() : before memcpy (data dump) : "
6082                         "data begin = 0, data_size = %d, data = %s");
6083                     formatter % buffer_element.second % datadump;
6084                     putLogDebug(600176, formatter.str(), __FILE__, __LINE__ );
6085                 }
6086                 /*------DEBUG LOG END------*/
6087
6088                 //copy data to send buffer
6089                 memcpy(sendbuffer.c_array() + datalen, buffer_element.first, buffer_element.second);
6090
6091                 /*-------- DEBUG LOG --------*/
6092                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6093                 {
6094                     std::string datadump;
6095                     dump_memory(sendbuffer.c_array() + datalen, buffer_element.second, datadump);
6096                     boost::format formatter(
6097                         "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
6098                         "put_data_to_sendbuffer() : after memcpy (data dump) : "
6099                         "data begin = 0, data_size = %d, data = %s");
6100                     formatter % buffer_element.second % datadump;
6101                     putLogDebug(600177, formatter.str(), __FILE__, __LINE__ );
6102                 }
6103                 /*------DEBUG LOG END------*/
6104
6105                 datalen += buffer_element.second;
6106                 sendbuffer_rest_size -= buffer_element.second;
6107
6108                 //it is the last item of the sequence
6109                 if (data_ptr->buffer_sequence.size() == 1)
6110                 {
6111                     //set data position
6112                     new_offset = buffer_element.first + buffer_element.second - data_ptr->data_buffer;
6113                     data_ptr->current_message_rest_size -= (new_offset - data_ptr->data_offset);
6114                     data_ptr->data_length -= (new_offset - data_ptr->data_offset);
6115                     data_ptr->data_offset = new_offset;
6116                 }
6117
6118
6119                 //delete the item
6120                 data_ptr->buffer_sequence.pop_front();
6121             }
6122             //sendbuffer rest size is too small
6123             else
6124             {
6125                 /*-------- DEBUG LOG --------*/
6126                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6127                 {
6128                     std::string datadump;
6129                     dump_memory(buffer_element.first, sendbuffer_rest_size, datadump);
6130                     boost::format formatter(
6131                         "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
6132                         "put_data_to_sendbuffer() : before memcpy (data dump) : "
6133                         "data begin = 0, data_size = %d, data = %s");
6134                     formatter % sendbuffer_rest_size % datadump;
6135                     putLogDebug(600178, formatter.str(), __FILE__, __LINE__ );
6136                 }
6137                 /*------DEBUG LOG END------*/
6138
6139                 //copy data to send buffer
6140                 memcpy(sendbuffer.c_array() + datalen, buffer_element.first, sendbuffer_rest_size);
6141
6142                 /*-------- DEBUG LOG --------*/
6143                 if (unlikely(LOG_LV_DEBUG == getloglevel()))
6144                 {
6145                     std::string datadump;
6146                     dump_memory(sendbuffer.c_array() + datalen, sendbuffer_rest_size, datadump);
6147                     boost::format formatter(
6148                         "function : protocol_module_base::EVENT_TAG protocol_module_ip::"
6149                         "put_data_to_sendbuffer() : after memcpy (data dump) : "
6150                         "data begin = 0, data_size = %d, data = %s");
6151                     formatter % sendbuffer_rest_size % datadump;
6152                     putLogDebug(600179, formatter.str(), __FILE__, __LINE__ );
6153                 }
6154                 /*------DEBUG LOG END------*/
6155
6156                 datalen += sendbuffer_rest_size;
6157
6158                 //set item position
6159                 buffer_element.first += sendbuffer_rest_size;
6160                 buffer_element.second -= sendbuffer_rest_size;
6161                 sendbuffer_rest_size = 0;
6162                 break;
6163             }
6164         }
6165
6166         /*-------- DEBUG LOG --------*/
6167         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6168         {
6169             boost::format formatter("out_function : bool protocol_module_ip::put_data_to_sendbuffer("
6170                                     "thread_data_ptr data_ptr, boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
6171                                     "size_t& datalen) : return_value = true. thread_id = %d.");
6172             formatter % boost::this_thread::get_id();
6173             putLogDebug(600180, formatter.str(), __FILE__, __LINE__);
6174         }
6175         /*------DEBUG LOG END------*/
6176
6177         return true;
6178     }
6179
6180
6181
6182
6183     //! call from put_data_to_buffer_with_x_forwarded_for(). get x_forwarded_for header's offset and length
6184     //! if the buffer isn't include x_forwarded_for header, create a new one
6185     //! @param[in]    buffer
6186     //! @param[in]    buffer_len
6187     //! @param[out] x_forwarded_for_insert_pos
6188     //! @param[out] x_forwarded_for_context
6189     //! @return  true:create a new x_forwarded_for header
6190     //!         false: edit old one
6191     bool protocol_module_ip::create_x_forwarded_for(const std::string& client_endpoint,
6192             const char* buffer,
6193             const size_t buffer_len,
6194             size_t& x_forwarded_for_insert_pos,
6195             std::string& x_forwarded_for_context)
6196     {
6197         /*-------- DEBUG LOG --------*/
6198         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6199         {
6200             boost::format formatter("in_function : bool protocol_module_ip::create_x_forwarded_for("
6201                                     "const std::string& client_endpoint, const char* buffer, const size_t buffer_len, "
6202                                     "size_t& x_forwarded_for_insert_pos, std::string& x_forwarded_for_context) : thread_id = %d.");
6203             formatter % boost::this_thread::get_id();
6204             putLogDebug(600181, formatter.str(), __FILE__, __LINE__);
6205         }
6206         /*------DEBUG LOG END------*/
6207
6208         bool find_ret = false;
6209         size_t x_forwarded_for_offset   = 0;
6210         size_t x_forwarded_for_len    = 0;
6211         size_t http_header_all_offset   = 0;
6212         size_t http_header_all_len    = 0;
6213         const char* X_FORWARDED_FOR    = "X-Forwarded-For";
6214
6215         //search "X-Forwared-For" header
6216         find_ret = http_utility::find_http_header_x_forwarded_for(buffer, buffer_len,
6217                    x_forwarded_for_offset, x_forwarded_for_len);
6218
6219         /*-------- DEBUG LOG --------*/
6220         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6221         {
6222             boost::format formatter("function : bool protocol_module_ip::create_x_forwarded_for() : "
6223                                     "call find_http_header_x_forwarded_for : "
6224                                     "return_value = %d. thread id : %d.");
6225             formatter % static_cast<int>(find_ret) % boost::this_thread::get_id();
6226             putLogDebug(600182, formatter.str(), __FILE__, __LINE__ );
6227         }
6228         /*------DEBUG LOG END------*/
6229
6230         //search "X-Forwared-For" result is OK
6231         if (find_ret)
6232         {
6233             //create "X-Forwared-For" string
6234             x_forwarded_for_insert_pos = x_forwarded_for_offset + x_forwarded_for_len;
6235             x_forwarded_for_context = ", ";
6236             x_forwarded_for_context += client_endpoint;
6237
6238             /*-------- DEBUG LOG --------*/
6239             if (unlikely(LOG_LV_DEBUG == getloglevel()))
6240             {
6241                 boost::format formatter("out_function : bool protocol_module_ip::create_x_forwarded_for("
6242                                         "const std::string& client_endpoint, const char* buffer, const size_t buffer_len, "
6243                                         "size_t& x_forwarded_for_insert_pos, std::string& x_forwarded_for_context) : return_value = false. thread_id = %d.");
6244                 formatter % boost::this_thread::get_id();
6245                 putLogDebug(600183, formatter.str(), __FILE__, __LINE__);
6246             }
6247             /*------DEBUG LOG END------*/
6248
6249             return false;
6250         }
6251
6252         //create a new one
6253         //because state is HTTP_HEADER, find_http_header_all must be return true
6254         http_utility::find_http_header_all(buffer, buffer_len, http_header_all_offset, http_header_all_len);
6255
6256         /*-------- DEBUG LOG --------*/
6257         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6258         {
6259             boost::format formatter("function : bool protocol_module_ip::create_x_forwarded_for() : "
6260                                     "call find_http_header_all : "
6261                                     "return_value = true. thread id : %d.");
6262             formatter % boost::this_thread::get_id();
6263             putLogDebug(600184, formatter.str(), __FILE__, __LINE__ );
6264         }
6265         /*------DEBUG LOG END------*/
6266
6267         //create "X-Forwared-For" string
6268         x_forwarded_for_insert_pos = http_header_all_offset;
6269         x_forwarded_for_context = X_FORWARDED_FOR;
6270         x_forwarded_for_context += ": ";
6271         x_forwarded_for_context += client_endpoint;
6272         x_forwarded_for_context += "\r\n";
6273
6274         /*-------- DEBUG LOG --------*/
6275         if (unlikely(LOG_LV_DEBUG == getloglevel()))
6276         {
6277             boost::format formatter("out_function : bool protocol_module_ip::create_x_forwarded_for("
6278                                     "const std::string& client_endpoint, const char* buffer, const size_t buffer_len, "
6279                                     "size_t& x_forwarded_for_insert_pos, std::string& x_forwarded_for_context) : return_value = true. thread_id = %d.");
6280             formatter % boost::this_thread::get_id();
6281             putLogDebug(600185, formatter.str(), __FILE__, __LINE__);
6282         }
6283         /*------DEBUG LOG END------*/
6284
6285         return true;
6286     }
6287
6288
6289
6290 }//namesapce l7vsd
6291
6292 extern "C" l7vs::protocol_module_base*
6293     create_module()
6294 {
6295     return dynamic_cast<l7vs::protocol_module_base*>(new l7vs::protocol_module_ip());
6296 }
6297
6298 extern "C" void
6299     destroy_module( l7vs::protocol_module_base* in )
6300 {
6301     delete in;
6302 }