http server unittest + refactoring
This commit is contained in:
@ -9,6 +9,7 @@
|
||||
#include "IXNetSystem.h"
|
||||
#include "IXSocketConnect.h"
|
||||
#include "IXSocketFactory.h"
|
||||
#include "IXUserAgent.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
@ -112,6 +113,9 @@ namespace ix
|
||||
uri = "/index.html";
|
||||
}
|
||||
|
||||
WebSocketHttpHeaders headers;
|
||||
headers["Server"] = userAgent();
|
||||
|
||||
std::string path("." + uri);
|
||||
auto res = readAsString(path);
|
||||
bool found = res.first;
|
||||
@ -129,7 +133,6 @@ namespace ix
|
||||
<< request->uri << " " << content.size();
|
||||
logInfo(ss.str());
|
||||
|
||||
WebSocketHttpHeaders headers;
|
||||
// FIXME: check extensions to set the content type
|
||||
// headers["Content-Type"] = "application/octet-stream";
|
||||
headers["Accept-Ranges"] = "none";
|
||||
@ -143,4 +146,36 @@ namespace ix
|
||||
200, "OK", HttpErrorCode::Ok, headers, content);
|
||||
});
|
||||
}
|
||||
|
||||
void HttpServer::makeRedirectServer(const std::string& redirectUrl)
|
||||
{
|
||||
//
|
||||
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections
|
||||
//
|
||||
setOnConnectionCallback(
|
||||
[this, redirectUrl](
|
||||
HttpRequestPtr request,
|
||||
std::shared_ptr<ConnectionState> /*connectionState*/) -> HttpResponsePtr {
|
||||
|
||||
WebSocketHttpHeaders headers;
|
||||
headers["Server"] = userAgent();
|
||||
|
||||
// Log request
|
||||
std::stringstream ss;
|
||||
ss << request->method << " " << request->headers["User-Agent"] << " "
|
||||
<< request->uri;
|
||||
logInfo(ss.str());
|
||||
|
||||
if (request->method == "POST")
|
||||
{
|
||||
return std::make_shared<HttpResponse>(
|
||||
200, "OK", HttpErrorCode::Ok, headers, std::string());
|
||||
}
|
||||
|
||||
headers["Location"] = redirectUrl;
|
||||
|
||||
return std::make_shared<HttpResponse>(
|
||||
301, "OK", HttpErrorCode::Ok, headers, std::string());
|
||||
});
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -34,6 +34,8 @@ namespace ix
|
||||
|
||||
void setOnConnectionCallback(const OnConnectionCallback& callback);
|
||||
|
||||
void makeRedirectServer(const std::string& redirectUrl);
|
||||
|
||||
private:
|
||||
// Member variables
|
||||
OnConnectionCallback _onConnectionCallback;
|
||||
|
@ -69,6 +69,7 @@ namespace ix
|
||||
ss << " ";
|
||||
ss << reason;
|
||||
ss << "\r\n";
|
||||
ss << "Server: " << userAgent() << "\r\n";
|
||||
|
||||
// Socket write can only be cancelled through a timeout here, not manually.
|
||||
static std::atomic<bool> requestInitCancellation(false);
|
||||
|
Reference in New Issue
Block a user