OSDN Git Service

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