Skip to content

Commit

Permalink
Add send customize http requests to drogon_ctl
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-peak committed Oct 17, 2024
1 parent 3fce70b commit 22cfca3
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 4 deletions.
148 changes: 144 additions & 4 deletions drogon_ctl/press.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include <memory>
#include <iomanip>
#include <cstdlib>
#include <json/json.h>
#include <fstream>
#include <string>
#include <unordered_map>
#ifndef _WIN32
#include <unistd.h>
#endif
Expand All @@ -33,9 +37,10 @@ std::string press::detail()
" -t num number of threads(default : 1)\n"
" -c num concurrent connections(default : 1)\n"
" -k disable SSL certificate validation(default: enable)\n"
" -f customize http request json file(default: disenable)\n"
" -q no progress indication(default: show)\n\n"
"example: drogon_ctl press -n 10000 -c 100 -t 4 -q "
"http://localhost:8080/index.html\n";
"http://localhost:8080/index.html -f ./http_request.json\n";
}

void outputErrorAndExit(const std::string_view &err)
Expand Down Expand Up @@ -151,6 +156,24 @@ void press::handleCommand(std::vector<std::string> &parameters)
continue;
}
}
else if (param.find("-f") == 0)
{
if (param == "-f")
{
++iter;
if (iter == parameters.end())
{
outputErrorAndExit("No http request json file!");
}
httpRequestJsonFile_ = *iter;
continue;
}
else
{
httpRequestJsonFile_ = param.substr(2);
continue;
}
}
else if (param == "-k")
{
certValidation_ = false;
Expand Down Expand Up @@ -190,6 +213,113 @@ void press::handleCommand(std::vector<std::string> &parameters)
path_ = url_.substr(posOfPath);
}
}

/*
http_request.json
{
"method": "POST",
"header": {
"token": "e2e9d0fe-dd14-4eaf-8ac1-0997730a805d"
},
"body": {
"passwd": "123456",
"account": "10001"
}
}
*/
if (!httpRequestJsonFile_.empty())
{
Json::Value httpRequestJson;
std::ifstream httpRequestFile(httpRequestJsonFile_,
std::ifstream::binary);
if (!httpRequestFile.is_open())
{
outputErrorAndExit(std::string{"No "} + httpRequestJsonFile_);
}
httpRequestFile >> httpRequestJson;
if (!httpRequestJson.isMember("method") ||
!httpRequestJson.isMember("body"))
{
outputErrorAndExit("No contain method or body");
}

std::unordered_map<std::string, std::string> header;
if (httpRequestJson.isMember("header"))
{
auto &jsonValue = httpRequestJson["header"];
for (const auto &key : jsonValue.getMemberNames())
{
if (jsonValue[key].isString())
{
header[key] = jsonValue[key].asString();
}
else
{
header[key] = jsonValue[key].toStyledString();
}
}
}

auto methodStr = httpRequestJson["method"].asString();
std::transform(methodStr.begin(),
methodStr.end(),
methodStr.begin(),
::toupper);

auto toHttpMethod = [&]() -> drogon::HttpMethod {
if (methodStr == "GET")
{
return drogon::HttpMethod::Get;
}
else if (methodStr == "POST")
{
return drogon::HttpMethod::Post;
}
else if (methodStr == "HEAD")
{
return drogon::HttpMethod::Head;
}
else if (methodStr == "PUT")
{
return drogon::HttpMethod::Put;
}
else if (methodStr == "DELETE")
{
return drogon::HttpMethod::Delete;
}
else if (methodStr == "OPTIONS")
{
return drogon::HttpMethod::Options;
}
else if (methodStr == "PATCH")
{
return drogon::HttpMethod::Patch;
}
else
{
outputErrorAndExit("invalid method");
}
return drogon::HttpMethod::Get;
};

Json::FastWriter fastWriter;
std::string body = fastWriter.write(httpRequestJson["body"]);

createHttpRequestFunc_ = [this,
method = toHttpMethod(),
body = std::move(body),
header =
std::move(header)]() -> HttpRequestPtr {
auto request = HttpRequest::newHttpRequest();
request->setPath(path_);
request->setMethod(method);
for (const auto &[field, val] : header)
request->addHeader(field, val);
request->setBody(body);
return request;
};
}

// std::cout << "host=" << host_ << std::endl;
// std::cout << "path=" << path_ << std::endl;
doTesting();
Expand Down Expand Up @@ -232,9 +362,19 @@ void press::sendRequest(const HttpClientPtr &client)
{
return;
}
auto request = HttpRequest::newHttpRequest();
request->setPath(path_);
request->setMethod(Get);

HttpRequestPtr request;
if (createHttpRequestFunc_)
{
request = createHttpRequestFunc_();
}
else
{
request = HttpRequest::newHttpRequest();
request->setPath(path_);
request->setMethod(Get);
}

// std::cout << "send!" << std::endl;
client->sendRequest(
request,
Expand Down
3 changes: 3 additions & 0 deletions drogon_ctl/press.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <drogon/HttpClient.h>
#include <trantor/utils/Date.h>
#include <trantor/net/EventLoopThreadPool.h>
#include <functional>
#include <string>
#include <atomic>
#include <memory>
Expand Down Expand Up @@ -62,6 +63,8 @@ class press : public DrObject<press>, public CommandHandler
size_t numOfThreads_{1};
size_t numOfRequests_{1};
size_t numOfConnections_{1};
std::string httpRequestJsonFile_;
std::function<HttpRequestPtr()> createHttpRequestFunc_;
bool certValidation_{true};
bool processIndication_{true};
std::string url_;
Expand Down

0 comments on commit 22cfca3

Please sign in to comment.