mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-16 20:27:08 +03:00
book: add line breaks for code formatting (#226)
This commit is contained in:
@@ -84,7 +84,8 @@ We use a simple example to briefly introduce the use of this library. Consider t
|
||||
|
||||
int main() {
|
||||
std::string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"};
|
||||
// In C++, `\` will be used as an escape character in the string. In order for `\.` to be passed as a regular expression, it is necessary to perform second escaping of `\`, thus we have `\\.`
|
||||
// In C++, `\` will be used as an escape character in the string. In order for `\.`
|
||||
// to be passed as a regular expression, it is necessary to perform second escaping of `\`, thus we have `\\.`
|
||||
std::regex txt_regex("[a-z]+\\.txt");
|
||||
for (const auto &fname: fnames)
|
||||
std::cout << fname << ": " << std::regex_match(fname, txt_regex) << std::endl;
|
||||
@@ -186,29 +187,32 @@ Please implement the member functions `start()` and `parse_request`. Enable serv
|
||||
template<typename SERVER_TYPE>
|
||||
void start_server(SERVER_TYPE &server) {
|
||||
|
||||
// process GET request for /match/[digit+numbers], e.g. GET request is /match/abc123, will return abc123
|
||||
// process GET request for /match/[digit+numbers], e.g.
|
||||
// GET request is /match/abc123, will return abc123
|
||||
server.resource["fill_your_reg_ex"]["GET"] = [](ostream& response, Request& request) {
|
||||
string number=request.path_match[1];
|
||||
response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
|
||||
response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length()
|
||||
<< "\r\n\r\n" << number;
|
||||
};
|
||||
|
||||
// peocess default GET request; anonymous function will be called if no other matches
|
||||
// response files in folder web/
|
||||
// default: index.html
|
||||
server.default_resource["fill_your_reg_ex"]["GET"] = [](ostream& response, Request& request) {
|
||||
string filename = "www/";
|
||||
server.default_resource["fill_your_reg_ex"]["GET"] =
|
||||
[](ostream& response, Request& request) {
|
||||
string filename = "www/";
|
||||
|
||||
string path = request.path_match[1];
|
||||
string path = request.path_match[1];
|
||||
|
||||
// forbidden use `..` access content outside folder web/
|
||||
size_t last_pos = path.rfind(".");
|
||||
size_t current_pos = 0;
|
||||
size_t pos;
|
||||
while((pos=path.find('.', current_pos)) != string::npos && pos != last_pos) {
|
||||
current_pos = pos;
|
||||
path.erase(pos, 1);
|
||||
last_pos--;
|
||||
}
|
||||
// forbidden use `..` access content outside folder web/
|
||||
size_t last_pos = path.rfind(".");
|
||||
size_t current_pos = 0;
|
||||
size_t pos;
|
||||
while((pos=path.find('.', current_pos)) != string::npos && pos != last_pos) {
|
||||
current_pos = pos;
|
||||
path.erase(pos, 1);
|
||||
last_pos--;
|
||||
}
|
||||
|
||||
// (...)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user