<commit_before>﻿#pragma once

#include <boost/uuid/uuid.hpp>

#include "net/http.h"
#include "app/khl-define.h"
#include "app/framework.h"
#include "app/server2hb-np.h"
#include "app/s2h-session.h"
#include "app/s2hb-storage.h"
#include "app/config.h"
#include "util/utils.h"
#include "util/i18n.h"

namespace khorost {
    class server2_hb : public network::connection_context {
    protected:
        class cb_connection : public network::connection {
        protected:
            virtual size_t	data_processing(const boost::uint8_t* buffer, const size_t buffer_size) {
                auto server = reinterpret_cast<server2_hb*>(get_controller()->get_context());
                return server->m_dispatcher.DoProcessCB(*this, buffer, buffer_size);
            }
        public:
            cb_connection(network::connection_controller* pThis_, int ID_, evutil_socket_t fd_, struct sockaddr* sa_, int socklen_) :
                network::connection(pThis_, ID_, fd_, sa_, socklen_)
                {
            }
        };

        class http_connection final : public network::connection {
            bool		        m_authenticate;
            std::string         m_salt_connect;
            boost::uuids::uuid  m_id_user{};

            network::http_text_protocol_header_ptr m_http;
            size_t m_receive_bytes_prev_request, m_send_bytes_prev_request;
        protected:
            size_t	data_processing(const boost::uint8_t* buffer, const size_t buffer_size) override {
                auto server = reinterpret_cast<server2_hb*>(get_controller()->get_context());
                const auto process_bytes = m_http->process_data(buffer, buffer_size);

                if (process_bytes != 0) {
                    if (m_http->is_ready()) {
                        server->process_http(*this);

                        const auto& request = m_http->get_request();
                        const auto& response = m_http->get_response();
                        const auto response_real_length = m_http->is_send_data() ? response.m_content_length_ : 0;
                        const auto logger = server->get_logger();
                        logger->debug("[HTTP] processed traffic in [Head={:d}+Body={:d}], out [Head={:d}+Body={:d}] with status {:d} and connection {}"
                                      , m_receive_bytes - m_receive_bytes_prev_request - request.m_content_length_
                                      , request.m_content_length_
                                      , m_send_bytes - m_send_bytes_prev_request - response_real_length
                                      , response_real_length
                                      , response.m_nCode
                                      , m_http->is_auto_close() ? HTTP_ATTRIBUTE_CONNECTION__CLOSE : HTTP_ATTRIBUTE_CONNECTION__KEEP_ALIVE);

                        if (m_http->is_auto_close()) {
                            close_connection();
                        } else {
                            m_receive_bytes_prev_request = m_receive_bytes;
                            m_send_bytes_prev_request = m_send_bytes;
                        }

                        m_http->clear();
                    }
                }
                return process_bytes;
            }
        public:
            http_connection(network::connection_controller* pThis_, int ID_, evutil_socket_t fd_, struct sockaddr* sa_, int socklen_) :
                network::connection(pThis_, ID_, fd_, sa_, socklen_)
                , m_authenticate(false)
                , m_receive_bytes_prev_request(0)
                , m_send_bytes_prev_request(0) {
                m_http = boost::make_shared<network::http_text_protocol_header>();
            }

            bool is_authenticate() const { return m_authenticate; }
            void set_authenticate(const bool value) { m_authenticate = value; }

            const std::string& get_salt(bool Reset_ = false);

            const network::http_text_protocol_header_ptr&   get_http() const { return m_http; }
            virtual void    get_client_ip(char* buffer, size_t buffer_size);
        };

        db::postgres m_db_connect_;
        config m_configure_;
        std::string m_geoip_city_path_;
        std::string m_geoip_asn_path_;
        std::shared_ptr<spdlog::logger> m_logger_;
        std::shared_ptr<spdlog::logger> m_logger_profiler_;

        typedef std::map<std::string, network::token_ptr> dict_tokens;
        dict_tokens m_refresh_tokens;
        dict_tokens m_access_tokens;
    private:
        class cb_controller final : public network::connection_controller {<commit_msg>fix logger<commit_after>﻿#pragma once

#include <boost/uuid/uuid.hpp>

#include "net/http.h"
#include "app/khl-define.h"
#include "app/framework.h"
#include "app/server2hb-np.h"
#include "app/s2h-session.h"
#include "app/s2hb-storage.h"
#include "app/config.h"
#include "util/utils.h"
#include "util/i18n.h"

namespace khorost {
    class server2_hb : public network::connection_context {
    protected:
        class cb_connection : public network::connection {
        protected:
            virtual size_t	data_processing(const boost::uint8_t* buffer, const size_t buffer_size) {
                auto server = reinterpret_cast<server2_hb*>(get_controller()->get_context());
                return server->m_dispatcher.DoProcessCB(*this, buffer, buffer_size);
            }
        public:
            cb_connection(network::connection_controller* pThis_, int ID_, evutil_socket_t fd_, struct sockaddr* sa_, int socklen_) :
                network::connection(pThis_, ID_, fd_, sa_, socklen_)
                {
            }
        };

        class http_connection final : public network::connection {
            bool		        m_authenticate;
            std::string         m_salt_connect;
            boost::uuids::uuid  m_id_user{};

            network::http_text_protocol_header_ptr m_http;
            size_t m_receive_bytes_prev_request, m_send_bytes_prev_request;
        protected:
            size_t	data_processing(const boost::uint8_t* buffer, const size_t buffer_size) override {
                auto server = reinterpret_cast<server2_hb*>(get_controller()->get_context());
                const auto process_bytes = m_http->process_data(buffer, buffer_size);

                if (process_bytes != 0) {
                    if (m_http->is_ready()) {
                        server->process_http(*this);

                        const auto& request = m_http->get_request();
                        const auto& response = m_http->get_response();
                        const auto response_real_length = m_http->is_send_data() ? response.m_content_length_ : 0;
                        const auto logger = server->get_logger();
                        logger->debug("[HTTP] processed traffic in [Head={:d}+Body={:d}], out [Head={:d}+Body={:d}] with status {:d} and connection {}"
                                      , m_receive_bytes - m_receive_bytes_prev_request - request.m_content_length_
                                      , request.m_content_length_
                                      , m_send_bytes - m_send_bytes_prev_request - response_real_length
                                      , response_real_length
                                      , response.m_nCode
                                      , m_http->is_auto_close() ? HTTP_ATTRIBUTE_CONNECTION__CLOSE : HTTP_ATTRIBUTE_CONNECTION__KEEP_ALIVE);

                        if (m_http->is_auto_close()) {
                            close_connection();
                        } else {
                            m_receive_bytes_prev_request = m_receive_bytes;
                            m_send_bytes_prev_request = m_send_bytes;
                        }

                        m_http->clear();
                    }
                }
                return process_bytes;
            }
        public:
            http_connection(network::connection_controller* pThis_, int ID_, evutil_socket_t fd_, struct sockaddr* sa_, int socklen_) :
                network::connection(pThis_, ID_, fd_, sa_, socklen_)
                , m_authenticate(false)
                , m_receive_bytes_prev_request(0)
                , m_send_bytes_prev_request(0) {
                m_http = boost::make_shared<network::http_text_protocol_header>();
            }

            bool is_authenticate() const { return m_authenticate; }
            void set_authenticate(const bool value) { m_authenticate = value; }

            const std::string& get_salt(bool Reset_ = false);

            const network::http_text_protocol_header_ptr&   get_http() const { return m_http; }
            virtual void    get_client_ip(char* buffer, size_t buffer_size);
        };

        db::postgres m_db_connect_;
        config m_configure_;
        std::string m_geoip_city_path_;
        std::string m_geoip_asn_path_;
        std::shared_ptr<spdlog::logger> m_logger_ = nullptr;
        std::shared_ptr<spdlog::logger> m_logger_profiler_ = nullptr;

        typedef std::map<std::string, network::token_ptr> dict_tokens;
        dict_tokens m_refresh_tokens;
        dict_tokens m_access_tokens;
    private:
        class cb_controller final : public network::connection_controller {
{'fraction_non_alphanumeric': 0.09013477088948788, 'fraction_numerical': 0.0030188679245283017, 'mean_word_length': 1.9447619047619047, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '81809', 'n_tokens_mistral': 2628, 'n_tokens_neox': 2499, 'n_words': 471}