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

                 try {
                    auto pos = r.path.find( "/", 1 );
                    auto first_dir = r.path.substr(1,pos);
                    //ilog( "command: ${command}", ("command",first_dir) );
                    if( first_dir == "pending" )
                    {
                       s.set_status( fc::http::reply::OK );
                       auto pending_json = fc::json::to_string( _pending );
                       s.set_length( pending_json.size() );
                       s.write( pending_json.c_str(), pending_json.size() );
                    }
                    else if( first_dir == "update_record" )
                    {
                       FC_ASSERT( r.body.size() );
                       std::string str(r.body.data(),r.body.size());
                       auto rec = fc::json::from_string( str ).as<signed_name_record>();

                       _self->update_record( rec );

                       s.set_status( fc::http::reply::RecordCreated );
                       s.set_length( 12 );
                       s.write( "Record Created", 12 );
                    }
                    else if( first_dir == "fetch_by_name/" )
                    {
                       auto name = r.path.substr( pos+1, std::string::npos );
                       auto record  = _self->fetch_record( name );
                       s.set_status( fc::http::reply::Found );
                       auto blkjson = fc::json::to_string( record );
                       s.set_length( blkjson.size() );
                       s.write( blkjson.c_str(), blkjson.size() );
                    }
                    else if( first_dir == "fetch_by_key/" )
                    {
                       auto key = r.path.substr( pos+1, std::string::npos );
                       auto record  = _self->fetch_record_by_key( key );
                       s.set_status( fc::http::reply::Found );

                       auto blkjson = fc::json::to_string( record );
                       s.set_length( blkjson.size() );
                       s.write( blkjson.c_str(), blkjson.size() );
                    }
                    else if( first_dir == "store_key/" )
                    {
                       auto name = r.path.substr( pos+1, std::string::npos );
                       std::string str(r.body.data(),r.body.size());
                       auto rec = fc::json::from_string( str ).as<stored_key>();

                       _self->store_key( name, rec );
                       s.set_status( fc::http::reply::RecordCreated );
                       s.set_length( 12 );
                       s.write( "Record Created", 12 );
                    }
                    else if( first_dir == "fetch_key/" )
                    {
                       auto user_name = r.path.substr( pos+1, std::string::npos );
                       auto key_data  = _self->fetch_key(  user_name );
                       s.set_status( fc::http::reply::Found );

                       auto blkjson = fc::json::to_string( key_data );
                       s.set_length( blkjson.size() );
                       s.write( blkjson.c_str(), blkjson.size() );
                    }
                    else if( first_dir == "fetch_block/" )
                    {
                       auto block_num = r.path.substr( pos+1, std::string::npos );
                       auto blk = _self->fetch_block(  fc::to_uint64( block_num ) );
                       s.set_status( fc::http::reply::Found );
                       auto blkjson = fc::json::to_string( blk );
                       s.set_length( blkjson.size() );
                       s.write( blkjson.c_str(), blkjson.size() );
                    }
                    else
                    {
                       auto dotpos = r.path.find( ".." );
                       FC_ASSERT( dotpos == std::string::npos );
                       auto filename = _data_dir / r.path.substr(1,std::string::npos);
                       if( fc::exists( filename ) )
                       {
                          FC_ASSERT( !fc::is_directory( filename ) );
                          auto file_size = fc::file_size( filename );
                          FC_ASSERT( file_size != 0 );

                          fc::file_mapping fm( filename.generic_string().c_str(), fc::read_only );
                          fc::mapped_region mr( fm, fc::read_only, 0, fc::file_size( filename ) );

                          s.set_status( fc::http::reply::OK );
                          s.set_length( file_size );
                          s.write( (const char*)mr.get_address(), mr.get_size() );
                          return;
                       }
                       s.set_status( fc::http::reply::NotFound );
                       s.set_length( 9 );
                       s.write( "Not Found", 9 );
                    }
                 } 
                 catch ( const fc::exception& e )
                 {
                    s.set_status( fc::http::reply::BadRequest );
                    auto msg = e.to_detail_string();
                    s.set_length( msg.size() );
                    if( msg.size() )
                    {
                      s.write( msg.c_str(), msg.size() );
                    }
                 }
             }