2016-01-28 14:52:40 +01:00
|
|
|
/** @file
|
|
|
|
|
*
|
|
|
|
|
* @par History
|
|
|
|
|
*/
|
2016-02-11 22:54:43 +01:00
|
|
|
|
|
|
|
|
#include "httpmod.h"
|
|
|
|
|
#include "../../bscript/berror.h"
|
2018-01-29 21:55:48 +01:00
|
|
|
#include "../../bscript/impstr.h"
|
2016-02-11 22:54:43 +01:00
|
|
|
#include "../../clib/logfacility.h"
|
2019-03-04 21:41:27 +01:00
|
|
|
#include "../../clib/network/wnsckt.h"
|
2016-02-11 22:54:43 +01:00
|
|
|
#include "../../plib/systemstate.h"
|
2018-01-29 21:55:48 +01:00
|
|
|
#include "../uoexec.h"
|
|
|
|
|
|
2019-11-10 22:04:08 +01:00
|
|
|
#include <module_defs/http.h>
|
2016-02-11 22:54:43 +01:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
namespace Pol
|
|
|
|
|
{
|
|
|
|
|
namespace Core
|
|
|
|
|
{
|
|
|
|
|
std::string http_decodestr( const std::string& s );
|
|
|
|
|
}
|
|
|
|
|
namespace Module
|
|
|
|
|
{
|
|
|
|
|
using namespace Bscript;
|
2016-12-14 21:50:23 +01:00
|
|
|
|
2019-02-23 21:30:20 +01:00
|
|
|
HttpExecutorModule::HttpExecutorModule( Bscript::Executor& exec, Clib::Socket&& isck )
|
2020-02-08 09:21:35 +01:00
|
|
|
: Bscript::TmplExecutorModule<HttpExecutorModule, Core::PolModule>( exec ),
|
2019-02-23 21:30:20 +01:00
|
|
|
sck_( std::move( isck ) ),
|
2020-02-08 09:21:35 +01:00
|
|
|
continuing_offset( 0 )
|
2016-12-14 21:50:23 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
BObjectImp* HttpExecutorModule::mf_WriteHtml()
|
|
|
|
|
{
|
|
|
|
|
const String* str;
|
|
|
|
|
if ( !sck_.connected() )
|
|
|
|
|
{
|
|
|
|
|
exec.seterror( true );
|
|
|
|
|
return new BError( "Socket is disconnected" );
|
2016-02-11 22:54:43 +01:00
|
|
|
}
|
2016-02-19 19:26:35 +01:00
|
|
|
if ( getStringParam( 0, str ) )
|
|
|
|
|
{
|
|
|
|
|
// TODO: some tricky stuff so if the socket blocks, the script goes to
|
|
|
|
|
// sleep for a bit and sends the rest later
|
|
|
|
|
|
|
|
|
|
unsigned nsent;
|
|
|
|
|
const std::string& s = str->value();
|
|
|
|
|
bool res =
|
|
|
|
|
sck_.send_nowait( (void*)( s.c_str() + continuing_offset ),
|
|
|
|
|
static_cast<unsigned int>( s.length() - continuing_offset ), &nsent );
|
|
|
|
|
if ( res )
|
|
|
|
|
{
|
|
|
|
|
continuing_offset = 0;
|
|
|
|
|
// we don't really care if this works or not, terribly.
|
|
|
|
|
sck_.send_nowait( "\n", 1, &nsent );
|
|
|
|
|
return new BLong( 1 );
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-02-11 22:54:43 +01:00
|
|
|
{
|
2016-02-19 19:26:35 +01:00
|
|
|
continuing_offset += nsent;
|
2020-02-08 09:21:35 +01:00
|
|
|
auto& uoex = uoexec();
|
|
|
|
|
uoex.SleepForMs( 500 );
|
|
|
|
|
--uoex.PC;
|
|
|
|
|
return uoex.fparams[0]->impptr();
|
2016-02-19 19:26:35 +01:00
|
|
|
}
|
2016-02-11 22:54:43 +01:00
|
|
|
}
|
2016-02-19 19:26:35 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new BError( "Invalid parameter type" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 22:54:43 +01:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
BObjectImp* HttpExecutorModule::mf_WriteHtmlRaw()
|
|
|
|
|
{
|
|
|
|
|
const String* str;
|
|
|
|
|
if ( !sck_.connected() )
|
|
|
|
|
{
|
|
|
|
|
exec.seterror( true );
|
|
|
|
|
return new BError( "Socket is disconnected" );
|
|
|
|
|
}
|
|
|
|
|
exec.makeString( 0 );
|
|
|
|
|
if ( getStringParam( 0, str ) )
|
|
|
|
|
{
|
|
|
|
|
// TODO: some tricky stuff so if the socket blocks, the script goes to
|
|
|
|
|
// sleep for a bit and sends the rest later
|
|
|
|
|
|
|
|
|
|
unsigned nsent;
|
|
|
|
|
const std::string& s = str->value();
|
|
|
|
|
bool res =
|
|
|
|
|
sck_.send_nowait( (void*)( s.c_str() + continuing_offset ),
|
|
|
|
|
static_cast<unsigned int>( s.length() - continuing_offset ), &nsent );
|
|
|
|
|
if ( res )
|
|
|
|
|
{
|
|
|
|
|
continuing_offset = 0;
|
|
|
|
|
return new BLong( 1 );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
continuing_offset += nsent;
|
2020-02-08 09:21:35 +01:00
|
|
|
auto& uoex = uoexec();
|
|
|
|
|
uoex.SleepForMs( 500 );
|
|
|
|
|
--uoex.PC;
|
|
|
|
|
return uoex.fparams[0]->impptr();
|
2016-02-19 19:26:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new BError( "Invalid parameter type" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 22:54:43 +01:00
|
|
|
|
|
|
|
|
#if 0
|
2018-04-03 04:13:03 +02:00
|
|
|
BObjectImp* HttpExecutorModule::mf_WriteHtml()
|
|
|
|
|
{
|
|
|
|
|
const String* str;
|
|
|
|
|
if (getStringParam( 0, str ))
|
|
|
|
|
{
|
|
|
|
|
// TODO: some tricky stuff so if the socket blocks, the script goes to
|
|
|
|
|
// sleep for a bit and sends the rest later
|
|
|
|
|
http_writeline( sck_, str->value() );
|
|
|
|
|
return new BLong(1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new BError( "Invalid parameter type" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 22:54:43 +01:00
|
|
|
#endif
|
|
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
BObjectImp* HttpExecutorModule::mf_QueryParam()
|
|
|
|
|
{
|
|
|
|
|
const String* str;
|
|
|
|
|
if ( getStringParam( 0, str ) )
|
|
|
|
|
{
|
|
|
|
|
QueryParamMap::iterator itr = params_.find( str->data() );
|
|
|
|
|
if ( itr != params_.end() )
|
|
|
|
|
return new String( ( *itr ).second );
|
|
|
|
|
else
|
|
|
|
|
return new BLong( 0 );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new BError( "Invalid parameter type" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-11 22:54:43 +01:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
BObjectImp* HttpExecutorModule::mf_QueryIP()
|
|
|
|
|
{
|
|
|
|
|
return new String( query_ip_ );
|
|
|
|
|
}
|
2016-02-11 22:54:43 +01:00
|
|
|
|
|
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
// query_string is everything after the '?'
|
|
|
|
|
void HttpExecutorModule::read_query_string( const std::string& query_string )
|
|
|
|
|
{
|
|
|
|
|
if ( !query_string.empty() )
|
|
|
|
|
{
|
|
|
|
|
std::string::size_type brk;
|
|
|
|
|
std::string::size_type start = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
brk = query_string.find( '&', start );
|
|
|
|
|
std::string param =
|
|
|
|
|
query_string.substr( start, ( brk == std::string::npos ) ? brk : brk - start );
|
|
|
|
|
|
|
|
|
|
std::string name, value;
|
|
|
|
|
std::string::size_type eq = param.find( '=' );
|
|
|
|
|
if ( eq != std::string::npos )
|
|
|
|
|
{
|
|
|
|
|
name = param.substr( 0, eq );
|
|
|
|
|
value = Core::http_decodestr( param.substr( eq + 1 ) );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
name = param;
|
|
|
|
|
value = "";
|
|
|
|
|
}
|
|
|
|
|
params_[name.c_str()] = value;
|
|
|
|
|
|
|
|
|
|
if ( Plib::systemstate.config.web_server_debug )
|
|
|
|
|
INFO_PRINT << "http-param: '" << param << "', '" << Core::http_decodestr( param ) << "'\n";
|
|
|
|
|
|
|
|
|
|
start = brk + 1;
|
|
|
|
|
} while ( brk != std::string::npos );
|
2016-02-11 22:54:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-19 19:26:35 +01:00
|
|
|
|
|
|
|
|
void HttpExecutorModule::read_query_ip()
|
|
|
|
|
{
|
|
|
|
|
query_ip_ = sck_.getpeername();
|
|
|
|
|
}
|
2019-02-01 23:18:43 +01:00
|
|
|
|
|
|
|
|
size_t HttpExecutorModule::sizeEstimate() const
|
|
|
|
|
{
|
|
|
|
|
size_t size = sizeof( *this );
|
|
|
|
|
for ( const auto& v : params_ )
|
|
|
|
|
size += v.first.capacity() + v.second.capacity() + ( sizeof( void* ) * 3 + 1 ) / 2;
|
|
|
|
|
return size;
|
2016-02-19 19:26:35 +01:00
|
|
|
}
|
2019-02-01 23:18:43 +01:00
|
|
|
} // namespace Module
|
|
|
|
|
} // namespace Pol
|