Merge request handler to server; add FileBody for serving static files.

This commit is contained in:
Chunting Gu
2019-07-12 16:05:01 +08:00
parent 91590b8ee8
commit 8b85248a34
12 changed files with 493 additions and 470 deletions
+67 -44
View File
@@ -1,8 +1,7 @@
#ifndef WEBCC_URL_H_
#define WEBCC_URL_H_
// A simplified implementation of URL (or URI).
#include <regex>
#include <string>
#include <utility>
#include <vector>
@@ -13,48 +12,7 @@ namespace webcc {
// -----------------------------------------------------------------------------
// URL query parameters.
class UrlQuery {
public:
using Parameter = std::pair<std::string, std::string>;
using Parameters = std::vector<Parameter>;
UrlQuery() = default;
// The query string should be key value pairs separated by '&'.
explicit UrlQuery(const std::string& str);
void Add(const std::string& key, const std::string& value);
void Add(std::string&& key, std::string&& value);
void Remove(const std::string& key);
// Get a value by key.
// Return empty string if the key doesn't exist.
const std::string& Get(const std::string& key) const;
bool Has(const std::string& key) const {
return Find(key) != parameters_.end();
}
bool IsEmpty() const {
return parameters_.empty();
}
// Return key-value pairs concatenated by '&'.
// E.g., "item=12731&color=blue&size=large".
std::string ToString() const;
private:
using ConstIterator = Parameters::const_iterator;
ConstIterator Find(const std::string& key) const;
Parameters parameters_;
};
// -----------------------------------------------------------------------------
// A simplified implementation of URL (or URI).
class Url {
public:
Url() = default;
@@ -127,6 +85,71 @@ private:
std::string query_;
};
// -----------------------------------------------------------------------------
// URL query parameters.
class UrlQuery {
public:
using Parameter = std::pair<std::string, std::string>;
UrlQuery() = default;
// The query string should be key value pairs separated by '&'.
explicit UrlQuery(const std::string& str);
void Add(const std::string& key, const std::string& value);
void Add(std::string&& key, std::string&& value);
void Remove(const std::string& key);
// Get a value by key.
// Return empty string if the key doesn't exist.
const std::string& Get(const std::string& key) const;
bool Has(const std::string& key) const {
return Find(key) != parameters_.end();
}
bool IsEmpty() const {
return parameters_.empty();
}
// Return key-value pairs concatenated by '&'.
// E.g., "item=12731&color=blue&size=large".
std::string ToString() const;
private:
using ConstIterator = std::vector<Parameter>::const_iterator;
ConstIterator Find(const std::string& key) const;
private:
std::vector<Parameter> parameters_;
};
// -----------------------------------------------------------------------------
// Wrapper for URL as regular expression.
// Used by Server::Route().
class UrlRegex {
public:
explicit UrlRegex(const std::string& url) : url_(url) {
}
std::regex operator()() const {
std::regex::flag_type flags = std::regex::ECMAScript | std::regex::icase;
return std::regex(url_, flags);
}
private:
std::string url_;
};
// Shortcut
using R = UrlRegex;
} // namespace webcc
#endif // WEBCC_URL_H_