Decoupling ETags from the server id

This commit is contained in:
Veloman Yunkan 2022-10-09 21:29:38 +04:00
parent 43c8da9b04
commit a31ccb6588
3 changed files with 17 additions and 16 deletions

View File

@ -41,7 +41,7 @@ const char all_options[] = "Zz";
static_assert(ETag::OPTION_COUNT == sizeof(all_options) - 1, ""); static_assert(ETag::OPTION_COUNT == sizeof(all_options) - 1, "");
bool isValidServerId(const std::string& s) bool isValidETagBody(const std::string& s)
{ {
return !s.empty() && s.find_first_of("\"/") == std::string::npos; return !s.empty() && s.find_first_of("\"/") == std::string::npos;
} }
@ -83,17 +83,17 @@ bool ETag::get_option(Option opt) const
std::string ETag::get_etag() const std::string ETag::get_etag() const
{ {
if ( m_serverId.empty() ) if ( m_body.empty() )
return std::string(); return std::string();
return "\"" + m_serverId + "/" + m_options + "\""; return "\"" + m_body + "/" + m_options + "\"";
} }
ETag::ETag(const std::string& serverId, const std::string& options) ETag::ETag(const std::string& body, const std::string& options)
{ {
if ( isValidServerId(serverId) && isValidOptionsString(options) ) if ( isValidETagBody(body) && isValidOptionsString(options) )
{ {
m_serverId = serverId; m_body = body;
m_options = options; m_options = options;
} }
} }
@ -115,7 +115,7 @@ ETag ETag::parse(std::string s)
return ETag(s.substr(0, i), s.substr(i+1)); return ETag(s.substr(0, i), s.substr(i+1));
} }
ETag ETag::match(const std::string& etags, const std::string& server_id) ETag ETag::match(const std::string& etags, const std::string& body)
{ {
std::istringstream ss(etags); std::istringstream ss(etags);
std::string etag_str; std::string etag_str;
@ -125,7 +125,7 @@ ETag ETag::match(const std::string& etags, const std::string& server_id)
etag_str.pop_back(); etag_str.pop_back();
const ETag etag = parse(etag_str); const ETag etag = parse(etag_str);
if ( etag && etag.m_serverId == server_id ) if ( etag && etag.m_body == body )
return etag; return etag;
} }

View File

@ -28,10 +28,11 @@ namespace kiwix {
// The ETag string used by Kiwix server (more precisely, its value inside the // The ETag string used by Kiwix server (more precisely, its value inside the
// double quotes) consists of two parts: // double quotes) consists of two parts:
// //
// 1. ServerId - The string obtained on server start up // 1. Body - A string uniquely identifying the object or state from which
// the resource has been obtained.
// //
// 2. Options - Zero or more characters encoding the values of some of the // 2. Options - Zero or more characters encoding the type of the ETag and/or
// headers of the response // the values of some of the headers of the response
// //
// The two parts are separated with a slash (/) symbol (which is always present, // The two parts are separated with a slash (/) symbol (which is always present,
// even when the the options part is empty). Neither portion of a Kiwix ETag // even when the the options part is empty). Neither portion of a Kiwix ETag
@ -40,7 +41,7 @@ namespace kiwix {
// //
// "abcdefghijklmn/" // "abcdefghijklmn/"
// "1234567890/z" // "1234567890/z"
// "1234567890/cz" // "6f1d19d0-633f-087b-fb55-7ac324ff9baf/Zz"
// //
// The options part of the Kiwix ETag allows to correctly set the required // The options part of the Kiwix ETag allows to correctly set the required
// headers when responding to a conditional If-None-Match request with a 304 // headers when responding to a conditional If-None-Match request with a 304
@ -59,10 +60,10 @@ class ETag
public: // functions public: // functions
ETag() {} ETag() {}
void set_server_id(const std::string& id) { m_serverId = id; } void set_body(const std::string& s) { m_body = s; }
void set_option(Option opt); void set_option(Option opt);
explicit operator bool() const { return !m_serverId.empty(); } explicit operator bool() const { return !m_body.empty(); }
bool get_option(Option opt) const; bool get_option(Option opt) const;
std::string get_etag() const; std::string get_etag() const;
@ -76,7 +77,7 @@ class ETag
static ETag parse(std::string s); static ETag parse(std::string s);
private: // data private: // data
std::string m_serverId; std::string m_body;
std::string m_options; std::string m_options;
}; };

View File

@ -66,7 +66,7 @@ class Response {
void set_code(int code) { m_returnCode = code; } void set_code(int code) { m_returnCode = code; }
void set_kind(Kind k); void set_kind(Kind k);
void set_server_id(const std::string& id) { m_etag.set_server_id(id); } void set_server_id(const std::string& id) { m_etag.set_body(id); }
void add_header(const std::string& name, const std::string& value) { m_customHeaders[name] = value; } void add_header(const std::string& name, const std::string& value) { m_customHeaders[name] = value; }
int getReturnCode() const { return m_returnCode; } int getReturnCode() const { return m_returnCode; }