Skip to content

Commit

Permalink
A few adjustments in the HTTP request.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Nov 30, 2024
1 parent ca0d80d commit b9037e3
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 15 deletions.
7 changes: 7 additions & 0 deletions include/eepp/network/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ class EE_API Http : NonCopyable {
** @param body Content of the body */
void setBody( const std::string& body );

/** @see setBody */
void setBody( std::string&& body );

/** @see setBody */
void appendBody( const std::string& body );
void appendBody( std::string&& body );

/** @return The request Uri */
const std::string& getUri() const;

Expand Down
6 changes: 3 additions & 3 deletions include/eepp/network/uri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ class EE_API URI {
/** URI-decodes the given string by replacing percent-encoded
* characters with the actual character. The decoded string
* is appended to decodedStr. */
static void decode( const std::string& str, std::string& decodedStr );
static void decode( const std::string_view& str, std::string& decodedStr );

/** URI encodes the string. */
static std::string encode( const std::string& str );
static std::string encode( const std::string_view& str );

/** URI decodes the string. */
static std::string decode( const std::string& str );
static std::string decode( const std::string_view& str );

protected:
/** @returns true if both uri's are equivalent. */
Expand Down
20 changes: 18 additions & 2 deletions src/eepp/network/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ void Http::Request::setBody( const std::string& body ) {
mBody = body;
}

void Http::Request::setBody( std::string&& body ) {
mBody = std::move( body );
}

void Http::Request::appendBody( const std::string& body ) {
mBody.append( body );
}

void Http::Request::appendBody( std::string&& body ) {
mBody.append( std::move( body ) );
}

const std::string& Http::Request::getUri() const {
return mUri;
}
Expand Down Expand Up @@ -265,9 +277,12 @@ const std::string& Http::Request::getField( const std::string& field ) const {
}

URI Http::getEnvProxyURI() {
char* http_proxy = getenv( "http_proxy" );
const char* http_proxy = getenv( "http_proxy" );
URI proxy;

if ( NULL == http_proxy )
http_proxy = getenv( "HTTP_PROXY" );

if ( NULL != http_proxy ) {
std::string httpProxy;
httpProxy = std::string( http_proxy );
Expand Down Expand Up @@ -1192,10 +1207,11 @@ Http::Request Http::prepareFields( const Http::Request& request ) {
if ( !toSend.hasField( "Accept" ) )
toSend.setField( "Accept", "*/*" );

if ( !toSend.hasField( "Host" ) )
if ( !toSend.hasField( "Host" ) ) {
toSend.setField(
"Host",
mHostName + ( mPort != 80 && mPort != 443 ? ":" + String::toString( mPort ) : "" ) );
}

if ( !toSend.hasField( "Content-Length" ) && toSend.mBody.size() > 0 ) {
std::ostringstream out;
Expand Down
12 changes: 6 additions & 6 deletions src/eepp/network/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,9 @@ void URI::encode( const std::string& str, const std::string& reserved, std::stri
}
}

void URI::decode( const std::string& str, std::string& decodedStr ) {
std::string::const_iterator it = str.begin();
std::string::const_iterator end = str.end();
void URI::decode( const std::string_view& str, std::string& decodedStr ) {
auto it = str.begin();
auto end = str.end();
while ( it != end ) {
char c = *it++;
if ( c == '%' ) {
Expand Down Expand Up @@ -497,9 +497,9 @@ void URI::decode( const std::string& str, std::string& decodedStr ) {
}
}

std::string URI::encode( const std::string& str ) {
std::string URI::encode( const std::string_view& str ) {
std::string uri;
for ( std::string::const_iterator it = str.begin(); it != str.end(); ++it ) {
for ( auto it = str.begin(); it != str.end(); ++it ) {
char c = *it;
if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c >= '0' && c <= '9' ) ||
c == '-' || c == '_' || c == '.' || c == '~' ) {
Expand All @@ -512,7 +512,7 @@ std::string URI::encode( const std::string& str ) {
return uri;
}

std::string URI::decode( const std::string& str ) {
std::string URI::decode( const std::string_view& str ) {
std::string uri;
decode( str, uri );
return uri;
Expand Down
69 changes: 65 additions & 4 deletions src/examples/http_request/http_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ EE_MAIN_FUNC int main( int argc, char* argv[] ) {
args::Flag resume( parser, "continue", "Resume getting a partially-downloaded file",
{ 'c', "continue" } );
args::Flag compressed( parser, "compressed", "Request compressed response", { "compressed" } );
args::ValueFlag<std::string> postData( parser, "data", "HTTP POST data",
{ 'd', "data", "data-raw" } );
args::ValueFlagList<std::string> postData( parser, "data", "HTTP POST data",
{ 'd', "data", "data-raw" } );
args::ValueFlagList<std::string> postDataRaw( parser, "data-raw", "HTTP POST data RAW",
{ "data-raw" } );
args::ValueFlagList<std::string> postUrlEncode(
parser, "data-urlencode",
"Post data, similar to the other -d, --data options with the exception that this performs "
"URL-encoding.",
{ "data-urlencode" } );
args::ValueFlagList<std::string> multipartData(
parser, "multipart-data", "Specify multipart MIME data", { 'F', "form" } );
args::ValueFlagList<std::string> headers( parser, "header", "Pass custom header(s) to server",
Expand Down Expand Up @@ -105,10 +112,64 @@ EE_MAIN_FUNC int main( int argc, char* argv[] ) {
}
}

// Set the post data / body
// Set the post data
if ( postData ) {
std::string body;
for ( auto& data : args::get( postData ) ) {
if ( !data.empty() && data[0] == '@' ) {
std::string path( data.substr( 1 ) );
String::trimInPlace( path, '"' );
if ( FileSystem::fileExists( path ) ) {
std::string readData;
FileSystem::fileGet( path, readData );
body += std::move( readData );
}
} else {
String::trimInPlace( data, '"' );
body += std::move( data );
}
body += "&";
}
if ( !body.empty() )
body.pop_back();
request.setMethod( Http::Request::Method::Post );
request.setBody( std::move( body ) );
}

// Set the post data URL encoded
if ( postUrlEncode ) {
std::string body;
for ( auto& data : args::get( postUrlEncode ) ) {
String::trimInPlace( data, '"' );
auto keyLen = data.find_first_of( '=' );
if ( keyLen != std::string::npos ) {
body += data.substr( 0, keyLen );
body += '=';
body += URI::encode( std::string_view{ data }.substr( keyLen + 1 ) );
} else {
body += std::move( data );
body += '=';
}
body += "&";
}
if ( !body.empty() )
body.pop_back();
request.setMethod( Http::Request::Method::Post );
request.setBody( std::move( body ) );
}

// Set the post raw data
if ( postDataRaw ) {
std::string body;
for ( auto& data : args::get( postDataRaw ) ) {
String::trimInPlace( data, '"' );
body += std::move( data );
body += "&";
}
if ( !body.empty() )
body.pop_back();
request.setMethod( Http::Request::Method::Post );
request.setBody( postData.Get() );
request.setBody( std::move( body ) );
}

// Set the multipart data
Expand Down

0 comments on commit b9037e3

Please sign in to comment.