Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/ASProcessHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ void ASProcessHandler::forkProcessASHandler(ASProcessHandlerSHMPointer_t SHMAdre

for (const auto& Namespace: ConfigRef.Namespaces) {

for (const auto &Index:_VHostOffsetsPrecalc.at(Namespace.first)) {
auto offsetIter = _VHostOffsetsPrecalc.find(Namespace.first);
if (offsetIter == _VHostOffsetsPrecalc.end()) {
ERR("_VHostOffsetsPrecalc does not contain Namespace:" << Namespace.first);
continue;
}

for (const auto &Index:offsetIter->second) {

pid_t ForkResult = fork();

Expand Down
8 changes: 4 additions & 4 deletions src/ASRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ void ASRequestHandler::_calculateOffsets() {
}

for (const auto &Offset:_VHostOffsets) {
uint16_t i = _VHostOffsets.at(Offset.first).OffsetStart;
uint16_t n = _VHostOffsets.at(Offset.first).OffsetEnd;
uint16_t i = Offset.second.OffsetStart;
uint16_t n = Offset.second.OffsetEnd;
while(i <= n) {
_VHostOffsetsPrecalc.at(Offset.first).push_back(i);
_VHostOffsetsPrecalc[Offset.first].push_back(i);
++i;
}
}
Expand Down Expand Up @@ -72,7 +72,7 @@ void ASRequestHandler::addRequest(const ASRequestProps_t RequestProps) {

AppServerID_t ASRequestHandler::_getNextFreeAppServerID(string VirtualHost) {
if (_VHostOffsetsPrecalc.contains(VirtualHost)) {
for (const auto &Index:_VHostOffsetsPrecalc.at(VirtualHost)) {
for (const auto &Index:_VHostOffsetsPrecalc[VirtualHost]) {
DBG(180, "Namespace:" << VirtualHost << " Index:" << Index);
if (!_Requests.contains(Index)) {
DBG(140, "Requests do not contain Index, check SHM");
Expand Down
2 changes: 1 addition & 1 deletion src/ClientHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void ClientHandler::readClientData(const uint16_t FDCount)
else {
//- if filedescriptor exists in map, append buffer data
if (Clients.contains(ReadFD)) {
ClientRef_t ClientRef = Clients.at(ReadFD);
ClientRef_t ClientRef = Clients[ReadFD];
ClientRef->appendBuffer(Buffer, RcvBytes);

if (ClientRef->processRequests(SHMGetRequests, _ASRequestHandlerRef) > 0) {
Expand Down
14 changes: 12 additions & 2 deletions src/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ void Filesystem::processFileProperties()

FindPos = FileName.rfind(".");
string FileExtension = FileName.substr(FindPos+1, File.length()-FindPos);
string MimeType = MimeRelations.at(FileExtension);

//- safely get mime type
auto mimeIter = MimeRelations.find(FileExtension);
string MimeType = (mimeIter != MimeRelations.end()) ? mimeIter->second : "application/octet-stream";

string ReplacePath = BasePath + Path + "/static";
string RelPath = FilePath.substr(ReplacePath.length(), FilePath.length()-ReplacePath.length());
string FileListKey = RelPath + "/" + FileName;
Expand Down Expand Up @@ -103,7 +107,13 @@ bool Filesystem::checkFileExists(const string &File)

FileProperties_t Filesystem::getFilePropertiesByFile(const string &File)
{
return _FilesExtended.at(File);
auto iter = _FilesExtended.find(File);
if (iter != _FilesExtended.end()) {
return iter->second;
}
//- return empty FileProperties if not found (should not happen after checkFileExists)
ERR("File not found in _FilesExtended map: " << File);
return FileProperties_t{};
}

string Filesystem::getFileEtag(const string &File) {
Expand Down
43 changes: 28 additions & 15 deletions src/ResultOrder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ void ResultOrder::append(
)
{
if (_Requests.contains(ReqProps.ClientFD)) {
_Requests.at(ReqProps.ClientFD).emplace(
auto& clientRequests = _Requests[ReqProps.ClientFD];
clientRequests.emplace(
ReqNr, std::move(ReqProps)
);
}
Expand Down Expand Up @@ -57,34 +58,46 @@ void ResultOrder::processRequests(const HTTPType_t HTTPType)

//- HTTP/1.2 (unordered)
if (HTTPType == HTTP1_2) {
for (const auto& [ReqNr, ReqProps]: _Requests.at(ClientFD)) {
if (ReqProps.HTTPVersion == HTTP1_2) {
DBG(120, "HTTP1_2 RequestProps ClientFD:" << ReqProps.ClientFD << " HTTPType:" << ReqProps.HTTPVersion);
ResultData.push_back(ReqProps);
auto requestsIter = _Requests.find(ClientFD);
if (requestsIter != _Requests.end()) {
for (const auto& [ReqNr, ReqProps]: requestsIter->second) {
if (ReqProps.HTTPVersion == HTTP1_2) {
DBG(120, "HTTP1_2 RequestProps ClientFD:" << ReqProps.ClientFD << " HTTPType:" << ReqProps.HTTPVersion);
ResultData.push_back(ReqProps);
}
}
}
}

//- HTTP/1.1 (ordered)
if (HTTPType == HTTP1_1) {

RequestNr_t LastRequestNrCheck = _LastRequest.at(ClientFD).RequestNr;
auto lastRequestIter = _LastRequest.find(ClientFD);
if (lastRequestIter == _LastRequest.end()) {
ERR("_LastRequest does not contain ClientFD:" << ClientFD);
continue;
}

RequestNr_t LastRequestNrCheck = lastRequestIter->second.RequestNr;
RequestNr_t ReqIndex = 0;

DBG(200, "Processing HTTP1.1 LastReqNrCheck:" << LastRequestNrCheck);

for (const auto& [ReqNr, ReqProps]: _Requests.at(ClientFD)) {
auto requestsIter = _Requests.find(ClientFD);
if (requestsIter != _Requests.end()) {
for (const auto& [ReqNr, ReqProps]: requestsIter->second) {

DBG(200, "ReqIndex:" << ReqIndex << " ReqNr:" << ReqNr);
DBG(200, "ReqIndex:" << ReqIndex << " ReqNr:" << ReqNr);

//- if first ReqNr is not next (LastRequest+1) do not process
if (ReqProps.HTTPVersion == HTTP1_1 && ReqIndex == 0 && ReqNr != LastRequestNrCheck) { break; }
if (ReqProps.HTTPVersion == HTTP1_1 && ReqNr == LastRequestNrCheck) {
DBG(200, "HTTP1_1 RequestProps ClientFD:" << ReqProps.ClientFD << " HTTPType:" << ReqProps.HTTPVersion);
ResultData.push_back(ReqProps);
//- if first ReqNr is not next (LastRequest+1) do not process
if (ReqProps.HTTPVersion == HTTP1_1 && ReqIndex == 0 && ReqNr != LastRequestNrCheck) { break; }
if (ReqProps.HTTPVersion == HTTP1_1 && ReqNr == LastRequestNrCheck) {
DBG(200, "HTTP1_1 RequestProps ClientFD:" << ReqProps.ClientFD << " HTTPType:" << ReqProps.HTTPVersion);
ResultData.push_back(ReqProps);
}
LastRequestNrCheck += 1;
ReqIndex += 1;
}
LastRequestNrCheck += 1;
ReqIndex += 1;
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/ResultProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ uint16_t ResultProcessor::_processPythonASResults()
uint16_t processed = 0;

for (const auto& Namespace: ConfigRef.Namespaces) {
for (const auto &Index: _VHostOffsetsPrecalc.at(Namespace.first)) {
auto offsetIter = _VHostOffsetsPrecalc.find(Namespace.first);
if (offsetIter == _VHostOffsetsPrecalc.end()) {
ERR("_VHostOffsetsPrecalc does not contain Namespace:" << Namespace.first);
continue;
}

for (const auto &Index: offsetIter->second) {
atomic_uint16_t* CanReadAddr = static_cast<atomic_uint16_t*>(getMetaAddress(Index, 0));
atomic_uint16_t* WriteReadyAddr = static_cast<atomic_uint16_t*>(getMetaAddress(Index, 1));
if (*(CanReadAddr) == 0 && *(WriteReadyAddr) == 1) {
Expand Down
72 changes: 59 additions & 13 deletions src/ThreadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void ThreadHandler::_addRequests(

for (const auto& RequestProps : Requests) {
if (_RequestsSorted.contains(RequestProps.ClientFD)) {
_RequestsSorted.at(RequestProps.ClientFD).push_back(std::move(RequestProps));
_RequestsSorted[RequestProps.ClientFD].push_back(std::move(RequestProps));
}
else {
ClientRequestDataVec_t ReqPropsVec;
Expand All @@ -45,11 +45,18 @@ void ThreadHandler::_checkProcessed()
if (it->second->join()) {
DBG(120, "Joined Thread for ClientFD:" << it->first << ", removing from Map");

_ProcessRequests.erase(
_ProcessRequests.begin()+_ProcessRequestsIndex.at(it->first)
);
//- safely check if index exists before erasing
auto indexIter = _ProcessRequestsIndex.find(it->first);
if (indexIter != _ProcessRequestsIndex.end()) {
_ProcessRequests.erase(
_ProcessRequests.begin()+indexIter->second
);
_ProcessRequestsIndex.erase(indexIter);
}
else {
ERR("_ProcessRequestsIndex does not contain ClientFD:" << it->first);
}

_ProcessRequestsIndex.erase(it->first);
_ClientThreads.erase(it++);
}
else {++it;};
Expand Down Expand Up @@ -138,11 +145,23 @@ void ClientThread::processRequests()
RequestHeaderResult_t Headers;
_parseRequestHeaders(_ClientRequests[i].HTTPPayload, Headers);

DBG(120, "RequestType:'" << BaseProps.at(2) << "'");
DBG(120, "RequestPath:'" << BaseProps.at(1) << "'");
DBG(120, "HTTPVersion:'" << BaseProps.at(0) << "'");
//- validate BaseProps has minimum required elements
if (BaseProps.size() < 3) {
ERR("Invalid HTTP request - insufficient base properties");
continue;
}

DBG(120, "RequestType:'" << BaseProps[2] << "'");
DBG(120, "RequestPath:'" << BaseProps[1] << "'");
DBG(120, "HTTPVersion:'" << BaseProps[0] << "'");

const string NamespaceID = Headers.at("Host");
//- check if Host header exists
auto hostIter = Headers.find("Host");
if (hostIter == Headers.end()) {
ERR("Missing Host header in request");
continue;
}
const string NamespaceID = hostIter->second;
DBG(120, "NamespaceID:'" << NamespaceID << "'");

FileProperties_t FileProps;
Expand All @@ -158,13 +177,39 @@ void ClientThread::processRequests()
}
*/

NamespaceProps_t NamespaceProps = _Namespaces.at(NamespaceID);
//- check if namespace exists
auto namespaceIter = _Namespaces.find(NamespaceID);
if (namespaceIter == _Namespaces.end()) {
ERR("Namespace not found for NamespaceID:'" << NamespaceID << "'");

//- send 404 response for non-existent vhost
stringstream current_date_404;
std::time_t tt_404 = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now());
struct std::tm * ptm_404 = std::localtime(&tt_404);
current_date_404 << std::put_time(ptm_404, "%a, %d %b %Y %T") << '\n';

string Response404 = "HTTP/1.1 404 Not Found\n";
Response404.append("Date: ");
Response404.append(current_date_404.str());
Response404.append(" GMT\n");
Response404.append("Server: falcon/0.1\n");
Response404.append("Content-Length: 26\n");
Response404.append("Content-Type: text/html\n\n");
Response404.append("<html>404 Not Found</html>");

const char* send_buf_404 = Response404.c_str();
const int rc_404 = write(_ClientRequests[i].ClientFDShared, send_buf_404, strlen(send_buf_404));
if (rc_404 < 0) { ERR("write() err:" << strerror(errno)); }
continue;
}

NamespaceProps_t NamespaceProps = namespaceIter->second;

DBG(120, "NamespacePath:'" << NamespaceProps.FilesystemRef->Path << "'");
DBG(120, "NamespaceBasePath:'" << NamespaceProps.FilesystemRef->BasePath << "'");

if (NamespaceProps.FilesystemRef->checkFileExists(BaseProps.at(1))) {
FileProps = NamespaceProps.FilesystemRef->getFilePropertiesByFile(BaseProps.at(1));
if (NamespaceProps.FilesystemRef->checkFileExists(BaseProps[1])) {
FileProps = NamespaceProps.FilesystemRef->getFilePropertiesByFile(BaseProps[1]);

DBG(80, "ClientFileDescriptor:" << _ClientRequests[i].ClientFD);
DBG(80, "FileDescriptor:" << FileProps.Filedescriptor);
Expand All @@ -174,7 +219,8 @@ void ClientThread::processRequests()
DBG(80, "JSONConfig:" << NamespaceProps.JSONConfig);

//- if etags match, send 304 not-modified
if (Headers.find("If-None-Match") != Headers.end() && Headers.at("If-None-Match") == FileProps.ETag) {
auto etagIter = Headers.find("If-None-Match");
if (etagIter != Headers.end() && etagIter->second == FileProps.ETag) {
string Response304 = "HTTP/1.1 304 Not Modified\n";
Response304.append("Date: ");
Response304.append(current_date.str());
Expand Down
Binary file added test/unit/memory-manager/test-memory-manager
Binary file not shown.
Loading