Improved comments on public functions and variables as suggested in https://github.com/openjournals/joss-reviews/issues/1592#issuecomment-514946444
This commit is contained in:
parent
5d89bbcd0f
commit
ed46b43fa7
7 changed files with 130 additions and 57 deletions
64
utility.hpp
64
utility.hpp
|
|
@ -151,7 +151,7 @@ namespace SimpleWeb {
|
|||
|
||||
class HttpHeader {
|
||||
public:
|
||||
/// Parse header fields
|
||||
/// Parse header fields from stream
|
||||
static CaseInsensitiveMultimap parse(std::istream &stream) noexcept {
|
||||
CaseInsensitiveMultimap result;
|
||||
std::string line;
|
||||
|
|
@ -170,36 +170,37 @@ namespace SimpleWeb {
|
|||
public:
|
||||
class SemicolonSeparatedAttributes {
|
||||
public:
|
||||
/// Parse Set-Cookie or Content-Disposition header field value. Attribute values are percent-decoded.
|
||||
static CaseInsensitiveMultimap parse(const std::string &str) {
|
||||
/// Parse Set-Cookie or Content-Disposition from given header field value.
|
||||
/// Attribute values are percent-decoded.
|
||||
static CaseInsensitiveMultimap parse(const std::string &value) {
|
||||
CaseInsensitiveMultimap result;
|
||||
|
||||
std::size_t name_start_pos = std::string::npos;
|
||||
std::size_t name_end_pos = std::string::npos;
|
||||
std::size_t value_start_pos = std::string::npos;
|
||||
for(std::size_t c = 0; c < str.size(); ++c) {
|
||||
for(std::size_t c = 0; c < value.size(); ++c) {
|
||||
if(name_start_pos == std::string::npos) {
|
||||
if(str[c] != ' ' && str[c] != ';')
|
||||
if(value[c] != ' ' && value[c] != ';')
|
||||
name_start_pos = c;
|
||||
}
|
||||
else {
|
||||
if(name_end_pos == std::string::npos) {
|
||||
if(str[c] == ';') {
|
||||
result.emplace(str.substr(name_start_pos, c - name_start_pos), std::string());
|
||||
if(value[c] == ';') {
|
||||
result.emplace(value.substr(name_start_pos, c - name_start_pos), std::string());
|
||||
name_start_pos = std::string::npos;
|
||||
}
|
||||
else if(str[c] == '=')
|
||||
else if(value[c] == '=')
|
||||
name_end_pos = c;
|
||||
}
|
||||
else {
|
||||
if(value_start_pos == std::string::npos) {
|
||||
if(str[c] == '"' && c + 1 < str.size())
|
||||
if(value[c] == '"' && c + 1 < value.size())
|
||||
value_start_pos = c + 1;
|
||||
else
|
||||
value_start_pos = c;
|
||||
}
|
||||
else if(str[c] == '"' || str[c] == ';') {
|
||||
result.emplace(str.substr(name_start_pos, name_end_pos - name_start_pos), Percent::decode(str.substr(value_start_pos, c - value_start_pos)));
|
||||
else if(value[c] == '"' || value[c] == ';') {
|
||||
result.emplace(value.substr(name_start_pos, name_end_pos - name_start_pos), Percent::decode(value.substr(value_start_pos, c - value_start_pos)));
|
||||
name_start_pos = std::string::npos;
|
||||
name_end_pos = std::string::npos;
|
||||
value_start_pos = std::string::npos;
|
||||
|
|
@ -209,12 +210,12 @@ namespace SimpleWeb {
|
|||
}
|
||||
if(name_start_pos != std::string::npos) {
|
||||
if(name_end_pos == std::string::npos)
|
||||
result.emplace(str.substr(name_start_pos), std::string());
|
||||
result.emplace(value.substr(name_start_pos), std::string());
|
||||
else if(value_start_pos != std::string::npos) {
|
||||
if(str.back() == '"')
|
||||
result.emplace(str.substr(name_start_pos, name_end_pos - name_start_pos), Percent::decode(str.substr(value_start_pos, str.size() - 1)));
|
||||
if(value.back() == '"')
|
||||
result.emplace(value.substr(name_start_pos, name_end_pos - name_start_pos), Percent::decode(value.substr(value_start_pos, value.size() - 1)));
|
||||
else
|
||||
result.emplace(str.substr(name_start_pos, name_end_pos - name_start_pos), Percent::decode(str.substr(value_start_pos)));
|
||||
result.emplace(value.substr(name_start_pos, name_end_pos - name_start_pos), Percent::decode(value.substr(value_start_pos)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -222,11 +223,21 @@ namespace SimpleWeb {
|
|||
}
|
||||
};
|
||||
};
|
||||
}; // namespace SimpleWeb
|
||||
};
|
||||
|
||||
class RequestMessage {
|
||||
public:
|
||||
/// Parse request line and header fields
|
||||
/** Parse request line and header fields from a request stream.
|
||||
*
|
||||
* @param[in] stream Stream to parse.
|
||||
* @param[out] method HTTP method.
|
||||
* @param[out] path Path from request URI.
|
||||
* @param[out] query_string Query string from request URI.
|
||||
* @param[out] version HTTP version.
|
||||
* @param[out] header Header fields.
|
||||
*
|
||||
* @return True if stream is parsed successfully, false if not.
|
||||
*/
|
||||
static bool parse(std::istream &stream, std::string &method, std::string &path, std::string &query_string, std::string &version, CaseInsensitiveMultimap &header) noexcept {
|
||||
std::string line;
|
||||
std::size_t method_end;
|
||||
|
|
@ -273,7 +284,15 @@ namespace SimpleWeb {
|
|||
|
||||
class ResponseMessage {
|
||||
public:
|
||||
/// Parse status line and header fields
|
||||
/** Parse status line and header fields from a response stream.
|
||||
*
|
||||
* @param[in] stream Stream to parse.
|
||||
* @param[out] version HTTP version.
|
||||
* @param[out] status_code HTTP status code.
|
||||
* @param[out] header Header fields.
|
||||
*
|
||||
* @return True if stream is parsed successfully, false if not.
|
||||
*/
|
||||
static bool parse(std::istream &stream, std::string &version, std::string &status_code, CaseInsensitiveMultimap &header) noexcept {
|
||||
std::string line;
|
||||
std::size_t version_end;
|
||||
|
|
@ -314,9 +333,9 @@ namespace SimpleWeb {
|
|||
#endif
|
||||
|
||||
namespace SimpleWeb {
|
||||
/// Makes it possible to for instance cancel Asio handlers without stopping asio::io_service
|
||||
/// Makes it possible to for instance cancel Asio handlers without stopping asio::io_service.
|
||||
class ScopeRunner {
|
||||
/// Scope count that is set to -1 if scopes are to be canceled
|
||||
/// Scope count that is set to -1 if scopes are to be canceled.
|
||||
std::atomic<long> count;
|
||||
|
||||
public:
|
||||
|
|
@ -335,7 +354,8 @@ namespace SimpleWeb {
|
|||
|
||||
ScopeRunner() noexcept : count(0) {}
|
||||
|
||||
/// Returns nullptr if scope should be exited, or a shared lock otherwise
|
||||
/// Returns nullptr if scope should be exited, or a shared lock otherwise.
|
||||
/// The shared lock ensures that a potential destructor call is delayed until all locks are released.
|
||||
std::unique_ptr<SharedLock> continue_lock() noexcept {
|
||||
long expected = count;
|
||||
while(expected >= 0 && !count.compare_exchange_weak(expected, expected + 1))
|
||||
|
|
@ -347,7 +367,7 @@ namespace SimpleWeb {
|
|||
return std::unique_ptr<SharedLock>(new SharedLock(count));
|
||||
}
|
||||
|
||||
/// Blocks until all shared locks are released, then prevents future shared locks
|
||||
/// Blocks until all shared locks are released, then prevents future shared locks.
|
||||
void stop() noexcept {
|
||||
long expected = 0;
|
||||
while(!count.compare_exchange_weak(expected, -1)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue