SNode.C
Loading...
Searching...
No Matches
express::dispatcher::ScopedPathStrip Class Reference

#include <regex_utils.h>

Collaboration diagram for express::dispatcher::ScopedPathStrip:

Public Member Functions

 ScopedPathStrip (express::Request &req, bool enabled, std::size_t consumedLength)
 ~ScopedPathStrip ()
 ScopedPathStrip (const ScopedPathStrip &)=delete
ScopedPathStripoperator= (const ScopedPathStrip &)=delete
 ScopedPathStrip (ScopedPathStrip &&)=delete
ScopedPathStripoperator= (ScopedPathStrip &&)=delete

Private Attributes

express::Requestreq_ {nullptr}
std::string backupUrl_
std::string backupBaseUrl_
std::string backupPath_
std::string backupFile_
bool enabled_ {false}

Detailed Description

Definition at line 102 of file regex_utils.h.

Constructor & Destructor Documentation

◆ ScopedPathStrip() [1/3]

express::dispatcher::ScopedPathStrip::ScopedPathStrip ( express::Request & req,
bool enabled,
std::size_t consumedLength )

Definition at line 711 of file regex_utils.cpp.

712 : req_(&req)
713 , enabled_(enabled) {
714 if (!enabled_) {
715 return;
716 }
717
718 backupUrl_ = req.url;
720 backupPath_ = req.path;
721 backupFile_ = req.file;
722
723 // Express semantics:
724 // - req.originalUrl stays constant
725 // - req.baseUrl is the consumed mount path (root mount -> empty string)
726 // - req.url and req.path become the remainder (path + query / path)
727 std::string_view fullPath;
728 std::string_view fullQuery;
729 splitPathAndQuery(req.url, fullPath, fullQuery);
730
731 // IMPORTANT:
732 // We mutate req.url below, which can reallocate and invalidate fullQuery/fullPath
733 // string_views.
734 // Keep an owning copy of the query part before touching req.url.
735 std::string fullQueryCopy;
736 if (!fullQuery.empty()) {
737 fullQueryCopy.assign(fullQuery.begin(), fullQuery.end());
738 }
739
740 // Compute consumed part and remainder (consumedLength refers to the matched prefix in the path).
741 const std::size_t cl = std::min<std::size_t>(consumedLength, fullPath.size());
742 std::string_view consumed = fullPath.substr(0, cl);
743 const std::string_view remainder = (fullPath.size() > cl) ? fullPath.substr(cl) : std::string_view{};
744
745 // baseUrl never ends with a trailing slash and is empty for the root mount.
746 consumed = trimOneTrailingSlash(consumed);
747 if (consumed.size() == 1 && consumed[0] == '/') {
748 consumed = {};
749 }
750 req.baseUrl = joinMountPath(backupBaseUrl_, consumed);
751
752 // Ensure remainder starts with '/'.
753 std::string remPath;
754 if (remainder.empty()) {
755 remPath = "/";
756 } else if (remainder.front() == '/') {
757 remPath.assign(remainder.begin(), remainder.end());
758 } else {
759 remPath = "/";
760 remPath.append(remainder.begin(), remainder.end());
761 }
762
763 req.path = remPath;
764 req.url = remPath;
765 if (!fullQueryCopy.empty()) {
766 req.url.push_back('?');
767 req.url.append(fullQueryCopy);
768 }
769 }
std::string url
Definition Request.h:98
std::string baseUrl
Definition Request.h:76
std::string path
Definition Request.h:79
std::string file
Definition Request.h:80
std::string joinMountPath(std::string_view parentMountPath, std::string_view relativeMountPath)
void splitPathAndQuery(std::string_view url, std::string_view &path, std::string_view &query)
std::string_view trimOneTrailingSlash(std::string_view s)

References backupBaseUrl_, backupFile_, backupPath_, backupUrl_, express::Request::baseUrl, enabled_, express::Request::file, express::dispatcher::joinMountPath(), express::Request::path, req_, express::dispatcher::splitPathAndQuery(), express::dispatcher::trimOneTrailingSlash(), and express::Request::url.

Referenced by express::dispatcher::ApplicationDispatcher::dispatch(), express::dispatcher::MiddlewareDispatcher::dispatch(), and express::dispatcher::RouterDispatcher::dispatch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~ScopedPathStrip()

express::dispatcher::ScopedPathStrip::~ScopedPathStrip ( )

Definition at line 771 of file regex_utils.cpp.

771 {
772 if (enabled_ && req_ != nullptr) {
773 req_->url = backupUrl_;
774 req_->baseUrl = backupBaseUrl_;
775 req_->path = backupPath_;
776 req_->file = backupFile_;
777 }
778 }

References backupBaseUrl_, backupFile_, backupPath_, backupUrl_, express::Request::baseUrl, enabled_, express::Request::file, express::Request::path, req_, and express::Request::url.

◆ ScopedPathStrip() [2/3]

express::dispatcher::ScopedPathStrip::ScopedPathStrip ( const ScopedPathStrip & )
delete

◆ ScopedPathStrip() [3/3]

express::dispatcher::ScopedPathStrip::ScopedPathStrip ( ScopedPathStrip && )
delete

Member Function Documentation

◆ operator=() [1/2]

ScopedPathStrip & express::dispatcher::ScopedPathStrip::operator= ( const ScopedPathStrip & )
delete

◆ operator=() [2/2]

ScopedPathStrip & express::dispatcher::ScopedPathStrip::operator= ( ScopedPathStrip && )
delete

Member Data Documentation

◆ backupBaseUrl_

std::string express::dispatcher::ScopedPathStrip::backupBaseUrl_
private

Definition at line 115 of file regex_utils.h.

Referenced by ScopedPathStrip(), and ~ScopedPathStrip().

◆ backupFile_

std::string express::dispatcher::ScopedPathStrip::backupFile_
private

Definition at line 117 of file regex_utils.h.

Referenced by ScopedPathStrip(), and ~ScopedPathStrip().

◆ backupPath_

std::string express::dispatcher::ScopedPathStrip::backupPath_
private

Definition at line 116 of file regex_utils.h.

Referenced by ScopedPathStrip(), and ~ScopedPathStrip().

◆ backupUrl_

std::string express::dispatcher::ScopedPathStrip::backupUrl_
private

Definition at line 114 of file regex_utils.h.

Referenced by ScopedPathStrip(), and ~ScopedPathStrip().

◆ enabled_

bool express::dispatcher::ScopedPathStrip::enabled_ {false}
private

Definition at line 118 of file regex_utils.h.

118{false};

Referenced by ScopedPathStrip(), and ~ScopedPathStrip().

◆ req_

express::Request* express::dispatcher::ScopedPathStrip::req_ {nullptr}
private

Definition at line 113 of file regex_utils.h.

113{nullptr};

Referenced by ScopedPathStrip(), and ~ScopedPathStrip().


The documentation for this class was generated from the following files: