OSDN Git Service

#30993: gcc4.6以降でのコンパイルエラー対処
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / module / protocol / protocol_module_sessionless.cpp
1 /*
2  * @file  protocol_module_sessionless.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 #include <boost/xpressive/xpressive.hpp>
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 "protocol_module_sessionless.h"
33 #include "utility.h"
34
35 namespace l7vs
36 {
37 #ifdef  DEBUG
38 std::string     eventtag_to_string(protocol_module_base::EVENT_TAG in)
39 {
40         return
41                 in == protocol_module_base::INITIALIZE                                ?       "INITIALIZE"
42                 : in == protocol_module_base::ACCEPT                                    ?       "ACCEPT"
43                 : in == protocol_module_base::CLIENT_RECV                               ?       "CLIENT_RECV"
44                 : in == protocol_module_base::REALSERVER_SELECT                 ?       "REALSERVER_SELECT"
45                 : in == protocol_module_base::REALSERVER_CONNECT                ?       "REALSERVER_CONNECT"
46                 : in == protocol_module_base::REALSERVER_SEND                   ?       "REALSERVER_SEND"
47                 : in == protocol_module_base::SORRYSERVER_SELECT                ?       "SORRYSERVER_SELECT"
48                 : in == protocol_module_base::SORRYSERVER_CONNECT               ?       "SORRYSERVER_CONNECT"
49                 : in == protocol_module_base::SORRYSERVER_SEND                  ?       "SORRYSERVER_SEND"
50                 : in == protocol_module_base::REALSERVER_RECV                   ?       "REALSERVER_RECV"
51                 : in == protocol_module_base::SORRYSERVER_RECV                  ?       "SORRYSERVER_RECV"
52                 : in == protocol_module_base::CLIENT_SELECT                             ?       "CLIENT_SELECT"
53                 : in == protocol_module_base::CLIENT_CONNECTION_CHECK   ?       "CLIENT_CONNECTION_CHECK"
54                 : in == protocol_module_base::CLIENT_SEND                               ?       "CLIENT_SEND"
55                 : in == protocol_module_base::CLIENT_RESPONSE_SEND              ?       "CLIENT_RESPONSE_SEND"
56                 : in == protocol_module_base::REALSERVER_DISCONNECT             ?       "REALSERVER_DISCONNECT"
57                 : in == protocol_module_base::SORRYSERVER_DISCONNECT    ?       "SORRYSERVER_DISCONNECT"
58                 : in == protocol_module_base::CLIENT_DISCONNECT                 ?       "CLIENT_DISCONNECT"
59                 : in == protocol_module_base::REALSERVER_CLOSE                  ?       "REALSERVER_CLOSE"
60                 : in == protocol_module_base::FINALIZE                                  ?       "FINALIZE"
61                 : in == protocol_module_base::STOP                                              ?       "STOP"
62                 :                                                                                                                       "NOT_FOUND"
63                 ;
64 }
65
66 std::string     session_thread_data_to_string(const boost::shared_ptr< protocol_module_sessionless::session_thread_data_sessionless > in_ptr)
67 {
68         boost::format   fmt("DATA : Thread ID[%d]\n"
69                             "           Thread_division[%d]\n"
70                             "           Pair Thread ID[%d]\n"
71                             "           end_flag[%d]\n"
72                             "           accept_end_flag[%d]\n"
73                             "           sorry_flag[%d]\n"
74                             "           sorryserver_switch_flag[%d]\n"
75                             "           realserver_switch_flag[%d]\n"
76                             "           target_endpoint[%s:%d]\n"
77                             "           client_endpoint[%s:%d]\n"
78                             "           last_status[%d]\n"
79                            );
80         fmt % in_ptr->thread_id
81         % in_ptr->thread_division
82         % in_ptr->pair_thread_id
83         % in_ptr->end_flag
84         % in_ptr->accept_end_flag
85         % in_ptr->sorry_flag
86         % in_ptr->sorryserver_switch_flag
87         % in_ptr->realserver_switch_flag
88         % in_ptr->target_endpoint.address().to_string() % in_ptr->target_endpoint.port()
89         % in_ptr->client_endpoint_tcp.address().to_string() % in_ptr->client_endpoint_tcp.port()
90         % eventtag_to_string(in_ptr->last_status);
91
92         return fmt.str();
93 }
94
95
96 #endif
97 const std::string protocol_module_sessionless::MODULE_NAME = "sessionless";
98 const int protocol_module_sessionless::THREAD_DIVISION_UP_STREAM = 0;
99 const int protocol_module_sessionless::THREAD_DIVISION_DOWN_STREAM = 1;
100
101 const int protocol_module_sessionless::END_FLAG_OFF = 0;
102 const int protocol_module_sessionless::END_FLAG_ON = 1;
103
104 const int protocol_module_sessionless::ACCEPT_END_FLAG_OFF = 0;
105 const int protocol_module_sessionless::ACCEPT_END_FLAG_ON = 1;
106
107 const int protocol_module_sessionless::SORRY_FLAG_ON = 1;
108 const int protocol_module_sessionless::SORRY_FLAG_OFF = 0;
109
110 const int protocol_module_sessionless::SORRYSERVER_SWITCH_FLAG_OFF = 0;
111 const int protocol_module_sessionless::SORRYSERVER_SWITCH_FLAG_ON = 1;
112
113 const int protocol_module_sessionless::REALSERVER_SWITCH_FLAG_OFF = 0;
114 const int protocol_module_sessionless::REALSERVER_SWITCH_FLAG_ON = 1;
115
116 const int protocol_module_sessionless::EDIT_DIVISION_NO_EDIT = 0;
117 const int protocol_module_sessionless::EDIT_DIVISION_EDIT = 1;
118
119 const int protocol_module_sessionless::FORWARDED_FOR_OFF = 0;
120 const int protocol_module_sessionless::FORWARDED_FOR_ON = 1;
121
122 const int protocol_module_sessionless::COLLECT_STATS_OFF = 0;
123 const int protocol_module_sessionless::COLLECT_STATS_ON = 1;
124
125 //! constructor
126 protocol_module_sessionless::protocol_module_sessionless() :
127         http_protocol_module_base(MODULE_NAME), forwarded_for(FORWARDED_FOR_OFF)
128 {
129         sorry_uri.assign('\0');
130         sorry_uri[0] = '/';
131 }
132 //! destructor
133 protocol_module_sessionless::~protocol_module_sessionless()
134 {
135 }
136 //! tcp protocol support check
137 //! @return tcp support is true
138 //! @return tcp not-support is false
139 bool protocol_module_sessionless::is_tcp()
140 {
141         /*-------- DEBUG LOG --------*/
142         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
143                 putLogDebug(100000, "in/out_function : bool protocol_module_sessionless::is_tcp() : "
144                             "return_value = true.", __FILE__, __LINE__);
145         }
146         /*------DEBUG LOG END------*/
147         return true;
148 }
149
150 //! udp protocol support check
151 //! @return udp support is true
152 //! @return udp not-support is false
153 bool protocol_module_sessionless::is_udp()
154 {
155         /*-------- DEBUG LOG --------*/
156         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
157                 putLogDebug(100001, "in/out_function : bool protocol_module_sessionless::is_udp() : "
158                             "return_value = false.", __FILE__, __LINE__);
159         }
160         /*------DEBUG LOG END------*/
161         return false;
162 }
163
164 //! replication interval interrupt
165 //! timer thread call this function. from virtualservice.
166 void protocol_module_sessionless::replication_interrupt()
167 {
168         /*-------- DEBUG LOG --------*/
169         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
170                 putLogDebug(100002, "in/out_function : void protocol_module_sessionless::"
171                             "replication_interrupt().", __FILE__, __LINE__);
172         }
173         /*------DEBUG LOG END------*/
174 }
175 //! initialize function. called from module control. module loaded call
176 //! @param[in]    realserver list iterator begin function object type
177 //!    @param[in]    realserver list iterator end function object type
178 //! @param[in]    realserver list iterator next function object type
179 //! @param[in]    realserver list mutex lock function object type.
180 //! @param[in]    realserver list mutex unlock function object type
181 void protocol_module_sessionless::initialize(rs_list_itr_func_type    inlist_begin,
182                 rs_list_itr_func_type    inlist_end,
183                 rs_list_itr_next_func_type    inlist_next,
184                 boost::function< void(void) >    inlist_lock,
185                 boost::function< void(void) >    inlist_unlock)
186 {
187         /*-------- DEBUG LOG --------*/
188         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
189                 putLogDebug(100003, "in_function : void protocol_module_sessionless::initialize("
190                             "rs_list_itr_func_type inlist_begin, rs_list_itr_func_type inlist_end, "
191                             "rs_list_itr_next_func_type inlist_next, boost::function< void( void ) > "
192                             "inlist_lock, boost::function< void( void ) > inlist_unlock).", __FILE__, __LINE__);
193         }
194         /*------DEBUG LOG END------*/
195
196         //RealServer list begin function
197         rs_list_begin = inlist_begin;
198         //RealServer list end function
199         rs_list_end = inlist_end;
200         //RealServer list next function
201         rs_list_next = inlist_next;
202         //RealServer list lock function
203         rs_list_lock = inlist_lock;
204         //RealServer list unlock function
205         rs_list_unlock = inlist_unlock;
206
207         /*-------- DEBUG LOG --------*/
208         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
209                 putLogDebug(100004, "out_function : void protocol_module_sessionless::initialize("
210                             "rs_list_itr_func_type inlist_begin, rs_list_itr_func_type inlist_end, "
211                             "rs_list_itr_next_func_type inlist_next, boost::function< void( void ) > "
212                             "inlist_lock, boost::function< void( void ) > inlist_unlock).", __FILE__, __LINE__);
213         }
214         /*-------- DEBUG LOG --------*/
215 }
216
217 //! finalize called from module control. module unloaded call.
218 void protocol_module_sessionless::finalize()
219 {
220         /*-------- DEBUG LOG --------*/
221         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
222                 putLogDebug(100005, "in_function : void protocol_module_sessionless::finalize().", __FILE__,
223                             __LINE__);
224         }
225         /*------DEBUG LOG END------*/
226
227         //RealServer list functions initialization
228         //RealServer list begin function
229         rs_list_begin.clear();
230         //RealServer list end function
231         rs_list_end.clear();
232         //RealServer list next function
233         rs_list_next.clear();
234         //RealServer list lock function
235         rs_list_lock.clear();
236         //RealServer list unlock function
237         rs_list_unlock.clear();
238
239         /*-------- DEBUG LOG --------*/
240         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
241                 putLogDebug(100006, "function : void protocol_module_sessionless::finalize() : "
242                             "rs_list_begin.clear(), rs_list_end.clear(), rs_list_next.clear(), rs_list_lock.clear(), rs_list_unlock.clear() end.", __FILE__,
243                             __LINE__);
244         }
245         /*------DEBUG LOG END------*/
246
247         //Replication functions initialization
248         //component memory allocate function
249         replication_pay_memory.clear();
250         //component memory lock function
251         replication_area_lock.clear();
252         //component memory unlock function
253         replication_area_unlock.clear();
254
255         /*-------- DEBUG LOG --------*/
256         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
257                 putLogDebug(100007, "function : void protocol_module_sessionless::finalize() : "
258                             "replication_pay_memory.clear(), replication_area_lock.clear(), replication_area_unlock.clear() end.", __FILE__,
259                             __LINE__);
260         }
261         /*------DEBUG LOG END------*/
262
263         //ScheduleModule's functions initialization
264         schedule_tcp.clear();
265
266         /*-------- DEBUG LOG --------*/
267         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
268                 putLogDebug(100008, "function : void protocol_module_sessionless::finalize() : "
269                             "schedule_tcp.clear(), schedule_udp.clear() end.", __FILE__,
270                             __LINE__);
271         }
272         /*------DEBUG LOG END------*/
273
274         //Module's option initialization
275         //forwarded_for
276         forwarded_for = FORWARDED_FOR_OFF;
277         //sorry-uri
278         sorry_uri.assign('\0');
279         /*-------- DEBUG LOG --------*/
280         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
281                 putLogDebug(100009, "function : void protocol_module_sessionless::finalize() : "
282                             "forwarded_for = 0, sorry_uri.assign('\\0') end.", __FILE__,
283                             __LINE__);
284         }
285         /*------DEBUG LOG END------*/
286         /*-------- DEBUG LOG --------*/
287         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
288                 putLogDebug(100010, "out_function : void protocol_module_sessionless::finalize().", __FILE__,
289                             __LINE__);
290         }
291         /*------DEBUG LOG END------*/
292
293
294         //logger functions initialization
295         //log level getting function
296         getloglevel.clear();
297         //logger(Fatal)
298         putLogFatal.clear();
299         //logger(Error)
300         putLogError.clear();
301         //logger(Warn)
302         putLogWarn.clear();
303         //logger(Info)
304         putLogInfo.clear();
305         //logger(Debug)
306         putLogDebug.clear();
307 }
308
309 //! sorry support check
310 //! @return true sorry mode is supported.
311 //! @return false sorry mode is unsupported.
312 bool protocol_module_sessionless::is_use_sorry()
313 {
314         /*-------- DEBUG LOG --------*/
315         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
316                 putLogDebug(100011, "in/out_function : bool protocol_module_sessionless::is_use_sorry() : "
317                             "return_value = true.", __FILE__, __LINE__);
318         }
319         /*------DEBUG LOG END------*/
320         return true;
321 }
322
323 //! realserver list update event
324 void protocol_module_sessionless::handle_rslist_update()
325 {
326         /*-------- DEBUG LOG --------*/
327         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
328                 putLogDebug(100012, "in/out_function : void protocol_module_sessionless::handle_rslist_update().", __FILE__,
329                             __LINE__);
330         }
331         /*------DEBUG LOG END------*/
332 }
333
334 //! module parameter check.used by l7vsadm
335 //! @param[in]    module parameter string list
336 //! @return    result.flag true is parameter is no problem.
337 //! @return result.flag false is parameter is problem.
338 protocol_module_base::check_message_result protocol_module_sessionless::check_parameter(const std::vector <
339                 std::string > & args)
340 {
341         /*-------- DEBUG LOG --------*/
342         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
343                 boost::format formatter("in_function : protocol_module_base::check_message_result "
344                                         "protocol_module_sessionless::check_parameter("
345                                         "const std::vector<std::string>& args) : args = %s.");
346                 std::string argsdump;
347                 for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it) {
348                         argsdump += *it;
349                         argsdump += " ";
350                 }
351                 formatter % argsdump;
352                 putLogDebug(100013, formatter.str(), __FILE__, __LINE__);
353         }
354         /*------DEBUG LOG END------*/
355         using namespace boost::xpressive;
356         //set check result true
357         check_message_result check_result;
358         check_result.flag = true;
359         bool sorryuri_checked = false;
360         bool stats_checked = false;
361
362         // cf RFC 2396 (A. Collected BNF for URI)
363         sregex    sorry_uri_regex
364         =    +('/' >>
365                *(
366                        alpha | digit |
367                        (set = '-', '_', '.', '!', '~', '*', '\'', '(', ')') |
368                        '%' >> repeat<2>(xdigit) |
369                        (set = ':', '@', '&', '=', '+', '$', ',')
370                )
371                >>
372                *(';' >>
373                  *(
374                          alpha | digit |
375                          (set = '-', '_', '.', '!', '~', '*', '\'', '(', ')') | // mark
376                          '%' >> repeat<2>(xdigit) | // escaped
377                          (set = ':', '@', '&', '=', '+', '$', ',')
378                  ) // pchar
379                 ) // param
380               ) // segment
381              >>
382              !('?' >>
383                *(
384                        (set = ';', '/', '?', ':', '@', '&', '=', '+', '$', ',') | //reserved
385                        alpha | digit |
386                        (set = '-', '_', '.', '!', '~', '*', '\'', '(', ')') | // mark
387                        '%' >> repeat<2>(xdigit) // escaped
388                ) // uric
389               ) // query
390              >>
391              !('#' >>
392                *(
393                        (set = ';', '/', '?', ':', '@', '&', '=', '+', '$', ',') | //reserved
394                        alpha | digit |
395                        (set = '-', '_', '.', '!', '~', '*', '\'', '(', ')') | // mark
396                        '%' >> repeat<2>(xdigit) // escaped
397                ) // uric
398               ); // fragment
399
400         typedef std::vector<std::string>::const_iterator vec_str_it;
401
402         try {
403                 vec_str_it it = args.begin();
404                 vec_str_it it_end = args.end();
405                 //loop option strings
406                 for (; it != it_end; ++it) {
407                         //option string = "-S"
408                         if (*it == "-S" || *it == "--sorry-uri") {
409                                 //set sorryURI flag OFF
410                                 if (!sorryuri_checked) {
411                                         //next item exist
412                                         if (++it != it_end) {
413                                                 if (!it->empty() && (it->substr(0, 1) == "-" || it->substr(0, 2) == "--")) {
414                                                         //set check result flag false
415                                                         check_result.flag = false;
416                                                         //set check result message
417                                                         check_result.message = "You have to set option value '-S/--sorry-uri'.";
418                                                         putLogError(100000, check_result.message, __FILE__, __LINE__);
419                                                         //loop break;
420                                                         break;
421                                                 }
422                                                 //next option string's length > 127
423                                                 if (it->size() > MAX_OPTION_SIZE - 1) {
424                                                         std::ostringstream ostr;
425                                                         ostr << "'-S/--sorry-uri' option value '" << *it << "' is too long.";
426
427                                                         //set check result flag false
428                                                         check_result.flag = false;
429                                                         //set check result message
430                                                         check_result.message = ostr.str();
431
432                                                         putLogError(100001, check_result.message, __FILE__, __LINE__);
433                                                         //loop break;
434                                                         break;
435                                                 }
436                                                 //next option string's length <= 127
437                                                 else {
438                                                         //regex check
439                                                         if (regex_match(*it, sorry_uri_regex)) {
440                                                                 //check OK
441                                                                 //set sorryURI flag ON
442                                                                 sorryuri_checked = true;
443                                                         }
444                                                         //check NG
445                                                         else {
446                                                                 std::ostringstream ostr;
447                                                                 ostr << "'-S/--sorry-uri' option value '" << *it << "' is not a valid URI.";
448
449                                                                 //set check result flag false
450                                                                 check_result.flag = false;
451                                                                 //set check result message
452                                                                 check_result.message = ostr.str();
453                                                                 putLogError(100002, check_result.message, __FILE__, __LINE__);
454                                                                 //loop break
455                                                                 break;
456                                                         }
457                                                 }
458                                         }
459                                         //next item is not exist
460                                         else {
461                                                 //set check flag false
462                                                 check_result.flag = false;
463                                                 //set check result message
464                                                 check_result.message = "You have to set option value '-S/--sorry-uri'.";
465                                                 putLogError(100003, check_result.message, __FILE__,
466                                                             __LINE__);
467                                                 //loop break
468                                                 break;
469                                         }
470                                 }
471                                 //sorryURI flag = ON
472                                 else {
473                                         //set check result flag false
474                                         check_result.flag = false;
475                                         //set check result message
476                                         check_result.message = "Cannot set multiple option '-S/--sorry-uri'.";
477                                         putLogError(100004, check_result.message, __FILE__,
478                                                     __LINE__);
479                                         //loop break
480                                         break;
481                                 }
482                         }
483                         //option string = "-c/--statistic"
484                         else if (*it == "-c" || *it == "--statistic") {
485                                 //statistic flag is OFF
486                                 if (!stats_checked) {
487                                         //next item exist
488                                         if (++it != it_end) {
489                                                 //collect statistic flag must be 0 or 1
490                                                 if (*it == "0" || *it == "1") {
491                                                         //check OK
492                                                         //set statistic flag ON
493                                                         stats_checked = true;
494                                                 } else {
495                                                         std::ostringstream ostr;
496                                                         ostr << "'-c/--statistic' option value '" << *it << "' is not a valid value.";
497
498                                                         //set check result flag false
499                                                         check_result.flag = false;
500                                                         //set check result message
501                                                         check_result.message = ostr.str();
502                                                         putLogError(100128, check_result.message, __FILE__, __LINE__);
503                                                         //loop break
504                                                         break;
505                                                 }
506                                         }
507                                         //next item is not exist
508                                         else {
509                                                 //set check flag false
510                                                 check_result.flag = false;
511                                                 //set check result message
512                                                 check_result.message = "You have to set option value '-c/--statistic'.";
513                                                 putLogError(100129, check_result.message, __FILE__,
514                                                             __LINE__);
515                                                 //loop break
516                                                 break;
517                                         }
518                                 }
519                                 //statistic flag is ON
520                                 else {
521                                         //set check result flag false
522                                         check_result.flag = false;
523                                         //set check result message
524                                         check_result.message = "Cannot set multiple option '-c/--statistic'.";
525                                         putLogError(100130, check_result.message, __FILE__,
526                                                     __LINE__);
527                                         //loop break
528                                         break;
529                                 }
530                         }
531                         //other option string
532                         else {
533                                 //set check result flag false
534                                 check_result.flag = false;
535                                 //set check result message
536                                 check_result.message = "Option error.";
537                                 putLogError(100005, check_result.message, __FILE__, __LINE__);
538                                 //loop break
539                                 break;
540                         }
541                 }
542         } catch (const std::exception &ex) {
543                 check_result.flag = false;
544                 std::cerr << "protocol_module_sessionless::check_parameter() : exception : error = " << ex.what() << "." << std::endl;
545                 boost::format formatter("function : protocol_module_base::check_message_result "
546                                         "protocol_module_sessionless::check_parameter() exception : "
547                                         "error = %s.");
548                 formatter % ex.what();
549                 putLogError(100006, formatter.str(), __FILE__, __LINE__);
550         } catch (...) {
551                 check_result.flag = false;
552                 std::cerr << "protocol_module_sessionless::check_parameter() : Unknown exception." << std::endl;
553                 putLogError(100007, "function : protocol_module_base::check_message_result "
554                             "protocol_module_sessionless::check_parameter() : "
555                             "Unknown exception.", __FILE__, __LINE__);
556         }
557
558         /*-------- DEBUG LOG --------*/
559         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
560                 boost::format formatter("out_function : protocol_module_base::check_message_result "
561                                         "protocol_module_sessionless::check_parameter("
562                                         "const std::vector<std::string>& args) : return_value = ("
563                                         "check_message_result.flag = %d, check_message_result.message = %s).");
564                 formatter % check_result.flag % check_result.message;
565                 putLogDebug(100014, formatter.str(), __FILE__, __LINE__);
566         }
567         /*------DEBUG LOG END------*/
568         return check_result;
569 }
570
571 //! parameter set
572 //! @param[in] module parameter string list
573 //! @return    result.flag true is parameter is no problem.
574 //! @return result.flag false is parameter is problem.
575 protocol_module_base::check_message_result protocol_module_sessionless::set_parameter(const std::vector <
576                 std::string > & args)
577 {
578         /*-------- DEBUG LOG --------*/
579         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
580                 boost::format formatter("in_function : protocol_module_base::check_message_result "
581                                         "protocol_module_sessionless::set_parameter("
582                                         "const std::vector<std::string>& args) : args = %s.");
583                 std::string argsdump;
584                 for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it) {
585                         argsdump += *it;
586                         argsdump += " ";
587                 }
588                 formatter % argsdump;
589                 putLogDebug(100015, formatter.str(), __FILE__, __LINE__);
590         }
591         /*------DEBUG LOG END------*/
592         using namespace boost::xpressive;
593         //set check result flag true
594         check_message_result check_result;
595         check_result.flag = true;
596         bool forward_checked = false;
597         bool sorryuri_checked = false;
598         bool stats_checked = false;
599
600         // cf RFC 2396 (A. Collected BNF for URI)
601         sregex    sorry_uri_regex
602         =    +('/' >>
603                *(
604                        alpha | digit |
605                        (set = '-', '_', '.', '!', '~', '*', '\'', '(', ')') |
606                        '%' >> repeat<2>(xdigit) |
607                        (set = ':', '@', '&', '=', '+', '$', ',')
608                )
609                >>
610                *(';' >>
611                  *(
612                          alpha | digit |
613                          (set = '-', '_', '.', '!', '~', '*', '\'', '(', ')') | // mark
614                          '%' >> repeat<2>(xdigit) | // escaped
615                          (set = ':', '@', '&', '=', '+', '$', ',')
616                  ) // pchar
617                 ) // param
618               ) // segment
619              >>
620              !('?' >>
621                *(
622                        (set = ';', '/', '?', ':', '@', '&', '=', '+', '$', ',') | //reserved
623                        alpha | digit |
624                        (set = '-', '_', '.', '!', '~', '*', '\'', '(', ')') | // mark
625                        '%' >> repeat<2>(xdigit) // escaped
626                ) // uric
627               ) // query
628              >>
629              !('#' >>
630                *(
631                        (set = ';', '/', '?', ':', '@', '&', '=', '+', '$', ',') | //reserved
632                        alpha | digit |
633                        (set = '-', '_', '.', '!', '~', '*', '\'', '(', ')') | // mark
634                        '%' >> repeat<2>(xdigit) // escaped
635                ) // uric
636               ); // fragment
637
638         typedef std::vector<std::string>::const_iterator vec_str_it;
639
640         //set forwarded flag true
641         forwarded_for = FORWARDED_FOR_ON;
642
643         try {
644                 vec_str_it it = args.begin();
645                 vec_str_it it_end = args.end();
646                 for (; it != it_end; ++it) {
647                         //option string = "-F"
648                         if (*it == "-F" || *it == "--forwarded-for") {
649                                 //set forwarded flag ON
650                                 forward_checked = true;
651                         }
652                         //option string  = "-S"
653                         else if (*it == "-S" || *it == "--sorry-uri") {
654                                 //sorryURI flag = OFF
655                                 if (!sorryuri_checked) {
656                                         //next item exist
657                                         if (++it != it_end) {
658                                                 if (!it->empty() && (it->substr(0, 1) == "-" || it->substr(0, 2) == "--")) {
659                                                         //set check result flag false
660                                                         check_result.flag = false;
661                                                         //set check result message
662                                                         check_result.message = "You have to set option value '-S/--sorry-uri'.";
663                                                         putLogError(100008, check_result.message, __FILE__,
664                                                                     __LINE__);
665                                                         //loop break
666                                                         break;
667                                                 }
668                                                 //next option string's length > 127
669                                                 if (it->size() > MAX_OPTION_SIZE - 1) {
670                                                         std::ostringstream ostr;
671                                                         ostr << "'-S/--sorry-uri' option value '" << *it << "' is too long.";
672
673                                                         //set check result flag false
674                                                         check_result.flag = false;
675                                                         //set check result message
676                                                         check_result.message = ostr.str();
677                                                         putLogError(100009, check_result.message, __FILE__,
678                                                                     __LINE__);
679                                                         //loop break
680                                                         break;
681                                                 }
682                                                 //next option string's length <= 127
683                                                 else {
684                                                         //regex check
685                                                         //check OK
686                                                         if (regex_match(*it, sorry_uri_regex)) {
687                                                                 sorryuri_checked = true;
688                                                                 strcpy(sorry_uri.data(), it->c_str());
689                                                         }
690                                                         //check NG
691                                                         else {
692                                                                 std::ostringstream ostr;
693                                                                 ostr << "'-S/--sorry-uri' option value '" << *it << "' is not a valid URI.";
694
695                                                                 //set check result flag false
696                                                                 check_result.flag = false;
697                                                                 //set check result message
698                                                                 check_result.message = ostr.str();
699                                                                 putLogError(100010, check_result.message, __FILE__,
700                                                                             __LINE__);
701
702                                                                 break;
703                                                         }
704                                                 }
705                                         }
706                                         //next item not exist
707                                         else {
708                                                 //set check result flag false
709                                                 check_result.flag = false;
710                                                 //set check result message
711                                                 check_result.message = "You have to set option value '-S/--sorry-uri'.";
712                                                 putLogError(100011, check_result.message, __FILE__,
713                                                             __LINE__);
714
715                                                 break;
716                                         }
717                                 }
718                                 //sorryURI flag = ON
719                                 else {
720                                         //set check result flag false
721                                         check_result.flag = false;
722                                         //set check result message
723                                         check_result.message = "Cannot set multiple option '-S/--sorry-uri'.";
724                                         putLogError(100012, check_result.message, __FILE__,
725                                                     __LINE__);
726
727                                         break;
728                                 }
729                         }
730                         //option string = "-c/--statistic"
731                         else if (*it == "-c" || *it == "--statistic") {
732                                 //statistic flag is OFF
733                                 if (!stats_checked) {
734                                         //next item exist
735                                         if (++it != it_end) {
736                                                 //collect statistic flag must be 0 or 1
737                                                 if (*it == "0" || *it == "1") {
738                                                         //check OK
739                                                         //set statistic flag ON
740                                                         stats_checked = true;
741
742                                                         //set collect statistic flag
743                                                         statistic = boost::lexical_cast<int>(*it);
744                                                 } else {
745                                                         std::ostringstream ostr;
746                                                         ostr << "'-c/--statistic' option value '" << *it << "' is not a valid value.";
747
748                                                         //set check result flag false
749                                                         check_result.flag = false;
750                                                         //set check result message
751                                                         check_result.message = ostr.str();
752                                                         putLogError(100131, check_result.message, __FILE__, __LINE__);
753                                                         //loop break
754                                                         break;
755                                                 }
756                                         }
757                                         //next item is not exist
758                                         else {
759                                                 //set check flag false
760                                                 check_result.flag = false;
761                                                 //set check result message
762                                                 check_result.message = "You have to set option value '-c/--statistic'.";
763                                                 putLogError(100132, check_result.message, __FILE__,
764                                                             __LINE__);
765                                                 //loop break
766                                                 break;
767                                         }
768                                 }
769                                 //statistic flag is ON
770                                 else {
771                                         //set check result flag false
772                                         check_result.flag = false;
773                                         //set check result message
774                                         check_result.message = "Cannot set multiple option '-c/--statistic'.";
775                                         putLogError(100133, check_result.message, __FILE__,
776                                                     __LINE__);
777                                         //loop break
778                                         break;
779                                 }
780                         }
781                         //others
782                         else {
783                                 //set check result flag false
784                                 check_result.flag = false;
785                                 //set check result message
786                                 check_result.message = "Option error.";
787
788                                 putLogError(100013, check_result.message, __FILE__,
789                                             __LINE__);
790
791                                 break;
792                         }
793                 }
794
795                 if (check_result.flag == true) {
796                         //forward flag = OFF
797                         if (!forward_checked) {
798                                 forwarded_for = FORWARDED_FOR_OFF;
799                         }
800
801                         //collect statistic flag = OFF
802                         if (!stats_checked) {
803                                 statistic = COLLECT_STATS_OFF;
804                         }
805                 }
806
807         } catch (const std::exception &ex) {
808                 check_result.flag = false;
809                 std::cerr << "protocol_module_sessionless::set_parameter() : exception : error = " << ex.what() << "." << std::endl;
810                 boost::format formatter("function : protocol_module_base::check_message_result "
811                                         "protocol_module_sessionless::set_parameter() : exception : "
812                                         "error = %s.");
813                 formatter % ex.what();
814                 putLogError(100014, formatter.str(), __FILE__, __LINE__);
815         } catch (...) {
816                 check_result.flag = false;
817                 std::cerr << "protocol_module_sessionless::set_parameter() : Unknown exception." << std::endl;
818                 putLogError(100015, "function : protocol_module_base::check_message_result "
819                             "protocol_module_sessionless::set_parameter() : "
820                             "Unknown exception.", __FILE__, __LINE__);
821         }
822
823         /*-------- DEBUG LOG --------*/
824         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
825                 boost::format formatter("out_function : protocol_module_base::check_message_result "
826                                         "protocol_module_sessionless::set_parameter("
827                                         "const std::vector<std::string>& args) : return_value = ("
828                                         "check_message_result.flag = %d, check_message_result.message = %s).");
829                 formatter % check_result.flag % check_result.message;
830                 putLogDebug(100016, formatter.str(), __FILE__, __LINE__);
831         }
832         /*-------- DEBUG LOG END--------*/
833
834         return check_result;
835 }
836
837 //! parameter add
838 //! @param[in] module parameter string list
839 //! @return    result.flag true is parameter is no problem.
840 //! @return result.flag false is parameter is problem.
841 protocol_module_base::check_message_result protocol_module_sessionless::add_parameter(const std::vector <
842                 std::string > & args)
843 {
844         /*-------- DEBUG LOG --------*/
845         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
846                 boost::format formatter("in_function : protocol_module_base::check_message_result "
847                                         "protocol_module_sessionless::add_parameter("
848                                         "const std::vector<std::string>& args) : args = %s.");
849                 std::string argsdump;
850                 for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it) {
851                         argsdump += *it;
852                         argsdump += " ";
853                 }
854                 formatter % argsdump;
855                 putLogDebug(100017, formatter.str(), __FILE__, __LINE__);
856         }
857         /*------DEBUG LOG END------*/
858         check_message_result check_result;
859         //set check result flag true
860         check_result.flag = true;
861
862         //param list is not empty
863         if (!args.empty()) {
864                 //set check result flag false
865                 check_result.flag = false;
866                 //set check result message
867                 check_result.message = "Cannot add option.";
868                 putLogError(100016, check_result.message, __FILE__, __LINE__);
869         }
870
871         /*-------- DEBUG LOG --------*/
872         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
873                 boost::format formatter("out_function : protocol_module_base::check_message_result "
874                                         "protocol_module_sessionless::add_parameter("
875                                         "const std::vector<std::string>& args) : return_value = ("
876                                         "check_message_result.flag = %d, check_message_result.message = %s).");
877                 formatter % check_result.flag % check_result.message;
878                 putLogDebug(100018, formatter.str(), __FILE__, __LINE__);
879         }
880         /*-------- DEBUG LOG --------*/
881
882         return check_result;
883 }
884
885 //! get option info
886 //! @param[out] module parameter string
887 void protocol_module_sessionless::get_option_info(std::string &option)
888 {
889         /*-------- DEBUG LOG --------*/
890         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
891                 putLogDebug(100019, "in_function : void protocol_module_sessionless::get_option_info("
892                             "std::string& option).", __FILE__, __LINE__);
893         }
894         /*------DEBUG LOG END------*/
895
896         boost::format option_formatter("%s--sorry-uri '%s' --statistic %d");
897         option_formatter % (forwarded_for ? "--forwarded-for " : "") % sorry_uri.c_array()
898         % statistic;
899         option.assign(option_formatter.str());
900
901         /*-------- DEBUG LOG --------*/
902         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
903                 boost::format formatter("out_function : void protocol_module_sessionless::get_option_info("
904                                         "std::string& option) : option = %s.");
905                 formatter % option;
906                 putLogDebug(100020, formatter.str(), __FILE__, __LINE__);
907         }
908         /*------DEBUG LOG END------*/
909 }
910
911 //! TCP/IP scheduled function registration.
912 //! @param[in] schedule module TCP/IP scheduled function object type
913 void protocol_module_sessionless::register_schedule(tcp_schedule_func_type inschedule)
914 {
915         /*-------- DEBUG LOG --------*/
916         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
917                 putLogDebug(100021, "in_function : void protocol_module_sessionless::register_schedule("
918                             "tcp_schedule_func_type inschedule).", __FILE__, __LINE__);
919         }
920         /*------DEBUG LOG END------*/
921         schedule_tcp = inschedule;
922         /*-------- DEBUG LOG --------*/
923         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
924                 putLogDebug(100022, "out_function : void protocol_module_sessionless::register_schedule("
925                             "tcp_schedule_func_type inschedule).", __FILE__, __LINE__);
926         }
927         /*------DEBUG LOG END------*/
928 }
929
930 //! UDP scheduled function registration
931 //! @param[in] schedule module UDP scheduled function object type
932 void protocol_module_sessionless::register_schedule(udp_schedule_func_type inschedule)
933 {
934         /*-------- DEBUG LOG --------*/
935         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
936                 putLogDebug(100023,
937                             "in/out_function : void protocol_module_sessionless::register_schedule(udp_schedule_func_type inschedule).",
938                             __FILE__, __LINE__);
939         }
940         /*------DEBUG LOG END------*/
941 }
942
943 //! called from session initialize use in upstream_thread
944 //! @param[in]    upstream thread id.
945 //! @param[in]    downstream thread id
946 //! @return        session use EVENT mode.
947 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_session_initialize(
948         const boost::thread::id up_thread_id, const boost::thread::id down_thread_id,
949         const boost::asio::ip::tcp::endpoint &client_endpoint_tcp,
950         const boost::asio::ip::udp::endpoint &client_endpoint_udp)
951 {
952         /*-------- DEBUG LOG --------*/
953         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
954                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
955                                         "handle_session_initialize(const boost::thread::id up_thread_id, "
956                                         "const boost::thread::id down_thread_id, const boost::asio::ip::tcp::endpoint& client_endpoint_tcp, "
957                                         "const boost::asio::ip::udp::endpoint& client_endpoint_udp) : "
958                                         "up_thread_id = %d, down_thread_id = %d, client_endpoint_tcp = [%s]:%d.");
959                 formatter % up_thread_id % down_thread_id % client_endpoint_tcp.address().to_string() % client_endpoint_tcp.port() ;
960                 putLogDebug(100024, formatter.str(), __FILE__, __LINE__);
961         }
962         /*------DEBUG LOG END------*/
963         EVENT_TAG status = FINALIZE;
964
965         //session thread initialization
966         try {
967                 thread_data_ptr p_up(new session_thread_data_sessionless);
968                 /*-------- DEBUG LOG --------*/
969                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
970                         boost::format formatter("new : address = &(%d), size = %lu.");
971                         formatter % static_cast<void *>(p_up.get()) % sizeof(session_thread_data_sessionless);
972                         putLogDebug(100025, formatter.str(), __FILE__, __LINE__);
973                 }
974                 /*------DEBUG LOG END------*/
975                 p_up->thread_id = up_thread_id;
976                 p_up->thread_division = THREAD_DIVISION_UP_STREAM;
977                 p_up->pair_thread_id = down_thread_id;
978                 p_up->accept_end_flag = ACCEPT_END_FLAG_OFF;
979                 p_up->end_flag = END_FLAG_OFF;
980                 p_up->sorry_flag = SORRY_FLAG_OFF;
981                 p_up->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_OFF;
982                 p_up->realserver_switch_flag = REALSERVER_SWITCH_FLAG_OFF;
983                 p_up->last_status = INITIALIZE;
984                 p_up->client_endpoint_tcp = client_endpoint_tcp;
985
986                 receive_data recv_data;
987                 p_up->receive_data_map[client_endpoint_tcp] = recv_data;
988
989                 /*-------- DEBUG LOG --------*/
990                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
991                         // data dump
992                         boost::format
993                         formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
994                                   "handle_session_initialize() : session_thread_data_sessionless(up_thread_id) : "
995                                   "thread_id = %d, thread_division = %d, "
996                                   "pair_thread_id = %d, accept_end_flag = %d, end_flag = %d, "
997                                   "sorry_flag = %d, sorryserver_switch_flag = %d, realserver_switch_flag = %d, last_status = %d, client_endpoint_tcp = [%s]:%d.");
998                         formatter % p_up->thread_id % p_up->thread_division % p_up->pair_thread_id % p_up->accept_end_flag
999                         % p_up->end_flag % p_up->sorry_flag % p_up->sorryserver_switch_flag % p_up->realserver_switch_flag
1000                         % p_up->last_status % client_endpoint_tcp.address().to_string() % client_endpoint_tcp.port();
1001                         putLogDebug(100026, formatter.str(), __FILE__, __LINE__);
1002                 }
1003                 /*------DEBUG LOG END------*/
1004
1005                 thread_data_ptr p_down(new session_thread_data_sessionless);
1006                 /*-------- DEBUG LOG --------*/
1007                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1008                         boost::format formatter("new : address = &(%d), size = %lu.");
1009                         formatter % static_cast<void *>(p_down.get()) % sizeof(session_thread_data_sessionless);
1010                         putLogDebug(100027, formatter.str(), __FILE__, __LINE__);
1011                 }
1012                 /*------DEBUG LOG END------*/
1013                 p_down->thread_id = down_thread_id;
1014                 p_down->thread_division = THREAD_DIVISION_DOWN_STREAM;
1015                 p_down->pair_thread_id = up_thread_id;
1016                 p_down->accept_end_flag = ACCEPT_END_FLAG_OFF;
1017                 p_down->end_flag = END_FLAG_OFF;
1018                 p_down->sorry_flag = SORRY_FLAG_OFF;
1019                 p_down->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_OFF;
1020                 p_down->realserver_switch_flag = REALSERVER_SWITCH_FLAG_OFF;
1021                 p_down->last_status = INITIALIZE;
1022                 p_down->client_endpoint_tcp = client_endpoint_tcp;
1023                 /*-------- DEBUG LOG --------*/
1024                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1025                         // data dump
1026                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1027                                                 "handle_session_initialize() : session_thread_data_sessionless(down_thread_id) : "
1028                                                 "thread_id = %d, thread_division = %d, pair_thread_id = %d, accept_end_flag = %d, end_flag = %d, "
1029                                                 "sorry_flag = %d, sorryserver_switch_flag = %d, realserver_switch_flag = %d, "
1030                                                 "last_status = %d, client_endpoint_tcp = [%s]:%d.");
1031                         formatter % p_down->thread_id % p_down->thread_division % p_down->pair_thread_id % p_down->accept_end_flag
1032                         % p_down->end_flag % p_down->sorry_flag % p_down->sorryserver_switch_flag
1033                         % p_down->realserver_switch_flag % p_down->last_status % client_endpoint_tcp.address().to_string() % client_endpoint_tcp.port();
1034                         putLogDebug(100028, formatter.str(), __FILE__, __LINE__);
1035                 }
1036                 /*------DEBUG LOG END------*/
1037
1038                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
1039
1040                 session_thread_data_map[up_thread_id] = p_up;
1041                 session_thread_data_map[down_thread_id] = p_down;
1042
1043                 status = ACCEPT;
1044         } catch (const std::bad_alloc &) {
1045                 std::cerr << "protocol_module_sessionless::handle_session_initialize() : exception : Could not allocate memory." << std::endl;
1046                 boost::format formatter("Could not allocate memory. thread id : %d.");
1047                 formatter % boost::this_thread::get_id();
1048                 putLogError(100017, formatter.str(), __FILE__, __LINE__);
1049                 status = FINALIZE;
1050         } catch (const std::exception &ex) {
1051                 std::cerr << "protocol_module_sessionless::handle_session_initialize() : exception : error = " << ex.what() << "." << std::endl;
1052                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1053                                         "handle_session_initialize() : exception : error = %s. thread id : %d.");
1054                 formatter % ex.what() % boost::this_thread::get_id();
1055                 putLogError(100018, formatter.str(), __FILE__, __LINE__);
1056                 status = FINALIZE;
1057         } catch (...) {
1058                 std::cerr << "protocol_module_sessionless::handle_session_initialize() : Unknown exception." << std::endl;
1059                 boost::format formatter("function : protocol_module_base::check_message_result "
1060                                         "protocol_module_sessionless::handle_session_initialize() : "
1061                                         "Unknown exception. thread id : %d.");
1062                 formatter % boost::this_thread::get_id();
1063                 putLogError(100019, formatter.str(), __FILE__, __LINE__);
1064
1065                 status = FINALIZE;
1066         }
1067
1068         /*-------- DEBUG LOG --------*/
1069         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1070                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1071                                         "handle_session_initialize(const boost::thread::id up_thread_id, "
1072                                         "const boost::thread::id down_thread_id, const boost::asio::ip::tcp::endpoint& client_endpoint_tcp, "
1073                                         "const boost::asio::ip::udp::endpoint& client_endpoint_udp) : return_value = %d. thread id : %d.");
1074                 formatter % status % boost::this_thread::get_id();
1075                 putLogDebug(100029, formatter.str(), __FILE__, __LINE__);
1076         }
1077         /*------DEBUG LOG END------*/
1078
1079         return status;
1080 }
1081 //! called from session finalize use in upstream thread.
1082 //! @param[in]    upstream thread id.
1083 //! @param[in]    downstream thread id
1084 //! @return        session use EVENT mode.
1085 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_session_finalize(
1086         const boost::thread::id up_thread_id, const boost::thread::id down_thread_id)
1087 {
1088
1089         /*-------- DEBUG LOG --------*/
1090         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1091                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1092                                         "handle_session_finalize(const boost::thread::id up_thread_id, "
1093                                         "const boost::thread::id down_thread_id) : "
1094                                         "up_thread_id = %d, down_thread_id = %d.");
1095                 formatter % up_thread_id % down_thread_id;
1096                 putLogDebug(100030, formatter.str(), __FILE__, __LINE__);
1097         }
1098         /*------DEBUG LOG END------*/
1099         EVENT_TAG status = STOP;
1100
1101         //session thread free
1102         try {
1103                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
1104
1105                 session_thread_data_map_it      session_thread_data_it = session_thread_data_map.find(up_thread_id);
1106                 if (session_thread_data_it != session_thread_data_map.end()) {
1107                         thread_data_ptr p_up = session_thread_data_it->second;
1108                         /*-------- DEBUG LOG --------*/
1109                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1110                                 boost::format formatter("delete : address = &(%d).");
1111                                 formatter % static_cast<void *>(p_up.get());
1112                                 putLogDebug(100031, formatter.str(), __FILE__, __LINE__);
1113                         }
1114                         /*------DEBUG LOG END------*/
1115                         session_thread_data_map.erase(up_thread_id);
1116                 }
1117
1118                 session_thread_data_it = session_thread_data_map.find(down_thread_id);
1119                 if (session_thread_data_it != session_thread_data_map.end()) {
1120                         thread_data_ptr p_down = session_thread_data_it->second;
1121                         /*-------- DEBUG LOG --------*/
1122                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1123                                 boost::format formatter("delete : address = &(%d).");
1124                                 formatter % static_cast<void *>(p_down.get());
1125                                 putLogDebug(100032, formatter.str(), __FILE__,
1126                                             __LINE__);
1127                         }
1128                         /*------DEBUG LOG END------*/
1129                         session_thread_data_map.erase(down_thread_id);
1130                 }
1131
1132                 status = STOP;
1133         } catch (const std::exception &ex) {
1134                 std::cerr << "protocol_module_sessionless::handle_session_finalize() : exception : error = " << ex.what() << "." << std::endl;
1135                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1136                                         "handle_session_finalize() : exception : error = %s. thread id : %d.");
1137                 formatter % ex.what() % boost::this_thread::get_id();
1138                 putLogError(100020, formatter.str(), __FILE__, __LINE__);
1139                 status = STOP;
1140         } catch (...) {
1141                 std::cerr << "protocol_module_sessionless::handle_session_finalize() : Unknown exception." << std::endl;
1142                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1143                                         "handle_session_finalize() : "
1144                                         "Unknown exception. thread id : %d.");
1145                 formatter % boost::this_thread::get_id();
1146                 putLogError(100021, formatter.str(), __FILE__, __LINE__);
1147                 status = STOP;
1148         }
1149
1150         /*-------- DEBUG LOG --------*/
1151         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1152                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1153                                         "handle_session_finalize(const boost::thread::id up_thread_id, "
1154                                         "const boost::thread::id down_thread_id) : return_value = %d. thread id : %d.");
1155                 formatter % status % boost::this_thread::get_id();
1156                 putLogDebug(100033, formatter.str(), __FILE__, __LINE__);
1157         }
1158         /*------DEBUG LOG END------*/
1159         return status;
1160 }
1161
1162 //! called from after session accept.in client socket use in upstream thread.
1163 //! @param[in]    upstream thread id.
1164 //! @return        session use EVENT mode.
1165 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_accept(const boost::thread::id thread_id)
1166 {
1167
1168         /*-------- DEBUG LOG --------*/
1169         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1170                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1171                                         "handle_accept(const boost::thread::id thread_id) : thread_id = %d.");
1172                 formatter % thread_id;
1173                 putLogDebug(100034, formatter.str(), __FILE__, __LINE__);
1174         }
1175         /*------DEBUG LOG END------*/
1176
1177         EVENT_TAG status = FINALIZE;
1178         thread_data_ptr session_data;
1179         session_thread_data_map_it session_thread_it;
1180
1181         try {
1182                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
1183
1184                 session_thread_it = session_thread_data_map.find(thread_id);
1185                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
1186                         boost::format formatter("Invalid thread id. thread id : %d.");
1187                         formatter % boost::this_thread::get_id();
1188                         putLogError(100022, formatter.str(), __FILE__, __LINE__);
1189                         throw - 1;
1190                 }
1191
1192                 session_data = session_thread_it->second;
1193
1194                 //set accept end flag ON
1195                 session_data->accept_end_flag = ACCEPT_END_FLAG_ON;
1196                 /*-------- DEBUG LOG --------*/
1197                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1198                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1199                                                 "handle_accept(const boost::thread::id thread_id) : ACCEPT_END_FLAG_ON. thread id : %d.");
1200                         formatter % boost::this_thread::get_id();
1201                         putLogDebug(100035, formatter.str(), __FILE__, __LINE__);
1202                 }
1203                 /*------DEBUG LOG END------*/
1204                 //sorry flag on
1205                 if (session_data->sorry_flag == SORRY_FLAG_ON) {
1206                         //set return status
1207                         status = SORRYSERVER_SELECT;
1208                 }
1209                 //sorry flag off
1210                 else {
1211                         //set return status
1212                         status = REALSERVER_SELECT;
1213                 }
1214
1215         } catch (int e) {
1216                 /*-------- DEBUG LOG --------*/
1217                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1218                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1219                                                 "handle_accept() : catch exception e = %d. thread id : %d.");
1220                         formatter % e % boost::this_thread::get_id();
1221                         putLogDebug(100036, formatter.str(), __FILE__, __LINE__);
1222                 }
1223                 status = FINALIZE;
1224                 /*------DEBUG LOG END------*/
1225         } catch (const std::exception &ex) {
1226                 std::cerr << "protocol_module_sessionless::handle_accept() : exception : error = " << ex.what() << "." << std::endl;
1227                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1228                                         "handle_accept() : exception : error = %s. thread id : %d.");
1229                 formatter % ex.what() % boost::this_thread::get_id();
1230                 putLogError(100023, formatter.str(), __FILE__, __LINE__);
1231
1232                 status = FINALIZE;
1233         } catch (...) {
1234                 std::cerr << "protocol_module_sessionless::handle_accept() : Unknown exception." << std::endl;
1235                 boost::format formatter("function : protocol_module_base::EVENT_TAG "
1236                                         "protocol_module_sessionless::handle_accept() : "
1237                                         "Unknown exception. thread id : %d.");
1238                 formatter % boost::this_thread::get_id();
1239                 putLogError(100024, formatter.str(), __FILE__, __LINE__);
1240                 status = FINALIZE;
1241         }
1242
1243         /*-------- DEBUG LOG --------*/
1244         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1245                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1246                                         "handle_accept(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
1247                 formatter % status % boost::this_thread::get_id();
1248                 putLogDebug(100037, formatter.str(), __FILE__, __LINE__);
1249         }
1250         /*------DEBUG LOG END------*/
1251         return status;
1252 }
1253
1254 //! called from after session recv in client socket. use in upstream thread.
1255 //! @param[in]    upstream thread id
1256 //! @param[in]    receive buffer reference.
1257 //! @param[in]    receive length
1258 //! @return        session use EVENT mode.
1259 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_recv(const boost::thread::id thread_id,
1260                 const boost::array<char, MAX_BUFFER_SIZE>& recvbuffer, const size_t recvlen)
1261 {
1262         /*-------- DEBUG LOG --------*/
1263         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1264                 size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
1265                 std::string buffer;
1266                 dump_memory(recvbuffer.data(), buffer_size, buffer);
1267                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1268                                         "handle_client_recv(const boost::thread::id thread_id, "
1269                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
1270                                         "const size_t recvlen) : thread_id = %d, recvbuffer = %s, recvlen = %d.");
1271                 formatter % thread_id % buffer % recvlen;
1272                 putLogDebug(100038, formatter.str(), __FILE__, __LINE__);
1273         }
1274         /*------DEBUG LOG END------*/
1275
1276         EVENT_TAG status = FINALIZE;
1277         size_t data_remain_start = 0;
1278         size_t data_remain_size = 0;
1279         size_t request_data_remain_size = 0;
1280         size_t header_offset = 0;
1281         size_t header_offset_len = 0;
1282         size_t content_length_header_offset = 0;
1283         size_t content_length_header_len = 0;
1284         size_t content_len_value = 0;
1285         size_t pos = 0;
1286         size_t buffer_size = 0;
1287         const size_t cr_lf_cr_lf_len = strlen("\r\n\r\n");
1288         const size_t cr_lf_len = strlen("\r\n");
1289         std::string str_value;
1290         const std::string http_header = "";
1291         const std::string content_header = "Content-Length";
1292         thread_data_ptr session_data;
1293         char *buffer1 = NULL;
1294         char *buffer2 = NULL;
1295         bool bret = false;
1296         CHECK_RESULT_TAG check_result;
1297         session_thread_data_map_it session_thread_it;
1298         receive_data_map_it receive_data_it;
1299
1300         //parameter check
1301         if (recvlen > recvbuffer.size()) {
1302                 std::cerr << "protocol_module_sessionless::handle_client_recv() : Data size bigger than buffer size." << std::endl;
1303                 boost::format formatter("Data size bigger than buffer size. thread id : %d.");
1304                 formatter % boost::this_thread::get_id();
1305                 putLogError(100025, formatter.str(), __FILE__, __LINE__);
1306                 /*-------- DEBUG LOG --------*/
1307                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1308                         boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1309                                                 "handle_client_recv(const boost::thread::id thread_id, "
1310                                                 "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
1311                                                 "const size_t recvlen) : return_value = %d. thread id : %d.");
1312                         formatter % FINALIZE % boost::this_thread::get_id();
1313                         putLogDebug(100039, formatter.str(), __FILE__, __LINE__);
1314                 }
1315                 /*------DEBUG LOG END------*/
1316                 return FINALIZE;
1317         }
1318
1319         try {
1320                 {
1321                         boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
1322
1323                         session_thread_it = session_thread_data_map.find(thread_id);
1324                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
1325                                 boost::format formatter("Invalid thread id. thread id : %d.");
1326                                 formatter % boost::this_thread::get_id();
1327                                 putLogError(100026, formatter.str(), __FILE__, __LINE__);
1328                                 throw - 1;
1329                         }
1330
1331                         session_data = session_thread_it->second;
1332                 }
1333
1334                 //end flag on
1335                 if (session_data->end_flag == END_FLAG_ON) {
1336                         status = CLIENT_RECV;
1337                 }
1338                 //end flag off
1339                 else {
1340                         receive_data_it = session_data->receive_data_map.find(session_data->client_endpoint_tcp);
1341                         if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
1342                                 boost::format formatter("Invalid endpoint. thread id : %d.");
1343                                 formatter % boost::this_thread::get_id();
1344                                 putLogError(100027, formatter.str(), __FILE__, __LINE__);
1345                                 throw - 1;
1346                         }
1347
1348                         receive_data &recv_data = receive_data_it->second;
1349
1350                         send_status_it it = recv_data.send_status_list.begin();
1351                         send_status_it it_end = recv_data.send_status_list.end();
1352
1353                         //status list check
1354                         it = std::find_if(it, it_end, data_send_ok());
1355                         if (unlikely(it != it_end)) {
1356                                 boost::format formatter("Sending data is not correct. thread id : %d.");
1357                                 formatter % boost::this_thread::get_id();
1358                                 putLogError(100028, formatter.str(), __FILE__, __LINE__);
1359                                 throw - 1;
1360                         }
1361
1362                         //status list check
1363                         it = recv_data.send_status_list.begin();
1364                         it = std::adjacent_find(it, it_end, data_send_repeated());
1365                         if (unlikely(it != it_end)) {
1366                                 boost::format formatter("Sending data is not correct. thread id : %d.");
1367                                 formatter % boost::this_thread::get_id();
1368                                 putLogError(100029, formatter.str(), __FILE__, __LINE__);
1369                                 throw - 1;
1370                         }
1371
1372                         /*-------- DEBUG LOG --------*/
1373                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1374                                 std::string datadump;
1375                                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
1376                                                         "send_rest_size = %d, send_possible_size = %d, "
1377                                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
1378                                 int i = 0;
1379                                 for (it = recv_data.send_status_list.begin();
1380                                      it != recv_data.send_status_list.end();
1381                                      ++it, ++i) {
1382                                         formatter % i % it->status % it->send_end_size
1383                                         % it->send_rest_size % it->send_possible_size
1384                                         % it->send_offset % it->unsend_size % it->edit_division;
1385                                         datadump += formatter.str();
1386                                 }
1387
1388                                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1389                                                 "handle_client_recv() : send status list dump : send status list size = %d.%s");
1390
1391                                 formatter % recv_data.send_status_list.size() % datadump;
1392                                 putLogDebug(100040, formatter.str(), __FILE__, __LINE__);
1393                         }
1394                         /*------DEBUG LOG END------*/
1395
1396                         it = recv_data.send_status_list.begin();
1397                         //get original status info
1398                         while (it != it_end) {
1399                                 //item status is SEND_END
1400                                 if (it->status == SEND_END) {
1401                                         //erase from list
1402                                         recv_data.send_status_list.erase(it++);
1403                                         continue;
1404                                 }
1405                                 //item status is SEND_CONTINUE
1406                                 else if (it->status == SEND_CONTINUE) {
1407                                         it->send_offset += it->send_end_size;
1408                                         data_remain_start = it->send_offset;
1409                                         break;
1410                                 }
1411                                 //item status is SEND_NG
1412                                 else {
1413                                         data_remain_start = it->send_offset;
1414                                         data_remain_size = it->unsend_size;
1415                                         break;
1416                                 }
1417
1418                                 ++it;
1419                         }
1420                         /*-------- DEBUG LOG --------*/
1421                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1422                                 std::string datadump;
1423                                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
1424                                                         "send_rest_size = %d, send_possible_size = %d, "
1425                                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
1426                                 int i = 0;
1427                                 for (it = recv_data.send_status_list.begin();
1428                                      it != recv_data.send_status_list.end();
1429                                      ++it, ++i) {
1430                                         formatter % i % it->status % it->send_end_size
1431                                         % it->send_rest_size % it->send_possible_size
1432                                         % it->send_offset % it->unsend_size % it->edit_division;
1433                                         datadump += formatter.str();
1434                                 }
1435
1436                                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1437                                                 "handle_client_recv() : send status list dump : send status list size = %d.%s");
1438
1439                                 formatter % recv_data.send_status_list.size() % datadump;
1440                                 putLogDebug(100041, formatter.str(), __FILE__, __LINE__);
1441                         }
1442                         /*------DEBUG LOG END------*/
1443
1444                         //receive buffer process
1445                         //buffer rest size < request size
1446                         if (recv_data.receive_buffer_rest_size < recvlen) {
1447                                 //buffer max size < remain size + request size
1448                                 //buffer is need reallocate
1449                                 if (recv_data.receive_buffer_max_size < data_remain_size + recvlen) {
1450                                         //the buffer's size that will be allocated is exceed the upper limit value
1451                                         if (MAX_SESSIONLESS_MODULE_BUFFER_SIZE < data_remain_size + recvlen) {
1452                                                 std::cerr << "protocol_module_sessionless::handle_client_recv() : the buffer's size that will be allocated is exceed the upper limit value." << std::endl;
1453                                                 boost::format formatter("The buffer's size that will be allocated is exceed the upper limit value. thread id : %d.");
1454                                                 formatter % boost::this_thread::get_id();
1455                                                 putLogError(100030, formatter.str(), __FILE__, __LINE__);
1456                                                 /*-------- DEBUG LOG --------*/
1457                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1458                                                         boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1459                                                                                 "handle_client_recv(const boost::thread::id thread_id, "
1460                                                                                 "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
1461                                                                                 "const size_t recvlen) : return_value = %d. thread id : %d.");
1462                                                         formatter % FINALIZE % boost::this_thread::get_id();
1463                                                         putLogDebug(100042, formatter.str(), __FILE__, __LINE__);
1464                                                 }
1465                                                 /*------DEBUG LOG END------*/
1466                                                 return FINALIZE;
1467                                         }
1468
1469                                         buffer_size = (data_remain_size + recvlen) > MAX_BUFFER_SIZE ? (data_remain_size + recvlen) : MAX_BUFFER_SIZE;
1470                                         //receive_buffer1's memory allocate and initialization
1471                                         buffer1 = new char[buffer_size];
1472                                         /*-------- DEBUG LOG --------*/
1473                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1474                                                 boost::format formatter("new : address = &(%d), size = %lu.");
1475                                                 formatter % static_cast<void *>(buffer1) % buffer_size;
1476                                                 putLogDebug(100043, formatter.str(), __FILE__, __LINE__);
1477                                         }
1478                                         /*------DEBUG LOG END------*/
1479                                         memset(buffer1, 0, buffer_size);
1480                                         //receive_buffer2's memory allocate and initialization
1481                                         buffer2 = new char[buffer_size];
1482                                         /*-------- DEBUG LOG --------*/
1483                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1484                                                 boost::format formatter("new : address = &(%d), size = %lu.");
1485                                                 formatter % static_cast<void *>(buffer2) % buffer_size;
1486                                                 putLogDebug(100044, formatter.str(), __FILE__, __LINE__);
1487                                         }
1488                                         /*------DEBUG LOG END------*/
1489                                         memset(buffer2, 0, buffer_size);
1490
1491                                         /*-------- DEBUG LOG --------*/
1492                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1493                                                 std::string datadump;
1494                                                 dump_memory(recv_data.receive_buffer + data_remain_start, data_remain_size, datadump);
1495                                                 boost::format formatter(
1496                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1497                                                         "handle_client_recv() : before memcpy (data dump) : "
1498                                                         "data begin = %d, data_size = %d, data = %s");
1499                                                 formatter % data_remain_start % data_remain_size % datadump;
1500                                                 putLogDebug(100045, formatter.str(), __FILE__, __LINE__);
1501                                         }
1502                                         /*------DEBUG LOG END------*/
1503                                         //copy data from old buffer to new buffer
1504                                         memcpy(buffer1, recv_data.receive_buffer + data_remain_start, data_remain_size);
1505                                         /*-------- DEBUG LOG --------*/
1506                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1507                                                 std::string datadump;
1508                                                 dump_memory(buffer1, data_remain_size, datadump);
1509                                                 boost::format formatter(
1510                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1511                                                         "handle_client_recv() : after memcpy (data dump) : "
1512                                                         "data begin = 0, data_size = %d, data = %s");
1513                                                 formatter % data_remain_size % datadump;
1514                                                 putLogDebug(100046, formatter.str(), __FILE__, __LINE__);
1515                                         }
1516                                         /*------DEBUG LOG END------*/
1517
1518                                         /*-------- DEBUG LOG --------*/
1519                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1520                                                 std::string datadump;
1521                                                 dump_memory(recvbuffer.data(), recvlen, datadump);
1522                                                 boost::format formatter(
1523                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1524                                                         "handle_client_recv() : before memcpy (data dump) : "
1525                                                         "data begin = 0, data_size = %d, data = %s");
1526                                                 formatter % recvlen % datadump;
1527                                                 putLogDebug(100047, formatter.str(), __FILE__, __LINE__);
1528                                         }
1529                                         /*------DEBUG LOG END------*/
1530                                         memcpy(buffer1 + data_remain_size, recvbuffer.data(), recvlen);
1531                                         /*-------- DEBUG LOG --------*/
1532                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1533                                                 std::string datadump;
1534                                                 dump_memory(buffer1 + data_remain_size, recvlen, datadump);
1535                                                 boost::format formatter(
1536                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1537                                                         "handle_client_recv() : after memcpy (data dump) : "
1538                                                         "data begin = %d, data_size = %d, data = %s");
1539                                                 formatter % data_remain_size % recvlen % datadump;
1540                                                 putLogDebug(100048, formatter.str(), __FILE__, __LINE__);
1541                                         }
1542                                         /*------DEBUG LOG END------*/
1543                                         //free old buffer1 and old buffer2
1544                                         if (recv_data.receive_buffer1 != NULL) {
1545                                                 /*-------- DEBUG LOG --------*/
1546                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1547                                                         boost::format formatter("delete : address = &(%d).");
1548                                                         formatter % static_cast<void *>(recv_data.receive_buffer1);
1549                                                         putLogDebug(100049, formatter.str(), __FILE__,
1550                                                                     __LINE__);
1551                                                 }
1552                                                 /*------DEBUG LOG END------*/
1553                                                 delete[] recv_data.receive_buffer1;
1554                                                 recv_data.receive_buffer1 = NULL;
1555                                         }
1556
1557                                         if (recv_data.receive_buffer2 != NULL) {
1558                                                 /*-------- DEBUG LOG --------*/
1559                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1560                                                         boost::format formatter("delete : address = &(%d).");
1561                                                         formatter % static_cast<void *>(recv_data.receive_buffer2);
1562                                                         putLogDebug(100050, formatter.str(), __FILE__,
1563                                                                     __LINE__);
1564                                                 }
1565                                                 /*------DEBUG LOG END------*/
1566                                                 delete[] recv_data.receive_buffer2;
1567                                                 recv_data.receive_buffer2 = NULL;
1568                                         }
1569
1570                                         //set new buffer pointer
1571                                         recv_data.receive_buffer1 = buffer1;
1572                                         recv_data.receive_buffer2 = buffer2;
1573                                         recv_data.receive_buffer = recv_data.receive_buffer1;
1574                                         //set new buffer's max size
1575                                         recv_data.receive_buffer_max_size = buffer_size;
1576                                 }
1577                                 //buffer's max size >= remain data size + request size
1578                                 //buffer isn't need reallocate, but switch
1579                                 else {
1580                                         //pointer valid check
1581                                         if (unlikely(recv_data.receive_buffer1 == NULL || recv_data.receive_buffer2 == NULL)) {
1582                                                 boost::format formatter("Invalid pointer. thread id : %d.");
1583                                                 formatter % boost::this_thread::get_id();
1584                                                 putLogError(100031, formatter.str(), __FILE__, __LINE__);
1585                                                 throw - 1;
1586                                         }
1587                                         //using buffer is buffer1
1588                                         if (recv_data.receive_buffer == recv_data.receive_buffer1) {
1589                                                 //buffer2 initialization
1590                                                 memset(recv_data.receive_buffer2, 0, recv_data.receive_buffer_max_size);
1591                                                 /*-------- DEBUG LOG --------*/
1592                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1593                                                         std::string datadump;
1594                                                         dump_memory(recv_data.receive_buffer + data_remain_start, data_remain_size, datadump);
1595                                                         boost::format formatter(
1596                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1597                                                                 "handle_client_recv() : before memcpy (data dump) : "
1598                                                                 "data begin = %d, data_size = %d, data = %s");
1599                                                         formatter % data_remain_start % data_remain_size  % datadump;
1600                                                         putLogDebug(100051, formatter.str(), __FILE__, __LINE__);
1601                                                 }
1602                                                 /*------DEBUG LOG END------*/
1603                                                 //copy data from buffer1 to buffer2
1604                                                 memcpy(recv_data.receive_buffer2, recv_data.receive_buffer + data_remain_start, data_remain_size);
1605                                                 /*-------- DEBUG LOG --------*/
1606                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1607                                                         std::string datadump;
1608                                                         dump_memory(recv_data.receive_buffer2, recvlen, datadump);
1609                                                         boost::format formatter(
1610                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1611                                                                 "handle_client_recv() : after memcpy (data dump) : "
1612                                                                 "data begin = 0, data_size = %d, data = %s");
1613                                                         formatter % recvlen % datadump;
1614                                                         putLogDebug(100052, formatter.str(), __FILE__, __LINE__);
1615                                                 }
1616                                                 /*------DEBUG LOG END------*/
1617                                                 /*-------- DEBUG LOG --------*/
1618                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1619                                                         std::string datadump;
1620                                                         dump_memory(recvbuffer.data(), recvlen, datadump);
1621                                                         boost::format formatter(
1622                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1623                                                                 "handle_client_recv() : before memcpy (data dump) : "
1624                                                                 "data begin = 0, data_size = %d, data = %s");
1625                                                         formatter % recvlen % datadump;
1626                                                         putLogDebug(100053, formatter.str(), __FILE__, __LINE__);
1627                                                 }
1628                                                 /*------DEBUG LOG END------*/
1629                                                 memcpy(recv_data.receive_buffer2 + data_remain_size, recvbuffer.data(), recvlen);
1630                                                 /*-------- DEBUG LOG --------*/
1631                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1632                                                         std::string datadump;
1633                                                         dump_memory(recv_data.receive_buffer2 + data_remain_size, recvlen, datadump);
1634                                                         boost::format formatter(
1635                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1636                                                                 "handle_client_recv() : after memcpy (data dump) : "
1637                                                                 "data begin = %d, data_size = %d, data = %s");
1638                                                         formatter % data_remain_size % recvlen % datadump;
1639                                                         putLogDebug(100054, formatter.str(), __FILE__, __LINE__);
1640                                                 }
1641                                                 /*------DEBUG LOG END------*/
1642                                                 //set buffer2 as using buffer
1643                                                 recv_data.receive_buffer = recv_data.receive_buffer2;
1644                                         }
1645                                         //using buffer is buffer2
1646                                         else {
1647                                                 //buffer1 initialization
1648                                                 memset(recv_data.receive_buffer1, 0, recv_data.receive_buffer_max_size);
1649                                                 /*-------- DEBUG LOG --------*/
1650                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1651                                                         std::string datadump;
1652                                                         dump_memory(recv_data.receive_buffer + data_remain_start, data_remain_size, datadump);
1653                                                         boost::format formatter(
1654                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1655                                                                 "handle_client_recv() : before memcpy (data dump) : "
1656                                                                 "data begin = %d, data_size = %d, data = %s");
1657                                                         formatter % data_remain_start % data_remain_size % datadump;
1658                                                         putLogDebug(100055, formatter.str(), __FILE__, __LINE__);
1659                                                 }
1660                                                 /*------DEBUG LOG END------*/
1661                                                 //copy data from buffer2 to buffer1
1662                                                 memcpy(recv_data.receive_buffer1, recv_data.receive_buffer + data_remain_start, data_remain_size);
1663                                                 /*-------- DEBUG LOG --------*/
1664                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1665                                                         std::string datadump;
1666                                                         dump_memory(recv_data.receive_buffer1, data_remain_size, datadump);
1667                                                         boost::format formatter(
1668                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1669                                                                 "handle_client_recv() : after memcpy (data dump) : "
1670                                                                 "data begin = 0, data_size = %d, data = %s");
1671                                                         formatter % data_remain_size % datadump;
1672                                                         putLogDebug(100056, formatter.str(), __FILE__, __LINE__);
1673                                                 }
1674                                                 /*------DEBUG LOG END------*/
1675                                                 /*-------- DEBUG LOG --------*/
1676                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1677                                                         std::string datadump;
1678                                                         dump_memory(recvbuffer.data(), recvlen, datadump);
1679                                                         boost::format formatter(
1680                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1681                                                                 "handle_client_recv() : before memcpy (data dump) : "
1682                                                                 "data begin = 0, data_size = %d, data = %s");
1683                                                         formatter % recvlen % datadump;
1684                                                         putLogDebug(100057, formatter.str(), __FILE__, __LINE__);
1685                                                 }
1686                                                 /*------DEBUG LOG END------*/
1687                                                 memcpy(recv_data.receive_buffer1 + data_remain_size, recvbuffer.data(), recvlen);
1688                                                 /*-------- DEBUG LOG --------*/
1689                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1690                                                         std::string datadump;
1691                                                         dump_memory(recv_data.receive_buffer1 + data_remain_size, recvlen, datadump);
1692                                                         boost::format formatter(
1693                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1694                                                                 "handle_client_recv() : after memcpy (data dump) : "
1695                                                                 "data begin = %d, data_size = %d, data = %s");
1696                                                         formatter % data_remain_size % recvlen % datadump;
1697                                                         putLogDebug(100058, formatter.str(), __FILE__, __LINE__);
1698                                                 }
1699                                                 /*------DEBUG LOG END------*/
1700                                                 //set buffer1 as using buffer
1701                                                 recv_data.receive_buffer = recv_data.receive_buffer1;
1702                                         }
1703                                 }
1704
1705                                 //set buffer's rest size
1706                                 recv_data.receive_buffer_rest_size = recv_data.receive_buffer_max_size - data_remain_size - recvlen;
1707
1708                                 //remain_size recalc
1709                                 data_remain_size += recvlen;
1710
1711                                 send_status_it it_begin = recv_data.send_status_list.begin();
1712                                 send_status_it it_end = recv_data.send_status_list.end();
1713
1714                                 //offset recalc
1715                                 for (; it_begin != it_end; ++it_begin) {
1716                                         it_begin->send_offset -= data_remain_start;
1717                                 }
1718                         }
1719                         //buffer's rest size >= request size
1720                         else {
1721                                 //pointer valid check
1722                                 if (unlikely(recv_data.receive_buffer == NULL)) {
1723                                         boost::format formatter("Invalid pointer. thread id : %d.");
1724                                         formatter % boost::this_thread::get_id();
1725                                         putLogError(100032, formatter.str(), __FILE__, __LINE__);
1726                                         throw - 1;
1727                                 }
1728                                 /*-------- DEBUG LOG --------*/
1729                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1730                                         std::string datadump;
1731                                         dump_memory(recvbuffer.data(), recvlen, datadump);
1732                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1733                                                                 "handle_client_recv() : before memcpy (data dump) : "
1734                                                                 "data begin = 0, data_size = %d, data = %s");
1735                                         formatter % recvlen % datadump;
1736                                         putLogDebug(100059, formatter.str(), __FILE__, __LINE__);
1737                                 }
1738                                 /*------DEBUG LOG END------*/
1739                                 //copy data from parameter to using buffer
1740                                 memcpy(recv_data.receive_buffer + recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size,
1741                                        recvbuffer.data(), recvlen);
1742                                 /*-------- DEBUG LOG --------*/
1743                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1744                                         std::string datadump;
1745                                         dump_memory(recv_data.receive_buffer + recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size,
1746                                                     recvlen, datadump);
1747                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1748                                                                 "handle_client_recv() : after memcpy (data dump) : "
1749                                                                 "data begin = %d, data_size = %d, data = %s");
1750                                         formatter % (recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size)
1751                                         % recvlen % datadump;
1752                                         putLogDebug(100060, formatter.str(), __FILE__, __LINE__);
1753                                 }
1754                                 /*------DEBUG LOG END------*/
1755                                 //buffer's rest size recalc
1756                                 recv_data.receive_buffer_rest_size -= recvlen;
1757                                 //remain data size recalc
1758                                 data_remain_size += recvlen;
1759                         }
1760
1761                         it = recv_data.send_status_list.begin();
1762                         it_end = recv_data.send_status_list.end();
1763
1764                         //set request rest size
1765                         request_data_remain_size = recvlen;
1766
1767                         //original status process
1768                         for (; it != it_end; ++it) {
1769                                 //status is SEND_CONTINUE
1770                                 if (it->status == SEND_CONTINUE) {
1771                                         //send rest size > request size
1772                                         if (it->send_rest_size > request_data_remain_size) {
1773                                                 //send possible size recalc
1774                                                 it->send_possible_size = request_data_remain_size;
1775                                                 //send rest size recalc
1776                                                 it->send_rest_size -= request_data_remain_size;
1777                                                 //send end size recalc
1778                                                 it->send_end_size = 0;
1779                                                 //request size recalc
1780                                                 request_data_remain_size = 0;
1781                                         }
1782                                         //send rest size <= request size
1783                                         else {
1784                                                 //send possible size recalc
1785                                                 it->send_possible_size = it->send_rest_size;
1786                                                 //send rest size recalc
1787                                                 request_data_remain_size -= it->send_rest_size;
1788                                                 //send end size recalc
1789                                                 it->send_end_size = 0;
1790                                                 //request size recalc
1791                                                 it->send_rest_size = 0;
1792                                         }
1793                                         //set edit_division flag off
1794                                         it->edit_division = EDIT_DIVISION_NO_EDIT;
1795                                         //set status SEND_OK
1796                                         it->status = SEND_OK;
1797                                 }
1798                                 //status is SEND_NG
1799                                 else if (it->status == SEND_NG) {
1800                                         if (statistic == COLLECT_STATS_ON || forwarded_for == FORWARDED_FOR_ON) {
1801                                                 //check http method
1802                                                 check_result = check_http_method(recv_data.receive_buffer + it->send_offset, data_remain_size);
1803                                                 /*-------- DEBUG LOG --------*/
1804                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1805                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1806                                                                                 "handle_client_recv() : call check_http_method : "
1807                                                                                 "return_value = %d. thread id : %d.");
1808                                                         formatter % check_result % boost::this_thread::get_id();
1809                                                         putLogDebug(100061, formatter.str(), __FILE__, __LINE__);
1810                                                 }
1811                                                 /*------DEBUG LOG END------*/
1812                                                 //check http method result is CHECK_OK
1813                                                 if (check_result == CHECK_OK) {
1814                                                         //check http version
1815                                                         check_result = check_http_version(recv_data.receive_buffer + it->send_offset, data_remain_size);
1816                                                         /*-------- DEBUG LOG --------*/
1817                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1818                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1819                                                                                         "handle_client_recv() : call check_http_version : "
1820                                                                                         "return_value = %d. thread id : %d.");
1821                                                                 formatter % check_result % boost::this_thread::get_id();
1822                                                                 putLogDebug(100062, formatter.str(), __FILE__, __LINE__);
1823                                                         }
1824                                                         /*------DEBUG LOG END------*/
1825                                                 }
1826                                                 //check method and version result is CHECK_OK
1827                                                 if (check_result == CHECK_OK) {
1828                                                         //search http header
1829                                                         bret = find_http_header(recv_data.receive_buffer + it->send_offset, data_remain_size, http_header,
1830                                                                                 header_offset, header_offset_len);
1831                                                         /*-------- DEBUG LOG --------*/
1832                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1833                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1834                                                                                         "handle_client_recv() : call find_http_header : "
1835                                                                                         "return_value = %d. thread id : %d.");
1836                                                                 formatter % static_cast<int>(bret) % boost::this_thread::get_id();
1837                                                                 putLogDebug(100063, formatter.str(), __FILE__, __LINE__);
1838                                                         }
1839                                                         /*------DEBUG LOG END------*/
1840                                                         //search http header result is OK
1841                                                         if (bret) {
1842                                                                 //search Content_Length header
1843                                                                 bret = find_http_header(recv_data.receive_buffer + it->send_offset, data_remain_size,
1844                                                                                         content_header, content_length_header_offset, content_length_header_len);
1845                                                                 /*-------- DEBUG LOG --------*/
1846                                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1847                                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1848                                                                                                 "handle_client_recv() : call find_http_header : "
1849                                                                                                 "return_value = %d. thread id : %d.");
1850                                                                         formatter % static_cast<int>(bret) % boost::this_thread::get_id();
1851                                                                         putLogDebug(100064, formatter.str(), __FILE__, __LINE__);
1852                                                                 }
1853                                                                 /*------DEBUG LOG END------*/
1854                                                                 //search Content_Length result is OK
1855                                                                 if (bret) {
1856                                                                         //Get Content_Length header's numeric value
1857                                                                         for (pos = 0; recv_data.receive_buffer[it->send_offset + content_length_header_offset + pos] != ':' && pos
1858                                                                              < content_length_header_len; ++pos)
1859                                                                                 ;
1860                                                                         if (pos == content_length_header_len) {
1861                                                                                 throw std::string("Content_Length field's value is invalid.");
1862                                                                         }
1863
1864                                                                         ++pos;
1865
1866                                                                         str_value.assign(recv_data.receive_buffer + it->send_offset + content_length_header_offset + pos,
1867                                                                                          content_length_header_len - pos);
1868
1869                                                                         size_t pos_end = str_value.find_last_of('\r');
1870                                                                         if (pos_end != std::string::npos) {
1871                                                                                 str_value = str_value.erase(pos_end);
1872                                                                         }
1873
1874                                                                         for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos);
1875
1876                                                                         str_value = str_value.substr(pos);
1877
1878                                                                         try {
1879                                                                                 content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
1880                                                                         } catch (const boost::bad_lexical_cast &ex) {
1881                                                                                 throw std::string("Content_Length field's value is invalid.");
1882                                                                         }
1883
1884                                                                         //send_rest_size recalc
1885                                                                         //set whole http header's length + Content_Length's value
1886                                                                         it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
1887                                                                 }
1888                                                                 //search Content_Length result is NG
1889                                                                 else {
1890                                                                         //send_rest_size recalc
1891                                                                         //set whole http header's length
1892                                                                         if (header_offset_len == 0) {
1893                                                                                 it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
1894                                                                         } else {
1895                                                                                 it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
1896                                                                         }
1897                                                                 }
1898
1899                                                                 //increment http statistics
1900                                                                 increment_stats(recv_data.receive_buffer + it->send_offset);
1901                                                                 /*-------- DEBUG LOG --------*/
1902                                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1903                                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1904                                                                                                 "handle_client_recv() : call increment_stats : thread id : %d.");
1905                                                                         formatter % boost::this_thread::get_id();
1906                                                                         putLogDebug(100263, formatter.str(), __FILE__, __LINE__);
1907                                                                 }
1908                                                                 /*------DEBUG LOG END------*/
1909
1910                                                                 //set edit_division flag on
1911                                                                 it->edit_division = EDIT_DIVISION_EDIT;
1912                                                         }
1913                                                         //search http header result is NG
1914                                                         else {
1915                                                                 //unsend_size recalc
1916                                                                 it->unsend_size += request_data_remain_size;
1917                                                                 //request data rest size recalc
1918                                                                 request_data_remain_size = 0;
1919                                                                 break;
1920                                                         }
1921                                                 }
1922                                                 //check method and version result is CHECK_NG
1923                                                 else if (check_result == CHECK_NG) {
1924                                                         //set edit_division flag off
1925                                                         it->edit_division = EDIT_DIVISION_NO_EDIT;
1926                                                         //send_rest_size recalc
1927                                                         it->send_rest_size = it->unsend_size + request_data_remain_size;
1928                                                 }
1929                                                 //check method and version result is CHECK_IMPOSSIBLE
1930                                                 else {
1931                                                         //unsend_size recalc
1932                                                         it->unsend_size += request_data_remain_size;
1933                                                         //request data rest size recalc
1934                                                         request_data_remain_size = 0;
1935                                                         break;
1936                                                 }
1937                                         } else {
1938                                                 //set edit_division flag off
1939                                                 it->edit_division = EDIT_DIVISION_NO_EDIT;
1940                                                 //send_rest_size recalc
1941                                                 it->send_rest_size = it->unsend_size + request_data_remain_size;
1942                                         }
1943
1944                                         //recalc fields value according to send_rest_size and request rest size
1945                                         if (it->send_rest_size > it->unsend_size + request_data_remain_size) {
1946                                                 it->send_possible_size = it->unsend_size + request_data_remain_size;
1947                                                 it->send_rest_size -= (it->unsend_size + request_data_remain_size);
1948                                                 it->send_end_size = 0;
1949                                                 it->unsend_size = 0;
1950                                                 request_data_remain_size = 0;
1951                                         } else {
1952                                                 it->send_possible_size = it->send_rest_size;
1953                                                 request_data_remain_size = it->unsend_size + request_data_remain_size - it->send_rest_size;
1954                                                 it->send_end_size = 0;
1955                                                 it->unsend_size = 0;
1956                                                 it->send_rest_size = 0;
1957                                         }
1958
1959                                         //change status from SEND_NG to SEND_OK
1960                                         it->status = SEND_OK;
1961                                 }
1962                                 //no request rest data to process
1963                                 if (request_data_remain_size <= 0) {
1964                                         break;
1965                                 }
1966                         }
1967                         /*-------- DEBUG LOG --------*/
1968                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1969                                 std::string datadump;
1970                                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
1971                                                         "send_rest_size = %d, send_possible_size = %d, "
1972                                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
1973                                 int i = 0;
1974                                 for (it = recv_data.send_status_list.begin();
1975                                      it != recv_data.send_status_list.end();
1976                                      ++it, ++i) {
1977                                         formatter % i % it->status % it->send_end_size
1978                                         % it->send_rest_size % it->send_possible_size
1979                                         % it->send_offset % it->unsend_size % it->edit_division;
1980                                         datadump += formatter.str();
1981                                 }
1982
1983                                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
1984                                                 "handle_client_recv() : send status list dump : send status list size = %d.%s");
1985
1986                                 formatter % recv_data.send_status_list.size() % datadump;
1987                                 putLogDebug(100065, formatter.str(), __FILE__, __LINE__);
1988                         }
1989                         /*------DEBUG LOG END------*/
1990                         //there are still rest data need to process
1991                         //new status created and add to status list
1992                         while (request_data_remain_size > 0) {
1993                                 //new status created
1994                                 send_status new_send_state;
1995
1996                                 new_send_state.edit_division = EDIT_DIVISION_NO_EDIT;
1997                                 new_send_state.send_end_size = 0;
1998                                 new_send_state.send_offset = 0;
1999                                 new_send_state.send_possible_size = 0;
2000                                 new_send_state.unsend_size = 0;
2001                                 new_send_state.send_rest_size = 0;
2002                                 //status initialize to SEND_NG
2003                                 new_send_state.status = SEND_NG;
2004                                 //add new status to status_list
2005                                 recv_data.send_status_list.push_back(new_send_state);
2006                                 std::list<send_status>::reverse_iterator new_send_it = recv_data.send_status_list.rbegin();
2007                                 //calc offset
2008                                 new_send_it->send_offset = recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size
2009                                                            - request_data_remain_size;
2010
2011                                 if (statistic == COLLECT_STATS_ON || forwarded_for == FORWARDED_FOR_ON || session_data->sorry_flag == SORRY_FLAG_ON) {
2012                                         //check http method
2013                                         check_result = check_http_method(recv_data.receive_buffer + new_send_it->send_offset,
2014                                                                          request_data_remain_size);
2015                                         /*-------- DEBUG LOG --------*/
2016                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2017                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2018                                                                         "handle_client_recv() : call check_http_method : "
2019                                                                         "return_value = %d. thread id : %d.");
2020                                                 formatter % check_result % boost::this_thread::get_id();
2021                                                 putLogDebug(100066, formatter.str(), __FILE__, __LINE__);
2022                                         }
2023                                         /*------DEBUG LOG END------*/
2024                                         //check http method result is CHECK_OK
2025                                         if (check_result == CHECK_OK) {
2026                                                 //check http version
2027                                                 check_result = check_http_version(recv_data.receive_buffer + new_send_it->send_offset,
2028                                                                                   request_data_remain_size);
2029                                                 /*-------- DEBUG LOG --------*/
2030                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2031                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2032                                                                                 "handle_client_recv() : call check_http_version : "
2033                                                                                 "return_value = %d. thread id : %d.");
2034                                                         formatter % check_result % boost::this_thread::get_id();
2035                                                         putLogDebug(100067, formatter.str(), __FILE__, __LINE__);
2036                                                 }
2037                                                 /*------DEBUG LOG END------*/
2038                                         }
2039                                         //check http method and version result is CHECK_OK
2040                                         if (check_result == CHECK_OK) {
2041                                                 //search whole http header, get whole http header's offset and length
2042                                                 bret = find_http_header(recv_data.receive_buffer + new_send_it->send_offset, request_data_remain_size,
2043                                                                         http_header, header_offset, header_offset_len);
2044                                                 /*-------- DEBUG LOG --------*/
2045                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2046                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2047                                                                                 "handle_client_recv() : call find_http_header : "
2048                                                                                 "return_value = %d. thread id : %d.");
2049                                                         formatter % check_result % boost::this_thread::get_id();
2050                                                         putLogDebug(100068, formatter.str(), __FILE__, __LINE__);
2051                                                 }
2052                                                 /*------DEBUG LOG END------*/
2053                                                 //searched whole http header
2054                                                 if (bret) {
2055                                                         //search ContentLength http header, get ContentLength header's offset and length
2056                                                         bret = find_http_header(recv_data.receive_buffer + new_send_it->send_offset,
2057                                                                                 request_data_remain_size, content_header, content_length_header_offset, content_length_header_len);
2058                                                         /*-------- DEBUG LOG --------*/
2059                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2060                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2061                                                                                         "handle_client_recv() : call find_http_header : "
2062                                                                                         "return_value = %d. thread id : %d.");
2063                                                                 formatter % static_cast<int>(bret) % boost::this_thread::get_id();
2064                                                                 putLogDebug(100069, formatter.str(), __FILE__, __LINE__);
2065                                                         }
2066                                                         /*------DEBUG LOG END------*/
2067
2068                                                         //searched ContentLength http header
2069                                                         if (bret) {
2070                                                                 //Get Content_Length header's numeric value
2071                                                                 for (pos = 0;
2072                                                                      recv_data.receive_buffer[new_send_it->send_offset + content_length_header_offset + pos] != ':'
2073                                                                      && pos < content_length_header_len;
2074                                                                      ++pos);
2075                                                                 if (pos == content_length_header_len) {
2076                                                                         throw std::string("Content_Length field's value is invalid.");
2077                                                                 }
2078
2079                                                                 ++pos;
2080
2081                                                                 str_value.assign(recv_data.receive_buffer + new_send_it->send_offset + content_length_header_offset + pos,
2082                                                                                  content_length_header_len - pos);
2083
2084                                                                 size_t pos_end = str_value.find_last_of('\r');
2085                                                                 if (pos_end != std::string::npos) {
2086                                                                         str_value = str_value.erase(pos_end);
2087                                                                 }
2088
2089                                                                 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos);
2090
2091                                                                 str_value = str_value.substr(pos);
2092                                                                 try {
2093                                                                         content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
2094                                                                 } catch (const boost::bad_lexical_cast &ex) {
2095                                                                         throw std::string("Content_Length field's value is invalid.");
2096                                                                 }
2097                                                                 //send_rest_size recalc
2098                                                                 //set whole http header's  + whole http header's length + Content_Length's value
2099                                                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
2100                                                         }
2101                                                         //not searched ContentLength http header
2102                                                         else {
2103                                                                 //send_rest_size recalc
2104                                                                 //set whole http header's  + whole http header's length
2105                                                                 if (header_offset_len == 0) {
2106                                                                         new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
2107                                                                 } else {
2108                                                                         new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
2109                                                                 }
2110
2111                                                         }
2112
2113                                                         //increment http statistics
2114                                                         increment_stats(recv_data.receive_buffer + new_send_it->send_offset);
2115                                                         /*-------- DEBUG LOG --------*/
2116                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2117                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2118                                                                                         "handle_client_recv() : call increment_stats : thread id : %d.");
2119                                                                 formatter % boost::this_thread::get_id();
2120                                                                 putLogDebug(100264, formatter.str(), __FILE__, __LINE__);
2121                                                         }
2122                                                         /*------DEBUG LOG END------*/
2123
2124                                                         //set edit_division flag on
2125                                                         new_send_it->edit_division = EDIT_DIVISION_EDIT;
2126                                                 }
2127                                                 //not searched whole http header
2128                                                 else {
2129                                                         new_send_it->unsend_size = request_data_remain_size;
2130                                                         request_data_remain_size = 0;
2131                                                         break;
2132                                                 }
2133                                         }
2134                                         //check http method or version result is CHECK_NG
2135                                         else if (check_result == CHECK_NG) {
2136                                                 new_send_it->edit_division = EDIT_DIVISION_NO_EDIT;
2137                                                 new_send_it->send_rest_size = request_data_remain_size;
2138                                         }
2139
2140                                         //check http method or version result is CHECK_IMPOSSIBLE
2141                                         else {
2142                                                 new_send_it->unsend_size = request_data_remain_size;
2143                                                 request_data_remain_size = 0;
2144                                                 break;
2145                                         }
2146                                 } else {
2147                                         new_send_it->edit_division = EDIT_DIVISION_NO_EDIT;
2148                                         new_send_it->send_rest_size = request_data_remain_size;
2149                                 }
2150
2151                                 //recalc fields value according to send_rest_size and request rest size
2152                                 if (new_send_it->send_rest_size > request_data_remain_size) {
2153                                         new_send_it->send_possible_size = request_data_remain_size;
2154                                         new_send_it->send_rest_size -= request_data_remain_size;
2155                                         new_send_it->send_end_size = 0;
2156                                         request_data_remain_size = 0;
2157                                 } else {
2158                                         new_send_it->send_possible_size = new_send_it->send_rest_size;
2159                                         request_data_remain_size -= new_send_it->send_rest_size;
2160                                         new_send_it->send_end_size = 0;
2161                                         new_send_it->send_rest_size = 0;
2162                                 }
2163
2164                                 //change status from SEND_NG to SEND_OK
2165                                 new_send_it->status = SEND_OK;
2166                         }
2167
2168                         /*-------- DEBUG LOG --------*/
2169                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2170                                 std::string datadump;
2171                                 boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
2172                                                         "send_rest_size = %d, send_possible_size = %d, "
2173                                                         "send_offset = %d, unsend_size = %d, edit_division = %d.");
2174                                 int i = 0;
2175                                 for (it = recv_data.send_status_list.begin();
2176                                      it != recv_data.send_status_list.end();
2177                                      ++it, ++i) {
2178                                         formatter % i % it->status % it->send_end_size
2179                                         % it->send_rest_size % it->send_possible_size
2180                                         % it->send_offset % it->unsend_size % it->edit_division;
2181                                         datadump += formatter.str();
2182                                 }
2183
2184                                 formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2185                                                 "handle_client_recv() : send status list dump : send status list size = %d.%s");
2186
2187                                 formatter % recv_data.send_status_list.size() % datadump;
2188                                 putLogDebug(100070, formatter.str(), __FILE__, __LINE__);
2189                         }
2190                         /*------DEBUG LOG END------*/
2191                         //search for send_possible item in status list
2192                         send_status_it it_find = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
2193                                                          data_send_possible());
2194                         //the data that can be sent possible is exist
2195                         if (it_find != recv_data.send_status_list.end()) {
2196                                 //sorry flag is on
2197                                 if (session_data->sorry_flag == SORRY_FLAG_ON) {
2198                                         status = SORRYSERVER_CONNECT;
2199                                 }
2200                                 //sorry flag is off
2201                                 else {
2202                                         status = REALSERVER_CONNECT;
2203                                 }
2204                         }
2205                         //the data that can be sent possible is not exist
2206                         else {
2207                                 status = CLIENT_RECV;
2208                         }
2209                 }
2210         } catch (int e) {
2211                 /*-------- DEBUG LOG --------*/
2212                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2213                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2214                                                 "handle_client_recv() : catch exception e = %d. thread id : %d.");
2215                         formatter % e % boost::this_thread::get_id();
2216                         putLogDebug(100071, formatter.str(), __FILE__, __LINE__);
2217                 }
2218                 /*------DEBUG LOG END------*/
2219                 status = FINALIZE;
2220         } catch (const std::string &ex) {
2221                 std::cerr << "protocol_module_sessionless::handle_client_recv() : exception : " << ex << std::endl;
2222                 boost::format formatter("protocol_module_sessionless::handle_client_recv() : exception :  %s. thread id : %d.");
2223                 formatter % ex.c_str() % boost::this_thread::get_id();
2224                 putLogError(100033, formatter.str(), __FILE__, __LINE__);
2225                 status = FINALIZE;
2226         } catch (const std::bad_alloc &) {
2227                 std::cerr << "protocol_module_sessionless::handle_client_recv() : exception : Could not allocate memory." << std::endl;
2228                 boost::format formatter("Could not allocate memory. thread id : %d.");
2229                 formatter % boost::this_thread::get_id();
2230                 putLogError(100034, formatter.str(), __FILE__, __LINE__);
2231                 status = FINALIZE;
2232         } catch (const std::exception &ex) {
2233                 std::cerr << "protocol_module_sessionless::handle_client_recv() : exception : error = " << ex.what() << "." << std::endl;
2234                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2235                                         "handle_client_recv() : exception : error = %s. thread id : %d.");
2236                 formatter % ex.what() % boost::this_thread::get_id();
2237                 putLogError(100035, formatter.str(), __FILE__, __LINE__);
2238
2239                 status = FINALIZE;
2240         } catch (...) {
2241                 std::cerr << "protocol_module_sessionless::handle_client_recv() : Unknown exception." << std::endl;
2242                 boost::format formatter("function : protocol_module_base::EVENT_TAG "
2243                                         "protocol_module_sessionless::handle_client_recv() : "
2244                                         "Unknown exception. thread id : %d.");
2245                 formatter % boost::this_thread::get_id();
2246                 putLogError(100036, formatter.str(), __FILE__, __LINE__);
2247                 status = FINALIZE;
2248         }
2249
2250         /*-------- DEBUG LOG --------*/
2251         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2252                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2253                                         "handle_client_recv(const boost::thread::id thread_id, "
2254                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
2255                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
2256                 formatter % status % boost::this_thread::get_id();
2257                 putLogDebug(100072, formatter.str(), __FILE__, __LINE__);
2258         }
2259         /*------DEBUG LOG END------*/
2260
2261         return status;
2262 }
2263
2264 //! called from after realserver select.use in upstream thread.
2265 //! @param[in]    upstream thread id
2266 //! @param[out]    realserver TCP endpoint
2267 //! @return        session use EVENT mode.
2268 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_select(
2269         const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint &rs_endpoint)
2270 {
2271         /*-------- DEBUG LOG --------*/
2272         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2273                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2274                                         "handle_realserver_select(const boost::thread::id thread_id, "
2275                                         "boost::asio::ip::tcp::endpoint & rs_endpoint) : "
2276                                         "thread_id = %d, rs_endpoint = [%s]:%d.");
2277                 formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
2278                 putLogDebug(100073, formatter.str(), __FILE__, __LINE__);
2279         }
2280         /*------DEBUG LOG END------*/
2281         EVENT_TAG status = FINALIZE;
2282         boost::asio::ip::tcp::endpoint tmp_endpoint;
2283         thread_data_ptr session_data;
2284         session_thread_data_map_it session_thread_it;
2285         session_thread_data_map_it session_thread_it_end;
2286         receive_data_map_it receive_data_it;
2287
2288         if (schedule_tcp.empty()) {
2289                 std::cerr << "protocol_module_sessionless::handle_realserver_select() : Schedule_tcp function is empty." << std::endl;
2290                 boost::format formatter("Schedule_tcp function is empty. thread id : %d.");
2291                 formatter % boost::this_thread::get_id();
2292                 putLogError(100037, formatter.str(), __FILE__, __LINE__);
2293                 /*-------- DEBUG LOG --------*/
2294                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2295                         boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2296                                                 "handle_realserver_select(const boost::thread::id thread_id, "
2297                                                 "boost::asio::ip::tcp::endpoint & rs_endpoint)"
2298                                                 " : return_value = %d. thread id : %d.");
2299                         formatter % FINALIZE % boost::this_thread::get_id();
2300                         putLogDebug(100074, formatter.str(), __FILE__, __LINE__);
2301                 }
2302                 /*------DEBUG LOG END------*/
2303                 return FINALIZE;
2304         }
2305
2306         try {
2307                 {
2308                         boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
2309
2310                         session_thread_it = session_thread_data_map.find(thread_id);
2311                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
2312                                 boost::format formatter("Invalid thread id. thread id : %d.");
2313                                 formatter % boost::this_thread::get_id();
2314                                 putLogError(100038, formatter.str(), __FILE__, __LINE__);
2315                                 throw - 1;
2316                         }
2317
2318                         session_data = session_thread_it->second;
2319                 }
2320
2321                 //call schedule_module's schedule function, get realserver endpoint
2322                 {
2323                         rs_list_scoped_lock scoped_lock(rs_list_lock, rs_list_unlock);
2324                         schedule_tcp(thread_id, rs_list_begin, rs_list_end, rs_list_next, rs_endpoint);
2325                 }
2326
2327                 /*-------- DEBUG LOG --------*/
2328                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2329                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2330                                                 "handle_realserver_select() : call schedule_tcp : "
2331                                                 "rs_endpoint = [%s]:%d. thread id : %d.");
2332                         formatter % rs_endpoint.address().to_string() % rs_endpoint.port() % boost::this_thread::get_id();
2333                         putLogDebug(100075, formatter.str(), __FILE__, __LINE__);
2334                 }
2335                 /*------DEBUG LOG END------*/
2336
2337                 //endpoint decide
2338                 if (rs_endpoint != tmp_endpoint) {
2339                         //save rs endpoint
2340                         session_data->target_endpoint = rs_endpoint;
2341                         status = REALSERVER_CONNECT;
2342                 } else {
2343                         //set end flag on
2344                         session_data->sorry_flag = SORRY_FLAG_ON;
2345                         /*-------- DEBUG LOG --------*/
2346                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2347                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2348                                                         "handle_realserver_select() : SORRY_FLAG_ON. thread id : %d.");
2349                                 formatter % boost::this_thread::get_id();
2350                                 putLogDebug(100076, formatter.str(), __FILE__, __LINE__);
2351                         }
2352                         /*------DEBUG LOG END------*/
2353                         status = SORRYSERVER_SELECT;
2354                 }
2355         } catch (int e) {
2356                 /*-------- DEBUG LOG --------*/
2357                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2358                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2359                                                 "handle_realserver_select() : catch exception e = %d. thread id : %d.");
2360                         formatter % e % boost::this_thread::get_id();
2361                         putLogDebug(100077, formatter.str(), __FILE__, __LINE__);
2362                 }
2363                 status = FINALIZE;
2364                 /*------DEBUG LOG END------*/
2365         } catch (const std::exception &ex) {
2366                 std::cerr << "protocol_module_sessionless::handle_realserver_select() : exception : error = " << ex.what() << "." << std::endl;
2367                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2368                                         "handle_realserver_select() : exception : error = %s. thread id : %d.");
2369                 formatter % ex.what() % boost::this_thread::get_id();
2370                 putLogError(100040, formatter.str(), __FILE__, __LINE__);
2371                 status = FINALIZE;
2372         } catch (...) {
2373                 std::cerr << "protocol_module_sessionless::handle_realserver_select() : Unknown exception." << std::endl;
2374                 boost::format formatter("function : protocol_module_base::EVENT_TAG "
2375                                         "protocol_module_sessionless::handle_realserver_select() : "
2376                                         "Unknown exception. thread id : %d.");
2377                 formatter % boost::this_thread::get_id();
2378                 putLogError(100041, formatter.str(), __FILE__, __LINE__);
2379                 status = FINALIZE;
2380         }
2381
2382         /*-------- DEBUG LOG --------*/
2383         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2384                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2385                                         "handle_realserver_select(const boost::thread::id thread_id, "
2386                                         "boost::asio::ip::tcp::endpoint & rs_endpoint)"
2387                                         " : return_value = %d. thread id : %d.");
2388                 formatter % status % boost::this_thread::get_id();
2389                 putLogDebug(100078, formatter.str(), __FILE__, __LINE__);
2390         }
2391         /*------DEBUG LOG END------*/
2392
2393         return status;
2394 }
2395
2396 //! called from after realserver select
2397 //! @param[in]    upstream thread id
2398 //! @param[out]    realserver UDP endpoint
2399 //! @param[out]    sendbuffer reference
2400 //! @param[out]    send data length
2401 //! @return        session use EVENT mode.
2402 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_select(
2403         const boost::thread::id thread_id, boost::asio::ip::udp::endpoint &rs_endpoint, boost::array < char,
2404         MAX_BUFFER_SIZE > & sendbuffer, size_t &datalen)
2405 {
2406         /*-------- DEBUG LOG --------*/
2407         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2408                 boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2409                                         "handle_realserver_select(const boost::thread::id thread_id, "
2410                                         "boost::asio::ip::udp::endpoint& rs_endpoint, boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
2411                                         "size_t& datalen) : "
2412                                         "return_value = %d. thread id : %d.");
2413                 formatter % STOP % boost::this_thread::get_id();
2414                 putLogDebug(100079, formatter.str(), __FILE__, __LINE__);
2415         }
2416         /*------DEBUG LOG END------*/
2417         return STOP;
2418 }
2419 //! called from after realserver connect
2420 //! @param[in]    upstream thread id
2421 //! @param[out]    sendbuffer reference
2422 //! @param[out]    send data length
2423 //! @return        session use EVENT mode.
2424 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_connect(
2425         const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t &datalen)
2426 {
2427         /*-------- DEBUG LOG --------*/
2428         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2429                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2430                                         "handle_realserver_connect(const boost::thread::id thread_id, "
2431                                         "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : "
2432                                         "thread_id = %d.");
2433                 formatter % thread_id;
2434                 putLogDebug(100080, formatter.str(), __FILE__, __LINE__);
2435         }
2436         /*------DEBUG LOG END------*/
2437
2438         EVENT_TAG status = FINALIZE;
2439         bool ret = false;
2440         size_t header_offset = 0;
2441         size_t header_offset_len = 0;
2442         size_t send_buffer_remian_size = 0;
2443         size_t copy_size = 0;
2444         const int send_buffer_end_size = sendbuffer.max_size();
2445         const std::string http_header = "";
2446         const std::string str_forword_for = "X-Forwarded-For";
2447         thread_data_ptr session_data;
2448
2449         try {
2450                 session_thread_data_map_mutex.lock();
2451
2452                 //thread id check
2453                 session_thread_data_map_it      session_thread_it = session_thread_data_map.find(thread_id);
2454                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
2455                         boost::format formatter("Invalid thread id. thread id : %d.");
2456                         formatter % boost::this_thread::get_id();
2457                         putLogError(100042, formatter.str(), __FILE__, __LINE__);
2458                         session_thread_data_map_mutex.unlock();
2459                         throw - 1;
2460                 }
2461
2462                 session_data = session_thread_it->second;
2463
2464                 //endpoint check
2465                 receive_data_map_it     receive_data_it = session_data->receive_data_map.find(session_data->client_endpoint_tcp);
2466                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
2467                         boost::format formatter("Invalid endpoint. thread id : %d.");
2468                         formatter % boost::this_thread::get_id();
2469                         putLogError(100043, formatter.str(), __FILE__, __LINE__);
2470                         session_thread_data_map_mutex.unlock();
2471                         throw - 1;
2472                 }
2473
2474                 //receive_buffer pointer check
2475                 receive_data &recv_data = receive_data_it->second;
2476                 if (unlikely(recv_data.receive_buffer == NULL)) {
2477                         session_thread_data_map_mutex.unlock();
2478                         return CLIENT_RECV;
2479                 }
2480
2481                 //send list check
2482                 send_status_it it = recv_data.send_status_list.begin();
2483                 send_status_it it_end = recv_data.send_status_list.end();
2484                 it = find_if(it, it_end, data_send_possible());
2485                 if (unlikely(it == it_end)) {
2486                         boost::format formatter("Sending possible data is not existed. thread id : %d.");
2487                         formatter % boost::this_thread::get_id();
2488                         putLogError(100045, formatter.str(), __FILE__, __LINE__);
2489                         session_thread_data_map_mutex.unlock();
2490                         throw - 1;
2491                 }
2492                 session_thread_data_map_mutex.unlock();
2493
2494                 //send buffer rest size initialization
2495                 send_buffer_remian_size = send_buffer_end_size;
2496
2497                 //edit_division flag on
2498                 if (it->edit_division == EDIT_DIVISION_EDIT  && forwarded_for == FORWARDED_FOR_ON) {
2499                         //edit list is empty
2500                         if (it->edit_data_list.empty()) {
2501                                 //edit data create
2502                                 edit_data edata;
2503                                 edata.data_size = 0;
2504                                 edata.insert_posission = 0;
2505                                 edata.replace_size = 0;
2506                                 //search X-Forwarded-For header
2507                                 ret = find_http_header(recv_data.receive_buffer + it->send_offset, it->send_possible_size,
2508                                                        str_forword_for, header_offset, header_offset_len);
2509                                 /*-------- DEBUG LOG --------*/
2510                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2511                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2512                                                                 "handle_realserver_connect() : call find_http_header : "
2513                                                                 "return_value = %d. thread id : %d.");
2514                                         formatter % static_cast<int>(ret) % boost::this_thread::get_id();
2515                                         putLogDebug(100081, formatter.str(), __FILE__, __LINE__);
2516                                 }
2517                                 /*------DEBUG LOG END------*/
2518                                 //search http header result is OK
2519                                 if (ret) {
2520                                         //edit X-Forwarded-For header, set it to edata.data
2521                                         edata.data.assign(recv_data.receive_buffer + it->send_offset + header_offset, header_offset_len);
2522                                         edata.data += ", ";
2523                                         edata.data += session_data->client_endpoint_tcp.address().to_string();
2524                                         //save new X-Forwarded-For header offset
2525                                         edata.insert_posission = header_offset;
2526                                         //save new X-Forwarded-For header length
2527                                         edata.data_size = edata.data.size();
2528                                         //save old X-Forwarded-For header length
2529                                         edata.replace_size = header_offset_len;
2530                                 }
2531                                 //search http header result is NG
2532                                 else {
2533                                         //search whole http header, get whole http header's offset and length
2534                                         ret = find_http_header(recv_data.receive_buffer + it->send_offset, it->send_possible_size, "",
2535                                                                header_offset, header_offset_len);
2536                                         /*-------- DEBUG LOG --------*/
2537                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2538                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2539                                                                         "handle_realserver_connect() : call find_http_header : "
2540                                                                         "return_value = %d. thread id : %d.");
2541                                                 formatter % static_cast<int>(ret) % boost::this_thread::get_id();
2542                                                 putLogDebug(100082, formatter.str(), __FILE__, __LINE__);
2543                                         }
2544                                         /*------DEBUG LOG END------*/
2545                                         if (!ret) {
2546                                                 boost::format formatter("find_http_header() function failure. thread id : %d.");
2547                                                 formatter % boost::this_thread::get_id();
2548                                                 putLogError(100046, formatter.str(), __FILE__, __LINE__);
2549                                                 /*-------- DEBUG LOG --------*/
2550                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2551                                                         boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2552                                                                                 "handle_realserver_connect(const boost::thread::id thread_id, "
2553                                                                                 "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
2554                                                                                 "size_t& datalen) : return_value = %d. thread id : %d.");
2555                                                         formatter % FINALIZE % boost::this_thread::get_id();
2556                                                         putLogDebug(100083, formatter.str(), __FILE__, __LINE__);
2557                                                 }
2558                                                 /*------DEBUG LOG END------*/
2559                                                 return FINALIZE;
2560                                         }
2561                                         //create X-Forwarded-For header, put it to edata.data
2562                                         edata.data = str_forword_for;
2563                                         edata.data += ": ";
2564                                         edata.data += session_data->client_endpoint_tcp.address().to_string();
2565                                         edata.data += "\r\n";
2566                                         //save new X-Forwarded-For header offset
2567                                         edata.insert_posission = header_offset;
2568                                         //save new X-Forwarded-For header length
2569                                         edata.data_size = edata.data.size();
2570                                         //save old X-Forwarded-For header length
2571                                         edata.replace_size = 0;
2572                                 }
2573
2574                                 //add to edit_data_list
2575                                 it->edit_data_list.push_back(edata);
2576                         }
2577
2578                         /*-------- DEBUG LOG --------*/
2579                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2580                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2581                                                         "handle_realserver_connect() : Copy data loop start. thread id : %d.");
2582                                 formatter % boost::this_thread::get_id();
2583                                 putLogDebug(100084, formatter.str(), __FILE__, __LINE__);
2584                         }
2585                         /*------DEBUG LOG END------*/
2586                         while (true) {
2587                                 //edit_data_list is empty
2588                                 if (it->edit_data_list.empty()) {
2589                                         //set edit_division flag on
2590                                         it->edit_division = EDIT_DIVISION_NO_EDIT;
2591
2592                                         if (send_buffer_remian_size > 0 && it->send_possible_size > 0) {
2593                                                 //send_buffer_remain_size is larger
2594                                                 if (send_buffer_remian_size >= it->send_possible_size) {
2595                                                         copy_size = it->send_possible_size;
2596                                                         /*-------- DEBUG LOG --------*/
2597                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2598                                                                 std::string datadump;
2599                                                                 dump_memory(recv_data.receive_buffer + it->send_offset + it->send_end_size,
2600                                                                             it->send_possible_size, datadump);
2601                                                                 boost::format formatter(
2602                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2603                                                                         "handle_realserver_connect() : before memcpy (data dump) : "
2604                                                                         "data begin = %d, data_size = %d, data = %s");
2605                                                                 formatter % (it->send_offset + it->send_end_size)
2606                                                                 % copy_size % datadump;
2607                                                                 putLogDebug(100085, formatter.str(), __FILE__, __LINE__);
2608                                                         }
2609                                                         /*------DEBUG LOG END------*/
2610                                                         //copy data from receive_buffer to sendbuffer by sending_possible size
2611                                                         memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2612                                                                recv_data.receive_buffer + it->send_offset + it->send_end_size,
2613                                                                it->send_possible_size);
2614                                                         /*-------- DEBUG LOG --------*/
2615                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2616                                                                 std::string datadump;
2617                                                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2618                                                                             it->send_possible_size, datadump);
2619
2620                                                                 boost::format formatter(
2621                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2622                                                                         "handle_realserver_connect() : after memcpy (data dump) : "
2623                                                                         "data begin = %d, data_size = %d, data = %s");
2624                                                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
2625                                                                 % copy_size % datadump;
2626                                                                 putLogDebug(100086, formatter.str(), __FILE__, __LINE__);
2627                                                         }
2628                                                         /*------DEBUG LOG END------*/
2629
2630                                                         it->send_end_size += copy_size;
2631                                                         it->send_possible_size = 0;
2632                                                         send_buffer_remian_size -= copy_size;
2633                                                 }
2634                                                 //send_possible_size is larger
2635                                                 else {
2636                                                         copy_size = send_buffer_remian_size;
2637                                                         /*-------- DEBUG LOG --------*/
2638                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2639                                                                 std::string datadump;
2640                                                                 dump_memory(recv_data.receive_buffer + it->send_offset + it->send_end_size,
2641                                                                             copy_size, datadump);
2642
2643                                                                 boost::format formatter(
2644                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2645                                                                         "handle_realserver_connect() : before memcpy (data dump) : "
2646                                                                         "data begin = %d, data_size = %d, data = %s");
2647                                                                 formatter % (it->send_offset + it->send_end_size)
2648                                                                 % copy_size % datadump;
2649                                                                 putLogDebug(100087, formatter.str(), __FILE__, __LINE__);
2650                                                         }
2651                                                         /*------DEBUG LOG END------*/
2652                                                         //copy data from receive_buffer to sendbuffer by send buffer rest size
2653                                                         memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2654                                                                recv_data.receive_buffer + it->send_offset + it->send_end_size, copy_size);
2655                                                         /*-------- DEBUG LOG --------*/
2656                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2657                                                                 std::string datadump;
2658                                                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2659                                                                             copy_size, datadump);
2660
2661                                                                 boost::format formatter(
2662                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2663                                                                         "handle_realserver_connect() : after memcpy (data dump) : "
2664                                                                         "data begin = %d, data_size = %d, data = %s");
2665                                                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
2666                                                                 % copy_size % datadump;
2667                                                                 putLogDebug(100088, formatter.str(), __FILE__, __LINE__);
2668                                                         }
2669                                                         /*------DEBUG LOG END------*/
2670                                                         it->send_end_size += copy_size;
2671                                                         it->send_possible_size -= copy_size;
2672                                                         send_buffer_remian_size = 0;
2673                                                 }
2674                                         }
2675                                         break;
2676                                 }
2677                                 //edit_data_list is not empty
2678                                 else {
2679                                         //find the item in the list which has minimum insert_position
2680                                         std::list<edit_data>::iterator edit_min = std::min_element(it->edit_data_list.begin(),
2681                                                         it->edit_data_list.end());
2682                                         //send_buffer_remain_size is larger than data that before X-Forwarded-For
2683                                         if (send_buffer_remian_size >= edit_min->insert_posission - it->send_end_size) {
2684                                                 //copy data before X-Forwarded-For
2685                                                 copy_size = edit_min->insert_posission - it->send_end_size;
2686                                                 /*-------- DEBUG LOG --------*/
2687                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2688                                                         std::string datadump;
2689                                                         dump_memory(recv_data.receive_buffer + it->send_offset + it->send_end_size,
2690                                                                     copy_size, datadump);
2691
2692                                                         boost::format formatter(
2693                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2694                                                                 "handle_realserver_connect() : before memcpy (data dump) : "
2695                                                                 "data begin = %d, data_size = %d, data = %s");
2696                                                         formatter % (it->send_offset + it->send_end_size)
2697                                                         % copy_size % datadump;
2698                                                         putLogDebug(100089, formatter.str(), __FILE__, __LINE__);
2699                                                 }
2700                                                 /*------DEBUG LOG END------*/
2701                                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2702                                                        recv_data.receive_buffer + it->send_offset + it->send_end_size, copy_size);
2703                                                 /*-------- DEBUG LOG --------*/
2704                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2705                                                         std::string datadump;
2706                                                         dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2707                                                                     copy_size, datadump);
2708                                                         boost::format formatter(
2709                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2710                                                                 "handle_realserver_connect() : after memcpy (data dump) : "
2711                                                                 "data begin = %d, data_size = %d, data = %s");
2712                                                         formatter % (send_buffer_end_size - send_buffer_remian_size)
2713                                                         % copy_size % datadump;
2714                                                         putLogDebug(100090, formatter.str(), __FILE__, __LINE__);
2715                                                 }
2716                                                 /*------DEBUG LOG END------*/
2717                                                 it->send_end_size += copy_size;
2718                                                 it->send_possible_size -= copy_size;
2719                                                 send_buffer_remian_size -= copy_size;
2720
2721                                                 //there is remain buffer for copy X-Forwarded-For
2722                                                 if (send_buffer_remian_size >= edit_min->data_size) {
2723                                                         /*-------- DEBUG LOG --------*/
2724                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2725                                                                 std::string datadump;
2726                                                                 dump_memory(edit_min->data.c_str(),
2727                                                                             edit_min->data_size, datadump);
2728
2729                                                                 boost::format formatter(
2730                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2731                                                                         "handle_realserver_connect() : before memcpy (data dump) : "
2732                                                                         "data begin = 0, data_size = %d, data = %s");
2733                                                                 formatter % edit_min->data_size % datadump;
2734                                                                 putLogDebug(100091, formatter.str(), __FILE__, __LINE__);
2735                                                         }
2736                                                         /*------DEBUG LOG END------*/
2737                                                         //copy  X-Forwarded-For
2738                                                         memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2739                                                                edit_min->data.c_str(), edit_min->data_size);
2740                                                         /*-------- DEBUG LOG --------*/
2741                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2742                                                                 std::string datadump;
2743                                                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2744                                                                             edit_min->data_size, datadump);
2745                                                                 boost::format formatter(
2746                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2747                                                                         "handle_realserver_connect() : after memcpy (data dump) : "
2748                                                                         "data begin = %d, data_size = %d, data = %s");
2749                                                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
2750                                                                 % edit_min->data_size % datadump;
2751                                                                 putLogDebug(100092, formatter.str(), __FILE__, __LINE__);
2752                                                         }
2753                                                         /*------DEBUG LOG END------*/
2754                                                         it->send_end_size += edit_min->replace_size;
2755                                                         it->send_possible_size -= edit_min->replace_size;
2756                                                         send_buffer_remian_size -= edit_min->data_size;
2757                                                         it->edit_data_list.erase(edit_min);
2758                                                 }
2759                                                 //
2760                                                 else {
2761                                                         break;
2762                                                 }
2763                                         }
2764                                         //data that before X-Forwarded-For is larger than send_buffer_remain_size
2765                                         else {
2766                                                 copy_size = send_buffer_remian_size;
2767                                                 /*-------- DEBUG LOG --------*/
2768                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2769                                                         std::string datadump;
2770                                                         dump_memory(recv_data.receive_buffer + it->send_offset + it->send_end_size,
2771                                                                     copy_size, datadump);
2772
2773                                                         boost::format formatter(
2774                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2775                                                                 "handle_realserver_connect() : before memcpy (data dump) : "
2776                                                                 "data begin = %d, data_size = %d, data = %s");
2777                                                         formatter % (it->send_offset + it->send_end_size)
2778                                                         % copy_size % datadump;
2779                                                         putLogDebug(100093, formatter.str(), __FILE__, __LINE__);
2780                                                 }
2781                                                 /*------DEBUG LOG END------*/
2782                                                 //copy data as large as possible
2783                                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2784                                                        recv_data.receive_buffer + it->send_offset + it->send_end_size, copy_size);
2785                                                 /*-------- DEBUG LOG --------*/
2786                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2787                                                         std::string datadump;
2788                                                         dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
2789                                                                     copy_size, datadump);
2790
2791                                                         boost::format formatter(
2792                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2793                                                                 "handle_realserver_connect() : after memcpy (data dump) : "
2794                                                                 "data begin = %d, data_size = %d, data = %s");
2795                                                         formatter % (send_buffer_end_size - send_buffer_remian_size)
2796                                                         % copy_size % datadump;
2797                                                         putLogDebug(100094, formatter.str(), __FILE__, __LINE__);
2798                                                 }
2799                                                 /*------DEBUG LOG END------*/
2800
2801                                                 it->send_end_size += copy_size;
2802                                                 it->send_possible_size -= copy_size;
2803                                                 send_buffer_remian_size -= copy_size;
2804                                                 break;
2805                                         }
2806                                 }
2807                         }
2808                         /*-------- DEBUG LOG --------*/
2809                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2810                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2811                                                         "handle_realserver_connect() : Copy data loop end. thread id : %d.");
2812                                 formatter % boost::this_thread::get_id();
2813                                 putLogDebug(100095, formatter.str(), __FILE__, __LINE__);
2814                         }
2815                         /*------DEBUG LOG END------*/
2816                 }
2817                 //edit_division flag is off
2818                 else {
2819                         //copy data as large as possible
2820                         //send_possible_size is larger
2821                         if (send_buffer_remian_size >= it->send_possible_size) {
2822                                 copy_size = it->send_possible_size;
2823                                 /*-------- DEBUG LOG --------*/
2824                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2825                                         std::string datadump;
2826                                         dump_memory(recv_data.receive_buffer + it->send_offset,
2827                                                     copy_size, datadump);
2828                                         boost::format formatter(
2829                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2830                                                 "handle_realserver_connect() : before memcpy (data dump) : "
2831                                                 "data begin = %d, data_size = %d, data = %s");
2832                                         formatter % it->send_offset
2833                                         % copy_size % datadump;
2834                                         putLogDebug(100096, formatter.str(), __FILE__, __LINE__);
2835                                 }
2836                                 /*------DEBUG LOG END------*/
2837                                 //copy data by send_possible size
2838                                 memcpy(sendbuffer.data(), recv_data.receive_buffer + it->send_offset, copy_size);
2839                                 /*-------- DEBUG LOG --------*/
2840                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2841                                         std::string datadump;
2842                                         dump_memory(sendbuffer.data(), copy_size, datadump);
2843                                         boost::format formatter(
2844                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2845                                                 "handle_realserver_connect() : after memcpy (data dump) : "
2846                                                 "data begin = 0, data_size = %d, data = %s");
2847                                         formatter % copy_size % datadump;
2848                                         putLogDebug(100097, formatter.str(), __FILE__, __LINE__);
2849                                 }
2850                                 /*------DEBUG LOG END------*/
2851                                 it->send_end_size = copy_size;
2852                                 it->send_possible_size = 0;
2853                                 send_buffer_remian_size -= copy_size;
2854                         }
2855                         //buffer rest size is larger
2856                         else {
2857                                 /*-------- DEBUG LOG --------*/
2858                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2859                                         std::string datadump;
2860                                         dump_memory(recv_data.receive_buffer + it->send_offset, send_buffer_remian_size, datadump);
2861
2862                                         boost::format formatter(
2863                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2864                                                 "handle_realserver_connect() : before memcpy (data dump) : "
2865                                                 "data begin = %d, data_size = %d, data = %s");
2866                                         formatter % it->send_offset
2867                                         % send_buffer_remian_size % datadump;
2868                                         putLogDebug(100098, formatter.str(), __FILE__, __LINE__);
2869                                 }
2870                                 /*------DEBUG LOG END------*/
2871                                 //copy data by buffer rest size
2872                                 memcpy(sendbuffer.data(), recv_data.receive_buffer + it->send_offset, send_buffer_remian_size);
2873                                 /*-------- DEBUG LOG --------*/
2874                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2875                                         std::string datadump;
2876                                         dump_memory(sendbuffer.data(), send_buffer_remian_size, datadump);
2877                                         boost::format formatter(
2878                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2879                                                 "handle_realserver_connect() : after memcpy (data dump) : "
2880                                                 "data begin = 0, data_size = %d, data = %s");
2881                                         formatter % send_buffer_remian_size % datadump;
2882                                         putLogDebug(100099, formatter.str(), __FILE__, __LINE__);
2883                                 }
2884                                 /*------DEBUG LOG END------*/
2885                                 it->send_end_size = send_buffer_remian_size;
2886                                 it->send_possible_size -= send_buffer_remian_size;
2887                                 send_buffer_remian_size = 0;
2888                         }
2889                 }
2890
2891                 //set copied data length
2892                 datalen = send_buffer_end_size - send_buffer_remian_size;
2893                 status = REALSERVER_SEND;
2894         } catch (int e) {
2895                 /*-------- DEBUG LOG --------*/
2896                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2897                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2898                                                 "handle_realserver_connect() : catch exception e = %d. thread id : %d.");
2899                         formatter % e % boost::this_thread::get_id();
2900                         putLogDebug(100100, formatter.str(), __FILE__, __LINE__);
2901                 }
2902                 status = FINALIZE;
2903                 /*------DEBUG LOG END------*/
2904         } catch (const std::exception &ex) {
2905                 std::cerr << "protocol_module_sessionless::handle_realserver_connect() : exception : error = " << ex.what() << "." << std::endl;
2906                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2907                                         "handle_realserver_connect() : exception : error = %s. thread id : %d.");
2908                 formatter % ex.what() % boost::this_thread::get_id();
2909                 putLogError(100047, formatter.str(), __FILE__, __LINE__);
2910                 status = FINALIZE;
2911         } catch (...) {
2912                 std::cerr << "protocol_module_sessionless::handle_realserver_connect() : Unknown exception." << std::endl;
2913                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2914                                         "handle_realserver_connect() : Unknown exception. thread id : %d.");
2915                 formatter % boost::this_thread::get_id();
2916                 putLogError(100048, formatter.str(), __FILE__, __LINE__);
2917                 status = FINALIZE;
2918         }
2919
2920         /*-------- DEBUG LOG --------*/
2921         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2922                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2923                                         "handle_realserver_connect(const boost::thread::id thread_id, "
2924                                         "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
2925                                         "size_t& datalen) : return_value = %d. thread id : %d.");
2926                 formatter % status % boost::this_thread::get_id();
2927                 putLogDebug(100101, formatter.str(), __FILE__, __LINE__);
2928         }
2929         /*------DEBUG LOG END------*/
2930
2931         return status;
2932 }
2933
2934 //! called from after realserver connection fail
2935 //! @param[in]    upstream thread id
2936 //! @param[in]    fail realserver endpoint reference
2937 //! @return        session use EVENT mode.
2938 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_connection_fail(
2939         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &rs_endpoint)
2940 {
2941         /*-------- DEBUG LOG --------*/
2942         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2943                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2944                                         "handle_realserver_connection_fail(const boost::thread::id thread_id, "
2945                                         "const boost::asio::ip::tcp::endpoint & rs_endpoint) : "
2946                                         "thread_id = %d, rs_endpoint = [%s]:%d.");
2947                 formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
2948                 putLogDebug(100102, formatter.str(), __FILE__, __LINE__);
2949         }
2950         /*------DEBUG LOG END------*/
2951
2952         EVENT_TAG status = FINALIZE;
2953         thread_data_ptr session_data;
2954         session_thread_data_map_it session_thread_it;
2955
2956         try {
2957                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
2958
2959                 session_thread_it = session_thread_data_map.find(thread_id);
2960                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
2961                         boost::format formatter("Invalid thread id. thread id : %d.");
2962                         formatter % boost::this_thread::get_id();
2963                         putLogError(100049, formatter.str(), __FILE__, __LINE__);
2964                         throw - 1;
2965                 }
2966
2967                 session_data = session_thread_it->second;
2968
2969                 //set end flag ON
2970                 session_data->end_flag = END_FLAG_ON;
2971                 /*-------- DEBUG LOG --------*/
2972                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2973                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2974                                                 "handle_realserver_connection_fail() : END_FLAG_ON. thread id : %d.");
2975                         formatter % boost::this_thread::get_id();
2976                         putLogDebug(100103, formatter.str(), __FILE__, __LINE__);
2977                 }
2978                 /*------DEBUG LOG END------*/
2979                 status = CLIENT_DISCONNECT;
2980         } catch (int e) {
2981                 /*-------- DEBUG LOG --------*/
2982                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
2983                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2984                                                 "handle_realserver_connection_fail() : catch exception e = %d. thread id : %d.");
2985                         formatter % e % boost::this_thread::get_id();
2986                         putLogDebug(100104, formatter.str(), __FILE__, __LINE__);
2987                 }
2988                 status = FINALIZE;
2989                 /*------DEBUG LOG END------*/
2990         } catch (std::exception &ex) {
2991                 std::cerr << "protocol_module_sessionless::handle_realserver_connection_fail() : exception : error = " << ex.what() << "." << std::endl;
2992                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
2993                                         "handle_realserver_connection_fail() : exception : error = %s. thread id : %d.");
2994                 formatter % ex.what() % boost::this_thread::get_id();
2995                 putLogError(100050, formatter.str(), __FILE__, __LINE__);
2996                 status = FINALIZE;
2997         } catch (...) {
2998                 std::cerr << "protocol_module_sessionless::handle_realserver_connection_fail() : Unknown exception." << std::endl;
2999                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3000                                         "handle_realserver_connection_fail() : Unknown exception. thread id : %d.");
3001                 formatter % boost::this_thread::get_id();
3002                 putLogError(100051, formatter.str(), __FILE__, __LINE__);
3003                 status = FINALIZE;
3004         }
3005
3006         /*-------- DEBUG LOG --------*/
3007         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3008                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3009                                         "handle_realserver_connection_fail(const boost::thread::id thread_id, "
3010                                         "const boost::asio::ip::tcp::endpoint & rs_endpoint) : return_value = %d. thread id : %d.");
3011                 formatter % status % boost::this_thread::get_id();
3012                 putLogDebug(100105, formatter.str(), __FILE__, __LINE__);
3013         }
3014         /*------DEBUG LOG END------*/
3015         return status;
3016 }
3017 //! called from after realserver send.
3018 //! @param[in]    upstream thread id
3019 //! @return        session use EVENT mode.
3020 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_send(
3021         const boost::thread::id thread_id)
3022 {
3023         /*-------- DEBUG LOG --------*/
3024         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3025                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3026                                         "handle_realserver_send(const boost::thread::id thread_id) : "
3027                                         "thread_id = %d.");
3028                 formatter % thread_id;
3029                 putLogDebug(100106, formatter.str(), __FILE__, __LINE__);
3030         }
3031         /*------DEBUG LOG END------*/
3032         EVENT_TAG status = FINALIZE;
3033         thread_data_ptr session_data;
3034         session_thread_data_map_it session_thread_it;
3035         receive_data_map_it receive_data_it;
3036
3037         try {
3038                 {
3039                         boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
3040
3041                         //thread_id check
3042                         session_thread_it = session_thread_data_map.find(thread_id);
3043                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
3044                                 boost::format formatter("Invalid thread id. thread id : %d.");
3045                                 formatter % boost::this_thread::get_id();
3046                                 putLogError(100052, formatter.str(), __FILE__, __LINE__);
3047                                 throw - 1;
3048                         }
3049
3050                         session_data = session_thread_it->second;
3051                 }
3052
3053                 //endpoint check
3054                 receive_data_it = session_data->receive_data_map.find(session_data->client_endpoint_tcp);
3055                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
3056                         boost::format formatter("Invalid endpoint. thread id : %d.");
3057                         formatter % boost::this_thread::get_id();
3058                         putLogError(100053, formatter.str(), __FILE__, __LINE__);
3059                         throw - 1;
3060                 }
3061
3062                 receive_data &recv_data = receive_data_it->second;
3063
3064                 send_status_it it = recv_data.send_status_list.begin();
3065                 send_status_it it_end = recv_data.send_status_list.end();
3066
3067                 //status list check
3068                 it = std::adjacent_find(it, it_end, data_send_list_incorrect());
3069                 if (unlikely(it != it_end)) {
3070                         boost::format formatter("Sending possible data is invalid. thread id : %d.");
3071                         formatter % boost::this_thread::get_id();
3072                         putLogError(100054, formatter.str(), __FILE__, __LINE__);
3073                         throw - 1;
3074                 }
3075
3076                 //status list check
3077                 it = recv_data.send_status_list.begin();
3078                 it = std::find_if(it, it_end, data_send_ok());
3079                 if (unlikely(it == it_end)) {
3080                         boost::format formatter("Sending possible data is not existed. thread id : %d.");
3081                         formatter % boost::this_thread::get_id();
3082                         putLogError(100055, formatter.str(), __FILE__, __LINE__);
3083                         throw - 1;
3084                 }
3085
3086                 //sending possible data is exist
3087                 if (it->send_possible_size > 0) {
3088                         //status remain SEND_OK
3089                         it->status = SEND_OK;
3090                         //offset recalc
3091                         it->send_offset += it->send_end_size;
3092
3093                         //insert_position recalc
3094                         for (std::list<edit_data>::iterator list_it = it->edit_data_list.begin(); list_it
3095                              != it->edit_data_list.end(); ++list_it) {
3096                                 list_it->insert_posission -= it->send_end_size;
3097                         }
3098
3099                         //send_end_size recalc
3100                         it->send_end_size = 0;
3101                 }
3102                 //sending possible data is not exist
3103                 else {
3104                         //can receive from client continue
3105                         if (it->send_rest_size > 0) {
3106                                 //change status from SEND_OK to SEND_CONTINUE
3107                                 it->status = SEND_CONTINUE;
3108                         }
3109                         //can not receive from client continue
3110                         else {
3111                                 //change status from SEND_OK to SEND_END
3112                                 it->status = SEND_END;
3113                         }
3114                 }
3115
3116                 it = recv_data.send_status_list.begin();
3117                 it = find_if(it, it_end, data_send_ok());
3118                 //send_ok item is exist
3119                 if (it != it_end) {
3120                         status = REALSERVER_CONNECT;
3121                 }
3122                 //send_ok item is exist
3123                 else {
3124                         status = CLIENT_RECV;
3125                 }
3126
3127         } catch (int e) {
3128                 /*-------- DEBUG LOG --------*/
3129                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3130                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3131                                                 "handle_realserver_send() : catch exception e = %d. thread id : %d.");
3132                         formatter % e % boost::this_thread::get_id();
3133                         putLogDebug(100107, formatter.str(), __FILE__, __LINE__);
3134                 }
3135                 status = FINALIZE;
3136                 /*------DEBUG LOG END------*/
3137         } catch (const std::exception &ex) {
3138                 std::cerr << "protocol_module_sessionless::handle_realserver_send() : exception : error = " << ex.what() << "." << std::endl;
3139                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3140                                         "handle_realserver_send() : exception : error = %s. thread id : %d.");
3141                 formatter % ex.what() % boost::this_thread::get_id();
3142                 putLogError(100056, formatter.str(), __FILE__, __LINE__);
3143
3144                 status = FINALIZE;
3145         } catch (...) {
3146                 std::cerr << "protocol_module_sessionless::handle_realserver_send() : Unknown exception." << std::endl;
3147                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3148                                         "handle_realserver_send() : Unknown exception. thread id : %d.");
3149                 formatter % boost::this_thread::get_id();
3150                 putLogError(100057, formatter.str(), __FILE__, __LINE__);
3151                 status = FINALIZE;
3152         }
3153
3154         /*-------- DEBUG LOG --------*/
3155         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3156                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3157                                         "handle_realserver_send(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
3158                 formatter % status % boost::this_thread::get_id();
3159                 putLogDebug(100108, formatter.str(), __FILE__, __LINE__);
3160         }
3161         /*------DEBUG LOG END------*/
3162
3163         return status;
3164 }
3165
3166 //! called from after sorryserver select
3167 //! @param[in]    upstream thread id
3168 //! @param[in]    sorryserver endpoint reference
3169 //! @return        session use EVENT mode.
3170 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_select(
3171         const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint &sorry_endpoint)
3172 {
3173         /*-------- DEBUG LOG --------*/
3174         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3175                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3176                                         "handle_sorryserver_select(const boost::thread::id thread_id, "
3177                                         "boost::asio::ip::tcp::endpoint& sorry_endpoint) : "
3178                                         "thread_id = %d, sorry_endpoint = [%s]:%d.");
3179                 formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port();
3180                 putLogDebug(100109, formatter.str(), __FILE__, __LINE__);
3181         }
3182         /*------DEBUG LOG END------*/
3183         EVENT_TAG status = FINALIZE;
3184         boost::asio::ip::tcp::endpoint client_endpoint;
3185
3186         thread_data_ptr session_data;
3187         session_thread_data_map_it session_thread_it;
3188         receive_data_map_it receive_data_it;
3189
3190         try {
3191                 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
3192
3193                 session_thread_it = session_thread_data_map.find(thread_id);
3194                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
3195                         boost::format formatter("Invalid thread id. thread id : %d.");
3196                         formatter % boost::this_thread::get_id();
3197                         putLogError(100058, formatter.str(), __FILE__, __LINE__);
3198                         throw - 1;
3199                 }
3200
3201                 session_data = session_thread_it->second;
3202                 //set sorry_endpoint
3203                 session_data->target_endpoint = sorry_endpoint;
3204
3205                 //endpoint check
3206                 receive_data_it = session_data->receive_data_map.find(session_data->client_endpoint_tcp);
3207                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
3208                         boost::format formatter("Invalid endpoint. thread id : %d.");
3209                         formatter % boost::this_thread::get_id();
3210                         putLogError(100059, formatter.str(), __FILE__, __LINE__);
3211                         throw - 1;
3212                 }
3213
3214                 status = SORRYSERVER_CONNECT;
3215         } catch (int e) {
3216                 /*-------- DEBUG LOG --------*/
3217                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3218                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3219                                                 "handle_sorryserver_select() : catch exception e = %d. thread id : %d.");
3220                         formatter % e % boost::this_thread::get_id();
3221                         putLogDebug(100110, formatter.str(), __FILE__, __LINE__);
3222                 }
3223                 status = FINALIZE;
3224                 /*------DEBUG LOG END------*/
3225         } catch (const std::bad_alloc &ex) {
3226                 std::cerr << "protocol_module_sessionless::handle_sorryserver_select() : exception : Could not allocate memory." << std::endl;
3227                 boost::format formatter("Could not allocate memory. thread id : %d.");
3228                 formatter % boost::this_thread::get_id();
3229                 putLogError(100060, formatter.str(), __FILE__, __LINE__);
3230                 status = FINALIZE;
3231         } catch (const std::exception &ex) {
3232                 std::cerr << "protocol_module_sessionless::handle_sorryserver_select() : exception : error = " << ex.what() << "." << std::endl;
3233                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3234                                         "handle_sorryserver_select() : exception : error = %s. thread id : %d.");
3235                 formatter % ex.what() % boost::this_thread::get_id();
3236                 putLogError(100061, formatter.str(), __FILE__, __LINE__);
3237                 status = FINALIZE;
3238         } catch (...) {
3239                 std::cerr << "protocol_module_sessionless::handle_sorryserver_select() : Unknown exception." << std::endl;
3240                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3241                                         "handle_sorryserver_select() : Unknown exception. thread id : %d.");
3242                 formatter % boost::this_thread::get_id();
3243                 putLogError(100062, formatter.str(), __FILE__, __LINE__);
3244                 status = FINALIZE;
3245         }
3246
3247         /*-------- DEBUG LOG --------*/
3248         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3249                 boost::format
3250                 formatter(
3251                         "out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3252                         "handle_sorryserver_select(const boost::thread::id thread_id, boost::asio::ip::tcp::endpoint& sorry_endpoint) : return_value = %d."
3253                         " thread id : %d.");
3254                 formatter % status % boost::this_thread::get_id();
3255                 putLogDebug(100111, formatter.str(), __FILE__, __LINE__);
3256         }
3257         /*------DEBUG LOG END------*/
3258         return status;
3259 }
3260
3261 //! called from after sorryserver connect
3262 //!    @param[in]    upstream thread id
3263 //! @param[out]    send buffer reference.
3264 //! @param[out]    send length
3265 //! @return        session use EVENT mode.
3266 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_connect(
3267         const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t &datalen)
3268 {
3269         /*-------- DEBUG LOG --------*/
3270         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3271                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3272                                         "handle_sorryserver_connect(const boost::thread::id thread_id, "
3273                                         "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : "
3274                                         "thread_id = %d.");
3275                 formatter % thread_id;
3276                 putLogDebug(100112, formatter.str(), __FILE__, __LINE__);
3277         }
3278         /*------DEBUG LOG END------*/
3279         EVENT_TAG status = FINALIZE;
3280         bool ret = false;
3281         size_t header_offset = 0;
3282         size_t header_offset_len = 0;
3283         size_t url_offset = 0;
3284         size_t url_offset_len = 0;
3285         size_t send_buffer_remian_size = 0;
3286         size_t copy_size = 0;
3287         const int send_buffer_end_size = sendbuffer.max_size();
3288         const std::string http_header = "";
3289         const std::string str_forword_for = "X-Forwarded-For";
3290         thread_data_ptr session_data;
3291         session_thread_data_map_it session_thread_it;
3292         receive_data_map_it receive_data_it;
3293
3294         try {
3295                 {
3296                         boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
3297
3298                         //thread id check
3299                         session_thread_it = session_thread_data_map.find(thread_id);
3300                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
3301                                 boost::format formatter("Invalid thread id. thread id : %d.");
3302                                 formatter % boost::this_thread::get_id();
3303                                 putLogError(100063, formatter.str(), __FILE__, __LINE__);
3304                                 throw - 1;
3305                         }
3306
3307                         session_data = session_thread_it->second;
3308                 }
3309
3310                 //endpoint check
3311                 receive_data_it = session_data->receive_data_map.find(session_data->client_endpoint_tcp);
3312                 if (unlikely(receive_data_it
3313                              == session_data->receive_data_map.end())) {
3314                         boost::format formatter("Invalid endpoint. thread id : %d.");
3315                         formatter % boost::this_thread::get_id();
3316                         putLogError(100064, formatter.str(), __FILE__, __LINE__);
3317                         throw - 1;
3318                 }
3319
3320                 //receive_buffer pointer check
3321                 receive_data &recv_data = receive_data_it->second;
3322                 if (unlikely(recv_data.receive_buffer == NULL)) {
3323                         status = CLIENT_RECV;
3324                         goto handle_sorryserver_connect_out;
3325                         /*
3326                                                 boost::format formatter("Invalid pointer. thread id : %d.");
3327                                                 formatter % boost::this_thread::get_id();
3328                                                 putLogError(100065, formatter.str(), __FILE__, __LINE__);
3329                                                 throw - 1;
3330                         */
3331                 }
3332
3333                 //send list check
3334                 send_status_it it = recv_data.send_status_list.begin();
3335                 send_status_it it_end = recv_data.send_status_list.end();
3336
3337                 it = find_if(it, it_end, data_send_possible());
3338                 if (unlikely(it == it_end)) {
3339                         boost::format formatter("Sending possible data is not existed. thread id : %d.");
3340                         formatter % boost::this_thread::get_id();
3341                         putLogError(100066, formatter.str(), __FILE__, __LINE__);
3342                         throw - 1;
3343                 }
3344
3345                 //send buffer rest size initialization
3346                 send_buffer_remian_size = send_buffer_end_size;
3347
3348                 //edit_division flag on
3349                 if (it->edit_division == EDIT_DIVISION_EDIT) {
3350                         //edit list is empty
3351                         if (it->edit_data_list.empty()) {
3352                                 //edit data create
3353                                 edit_data edata;
3354                                 edata.data_size = 0;
3355                                 edata.insert_posission = 0;
3356                                 edata.replace_size = 0;
3357                                 //search uri
3358                                 if (strlen(sorry_uri.data()) > 0) {
3359                                         ret = find_uri(recv_data.receive_buffer + it->send_offset, it->send_possible_size, url_offset,
3360                                                        url_offset_len);
3361                                         /*-------- DEBUG LOG --------*/
3362                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3363                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3364                                                                         "handle_sorryserver_connect() : call find_uri : "
3365                                                                         "return_value = %d. thread id : %d.");
3366                                                 formatter % static_cast<int>(ret) % boost::this_thread::get_id();
3367                                                 putLogDebug(100113, formatter.str(), __FILE__, __LINE__);
3368                                         }
3369                                         /*------DEBUG LOG END------*/
3370                                         //search http header result is OK
3371                                         if (ret) {
3372                                                 //edit sorry_uri, put it to edata.data
3373                                                 edata.data = sorry_uri.data();
3374                                                 //save new uri offset
3375                                                 edata.insert_posission = url_offset;
3376                                                 //save new uri length
3377                                                 edata.data_size = edata.data.size();
3378                                                 //save old uri length
3379                                                 edata.replace_size = url_offset_len;
3380                                                 //add to edit_data_list
3381                                                 it->edit_data_list.push_back(edata);
3382                                         }
3383                                 }
3384
3385                                 if (forwarded_for == FORWARDED_FOR_ON) {
3386                                         //search X-Forwarded-For header
3387                                         ret = find_http_header(recv_data.receive_buffer + it->send_offset, it->send_possible_size,
3388                                                                str_forword_for.c_str(), header_offset, header_offset_len);
3389                                         /*-------- DEBUG LOG --------*/
3390                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3391                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3392                                                                         "handle_sorryserver_connect() : call find_http_header : "
3393                                                                         "return_value = %d. thread id : %d.");
3394                                                 formatter % static_cast<int>(ret) % boost::this_thread::get_id();
3395                                                 putLogDebug(100114, formatter.str(), __FILE__, __LINE__);
3396                                         }
3397                                         /*------DEBUG LOG END------*/
3398
3399                                         //search http header result is OK
3400                                         if (ret) {
3401                                                 //edit X-Forwarded-For header, put it to edata.data
3402                                                 edata.data.assign(recv_data.receive_buffer + it->send_offset + header_offset, header_offset_len);
3403                                                 edata.data += ", ";
3404                                                 edata.data += session_data->client_endpoint_tcp.address().to_string();
3405                                                 //save new X-Forwarded-For header offset
3406                                                 edata.insert_posission = header_offset;
3407                                                 //save new X-Forwarded-For header length
3408                                                 edata.data_size = edata.data.size();
3409                                                 //save old X-Forwarded-For header length
3410                                                 edata.replace_size = header_offset_len;
3411                                         }
3412                                         //search http header result is NG
3413                                         else {
3414                                                 //search whole http header, get whole http header's offset and length
3415                                                 ret = find_http_header(recv_data.receive_buffer + it->send_offset, it->send_possible_size, "",
3416                                                                        header_offset, header_offset_len);
3417                                                 /*-------- DEBUG LOG --------*/
3418                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3419                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3420                                                                                 "handle_sorryserver_connect() : call find_http_header : "
3421                                                                                 "return_value = %d. thread id : %d.");
3422                                                         formatter % static_cast<int>(ret) % boost::this_thread::get_id();
3423                                                         putLogDebug(100115, formatter.str(), __FILE__, __LINE__);
3424                                                 }
3425                                                 /*------DEBUG LOG END------*/
3426                                                 if (!ret) {
3427                                                         boost::format formatter("find_http_header() function failure. thread id : %d.");
3428                                                         formatter % boost::this_thread::get_id();
3429                                                         putLogError(100067, formatter.str(), __FILE__, __LINE__);
3430                                                         /*-------- DEBUG LOG --------*/
3431                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3432                                                                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3433                                                                                         "handle_sorryserver_connect(const boost::thread::id thread_id, "
3434                                                                                         "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
3435                                                                                         "size_t& datalen) : return_value = %d. thread id : %d.");
3436                                                                 formatter % FINALIZE % boost::this_thread::get_id();
3437                                                                 putLogDebug(100116, formatter.str(), __FILE__, __LINE__);
3438                                                         }
3439                                                         /*------DEBUG LOG END------*/
3440                                                         return FINALIZE;
3441                                                 }
3442                                                 //create X-Forwarded-For header, set it to edata.data
3443                                                 edata.data = str_forword_for;
3444                                                 edata.data += ": ";
3445                                                 edata.data += session_data->client_endpoint_tcp.address().to_string();
3446                                                 edata.data += "\r\n";
3447                                                 //save new X-Forwarded-For header offset
3448                                                 edata.insert_posission = header_offset;
3449                                                 //save new X-Forwarded-For header length
3450                                                 edata.data_size = edata.data.size();
3451                                                 //save old X-Forwarded-For header length
3452                                                 edata.replace_size = 0;
3453                                         }
3454
3455                                         //add to edit_data_list
3456                                         it->edit_data_list.push_back(edata);
3457                                 }
3458                         }
3459
3460                         /*-------- DEBUG LOG --------*/
3461                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3462                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3463                                                         "handle_sorryserver_connect() : Copy data loop start. thread id : %d.");
3464                                 formatter % boost::this_thread::get_id();
3465                                 putLogDebug(100117, formatter.str(), __FILE__, __LINE__);
3466                         }
3467                         /*------DEBUG LOG END------*/
3468                         while (true) {
3469                                 //edit_data_list is empty
3470                                 if (it->edit_data_list.empty()) {
3471                                         //set edit_division flag off
3472                                         it->edit_division = EDIT_DIVISION_NO_EDIT;
3473
3474                                         if (send_buffer_remian_size > 0 && it->send_possible_size > 0) {
3475                                                 //send_buffer_remain_size is larger
3476                                                 if (send_buffer_remian_size > it->send_possible_size) {
3477                                                         //copy data from receive_buffer to sendbuffer by sending_possible size
3478                                                         copy_size = it->send_possible_size;
3479                                                         /*-------- DEBUG LOG --------*/
3480                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3481                                                                 std::string datadump;
3482                                                                 dump_memory(recv_data.receive_buffer + it->send_offset + it->send_end_size,
3483                                                                             copy_size, datadump);
3484                                                                 boost::format formatter(
3485                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3486                                                                         "handle_sorryserver_connect() : before memcpy (data dump) : "
3487                                                                         "data begin = %d, data_size = %d, data = %s");
3488                                                                 formatter % (it->send_offset + it->send_end_size)
3489                                                                 % copy_size % datadump;
3490                                                                 putLogDebug(100118, formatter.str(), __FILE__, __LINE__);
3491                                                         }
3492                                                         /*------DEBUG LOG END------*/
3493                                                         memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3494                                                                recv_data.receive_buffer + it->send_offset + it->send_end_size, copy_size);
3495                                                         /*-------- DEBUG LOG --------*/
3496                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3497                                                                 std::string datadump;
3498                                                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3499                                                                             copy_size, datadump);
3500                                                                 boost::format formatter(
3501                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3502                                                                         "handle_sorryserver_connect() : after memcpy (data dump) : "
3503                                                                         "data begin = %d, data_size = %d, data = %s");
3504                                                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
3505                                                                 % copy_size % datadump;
3506                                                                 putLogDebug(100119, formatter.str(), __FILE__, __LINE__);
3507                                                         }
3508                                                         /*------DEBUG LOG END------*/
3509                                                         it->send_end_size += copy_size;
3510                                                         it->send_possible_size = 0;
3511                                                         send_buffer_remian_size -= copy_size;
3512                                                 }
3513                                                 //send_possible_size is larger
3514                                                 else {
3515                                                         copy_size = send_buffer_remian_size;
3516                                                         /*-------- DEBUG LOG --------*/
3517                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3518                                                                 std::string datadump;
3519                                                                 dump_memory(recv_data.receive_buffer + it->send_offset + it->send_end_size,
3520                                                                             copy_size, datadump);
3521                                                                 boost::format formatter(
3522                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3523                                                                         "handle_sorryserver_connect() : before memcpy (data dump) : "
3524                                                                         "data begin = %d, data_size = %d, data = %s");
3525                                                                 formatter % (it->send_offset + it->send_end_size)
3526                                                                 % copy_size % datadump;
3527                                                                 putLogDebug(100120, formatter.str(), __FILE__, __LINE__);
3528                                                         }
3529                                                         /*------DEBUG LOG END------*/
3530                                                         //copy data from receive_buffer to sendbuffer by send buffer rest size
3531                                                         memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3532                                                                recv_data.receive_buffer + it->send_offset + it->send_end_size,
3533                                                                copy_size);
3534                                                         /*-------- DEBUG LOG --------*/
3535                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3536                                                                 std::string datadump;
3537                                                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3538                                                                             copy_size, datadump);
3539                                                                 boost::format formatter(
3540                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3541                                                                         "handle_sorryserver_connect() : after memcpy (data dump) : "
3542                                                                         "data begin = %d, data_size = %d, data = %s");
3543                                                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
3544                                                                 % copy_size % datadump;
3545                                                                 putLogDebug(100121, formatter.str(), __FILE__, __LINE__);
3546                                                         }
3547                                                         /*------DEBUG LOG END------*/
3548                                                         it->send_end_size += copy_size;
3549                                                         it->send_possible_size -= copy_size;
3550                                                         send_buffer_remian_size = 0;
3551                                                 }
3552                                         }
3553
3554                                         break;
3555                                 }
3556                                 //edit_data_list is not empty
3557                                 else {
3558                                         //search item which insert_position is minimum
3559                                         std::list<edit_data>::iterator edit_min = std::min_element(it->edit_data_list.begin(),
3560                                                         it->edit_data_list.end());
3561                                         //send_buffer_remain_size is larger than data that before X-Forwarded-For/uri
3562                                         if (send_buffer_remian_size >= edit_min->insert_posission - it->send_end_size) {
3563                                                 //copy data before X-Forwarded-For/url
3564                                                 copy_size = edit_min->insert_posission - it->send_end_size;
3565                                                 /*-------- DEBUG LOG --------*/
3566                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3567                                                         std::string datadump;
3568                                                         dump_memory(recv_data.receive_buffer + it->send_offset + it->send_end_size,
3569                                                                     copy_size, datadump);
3570
3571                                                         boost::format formatter(
3572                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3573                                                                 "handle_sorryserver_connect() : before memcpy (data dump) : "
3574                                                                 "data begin = %d, data_size = %d, data = %s");
3575                                                         formatter % (it->send_offset + it->send_end_size)
3576                                                         % copy_size % datadump;
3577                                                         putLogDebug(100122, formatter.str(), __FILE__, __LINE__);
3578                                                 }
3579                                                 /*------DEBUG LOG END------*/
3580                                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3581                                                        recv_data.receive_buffer + it->send_offset + it->send_end_size, copy_size);
3582                                                 /*-------- DEBUG LOG --------*/
3583                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3584                                                         std::string datadump;
3585                                                         dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3586                                                                     copy_size, datadump);
3587                                                         boost::format formatter(
3588                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3589                                                                 "handle_sorryserver_connect() : after memcpy (data dump) : "
3590                                                                 "data begin = %d, data_size = %d, data = %s");
3591                                                         formatter % (send_buffer_end_size - send_buffer_remian_size)
3592                                                         % copy_size % datadump;
3593                                                         putLogDebug(100123, formatter.str(), __FILE__, __LINE__);
3594                                                 }
3595                                                 /*------DEBUG LOG END------*/
3596                                                 it->send_end_size += copy_size;
3597                                                 it->send_possible_size -= copy_size;
3598                                                 send_buffer_remian_size -= copy_size;
3599
3600                                                 //there is remain buffer for copy X-Forwarded-For/url
3601                                                 if (send_buffer_remian_size >= edit_min->data_size) {
3602                                                         /*-------- DEBUG LOG --------*/
3603                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3604                                                                 std::string datadump;
3605                                                                 dump_memory(edit_min->data.c_str(),
3606                                                                             edit_min->data_size, datadump);
3607                                                                 boost::format formatter(
3608                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3609                                                                         "handle_sorryserver_connect() : before memcpy (data dump) : "
3610                                                                         "data begin = 0, data_size = %d, data = %s");
3611                                                                 formatter % edit_min->data_size % datadump;
3612                                                                 putLogDebug(100124, formatter.str(), __FILE__, __LINE__);
3613                                                         }
3614                                                         /*------DEBUG LOG END------*/
3615                                                         //copy X-Forwarded-For/uri
3616                                                         memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3617                                                                edit_min->data.c_str(), edit_min->data_size);
3618                                                         /*-------- DEBUG LOG --------*/
3619                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3620                                                                 std::string datadump;
3621                                                                 dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3622                                                                             edit_min->data_size, datadump);
3623
3624                                                                 boost::format formatter(
3625                                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3626                                                                         "handle_sorryserver_connect() : after memcpy (data dump) : "
3627                                                                         "data begin = %d, data_size = %d, data = %s");
3628                                                                 formatter % (send_buffer_end_size - send_buffer_remian_size)
3629                                                                 % edit_min->data_size % datadump;
3630                                                                 putLogDebug(100125, formatter.str(), __FILE__, __LINE__);
3631                                                         }
3632                                                         /*------DEBUG LOG END------*/
3633                                                         it->send_end_size += edit_min->replace_size;
3634                                                         it->send_possible_size -= edit_min->replace_size;
3635                                                         send_buffer_remian_size -= edit_min->data_size;
3636                                                         it->edit_data_list.erase(edit_min);
3637                                                 }
3638                                                 //
3639                                                 else {
3640                                                         break;
3641                                                 }
3642                                         }
3643                                         //data that before X-Forwarded-For/uri is larger than send_buffer_remain_size
3644                                         else {
3645                                                 copy_size = send_buffer_remian_size;
3646                                                 /*-------- DEBUG LOG --------*/
3647                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3648                                                         std::string datadump;
3649                                                         dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3650                                                                     copy_size, datadump);
3651
3652                                                         boost::format formatter(
3653                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3654                                                                 "handle_sorryserver_connect() : before memcpy (data dump) : "
3655                                                                 "data begin = %d, data_size = %d, data = %s");
3656                                                         formatter % (it->send_offset + it->send_end_size)
3657                                                         % copy_size % datadump;
3658                                                         putLogDebug(100126, formatter.str(), __FILE__, __LINE__);
3659                                                 }
3660                                                 /*------DEBUG LOG END------*/
3661                                                 //copy data as large as possible
3662                                                 memcpy(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3663                                                        recv_data.receive_buffer + it->send_offset + it->send_end_size, send_buffer_remian_size);
3664                                                 /*-------- DEBUG LOG --------*/
3665                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3666                                                         std::string datadump;
3667                                                         dump_memory(sendbuffer.data() + send_buffer_end_size - send_buffer_remian_size,
3668                                                                     copy_size, datadump);
3669
3670                                                         boost::format formatter(
3671                                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3672                                                                 "handle_sorryserver_connect() : after memcpy (data dump) : "
3673                                                                 "data begin = %d, data_size = %d, data = %s");
3674                                                         formatter % (send_buffer_end_size - send_buffer_remian_size)
3675                                                         % copy_size % datadump;
3676                                                         putLogDebug(100127, formatter.str(), __FILE__, __LINE__);
3677                                                 }
3678                                                 /*------DEBUG LOG END------*/
3679                                                 it->send_end_size += copy_size;
3680                                                 it->send_possible_size -= copy_size;
3681                                                 send_buffer_remian_size -= copy_size;
3682                                                 break;
3683                                         }
3684                                 }
3685                         }
3686                         /*-------- DEBUG LOG --------*/
3687                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3688                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3689                                                         "handle_sorryserver_connect() : Copy data loop end. thread id : %d.");
3690                                 formatter % boost::this_thread::get_id();
3691                                 putLogDebug(100128, formatter.str(), __FILE__, __LINE__);
3692                         }
3693                         /*------DEBUG LOG END------*/
3694                 }
3695                 //edit_division flag is off
3696                 else {
3697                         //copy data as large as possible
3698                         //send_possible_size is larger
3699                         if (send_buffer_remian_size >= it->send_possible_size) {
3700                                 copy_size = it->send_possible_size;
3701                                 /*-------- DEBUG LOG --------*/
3702                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3703                                         std::string datadump;
3704                                         dump_memory(recv_data.receive_buffer + it->send_offset, copy_size, datadump);
3705
3706                                         boost::format formatter(
3707                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3708                                                 "handle_sorryserver_connect() : before memcpy (data dump) : "
3709                                                 "data begin = %d, data_size = %d, data = %s");
3710                                         formatter % it->send_offset
3711                                         % copy_size % datadump;
3712                                         putLogDebug(100129, formatter.str(), __FILE__, __LINE__);
3713                                 }
3714                                 /*------DEBUG LOG END------*/
3715                                 //copy data by send_possible size
3716                                 memcpy(sendbuffer.data(), recv_data.receive_buffer
3717                                        + it->send_offset, copy_size);
3718                                 /*-------- DEBUG LOG --------*/
3719                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3720                                         std::string datadump;
3721                                         dump_memory(sendbuffer.data(), copy_size, datadump);
3722
3723                                         boost::format formatter(
3724                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3725                                                 "handle_sorryserver_connect() : after memcpy (data dump) : "
3726                                                 "data begin = 0, data_size = %d, data = %s");
3727                                         formatter % copy_size % datadump;
3728                                         putLogDebug(100130, formatter.str(), __FILE__, __LINE__);
3729                                 }
3730                                 /*------DEBUG LOG END------*/
3731                                 it->send_end_size = it->send_possible_size;
3732                                 it->send_possible_size = 0;
3733                                 send_buffer_remian_size -= copy_size;
3734                         }
3735                         //buffer rest size is larger
3736                         else {
3737                                 /*-------- DEBUG LOG --------*/
3738                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3739                                         std::string datadump;
3740                                         dump_memory(recv_data.receive_buffer + it->send_offset, send_buffer_remian_size, datadump);
3741
3742                                         boost::format formatter(
3743                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3744                                                 "handle_sorryserver_connect() : before memcpy (data dump) : "
3745                                                 "data begin = %d, data_size = %d, data = %s");
3746                                         formatter % it->send_offset
3747                                         % send_buffer_remian_size % datadump;
3748                                         putLogDebug(100131, formatter.str(), __FILE__, __LINE__);
3749                                 }
3750                                 /*------DEBUG LOG END------*/
3751                                 //copy data by buffer rest size
3752                                 memcpy(sendbuffer.data(), recv_data.receive_buffer
3753                                        + it->send_offset, send_buffer_remian_size);
3754                                 /*-------- DEBUG LOG --------*/
3755                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3756                                         std::string datadump;
3757                                         dump_memory(sendbuffer.data(), send_buffer_remian_size, datadump);
3758
3759                                         boost::format formatter(
3760                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3761                                                 "handle_sorryserver_connect() : after memcpy (data dump) : "
3762                                                 "data begin = 0, data_size = %d, data = %s");
3763                                         formatter % send_buffer_remian_size % datadump;
3764                                         putLogDebug(100132, formatter.str(), __FILE__, __LINE__);
3765                                 }
3766                                 /*------DEBUG LOG END------*/
3767                                 it->send_end_size = send_buffer_remian_size;
3768                                 it->send_possible_size -= send_buffer_remian_size;
3769                                 send_buffer_remian_size = 0;
3770                         }
3771                 }
3772
3773                 //set copied data length
3774                 datalen = send_buffer_end_size - send_buffer_remian_size;
3775
3776                 status = SORRYSERVER_SEND;
3777
3778         } catch (int e) {
3779                 /*-------- DEBUG LOG --------*/
3780                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3781                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3782                                                 "handle_sorryserver_connect() : catch exception e = %d. thread id : %d.");
3783                         formatter % e % boost::this_thread::get_id();
3784                         putLogDebug(100133, formatter.str(), __FILE__, __LINE__);
3785                 }
3786                 status = FINALIZE;
3787                 /*------DEBUG LOG END------*/
3788         } catch (const std::exception &ex) {
3789                 std::cerr << "protocol_module_sessionless::handle_sorryserver_connect() : exception : error = " << ex.what() << "." << std::endl;
3790                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3791                                         "handle_sorryserver_connect() : exception : error = %s. thread id : %d.");
3792                 formatter % ex.what() % boost::this_thread::get_id();
3793                 putLogError(100068, formatter.str(), __FILE__, __LINE__);
3794                 status = FINALIZE;
3795         } catch (...) {
3796                 std::cerr << "protocol_module_sessionless::handle_sorryserver_connect() : Unknown exception." << std::endl;
3797                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3798                                         "handle_sorryserver_connect() : Unknown exception. thread id : %d.");
3799                 formatter % boost::this_thread::get_id();
3800                 putLogError(100069, formatter.str(), __FILE__, __LINE__);
3801                 status = FINALIZE;
3802         }
3803
3804 handle_sorryserver_connect_out:
3805         /*-------- DEBUG LOG --------*/
3806         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3807                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3808                                         "handle_sorryserver_connect(const boost::thread::id thread_id, "
3809                                         "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
3810                                         "size_t& datalen) : return_value = %d. thread id : %d.");
3811                 formatter % status % boost::this_thread::get_id();
3812                 putLogDebug(100134, formatter.str(), __FILE__, __LINE__);
3813         }
3814         /*------DEBUG LOG END------*/
3815
3816         return status;
3817 }
3818
3819 //! called from after sorryserver connection fail
3820 //! @param[in]    upstream thread id
3821 //! @param[in]    sorryserver endpoint reference.
3822 //! @return        session use EVENT mode.
3823 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_connection_fail(
3824         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &sorry_endpoint)
3825 {
3826         /*-------- DEBUG LOG --------*/
3827         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3828                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3829                                         "handle_sorryserver_connection_fail(const boost::thread::id thread_id, "
3830                                         "const boost::asio::ip::tcp::endpoint & sorry_endpoint) : "
3831                                         "thread_id = %d, sorry_endpoint = [%s]:%d.");
3832                 formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port();
3833                 putLogDebug(100135, formatter.str(), __FILE__, __LINE__);
3834         }
3835         /*------DEBUG LOG END------*/
3836
3837         EVENT_TAG status = FINALIZE;
3838         thread_data_ptr session_data;
3839         session_thread_data_map_it session_thread_it;
3840
3841         try {
3842                 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
3843
3844                 session_thread_it = session_thread_data_map.find(thread_id);
3845                 if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
3846                         boost::format formatter("Invalid thread id. thread id : %d.");
3847                         formatter % boost::this_thread::get_id();
3848                         putLogError(100070, formatter.str(), __FILE__, __LINE__);
3849                         throw - 1;
3850                 }
3851
3852                 session_data = session_thread_it->second;
3853
3854                 //set end flag on
3855                 session_data->end_flag = END_FLAG_ON;
3856                 /*-------- DEBUG LOG --------*/
3857                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3858                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3859                                                 "handle_sorryserver_connection_fail() : END_FLAG_ON. thread id : %d.");
3860                         formatter % boost::this_thread::get_id();
3861                         putLogDebug(100136, formatter.str(), __FILE__, __LINE__);
3862                 }
3863                 /*------DEBUG LOG END------*/
3864
3865                 status = FINALIZE;
3866         } catch (int e) {
3867                 /*-------- DEBUG LOG --------*/
3868                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3869                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3870                                                 "handle_sorryserver_connection_fail() : catch exception e = %d. thread id : %d.");
3871                         formatter % e % boost::this_thread::get_id();
3872                         putLogDebug(100137, formatter.str(), __FILE__, __LINE__);
3873                 }
3874                 status = FINALIZE;
3875                 /*------DEBUG LOG END------*/
3876         } catch (const std::exception &ex) {
3877                 std::cerr << "protocol_module_sessionless::handle_sorryserver_connection_fail() : exception : error=" << ex.what() << "." << std::endl;
3878                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3879                                         "handle_sorryserver_connection_fail() : exception : error = %s. thread id : %d.");
3880                 formatter % ex.what() % boost::this_thread::get_id();
3881                 putLogError(100071, formatter.str(), __FILE__, __LINE__);
3882                 status = FINALIZE;
3883         } catch (...) {
3884                 std::cerr << "protocol_module_sessionless::handle_sorryserver_connection_fail() : Unknown exception." << std::endl;
3885                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3886                                         "handle_sorryserver_connection_fail() : Unknown exception. thread id : %d.");
3887                 formatter % boost::this_thread::get_id();
3888                 putLogError(100072, formatter.str(), __FILE__, __LINE__);
3889                 status = FINALIZE;
3890         }
3891
3892         /*-------- DEBUG LOG --------*/
3893         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3894                 boost::format
3895                 formatter(
3896                         "out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3897                         "handle_sorryserver_connection_fail( const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & sorry_endpoint) : return_value = %d."
3898                         " thread id : %d.");
3899                 formatter % status % boost::this_thread::get_id();
3900                 putLogDebug(100138, formatter.str(), __FILE__, __LINE__);
3901         }
3902         /*------DEBUG LOG END------*/
3903         return status;
3904 }
3905
3906 //! called from after sorryserver send
3907 //! @param[in]    upstream thread id
3908 //! @return        session use EVENT mode.
3909 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_send(
3910         const boost::thread::id thread_id)
3911 {
3912         /*-------- DEBUG LOG --------*/
3913         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3914                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
3915                                         "handle_sorryserver_send(const boost::thread::id thread_id) : "
3916                                         "thread_id = %d.");
3917                 formatter % thread_id;
3918                 putLogDebug(100139, formatter.str(), __FILE__, __LINE__);
3919         }
3920         /*------DEBUG LOG END------*/
3921         EVENT_TAG status = FINALIZE;
3922         thread_data_ptr session_data;
3923         session_thread_data_map_it session_thread_it;
3924         receive_data_map_it receive_data_it;
3925
3926         try {
3927                 {
3928                         boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
3929
3930                         //thread_id check
3931                         session_thread_it = session_thread_data_map.find(thread_id);
3932                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
3933                                 boost::format formatter("Invalid thread id. thread id : %d.");
3934                                 formatter % boost::this_thread::get_id();
3935                                 putLogError(100073, formatter.str(), __FILE__, __LINE__);
3936                                 throw - 1;
3937                         }
3938
3939                         session_data = session_thread_it->second;
3940                 }
3941
3942                 //endpoint check
3943                 receive_data_it = session_data->receive_data_map.find(session_data->client_endpoint_tcp);
3944                 if (unlikely(receive_data_it
3945                              == session_data->receive_data_map.end())) {
3946                         boost::format formatter("Invalid endpoint. thread id : %d.");
3947                         formatter % boost::this_thread::get_id();
3948                         putLogError(100074, formatter.str(), __FILE__, __LINE__);
3949                         throw - 1;
3950                 }
3951
3952                 receive_data &recv_data = receive_data_it->second;
3953
3954                 send_status_it it = recv_data.send_status_list.begin();
3955                 send_status_it it_end = recv_data.send_status_list.end();
3956                 //status list check
3957                 it = std::adjacent_find(it, it_end, data_send_list_incorrect());
3958                 if (unlikely(it != it_end)) {
3959                         boost::format formatter("Sending possible data is invalid. thread id : %d.");
3960                         formatter % boost::this_thread::get_id();
3961                         putLogError(100075, formatter.str(), __FILE__, __LINE__);
3962                         throw - 1;
3963                 }
3964
3965                 //status list check
3966                 it = recv_data.send_status_list.begin();
3967                 it = find_if(it, it_end, data_send_ok());
3968                 if (unlikely(it == it_end)) {
3969                         boost::format formatter("Sending possible data is not existed. thread id : %d.");
3970                         formatter % boost::this_thread::get_id();
3971                         putLogError(100076, formatter.str(), __FILE__, __LINE__);
3972                         throw - 1;
3973                 }
3974
3975                 //sending possible data is exist
3976                 if (it->send_possible_size > 0) {
3977                         //status remain SEND_OK
3978                         it->status = SEND_OK;
3979                         //offset recalc
3980                         it->send_offset += it->send_end_size;
3981
3982                         //insert_position recalc
3983                         for (std::list<edit_data>::iterator list_it = it->edit_data_list.begin(); list_it
3984                              != it->edit_data_list.end(); ++list_it) {
3985                                 list_it->insert_posission -= it->send_end_size;
3986                         }
3987
3988                         //send_end_size recalc
3989                         it->send_end_size = 0;
3990                 }
3991                 //sending possible data is not exist
3992                 else {
3993                         //can receive from client continue
3994                         if (it->send_rest_size > 0) {
3995                                 //change status from SEND_OK to SEND_CONTINUE
3996                                 it->status = SEND_CONTINUE;
3997                         }
3998                         //can not receive from client continue
3999                         else {
4000                                 //change status from SEND_OK to SEND_END
4001                                 it->status = SEND_END;
4002                         }
4003                 }
4004
4005                 it = recv_data.send_status_list.begin();
4006                 it = find_if(it, it_end, data_send_ok());
4007                 //send_ok item is exist
4008                 if (it != it_end) {
4009                         status = SORRYSERVER_CONNECT;
4010                 }
4011                 //send_ok item is exist
4012                 else {
4013                         status = CLIENT_RECV;
4014                 }
4015
4016         } catch (int e) {
4017                 /*-------- DEBUG LOG --------*/
4018                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4019                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4020                                                 "handle_sorryserver_send() : catch exception e = %d. thread id : %d.");
4021                         formatter % e % boost::this_thread::get_id();
4022                         putLogDebug(100140, formatter.str(), __FILE__, __LINE__);
4023                 }
4024                 status = FINALIZE;
4025                 /*------DEBUG LOG END------*/
4026         } catch (const std::exception &ex) {
4027                 std::cerr << "protocol_module_sessionless::handle_sorryserver_send() : exception : error = " << ex.what() << "." << std::endl;
4028                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4029                                         "handle_sorryserver_send() : exception : error = %s. thread id : %d.");
4030                 formatter % ex.what() % boost::this_thread::get_id();
4031                 putLogError(100077, formatter.str(), __FILE__, __LINE__);
4032
4033                 status = FINALIZE;
4034         } catch (...) {
4035                 std::cerr << "protocol_module_sessionless::handle_sorryserver_send() : Unknown exception." << std::endl;
4036                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4037                                         "handle_sorryserver_send() : Unknown exception. thread id : %d.");
4038                 formatter % boost::this_thread::get_id();
4039                 putLogError(100078, formatter.str(), __FILE__, __LINE__);
4040                 status = FINALIZE;
4041         }
4042
4043         /*-------- DEBUG LOG --------*/
4044         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4045                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4046                                         "handle_sorryserver_send(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
4047                 formatter % status % boost::this_thread::get_id();
4048                 putLogDebug(100141, formatter.str(), __FILE__, __LINE__);
4049         }
4050         /*------DEBUG LOG END------*/
4051
4052         return status;
4053 }
4054
4055 //! called from after realserver receive.for UDP
4056 //! @param[in]    downstream thread id
4057 //! @param[in]    realserver UDP endpoint reference
4058 //! @param[in]    receive from realserver buffer reference
4059 //! @param[in]    recv data length
4060 //! @return        session use EVENT mode.
4061 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_recv(
4062         const boost::thread::id thread_id, const boost::asio::ip::udp::endpoint &rs_endpoint, const boost::array < char,
4063         MAX_BUFFER_SIZE > & recvbuffer, const size_t recvlen)
4064 {
4065         /*-------- DEBUG LOG --------*/
4066         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4067                 boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4068                                         "handle_realserver_recv(const boost::thread::id thread_id, "
4069                                         "const boost::asio::ip::udp::endpoint& rs_endpoint, "
4070                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4071                                         "const size_t recvlen) : "
4072                                         "return_value = %d. thread id : %d.");
4073                 formatter % STOP % boost::this_thread::get_id();
4074                 putLogDebug(100142, formatter.str(), __FILE__, __LINE__);
4075         }
4076         /*------DEBUG LOG END------*/
4077         return STOP;
4078 }
4079
4080 //! called from after realserver receive for TCP/IP
4081 //! @param[in]    downstream thread id
4082 //! @param[in]    realserver TCP/IP endpoint reference
4083 //! @param[in]    realserver receive buffer reference.
4084 //! @param[in]    recv data length
4085 //! @return        session use EVENT mode.
4086 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_recv(
4087         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &rs_endpoint, const boost::array < char,
4088         MAX_BUFFER_SIZE > & recvbuffer, const size_t recvlen)
4089 {
4090         /*-------- DEBUG LOG --------*/
4091         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4092                 size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
4093                 std::string buffer;
4094                 dump_memory(recvbuffer.data(), buffer_size, buffer);
4095                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4096                                         "handle_realserver_recv(const boost::thread::id thread_id, "
4097                                         "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
4098                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4099                                         "const size_t recvlen) : thread_id = %d, rs_endpoint = [%s]:%d, recvbuffer = %s, recvlen = %d.");
4100                 formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port()
4101                 % buffer % recvlen;
4102                 putLogDebug(100143, formatter.str(), __FILE__, __LINE__);
4103         }
4104         /*------DEBUG LOG END------*/
4105
4106         EVENT_TAG status = FINALIZE;
4107         size_t data_remain_start = 0;
4108         size_t data_remain_size = 0;
4109         size_t request_data_remain_size = 0;
4110         size_t header_offset = 0;
4111         size_t header_offset_len = 0;
4112         size_t content_length_header_offset = 0;
4113         size_t content_length_header_len = 0;
4114         size_t content_len_value = 0;
4115         size_t pos = 0;
4116         size_t buffer_size = 0;
4117         const size_t cr_lf_cr_lf_len = strlen("\r\n\r\n");
4118         const size_t cr_lf_len = strlen("\r\n");
4119         thread_data_ptr session_data;
4120         char *buffer1 = NULL;
4121         char *buffer2 = NULL;
4122         std::string str_value;
4123         const std::string http_header = "";
4124         const std::string content_header = "Content-Length";
4125         bool bret = false;
4126         CHECK_RESULT_TAG check_result;
4127         session_thread_data_map_it session_thread_it;
4128         receive_data_map_it receive_data_it;
4129
4130         //parameter check
4131         if (recvlen > recvbuffer.size()) {
4132                 std::cerr << "protocol_module_sessionless::handle_realserver_recv() : Data size bigger than buffer size." << std::endl;
4133                 boost::format formatter("Data size bigger than buffer size. thread id : %d.");
4134                 formatter % boost::this_thread::get_id();
4135                 putLogError(100079, formatter.str(), __FILE__, __LINE__);
4136                 /*-------- DEBUG LOG --------*/
4137                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4138                         boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4139                                                 "handle_realserver_recv(const boost::thread::id thread_id, "
4140                                                 "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
4141                                                 "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4142                                                 "const size_t recvlen) : return_value = %d. thread id : %d.");
4143                         formatter % FINALIZE % boost::this_thread::get_id();
4144                         putLogDebug(100144, formatter.str(), __FILE__, __LINE__);
4145                 }
4146                 /*------DEBUG LOG END------*/
4147                 return FINALIZE;
4148         }
4149
4150         try {
4151                 {
4152                         boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
4153
4154                         session_thread_it = session_thread_data_map.find(thread_id);
4155                         if (unlikely(session_thread_it == session_thread_data_map.end()
4156                                      || session_thread_it->second == NULL)) {
4157 #ifdef  DEBUG
4158                                 // debug code insert
4159                                 std::stringstream       buff;
4160                                 buff << "Invalid Thread ID. Thread ID[" << boost::this_thread::get_id() << "] map has thread datas = \n";
4161                                 for (session_thread_data_map_it itr = session_thread_data_map.begin(); itr != session_thread_data_map.end(); itr++) {
4162                                         buff << session_thread_data_to_string(itr->second) << "\n";
4163                                 }
4164                                 putLogError(100080, buff.str(), __FILE__, __LINE__);
4165 #else
4166                                 boost::format formatter("Invalid thread id. thread id : %d.");
4167                                 formatter % boost::this_thread::get_id();
4168                                 putLogError(100080, formatter.str(), __FILE__, __LINE__);
4169                                 throw - 1;
4170 #endif
4171                         }
4172
4173                         session_data = session_thread_it->second;
4174                 }
4175
4176                 receive_data_it = session_data->receive_data_map.find(rs_endpoint);
4177                 if (receive_data_it == session_data->receive_data_map.end()) {
4178                         receive_data recv_data;
4179                         session_data->receive_data_map[rs_endpoint] = recv_data;
4180                 }
4181
4182                 session_data->target_endpoint = rs_endpoint;
4183
4184                 receive_data &recv_data = session_data->receive_data_map[rs_endpoint];
4185
4186                 send_status_it it = recv_data.send_status_list.begin();
4187                 send_status_it it_end = recv_data.send_status_list.end();
4188
4189                 //status list check
4190                 it = std::find_if(it, it_end, data_send_ok());
4191                 if (unlikely(it != it_end)) {
4192                         boost::format formatter("Sending data is not correct. thread id : %d.");
4193                         formatter % boost::this_thread::get_id();
4194                         putLogError(100081, formatter.str(), __FILE__, __LINE__);
4195                         throw - 1;
4196                 }
4197
4198                 //status list check
4199                 it = recv_data.send_status_list.begin();
4200                 it = std::adjacent_find(it, it_end, data_send_repeated());
4201                 if (unlikely(it != it_end)) {
4202                         boost::format formatter("Sending data is not correct. thread id : %d.");
4203                         formatter % boost::this_thread::get_id();
4204                         putLogError(100082, formatter.str(), __FILE__, __LINE__);
4205                         throw - 1;
4206                 }
4207
4208                 /*-------- DEBUG LOG --------*/
4209                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4210                         std::string datadump;
4211                         boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
4212                                                 "send_rest_size = %d, send_possible_size = %d, "
4213                                                 "send_offset = %d, unsend_size = %d, edit_division = %d.");
4214                         int i = 0;
4215                         for (it = recv_data.send_status_list.begin();
4216                              it != recv_data.send_status_list.end();
4217                              ++it, ++i) {
4218                                 formatter % i % it->status % it->send_end_size
4219                                 % it->send_rest_size % it->send_possible_size
4220                                 % it->send_offset % it->unsend_size % it->edit_division;
4221                                 datadump += formatter.str();
4222                         }
4223
4224                         formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4225                                         "handle_realserver_recv() : send status list dump : send status list size = %d.%s");
4226
4227                         formatter % recv_data.send_status_list.size() % datadump;
4228                         putLogDebug(100145, formatter.str(), __FILE__, __LINE__);
4229                 }
4230                 /*------DEBUG LOG END------*/
4231                 it = recv_data.send_status_list.begin();
4232                 //get original status info
4233                 while (it != it_end) {
4234                         //item status is SEND_END
4235                         if (it->status == SEND_END) {
4236                                 //erase from list
4237                                 recv_data.send_status_list.erase(it++);
4238                                 continue;
4239                         }
4240                         //item status is SEND_CONTINUE
4241                         else if (it->status == SEND_CONTINUE) {
4242                                 it->send_offset += it->send_end_size;
4243                                 data_remain_start = it->send_offset;
4244                                 break;
4245                         }
4246                         //item status is SEND_NG
4247                         else {
4248                                 data_remain_start = it->send_offset;
4249                                 data_remain_size = it->unsend_size;
4250                                 break;
4251                         }
4252
4253                         ++it;
4254                 }
4255                 /*-------- DEBUG LOG --------*/
4256                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4257                         std::string datadump;
4258                         boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
4259                                                 "send_rest_size = %d, send_possible_size = %d, "
4260                                                 "send_offset = %d, unsend_size = %d, edit_division = %d.");
4261                         int i = 0;
4262                         for (it = recv_data.send_status_list.begin();
4263                              it != recv_data.send_status_list.end();
4264                              ++it, ++i) {
4265                                 formatter % i % it->status % it->send_end_size
4266                                 % it->send_rest_size % it->send_possible_size
4267                                 % it->send_offset % it->unsend_size % it->edit_division;
4268                                 datadump += formatter.str();
4269                         }
4270
4271                         formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4272                                         "handle_realserver_recv() : send status list dump : send status list size = %d.%s");
4273
4274                         formatter % recv_data.send_status_list.size() % datadump;
4275                         putLogDebug(100146, formatter.str(), __FILE__, __LINE__);
4276                 }
4277                 /*------DEBUG LOG END------*/
4278                 //receive buffer process
4279                 //buffer rest size < request size
4280                 if (recv_data.receive_buffer_rest_size < recvlen) {
4281                         //buffer max size < remain size + request size
4282                         //buffer is need reallocate
4283                         if (recv_data.receive_buffer_max_size < data_remain_size + recvlen) {
4284                                 //the buffer's size that will be allocated is exceed the upper limit value
4285                                 if (MAX_SESSIONLESS_MODULE_BUFFER_SIZE < data_remain_size + recvlen) {
4286                                         std::cerr << "protocol_module_sessionless::handle_realserver_recv() : the buffer's size that will be allocated is exceed the upper limit value." << std::endl;
4287                                         boost::format formatter("The buffer's size that will be allocated is exceed the upper limit value. thread id : %d.");
4288                                         formatter % boost::this_thread::get_id();
4289                                         putLogError(100083, formatter.str(), __FILE__, __LINE__);
4290
4291                                         /*-------- DEBUG LOG --------*/
4292                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4293                                                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4294                                                                         "handle_realserver_recv(const boost::thread::id thread_id, "
4295                                                                         "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
4296                                                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
4297                                                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
4298                                                 formatter % FINALIZE % boost::this_thread::get_id();
4299                                                 putLogDebug(100147, formatter.str(), __FILE__, __LINE__);
4300                                         }
4301                                         /*------DEBUG LOG END------*/
4302
4303                                         return FINALIZE;
4304                                 }
4305                                 //receive_buffer1's memory allocate and initialization
4306                                 buffer_size = (data_remain_size + recvlen) > MAX_BUFFER_SIZE ? (data_remain_size + recvlen) : MAX_BUFFER_SIZE;
4307                                 buffer1 = new char[buffer_size];
4308                                 /*-------- DEBUG LOG --------*/
4309                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4310                                         boost::format formatter("new : address = &(%d), size = %lu.");
4311                                         formatter % static_cast<void *>(buffer1) % buffer_size;
4312                                         putLogDebug(100148, formatter.str(), __FILE__, __LINE__);
4313                                 }
4314                                 /*-------- DEBUG LOG --------*/
4315                                 memset(buffer1, 0, buffer_size);
4316                                 //receive_buffer2's memory allocate and initialization
4317                                 buffer2 = new char[buffer_size];
4318                                 /*-------- DEBUG LOG --------*/
4319                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4320                                         boost::format formatter("new : address = &(%d), size = %lu.");
4321                                         formatter % static_cast<void *>(buffer2) % buffer_size;
4322                                         putLogDebug(100149, formatter.str(), __FILE__, __LINE__);
4323                                 }
4324                                 /*-------- DEBUG LOG END--------*/
4325                                 memset(buffer2, 0, buffer_size);
4326
4327                                 /*-------- DEBUG LOG --------*/
4328                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4329                                         std::string datadump;
4330                                         dump_memory(recv_data.receive_buffer + data_remain_start, data_remain_size, datadump);
4331
4332                                         boost::format formatter(
4333                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4334                                                 "handle_realserver_recv() : before memcpy (data dump) : "
4335                                                 "data begin = %d, data_size = %d, data = %s");
4336                                         formatter % data_remain_start % data_remain_size % datadump;
4337                                         putLogDebug(100150, formatter.str(), __FILE__, __LINE__);
4338                                 }
4339                                 /*------DEBUG LOG END------*/
4340                                 //copy data from old buffer to new buffer
4341                                 memcpy(buffer1, recv_data.receive_buffer + data_remain_start, data_remain_size);
4342                                 /*-------- DEBUG LOG --------*/
4343                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4344                                         std::string datadump;
4345                                         dump_memory(buffer1, data_remain_size, datadump);
4346
4347                                         boost::format formatter(
4348                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4349                                                 "handle_realserver_recv() : after memcpy (data dump) : "
4350                                                 "data begin = 0, data_size = %d, data = %s");
4351                                         formatter % data_remain_size % datadump;
4352                                         putLogDebug(100151, formatter.str(), __FILE__, __LINE__);
4353                                 }
4354                                 /*------DEBUG LOG END------*/
4355
4356                                 /*-------- DEBUG LOG --------*/
4357                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4358                                         std::string datadump;
4359                                         dump_memory(recvbuffer.data(), recvlen, datadump);
4360                                         boost::format formatter(
4361                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4362                                                 "handle_realserver_recv() : before memcpy (data dump) : "
4363                                                 "data begin = 0, data_size = %d, data = %s");
4364                                         formatter % recvlen % datadump;
4365                                         putLogDebug(100152, formatter.str(), __FILE__, __LINE__);
4366                                 }
4367                                 /*------DEBUG LOG END------*/
4368                                 memcpy(buffer1 + data_remain_size, recvbuffer.data(), recvlen);
4369                                 /*-------- DEBUG LOG --------*/
4370                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4371                                         std::string datadump;
4372                                         dump_memory(buffer1 + data_remain_size, recvlen, datadump);
4373                                         boost::format formatter(
4374                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4375                                                 "handle_realserver_recv() : after memcpy (data dump) : "
4376                                                 "data begin = %d, data_size = %d, data = %s");
4377                                         formatter % data_remain_size % recvlen % datadump;
4378                                         putLogDebug(100153, formatter.str(), __FILE__, __LINE__);
4379                                 }
4380                                 /*------DEBUG LOG END------*/
4381                                 //free old buffer1 and old buffer2
4382                                 if (recv_data.receive_buffer1 != NULL) {
4383                                         /*-------- DEBUG LOG --------*/
4384                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4385                                                 boost::format formatter("delete : address = &(%d).");
4386                                                 formatter % static_cast<void *>(recv_data.receive_buffer1);
4387                                                 putLogDebug(100154, formatter.str(), __FILE__,
4388                                                             __LINE__);
4389                                         }
4390                                         /*------DEBUG LOG END------*/
4391                                         delete[] recv_data.receive_buffer1;
4392                                         recv_data.receive_buffer1 = NULL;
4393                                 }
4394
4395                                 if (recv_data.receive_buffer2 != NULL) {
4396                                         /*-------- DEBUG LOG --------*/
4397                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4398                                                 boost::format formatter("delete : address = &(%d).");
4399                                                 formatter % static_cast<void *>(recv_data.receive_buffer2);
4400                                                 putLogDebug(100155, formatter.str(), __FILE__,
4401                                                             __LINE__);
4402                                         }
4403                                         /*------DEBUG LOG END------*/
4404                                         delete[] recv_data.receive_buffer2;
4405                                         recv_data.receive_buffer2 = NULL;
4406                                 }
4407
4408                                 //set new buffer pointer
4409                                 recv_data.receive_buffer1 = buffer1;
4410                                 recv_data.receive_buffer2 = buffer2;
4411                                 recv_data.receive_buffer = recv_data.receive_buffer1;
4412                                 //set new buffer's max size
4413                                 recv_data.receive_buffer_max_size = buffer_size;
4414                         }
4415                         //buffer's max size >= remain data size + request size
4416                         //buffer isn't need reallocate, but switch
4417                         else {
4418                                 //pointer valid check
4419                                 if (unlikely(recv_data.receive_buffer1 == NULL || recv_data.receive_buffer2 == NULL)) {
4420                                         boost::format formatter("Invalid pointer. thread id : %d.");
4421                                         formatter % boost::this_thread::get_id();
4422                                         putLogError(100084, formatter.str(), __FILE__, __LINE__);
4423                                         throw - 1;
4424                                 }
4425                                 //using buffer is buffer1
4426                                 if (recv_data.receive_buffer == recv_data.receive_buffer1) {
4427                                         //buffer2 initialization
4428                                         memset(recv_data.receive_buffer2, 0, recv_data.receive_buffer_max_size);
4429                                         /*-------- DEBUG LOG --------*/
4430                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4431                                                 std::string datadump;
4432                                                 dump_memory(recv_data.receive_buffer + data_remain_start, data_remain_size, datadump);
4433                                                 boost::format formatter(
4434                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4435                                                         "handle_realserver_recv() : before memcpy (data dump) : "
4436                                                         "data begin = %d, data_size = %d, data = %s");
4437                                                 formatter % data_remain_start % data_remain_size % datadump;
4438                                                 putLogDebug(100156, formatter.str(), __FILE__, __LINE__);
4439                                         }
4440                                         /*------DEBUG LOG END------*/
4441                                         //copy data from buffer1 to buffer2
4442                                         memcpy(recv_data.receive_buffer2, recv_data.receive_buffer + data_remain_start, data_remain_size);
4443                                         /*-------- DEBUG LOG --------*/
4444                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4445                                                 std::string datadump;
4446                                                 dump_memory(recv_data.receive_buffer2, data_remain_size, datadump);
4447                                                 boost::format formatter(
4448                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4449                                                         "handle_realserver_recv() : after memcpy (data dump) : "
4450                                                         "data begin = 0, data_size = %d, data = %s");
4451                                                 formatter % data_remain_size % datadump;
4452                                                 putLogDebug(100157, formatter.str(), __FILE__, __LINE__);
4453                                         }
4454                                         /*------DEBUG LOG END------*/
4455                                         /*-------- DEBUG LOG --------*/
4456                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4457                                                 std::string datadump;
4458                                                 dump_memory(recvbuffer.data(), recvlen, datadump);
4459                                                 boost::format formatter(
4460                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4461                                                         "handle_realserver_recv() : before memcpy (data dump) : "
4462                                                         "data begin = 0, data_size = %d, data = %s");
4463                                                 formatter % data_remain_size % datadump;
4464                                                 putLogDebug(100158, formatter.str(), __FILE__, __LINE__);
4465                                         }
4466                                         /*------DEBUG LOG END------*/
4467                                         memcpy(recv_data.receive_buffer2 + data_remain_size, recvbuffer.data(), recvlen);
4468                                         /*-------- DEBUG LOG --------*/
4469                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4470                                                 std::string datadump;
4471                                                 dump_memory(recv_data.receive_buffer2 + data_remain_size, recvlen, datadump);
4472
4473                                                 boost::format formatter(
4474                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4475                                                         "handle_realserver_recv() : after memcpy (data dump) : "
4476                                                         "data begin = %d, data_size = %d, data = %s");
4477                                                 formatter % data_remain_size % recvlen % datadump;
4478                                                 putLogDebug(100159, formatter.str(), __FILE__, __LINE__);
4479                                         }
4480                                         /*------DEBUG LOG END------*/
4481                                         //set buffer2 as using buffer
4482                                         recv_data.receive_buffer = recv_data.receive_buffer2;
4483                                 }
4484                                 //using buffer is buffer2
4485                                 else {
4486                                         //buffer1 initialization
4487                                         memset(recv_data.receive_buffer1, 0, recv_data.receive_buffer_max_size);
4488                                         /*-------- DEBUG LOG --------*/
4489                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4490                                                 std::string datadump;
4491                                                 dump_memory(recv_data.receive_buffer + data_remain_start, data_remain_size, datadump);
4492
4493                                                 boost::format formatter(
4494                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4495                                                         "handle_realserver_recv() : before memcpy (data dump) : "
4496                                                         "data begin = %d, data_size = %d, data = %s");
4497                                                 formatter % data_remain_start % data_remain_size % datadump;
4498                                                 putLogDebug(100160, formatter.str(), __FILE__, __LINE__);
4499                                         }
4500                                         /*------DEBUG LOG END------*/
4501                                         //copy data from buffer2 to buffer1
4502                                         memcpy(recv_data.receive_buffer1, recv_data.receive_buffer + data_remain_start, data_remain_size);
4503                                         /*-------- DEBUG LOG --------*/
4504                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4505                                                 std::string datadump;
4506                                                 dump_memory(recv_data.receive_buffer1, data_remain_size, datadump);
4507
4508                                                 boost::format formatter(
4509                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4510                                                         "handle_realserver_recv() : after memcpy (data dump) : "
4511                                                         "data begin = 0, data_size = %d, data = %s");
4512                                                 formatter % data_remain_size % datadump;
4513                                                 putLogDebug(100161, formatter.str(), __FILE__, __LINE__);
4514                                         }
4515                                         /*------DEBUG LOG END------*/
4516                                         /*-------- DEBUG LOG --------*/
4517                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4518                                                 std::string datadump;
4519                                                 dump_memory(recvbuffer.data(), recvlen, datadump);
4520
4521                                                 boost::format formatter(
4522                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4523                                                         "handle_realserver_recv() : before memcpy (data dump) : "
4524                                                         "data begin = 0, data_size = %d, data = %s");
4525                                                 formatter % recvlen % datadump;
4526                                                 putLogDebug(100162, formatter.str(), __FILE__, __LINE__);
4527                                         }
4528                                         /*------DEBUG LOG END------*/
4529                                         memcpy(recv_data.receive_buffer1 + data_remain_size, recvbuffer.data(), recvlen);
4530                                         /*-------- DEBUG LOG --------*/
4531                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4532                                                 std::string datadump;
4533                                                 dump_memory(recv_data.receive_buffer1 + data_remain_size, recvlen, datadump);
4534
4535                                                 boost::format formatter(
4536                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4537                                                         "handle_realserver_recv() : after memcpy (data dump) : "
4538                                                         "data begin = %d, data_size = %d, data = %s");
4539                                                 formatter % data_remain_size % recvlen % datadump;
4540                                                 putLogDebug(100163, formatter.str(), __FILE__, __LINE__);
4541                                         }
4542                                         /*------DEBUG LOG END------*/
4543                                         //set buffer1 as using buffer
4544                                         recv_data.receive_buffer = recv_data.receive_buffer1;
4545                                 }
4546                         }
4547
4548                         //set buffer's rest size
4549                         recv_data.receive_buffer_rest_size = recv_data.receive_buffer_max_size - data_remain_size - recvlen;
4550
4551                         //remain_size recalc
4552                         data_remain_size += recvlen;
4553
4554                         send_status_it it_begin = recv_data.send_status_list.begin();
4555                         send_status_it it_end = recv_data.send_status_list.end();
4556                         //offset recalc
4557                         for (; it_begin != it_end; ++it_begin) {
4558                                 it_begin->send_offset -= data_remain_start;
4559                         }
4560                 }
4561                 //buffer's rest size >= request size
4562                 //copy directly
4563                 else {
4564                         //pointer valid check
4565                         if (unlikely(recv_data.receive_buffer == NULL)) {
4566                                 boost::format formatter("Invalid pointer. thread id : %d.");
4567                                 formatter % boost::this_thread::get_id();
4568                                 putLogError(100085, formatter.str(), __FILE__, __LINE__);
4569                                 throw - 1;
4570                         }
4571                         /*-------- DEBUG LOG --------*/
4572                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4573                                 std::string datadump;
4574                                 dump_memory(recvbuffer.data(), recvlen, datadump);
4575                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4576                                                         "handle_realserver_recv() : before memcpy (data dump) : "
4577                                                         "data begin = 0, data_size = %d, data = %s");
4578                                 formatter % recvlen % datadump;
4579                                 putLogDebug(100164, formatter.str(), __FILE__, __LINE__);
4580                         }
4581                         /*------DEBUG LOG END------*/
4582
4583                         //copy data from parameter to using buffer
4584                         memcpy(recv_data.receive_buffer + recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size,
4585                                recvbuffer.data(), recvlen);
4586                         /*-------- DEBUG LOG --------*/
4587                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4588                                 std::string datadump;
4589                                 dump_memory(recv_data.receive_buffer + recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size,
4590                                             recvlen, datadump);
4591                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4592                                                         "handle_realserver_recv() : before memcpy (data dump) : "
4593                                                         "data begin = %d, data_size = %d, data = %s");
4594                                 formatter % (recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size)
4595                                 % recvlen % datadump;
4596                                 putLogDebug(100165, formatter.str(), __FILE__, __LINE__);
4597                         }
4598                         /*------DEBUG LOG END------*/
4599                         //buffer's rest size recalc
4600                         recv_data.receive_buffer_rest_size -= recvlen;
4601                         //remain data size recalc
4602                         data_remain_size += recvlen;
4603                 }
4604
4605                 it = recv_data.send_status_list.begin();
4606                 it_end = recv_data.send_status_list.end();
4607                 //request rest size initialization
4608                 request_data_remain_size = recvlen;
4609                 //original status process
4610                 for (; it != it_end; ++it) {
4611                         //status is SEND_CONTINUE
4612                         if (it->status == SEND_CONTINUE) {
4613                                 //send rest size > request size
4614                                 if (it->send_rest_size > request_data_remain_size) {
4615                                         //send possible size recalc
4616                                         it->send_possible_size = request_data_remain_size;
4617                                         //send rest size recalc
4618                                         it->send_rest_size -= request_data_remain_size;
4619                                         //send end size recalc
4620                                         it->send_end_size = 0;
4621                                         //request size recalc
4622                                         request_data_remain_size = 0;
4623                                 }
4624                                 //send rest size <= request size
4625                                 else {
4626                                         //send possible size recalc
4627                                         it->send_possible_size = it->send_rest_size;
4628                                         //send rest size recalc
4629                                         request_data_remain_size -= it->send_rest_size;
4630                                         //send end size recalc
4631                                         it->send_end_size = 0;
4632                                         //request size recalc
4633                                         it->send_rest_size = 0;
4634                                 }
4635                                 //change status from SEND_CONTINUE to SEND_OK
4636                                 it->status = SEND_OK;
4637                         }
4638                         //status is SEND_NG
4639                         else if (it->status == SEND_NG) {
4640                                 if (forwarded_for == FORWARDED_FOR_ON) {
4641                                         //check http method
4642                                         check_result = check_status_code(recv_data.receive_buffer + it->send_offset, data_remain_size);
4643                                         /*-------- DEBUG LOG --------*/
4644                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4645                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4646                                                                         "handle_realserver_recv() : call check_http_method : "
4647                                                                         "return_value = %d. thread id : %d.");
4648                                                 formatter % check_result % boost::this_thread::get_id();
4649                                                 putLogDebug(100166, formatter.str(), __FILE__, __LINE__);
4650                                         }
4651                                         /*------DEBUG LOG END------*/
4652                                         //check http method result is CHECK_OK
4653                                         if (check_result == CHECK_OK) {
4654                                                 //check http version
4655                                                 check_result = check_http_version(recv_data.receive_buffer + it->send_offset, data_remain_size);
4656                                                 /*-------- DEBUG LOG --------*/
4657                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4658                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4659                                                                                 "handle_realserver_recv() : call check_http_version : "
4660                                                                                 "return_value = %d. thread id : %d.");
4661                                                         formatter % check_result % boost::this_thread::get_id();
4662                                                         putLogDebug(100167, formatter.str(), __FILE__, __LINE__);
4663                                                 }
4664                                                 /*------DEBUG LOG END------*/
4665                                         }
4666                                         //check method and version result is CHECK_OK
4667                                         if (check_result == CHECK_OK) {
4668                                                 //search http header
4669                                                 bret = find_http_header(recv_data.receive_buffer + it->send_offset, data_remain_size, http_header,
4670                                                                         header_offset, header_offset_len);
4671                                                 /*-------- DEBUG LOG --------*/
4672                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4673                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4674                                                                                 "handle_realserver_recv() : call find_http_header : "
4675                                                                                 "return_value = %d. thread id : %d.");
4676                                                         formatter % static_cast<int>(bret) % boost::this_thread::get_id();
4677                                                         putLogDebug(100168, formatter.str(), __FILE__, __LINE__);
4678                                                 }
4679                                                 /*------DEBUG LOG END------*/
4680                                                 //search http header result is OK
4681                                                 if (bret) {
4682                                                         //search Content_Length header
4683                                                         bret = find_http_header(recv_data.receive_buffer + it->send_offset, data_remain_size,
4684                                                                                 content_header, content_length_header_offset, content_length_header_len);
4685                                                         /*-------- DEBUG LOG --------*/
4686                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4687                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4688                                                                                         "handle_realserver_recv() : call find_http_header : "
4689                                                                                         "return_value = %d. thread id : %d.");
4690                                                                 formatter % static_cast<int>(bret) % boost::this_thread::get_id();
4691                                                                 putLogDebug(100169, formatter.str(), __FILE__, __LINE__);
4692                                                         }
4693                                                         /*------DEBUG LOG END------*/
4694                                                         //search Content_Length result is OK
4695                                                         if (bret) {
4696                                                                 //Get Content_Length header's numeric value
4697                                                                 for (pos = 0; recv_data.receive_buffer[it->send_offset + content_length_header_offset + pos] != ':' && pos
4698                                                                      < content_length_header_len; ++pos)
4699                                                                         ;
4700                                                                 if (pos == content_length_header_len) {
4701                                                                         throw std::string("Content_Length field's value is invalid.");
4702                                                                 }
4703
4704                                                                 ++pos;
4705
4706                                                                 str_value.assign(recv_data.receive_buffer + it->send_offset + content_length_header_offset + pos,
4707                                                                                  content_length_header_len - pos);
4708
4709                                                                 size_t pos_end = str_value.find_last_of('\r');
4710                                                                 if (pos_end != std::string::npos) {
4711                                                                         str_value = str_value.erase(pos_end);
4712                                                                 }
4713
4714                                                                 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
4715                                                                         ;
4716
4717                                                                 str_value = str_value.substr(pos);
4718
4719                                                                 try {
4720                                                                         content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
4721                                                                 } catch (const boost::bad_lexical_cast &ex) {
4722                                                                         throw std::string("Content_Length field's value is invalid.");
4723                                                                 }
4724                                                                 //send_rest_size recalc
4725                                                                 //set whole http header's length + Content_Length's value
4726                                                                 it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
4727                                                         }
4728                                                         //search Content_Length result is NG
4729                                                         else {
4730                                                                 //send_rest_size recalc
4731                                                                 //set whole http header's length
4732                                                                 if (header_offset_len == 0) {
4733                                                                         it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
4734                                                                 } else {
4735                                                                         it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
4736                                                                 }
4737                                                         }
4738                                                 }
4739                                                 //search http header result is CHECK_NG
4740                                                 else {
4741                                                         it->unsend_size += request_data_remain_size;
4742                                                         request_data_remain_size = 0;
4743                                                         break;
4744                                                 }
4745                                         }
4746                                         //check method and version result is CHECK_NG
4747                                         else if (check_result == CHECK_NG) {
4748                                                 //send_rest_size recalc
4749                                                 it->send_rest_size = it->unsend_size + request_data_remain_size;
4750                                         }
4751                                         //check method and version result is CHECK_IMPOSSIBLE
4752                                         else {
4753                                                 it->unsend_size += request_data_remain_size;
4754                                                 request_data_remain_size = 0;
4755                                                 break;
4756                                         }
4757                                 } else {
4758                                         //send_rest_size recalc
4759                                         it->send_rest_size = it->unsend_size + request_data_remain_size;
4760                                 }
4761
4762                                 //recalc fields value according to send_rest_size and request rest size
4763                                 if (it->send_rest_size > it->unsend_size + request_data_remain_size) {
4764                                         it->send_possible_size = it->unsend_size + request_data_remain_size;
4765                                         it->send_rest_size -= (it->unsend_size + request_data_remain_size);
4766                                         it->send_end_size = 0;
4767                                         it->unsend_size = 0;
4768                                         request_data_remain_size = 0;
4769                                 } else {
4770                                         it->send_possible_size = it->send_rest_size;
4771                                         request_data_remain_size = it->unsend_size + request_data_remain_size - it->send_rest_size;
4772                                         it->send_end_size = 0;
4773                                         it->unsend_size = 0;
4774                                         it->send_rest_size = 0;
4775                                 }
4776
4777                                 //change status from SEND_NG to SEND_OK
4778                                 it->status = SEND_OK;
4779                         }
4780                         //no request rest data to process
4781                         if (request_data_remain_size <= 0) {
4782                                 break;
4783                         }
4784                 }
4785
4786                 /*-------- DEBUG LOG --------*/
4787                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4788                         std::string datadump;
4789                         boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
4790                                                 "send_rest_size = %d, send_possible_size = %d, "
4791                                                 "send_offset = %d, unsend_size = %d, edit_division = %d.");
4792                         int i = 0;
4793                         for (it = recv_data.send_status_list.begin();
4794                              it != recv_data.send_status_list.end();
4795                              ++it, ++i) {
4796                                 formatter % i % it->status % it->send_end_size
4797                                 % it->send_rest_size % it->send_possible_size
4798                                 % it->send_offset % it->unsend_size % it->edit_division;
4799                                 datadump += formatter.str();
4800                         }
4801
4802                         formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4803                                         "handle_realserver_recv() : send status list dump : send status list size = %d.%s");
4804
4805                         formatter % recv_data.send_status_list.size() % datadump;
4806                         putLogDebug(100170, formatter.str(), __FILE__, __LINE__);
4807                 }
4808                 /*------DEBUG LOG END------*/
4809                 //there are still rest data need to process
4810                 //new status created and add to status list
4811                 while (request_data_remain_size > 0) {
4812                         //new status created
4813                         send_status new_send_state;
4814                         new_send_state.edit_division = EDIT_DIVISION_NO_EDIT;
4815                         new_send_state.send_end_size = 0;
4816                         new_send_state.send_offset = 0;
4817                         new_send_state.send_possible_size = 0;
4818                         new_send_state.unsend_size = 0;
4819                         new_send_state.send_rest_size = 0;
4820                         //status initialize to SEND_NG
4821                         new_send_state.status = SEND_NG;
4822                         //add new status to status_list
4823                         recv_data.send_status_list.push_back(new_send_state);
4824                         std::list<send_status>::reverse_iterator new_send_it = recv_data.send_status_list.rbegin();
4825                         //calc offset
4826                         new_send_it->send_offset = recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size
4827                                                    - request_data_remain_size;
4828
4829                         if (forwarded_for == FORWARDED_FOR_ON) {
4830                                 //check http method
4831                                 check_result = check_status_code(recv_data.receive_buffer + new_send_it->send_offset,
4832                                                                  request_data_remain_size);
4833                                 /*-------- DEBUG LOG --------*/
4834                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4835                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4836                                                                 "handle_realserver_recv() : call check_http_method : "
4837                                                                 "return_value = %d. thread id : %d.");
4838                                         formatter % check_result % boost::this_thread::get_id();
4839                                         putLogDebug(100171, formatter.str(), __FILE__, __LINE__);
4840                                 }
4841                                 /*------DEBUG LOG END------*/
4842                                 //check http method result is CHECK_OK
4843                                 if (check_result == CHECK_OK) {
4844                                         //check http version
4845                                         check_result = check_http_version(recv_data.receive_buffer + new_send_it->send_offset,
4846                                                                           request_data_remain_size);
4847                                         /*-------- DEBUG LOG --------*/
4848                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4849                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4850                                                                         "handle_realserver_recv() : call check_http_version : "
4851                                                                         "return_value = %d. thread id : %d.");
4852                                                 formatter % check_result % boost::this_thread::get_id();
4853                                                 putLogDebug(100172, formatter.str(), __FILE__, __LINE__);
4854                                         }
4855                                         /*------DEBUG LOG END------*/
4856                                 }
4857                                 //check http method and version result is CHECK_OK
4858                                 if (check_result == CHECK_OK) {
4859                                         //search whole http header, get whole http header's offset and length
4860                                         bret = find_http_header(recv_data.receive_buffer + new_send_it->send_offset, request_data_remain_size,
4861                                                                 http_header, header_offset, header_offset_len);
4862                                         /*-------- DEBUG LOG --------*/
4863                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4864                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4865                                                                         "handle_realserver_recv() : call find_http_header : "
4866                                                                         "return_value = %d. thread id : %d.");
4867                                                 formatter % static_cast<int>(bret) % boost::this_thread::get_id();
4868                                                 putLogDebug(100173, formatter.str(), __FILE__, __LINE__);
4869                                         }
4870                                         /*------DEBUG LOG END------*/
4871                                         //searched whole http header
4872                                         if (bret) {
4873                                                 //search ContentLength http header, get ContentLength header's offset and length
4874                                                 bret = find_http_header(recv_data.receive_buffer + new_send_it->send_offset,
4875                                                                         request_data_remain_size, content_header, content_length_header_offset, content_length_header_len);
4876                                                 /*-------- DEBUG LOG --------*/
4877                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4878                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4879                                                                                 "handle_realserver_recv() : call find_http_header : "
4880                                                                                 "return_value = %d. thread id : %d.");
4881                                                         formatter % static_cast<int>(bret) % boost::this_thread::get_id();
4882                                                         putLogDebug(100174, formatter.str(), __FILE__, __LINE__);
4883                                                 }
4884                                                 /*------DEBUG LOG END------*/
4885
4886                                                 //searched ContentLength http header
4887                                                 if (bret) {
4888                                                         //Get Content_Length header's numeric value
4889                                                         for (pos = 0; recv_data.receive_buffer[new_send_it->send_offset + content_length_header_offset + pos] != ':'
4890                                                              && pos < content_length_header_len; ++pos)
4891                                                                 ;
4892                                                         if (pos == content_length_header_len) {
4893                                                                 throw std::string("Content_Length field's value is invalid.");
4894                                                         }
4895
4896                                                         ++pos;
4897
4898                                                         str_value.assign(recv_data.receive_buffer + new_send_it->send_offset + content_length_header_offset + pos,
4899                                                                          content_length_header_len - pos);
4900
4901                                                         size_t pos_end = str_value.find_last_of('\r');
4902                                                         if (pos_end != std::string::npos) {
4903                                                                 str_value = str_value.erase(pos_end);
4904                                                         }
4905
4906                                                         for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
4907                                                                 ;
4908
4909                                                         str_value = str_value.substr(pos);
4910                                                         try {
4911                                                                 content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
4912                                                         } catch (const boost::bad_lexical_cast &ex) {
4913                                                                 throw std::string("Content_Length field's value is invalid.");
4914                                                         }
4915                                                         //send_rest_size recalc
4916                                                         //set whole http header's  + whole http header's length + Content_Length's value
4917                                                         new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
4918                                                 }
4919                                                 //not searched ContentLength http header
4920                                                 else {
4921                                                         //send_rest_size recalc
4922                                                         //set whole http header's  + whole http header's length
4923                                                         if (header_offset_len == 0) {
4924                                                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
4925                                                         } else {
4926                                                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
4927                                                         }
4928
4929                                                 }
4930                                         }
4931                                         //not searched whole http header
4932                                         else {
4933                                                 new_send_it->unsend_size = request_data_remain_size;
4934                                                 request_data_remain_size = 0;
4935                                                 break;
4936                                         }
4937                                 }
4938                                 //check http method or version result is CHECK_NG
4939                                 else if (check_result == CHECK_NG) {
4940                                         new_send_it->send_rest_size = request_data_remain_size;
4941                                 }
4942                                 //check http method or version result is CHECK_IMPOSSIBLE
4943                                 else {
4944                                         new_send_it->unsend_size = request_data_remain_size;
4945                                         request_data_remain_size = 0;
4946                                         break;
4947                                 }
4948                         } else {
4949                                 new_send_it->send_rest_size = request_data_remain_size;
4950                         }
4951
4952                         //recalc fields value according to send_rest_size and request rest size
4953                         if (new_send_it->send_rest_size > request_data_remain_size) {
4954                                 new_send_it->send_possible_size = request_data_remain_size;
4955                                 new_send_it->send_rest_size -= request_data_remain_size;
4956                                 new_send_it->send_end_size = 0;
4957                                 new_send_it->send_end_size = 0;
4958                                 request_data_remain_size = 0;
4959                         } else {
4960                                 new_send_it->send_possible_size = new_send_it->send_rest_size;
4961                                 request_data_remain_size -= new_send_it->send_rest_size;
4962                                 new_send_it->send_end_size = 0;
4963                                 new_send_it->send_rest_size = 0;
4964                         }
4965
4966                         //change status from SEND_NG to SEND_OK
4967                         new_send_it->status = SEND_OK;
4968                 }
4969                 /*-------- DEBUG LOG --------*/
4970                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4971                         std::string datadump;
4972                         boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
4973                                                 "send_rest_size = %d, send_possible_size = %d, "
4974                                                 "send_offset = %d, unsend_size = %d, edit_division = %d.");
4975                         int i = 0;
4976                         for (it = recv_data.send_status_list.begin();
4977                              it != recv_data.send_status_list.end();
4978                              ++it, ++i) {
4979                                 formatter % i % it->status % it->send_end_size
4980                                 % it->send_rest_size % it->send_possible_size
4981                                 % it->send_offset % it->unsend_size % it->edit_division;
4982                                 datadump += formatter.str();
4983                         }
4984
4985                         formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
4986                                         "handle_realserver_recv() : send status list dump : send status list size = %d.%s");
4987
4988                         formatter % recv_data.send_status_list.size() % datadump;
4989                         putLogDebug(100175, formatter.str(), __FILE__, __LINE__);
4990                 }
4991                 /*------DEBUG LOG END------*/
4992
4993                 //search for send_possible item in status list
4994                 send_status_it it_find = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
4995                                                  data_send_possible());
4996                 //the data that can be sent possible is exist
4997                 if (it_find != recv_data.send_status_list.end()) {
4998                         status = CLIENT_CONNECTION_CHECK;
4999                 }
5000                 //the data that can be sent possible is not exist
5001                 else {
5002                         status = REALSERVER_RECV;
5003                 }
5004         } catch (int e) {
5005                 /*-------- DEBUG LOG --------*/
5006                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5007                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5008                                                 "handle_realserver_recv() : catch exception e = %d. thread id : %d.");
5009                         formatter % e % boost::this_thread::get_id();
5010                         putLogDebug(100176, formatter.str(), __FILE__, __LINE__);
5011                 }
5012                 /*------DEBUG LOG END------*/
5013                 status = FINALIZE;
5014         } catch (const std::string &ex) {
5015                 std::cerr << "protocol_module_sessionless::handle_realserver_recv() : exception : " << ex << std::endl;
5016                 boost::format formatter("protocol_module_sessionless::handle_realserver_recv() : exception : %s. thread id : %d.");
5017                 formatter % ex.c_str() % boost::this_thread::get_id();
5018                 putLogError(100086, formatter.str(), __FILE__, __LINE__);
5019                 status = FINALIZE;
5020         } catch (const std::bad_alloc &) {
5021                 std::cerr << "protocol_module_sessionless::handle_realserver_recv() : exception : Could not allocate memory." << std::endl;
5022                 boost::format formatter("Could not allocate memory. thread id : %d.");
5023                 formatter % boost::this_thread::get_id();
5024                 putLogError(100087, formatter.str(), __FILE__, __LINE__);
5025                 status = FINALIZE;
5026         } catch (const std::exception &ex) {
5027                 std::cerr << "protocol_module_sessionless::handle_realserver_recv() : exception : error = " << ex.what() << "." << std::endl;
5028                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5029                                         "handle_realserver_recv() : exception : error = %s. thread id : %d.");
5030                 formatter % ex.what() % boost::this_thread::get_id();
5031                 putLogError(100088, formatter.str(), __FILE__, __LINE__);
5032
5033                 status = FINALIZE;
5034         } catch (...) {
5035                 std::cerr << "protocol_module_sessionless::handle_realserver_recv() : Unknown exception." << std::endl;
5036                 boost::format formatter("function : protocol_module_base::EVENT_TAG "
5037                                         "protocol_module_sessionless::handle_realserver_recv() : "
5038                                         "Unknown exception. thread id : %d.");
5039                 formatter % boost::this_thread::get_id();
5040                 putLogError(100089, formatter.str(), __FILE__, __LINE__);
5041                 status = FINALIZE;
5042         }
5043
5044         /*-------- DEBUG LOG --------*/
5045         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5046                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5047                                         "handle_realserver_recv(const boost::thread::id thread_id, "
5048                                         "const boost::asio::ip::tcp::endpoint& rs_endpoint, "
5049                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5050                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
5051                 formatter % status % boost::this_thread::get_id();
5052                 putLogDebug(100177, formatter.str(), __FILE__, __LINE__);
5053         }
5054         /*------DEBUG LOG END------*/
5055
5056         return status;
5057 }
5058
5059
5060
5061 //! called from after sorryserver receive
5062 //! @param[in]    downstream thread id
5063 //! @param[in]    sorryserver endpoint reference
5064 //! @param[in]    receive from realserver buffer reference.
5065 //! @param[in]    recv data length
5066 //! @return     session use EVENT mode
5067 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_recv(
5068         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &sorry_endpoint, const boost::array <
5069         char, MAX_BUFFER_SIZE > & recvbuffer, const size_t recvlen)
5070 {
5071         /*-------- DEBUG LOG --------*/
5072         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5073                 size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
5074                 std::string buffer;
5075                 dump_memory(recvbuffer.data(), buffer_size, buffer);
5076                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5077                                         "handle_sorryserver_recv(const boost::thread::id thread_id, "
5078                                         "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
5079                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5080                                         "const size_t recvlen) : thread_id = %d, sorry_endpoint = [%s]:%d, recvbuffer = %s, recvlen = %d.");
5081                 formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port()
5082                 % buffer % recvlen;
5083                 putLogDebug(100178, formatter.str(), __FILE__, __LINE__);
5084         }
5085         /*------DEBUG LOG END------*/
5086
5087         EVENT_TAG status = FINALIZE;
5088         size_t data_remain_start = 0;
5089         size_t data_remain_size = 0;
5090         size_t request_data_remain_size = 0;
5091         size_t header_offset = 0;
5092         size_t header_offset_len = 0;
5093         size_t content_length_header_offset = 0;
5094         size_t content_length_header_len = 0;
5095         size_t content_len_value = 0;
5096         size_t pos = 0;
5097         size_t buffer_size = 0;
5098         const size_t cr_lf_cr_lf_len = strlen("\r\n\r\n");
5099         const size_t cr_lf_len = strlen("\r\n");
5100         std::string str_value;
5101         const std::string http_header = "";
5102         const std::string content_header = "Content-Length";
5103         thread_data_ptr session_data;
5104         char *buffer1 = NULL;
5105         char *buffer2 = NULL;
5106         bool bret = false;
5107         CHECK_RESULT_TAG check_result;
5108         session_thread_data_map_it session_thread_it;
5109         receive_data_map_it receive_data_it;
5110
5111         //parameter check
5112         if (recvlen > recvbuffer.size()) {
5113                 std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : Data size bigger than buffer size." << std::endl;
5114                 boost::format formatter("Data size bigger than buffer size. thread id : %d.");
5115                 formatter % boost::this_thread::get_id();
5116                 putLogError(100090, formatter.str(), __FILE__,
5117                             __LINE__);
5118                 /*-------- DEBUG LOG --------*/
5119                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5120                         boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5121                                                 "handle_sorryserver_recv(const boost::thread::id thread_id, "
5122                                                 "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
5123                                                 "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5124                                                 "const size_t recvlen) : return_value = %d. thread id : %d.");
5125                         formatter % FINALIZE % boost::this_thread::get_id();
5126                         putLogDebug(100179, formatter.str(), __FILE__, __LINE__);
5127                 }
5128                 /*------DEBUG LOG END------*/
5129                 return FINALIZE;
5130         }
5131
5132         try {
5133                 {
5134                         boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
5135
5136                         session_thread_it = session_thread_data_map.find(thread_id);
5137                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
5138                                 boost::format formatter("Invalid thread id. thread id : %d.");
5139                                 formatter % boost::this_thread::get_id();
5140                                 putLogError(100091, formatter.str(), __FILE__, __LINE__);
5141                                 throw - 1;
5142                         }
5143
5144                         session_data = session_thread_it->second;
5145                 }
5146
5147                 receive_data_it = session_data->receive_data_map.find(sorry_endpoint);
5148                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
5149                         receive_data recv_data;
5150                         session_data->receive_data_map[sorry_endpoint] = recv_data;
5151                 }
5152
5153                 session_data->target_endpoint = sorry_endpoint;
5154
5155                 receive_data &recv_data = session_data->receive_data_map[sorry_endpoint];
5156
5157                 //status list check
5158                 send_status_it it = recv_data.send_status_list.begin();
5159                 send_status_it it_end = recv_data.send_status_list.end();
5160                 it = std::find_if(it, it_end, data_send_ok());
5161                 if (unlikely(it != it_end)) {
5162                         boost::format formatter("Sending data is invalid. thread id : %d.");
5163                         formatter % boost::this_thread::get_id();
5164                         putLogError(100092, formatter.str(), __FILE__, __LINE__);
5165                         throw - 1;
5166                 }
5167
5168                 //status list check
5169                 it = recv_data.send_status_list.begin();
5170                 it = std::adjacent_find(it, it_end, data_send_repeated());
5171                 if (unlikely(it != it_end)) {
5172                         boost::format formatter("Sending data is invalid. thread id : %d.");
5173                         formatter % boost::this_thread::get_id();
5174                         putLogError(100093, formatter.str(), __FILE__, __LINE__);
5175                         throw - 1;
5176                 }
5177
5178                 /*-------- DEBUG LOG --------*/
5179                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5180                         std::string datadump;
5181                         boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
5182                                                 "send_rest_size = %d, send_possible_size = %d, "
5183                                                 "send_offset = %d, unsend_size = %d, edit_division = %d.");
5184                         int i = 0;
5185                         for (it = recv_data.send_status_list.begin();
5186                              it != recv_data.send_status_list.end();
5187                              ++it, ++i) {
5188                                 formatter % i % it->status % it->send_end_size
5189                                 % it->send_rest_size % it->send_possible_size
5190                                 % it->send_offset % it->unsend_size % it->edit_division;
5191                                 datadump += formatter.str();
5192                         }
5193
5194                         formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5195                                         "handle_sorryserver_recv() : send status list dump : send status list size = %d.%s");
5196
5197                         formatter % recv_data.send_status_list.size() % datadump;
5198                         putLogDebug(100180, formatter.str(), __FILE__, __LINE__);
5199                 }
5200                 /*------DEBUG LOG END------*/
5201
5202                 it = recv_data.send_status_list.begin();
5203                 //get original status info
5204                 while (it != it_end) {
5205                         //item status is SEND_END
5206                         if (it->status == SEND_END) {
5207                                 //erase from list
5208                                 recv_data.send_status_list.erase(it++);
5209                                 continue;
5210                         }
5211                         //item status is SEND_CONTINUE
5212                         else if (it->status == SEND_CONTINUE) {
5213                                 it->send_offset += it->send_end_size;
5214                                 data_remain_start = it->send_offset;
5215                                 break;
5216                         }
5217                         //item status is SEND_NG
5218                         else {
5219                                 data_remain_start = it->send_offset;
5220                                 data_remain_size = it->unsend_size;
5221                                 break;
5222                         }
5223
5224                         ++it;
5225                 }
5226                 /*-------- DEBUG LOG --------*/
5227                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5228                         std::string datadump;
5229                         boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
5230                                                 "send_rest_size = %d, send_possible_size = %d, "
5231                                                 "send_offset = %d, unsend_size = %d, edit_division = %d.");
5232                         int i = 0;
5233                         for (it = recv_data.send_status_list.begin();
5234                              it != recv_data.send_status_list.end();
5235                              ++it, ++i) {
5236                                 formatter % i % it->status % it->send_end_size
5237                                 % it->send_rest_size % it->send_possible_size
5238                                 % it->send_offset % it->unsend_size % it->edit_division;
5239                                 datadump += formatter.str();
5240                         }
5241
5242                         formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5243                                         "handle_sorryserver_recv() : send status list dump : send status list size = %d.%s");
5244
5245                         formatter % recv_data.send_status_list.size() % datadump;
5246                         putLogDebug(100181, formatter.str(), __FILE__, __LINE__);
5247                 }
5248                 /*------DEBUG LOG END------*/
5249                 //receive buffer process
5250                 //buffer rest size < request size
5251                 if (recv_data.receive_buffer_rest_size < recvlen) {
5252                         //buffer max size < remain size + request size
5253                         //buffer is need reallocate
5254                         if (recv_data.receive_buffer_max_size < data_remain_size + recvlen) {
5255                                 //the buffer's size that will be allocated is exceed the upper limit value
5256                                 if (MAX_SESSIONLESS_MODULE_BUFFER_SIZE < data_remain_size + recvlen) {
5257                                         std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : the buffer's size that will be allocated is exceed the upper limit value." << std::endl;
5258                                         boost::format formatter("The buffer's size that will be allocated is exceed the upper limit value. thread id : %d.");
5259                                         formatter % boost::this_thread::get_id();
5260                                         putLogError(100094, formatter.str(), __FILE__, __LINE__);
5261                                         /*-------- DEBUG LOG --------*/
5262                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5263                                                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5264                                                                         "handle_sorryserver_recv(const boost::thread::id thread_id, "
5265                                                                         "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
5266                                                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
5267                                                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
5268                                                 formatter % FINALIZE % boost::this_thread::get_id();
5269                                                 putLogDebug(100182, formatter.str(), __FILE__, __LINE__);
5270                                         }
5271                                         /*------DEBUG LOG END------*/
5272                                         return FINALIZE;
5273                                 }
5274                                 //receive_buffer1's memory allocate and initialization
5275                                 buffer_size = (data_remain_size + recvlen) > MAX_BUFFER_SIZE ? (data_remain_size + recvlen) : MAX_BUFFER_SIZE;
5276                                 buffer1 = new char[buffer_size];
5277                                 /*-------- DEBUG LOG --------*/
5278                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5279                                         boost::format formatter("new : address = &(%d), size = %lu.");
5280                                         formatter % static_cast<void *>(buffer1) % buffer_size;
5281                                         putLogDebug(100183, formatter.str(), __FILE__, __LINE__);
5282                                 }
5283                                 /*-------- DEBUG LOG END--------*/
5284                                 memset(buffer1, 0, buffer_size);
5285                                 //receive_buffer2's memory allocate and initialization
5286                                 buffer2 = new char[buffer_size];
5287                                 /*-------- DEBUG LOG --------*/
5288                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5289                                         boost::format formatter("new : address = &(%d), size = %lu.");
5290                                         formatter % static_cast<void *>(buffer2) % buffer_size;
5291                                         putLogDebug(100184, formatter.str(), __FILE__, __LINE__);
5292                                 }
5293                                 /*-------- DEBUG LOG END--------*/
5294                                 memset(buffer2, 0, buffer_size);
5295
5296                                 /*-------- DEBUG LOG --------*/
5297                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5298                                         std::string datadump;
5299                                         dump_memory(recv_data.receive_buffer + data_remain_start, data_remain_size, datadump);
5300                                         boost::format formatter(
5301                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5302                                                 "handle_sorryserver_recv() : before memcpy (data dump) : "
5303                                                 "data begin = %d, data_size = %d, data = %s");
5304                                         formatter % data_remain_start % data_remain_size % datadump;
5305                                         putLogDebug(100185, formatter.str(), __FILE__, __LINE__);
5306                                 }
5307                                 /*------DEBUG LOG END------*/
5308                                 //copy data from old buffer to new buffer
5309                                 memcpy(buffer1, recv_data.receive_buffer + data_remain_start, data_remain_size);
5310                                 /*-------- DEBUG LOG --------*/
5311                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5312                                         std::string datadump;
5313                                         dump_memory(buffer1, data_remain_size, datadump);
5314                                         boost::format formatter(
5315                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5316                                                 "handle_sorryserver_recv() : after memcpy (data dump) : "
5317                                                 "data begin = 0, data_size = %d, data = %s");
5318                                         formatter % data_remain_size % datadump;
5319                                         putLogDebug(100186, formatter.str(), __FILE__, __LINE__);
5320                                 }
5321                                 /*------DEBUG LOG END------*/
5322
5323                                 /*-------- DEBUG LOG --------*/
5324                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5325                                         std::string datadump;
5326                                         dump_memory(recvbuffer.data(), recvlen, datadump);
5327                                         boost::format formatter(
5328                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5329                                                 "handle_sorryserver_recv() : before memcpy (data dump) : "
5330                                                 "data begin = 0, data_size = %d, data = %s");
5331                                         formatter % recvlen % datadump;
5332                                         putLogDebug(100187, formatter.str(), __FILE__, __LINE__);
5333                                 }
5334                                 /*------DEBUG LOG END------*/
5335                                 memcpy(buffer1 + data_remain_size, recvbuffer.data(), recvlen);
5336                                 /*-------- DEBUG LOG --------*/
5337                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5338                                         std::string datadump;
5339                                         dump_memory(buffer1 + data_remain_size, recvlen, datadump);
5340                                         boost::format formatter(
5341                                                 "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5342                                                 "handle_sorryserver_recv() : after memcpy (data dump) : "
5343                                                 "data begin = %d, data_size = %d, data = %s");
5344                                         formatter % data_remain_size % recvlen % datadump;
5345                                         putLogDebug(100188, formatter.str(), __FILE__, __LINE__);
5346                                 }
5347                                 /*------DEBUG LOG END------*/
5348                                 //free old buffer1 and old buffer2
5349                                 if (recv_data.receive_buffer1 != NULL) {
5350                                         /*-------- DEBUG LOG --------*/
5351                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5352                                                 boost::format formatter("delete : address = &(%d).");
5353                                                 formatter % static_cast<void *>(recv_data.receive_buffer1);
5354                                                 putLogDebug(100189, formatter.str(), __FILE__,
5355                                                             __LINE__);
5356                                         }
5357                                         /*------DEBUG LOG END------*/
5358                                         delete[] recv_data.receive_buffer1;
5359                                         recv_data.receive_buffer1 = NULL;
5360                                 }
5361
5362                                 if (recv_data.receive_buffer2 != NULL) {
5363                                         /*-------- DEBUG LOG --------*/
5364                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5365                                                 boost::format formatter("delete : address = &(%d).");
5366                                                 formatter % static_cast<void *>(recv_data.receive_buffer2);
5367                                                 putLogDebug(100190, formatter.str(), __FILE__,
5368                                                             __LINE__);
5369                                         }
5370                                         /*------DEBUG LOG END------*/
5371                                         delete[] recv_data.receive_buffer2;
5372                                         recv_data.receive_buffer2 = NULL;
5373                                 }
5374
5375                                 //set new buffer pointer
5376                                 recv_data.receive_buffer1 = buffer1;
5377                                 recv_data.receive_buffer2 = buffer2;
5378                                 recv_data.receive_buffer = recv_data.receive_buffer1;
5379                                 //set new buffer's max size
5380                                 recv_data.receive_buffer_max_size = buffer_size;
5381                         }
5382                         //buffer's max size >= remain data size + request size
5383                         //buffer isn't need reallocate, but switch
5384                         else {
5385                                 //pointer valid check
5386                                 if (unlikely(recv_data.receive_buffer1 == NULL || recv_data.receive_buffer2 == NULL)) {
5387                                         boost::format formatter("Invalid pointer. thread id : %d.");
5388                                         formatter % boost::this_thread::get_id();
5389                                         putLogError(100095, formatter.str(), __FILE__, __LINE__);
5390                                         throw - 1;
5391                                 }
5392                                 //using buffer is buffer1
5393                                 if (recv_data.receive_buffer == recv_data.receive_buffer1) {
5394                                         //buffer2 initialization
5395                                         memset(recv_data.receive_buffer2, 0, recv_data.receive_buffer_max_size);
5396                                         /*-------- DEBUG LOG --------*/
5397                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5398                                                 std::string datadump;
5399                                                 dump_memory(recv_data.receive_buffer + data_remain_start, data_remain_size, datadump);
5400                                                 boost::format formatter(
5401                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5402                                                         "handle_sorryserver_recv() : before memcpy (data dump) : "
5403                                                         "data begin = %d, data_size = %d, data = %s");
5404                                                 formatter % data_remain_start % data_remain_size % datadump;
5405                                                 putLogDebug(100191, formatter.str(), __FILE__, __LINE__);
5406                                         }
5407                                         /*------DEBUG LOG END------*/
5408                                         //copy data from buffer1 to buffer2
5409                                         memcpy(recv_data.receive_buffer2, recv_data.receive_buffer + data_remain_start, data_remain_size);
5410                                         /*-------- DEBUG LOG --------*/
5411                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5412                                                 std::string datadump;
5413                                                 dump_memory(recv_data.receive_buffer2, data_remain_size, datadump);
5414                                                 boost::format formatter(
5415                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5416                                                         "handle_sorryserver_recv() : after memcpy (data dump) : "
5417                                                         "data begin = 0, data_size = %d, data = %s");
5418                                                 formatter % data_remain_size % datadump;
5419                                                 putLogDebug(100192, formatter.str(), __FILE__, __LINE__);
5420                                         }
5421                                         /*------DEBUG LOG END------*/
5422                                         /*-------- DEBUG LOG --------*/
5423                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5424                                                 std::string datadump;
5425                                                 dump_memory(recvbuffer.data(), recvlen, datadump);
5426                                                 boost::format formatter(
5427                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5428                                                         "handle_sorryserver_recv() : before memcpy (data dump) : "
5429                                                         "data begin = 0, data_size = %d, data = %s");
5430                                                 formatter % recvlen % datadump;
5431                                                 putLogDebug(100193, formatter.str(), __FILE__, __LINE__);
5432                                         }
5433                                         /*------DEBUG LOG END------*/
5434                                         memcpy(recv_data.receive_buffer2 + data_remain_size, recvbuffer.data(), recvlen);
5435                                         /*-------- DEBUG LOG --------*/
5436                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5437                                                 std::string datadump;
5438                                                 dump_memory(recv_data.receive_buffer2 + data_remain_size, recvlen, datadump);
5439                                                 boost::format formatter(
5440                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5441                                                         "handle_sorryserver_recv() : after memcpy (data dump) : "
5442                                                         "data begin = %d, data_size = %d, data = %s");
5443                                                 formatter % data_remain_size % recvlen % datadump;
5444                                                 putLogDebug(100194, formatter.str(), __FILE__, __LINE__);
5445                                         }
5446                                         /*------DEBUG LOG END------*/
5447                                         //set buffer2 as using buffer
5448                                         recv_data.receive_buffer = recv_data.receive_buffer2;
5449                                 }
5450                                 //using buffer is buffer2
5451                                 else {
5452                                         //buffer1 initialization
5453                                         memset(recv_data.receive_buffer1, 0, recv_data.receive_buffer_max_size);
5454                                         /*-------- DEBUG LOG --------*/
5455                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5456                                                 std::string datadump;
5457                                                 dump_memory(recv_data.receive_buffer + data_remain_start, data_remain_size, datadump);
5458                                                 boost::format formatter(
5459                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5460                                                         "handle_sorryserver_recv() : before memcpy (data dump) : "
5461                                                         "data begin = %d, data_size = %d, data = %s");
5462                                                 formatter % data_remain_start % data_remain_size % datadump;
5463                                                 putLogDebug(100195, formatter.str(), __FILE__, __LINE__);
5464                                         }
5465                                         /*------DEBUG LOG END------*/
5466                                         //copy data from buffer2 to buffer1
5467                                         memcpy(recv_data.receive_buffer1, recv_data.receive_buffer + data_remain_start, data_remain_size);
5468                                         /*-------- DEBUG LOG --------*/
5469                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5470                                                 std::string datadump;
5471                                                 dump_memory(recv_data.receive_buffer1, data_remain_size, datadump);
5472                                                 boost::format formatter(
5473                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5474                                                         "handle_sorryserver_recv() : after memcpy (data dump) : "
5475                                                         "data begin = 0, data_size = %d, data = %s");
5476                                                 formatter % data_remain_size % datadump;
5477                                                 putLogDebug(100196, formatter.str(), __FILE__, __LINE__);
5478                                         }
5479                                         /*------DEBUG LOG END------*/
5480
5481                                         /*-------- DEBUG LOG --------*/
5482                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5483                                                 std::string datadump;
5484                                                 dump_memory(recvbuffer.data(), recvlen, datadump);
5485                                                 boost::format formatter(
5486                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5487                                                         "handle_sorryserver_recv() : before memcpy (data dump) : "
5488                                                         "data begin = 0, data_size = %d, data = %s");
5489                                                 formatter % recvlen % datadump;
5490                                                 putLogDebug(100197, formatter.str(), __FILE__, __LINE__);
5491                                         }
5492                                         /*------DEBUG LOG END------*/
5493                                         memcpy(recv_data.receive_buffer1 + data_remain_size, recvbuffer.data(), recvlen);
5494                                         /*-------- DEBUG LOG --------*/
5495                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5496                                                 std::string datadump;
5497                                                 dump_memory(recv_data.receive_buffer1 + data_remain_size, recvlen, datadump);
5498                                                 boost::format formatter(
5499                                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5500                                                         "handle_sorryserver_recv() : after memcpy (data dump) : "
5501                                                         "data begin = %d, data_size = %d, data = %s");
5502                                                 formatter % data_remain_size % recvlen % datadump;
5503                                                 putLogDebug(100198, formatter.str(), __FILE__, __LINE__);
5504                                         }
5505                                         /*------DEBUG LOG END------*/
5506                                         //set buffer1 as using buffer
5507                                         recv_data.receive_buffer = recv_data.receive_buffer1;
5508                                 }
5509                         }
5510
5511                         //set buffer's rest size
5512                         recv_data.receive_buffer_rest_size = recv_data.receive_buffer_max_size - data_remain_size - recvlen;
5513
5514                         //remain_size recalc
5515                         data_remain_size += recvlen;
5516
5517                         send_status_it it_begin = recv_data.send_status_list.begin();
5518                         send_status_it it_end = recv_data.send_status_list.end();
5519                         //offset recalc
5520                         for (; it_begin != it_end; ++it_begin) {
5521                                 it_begin->send_offset -= data_remain_start;
5522                         }
5523                 }
5524                 //buffer's rest size >= request size
5525                 //copy directly
5526                 else {
5527                         //pointer valid check
5528                         if (unlikely(recv_data.receive_buffer == NULL)) {
5529                                 boost::format formatter("Invalid pointer. thread id : %d");
5530                                 formatter % boost::this_thread::get_id();
5531                                 putLogError(100096, formatter.str(), __FILE__, __LINE__);
5532                                 throw - 1;
5533                         }
5534                         /*-------- DEBUG LOG --------*/
5535                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5536                                 std::string datadump;
5537                                 dump_memory(recvbuffer.data(), recvlen, datadump);
5538                                 boost::format formatter(
5539                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5540                                         "handle_sorryserver_recv() : before memcpy (data dump) : "
5541                                         "data begin = 0, data_size = %d, data = %s");
5542                                 formatter % recvlen % datadump;
5543                                 putLogDebug(100199, formatter.str(), __FILE__, __LINE__);
5544                         }
5545                         /*------DEBUG LOG END------*/
5546
5547                         //copy data from parameter to using buffer
5548                         memcpy(recv_data.receive_buffer + recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size,
5549                                recvbuffer.data(), recvlen);
5550                         /*-------- DEBUG LOG --------*/
5551                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5552                                 std::string datadump;
5553                                 dump_memory(recv_data.receive_buffer + recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size, recvlen, datadump);
5554                                 boost::format formatter(
5555                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5556                                         "handle_sorryserver_recv() : after memcpy (data dump) : "
5557                                         "data begin = %d, data_size = %d, data = %s");
5558                                 formatter % (recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size) % recvlen % datadump;
5559                                 putLogDebug(100200, formatter.str(), __FILE__, __LINE__);
5560                         }
5561                         /*------DEBUG LOG END------*/
5562                         //buffer's rest size recalc
5563                         recv_data.receive_buffer_rest_size -= recvlen;
5564                         //remain data size recalc
5565                         data_remain_size += recvlen;
5566                 }
5567
5568                 it = recv_data.send_status_list.begin();
5569                 it_end = recv_data.send_status_list.end();
5570                 //request rest size initialization
5571                 request_data_remain_size = recvlen;
5572                 //original status process
5573                 for (; it != it_end; ++it) {
5574                         //status is SEND_CONTINUE
5575                         if (it->status == SEND_CONTINUE) {
5576                                 //
5577                                 if (it->send_rest_size > request_data_remain_size) {
5578                                         it->send_possible_size = request_data_remain_size;
5579                                         it->send_rest_size -= request_data_remain_size;
5580                                         it->send_end_size = 0;
5581                                         request_data_remain_size = 0;
5582                                 } else {
5583                                         it->send_possible_size = it->send_rest_size;
5584                                         request_data_remain_size -= it->send_rest_size;
5585                                         it->send_end_size = 0;
5586                                         it->send_rest_size = 0;
5587                                 }
5588
5589                                 //change status from SEND_CONTINUE to SEND_OK
5590                                 it->status = SEND_OK;
5591                         }
5592                         //status is SEND_NG
5593                         else if (it->status == SEND_NG) {
5594                                 if (forwarded_for == FORWARDED_FOR_ON) {
5595                                         //check http method
5596                                         check_result = check_status_code(recv_data.receive_buffer + it->send_offset, data_remain_size);
5597                                         /*-------- DEBUG LOG --------*/
5598                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5599                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5600                                                                         "handle_sorryserver_recv() : call check_status_code : "
5601                                                                         "return_value = %d. thread id : %d.");
5602                                                 formatter % check_result % boost::this_thread::get_id();
5603                                                 putLogDebug(100201, formatter.str(), __FILE__, __LINE__);
5604                                         }
5605                                         /*------DEBUG LOG END------*/
5606                                         //check http method result is CHECK_OK
5607                                         if (check_result == CHECK_OK) {
5608                                                 //check http version
5609                                                 check_result = check_http_version(recv_data.receive_buffer + it->send_offset, data_remain_size);
5610                                                 /*-------- DEBUG LOG --------*/
5611                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5612                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5613                                                                                 "handle_sorryserver_recv() : call check_http_version : "
5614                                                                                 "return_value = %d. thread id : %d.");
5615                                                         formatter % check_result % boost::this_thread::get_id();
5616                                                         putLogDebug(100202, formatter.str(), __FILE__, __LINE__);
5617                                                 }
5618                                                 /*------DEBUG LOG END------*/
5619                                         }
5620                                         //check method and version result is CHECK_OK
5621                                         if (check_result == CHECK_OK) {
5622                                                 //search http header
5623                                                 bret = find_http_header(recv_data.receive_buffer + it->send_offset, data_remain_size, http_header,
5624                                                                         header_offset, header_offset_len);
5625                                                 /*-------- DEBUG LOG --------*/
5626                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5627                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5628                                                                                 "handle_sorryserver_recv() : call find_http_header : "
5629                                                                                 "return_value = %d. thread id : %d.");
5630                                                         formatter % static_cast<int>(bret) % boost::this_thread::get_id();
5631                                                         putLogDebug(100203, formatter.str(), __FILE__, __LINE__);
5632                                                 }
5633                                                 /*------DEBUG LOG END------*/
5634                                                 //search http header result is OK
5635                                                 if (bret) {
5636                                                         //search Content_Length header
5637                                                         bret = find_http_header(recv_data.receive_buffer + it->send_offset, data_remain_size,
5638                                                                                 content_header, content_length_header_offset, content_length_header_len);
5639                                                         /*-------- DEBUG LOG --------*/
5640                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5641                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5642                                                                                         "handle_sorryserver_recv() : call find_http_header : "
5643                                                                                         "return_value = %d. thread id : %d.");
5644                                                                 formatter % static_cast<int>(bret) % boost::this_thread::get_id();
5645                                                                 putLogDebug(100204, formatter.str(), __FILE__, __LINE__);
5646                                                         }
5647                                                         /*------DEBUG LOG END------*/
5648                                                         //search Content_Length result is OK
5649                                                         if (bret) {
5650                                                                 //Get Content_Length header's numeric value
5651                                                                 for (pos = 0; recv_data.receive_buffer[it->send_offset + content_length_header_offset + pos] != ':' && pos
5652                                                                      < content_length_header_len; ++pos)
5653                                                                         ;
5654                                                                 if (pos == content_length_header_len) {
5655                                                                         throw std::string("Content_Length field's value is invalid.");
5656                                                                 }
5657
5658                                                                 ++pos;
5659
5660                                                                 str_value.assign(recv_data.receive_buffer + it->send_offset + content_length_header_offset + pos,
5661                                                                                  content_length_header_len - pos);
5662
5663                                                                 size_t pos_end = str_value.find_last_of('\r');
5664                                                                 if (pos_end != std::string::npos) {
5665                                                                         str_value = str_value.erase(pos_end);
5666                                                                 }
5667
5668                                                                 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
5669                                                                         ;
5670
5671                                                                 str_value = str_value.substr(pos);
5672
5673                                                                 try {
5674                                                                         content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
5675                                                                 } catch (const boost::bad_lexical_cast &ex) {
5676                                                                         throw std::string("Content_Length field's value is invalid.");
5677                                                                 }
5678                                                                 //send_rest_size recalc
5679                                                                 //set whole http header's length + Content_Length's value
5680                                                                 it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
5681                                                         }
5682                                                         //search Content_Length result is NG
5683                                                         else {
5684                                                                 //send_rest_size recalc
5685                                                                 //set whole http header's length
5686                                                                 if (header_offset_len == 0) {
5687                                                                         it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
5688                                                                 } else {
5689                                                                         it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
5690                                                                 }
5691                                                         }
5692                                                 }
5693                                                 //search http header result is CHECK_NG
5694                                                 else {
5695                                                         it->unsend_size += request_data_remain_size;
5696                                                         request_data_remain_size = 0;
5697                                                         break;
5698                                                 }
5699                                         }
5700                                         //check method or version result is CHECK_NG
5701                                         else if (check_result == CHECK_NG) {
5702                                                 it->send_rest_size = it->unsend_size + request_data_remain_size;
5703                                         }
5704                                         //check method and version result is CHECK_IMPOSSIBLE
5705                                         else {
5706                                                 it->unsend_size += request_data_remain_size;
5707                                                 request_data_remain_size = 0;
5708                                                 break;
5709                                         }
5710                                 } else {
5711                                         it->send_rest_size = it->unsend_size + request_data_remain_size;
5712                                 }
5713
5714                                 //recalc fields value according to send_rest_size and request rest size
5715                                 if (it->send_rest_size > it->unsend_size + request_data_remain_size) {
5716                                         it->send_possible_size = it->unsend_size + request_data_remain_size;
5717                                         it->send_rest_size -= (it->unsend_size + request_data_remain_size);
5718                                         it->send_end_size = 0;
5719                                         it->unsend_size = 0;
5720                                         request_data_remain_size = 0;
5721                                 } else {
5722                                         it->send_possible_size = it->send_rest_size;
5723                                         request_data_remain_size = it->unsend_size + request_data_remain_size - it->send_rest_size;
5724                                         it->send_end_size = 0;
5725                                         it->unsend_size = 0;
5726                                         it->send_rest_size = 0;
5727                                 }
5728
5729                                 //change status from SEND_NG to SEND_OK
5730                                 it->status = SEND_OK;
5731                         }
5732                         //no request rest data to process
5733                         if (request_data_remain_size <= 0) {
5734                                 break;
5735                         }
5736                 }
5737                 /*-------- DEBUG LOG --------*/
5738                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5739                         std::string datadump;
5740                         boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
5741                                                 "send_rest_size = %d, send_possible_size = %d, "
5742                                                 "send_offset = %d, unsend_size = %d, edit_division = %d.");
5743                         int i = 0;
5744                         for (it = recv_data.send_status_list.begin();
5745                              it != recv_data.send_status_list.end();
5746                              ++it, ++i) {
5747                                 formatter % i % it->status % it->send_end_size
5748                                 % it->send_rest_size % it->send_possible_size
5749                                 % it->send_offset % it->unsend_size % it->edit_division;
5750                                 datadump += formatter.str();
5751                         }
5752
5753                         formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5754                                         "handle_sorryserver_recv() : send status list dump : send status list size = %d.%s");
5755
5756                         formatter % recv_data.send_status_list.size() % datadump;
5757                         putLogDebug(100205, formatter.str(), __FILE__, __LINE__);
5758                 }
5759                 /*------DEBUG LOG END------*/
5760
5761                 //there are still rest data need to process
5762                 //new status created and add to status list
5763                 while (request_data_remain_size > 0) {
5764                         //new status created
5765                         send_status new_send_state;
5766                         new_send_state.edit_division = EDIT_DIVISION_NO_EDIT;
5767                         new_send_state.send_end_size = 0;
5768                         new_send_state.send_offset = 0;
5769                         new_send_state.send_possible_size = 0;
5770                         new_send_state.unsend_size = 0;
5771                         new_send_state.send_rest_size = 0;
5772                         //status initialize to SEND_NG
5773                         new_send_state.status = SEND_NG;
5774                         //add new status to status_list
5775                         recv_data.send_status_list.push_back(new_send_state);
5776                         std::list<send_status>::reverse_iterator new_send_it = recv_data.send_status_list.rbegin();
5777                         //calc offset
5778                         new_send_it->send_offset = recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size
5779                                                    - request_data_remain_size;
5780
5781                         if (forwarded_for == FORWARDED_FOR_ON) {
5782                                 //check http method
5783                                 check_result = check_status_code(recv_data.receive_buffer + new_send_it->send_offset,
5784                                                                  request_data_remain_size);
5785                                 /*-------- DEBUG LOG --------*/
5786                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5787                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5788                                                                 "handle_sorryserver_recv() : call check_status_code : "
5789                                                                 "return_value = %d. thread id : %d.");
5790                                         formatter % check_result % boost::this_thread::get_id();
5791                                         putLogDebug(100206, formatter.str(), __FILE__, __LINE__);
5792                                 }
5793                                 /*------DEBUG LOG END------*/
5794                                 //check http method result is CHECK_OK
5795                                 if (check_result == CHECK_OK) {
5796                                         //check http version
5797                                         check_result = check_http_version(recv_data.receive_buffer + new_send_it->send_offset,
5798                                                                           request_data_remain_size);
5799                                         /*-------- DEBUG LOG --------*/
5800                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5801                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5802                                                                         "handle_sorryserver_recv() : call check_http_version : "
5803                                                                         "return_value = %d. thread id : %d.");
5804                                                 formatter % check_result % boost::this_thread::get_id();
5805                                                 putLogDebug(100207, formatter.str(), __FILE__, __LINE__);
5806                                         }
5807                                         /*------DEBUG LOG END------*/
5808                                 }
5809                                 //check http method and version result is CHECK_OK
5810                                 if (check_result == CHECK_OK) {
5811                                         //search whole http header, get whole http header's offset and length
5812                                         bret = find_http_header(recv_data.receive_buffer + new_send_it->send_offset, request_data_remain_size,
5813                                                                 http_header, header_offset, header_offset_len);
5814                                         /*-------- DEBUG LOG --------*/
5815                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5816                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5817                                                                         "handle_sorryserver_recv() : call find_http_header : "
5818                                                                         "return_value = %d. thread id : %d.");
5819                                                 formatter % static_cast<int>(bret) % boost::this_thread::get_id();
5820                                                 putLogDebug(100208, formatter.str(), __FILE__, __LINE__);
5821                                         }
5822                                         /*------DEBUG LOG END------*/
5823                                         //searched whole http header
5824                                         if (bret) {
5825                                                 //search ContentLength http header, get ContentLength header's offset and length
5826                                                 bret = find_http_header(recv_data.receive_buffer + new_send_it->send_offset,
5827                                                                         request_data_remain_size, content_header, content_length_header_offset, content_length_header_len);
5828                                                 /*-------- DEBUG LOG --------*/
5829                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5830                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5831                                                                                 "handle_sorryserver_recv() : call find_http_header : "
5832                                                                                 "return_value = %d. thread id : %d.");
5833                                                         formatter % static_cast<int>(bret) % boost::this_thread::get_id();
5834                                                         putLogDebug(100209, formatter.str(), __FILE__, __LINE__);
5835                                                 }
5836                                                 /*------DEBUG LOG END------*/
5837
5838                                                 //searched ContentLength http header
5839                                                 if (bret) {
5840                                                         //Get Content_Length header's numeric value
5841                                                         for (pos = 0; recv_data.receive_buffer[new_send_it->send_offset + content_length_header_offset + pos] != ':'
5842                                                              && pos < content_length_header_len; ++pos)
5843                                                                 ;
5844                                                         if (pos == content_length_header_len) {
5845                                                                 throw std::string("Content_Length field's value is invalid.");
5846                                                         }
5847                                                         ++pos;
5848
5849                                                         str_value.assign(recv_data.receive_buffer + new_send_it->send_offset + content_length_header_offset + pos,
5850                                                                          content_length_header_len - pos);
5851
5852                                                         size_t pos_end = str_value.find_last_of('\r');
5853                                                         if (pos_end != std::string::npos) {
5854                                                                 str_value = str_value.erase(pos_end);
5855                                                         }
5856
5857                                                         for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
5858                                                                 ;
5859
5860                                                         str_value = str_value.substr(pos);
5861                                                         try {
5862                                                                 content_len_value = boost::lexical_cast<size_t>(str_value.c_str());
5863                                                         } catch (const boost::bad_lexical_cast &ex) {
5864                                                                 throw std::string("Content_Length field's value is invalid.");
5865                                                         }
5866                                                         //send_rest_size recalc
5867                                                         //set whole http header's  + whole http header's length + Content_Length's value
5868                                                         new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len + content_len_value;
5869                                                 }
5870                                                 //not searched ContentLength http header
5871                                                 else {
5872                                                         //send_rest_size recalc
5873                                                         //set whole http header's  + whole http header's length
5874                                                         if (header_offset_len == 0) {
5875                                                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_len;
5876                                                         } else {
5877                                                                 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
5878                                                         }
5879
5880
5881                                                 }
5882                                         }
5883                                         //not searched whole http header
5884                                         else {
5885                                                 new_send_it->unsend_size = request_data_remain_size;
5886                                                 request_data_remain_size = 0;
5887                                                 break;
5888                                         }
5889                                 }
5890                                 //check http method or version result is CHECK_NG
5891                                 else if (check_result == CHECK_NG) {
5892                                         new_send_it->send_rest_size = request_data_remain_size;
5893                                 }
5894                                 //check http method or version result is CHECK_IMPOSSIBLE
5895                                 else {
5896                                         new_send_it->unsend_size = request_data_remain_size;
5897                                         request_data_remain_size = 0;
5898                                         break;
5899                                 }
5900                         } else {
5901                                 new_send_it->send_rest_size = request_data_remain_size;
5902                         }
5903
5904                         //recalc fields value according to send_rest_size and request rest size
5905                         if (new_send_it->send_rest_size > request_data_remain_size) {
5906                                 new_send_it->send_possible_size = request_data_remain_size;
5907                                 new_send_it->send_rest_size -= request_data_remain_size;
5908                                 new_send_it->send_end_size = 0;
5909                                 request_data_remain_size = 0;
5910                         } else {
5911                                 new_send_it->send_possible_size = new_send_it->send_rest_size;
5912                                 request_data_remain_size -= new_send_it->send_rest_size;
5913                                 new_send_it->send_end_size = 0;
5914                                 new_send_it->send_rest_size = 0;
5915                         }
5916
5917                         //change status from SEND_NG to SEND_OK
5918                         new_send_it->status = SEND_OK;
5919                 }
5920                 /*-------- DEBUG LOG --------*/
5921                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5922                         std::string datadump;
5923                         boost::format formatter("\nitem[%d] : status = %d, send_end_size = %d, "
5924                                                 "send_rest_size = %d, send_possible_size = %d, "
5925                                                 "send_offset = %d, unsend_size = %d, edit_division = %d.");
5926                         int i = 0;
5927                         for (it = recv_data.send_status_list.begin();
5928                              it != recv_data.send_status_list.end();
5929                              ++it, ++i) {
5930                                 formatter % i % it->status % it->send_end_size
5931                                 % it->send_rest_size % it->send_possible_size
5932                                 % it->send_offset % it->unsend_size % it->edit_division;
5933                                 datadump += formatter.str();
5934                         }
5935
5936                         formatter.parse("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5937                                         "handle_sorryserver_recv() : send status list dump : send status list size = %d.%s");
5938
5939                         formatter % recv_data.send_status_list.size() % datadump;
5940                         putLogDebug(100210, formatter.str(), __FILE__, __LINE__);
5941                 }
5942                 /*------DEBUG LOG END------*/
5943
5944                 //search for send_possible item in status list
5945                 send_status_it it_find = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
5946                                                  data_send_possible());
5947                 //the data that can be sent possible is exist
5948                 if (it_find != recv_data.send_status_list.end()) {
5949                         status = CLIENT_CONNECTION_CHECK;
5950                 }
5951                 //the data that can be sent possible is not exist
5952                 else {
5953                         status = SORRYSERVER_RECV;
5954                 }
5955         } catch (int e) {
5956                 /*-------- DEBUG LOG --------*/
5957                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5958                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5959                                                 "handle_sorryserver_recv() : catch exception e = %d. thread id : %d.");
5960                         formatter % e % boost::this_thread::get_id();
5961                         putLogDebug(100211, formatter.str(), __FILE__, __LINE__);
5962                 }
5963                 /*------DEBUG LOG END------*/
5964                 status = FINALIZE;
5965         } catch (const std::string &ex) {
5966                 std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : exception : " << ex << std::endl;
5967                 boost::format formatter("protocol_module_sessionless::handle_sorryserver_recv() : exception : %s. thread id : %d.");
5968                 formatter % ex.c_str() % boost::this_thread::get_id();
5969                 putLogError(100097, formatter.str(), __FILE__, __LINE__);
5970                 status = FINALIZE;
5971         } catch (const std::bad_alloc &) {
5972                 std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : exception : Could not allocate memory." << std::endl;
5973                 boost::format formatter("Could not allocate memory. thread id : %d.");
5974                 formatter % boost::this_thread::get_id();
5975                 putLogError(100098, formatter.str(), __FILE__, __LINE__);
5976                 status = FINALIZE;
5977         } catch (const std::exception &ex) {
5978                 std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : exception : error = " << ex.what() << "." << std::endl;
5979                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5980                                         "handle_sorryserver_recv() : exception : error = %s. thread id : %d.");
5981                 formatter % ex.what() % boost::this_thread::get_id();
5982                 putLogError(100099, formatter.str(), __FILE__, __LINE__);
5983
5984                 status = FINALIZE;
5985         } catch (...) {
5986                 std::cerr << "protocol_module_sessionless::handle_sorryserver_recv() : Unknown exception." << std::endl;
5987                 boost::format formatter("function : protocol_module_base::EVENT_TAG "
5988                                         "protocol_module_sessionless::handle_sorryserver_recv() : "
5989                                         "Unknown exception. thread id : %d.");
5990                 formatter % boost::this_thread::get_id();
5991                 putLogError(100100, formatter.str(), __FILE__, __LINE__);
5992                 status = FINALIZE;
5993         }
5994
5995         /*-------- DEBUG LOG --------*/
5996         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
5997                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
5998                                         "handle_sorryserver_recv(const boost::thread::id thread_id, "
5999                                         "const boost::asio::ip::tcp::endpoint& sorry_endpoint, "
6000                                         "const boost::array<char,MAX_BUFFER_SIZE>& recvbuffer, "
6001                                         "const size_t recvlen) : return_value = %d. thread id : %d.");
6002                 formatter % status % boost::this_thread::get_id();
6003                 putLogDebug(100212, formatter.str(), __FILE__, __LINE__);
6004         }
6005         /*------DEBUG LOG END------*/
6006
6007         return status;
6008 }
6009
6010 //! called from UPSTREAM thread. make module original message.
6011 //! @param[in]    downstream thread id.
6012 //! @return     session use EVENT mode
6013 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_response_send_inform(
6014         const boost::thread::id thread_id)
6015 {
6016         /*-------- DEBUG LOG --------*/
6017         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6018                 boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6019                                         "handle_response_send_inform(const boost::thread::id thread_id) : "
6020                                         "return_value = %d. thread id : %d.");
6021                 formatter % STOP % boost::this_thread::get_id();
6022                 putLogDebug(100213, formatter.str(), __FILE__, __LINE__);
6023         }
6024         /*------DEBUG LOG END------*/
6025         return STOP;
6026 }
6027
6028 //! called from after client connection check. use TCP/IP only. create client send message.
6029 //! @param[in]    downstream thread id
6030 //! @param[out]    send buffer reference
6031 //! @param[out]    send data length
6032 //! @return     session use EVENT mode
6033 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_connection_check(
6034         const boost::thread::id thread_id, boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t &datalen)
6035 {
6036
6037         /*-------- DEBUG LOG --------*/
6038         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6039                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6040                                         "handle_client_connection_check(const boost::thread::id thread_id, "
6041                                         "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen) : "
6042                                         "thread_id = %d.");
6043                 formatter % thread_id;
6044                 putLogDebug(100214, formatter.str(), __FILE__, __LINE__);
6045         }
6046         /*------DEBUG LOG END------*/
6047
6048         EVENT_TAG status = FINALIZE;
6049         size_t send_buffer_size = sendbuffer.max_size();
6050         thread_data_ptr session_data;
6051         session_thread_data_map_it session_thread_it;
6052         receive_data_map_it receive_data_it;
6053
6054         try {
6055                 {
6056                         boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
6057
6058                         session_thread_it = session_thread_data_map.find(thread_id);
6059                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
6060                                 boost::format formatter("Invalid thread id. thread id : %d.");
6061                                 formatter % boost::this_thread::get_id();
6062                                 putLogError(100101, formatter.str(), __FILE__, __LINE__);
6063                                 throw - 1;
6064                         }
6065
6066                         session_data = session_thread_it->second;
6067                 }
6068
6069                 receive_data_it = session_data->receive_data_map.find(session_data->target_endpoint);
6070                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
6071                         boost::format formatter("Invalid endpoint. thread id : %d.");
6072                         formatter % boost::this_thread::get_id();
6073                         putLogError(100102, formatter.str(), __FILE__, __LINE__);
6074                         throw - 1;
6075                 }
6076
6077                 receive_data &recv_data = receive_data_it->second;
6078
6079                 //get the data that can be sent possible
6080                 send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
6081                                             data_send_possible());
6082                 if (unlikely(it == recv_data.send_status_list.end())) {
6083                         boost::format formatter("Sending possible data is not existed. thread id : %d.");
6084                         formatter % boost::this_thread::get_id();
6085                         putLogError(100103, formatter.str(), __FILE__, __LINE__);
6086                         throw - 1;
6087                 }
6088
6089                 //buffer size >= sending_possible size
6090                 if (send_buffer_size > it->send_possible_size) {
6091                         /*-------- DEBUG LOG --------*/
6092                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6093                                 std::string datadump;
6094                                 dump_memory(recv_data.receive_buffer + it->send_offset, it->send_possible_size, datadump);
6095
6096                                 boost::format formatter(
6097                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6098                                         "handle_client_connection_check() : before memcpy (data dump) : "
6099                                         "data begin = %d, data_size = %d, data = %s");
6100                                 formatter % it->send_offset % (it->send_possible_size) % datadump;
6101                                 putLogDebug(100215, formatter.str(), __FILE__, __LINE__);
6102                         }
6103                         /*------DEBUG LOG END------*/
6104                         //copy data from receive_buffer to sendbuffer by sending_possible size
6105                         memcpy(sendbuffer.data(), recv_data.receive_buffer + it->send_offset, it->send_possible_size);
6106                         /*-------- DEBUG LOG --------*/
6107                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6108                                 std::string datadump;
6109                                 dump_memory(sendbuffer.data(), it->send_possible_size, datadump);
6110
6111                                 boost::format formatter(
6112                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6113                                         "handle_client_connection_check() : after memcpy (data dump) : "
6114                                         "data begin = 0, data_size = %d, data = %s");
6115                                 formatter % (it->send_possible_size) % datadump;
6116                                 putLogDebug(100216, formatter.str(), __FILE__, __LINE__);
6117                         }
6118                         /*------DEBUG LOG END------*/
6119                         //send_end_size recalc
6120                         it->send_end_size = it->send_possible_size;
6121                         //set copied data length
6122                         datalen = it->send_possible_size;
6123                         //sending_possible size recalc
6124                         it->send_possible_size = 0;
6125                 }
6126                 //buffer size < sending_possible size
6127                 else {
6128                         /*-------- DEBUG LOG --------*/
6129                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6130                                 std::string datadump;
6131                                 dump_memory(recv_data.receive_buffer + it->send_offset, send_buffer_size, datadump);
6132
6133                                 boost::format formatter(
6134                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6135                                         "handle_client_connection_check() : before memcpy (data dump) : "
6136                                         "data begin = %d, data_size = %d, data = %s");
6137                                 formatter % it->send_offset % send_buffer_size % datadump;
6138                                 putLogDebug(100217, formatter.str(), __FILE__, __LINE__);
6139                         }
6140                         /*------DEBUG LOG END------*/
6141                         //copy data from receive_buffer to sendbuffer by buffer size
6142                         memcpy(sendbuffer.data(), recv_data.receive_buffer + it->send_offset, send_buffer_size);
6143                         /*-------- DEBUG LOG --------*/
6144                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6145                                 std::string datadump;
6146                                 dump_memory(sendbuffer.data(), send_buffer_size, datadump);
6147
6148                                 boost::format formatter(
6149                                         "function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6150                                         "handle_client_connection_check() : after memcpy (data dump) : "
6151                                         "data begin = 0, data_size = %d, data = %s");
6152                                 formatter % send_buffer_size % datadump;
6153                                 putLogDebug(100218, formatter.str(), __FILE__, __LINE__);
6154                         }
6155                         /*------DEBUG LOG END------*/
6156                         //send_end_size recalc
6157                         it->send_end_size = send_buffer_size;
6158                         //sending_possible size recalc
6159                         it->send_possible_size -= send_buffer_size;
6160                         //set copied data length
6161                         datalen = send_buffer_size;
6162                 }
6163
6164                 status = CLIENT_SEND;
6165         } catch (int e) {
6166                 /*-------- DEBUG LOG --------*/
6167                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6168                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6169                                                 "handle_client_connection_check() : catch exception e = %d. thread id : %d.");
6170                         formatter % e % boost::this_thread::get_id();
6171                         putLogDebug(100219, formatter.str(), __FILE__, __LINE__);
6172                 }
6173                 /*------DEBUG LOG END------*/
6174                 status = FINALIZE;
6175         } catch (const std::exception &ex) {
6176                 std::cerr << "protocol_module_sessionless::handle_client_connection_check() : exception : error = " << ex.what() << "." << std::endl;
6177                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6178                                         "handle_client_connection_check() : exception : error = %s. thread id : %d.");
6179                 formatter % ex.what() % boost::this_thread::get_id();
6180                 putLogError(100104, formatter.str(), __FILE__, __LINE__);
6181                 status = FINALIZE;
6182         } catch (...) {
6183                 std::cerr << "protocol_module_sessionless::handle_client_connection_check() : Unknown exception." << std::endl;
6184                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6185                                         "handle_client_connection_check() : Unknown exception. thread id : %d.");
6186                 formatter % boost::this_thread::get_id();
6187                 putLogError(100105, formatter.str(), __FILE__, __LINE__);
6188                 status = FINALIZE;
6189         }
6190
6191         /*-------- DEBUG LOG --------*/
6192         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6193                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6194                                         "handle_client_connection_check(const boost::thread::id thread_id, "
6195                                         "boost::array<char, MAX_BUFFER_SIZE>& sendbuffer, size_t& datalen)"
6196                                         " : return_value = %d. thread id : %d.");
6197                 formatter % status % boost::this_thread::get_id();
6198                 putLogDebug(100220, formatter.str(), __FILE__, __LINE__);
6199         }
6200         /*------DEBUG LOG END------*/
6201         return status;
6202 }
6203
6204 //! called from after client select. use UDP only
6205 //! @param[in]    downstream thread id
6206 //!    @param[in]    client udp endpoint
6207 //! @param[out]    send buffer reference
6208 //! @param[out]    send data length
6209 //! @return     session use EVENT mode
6210 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_select(
6211         const boost::thread::id thread_id, boost::asio::ip::udp::endpoint &cl_endpoint, boost::array < char,
6212         MAX_BUFFER_SIZE > & sendbuffer, size_t &datalen)
6213 {
6214         /*-------- DEBUG LOG --------*/
6215         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6216                 boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6217                                         "handle_client_select(const boost::thread::id thread_id, "
6218                                         "boost::asio::ip::udp::endpoint& cl_endpoint, "
6219                                         "boost::array<char,MAX_BUFFER_SIZE>& sendbuffer, "
6220                                         "size_t& datalen) : "
6221                                         "return_value = %d. thread id : %d.");
6222                 formatter % STOP % boost::this_thread::get_id();
6223                 putLogDebug(100221, formatter.str(), __FILE__, __LINE__);
6224         }
6225         /*------DEBUG LOG END------*/
6226         return STOP;
6227 }
6228
6229 //!    called from after client send
6230 //!    @param[in]    downstream thread id
6231 //! @return     session use EVENT mode
6232 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_send(
6233         const boost::thread::id thread_id)
6234 {
6235         /*-------- DEBUG LOG --------*/
6236         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6237                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6238                                         "handle_client_send(const boost::thread::id thread_id) : "
6239                                         "thread_id = %d.");
6240                 formatter % thread_id;
6241                 putLogDebug(100222, formatter.str(), __FILE__, __LINE__);
6242         }
6243         /*------DEBUG LOG END------*/
6244         EVENT_TAG status = FINALIZE;
6245         thread_data_ptr session_data;
6246         session_thread_data_map_it session_thread_it;
6247         receive_data_map_it receive_data_it;
6248
6249         try {
6250                 {
6251                         boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
6252                         //thread_id check
6253                         session_thread_it = session_thread_data_map.find(thread_id);
6254                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
6255                                 boost::format formatter("Invalid thread id. thread id : %d.");
6256                                 formatter % boost::this_thread::get_id();
6257                                 putLogError(100106, formatter.str(), __FILE__, __LINE__);
6258                                 throw - 1;
6259                         }
6260                         session_data = session_thread_it->second;
6261                 }
6262                 //endpoint check
6263                 receive_data_it = session_data->receive_data_map.find(session_data->target_endpoint);
6264                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
6265                         boost::format formatter("Invalid endpoint. thread id : %d.");
6266                         formatter % boost::this_thread::get_id();
6267                         putLogError(100107, formatter.str(), __FILE__, __LINE__);
6268                         throw - 1;
6269                 }
6270
6271                 receive_data &recv_data = receive_data_it->second;
6272
6273                 send_status_it it = recv_data.send_status_list.begin();
6274                 send_status_it it_end = recv_data.send_status_list.end();
6275
6276                 //check status list
6277                 it = std::adjacent_find(it, it_end, data_send_list_incorrect());
6278                 if (unlikely(it != it_end)) {
6279                         boost::format formatter("Sending possible data is invalid. thread id : %d.");
6280                         formatter % boost::this_thread::get_id();
6281                         putLogError(100108, formatter.str(), __FILE__, __LINE__);
6282                         throw - 1;
6283                 }
6284                 //status list check
6285                 it = recv_data.send_status_list.begin();
6286                 it = find_if(it, it_end, data_send_ok());
6287                 if (unlikely(it == it_end)) {
6288                         boost::format formatter("Sending possible data is not existed. thread id : %d.");
6289                         formatter % boost::this_thread::get_id();
6290                         putLogError(100109, formatter.str(), __FILE__, __LINE__);
6291                         throw - 1;
6292                 }
6293
6294                 //sending possible data is exist
6295                 if (it->send_possible_size > 0) {
6296                         //status remain SEND_OK
6297                         it->status = SEND_OK;
6298                         //offset recalc
6299                         it->send_offset += it->send_end_size;
6300                         //send_end_size recalc
6301                         it->send_end_size = 0;
6302                 }
6303                 //sending possible data is not exist
6304                 else {
6305                         //can receive from client continue
6306                         if (it->send_rest_size > 0) {
6307                                 //change status from SEND_OK to SEND_CONTINUE
6308                                 it->status = SEND_CONTINUE;
6309                         }
6310                         //can not receive from client continue
6311                         else {
6312                                 //change status from SEND_OK to SEND_END
6313                                 it->status = SEND_END;
6314                         }
6315                 }
6316
6317                 it = recv_data.send_status_list.begin();
6318                 it = find_if(it, it_end, data_send_ok());
6319                 //send_ok item is exist
6320                 if (it != it_end) {
6321                         status = CLIENT_CONNECTION_CHECK;
6322                 }
6323                 //send_ok item is not exist
6324                 else {
6325                         //end flag is on
6326                         if (session_data->end_flag == END_FLAG_ON) {
6327                                 status = CLIENT_DISCONNECT;
6328                         }
6329                         //end flag is off
6330                         else {
6331                                 //sorry flag is on
6332                                 if (session_data->sorry_flag == SORRY_FLAG_ON) {
6333                                         status = SORRYSERVER_RECV;
6334                                 }
6335                                 //sorry flag is off
6336                                 else {
6337                                         status = REALSERVER_RECV;
6338                                 }
6339                         }
6340                 }
6341         } catch (int e) {
6342                 /*-------- DEBUG LOG --------*/
6343                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6344                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6345                                                 "handle_client_send() : catch exception e = %d. thread id : %d.");
6346                         formatter % e % boost::this_thread::get_id();
6347                         putLogDebug(100223, formatter.str(), __FILE__, __LINE__);
6348                 }
6349                 /*------DEBUG LOG END------*/
6350                 status = FINALIZE;
6351         } catch (const std::exception &ex) {
6352                 std::cerr << "protocol_module_sessionless::handle_client_send() : exception : error = " << ex.what() << "." << std::endl;
6353                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6354                                         "handle_client_send() : exception : error = %s. thread id : %d.");
6355                 formatter % ex.what() % boost::this_thread::get_id();
6356                 putLogError(100110, formatter.str(), __FILE__, __LINE__);
6357                 status = FINALIZE;
6358         } catch (...) {
6359                 std::cerr << "protocol_module_sessionless::handle_client_send() : Unknown exception." << std::endl;
6360                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6361                                         "handle_client_send() : Unknown exception. thread id : %d.");
6362                 formatter % boost::this_thread::get_id();
6363                 putLogError(100111, formatter.str(), __FILE__, __LINE__);
6364                 status = FINALIZE;
6365         }
6366
6367         /*-------- DEBUG LOG --------*/
6368         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6369                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6370                                         "handle_client_send(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
6371                 formatter % status % boost::this_thread::get_id();
6372                 putLogDebug(100224, formatter.str(), __FILE__, __LINE__);
6373         }
6374         /*------DEBUG LOG END------*/
6375
6376         return status;
6377 }
6378
6379 //! call from client disconnect event. use upstream thread and downstream thread.
6380 //! @param[in]    upstream and downstream thread id( check! one thread one event! )
6381 //! @return     session use EVENT mode
6382 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_client_disconnect(
6383         const boost::thread::id thread_id)
6384 {
6385         /*-------- DEBUG LOG --------*/
6386         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6387                 boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6388                                         "handle_client_disconnect(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
6389                 formatter % FINALIZE % boost::this_thread::get_id();
6390                 putLogDebug(100225, formatter.str(), __FILE__, __LINE__);
6391         }
6392         /*------DEBUG LOG END------*/
6393         return FINALIZE;
6394 }
6395
6396 //! call from sorry mode event. use upstream thread and downstream thread
6397 //! @param[in]    upstream and downstream thread id( check! one thread one event and first time call pattern )
6398 //! @return     session use EVENT mode
6399 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorry_enable(
6400         const boost::thread::id thread_id)
6401 {
6402         /*-------- DEBUG LOG --------*/
6403         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6404                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6405                                         "handle_sorry_enable(const boost::thread::id thread_id) : "
6406                                         "thread_id = %d.");
6407                 formatter % thread_id;
6408                 putLogDebug(100226, formatter.str(), __FILE__, __LINE__);
6409         }
6410         /*------DEBUG LOG END------*/
6411
6412         EVENT_TAG status = FINALIZE;
6413         boost::asio::ip::tcp::endpoint endpoint;
6414         bool send_possible = false;
6415         bool send_continue = false;
6416         bool send_disable = false;
6417         thread_data_ptr session_data;
6418         session_thread_data_map_it session_thread_it;
6419         receive_data_map_it receive_data_it;
6420
6421         try {
6422                 {
6423                         boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
6424                         //check thread_id
6425                         session_thread_it = session_thread_data_map.find(thread_id);
6426                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
6427                                 boost::format formatter("Invalid thread id. thread id : %d.");
6428                                 formatter % boost::this_thread::get_id();
6429                                 putLogError(100112, formatter.str(), __FILE__, __LINE__);
6430                                 throw - 1;
6431                         }
6432
6433                         session_data = session_thread_it->second;
6434                 }
6435                 //check endpoint
6436                 endpoint = session_data->thread_division == THREAD_DIVISION_UP_STREAM ? session_data->client_endpoint_tcp
6437                            : session_data->target_endpoint;
6438                 receive_data_it = session_data->receive_data_map.find(endpoint);
6439                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
6440                         //must be down thread
6441                         if (unlikely(session_data->thread_division == THREAD_DIVISION_UP_STREAM)) {
6442                                 boost::format formatter("Invalid endpoint. thread id : %d.");
6443                                 formatter % boost::this_thread::get_id();
6444                                 putLogError(100113, formatter.str(), __FILE__, __LINE__);
6445                                 throw - 1;
6446                         }
6447                         session_data->sorry_flag = SORRY_FLAG_ON;
6448                         /*-------- DEBUG LOG --------*/
6449                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6450                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6451                                                         "handle_sorry_enable() : SORRY_FLAG_ON. thread id : %d.");
6452                                 formatter % boost::this_thread::get_id();
6453                                 putLogDebug(100227, formatter.str(), __FILE__, __LINE__);
6454                         }
6455                         /*------DEBUG LOG END------*/
6456                         status = SORRYSERVER_RECV;
6457                 } else {
6458                         receive_data &recv_data = receive_data_it->second;
6459
6460                         //get this thread sending possible data
6461                         send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
6462                                                     data_send_possible());
6463                         if (it != recv_data.send_status_list.end()) {
6464                                 send_possible = true;
6465                         }
6466
6467                         it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(), data_send_continue());
6468                         if (it != recv_data.send_status_list.end()) {
6469                                 send_continue = true;
6470                         }
6471
6472                         it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(), data_send_disable());
6473                         if (it != recv_data.send_status_list.end()) {
6474                                 send_disable = true;
6475                         }
6476
6477                         //up thread
6478                         if (session_data->thread_division == THREAD_DIVISION_UP_STREAM) {
6479                                 //accept_end_flag is off
6480                                 if (session_data->accept_end_flag == ACCEPT_END_FLAG_OFF) {
6481                                         //set sorry flag on
6482                                         session_data->sorry_flag = SORRY_FLAG_ON;
6483                                         /*-------- DEBUG LOG --------*/
6484                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6485                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6486                                                                         "handle_sorry_enable() : SORRY_FLAG_ON. thread id : %d.");
6487                                                 formatter % boost::this_thread::get_id();
6488                                                 putLogDebug(100228, formatter.str(), __FILE__, __LINE__);
6489                                         }
6490                                         /*------DEBUG LOG END------*/
6491                                         status = ACCEPT;
6492                                 }
6493                                 //accept_end_flag is on
6494                                 else {
6495                                         //set sorry flag on
6496                                         if (session_data->sorry_flag == SORRY_FLAG_ON) {
6497                                                 if (send_possible) {
6498                                                         status = SORRYSERVER_CONNECT;
6499                                                 } else {
6500                                                         status = SORRYSERVER_SELECT;
6501                                                 }
6502
6503                                         }
6504                                         //set sorry flag off
6505                                         else {
6506                                                 //the data that can be sent continue is exist
6507                                                 if (send_continue) {
6508                                                         //set end flag on
6509                                                         session_data->end_flag = END_FLAG_ON;
6510                                                         /*-------- DEBUG LOG --------*/
6511                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6512                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6513                                                                                         "handle_sorry_enable() : END_FLAG_ON. thread id : %d.");
6514                                                                 formatter % boost::this_thread::get_id();
6515                                                                 putLogDebug(100229, formatter.str(), __FILE__, __LINE__);
6516                                                         }
6517                                                         /*------DEBUG LOG END------*/
6518                                                         status = REALSERVER_DISCONNECT;
6519                                                 }
6520                                                 //the data that can be sent continue is not exist
6521                                                 else {
6522                                                         //set sorryserver_switch_flag on
6523                                                         session_data->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_ON;
6524                                                         /*-------- DEBUG LOG --------*/
6525                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6526                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6527                                                                                         "handle_sorry_enable() : SORRYSERVER_SWITCH_FLAG_ON. thread id : %d.");
6528                                                                 formatter % boost::this_thread::get_id();
6529                                                                 putLogDebug(100230, formatter.str(), __FILE__, __LINE__);
6530                                                         }
6531                                                         /*------DEBUG LOG END------*/
6532                                                         //set sorry_flag on
6533                                                         session_data->sorry_flag = SORRY_FLAG_ON;
6534                                                         /*-------- DEBUG LOG --------*/
6535                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6536                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6537                                                                                         "handle_sorry_enable() : SORRY_FLAG_ON. thread id : %d.");
6538                                                                 formatter % boost::this_thread::get_id();
6539                                                                 putLogDebug(100231, formatter.str(), __FILE__, __LINE__);
6540                                                         }
6541                                                         /*------DEBUG LOG END------*/
6542                                                         status = REALSERVER_DISCONNECT;
6543                                                 }
6544                                         }
6545                                 }
6546                         }
6547                         //down thread
6548                         else {
6549                                 //sorry_flag is on
6550                                 if (session_data->sorry_flag == SORRY_FLAG_ON) {
6551                                         //sending possible data is exist
6552                                         if (send_possible) {
6553                                                 status = CLIENT_CONNECTION_CHECK;
6554                                         }
6555                                         //sending possible data is not exist
6556                                         else {
6557                                                 status = SORRYSERVER_RECV;
6558                                         }
6559                                 }
6560                                 //sorry_flag is off
6561                                 else {
6562                                         //set sorry_flag on
6563                                         session_data->sorry_flag = SORRY_FLAG_ON;
6564                                         /*-------- DEBUG LOG --------*/
6565                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6566                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6567                                                                         "handle_sorry_enable() : SORRY_FLAG_ON. thread id : %d.");
6568                                                 formatter % boost::this_thread::get_id();
6569                                                 putLogDebug(100232, formatter.str(), __FILE__, __LINE__);
6570                                         }
6571                                         /*------DEBUG LOG END------*/
6572                                         session_data->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_ON;
6573                                         /*-------- DEBUG LOG --------*/
6574                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6575                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6576                                                                         "handle_sorry_enable() : SORRYSERVER_SWITCH_FLAG_ON. thread id : %d.");
6577                                                 formatter % boost::this_thread::get_id();
6578                                                 putLogDebug(100233, formatter.str(), __FILE__, __LINE__);
6579                                         }
6580                                         /*------DEBUG LOG END------*/
6581
6582                                         //sending NG data is exist or send_rest_size > 0
6583                                         if (send_disable) {
6584                                                 //set end flag on
6585                                                 session_data->end_flag = END_FLAG_ON;
6586                                                 /*-------- DEBUG LOG --------*/
6587                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6588                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6589                                                                                 "handle_sorry_enable() : END_FLAG_ON. thread id : %d.");
6590                                                         formatter % boost::this_thread::get_id();
6591                                                         putLogDebug(100234, formatter.str(), __FILE__, __LINE__);
6592                                                 }
6593                                                 /*------DEBUG LOG END------*/
6594                                                 status = REALSERVER_DISCONNECT;
6595                                         }
6596                                         //
6597                                         else {
6598                                                 //sending possible data is exist
6599                                                 if (send_possible) {
6600                                                         status = CLIENT_CONNECTION_CHECK;
6601                                                 }
6602                                                 //sending possible data is not exist
6603                                                 else {
6604                                                         status = SORRYSERVER_RECV;
6605                                                 }
6606                                         }
6607                                 }
6608                         }
6609                 }
6610         } catch (int e) {
6611                 /*-------- DEBUG LOG --------*/
6612                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6613                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6614                                                 "handle_sorry_enable() : catch exception e = %d. thread id : %d.");
6615                         formatter % e % boost::this_thread::get_id();
6616                         putLogDebug(100235, formatter.str(), __FILE__, __LINE__);
6617                 }
6618                 /*------DEBUG LOG END------*/
6619                 status = FINALIZE;
6620         } catch (const std::exception &ex) {
6621                 std::cerr << "protocol_module_sessionless::handle_sorry_enable() : exception : error = " << ex.what() << "." << std::endl;
6622                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6623                                         "handle_sorry_enable() : exception : error = %s. thread id : %d.");
6624                 formatter % ex.what() % boost::this_thread::get_id();
6625                 putLogError(100114, formatter.str(), __FILE__, __LINE__);
6626                 status = FINALIZE;
6627         } catch (...) {
6628                 std::cerr << "protocol_module_sessionless::handle_sorry_enable() : Unknown exception." << std::endl;
6629                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6630                                         "handle_sorry_enable() : Unknown exception. thread id : %d.");
6631                 formatter % boost::this_thread::get_id();
6632                 putLogError(100115, formatter.str(), __FILE__, __LINE__);
6633                 status = FINALIZE;
6634         }
6635
6636         /*-------- DEBUG LOG --------*/
6637         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6638                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6639                                         "handle_sorry_enable(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
6640                 formatter % status % boost::this_thread::get_id();
6641                 putLogDebug(100236, formatter.str(), __FILE__, __LINE__);
6642         }
6643         /*------DEBUG LOG END------*/
6644
6645         return status;
6646 }
6647
6648 //! call from sorry mode disable. use upstream thread and downstream thread.
6649 //! @param[in]    upstream and downstream thread id( check! one thread one event )
6650 //! @return     session use EVENT mode
6651 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorry_disable(
6652         const boost::thread::id thread_id)
6653 {
6654         /*-------- DEBUG LOG --------*/
6655         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6656                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6657                                         "handle_sorry_disable(const boost::thread::id thread_id) : "
6658                                         "thread_id = %d.");
6659                 formatter % thread_id;
6660                 putLogDebug(100237, formatter.str(), __FILE__, __LINE__);
6661         }
6662         /*------DEBUG LOG END------*/
6663         EVENT_TAG status = FINALIZE;
6664         boost::asio::ip::tcp::endpoint endpoint;
6665         bool send_possible = false;
6666         bool send_disable = false;
6667         bool send_continue = false;
6668         thread_data_ptr session_data;
6669
6670         try {
6671                 {
6672                         boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
6673                         //check thread_id
6674                         session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
6675                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
6676                                 boost::format formatter("Invalid thread id. thread id : %d.");
6677                                 formatter % boost::this_thread::get_id();
6678                                 putLogError(100116, formatter.str(), __FILE__, __LINE__);
6679                                 throw - 1;
6680                         }
6681                         //check pointer
6682                         session_data = session_thread_it->second;
6683                 }
6684                 //check endpoint
6685                 endpoint = session_data->thread_division == THREAD_DIVISION_UP_STREAM ? session_data->client_endpoint_tcp
6686                            : session_data->target_endpoint;
6687                 receive_data_map_it receive_data_it = session_data->receive_data_map.find(endpoint);
6688                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
6689                         //must be down thread
6690                         if (unlikely(session_data->thread_division == THREAD_DIVISION_UP_STREAM)) {
6691                                 boost::format formatter("Invalid endpoint. thread id : %d.");
6692                                 formatter % boost::this_thread::get_id();
6693                                 putLogError(100117, formatter.str(), __FILE__, __LINE__);
6694                                 throw - 1;
6695                         }
6696
6697                         session_data->sorry_flag = SORRY_FLAG_OFF;
6698                         /*-------- DEBUG LOG --------*/
6699                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6700                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6701                                                         "handle_sorry_disable() : SORRY_FLAG_OFF. thread id : %d.");
6702                                 formatter % boost::this_thread::get_id();
6703                                 putLogDebug(100238, formatter.str(), __FILE__, __LINE__);
6704                         }
6705                         /*------DEBUG LOG END------*/
6706                         status = REALSERVER_RECV;
6707                 } else {
6708                         receive_data &recv_data = receive_data_it->second;
6709
6710                         //get this thread sending possible data
6711                         send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
6712                                                     data_send_possible());
6713                         if (it != recv_data.send_status_list.end()) {
6714                                 send_possible = true;
6715                         }
6716
6717                         //sending NG data is exist or send_rest_size > 0
6718                         it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(), data_send_disable());
6719                         if (it != recv_data.send_status_list.end()) {
6720                                 send_disable = true;
6721                         }
6722
6723                         //the data that can be sent continue is exist
6724                         it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(), data_send_continue());
6725                         if (it != recv_data.send_status_list.end()) {
6726                                 send_continue = true;
6727                         }
6728
6729                         //up thread
6730                         if (session_data->thread_division == THREAD_DIVISION_UP_STREAM) {
6731                                 //accept_end_flag is off
6732                                 if (session_data->accept_end_flag == ACCEPT_END_FLAG_OFF) {
6733                                         //set sorry flag off
6734                                         session_data->sorry_flag = SORRY_FLAG_OFF;
6735                                         /*-------- DEBUG LOG --------*/
6736                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6737                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6738                                                                         "handle_sorry_disable() : SORRY_FLAG_OFF. thread id : %d.");
6739                                                 formatter % boost::this_thread::get_id();
6740                                                 putLogDebug(100239, formatter.str(), __FILE__, __LINE__);
6741                                         }
6742                                         /*------DEBUG LOG END------*/
6743
6744                                         status = ACCEPT;
6745                                 }
6746                                 //accept_end_flag is on
6747                                 else {
6748                                         //sorry flag is on
6749                                         if (session_data->sorry_flag == SORRY_FLAG_ON) {
6750                                                 //the data that can be sent continue is exist
6751                                                 if (send_continue) {
6752                                                         //set end flag on
6753                                                         session_data->end_flag = END_FLAG_ON;
6754                                                         /*-------- DEBUG LOG --------*/
6755                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6756                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6757                                                                                         "handle_sorry_disable() : END_FLAG_ON. thread id : %d.");
6758                                                                 formatter % boost::this_thread::get_id();
6759                                                                 putLogDebug(100240, formatter.str(), __FILE__, __LINE__);
6760                                                         }
6761                                                         /*------DEBUG LOG END------*/
6762                                                         status = SORRYSERVER_DISCONNECT;
6763                                                 }
6764                                                 //the data that can be sent continue is not exist
6765                                                 else {
6766                                                         //set realserver_switch_flag on
6767                                                         session_data->realserver_switch_flag = REALSERVER_SWITCH_FLAG_ON;
6768                                                         /*-------- DEBUG LOG --------*/
6769                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6770                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6771                                                                                         "handle_sorry_disable() : REALSERVER_SWITCH_FLAG_ON. thread id : %d.");
6772                                                                 formatter % boost::this_thread::get_id();
6773                                                                 putLogDebug(100241, formatter.str(), __FILE__, __LINE__);
6774                                                         }
6775                                                         /*------DEBUG LOG END------*/
6776                                                         //set sorry_flag off
6777                                                         session_data->sorry_flag = SORRY_FLAG_OFF;
6778                                                         /*-------- DEBUG LOG --------*/
6779                                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6780                                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6781                                                                                         "handle_sorry_disable() : SORRY_FLAG_OFF. thread id : %d.");
6782                                                                 formatter % boost::this_thread::get_id();
6783                                                                 putLogDebug(100242, formatter.str(), __FILE__, __LINE__);
6784                                                         }
6785                                                         /*------DEBUG LOG END------*/
6786                                                         status = SORRYSERVER_DISCONNECT;
6787                                                 }
6788                                         }
6789                                         //sorry flag is off
6790                                         else {
6791                                                 if (send_possible) {
6792                                                         status = REALSERVER_CONNECT;
6793                                                 } else {
6794                                                         status = REALSERVER_SELECT;
6795                                                 }
6796                                         }
6797                                 }
6798                         }
6799                         //down thread
6800                         else {
6801                                 //sorry_flag is on
6802                                 if (session_data->sorry_flag == SORRY_FLAG_ON) {
6803                                         //set sorry_flag off
6804                                         session_data->sorry_flag = SORRY_FLAG_OFF;
6805                                         /*-------- DEBUG LOG --------*/
6806                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6807                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6808                                                                         "handle_sorry_disable() : SORRY_FLAG_OFF. thread id : %d.");
6809                                                 formatter % boost::this_thread::get_id();
6810                                                 putLogDebug(100243, formatter.str(), __FILE__, __LINE__);
6811                                         }
6812                                         /*------DEBUG LOG END------*/
6813                                         session_data->realserver_switch_flag = REALSERVER_SWITCH_FLAG_ON;
6814                                         /*-------- DEBUG LOG --------*/
6815                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6816                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6817                                                                         "handle_sorry_disable() : REALSERVER_SWITCH_FLAG_ON. thread id : %d.");
6818                                                 formatter % boost::this_thread::get_id();
6819                                                 putLogDebug(100244, formatter.str(), __FILE__, __LINE__);
6820                                         }
6821                                         /*------DEBUG LOG END------*/
6822                                         //sending NG data is exist or send_rest_size > 0
6823                                         if (send_disable) {
6824                                                 //set end flag on
6825                                                 session_data->end_flag = END_FLAG_ON;
6826                                                 /*-------- DEBUG LOG --------*/
6827                                                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6828                                                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6829                                                                                 "handle_sorry_disable() : END_FLAG_ON. thread id : %d.");
6830                                                         formatter % boost::this_thread::get_id();
6831                                                         putLogDebug(100245, formatter.str(), __FILE__, __LINE__);
6832                                                 }
6833                                                 /*------DEBUG LOG END------*/
6834                                                 status = SORRYSERVER_DISCONNECT;
6835                                         }
6836                                         //
6837                                         else {
6838                                                 //sending possible data is exist
6839                                                 if (send_possible) {
6840                                                         status = CLIENT_CONNECTION_CHECK;
6841                                                 }
6842                                                 //sending possible data is not exist
6843                                                 else {
6844                                                         status = REALSERVER_RECV;
6845                                                 }
6846                                         }
6847                                 }
6848                                 //sorry_flag is off
6849                                 else {
6850                                         //sending possible data is exist
6851                                         if (send_possible) {
6852                                                 status = CLIENT_CONNECTION_CHECK;
6853                                         }
6854                                         //sending possible data is not exist
6855                                         else {
6856                                                 status = REALSERVER_RECV;
6857                                         }
6858                                 }
6859                         }
6860                 }
6861
6862
6863         } catch (int e) {
6864                 /*-------- DEBUG LOG --------*/
6865                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6866                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6867                                                 "handle_sorry_disable() : catch exception e = %d. thread id : %d.");
6868                         formatter % e % boost::this_thread::get_id();
6869                         putLogDebug(100246, formatter.str(), __FILE__, __LINE__);
6870                 }
6871                 /*------DEBUG LOG END------*/
6872                 status = FINALIZE;
6873         } catch (std::exception &ex) {
6874                 std::cerr << "protocol_module_sessionless::handle_sorry_disable() : exception : error = " << ex.what() << "." << std::endl;
6875                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6876                                         "handle_sorry_disable() : exception : error = %s. thread id : %d.");
6877                 formatter % ex.what() % boost::this_thread::get_id();
6878                 putLogError(100118, formatter.str(), __FILE__, __LINE__);
6879                 status = FINALIZE;
6880         } catch (...) {
6881                 std::cerr << "protocol_module_sessionless::handle_sorry_disable() : Unknown exception." << std::endl;
6882                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6883                                         "handle_sorry_disable() : Unknown exception. thread id : %d.");
6884                 formatter % boost::this_thread::get_id();
6885                 putLogError(100119, formatter.str(), __FILE__, __LINE__);
6886                 status = FINALIZE;
6887         }
6888
6889         /*-------- DEBUG LOG --------*/
6890         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6891                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6892                                         "handle_sorry_disable(const boost::thread::id thread_id) : return_value = %d. thread id : %d.");
6893                 formatter % status % boost::this_thread::get_id();
6894                 putLogDebug(100247, formatter.str(), __FILE__, __LINE__);
6895         }
6896         /*------DEBUG LOG END------*/
6897
6898         return status;
6899 }
6900
6901 //! call from realserver disconnect. use upstream thread and downstream thread
6902 //! @param[in]    upstream and downstream thread id( check! one thread one event )
6903 //! @param[in]    disconnected realserver endpoint.
6904 //! @return     session use EVENT mode
6905 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_disconnect(
6906         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &rs_endpoint)
6907 {
6908         /*-------- DEBUG LOG --------*/
6909         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6910                 boost::format formatter("in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6911                                         "handle_realserver_disconnect(const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint & rs_endpoint) : "
6912                                         "thread_id = %d, rs_endpoint = [%s]:%d.");
6913                 formatter % thread_id % rs_endpoint.address().to_string() % rs_endpoint.port();
6914                 putLogDebug(100248, formatter.str(), __FILE__, __LINE__);
6915         }
6916         /*------DEBUG LOG END------*/
6917         EVENT_TAG status = FINALIZE;
6918         bool possible_flag = false;
6919         thread_data_ptr session_data;
6920         boost::asio::ip::tcp::endpoint endpoint;
6921
6922         try {
6923                 {
6924                         boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
6925
6926                         session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
6927                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
6928                                 boost::format formatter("Invalid thread id. thread id : %d.");
6929                                 formatter % boost::this_thread::get_id();
6930                                 putLogError(100120, formatter.str(), __FILE__, __LINE__);
6931                                 throw - 1;
6932                         }
6933
6934                         session_data = session_thread_it->second;
6935                 }
6936
6937                 endpoint = session_data->thread_division == THREAD_DIVISION_UP_STREAM ? session_data->client_endpoint_tcp
6938                            : session_data->target_endpoint;
6939                 receive_data_map_it receive_data_it = session_data->receive_data_map.find(endpoint);
6940                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
6941                         /*-------- DEBUG LOG --------*/
6942                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6943                                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6944                                                         "handle_realserver_disconnect(const boost::thread::id thread_id, "
6945                                                         "const boost::asio::ip::tcp::endpoint & rs_endpoint) : return_value = %d. thread id : %d.");
6946                                 formatter % FINALIZE % boost::this_thread::get_id();
6947                                 putLogDebug(100263, formatter.str(), __FILE__, __LINE__);
6948                         }
6949                         /*------DEBUG LOG END------*/
6950
6951                         return FINALIZE;
6952                 }
6953
6954                 receive_data &recv_data = receive_data_it->second;
6955
6956                 //the data that can be sent possible is exist
6957                 send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
6958                                             data_send_possible());
6959                 if (it != recv_data.send_status_list.end()) {
6960                         possible_flag = true;
6961                 }
6962
6963                 //up thread
6964                 if (session_data->thread_division == THREAD_DIVISION_UP_STREAM) {
6965                         //end flag is on
6966                         if (session_data->end_flag == END_FLAG_ON) {
6967                                 status = CLIENT_RECV;
6968                         }
6969                         //end flag is off
6970                         else {
6971                                 //sorryserver_switch_flag is on
6972                                 if (session_data->sorryserver_switch_flag == SORRYSERVER_SWITCH_FLAG_ON) {
6973                                         session_data->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_OFF;
6974                                         /*-------- DEBUG LOG --------*/
6975                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6976                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6977                                                                         "handle_realserver_disconnect() : SORRYSERVER_SWITCH_FLAG_OFF. thread id : %d.");
6978                                                 formatter % boost::this_thread::get_id();
6979                                                 putLogDebug(100249, formatter.str(), __FILE__, __LINE__);
6980                                         }
6981                                         /*------DEBUG LOG END------*/
6982
6983                                         status = SORRYSERVER_SELECT;
6984
6985                                 }
6986                                 //sorryserver_switch_flag is off
6987                                 else {
6988                                         //set end flag on
6989                                         session_data->end_flag = END_FLAG_ON;
6990                                         /*-------- DEBUG LOG --------*/
6991                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
6992                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
6993                                                                         "handle_realserver_disconnect() : END_FLAG_ON. thread id : %d.");
6994                                                 formatter % boost::this_thread::get_id();
6995                                                 putLogDebug(100250, formatter.str(), __FILE__, __LINE__);
6996                                         }
6997                                         /*------DEBUG LOG END------*/
6998                                         status = CLIENT_RECV;
6999                                 }
7000                         }
7001                 }
7002                 //down thread
7003                 else {
7004                         if (session_data->end_flag == END_FLAG_ON) {
7005                                 status = CLIENT_DISCONNECT;
7006                         } else {
7007                                 if (session_data->sorryserver_switch_flag == SORRYSERVER_SWITCH_FLAG_ON) {
7008                                         session_data->sorryserver_switch_flag = SORRYSERVER_SWITCH_FLAG_OFF;
7009                                         /*-------- DEBUG LOG --------*/
7010                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7011                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7012                                                                         "handle_realserver_disconnect() : SORRYSERVER_SWITCH_FLAG_OFF. thread id : %d.");
7013                                                 formatter % boost::this_thread::get_id();
7014                                                 putLogDebug(100251, formatter.str(), __FILE__, __LINE__);
7015                                         }
7016                                         /*------DEBUG LOG END------*/
7017                                         status = SORRYSERVER_RECV;
7018                                 } else {
7019                                         //set end flag on
7020                                         session_data->end_flag = END_FLAG_ON;
7021                                         /*-------- DEBUG LOG --------*/
7022                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7023                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7024                                                                         "handle_realserver_disconnect() : END_FLAG_ON. thread id : %d.");
7025                                                 formatter % boost::this_thread::get_id();
7026                                                 putLogDebug(100252, formatter.str(), __FILE__, __LINE__);
7027                                         }
7028                                         /*------DEBUG LOG END------*/
7029                                         status = CLIENT_DISCONNECT;
7030                                 }
7031                         }
7032
7033                         //the data that can be sent possible is exist
7034                         if (possible_flag) {
7035                                 status = CLIENT_CONNECTION_CHECK;
7036                         }
7037                 }
7038         } catch (int e) {
7039                 /*-------- DEBUG LOG --------*/
7040                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7041                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7042                                                 "handle_realserver_disconnect() : catch exception e = %d. thread id : %d.");
7043                         formatter % e % boost::this_thread::get_id();
7044                         putLogDebug(100253, formatter.str(), __FILE__, __LINE__);
7045                 }
7046                 /*------DEBUG LOG END------*/
7047                 status = FINALIZE;
7048         } catch (std::exception &ex) {
7049                 std::cerr << "protocol_module_sessionless::handle_realserver_disconnect() : exception: error = " << ex.what() << "." << std::endl;
7050                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7051                                         "handle_realserver_disconnect() : exception : error = %s. thread id : %d.");
7052                 formatter % ex.what() % boost::this_thread::get_id();
7053                 putLogError(100122, formatter.str(), __FILE__, __LINE__);
7054                 status = FINALIZE;
7055         } catch (...) {
7056                 std::cerr << "protocol_module_sessionless::handle_realserver_disconnect() : Unknown exception." << std::endl;
7057                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7058                                         "handle_realserver_disconnect() : Unknown exception. thread id : %d.");
7059                 formatter % boost::this_thread::get_id();
7060                 putLogError(100123, formatter.str(), __FILE__, __LINE__);
7061                 status = FINALIZE;
7062         }
7063
7064         /*-------- DEBUG LOG --------*/
7065         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7066                 boost::format formatter("out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7067                                         "handle_realserver_disconnect(const boost::thread::id thread_id, "
7068                                         "const boost::asio::ip::tcp::endpoint & rs_endpoint) : return_value = %d. thread id : %d.");
7069                 formatter % status % boost::this_thread::get_id();
7070                 putLogDebug(100254, formatter.str(), __FILE__, __LINE__);
7071         }
7072         /*------DEBUG LOG END------*/
7073
7074         return status;
7075 }
7076
7077 //! call from sorry server disconnect. use upstream thread and downstream thread
7078 //! @param[in]    upstream and downstream thread id( check! one thread one event )
7079 //! @param[in]    disconnect sorryserver endpoint
7080 //! @return        session use EVENT mode
7081 //! @return        session use EVENT mode
7082 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_sorryserver_disconnect(
7083         const boost::thread::id thread_id, const boost::asio::ip::tcp::endpoint &sorry_endpoint)
7084 {
7085         /*-------- DEBUG LOG --------*/
7086         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7087                 boost::format
7088                 formatter(
7089                         "in_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7090                         "handle_sorryserver_disconnect(const boost::thread::id thread_id, "
7091                         "const boost::asio::ip::tcp::endpoint & sorry_endpoint) : "
7092                         "thread_id = %d, sorry_endpoint = [%s]:%d.");
7093                 formatter % thread_id % sorry_endpoint.address().to_string() % sorry_endpoint.port() ;
7094                 putLogDebug(100255, formatter.str(), __FILE__, __LINE__);
7095         }
7096         /*------DEBUG LOG END------*/
7097         EVENT_TAG status = FINALIZE;
7098         bool possible_flag = false;
7099         thread_data_ptr session_data;
7100         boost::asio::ip::tcp::endpoint endpoint;
7101
7102         try {
7103                 {
7104                         boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
7105
7106                         session_thread_data_map_it session_thread_it = session_thread_data_map.find(thread_id);
7107                         if (unlikely(session_thread_it == session_thread_data_map.end() || session_thread_it->second == NULL)) {
7108                                 boost::format formatter("Invalid thread id. thread id : %d.");
7109                                 formatter % boost::this_thread::get_id();
7110                                 putLogError(100124, formatter.str(), __FILE__, __LINE__);
7111                                 throw - 1;
7112                         }
7113
7114                         session_data = session_thread_it->second;
7115                 }
7116
7117                 endpoint = session_data->thread_division == THREAD_DIVISION_UP_STREAM ? session_data->client_endpoint_tcp
7118                            : session_data->target_endpoint;
7119                 receive_data_map_it receive_data_it = session_data->receive_data_map.find(endpoint);
7120                 if (unlikely(receive_data_it == session_data->receive_data_map.end())) {
7121                         boost::format formatter("Invalid endpoint(%s). thread id: %d.");
7122                         formatter % endpoint % boost::this_thread::get_id();
7123                         putLogError(100125, formatter.str(), __FILE__, __LINE__);
7124                         throw - 1;
7125                 }
7126
7127                 receive_data &recv_data = receive_data_it->second;
7128
7129                 //the data that can be sent possible is exist
7130                 send_status_it it = find_if(recv_data.send_status_list.begin(), recv_data.send_status_list.end(),
7131                                             data_send_possible());
7132                 if (it != recv_data.send_status_list.end()) {
7133                         possible_flag = true;
7134                 }
7135
7136                 //up thread
7137                 if (session_data->thread_division == THREAD_DIVISION_UP_STREAM) {
7138                         //end flag is on
7139                         if (session_data->end_flag == END_FLAG_ON) {
7140                                 status = CLIENT_RECV;
7141                         }
7142                         //end flag is off
7143                         else {
7144                                 //realserver_switch_flag is on
7145                                 if (session_data->realserver_switch_flag == REALSERVER_SWITCH_FLAG_ON) {
7146                                         session_data->realserver_switch_flag = REALSERVER_SWITCH_FLAG_OFF;
7147                                         /*-------- DEBUG LOG --------*/
7148                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7149                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7150                                                                         "handle_sorryserver_disconnect() : REALSERVER_SWITCH_FLAG_OFF. thread id : %d.");
7151                                                 formatter % boost::this_thread::get_id();
7152                                                 putLogDebug(100256, formatter.str(), __FILE__, __LINE__);
7153                                         }
7154                                         /*------DEBUG LOG END------*/
7155                                         status = REALSERVER_SELECT;
7156                                 }
7157                                 //realserver_switch_flag is off
7158                                 else {
7159                                         //set end flag on
7160                                         session_data->end_flag = END_FLAG_ON;
7161                                         /*-------- DEBUG LOG --------*/
7162                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7163                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7164                                                                         "handle_sorryserver_disconnect() : END_FLAG_ON. thread id : %d.");
7165                                                 formatter % boost::this_thread::get_id();
7166                                                 putLogDebug(100257, formatter.str(), __FILE__, __LINE__);
7167                                         }
7168                                         /*------DEBUG LOG END------*/
7169                                         status = CLIENT_RECV;
7170                                 }
7171                         }
7172                 }
7173                 //down thread
7174                 else {
7175                         if (session_data->end_flag == END_FLAG_ON) {
7176                                 status = CLIENT_DISCONNECT;
7177                         } else {
7178                                 if (session_data->realserver_switch_flag == REALSERVER_SWITCH_FLAG_ON) {
7179                                         session_data->realserver_switch_flag = REALSERVER_SWITCH_FLAG_OFF;
7180                                         /*-------- DEBUG LOG --------*/
7181                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7182                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7183                                                                         "handle_sorryserver_disconnect() : REALSERVER_SWITCH_FLAG_OFF. thread id : %d.");
7184                                                 formatter % boost::this_thread::get_id();
7185                                                 putLogDebug(100258, formatter.str(), __FILE__, __LINE__);
7186                                         }
7187                                         /*------DEBUG LOG END------*/
7188                                         status = REALSERVER_RECV;
7189                                 } else {
7190                                         session_data->end_flag = END_FLAG_ON;
7191                                         /*-------- DEBUG LOG --------*/
7192                                         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7193                                                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7194                                                                         "handle_sorryserver_disconnect() : END_FLAG_ON. thread id : %d.");
7195                                                 formatter % boost::this_thread::get_id();
7196                                                 putLogDebug(100259, formatter.str(), __FILE__, __LINE__);
7197                                         }
7198                                         /*------DEBUG LOG END------*/
7199                                         status = CLIENT_DISCONNECT;
7200                                 }
7201                         }
7202
7203                         //the data that can be sent possible is exist
7204                         if (possible_flag) {
7205                                 status = CLIENT_CONNECTION_CHECK;
7206                         }
7207                 }
7208         } catch (int e) {
7209                 /*-------- DEBUG LOG --------*/
7210                 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7211                         boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7212                                                 "handle_sorryserver_disconnect() : catch exception e = %d. thread id : %d.");
7213                         formatter % e % boost::this_thread::get_id();
7214                         putLogDebug(100260, formatter.str(), __FILE__, __LINE__);
7215                 }
7216                 /*------DEBUG LOG END------*/
7217                 status = FINALIZE;
7218         } catch (const std::exception &ex) {
7219                 std::cerr << "protocol_module_sessionless::handle_sorryserver_disconnect() : exception : error = " << ex.what() << "." << std::endl;
7220                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7221                                         "handle_sorryserver_disconnect() : exception : error = %s. thread id : %d.");
7222                 formatter % ex.what() % boost::this_thread::get_id();
7223                 putLogError(100126, formatter.str(), __FILE__, __LINE__);
7224                 status = FINALIZE;
7225         } catch (...) {
7226                 std::cerr << "protocol_module_sessionless::handle_sorryserver_disconnect() : Unknown exception." << std::endl;
7227                 boost::format formatter("function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7228                                         "handle_sorryserver_disconnect() : Unknown exception. thread id : %d.");
7229                 formatter % boost::this_thread::get_id();
7230                 putLogError(100127, formatter.str(), __FILE__, __LINE__);
7231                 status = FINALIZE;
7232         }
7233
7234         /*-------- DEBUG LOG --------*/
7235         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7236                 boost::format
7237                 formatter(
7238                         "out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7239                         "handle_sorryserver_disconnect(const boost::thread::id thread_id, "
7240                         "const boost::asio::ip::tcp::endpoint& sorry_endpoint) : return_value = %d. thread id : %d.");
7241                 formatter % status % boost::this_thread::get_id();
7242                 putLogDebug(100261, formatter.str(), __FILE__, __LINE__);
7243         }
7244         /*------DEBUG LOG END------*/
7245
7246         return status;
7247 }
7248
7249 //! call from realserver disconnect. use upstream thread and downstream thread.
7250 //! @param[in]    upstream and downstream thread id( check! one thread one event )
7251 //! @param[in]    disconnect realserver endpoint
7252 //! @return        session use EVENT mode.
7253 protocol_module_base::EVENT_TAG protocol_module_sessionless::handle_realserver_close(
7254         const boost::thread::id thread_id, const boost::asio::ip::udp::endpoint &rs_endpoint)
7255 {
7256         /*-------- DEBUG LOG --------*/
7257         if (unlikely(LOG_LV_DEBUG == getloglevel())) {
7258                 boost::format formatter("in/out_function : protocol_module_base::EVENT_TAG protocol_module_sessionless::"
7259                                         "handle_realserver_close(const boost::thread::id thread_id, "
7260                                         "const boost::asio::ip::udp::endpoint & rs_endpoint) : "
7261                                         "return_value = %d. thread id : %d.");
7262                 formatter % STOP % boost::this_thread::get_id();
7263                 putLogDebug(100262, formatter.str(), __FILE__, __LINE__);
7264         }
7265         /*------DEBUG LOG END------*/
7266         return STOP;
7267 }
7268
7269 }
7270
7271 extern "C" l7vs::protocol_module_base*
7272 create_module()
7273 {
7274         return dynamic_cast<l7vs::protocol_module_base *>(new l7vs::protocol_module_sessionless());
7275 }
7276
7277 extern "C" void
7278 destroy_module(l7vs::protocol_module_base *in)
7279 {
7280         delete in;
7281 }