<commit_before>#include <bts/rpc/rpc_server.hpp>
#include <fc/reflect/variant.hpp>
#include <fc/network/tcp_socket.hpp>
#include <fc/rpc/json_connection.hpp>
#include <fc/io/json.hpp>
#include <fc/thread/thread.hpp>
#include <fc/network/http/server.hpp>
#include <fc/interprocess/file_mapping.hpp>
#include <boost/bind.hpp>
#include <boost/algorithm/string/join.hpp>
#include <sstream>
#include <limits>


namespace bts { namespace rpc { 

  namespace detail 
  {
    class rpc_server_impl
    {
       public:
         rpc_server::config  _config;
         client_ptr          _client;
         fc::http::server    _httpd;
         fc::tcp_server      _tcp_serv;
         fc::future<void>    _accept_loop_complete;
         rpc_server*         _self;
         std::string         _username;

         typedef std::map<std::string, rpc_server::method_data> method_map_type;
         method_map_type _method_map;

         /** the set of connections that have successfully logged in */
         std::unordered_set<fc::rpc::json_connection*> _authenticated_connection_set;

         void handle_request( const fc::http::request& r, const fc::http::server::response& s )
         {
             s.add_header( "Connection", "close" );
             ilog( "handle request ${r}", ("r",r.path) );

             try {
                auto auth_value = r.get_header( "Authorization" );
                std::string username, password;
                if( auth_value.size() )
                {
                   auto auth_b64    = auth_value.substr( 6 );
                   auto userpass    = fc::base64_decode( auth_b64 );
                   auto split       = userpass.find( ':' );
                   username    = userpass.substr( 0, split );
                   password    = userpass.substr( split + 1 );
                }
                ilog( "username: '${u}' password: '${p}' config ${c}", ("u",username)("p",password)("c",_config) );
                if( _config.rpc_user     != username ||
                    _config.rpc_password != password )
                {
                   s.add_header( "WWW-Authenticate", "Basic realm=\"bts wallet\"" );
                   std::string message = "Unauthorized";
                   s.set_length( message.size() );
                   s.set_status( fc::http::reply::NotAuthorized );
                   s.write( message.c_str(), message.size() );
                }

                auto dotpos = r.path.find( ".." );
                FC_ASSERT( dotpos == std::string::npos );
                auto filename = _config.htdocs / r.path.substr(1,std::string::npos);
                if( r.path == "/" )
                {
                    filename = _config.htdocs / "index.html";
                }
                if( fc::exists( filename ) )
                {
                    FC_ASSERT( !fc::is_directory( filename ) );<commit_msg>make rpc basic auth optional<commit_after>#include <bts/rpc/rpc_server.hpp>
#include <fc/reflect/variant.hpp>
#include <fc/network/tcp_socket.hpp>
#include <fc/rpc/json_connection.hpp>
#include <fc/io/json.hpp>
#include <fc/thread/thread.hpp>
#include <fc/network/http/server.hpp>
#include <fc/interprocess/file_mapping.hpp>
#include <boost/bind.hpp>
#include <boost/algorithm/string/join.hpp>
#include <sstream>
#include <limits>


namespace bts { namespace rpc { 

  namespace detail 
  {
    class rpc_server_impl
    {
       public:
         rpc_server::config  _config;
         client_ptr          _client;
         fc::http::server    _httpd;
         fc::tcp_server      _tcp_serv;
         fc::future<void>    _accept_loop_complete;
         rpc_server*         _self;
         std::string         _username;

         typedef std::map<std::string, rpc_server::method_data> method_map_type;
         method_map_type _method_map;

         /** the set of connections that have successfully logged in */
         std::unordered_set<fc::rpc::json_connection*> _authenticated_connection_set;

         void handle_request( const fc::http::request& r, const fc::http::server::response& s )
         {
             s.add_header( "Connection", "close" );
             ilog( "handle request ${r}", ("r",r.path) );

             try {
                if( _config.rpc_user.size() )
                {
                   auto auth_value = r.get_header( "Authorization" );
                   std::string username, password;
                   if( auth_value.size() )
                   {
                      auto auth_b64    = auth_value.substr( 6 );
                      auto userpass    = fc::base64_decode( auth_b64 );
                      auto split       = userpass.find( ':' );
                      username    = userpass.substr( 0, split );
                      password    = <PASSWORD>( split + 1 );
                   }
                   ilog( "username: '${u}' password: '${p}' config ${c}", ("u",username)("p",password)("c",_config) );
                   if( _config.rpc_user     != username ||
                       _config.rpc_password != password )
                   {
                      s.add_header( "WWW-Authenticate", "Basic realm=\"bts wallet\"" );
                      std::string message = "Unauthorized";
                      s.set_length( message.size() );
                      s.set_status( fc::http::reply::NotAuthorized );
                      s.write( message.c_str(), message.size() );
                   }
                }

                auto dotpos = r.path.find( ".." );
                FC_ASSERT( dotpos == std::string::npos );
                auto filename = _config.htdocs / r.path.substr(1,std::string::npos);
                if( r.path == "/" )
                {
                    filename = _config.htdocs / "index.html";
                }
                if( fc::exists( filename ) )
                {
                    FC_ASSERT( !fc::is_directory( filename ) );
{'fraction_non_alphanumeric': 0.12852292020373515, 'fraction_numerical': 0.003395585738539898, 'mean_word_length': 1.6595936794582393, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1180820', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1644, 'n_words': 322}