(http client + server + ws) Add support for uploading files with ws -F foo=@filename, new -D http server option to debug incoming client requests, internal api changed for http POST, PUT and PATCH to supply an HttpFormDataParameters

This commit is contained in:
Benjamin Sergeant
2020-10-08 12:43:18 -07:00
parent 032ed9af9c
commit fa0408e70b
8 changed files with 183 additions and 43 deletions

View File

@ -555,9 +555,21 @@ namespace ix
HttpResponsePtr HttpClient::post(const std::string& url,
const HttpParameters& httpParameters,
const HttpFormDataParameters& httpFormDataParameters,
HttpRequestArgsPtr args)
{
return request(url, kPost, serializeHttpParameters(httpParameters), args);
if (httpFormDataParameters.empty())
{
return request(url, kPost, serializeHttpParameters(httpParameters), args);
}
else
{
std::string multipartBoundary = generateMultipartBoundary();
args->multipartBoundary = multipartBoundary;
std::string body = serializeHttpFormDataParameters(
multipartBoundary, httpFormDataParameters, httpParameters);
return request(url, kPost, body, args);
}
}
HttpResponsePtr HttpClient::post(const std::string& url,
@ -569,9 +581,21 @@ namespace ix
HttpResponsePtr HttpClient::put(const std::string& url,
const HttpParameters& httpParameters,
const HttpFormDataParameters& httpFormDataParameters,
HttpRequestArgsPtr args)
{
return request(url, kPut, serializeHttpParameters(httpParameters), args);
if (httpFormDataParameters.empty())
{
return request(url, kPut, serializeHttpParameters(httpParameters), args);
}
else
{
std::string multipartBoundary = generateMultipartBoundary();
args->multipartBoundary = multipartBoundary;
std::string body = serializeHttpFormDataParameters(
multipartBoundary, httpFormDataParameters, httpParameters);
return request(url, kPut, body, args);
}
}
HttpResponsePtr HttpClient::put(const std::string& url,
@ -583,9 +607,21 @@ namespace ix
HttpResponsePtr HttpClient::patch(const std::string& url,
const HttpParameters& httpParameters,
const HttpFormDataParameters& httpFormDataParameters,
HttpRequestArgsPtr args)
{
return request(url, kPatch, serializeHttpParameters(httpParameters), args);
if (httpFormDataParameters.empty())
{
return request(url, kPatch, serializeHttpParameters(httpParameters), args);
}
else
{
std::string multipartBoundary = generateMultipartBoundary();
args->multipartBoundary = multipartBoundary;
std::string body = serializeHttpFormDataParameters(
multipartBoundary, httpFormDataParameters, httpParameters);
return request(url, kPatch, body, args);
}
}
HttpResponsePtr HttpClient::patch(const std::string& url,

View File

@ -34,6 +34,7 @@ namespace ix
HttpResponsePtr post(const std::string& url,
const HttpParameters& httpParameters,
const HttpFormDataParameters& httpFormDataParameters,
HttpRequestArgsPtr args);
HttpResponsePtr post(const std::string& url,
const std::string& body,
@ -41,6 +42,7 @@ namespace ix
HttpResponsePtr put(const std::string& url,
const HttpParameters& httpParameters,
const HttpFormDataParameters& httpFormDataParameters,
HttpRequestArgsPtr args);
HttpResponsePtr put(const std::string& url,
const std::string& body,
@ -48,6 +50,7 @@ namespace ix
HttpResponsePtr patch(const std::string& url,
const HttpParameters& httpParameters,
const HttpFormDataParameters& httpFormDataParameters,
HttpRequestArgsPtr args);
HttpResponsePtr patch(const std::string& url,
const std::string& body,

View File

@ -190,4 +190,40 @@ namespace ix
301, "OK", HttpErrorCode::Ok, headers, std::string());
});
}
//
// Display the client parameter and body on the console
//
void HttpServer::makeDebugServer()
{
setOnConnectionCallback(
[this](HttpRequestPtr request,
std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr {
WebSocketHttpHeaders headers;
headers["Server"] = userAgent();
// Log request
std::stringstream ss;
ss << connectionState->getRemoteIp() << ":" << connectionState->getRemotePort()
<< " " << request->method << " " << request->headers["User-Agent"] << " "
<< request->uri;
logInfo(ss.str());
logInfo("== Headers == ");
for (auto&& it : request->headers)
{
std::ostringstream oss;
oss << it.first << ": " << it.second;
logInfo(oss.str());
}
logInfo("");
logInfo("== Body == ");
logInfo(request->body);
logInfo("");
return std::make_shared<HttpResponse>(
200, "OK", HttpErrorCode::Ok, headers, std::string("OK"));
});
}
} // namespace ix

View File

@ -38,6 +38,8 @@ namespace ix
void makeRedirectServer(const std::string& redirectUrl);
void makeDebugServer();
private:
// Member variables
OnConnectionCallback _onConnectionCallback;

View File

@ -6,4 +6,4 @@
#pragma once
#define IX_WEBSOCKET_VERSION "10.4.9"
#define IX_WEBSOCKET_VERSION "10.5.0"