DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
WebApplicationRouting.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include <string>
3
#include <vector>
4
#include <utility>
5
6
namespace
DotNetDupe
{
7
namespace
WebAppCore
{
8
namespace
Builder
{
9
namespace
Internal
{
10
11
std::vector<std::string>
GetPathSegments
(
const
std::string& path) {
13
std::vector<std::string> segments;
14
std::string segment;
15
17
for
(
char
c : path) {
18
if
(c ==
'/'
) {
19
if
(!segment.empty()) {
20
segments.push_back(segment);
21
segment.clear();
22
}
23
}
else
{
24
segment += c;
25
}
26
}
27
28
if
(!segment.empty()) {
29
segments.push_back(segment);
30
}
31
33
return
segments;
34
}
35
36
bool
MatchCatchAllRoute
(
const
std::vector<std::string>& patternSegs,
const
std::vector<std::string>& pathSegs, std::vector<std::pair<std::string, std::string>>& extractedParams) {
38
size_t
fixedCount = patternSegs.size() - 1;
39
if
(pathSegs.size() < fixedCount) {
40
return
false
;
41
}
42
44
for
(
size_t
i = 0; i < fixedCount; ++i) {
45
const
std::string& patternSeg = patternSegs[i];
46
const
std::string& pathSeg = pathSegs[i];
47
if
(patternSeg.length() >= 2 && patternSeg.front() ==
'{'
&& patternSeg.back() ==
'}'
) {
48
std::string paramName = patternSeg.substr(1, patternSeg.length() - 2);
49
extractedParams.push_back({paramName, pathSeg});
50
}
else
if
(patternSeg != pathSeg) {
51
return
false
;
52
}
53
}
54
56
std::string catchAllName = patternSegs.back().substr(2, patternSegs.back().length() - 3);
57
std::string restPath;
58
for
(
size_t
i = fixedCount; i < pathSegs.size(); ++i) {
59
if
(!restPath.empty()) {
60
restPath +=
"/"
;
61
}
62
restPath += pathSegs[i];
63
}
64
65
extractedParams.push_back({catchAllName, restPath});
66
return
true
;
67
}
68
69
bool
MatchStandardRoute
(
const
std::vector<std::string>& patternSegs,
const
std::vector<std::string>& pathSegs, std::vector<std::pair<std::string, std::string>>& extractedParams) {
71
if
(patternSegs.size() != pathSegs.size()) {
72
return
false
;
73
}
74
76
for
(
size_t
i = 0; i < patternSegs.size(); ++i) {
77
const
std::string& patternSeg = patternSegs[i];
78
const
std::string& pathSeg = pathSegs[i];
79
if
(patternSeg.length() >= 2 && patternSeg.front() ==
'{'
&& patternSeg.back() ==
'}'
) {
80
std::string paramName = patternSeg.substr(1, patternSeg.length() - 2);
81
extractedParams.push_back({paramName, pathSeg});
82
}
else
if
(patternSeg != pathSeg) {
83
return
false
;
84
}
85
}
86
88
return
true
;
89
}
90
91
bool
MatchRoute
(
const
std::vector<std::string>& patternSegs,
const
std::vector<std::string>& pathSegs, std::vector<std::pair<std::string, std::string>>& extractedParams) {
93
if
(!patternSegs.empty() && patternSegs.back().length() >= 3 && patternSegs.back().front() ==
'{'
&& patternSegs.back()[1] ==
'*'
&& patternSegs.back().back() ==
'}'
) {
94
return
MatchCatchAllRoute
(patternSegs, pathSegs, extractedParams);
95
}
96
98
return
MatchStandardRoute
(patternSegs, pathSegs, extractedParams);
99
}
100
101
void
ParseServerUrl
(
const
std::string& sUrl, std::string& host,
int
& port) {
103
host =
"127.0.0.1"
;
104
port = 5000;
105
107
size_t
protocolPos = sUrl.find(
"://"
);
108
std::string hostPort = (protocolPos != std::string::npos) ? sUrl.substr(protocolPos + 3) : sUrl;
109
111
size_t
colonPos = hostPort.find(
':'
);
112
if
(colonPos != std::string::npos) {
113
host = hostPort.substr(0, colonPos);
114
std::string sPort = hostPort.substr(colonPos + 1);
115
size_t
slashPos = sPort.find(
'/'
);
116
if
(slashPos != std::string::npos) sPort = sPort.substr(0, slashPos);
117
try
{ port = std::stoi(sPort); }
catch
(
const
std::exception&) { port = 5000; }
118
}
else
{
119
size_t
slashPos = hostPort.find(
'/'
);
120
host = (slashPos != std::string::npos) ? hostPort.substr(0, slashPos) : hostPort;
121
}
122
124
if
(host ==
"localhost"
) host =
"127.0.0.1"
;
125
}
126
127
}
128
}
129
}
130
}
DotNetDupe::WebAppCore::Builder::Internal
Definition
WebApplication.cpp:22
DotNetDupe::WebAppCore::Builder::Internal::ParseServerUrl
void ParseServerUrl(const std::string &sUrl, std::string &host, int &port)
Definition
WebApplicationRouting.cpp:101
DotNetDupe::WebAppCore::Builder::Internal::MatchRoute
bool MatchRoute(const std::vector< std::string > &patternSegs, const std::vector< std::string > &pathSegs, std::vector< std::pair< std::string, std::string > > &extractedParams)
Definition
WebApplicationRouting.cpp:91
DotNetDupe::WebAppCore::Builder::Internal::MatchCatchAllRoute
bool MatchCatchAllRoute(const std::vector< std::string > &patternSegs, const std::vector< std::string > &pathSegs, std::vector< std::pair< std::string, std::string > > &extractedParams)
Definition
WebApplicationRouting.cpp:36
DotNetDupe::WebAppCore::Builder::Internal::GetPathSegments
std::vector< std::string > GetPathSegments(const std::string &path)
Definition
WebApplicationRouting.cpp:11
DotNetDupe::WebAppCore::Builder::Internal::MatchStandardRoute
bool MatchStandardRoute(const std::vector< std::string > &patternSegs, const std::vector< std::string > &pathSegs, std::vector< std::pair< std::string, std::string > > &extractedParams)
Definition
WebApplicationRouting.cpp:69
DotNetDupe::WebAppCore::Builder
Definition
WebApplication.h:21
DotNetDupe::WebAppCore
Definition
WebApplication.h:20
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe
WebApplicationRouting.cpp
Generated by
1.18.0