Skip to content

Commit

Permalink
临时简化 http 请求报文的解析,避免 std::regex(g++/clang++)导致崩溃
Browse files Browse the repository at this point in the history
Signed-off-by: solym <[email protected]>
  • Loading branch information
sotex committed Apr 15, 2021
1 parent c91642e commit 79ae9a2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
47 changes: 35 additions & 12 deletions source/corvusoft/restbed/detail/service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <stdexcept>
#include <algorithm>
#include <functional>
#include <vector>

//Project Includes
#include "corvusoft/restbed/uri.hpp"
Expand Down Expand Up @@ -64,6 +65,8 @@ using std::current_exception;
using std::rethrow_exception;
using std::chrono::steady_clock;
using std::regex_constants::icase;
using std::sregex_token_iterator;
using std::vector;

//Project Namespaces

Expand Down Expand Up @@ -698,23 +701,28 @@ namespace restbed

const map< string, string > ServiceImpl::parse_request_line( istream& stream )
{
smatch matches;
static const regex pattern( "^([0-9a-zA-Z]*) ([a-zA-Z0-9:@_~!,;=#%&'\\-\\.\\/\\?\\$\\(\\)\\*\\+]+) (HTTP\\/[0-9]\\.[0-9])\\s*$" );
static const regex re_delimiter{" +"};
string data = "";
getline( stream, data );

if ( not regex_match( data, matches, pattern ) or matches.size( ) not_eq 4 )
auto matches = vector< string > {
sregex_token_iterator( data.begin() , data.end() , re_delimiter , -1 ),
sregex_token_iterator() };

if ( matches.size( ) != 3 || matches[2].find_first_of( '/' ) == string::npos )
{
throw runtime_error( "Your client has issued a malformed or illegal request status line. That’s all we know." );
}

const string protocol = matches[ 3 ].str( );
const auto delimiter = protocol.find_first_of( "/" );


string method = move( matches[ 0 ] );
string path = move( matches[ 1 ] );
string protocol = move( matches[ 2 ] );
const auto delimiter = protocol.find( '/' );

return map< string, string >
{
{ "path", matches[ 2 ].str( ) },
{ "method", matches[ 1 ].str( ) },
{ "path", path },
{ "method", method },
{ "version", protocol.substr( delimiter + 1 ) },
{ "protocol", protocol.substr( 0, delimiter ) }
};
Expand Down Expand Up @@ -754,13 +762,28 @@ namespace restbed
try
{
const auto items = parse_request_line( stream );
const auto uri = Uri::parse( "http://localhost" + items.at( "path" ) );

// const auto uri = Uri::parse( "http://localhost" + items.at( "path" ) );
const string uri = move( items.at( "path" ) );
const auto delimiter = uri.find_first_of( "?#" );
multimap< string, string > parameters;
if ( delimiter != string::npos ) {
auto query = String::split( uri.substr( delimiter+1 ), '&' );
for ( auto parameter : query )
{
auto index = parameter.find_first_of( '=' );
auto name = Uri::decode_parameter( parameter.substr( 0, index ) );
auto value = Uri::decode_parameter( parameter.substr( index + 1, parameter.length( ) ) );

parameters.insert( make_pair( name, value ) );
}
}

session->m_pimpl->m_request->m_pimpl->m_body.clear( );
session->m_pimpl->m_request->m_pimpl->m_path = Uri::decode( uri.get_path( ) );
session->m_pimpl->m_request->m_pimpl->m_path = Uri::decode( uri.substr( 0 , delimiter ) );
session->m_pimpl->m_request->m_pimpl->m_method = items.at( "method" );
session->m_pimpl->m_request->m_pimpl->m_headers = parse_request_headers( stream );
session->m_pimpl->m_request->m_pimpl->m_query_parameters = uri.get_query_parameters( );
session->m_pimpl->m_request->m_pimpl->m_query_parameters = parameters;

char* locale = strdup( setlocale( LC_NUMERIC, nullptr ) );
setlocale( LC_NUMERIC, "C" );
Expand Down
5 changes: 3 additions & 2 deletions source/corvusoft/restbed/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ namespace restbed

bool Uri::is_valid( const string& value )
{
static const regex pattern( "^[a-zA-Z][a-zA-Z0-9+\\-.]*://(([a-zA-Z0-9\\-._~%!$&'()*+,;=]+)(:([a-zA-Z0-9\\-._~%!$&'()*+,;=]+))?@)?([a-zA-Z0-9\\-._~%!$&'()*+,;=:\\[\\]]*(:[0-9]+)?)?[a-zA-Z0-9\\-._~%!$&'()*+,;=:@/]+(\\?[a-zA-Z0-9\\-._~%!$&'()*+,;=:@/]*)?(#[a-zA-Z0-9\\-._~%!$&'()*+,;=:@/?]*)?$" );
// static const regex pattern( "^[a-zA-Z][a-zA-Z0-9+\\-.]*://(([a-zA-Z0-9\\-._~%!$&'()*+,;=]+)(:([a-zA-Z0-9\\-._~%!$&'()*+,;=]+))?@)?([a-zA-Z0-9\\-._~%!$&'()*+,;=:\\[\\]]*(:[0-9]+)?)?[a-zA-Z0-9\\-._~%!$&'()*+,;=:@/]+(\\?[a-zA-Z0-9\\-._~%!$&'()*+,;=:@/]*)?(#[a-zA-Z0-9\\-._~%!$&'()*+,;=:@/?]*)?$" );

return regex_match( value, pattern );
// return regex_match( value, pattern );
return value.find_first_of("://") != string::npos;
}

Uri Uri::parse( const string& value )
Expand Down

0 comments on commit 79ae9a2

Please sign in to comment.