2 * @file protocol_module_sessionless.cpp
3 * @brief protocol module of any protocol.
4 * @brief this module never keep session persistence.
6 * L7VSD: Linux Virtual Server for Layer7 Load Balancing
7 * Copyright (C) 2009 NTT COMWARE Corporation.
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.
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.
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
24 **********************************************************************/
25 #include <boost/xpressive/xpressive.hpp>
30 #include <boost/asio/ip/tcp.hpp>
31 #include <boost/format.hpp>
32 #include "protocol_module_sessionless.h"
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;
42 const int protocol_module_sessionless::END_FLAG_OFF = 0;
43 const int protocol_module_sessionless::END_FLAG_ON = 1;
45 const int protocol_module_sessionless::ACCEPT_END_FLAG_OFF = 0;
46 const int protocol_module_sessionless::ACCEPT_END_FLAG_ON = 1;
48 const int protocol_module_sessionless::SORRY_FLAG_ON = 1;
49 const int protocol_module_sessionless::SORRY_FLAG_OFF = 0;
51 const int protocol_module_sessionless::SORRYSERVER_SWITCH_FLAG_OFF = 0;
52 const int protocol_module_sessionless::SORRYSERVER_SWITCH_FLAG_ON = 1;
54 const int protocol_module_sessionless::REALSERVER_SWITCH_FLAG_OFF = 0;
55 const int protocol_module_sessionless::REALSERVER_SWITCH_FLAG_ON = 1;
57 const int protocol_module_sessionless::EDIT_DIVISION_NO_EDIT = 0;
58 const int protocol_module_sessionless::EDIT_DIVISION_EDIT = 1;
60 const int protocol_module_sessionless::FORWARDED_FOR_OFF = 0;
61 const int protocol_module_sessionless::FORWARDED_FOR_ON = 1;
63 const int protocol_module_sessionless::COLLECT_STATS_OFF = 0;
64 const int protocol_module_sessionless::COLLECT_STATS_ON = 1;
67 protocol_module_sessionless::protocol_module_sessionless() :
68 http_protocol_module_base(MODULE_NAME), forwarded_for(FORWARDED_FOR_OFF)
70 sorry_uri.assign('\0');
74 protocol_module_sessionless::~protocol_module_sessionless()
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()
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__);
87 /*------DEBUG LOG END------*/
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()
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__);
101 /*------DEBUG LOG END------*/
105 //! replication interval interrupt
106 //! timer thread call this function. from virtualservice.
107 void protocol_module_sessionless::replication_interrupt()
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__);
114 /*------DEBUG LOG END------*/
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)
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__);
135 /*------DEBUG LOG END------*/
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;
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__);
155 /*-------- DEBUG LOG --------*/
158 //! finalize called from module control. module unloaded call.
159 void protocol_module_sessionless::finalize()
161 /*-------- DEBUG LOG --------*/
162 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
163 putLogDebug(100005, "in_function : void protocol_module_sessionless::finalize().", __FILE__,
166 /*------DEBUG LOG END------*/
168 //RealServer list functions initialization
169 //RealServer list begin function
170 rs_list_begin.clear();
171 //RealServer list end function
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();
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__,
186 /*------DEBUG LOG END------*/
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();
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__,
202 /*------DEBUG LOG END------*/
204 //ScheduleModule's functions initialization
205 schedule_tcp.clear();
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__,
213 /*------DEBUG LOG END------*/
215 //Module's option initialization
217 forwarded_for = FORWARDED_FOR_OFF;
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__,
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__,
232 /*------DEBUG LOG END------*/
235 //logger functions initialization
236 //log level getting function
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()
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__);
260 /*------DEBUG LOG END------*/
264 //! realserver list update event
265 void protocol_module_sessionless::handle_rslist_update()
267 /*-------- DEBUG LOG --------*/
268 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
269 putLogDebug(100012, "in/out_function : void protocol_module_sessionless::handle_rslist_update().", __FILE__,
272 /*------DEBUG LOG END------*/
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)
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) {
292 formatter % argsdump;
293 putLogDebug(100013, formatter.str(), __FILE__, __LINE__);
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
307 (set = ';', ':', '@', '&', '=') |
308 (set = '$', '-', '_', '.', '+') |
309 (set = '!', '*', '\'', '\(', ')', ',') |
310 '%' >> repeat<2>(xdigit)));
311 typedef std::vector<std::string>::const_iterator vec_str_it;
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;
323 //option string = "-S"
324 else if (*it == "-S" || *it == "--sorry-uri") {
325 //set sorryURI flag OFF
326 if (!sorryuri_checked) {
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__);
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.";
343 //set check result flag false
344 check_result.flag = false;
345 //set check result message
346 check_result.message = ostr.str();
348 putLogError(100001, check_result.message, __FILE__, __LINE__);
352 //next option string's length <= 127
355 if (regex_match(*it, sorry_uri_regex)) {
357 //set sorryURI flag ON
358 sorryuri_checked = true;
362 std::ostringstream ostr;
363 ostr << "'-S/--sorry-uri' option value '" << *it << "' is not a valid URI.";
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__);
375 //next item is not exist
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__,
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__,
399 //option string = "-c/--statistic"
400 else if (*it == "-c" || *it == "--statistic") {
401 //statistic flag is OFF
402 if (!stats_checked) {
405 //collect statistic flag must be 0 or 1
406 if(*it == "0" || *it == "1"){
408 //set statistic flag ON
409 stats_checked = true; }
411 std::ostringstream ostr;
412 ostr << "'-c/--statistic' option value '" << *it << "' is not a valid value.";
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__);
423 //next item is not exist
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__,
435 //statistic flag is ON
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__,
447 //other option string
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__);
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 : "
464 formatter % ex.what();
465 putLogError(100006, formatter.str(), __FILE__, __LINE__);
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__);
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__);
483 /*------DEBUG LOG END------*/
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)
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) {
504 formatter % argsdump;
505 putLogDebug(100015, formatter.str(), __FILE__, __LINE__);
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
519 (set = ';', ':', '@', '&', '=') |
520 (set = '$', '-', '_', '.', '+') |
521 (set = '!', '*', '\'', '\(', ')', ',') |
522 '%' >> repeat<2>(xdigit)));
524 typedef std::vector<std::string>::const_iterator vec_str_it;
526 //set forwarded flag true
527 forwarded_for = FORWARDED_FOR_ON;
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;
538 //option string = "-S"
539 else if (*it == "-S" || *it == "--sorry-uri") {
540 //sorryURI flag = OFF
541 if (!sorryuri_checked) {
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__,
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.";
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__,
568 //next option string's length <= 127
572 if (regex_match(*it, sorry_uri_regex)) {
573 sorryuri_checked = true;
574 strcpy(sorry_uri.data(), it->c_str());
578 std::ostringstream ostr;
579 ostr << "'-S/--sorry-uri' option value '" << *it << "' is not a valid URI.";
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__,
592 //next item not exist
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__,
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__,
616 //option string = "-c/--statistic"
617 else if (*it == "-c" || *it == "--statistic") {
618 //statistic flag is OFF
619 if (!stats_checked) {
622 //collect statistic flag must be 0 or 1
623 if(*it == "0" || *it == "1"){
625 //set statistic flag ON
626 stats_checked = true;
628 //set collect statistic flag
629 statistic = boost::lexical_cast<int>(*it); }
631 std::ostringstream ostr;
632 ostr << "'-c/--statistic' option value '" << *it << "' is not a valid value.";
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__);
643 //next item is not exist
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__,
655 //statistic flag is ON
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__,
669 //set check result flag false
670 check_result.flag = false;
671 //set check result message
672 check_result.message = "Option error.";
674 putLogError(100013, check_result.message, __FILE__,
681 if (check_result.flag == true) {
683 if (!forward_checked) {
684 forwarded_for = FORWARDED_FOR_OFF;
687 //collect statistic flag = OFF
688 if (!stats_checked) {
689 statistic = COLLECT_STATS_OFF;
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 : "
699 formatter % ex.what();
700 putLogError(100014, formatter.str(), __FILE__, __LINE__);
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__);
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__);
718 /*-------- DEBUG LOG END--------*/
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)
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) {
740 formatter % argsdump;
741 putLogDebug(100017, formatter.str(), __FILE__, __LINE__);
743 /*------DEBUG LOG END------*/
744 check_message_result check_result;
745 //set check result flag true
746 check_result.flag = true;
748 //param list is not 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__);
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__);
766 /*-------- DEBUG LOG --------*/
772 //! @param[out] module parameter string
773 void protocol_module_sessionless::get_option_info(std::string &option)
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__);
780 /*------DEBUG LOG END------*/
782 boost::format option_formatter("%s--sorry-uri '%s' --statistic %d");
783 option_formatter % (forwarded_for ? "--forwarded-for " : "") % sorry_uri.c_array()
785 option.assign(option_formatter.str());
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.");
792 putLogDebug(100020, formatter.str(), __FILE__, __LINE__);
794 /*------DEBUG LOG END------*/
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)
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__);
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__);
813 /*------DEBUG LOG END------*/
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)
820 /*-------- DEBUG LOG --------*/
821 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
823 "in/out_function : void protocol_module_sessionless::register_schedule(udp_schedule_func_type inschedule).",
826 /*------DEBUG LOG END------*/
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)
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__);
848 /*------DEBUG LOG END------*/
849 EVENT_TAG status = FINALIZE;
851 //session thread initialization
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__);
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;
872 receive_data recv_data;
873 p_up->receive_data_map[client_endpoint_tcp] = recv_data;
875 /*-------- DEBUG LOG --------*/
876 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
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__);
889 /*------DEBUG LOG END------*/
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__);
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())) {
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__);
922 /*------DEBUG LOG END------*/
924 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
926 session_thread_data_map[up_thread_id] = p_up;
927 session_thread_data_map[down_thread_id] = p_down;
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__);
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__);
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__);
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__);
963 /*------DEBUG LOG END------*/
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)
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__);
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;
991 //session thread free
993 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
1004 /*------DEBUG LOG END------*/
1005 session_thread_data_map.erase(up_thread_id);
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__,
1017 /*------DEBUG LOG END------*/
1018 session_thread_data_map.erase(down_thread_id);
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__);
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__);
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__);
1047 /*------DEBUG LOG END------*/
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)
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__);
1064 /*------DEBUG LOG END------*/
1066 EVENT_TAG status = FINALIZE;
1067 thread_data_ptr session_data;
1068 session_thread_data_map_it session_thread_it;
1071 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
1081 session_data = session_thread_it->second;
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__);
1092 /*------DEBUG LOG END------*/
1095 if (session_data->sorry_flag == SORRY_FLAG_ON) {
1097 status = SORRYSERVER_SELECT;
1102 status = REALSERVER_SELECT;
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__);
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__);
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__);
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__);
1139 /*------DEBUG LOG END------*/
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)
1151 /*-------- DEBUG LOG --------*/
1152 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
1153 size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
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__);
1163 /*------DEBUG LOG END------*/
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;
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;
1185 CHECK_RESULT_TAG check_result;
1186 session_thread_data_map_it session_thread_it;
1187 receive_data_map_it receive_data_it;
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__);
1204 /*------DEBUG LOG END------*/
1210 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
1220 session_data = session_thread_it->second;
1224 if (session_data->end_flag == END_FLAG_ON) {
1225 status = CLIENT_RECV;
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__);
1237 receive_data &recv_data = receive_data_it->second;
1239 send_status_it it = recv_data.send_status_list.begin();
1240 send_status_it it_end = recv_data.send_status_list.end();
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__);
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__);
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.");
1268 for (it = recv_data.send_status_list.begin();
1269 it != recv_data.send_status_list.end();
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();
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");
1280 formatter % recv_data.send_status_list.size() % datadump;
1281 putLogDebug(100040, formatter.str(), __FILE__, __LINE__);
1283 /*------DEBUG LOG END------*/
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) {
1291 recv_data.send_status_list.erase(it++);
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;
1300 //item status is SEND_NG
1302 data_remain_start = it->send_offset;
1303 data_remain_size = it->unsend_size;
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.");
1316 for (it = recv_data.send_status_list.begin();
1317 it != recv_data.send_status_list.end();
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();
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");
1328 formatter % recv_data.send_status_list.size() % datadump;
1329 putLogDebug(100041, formatter.str(), __FILE__, __LINE__);
1331 /*------DEBUG LOG END------*/
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__);
1354 /*------DEBUG LOG END------*/
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__);
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__);
1377 /*------DEBUG LOG END------*/
1378 memset(buffer2, 0, buffer_size);
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__);
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__);
1405 /*------DEBUG LOG END------*/
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__);
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__);
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__,
1441 /*------DEBUG LOG END------*/
1442 delete[] recv_data.receive_buffer1;
1443 recv_data.receive_buffer1 = NULL;
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__,
1454 /*------DEBUG LOG END------*/
1455 delete[] recv_data.receive_buffer2;
1456 recv_data.receive_buffer2 = NULL;
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;
1466 //buffer's max size >= remain data size + request size
1467 //buffer isn't need reallocate, but switch
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__);
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__);
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__);
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__);
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__);
1530 /*------DEBUG LOG END------*/
1531 //set buffer2 as using buffer
1532 recv_data.receive_buffer = recv_data.receive_buffer2;
1534 //using buffer is buffer2
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__);
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__);
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__);
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__);
1588 /*------DEBUG LOG END------*/
1589 //set buffer1 as using buffer
1590 recv_data.receive_buffer = recv_data.receive_buffer1;
1594 //set buffer's rest size
1595 recv_data.receive_buffer_rest_size = recv_data.receive_buffer_max_size - data_remain_size - recvlen;
1597 //remain_size recalc
1598 data_remain_size += recvlen;
1600 send_status_it it_begin = recv_data.send_status_list.begin();
1601 send_status_it it_end = recv_data.send_status_list.end();
1604 for (; it_begin != it_end; ++it_begin) {
1605 it_begin->send_offset -= data_remain_start;
1608 //buffer's rest size >= request size
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__);
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__);
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,
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__);
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;
1650 it = recv_data.send_status_list.begin();
1651 it_end = recv_data.send_status_list.end();
1653 //set request rest size
1654 request_data_remain_size = recvlen;
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;
1671 //send rest size <= request size
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;
1682 //set edit_division flag off
1683 it->edit_division = EDIT_DIVISION_NO_EDIT;
1684 //set status SEND_OK
1685 it->status = SEND_OK;
1688 else if (it->status == SEND_NG) {
1689 if (forwarded_for == FORWARDED_FOR_ON) {
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__);
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__);
1713 /*------DEBUG LOG END------*/
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__);
1728 /*------DEBUG LOG END------*/
1729 //search http header result is OK
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__);
1742 /*------DEBUG LOG END------*/
1743 //search Content_Length result is OK
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)
1749 if (pos == content_length_header_len) {
1750 throw std::string("Content_Length field's value is invalid.");
1755 str_value.assign(recv_data.receive_buffer + it->send_offset + content_length_header_offset + pos,
1756 content_length_header_len - pos);
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);
1763 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos);
1765 str_value = str_value.substr(pos);
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.");
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;
1777 //search Content_Length result is NG
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;
1784 it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
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__);
1797 /*------DEBUG LOG END------*/
1799 //set edit_division flag on
1800 it->edit_division = EDIT_DIVISION_EDIT;
1802 //search http header result is NG
1804 //unsend_size recalc
1805 it->unsend_size += request_data_remain_size;
1806 //request data rest size recalc
1807 request_data_remain_size = 0;
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;
1818 //check method and version result is CHECK_IMPOSSIBLE
1820 //unsend_size recalc
1821 it->unsend_size += request_data_remain_size;
1822 //request data rest size recalc
1823 request_data_remain_size = 0;
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;
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;
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;
1848 //change status from SEND_NG to SEND_OK
1849 it->status = SEND_OK;
1851 //no request rest data to process
1852 if (request_data_remain_size <= 0) {
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.");
1863 for (it = recv_data.send_status_list.begin();
1864 it != recv_data.send_status_list.end();
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();
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");
1875 formatter % recv_data.send_status_list.size() % datadump;
1876 putLogDebug(100065, formatter.str(), __FILE__, __LINE__);
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;
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();
1897 new_send_it->send_offset = recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size
1898 - request_data_remain_size;
1900 if (forwarded_for == FORWARDED_FOR_ON || session_data->sorry_flag == SORRY_FLAG_ON) {
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__);
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__);
1926 /*------DEBUG LOG END------*/
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__);
1941 /*------DEBUG LOG END------*/
1942 //searched whole http header
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__);
1955 /*------DEBUG LOG END------*/
1957 //searched ContentLength http header
1959 //Get Content_Length header's numeric value
1961 recv_data.receive_buffer[new_send_it->send_offset + content_length_header_offset + pos] != ':'
1962 && pos < content_length_header_len;
1964 if (pos == content_length_header_len) {
1965 throw std::string("Content_Length field's value is invalid.");
1970 str_value.assign(recv_data.receive_buffer + new_send_it->send_offset + content_length_header_offset + pos,
1971 content_length_header_len - pos);
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);
1978 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos);
1980 str_value = str_value.substr(pos);
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.");
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;
1990 //not searched ContentLength http header
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;
1997 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
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__);
2011 /*------DEBUG LOG END------*/
2013 //set edit_division flag on
2014 new_send_it->edit_division = EDIT_DIVISION_EDIT;
2016 //not searched whole http header
2018 new_send_it->unsend_size = request_data_remain_size;
2019 request_data_remain_size = 0;
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;
2029 //check http method or version result is CHECK_IMPOSSIBLE
2031 new_send_it->unsend_size = request_data_remain_size;
2032 request_data_remain_size = 0;
2036 new_send_it->edit_division = EDIT_DIVISION_NO_EDIT;
2037 new_send_it->send_rest_size = request_data_remain_size;
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;
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;
2053 //change status from SEND_NG to SEND_OK
2054 new_send_it->status = SEND_OK;
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.");
2064 for (it = recv_data.send_status_list.begin();
2065 it != recv_data.send_status_list.end();
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();
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");
2076 formatter % recv_data.send_status_list.size() % datadump;
2077 putLogDebug(100070, formatter.str(), __FILE__, __LINE__);
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()) {
2086 if (session_data->sorry_flag == SORRY_FLAG_ON) {
2087 status = SORRYSERVER_CONNECT;
2091 status = REALSERVER_CONNECT;
2094 //the data that can be sent possible is not exist
2096 status = CLIENT_RECV;
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__);
2107 /*------DEBUG LOG END------*/
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__);
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__);
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__);
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__);
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__);
2148 /*------DEBUG LOG END------*/
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)
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__);
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;
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__);
2191 /*------DEBUG LOG END------*/
2197 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
2207 session_data = session_thread_it->second;
2210 //call schedule_module's schedule function, get realserver endpoint
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);
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__);
2224 /*------DEBUG LOG END------*/
2227 if (rs_endpoint != tmp_endpoint) {
2229 session_data->target_endpoint = rs_endpoint;
2230 status = REALSERVER_CONNECT;
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__);
2241 /*------DEBUG LOG END------*/
2242 status = SORRYSERVER_SELECT;
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__);
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__);
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__);
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__);
2280 /*------DEBUG LOG END------*/
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)
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__);
2305 /*------DEBUG LOG END------*/
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)
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) : "
2322 formatter % thread_id;
2323 putLogDebug(100080, formatter.str(), __FILE__, __LINE__);
2325 /*------DEBUG LOG END------*/
2327 EVENT_TAG status = FINALIZE;
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;
2342 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
2353 session_data = session_thread_it->second;
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__);
2365 //receive_buffer pointer check
2366 receive_data &recv_data = receive_data_it->second;
2367 if (unlikely(recv_data.receive_buffer == NULL)) {
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__);
2382 //send buffer rest size initialization
2383 send_buffer_remian_size = send_buffer_end_size;
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()) {
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__);
2405 /*------DEBUG LOG END------*/
2406 //search http header result is OK
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);
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;
2419 //search http header result is NG
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__);
2432 /*------DEBUG LOG END------*/
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__);
2446 /*------DEBUG LOG END------*/
2449 //create X-Forwarded-For header, put it to edata.data
2450 edata.data = str_forword_for;
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;
2462 //add to edit_data_list
2463 it->edit_data_list.push_back(edata);
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__);
2473 /*------DEBUG LOG END------*/
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;
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__);
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);
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__);
2516 /*------DEBUG LOG END------*/
2518 it->send_end_size += copy_size;
2519 it->send_possible_size = 0;
2520 send_buffer_remian_size -= copy_size;
2522 //send_possible_size is larger
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);
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__);
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);
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__);
2557 /*------DEBUG LOG END------*/
2558 it->send_end_size += copy_size;
2559 it->send_possible_size -= copy_size;
2560 send_buffer_remian_size = 0;
2565 //edit_data_list is not empty
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);
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__);
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__);
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;
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);
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__);
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__);
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);
2652 //data that before X-Forwarded-For is larger than send_buffer_remain_size
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);
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__);
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);
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__);
2687 /*------DEBUG LOG END------*/
2689 it->send_end_size += copy_size;
2690 it->send_possible_size -= copy_size;
2691 send_buffer_remian_size -= copy_size;
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__);
2703 /*------DEBUG LOG END------*/
2705 //edit_division flag is off
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__);
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__);
2738 /*------DEBUG LOG END------*/
2739 it->send_end_size = copy_size;
2740 it->send_possible_size = 0;
2741 send_buffer_remian_size -= copy_size;
2743 //buffer rest size is larger
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);
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__);
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__);
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;
2779 //set copied data length
2780 datalen = send_buffer_end_size - send_buffer_remian_size;
2781 status = REALSERVER_SEND;
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__);
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__);
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__);
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__);
2817 /*------DEBUG LOG END------*/
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)
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__);
2838 /*------DEBUG LOG END------*/
2840 EVENT_TAG status = FINALIZE;
2841 thread_data_ptr session_data;
2842 session_thread_data_map_it session_thread_it;
2845 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
2855 session_data = session_thread_it->second;
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__);
2866 /*------DEBUG LOG END------*/
2867 status = CLIENT_DISCONNECT;
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__);
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__);
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__);
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__);
2902 /*------DEBUG LOG END------*/
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)
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) : "
2916 formatter % thread_id;
2917 putLogDebug(100106, formatter.str(), __FILE__, __LINE__);
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;
2927 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
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__);
2938 session_data = session_thread_it->second;
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__);
2950 receive_data &recv_data = receive_data_it->second;
2952 send_status_it it = recv_data.send_status_list.begin();
2953 send_status_it it_end = recv_data.send_status_list.end();
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__);
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__);
2974 //sending possible data is exist
2975 if (it->send_possible_size > 0) {
2976 //status remain SEND_OK
2977 it->status = SEND_OK;
2979 it->send_offset += it->send_end_size;
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;
2987 //send_end_size recalc
2988 it->send_end_size = 0;
2990 //sending possible data is not exist
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;
2997 //can not receive from client continue
2999 //change status from SEND_OK to SEND_END
3000 it->status = SEND_END;
3004 it = recv_data.send_status_list.begin();
3005 it = find_if(it, it_end, data_send_ok());
3006 //send_ok item is exist
3008 status = REALSERVER_CONNECT;
3010 //send_ok item is exist
3012 status = CLIENT_RECV;
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__);
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__);
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__);
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__);
3049 /*------DEBUG LOG END------*/
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)
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__);
3070 /*------DEBUG LOG END------*/
3071 EVENT_TAG status = FINALIZE;
3072 boost::asio::ip::tcp::endpoint client_endpoint;
3074 thread_data_ptr session_data;
3075 session_thread_data_map_it session_thread_it;
3076 receive_data_map_it receive_data_it;
3079 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
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__);
3089 session_data = session_thread_it->second;
3090 //set sorry_endpoint
3091 session_data->target_endpoint = sorry_endpoint;
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__);
3102 status = SORRYSERVER_CONNECT;
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__);
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__);
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__);
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__);
3135 /*-------- DEBUG LOG --------*/
3136 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
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__);
3145 /*------DEBUG LOG END------*/
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)
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) : "
3163 formatter % thread_id;
3164 putLogDebug(100112, formatter.str(), __FILE__, __LINE__);
3166 /*------DEBUG LOG END------*/
3167 EVENT_TAG status = FINALIZE;
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;
3184 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
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__);
3195 session_data = session_thread_it->second;
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__);
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;
3214 boost::format formatter("Invalid pointer. thread id : %d.");
3215 formatter % boost::this_thread::get_id();
3216 putLogError(100065, formatter.str(), __FILE__, __LINE__);
3222 send_status_it it = recv_data.send_status_list.begin();
3223 send_status_it it_end = recv_data.send_status_list.end();
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__);
3233 //send buffer rest size initialization
3234 send_buffer_remian_size = send_buffer_end_size;
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()) {
3242 edata.data_size = 0;
3243 edata.insert_posission = 0;
3244 edata.replace_size = 0;
3246 if (strlen(sorry_uri.data()) > 0) {
3247 ret = find_uri(recv_data.receive_buffer + it->send_offset, it->send_possible_size, url_offset,
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__);
3257 /*------DEBUG LOG END------*/
3258 //search http header result is OK
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);
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__);
3285 /*------DEBUG LOG END------*/
3287 //search http header result is OK
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);
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;
3300 //search http header result is NG
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__);
3313 /*------DEBUG LOG END------*/
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__);
3327 /*------DEBUG LOG END------*/
3330 //create X-Forwarded-For header, set it to edata.data
3331 edata.data = str_forword_for;
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;
3343 //add to edit_data_list
3344 it->edit_data_list.push_back(edata);
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__);
3355 /*------DEBUG LOG END------*/
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;
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__);
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__);
3396 /*------DEBUG LOG END------*/
3397 it->send_end_size += copy_size;
3398 it->send_possible_size = 0;
3399 send_buffer_remian_size -= copy_size;
3401 //send_possible_size is larger
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__);
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,
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__);
3435 /*------DEBUG LOG END------*/
3436 it->send_end_size += copy_size;
3437 it->send_possible_size -= copy_size;
3438 send_buffer_remian_size = 0;
3444 //edit_data_list is not empty
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);
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__);
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__);
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;
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__);
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);
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__);
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);
3531 //data that before X-Forwarded-For/uri is larger than send_buffer_remain_size
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);
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__);
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);
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__);
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;
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__);
3581 /*------DEBUG LOG END------*/
3583 //edit_division flag is off
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);
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__);
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);
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__);
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;
3623 //buffer rest size is larger
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);
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__);
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);
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__);
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;
3661 //set copied data length
3662 datalen = send_buffer_end_size - send_buffer_remian_size;
3664 status = SORRYSERVER_SEND;
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__);
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__);
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__);
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__);
3702 /*------DEBUG LOG END------*/
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)
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__);
3723 /*------DEBUG LOG END------*/
3725 EVENT_TAG status = FINALIZE;
3726 thread_data_ptr session_data;
3727 session_thread_data_map_it session_thread_it;
3730 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
3740 session_data = session_thread_it->second;
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__);
3751 /*------DEBUG LOG END------*/
3753 status = CLIENT_DISCONNECT;
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__);
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__);
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__);
3780 /*-------- DEBUG LOG --------*/
3781 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
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__);
3790 /*------DEBUG LOG END------*/
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)
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) : "
3805 formatter % thread_id;
3806 putLogDebug(100139, formatter.str(), __FILE__, __LINE__);
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;
3816 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
3827 session_data = session_thread_it->second;
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__);
3840 receive_data &recv_data = receive_data_it->second;
3842 send_status_it it = recv_data.send_status_list.begin();
3843 send_status_it it_end = recv_data.send_status_list.end();
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__);
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__);
3863 //sending possible data is exist
3864 if (it->send_possible_size > 0) {
3865 //status remain SEND_OK
3866 it->status = SEND_OK;
3868 it->send_offset += it->send_end_size;
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;
3876 //send_end_size recalc
3877 it->send_end_size = 0;
3879 //sending possible data is not exist
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;
3886 //can not receive from client continue
3888 //change status from SEND_OK to SEND_END
3889 it->status = SEND_END;
3893 it = recv_data.send_status_list.begin();
3894 it = find_if(it, it_end, data_send_ok());
3895 //send_ok item is exist
3897 status = SORRYSERVER_CONNECT;
3899 //send_ok item is exist
3901 status = CLIENT_RECV;
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__);
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__);
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__);
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__);
3938 /*------DEBUG LOG END------*/
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)
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__);
3964 /*------DEBUG LOG END------*/
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)
3978 /*-------- DEBUG LOG --------*/
3979 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
3980 size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
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()
3990 putLogDebug(100143, formatter.str(), __FILE__, __LINE__);
3992 /*------DEBUG LOG END------*/
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;
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";
4014 CHECK_RESULT_TAG check_result;
4015 session_thread_data_map_it session_thread_it;
4016 receive_data_map_it receive_data_it;
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__);
4034 /*------DEBUG LOG END------*/
4040 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
4051 session_data = session_thread_it->second;
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;
4060 session_data->target_endpoint = rs_endpoint;
4062 receive_data &recv_data = session_data->receive_data_map[rs_endpoint];
4064 send_status_it it = recv_data.send_status_list.begin();
4065 send_status_it it_end = recv_data.send_status_list.end();
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__);
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__);
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.");
4093 for (it = recv_data.send_status_list.begin();
4094 it != recv_data.send_status_list.end();
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();
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");
4105 formatter % recv_data.send_status_list.size() % datadump;
4106 putLogDebug(100145, formatter.str(), __FILE__, __LINE__);
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) {
4115 recv_data.send_status_list.erase(it++);
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;
4124 //item status is SEND_NG
4126 data_remain_start = it->send_offset;
4127 data_remain_size = it->unsend_size;
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.");
4140 for (it = recv_data.send_status_list.begin();
4141 it != recv_data.send_status_list.end();
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();
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");
4152 formatter % recv_data.send_status_list.size() % datadump;
4153 putLogDebug(100146, formatter.str(), __FILE__, __LINE__);
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__);
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__);
4179 /*------DEBUG LOG END------*/
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__);
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__);
4202 /*-------- DEBUG LOG END--------*/
4203 memset(buffer2, 0, buffer_size);
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);
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__);
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);
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__);
4232 /*------DEBUG LOG END------*/
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__);
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__);
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__,
4268 /*------DEBUG LOG END------*/
4269 delete[] recv_data.receive_buffer1;
4270 recv_data.receive_buffer1 = NULL;
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__,
4281 /*------DEBUG LOG END------*/
4282 delete[] recv_data.receive_buffer2;
4283 recv_data.receive_buffer2 = NULL;
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;
4293 //buffer's max size >= remain data size + request size
4294 //buffer isn't need reallocate, but switch
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__);
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__);
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__);
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__);
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);
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__);
4358 /*------DEBUG LOG END------*/
4359 //set buffer2 as using buffer
4360 recv_data.receive_buffer = recv_data.receive_buffer2;
4362 //using buffer is buffer2
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);
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__);
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);
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__);
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);
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__);
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);
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__);
4420 /*------DEBUG LOG END------*/
4421 //set buffer1 as using buffer
4422 recv_data.receive_buffer = recv_data.receive_buffer1;
4426 //set buffer's rest size
4427 recv_data.receive_buffer_rest_size = recv_data.receive_buffer_max_size - data_remain_size - recvlen;
4429 //remain_size recalc
4430 data_remain_size += recvlen;
4432 send_status_it it_begin = recv_data.send_status_list.begin();
4433 send_status_it it_end = recv_data.send_status_list.end();
4435 for (; it_begin != it_end; ++it_begin) {
4436 it_begin->send_offset -= data_remain_start;
4439 //buffer's rest size >= request size
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__);
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__);
4459 /*------DEBUG LOG END------*/
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,
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__);
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;
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;
4502 //send rest size <= request size
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;
4513 //change status from SEND_CONTINUE to SEND_OK
4514 it->status = SEND_OK;
4517 else if (it->status == SEND_NG) {
4518 if (forwarded_for == FORWARDED_FOR_ON) {
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__);
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__);
4542 /*------DEBUG LOG END------*/
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__);
4557 /*------DEBUG LOG END------*/
4558 //search http header result is OK
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__);
4571 /*------DEBUG LOG END------*/
4572 //search Content_Length result is OK
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)
4578 if (pos == content_length_header_len) {
4579 throw std::string("Content_Length field's value is invalid.");
4584 str_value.assign(recv_data.receive_buffer + it->send_offset + content_length_header_offset + pos,
4585 content_length_header_len - pos);
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);
4592 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
4595 str_value = str_value.substr(pos);
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.");
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;
4606 //search Content_Length result is NG
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;
4613 it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
4617 //search http header result is CHECK_NG
4619 it->unsend_size += request_data_remain_size;
4620 request_data_remain_size = 0;
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;
4629 //check method and version result is CHECK_IMPOSSIBLE
4631 it->unsend_size += request_data_remain_size;
4632 request_data_remain_size = 0;
4636 //send_rest_size recalc
4637 it->send_rest_size = it->unsend_size + request_data_remain_size;
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;
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;
4655 //change status from SEND_NG to SEND_OK
4656 it->status = SEND_OK;
4658 //no request rest data to process
4659 if (request_data_remain_size <= 0) {
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.");
4671 for (it = recv_data.send_status_list.begin();
4672 it != recv_data.send_status_list.end();
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();
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");
4683 formatter % recv_data.send_status_list.size() % datadump;
4684 putLogDebug(100170, formatter.str(), __FILE__, __LINE__);
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();
4704 new_send_it->send_offset = recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size
4705 - request_data_remain_size;
4707 if (forwarded_for == FORWARDED_FOR_ON) {
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__);
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__);
4733 /*------DEBUG LOG END------*/
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__);
4748 /*------DEBUG LOG END------*/
4749 //searched whole http header
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__);
4762 /*------DEBUG LOG END------*/
4764 //searched ContentLength http header
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)
4770 if (pos == content_length_header_len) {
4771 throw std::string("Content_Length field's value is invalid.");
4776 str_value.assign(recv_data.receive_buffer + new_send_it->send_offset + content_length_header_offset + pos,
4777 content_length_header_len - pos);
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);
4784 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
4787 str_value = str_value.substr(pos);
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.");
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;
4797 //not searched ContentLength http header
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;
4804 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
4809 //not searched whole http header
4811 new_send_it->unsend_size = request_data_remain_size;
4812 request_data_remain_size = 0;
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;
4820 //check http method or version result is CHECK_IMPOSSIBLE
4822 new_send_it->unsend_size = request_data_remain_size;
4823 request_data_remain_size = 0;
4827 new_send_it->send_rest_size = request_data_remain_size;
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;
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;
4844 //change status from SEND_NG to SEND_OK
4845 new_send_it->status = SEND_OK;
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.");
4854 for (it = recv_data.send_status_list.begin();
4855 it != recv_data.send_status_list.end();
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();
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");
4866 formatter % recv_data.send_status_list.size() % datadump;
4867 putLogDebug(100175, formatter.str(), __FILE__, __LINE__);
4869 /*------DEBUG LOG END------*/
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;
4878 //the data that can be sent possible is not exist
4880 status = REALSERVER_RECV;
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__);
4890 /*------DEBUG LOG END------*/
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__);
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__);
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__);
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__);
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__);
4932 /*------DEBUG LOG END------*/
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)
4949 /*-------- DEBUG LOG --------*/
4950 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
4951 size_t buffer_size = recvbuffer.size() < recvlen ? recvbuffer.size() : recvlen;
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()
4961 putLogDebug(100178, formatter.str(), __FILE__, __LINE__);
4963 /*------DEBUG LOG END------*/
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;
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;
4985 CHECK_RESULT_TAG check_result;
4986 session_thread_data_map_it session_thread_it;
4987 receive_data_map_it receive_data_it;
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__,
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__);
5006 /*------DEBUG LOG END------*/
5012 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
5022 session_data = session_thread_it->second;
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;
5031 session_data->target_endpoint = sorry_endpoint;
5033 receive_data &recv_data = session_data->receive_data_map[sorry_endpoint];
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__);
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__);
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.");
5063 for (it = recv_data.send_status_list.begin();
5064 it != recv_data.send_status_list.end();
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();
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");
5075 formatter % recv_data.send_status_list.size() % datadump;
5076 putLogDebug(100180, formatter.str(), __FILE__, __LINE__);
5078 /*------DEBUG LOG END------*/
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) {
5086 recv_data.send_status_list.erase(it++);
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;
5095 //item status is SEND_NG
5097 data_remain_start = it->send_offset;
5098 data_remain_size = it->unsend_size;
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.");
5111 for (it = recv_data.send_status_list.begin();
5112 it != recv_data.send_status_list.end();
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();
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");
5123 formatter % recv_data.send_status_list.size() % datadump;
5124 putLogDebug(100181, formatter.str(), __FILE__, __LINE__);
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__);
5149 /*------DEBUG LOG END------*/
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__);
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__);
5171 /*-------- DEBUG LOG END--------*/
5172 memset(buffer2, 0, buffer_size);
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__);
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__);
5199 /*------DEBUG LOG END------*/
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__);
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__);
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__,
5235 /*------DEBUG LOG END------*/
5236 delete[] recv_data.receive_buffer1;
5237 recv_data.receive_buffer1 = NULL;
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__,
5248 /*------DEBUG LOG END------*/
5249 delete[] recv_data.receive_buffer2;
5250 recv_data.receive_buffer2 = NULL;
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;
5260 //buffer's max size >= remain data size + request size
5261 //buffer isn't need reallocate, but switch
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__);
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__);
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__);
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__);
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__);
5324 /*------DEBUG LOG END------*/
5325 //set buffer2 as using buffer
5326 recv_data.receive_buffer = recv_data.receive_buffer2;
5328 //using buffer is buffer2
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__);
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__);
5357 /*------DEBUG LOG END------*/
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__);
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__);
5383 /*------DEBUG LOG END------*/
5384 //set buffer1 as using buffer
5385 recv_data.receive_buffer = recv_data.receive_buffer1;
5389 //set buffer's rest size
5390 recv_data.receive_buffer_rest_size = recv_data.receive_buffer_max_size - data_remain_size - recvlen;
5392 //remain_size recalc
5393 data_remain_size += recvlen;
5395 send_status_it it_begin = recv_data.send_status_list.begin();
5396 send_status_it it_end = recv_data.send_status_list.end();
5398 for (; it_begin != it_end; ++it_begin) {
5399 it_begin->send_offset -= data_remain_start;
5402 //buffer's rest size >= request size
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__);
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__);
5423 /*------DEBUG LOG END------*/
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__);
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;
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) {
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;
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;
5467 //change status from SEND_CONTINUE to SEND_OK
5468 it->status = SEND_OK;
5471 else if (it->status == SEND_NG) {
5472 if (forwarded_for == FORWARDED_FOR_ON) {
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__);
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__);
5496 /*------DEBUG LOG END------*/
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__);
5511 /*------DEBUG LOG END------*/
5512 //search http header result is OK
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__);
5525 /*------DEBUG LOG END------*/
5526 //search Content_Length result is OK
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)
5532 if (pos == content_length_header_len) {
5533 throw std::string("Content_Length field's value is invalid.");
5538 str_value.assign(recv_data.receive_buffer + it->send_offset + content_length_header_offset + pos,
5539 content_length_header_len - pos);
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);
5546 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
5549 str_value = str_value.substr(pos);
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.");
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;
5560 //search Content_Length result is NG
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;
5567 it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
5571 //search http header result is CHECK_NG
5573 it->unsend_size += request_data_remain_size;
5574 request_data_remain_size = 0;
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;
5582 //check method and version result is CHECK_IMPOSSIBLE
5584 it->unsend_size += request_data_remain_size;
5585 request_data_remain_size = 0;
5589 it->send_rest_size = it->unsend_size + request_data_remain_size;
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;
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;
5607 //change status from SEND_NG to SEND_OK
5608 it->status = SEND_OK;
5610 //no request rest data to process
5611 if (request_data_remain_size <= 0) {
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.");
5622 for (it = recv_data.send_status_list.begin();
5623 it != recv_data.send_status_list.end();
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();
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");
5634 formatter % recv_data.send_status_list.size() % datadump;
5635 putLogDebug(100205, formatter.str(), __FILE__, __LINE__);
5637 /*------DEBUG LOG END------*/
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();
5656 new_send_it->send_offset = recv_data.receive_buffer_max_size - recv_data.receive_buffer_rest_size
5657 - request_data_remain_size;
5659 if (forwarded_for == FORWARDED_FOR_ON) {
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__);
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__);
5685 /*------DEBUG LOG END------*/
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__);
5700 /*------DEBUG LOG END------*/
5701 //searched whole http header
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__);
5714 /*------DEBUG LOG END------*/
5716 //searched ContentLength http header
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)
5722 if (pos == content_length_header_len) {
5723 throw std::string("Content_Length field's value is invalid.");
5727 str_value.assign(recv_data.receive_buffer + new_send_it->send_offset + content_length_header_offset + pos,
5728 content_length_header_len - pos);
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);
5735 for (pos = 0; !isgraph(str_value[pos]) && str_value[pos] != '\0'; ++pos)
5738 str_value = str_value.substr(pos);
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.");
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;
5748 //not searched ContentLength http header
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;
5755 new_send_it->send_rest_size = header_offset + header_offset_len + cr_lf_cr_lf_len;
5761 //not searched whole http header
5763 new_send_it->unsend_size = request_data_remain_size;
5764 request_data_remain_size = 0;
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;
5772 //check http method or version result is CHECK_IMPOSSIBLE
5774 new_send_it->unsend_size = request_data_remain_size;
5775 request_data_remain_size = 0;
5779 new_send_it->send_rest_size = request_data_remain_size;
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;
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;
5795 //change status from SEND_NG to SEND_OK
5796 new_send_it->status = SEND_OK;
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.");
5805 for (it = recv_data.send_status_list.begin();
5806 it != recv_data.send_status_list.end();
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();
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");
5817 formatter % recv_data.send_status_list.size() % datadump;
5818 putLogDebug(100210, formatter.str(), __FILE__, __LINE__);
5820 /*------DEBUG LOG END------*/
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;
5829 //the data that can be sent possible is not exist
5831 status = SORRYSERVER_RECV;
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__);
5841 /*------DEBUG LOG END------*/
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__);
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__);
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__);
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__);
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__);
5883 /*------DEBUG LOG END------*/
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)
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__);
5902 /*------DEBUG LOG END------*/
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)
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) : "
5921 formatter % thread_id;
5922 putLogDebug(100214, formatter.str(), __FILE__, __LINE__);
5924 /*------DEBUG LOG END------*/
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;
5934 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
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__);
5944 session_data = session_thread_it->second;
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__);
5955 receive_data &recv_data = receive_data_it->second;
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__);
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);
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__);
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);
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__);
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;
6004 //buffer size < sending_possible size
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);
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__);
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);
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__);
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;
6042 status = CLIENT_SEND;
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__);
6051 /*------DEBUG LOG END------*/
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__);
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__);
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__);
6078 /*------DEBUG LOG END------*/
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)
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__);
6103 /*------DEBUG LOG END------*/
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)
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) : "
6118 formatter % thread_id;
6119 putLogDebug(100222, formatter.str(), __FILE__, __LINE__);
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;
6129 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
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__);
6138 session_data = session_thread_it->second;
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__);
6149 receive_data &recv_data = receive_data_it->second;
6151 send_status_it it = recv_data.send_status_list.begin();
6152 send_status_it it_end = recv_data.send_status_list.end();
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__);
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__);
6172 //sending possible data is exist
6173 if (it->send_possible_size > 0) {
6174 //status remain SEND_OK
6175 it->status = SEND_OK;
6177 it->send_offset += it->send_end_size;
6178 //send_end_size recalc
6179 it->send_end_size = 0;
6181 //sending possible data is not exist
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;
6188 //can not receive from client continue
6190 //change status from SEND_OK to SEND_END
6191 it->status = SEND_END;
6195 it = recv_data.send_status_list.begin();
6196 it = find_if(it, it_end, data_send_ok());
6197 //send_ok item is exist
6199 status = CLIENT_CONNECTION_CHECK;
6201 //send_ok item is not exist
6204 if (session_data->end_flag == END_FLAG_ON) {
6205 status = CLIENT_DISCONNECT;
6210 if (session_data->sorry_flag == SORRY_FLAG_ON) {
6211 status = SORRYSERVER_RECV;
6215 status = REALSERVER_RECV;
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__);
6227 /*------DEBUG LOG END------*/
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__);
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__);
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__);
6252 /*------DEBUG LOG END------*/
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)
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__);
6270 /*------DEBUG LOG END------*/
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)
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) : "
6285 formatter % thread_id;
6286 putLogDebug(100226, formatter.str(), __FILE__, __LINE__);
6288 /*------DEBUG LOG END------*/
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;
6301 boost::mutex::scoped_lock slock(session_thread_data_map_mutex);
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__);
6311 session_data = session_thread_it->second;
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__);
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__);
6333 /*------DEBUG LOG END------*/
6334 status = SORRYSERVER_RECV;
6336 receive_data &recv_data = receive_data_it->second;
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;
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;
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;
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) {
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__);
6368 /*------DEBUG LOG END------*/
6371 //accept_end_flag is on
6374 if (session_data->sorry_flag == SORRY_FLAG_ON) {
6375 if (send_possible) {
6376 status = SORRYSERVER_CONNECT;
6378 status = SORRYSERVER_SELECT;
6382 //set sorry flag off
6384 //the data that can be sent continue is exist
6385 if (send_continue) {
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__);
6395 /*------DEBUG LOG END------*/
6396 status = REALSERVER_DISCONNECT;
6398 //the data that can be sent continue is not exist
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__);
6409 /*------DEBUG LOG END------*/
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__);
6419 /*------DEBUG LOG END------*/
6420 status = REALSERVER_DISCONNECT;
6428 if (session_data->sorry_flag == SORRY_FLAG_ON) {
6429 //sending possible data is exist
6430 if (send_possible) {
6431 status = CLIENT_CONNECTION_CHECK;
6433 //sending possible data is not exist
6435 status = SORRYSERVER_RECV;
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__);
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__);
6458 /*------DEBUG LOG END------*/
6460 //sending NG data is exist or send_rest_size > 0
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__);
6471 /*------DEBUG LOG END------*/
6472 status = REALSERVER_DISCONNECT;
6476 //sending possible data is exist
6477 if (send_possible) {
6478 status = CLIENT_CONNECTION_CHECK;
6480 //sending possible data is not exist
6482 status = SORRYSERVER_RECV;
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__);
6496 /*------DEBUG LOG END------*/
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__);
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__);
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__);
6521 /*------DEBUG LOG END------*/
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)
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) : "
6537 formatter % thread_id;
6538 putLogDebug(100237, formatter.str(), __FILE__, __LINE__);
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;
6550 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
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__);
6560 session_data = session_thread_it->second;
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__);
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__);
6583 /*------DEBUG LOG END------*/
6584 status = REALSERVER_RECV;
6586 receive_data &recv_data = receive_data_it->second;
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;
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;
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;
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__);
6620 /*------DEBUG LOG END------*/
6624 //accept_end_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) {
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__);
6639 /*------DEBUG LOG END------*/
6640 status = SORRYSERVER_DISCONNECT;
6642 //the data that can be sent continue is not exist
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__);
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__);
6663 /*------DEBUG LOG END------*/
6664 status = SORRYSERVER_DISCONNECT;
6669 if (send_possible) {
6670 status = REALSERVER_CONNECT;
6672 status = REALSERVER_SELECT;
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__);
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__);
6699 /*------DEBUG LOG END------*/
6700 //sending NG data is exist or send_rest_size > 0
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__);
6711 /*------DEBUG LOG END------*/
6712 status = SORRYSERVER_DISCONNECT;
6716 //sending possible data is exist
6717 if (send_possible) {
6718 status = CLIENT_CONNECTION_CHECK;
6720 //sending possible data is not exist
6722 status = REALSERVER_RECV;
6728 //sending possible data is exist
6729 if (send_possible) {
6730 status = CLIENT_CONNECTION_CHECK;
6732 //sending possible data is not exist
6734 status = REALSERVER_RECV;
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__);
6749 /*------DEBUG LOG END------*/
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__);
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__);
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__);
6774 /*------DEBUG LOG END------*/
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)
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__);
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;
6802 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
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__);
6812 session_data = session_thread_it->second;
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__);
6827 /*------DEBUG LOG END------*/
6832 receive_data &recv_data = receive_data_it->second;
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;
6842 if (session_data->thread_division == THREAD_DIVISION_UP_STREAM) {
6844 if (session_data->end_flag == END_FLAG_ON) {
6845 status = CLIENT_RECV;
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__);
6859 /*------DEBUG LOG END------*/
6861 status = SORRYSERVER_SELECT;
6864 //sorryserver_switch_flag is off
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__);
6875 /*------DEBUG LOG END------*/
6876 status = CLIENT_RECV;
6882 if (session_data->end_flag == END_FLAG_ON) {
6883 status = CLIENT_DISCONNECT;
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__);
6894 /*------DEBUG LOG END------*/
6895 status = SORRYSERVER_RECV;
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__);
6906 /*------DEBUG LOG END------*/
6907 status = CLIENT_DISCONNECT;
6911 //the data that can be sent possible is exist
6912 if (possible_flag) {
6913 status = CLIENT_CONNECTION_CHECK;
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__);
6924 /*------DEBUG LOG END------*/
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__);
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__);
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__);
6950 /*------DEBUG LOG END------*/
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)
6963 /*-------- DEBUG LOG --------*/
6964 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
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__);
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;
6982 boost::mutex::scoped_lock sclock(session_thread_data_map_mutex);
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__);
6992 session_data = session_thread_it->second;
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__);
7005 receive_data &recv_data = receive_data_it->second;
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;
7015 if (session_data->thread_division == THREAD_DIVISION_UP_STREAM) {
7017 if (session_data->end_flag == END_FLAG_ON) {
7018 status = CLIENT_RECV;
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__);
7032 /*------DEBUG LOG END------*/
7033 status = REALSERVER_SELECT;
7035 //realserver_switch_flag is off
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__);
7046 /*------DEBUG LOG END------*/
7047 status = CLIENT_RECV;
7053 if (session_data->end_flag == END_FLAG_ON) {
7054 status = CLIENT_DISCONNECT;
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__);
7065 /*------DEBUG LOG END------*/
7066 status = REALSERVER_RECV;
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__);
7076 /*------DEBUG LOG END------*/
7077 status = CLIENT_DISCONNECT;
7081 //the data that can be sent possible is exist
7082 if (possible_flag) {
7083 status = CLIENT_CONNECTION_CHECK;
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__);
7094 /*------DEBUG LOG END------*/
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__);
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__);
7112 /*-------- DEBUG LOG --------*/
7113 if (unlikely(LOG_LV_DEBUG == getloglevel())) {
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__);
7122 /*------DEBUG LOG END------*/
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)
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__);
7143 /*------DEBUG LOG END------*/
7149 extern "C" l7vs::protocol_module_base*
7152 return dynamic_cast<l7vs::protocol_module_base *>(new l7vs::protocol_module_sessionless());
7156 destroy_module(l7vs::protocol_module_base *in)