simple HTTP post support (urlencode parameters)

This commit is contained in:
Benjamin Sergeant 2019-02-25 15:55:38 -08:00
parent c04bc3cdfc
commit 3bcd6f97a6
10 changed files with 164 additions and 47 deletions

View File

@ -1 +0,0 @@
build

View File

@ -1,22 +0,0 @@
#
# Author: Benjamin Sergeant
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
#
cmake_minimum_required (VERSION 3.4.1)
project (http_client)
set (CMAKE_CXX_STANDARD 14)
option(USE_TLS "Add TLS support" ON)
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
add_executable(http_client http_client.cpp)
if (APPLE AND USE_TLS)
target_link_libraries(http_client "-framework foundation" "-framework security")
endif()
target_link_libraries(http_client ixwebsocket)
install(TARGETS http_client DESTINATION bin)

View File

@ -18,6 +18,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <iomanip>
#include <vector> #include <vector>
namespace ix namespace ix
@ -32,7 +33,10 @@ namespace ix
} }
HttpResponse HttpClient::get(const std::string& url, HttpResponse HttpClient::request(
const std::string& url,
const std::string& verb,
const HttpParameters& httpParameters,
bool verbose) bool verbose)
{ {
int code = 0; int code = 0;
@ -68,17 +72,47 @@ namespace ix
return std::make_tuple(code, headers, payload, errorMsg); return std::make_tuple(code, headers, payload, errorMsg);
} }
// FIXME: missing url parsing std::string body;
if (verb == "POST")
{
std::stringstream ss;
size_t count = httpParameters.size();
size_t i = 0;
for (auto&& it : httpParameters)
{
ss << urlEncode(it.first)
<< "="
<< urlEncode(it.second);
if (i++ < (count-1))
{
ss << "&";
}
}
body = ss.str();
}
std::stringstream ss; std::stringstream ss;
ss << "GET " << path << " HTTP/1.1\r\n"; ss << verb << " " << path << " HTTP/1.1\r\n";
ss << "Host: " << host << "\r\n"; ss << "Host: " << host << "\r\n";
ss << "User-Agent: ixwebsocket/1.0.0" << "\r\n"; ss << "User-Agent: ixwebsocket/1.0.0" << "\r\n";
ss << "Accept: */*" << "\r\n"; ss << "Accept: */*" << "\r\n";
if (verb == "POST")
{
ss << "Content-Length: " << body.size() << "\r\n";
ss << "Content-Type: application/x-www-form-urlencoded" << "\r\n";
ss << "\r\n"; ss << "\r\n";
ss << body;
}
else
{
ss << "\r\n";
}
std::string request(ss.str()); std::string request(ss.str());
int timeoutSecs = 3; int timeoutSecs = 10;
std::string errMsg; std::string errMsg;
static std::atomic<bool> requestInitCancellation(false); static std::atomic<bool> requestInitCancellation(false);
@ -95,8 +129,12 @@ namespace ix
if (verbose) if (verbose)
{ {
std::cout << "Sending request: " << request std::cout << "Sending " << verb << " request "
<< "to " << host << ":" << port << "to " << host << ":" << port << std::endl
<< "request size: " << request.size() << " bytes"
<< "=============" << std::endl
<< request
<< "=============" << std::endl
<< std::endl; << std::endl;
} }
@ -174,6 +212,48 @@ namespace ix
return std::make_tuple(code, headers, payload, ""); return std::make_tuple(code, headers, payload, "");
} }
HttpResponse HttpClient::get(
const std::string& url,
bool verbose)
{
return request(url, "GET", HttpParameters(), verbose);
}
HttpResponse HttpClient::post(
const std::string& url,
const HttpParameters& httpParameters,
bool verbose)
{
return request(url, "POST", httpParameters, verbose);
}
std::string HttpClient::urlEncode(const std::string& value)
{
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (std::string::const_iterator i = value.begin(), n = value.end();
i != n; ++i)
{
std::string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
{
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << std::uppercase;
escaped << '%' << std::setw(2) << int((unsigned char) c);
escaped << std::nouppercase;
}
return escaped.str();
}
} }
#if 0 #if 0

View File

@ -12,6 +12,7 @@
#include <atomic> #include <atomic>
#include <tuple> #include <tuple>
#include <memory> #include <memory>
#include <map>
#include "IXSocket.h" #include "IXSocket.h"
#include "IXWebSocketHttpHeaders.h" #include "IXWebSocketHttpHeaders.h"
@ -19,6 +20,7 @@
namespace ix namespace ix
{ {
using HttpResponse = std::tuple<int, WebSocketHttpHeaders, std::string, std::string>; using HttpResponse = std::tuple<int, WebSocketHttpHeaders, std::string, std::string>;
using HttpParameters = std::map<std::string, std::string>;
class HttpClient { class HttpClient {
public: public:
@ -27,8 +29,18 @@ namespace ix
// Static methods ? // Static methods ?
HttpResponse get(const std::string& url, bool verbose); HttpResponse get(const std::string& url, bool verbose);
HttpResponse post(const std::string& url,
const HttpParameters& httpParameters,
bool verbose);
private: private:
HttpResponse request(const std::string& url,
const std::string& verb,
const HttpParameters& httpParameters,
bool verbose);
std::string urlEncode(const std::string& value);
std::shared_ptr<Socket> _socket; std::shared_ptr<Socket> _socket;
}; };
} }

View File

@ -17,7 +17,8 @@
namespace ix namespace ix
{ {
int ws_http_client_main(const std::string& url); int ws_http_client_main(const std::string& url,
const std::string& data);
int ws_ping_pong_main(const std::string& url); int ws_ping_pong_main(const std::string& url);
@ -47,11 +48,13 @@ int main(int argc, char** argv)
std::string url("ws://127.0.0.1:8080"); std::string url("ws://127.0.0.1:8080");
std::string path; std::string path;
std::string user; std::string user;
std::string data;
int port = 8080; int port = 8080;
CLI::App* sendApp = app.add_subcommand("send", "Send a file"); CLI::App* sendApp = app.add_subcommand("send", "Send a file");
sendApp->add_option("url", url, "Connection url")->required(); sendApp->add_option("url", url, "Connection url")->required();
sendApp->add_option("path", path, "Path to the file to send")->required(); sendApp->add_option("path", path, "Path to the file to send")
->required()->check(CLI::ExistingPath);
CLI::App* receiveApp = app.add_subcommand("receive", "Receive a file"); CLI::App* receiveApp = app.add_subcommand("receive", "Receive a file");
receiveApp->add_option("url", url, "Connection url")->required(); receiveApp->add_option("url", url, "Connection url")->required();
@ -77,6 +80,8 @@ int main(int argc, char** argv)
CLI::App* httpClientApp = app.add_subcommand("http_client", "HTTP Client"); CLI::App* httpClientApp = app.add_subcommand("http_client", "HTTP Client");
httpClientApp->add_option("url", url, "Connection url")->required(); httpClientApp->add_option("url", url, "Connection url")->required();
httpClientApp->add_option("-d", data, "Form data")->join();
httpClientApp->add_option("-F", data, "Form data")->join();
CLI11_PARSE(app, argc, argv); CLI11_PARSE(app, argc, argv);
@ -117,7 +122,8 @@ int main(int argc, char** argv)
} }
else if (app.got_subcommand("http_client")) else if (app.got_subcommand("http_client"))
{ {
return ix::ws_http_client_main(url); std::cout << "data: " << data << std::endl;
return ix::ws_http_client_main(url, data);
} }
return 1; return 1;

View File

@ -10,11 +10,51 @@
namespace ix namespace ix
{ {
void ws_http_client_main(const std::string& url) //
// Useful endpoint to test HTTP post
// https://postman-echo.com/post
//
HttpParameters parsePostParameters(const std::string& data)
{ {
HttpParameters httpParameters;
// Split by ;
std::string token;
std::stringstream tokenStream(data);
while (std::getline(tokenStream, token))
{
std::size_t pos = token.rfind('=');
// Bail out if last '.' is found
if (pos == std::string::npos) continue;
auto key = token.substr(0, pos);
auto val = token.substr(pos+1);
std::cout << key << ": " << val << std::endl;
httpParameters[key] = val;
}
return httpParameters;
}
int ws_http_client_main(const std::string& url,
const std::string& data)
{
HttpParameters httpParameters = parsePostParameters(data);
HttpClient httpClient; HttpClient httpClient;
bool verbose = true; bool verbose = true;
auto out = httpClient.get(url, verbose); HttpResponse out;
if (data.empty())
{
out = httpClient.get(url, verbose);
}
else
{
out = httpClient.post(url, httpParameters, verbose);
}
auto errorCode = std::get<0>(out); auto errorCode = std::get<0>(out);
auto headers = std::get<1>(out); auto headers = std::get<1>(out);
auto payload = std::get<2>(out); auto payload = std::get<2>(out);
@ -32,5 +72,7 @@ namespace ix
} }
std::cout << "payload: " << payload << std::endl; std::cout << "payload: " << payload << std::endl;
return 0;
} }
} }