Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: Abstract string_view for easier maintenance #1184

Merged
merged 3 commits into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/net/http/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
#define HTTP_COMMON_HPP

#include <delegate>
#include <experimental/string_view>
#include <memory>
#include <uri>
#include <utility>
#include <vector>

#include "../../util/detail/string_view"

namespace http {

using URI = uri::URI;
Expand Down
46 changes: 24 additions & 22 deletions api/net/http/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
#include <ostream>
#include <type_traits>

#include "header_fields.hpp"
#include "common.hpp"
#include "header_fields.hpp"

#include "../../util/detail/string_view"

namespace http {

Expand Down Expand Up @@ -118,7 +120,7 @@ class Header {
/// @return true if the field is a member,
/// false otherwise
///
bool has_field(const std::experimental::string_view field) const noexcept;
bool has_field(util::csview field) const noexcept;

///
/// Get the value associated with a field
Expand All @@ -129,7 +131,7 @@ class Header {
/// view otherwise
///
///
std::experimental::string_view value(const std::experimental::string_view field) const noexcept;
util::sview value(util::csview field) const noexcept;

///
/// Check to see if the set is empty
Expand All @@ -154,31 +156,31 @@ class Header {
///
/// @param field The field name to remove
///
void erase(const std::experimental::string_view field) noexcept;
void erase(util::csview field) noexcept;

///
/// Remove all fields from the set leaving it
/// empty
///
void clear() noexcept;

/**
* @brief Returns the Content-Length value in header as an integer
*
* @note Return value 0 can mean its either unset or zero.
*
* @return The content length as integer
*/
size_t content_length() const;

/**
* @brief Sets the Content-Length value in the header
*
* @param[in] len The length of the content
*
* @return Outcome of wether the field got updated or not
*/
bool set_content_length(size_t len);
///
/// Returns the Content-Length value in header as an integer
///
/// @note Return value 0 can mean its either unset or zero.
///
/// @return The content length as integer
///
size_t content_length() const noexcept;

///
/// Sets the Content-Length value in the header
///
/// @param len The length of the content
///
/// @return Outcome of whether the field got updated or not
///
bool set_content_length(const size_t len);

private:
///
Expand All @@ -194,7 +196,7 @@ class Header {
/// @return Iterator to the location of the field,
/// else location to the end of the sequence
///
Const_iterator find(const std::experimental::string_view field) const noexcept;
Const_iterator find(util::csview field) const noexcept;

///
/// Operator to stream the contents of the set
Expand Down
12 changes: 6 additions & 6 deletions api/net/http/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Message {
///
/// @return A view of the entity in this message
///
std::experimental::string_view body() const noexcept;
util::sview body() const noexcept;

///
/// Remove the entity from the message
Expand Down Expand Up @@ -172,7 +172,7 @@ class Message {
///
/// @return A view of a buffer holding intermediate information
///
const std::experimental::string_view private_field() const noexcept;
util::sview private_field() const noexcept;

///
/// Set the content of the buffer holding intermediate information
Expand All @@ -182,9 +182,9 @@ class Message {
///
/// Class data members
///
Header header_fields_;
Message_body message_body_;
std::experimental::string_view field_;
Header header_fields_;
Message_body message_body_;
util::sview field_;
}; //< class Message

/**--v----------- Helper Functions -----------v--**/
Expand All @@ -201,7 +201,7 @@ inline size_t Message::content_length() const {
return header_fields_.content_length();
}

inline bool Message::set_content_length(size_t len) {
inline bool Message::set_content_length(const size_t len) {
return header_fields_.set_content_length(len);
}
/**--^-------- Inline Implementations --------^--**/
Expand Down
11 changes: 6 additions & 5 deletions api/net/http/methods.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
#define HTTP_METHODS_HPP

#include <array>
#include <experimental/string_view>
#include <ostream>
#include <unordered_map>

#include "../../util/detail/string_view"

namespace http {

///
Expand All @@ -44,9 +45,9 @@ namespace http {
/// @return The string representation of the code
///
template<typename = void>
std::experimental::string_view str(const Method m) noexcept {
util::sview str(const Method m) noexcept {

const static std::array<std::experimental::string_view, 10> views
const static std::array<util::sview, 10> views
{
{
"GET", "POST", "PUT", "DELETE", "OPTIONS",
Expand All @@ -71,9 +72,9 @@ namespace http {
/// @return The code mapped to the method string representation
///
template<typename = void>
Method code(const std::experimental::string_view method) noexcept {
Method code(util::csview method) noexcept {

const static std::unordered_map<std::experimental::string_view, Method> code_map {
const static std::unordered_map<util::sview, Method> code_map {
{"GET", GET},
{"POST", POST},
{"PUT", PUT},
Expand Down
4 changes: 2 additions & 2 deletions api/net/http/mime_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef HTTP_MIME_TYPES_HPP
#define HTTP_MIME_TYPES_HPP

#include <experimental/string_view>
#include "../../util/detail/string_view"

namespace http {

Expand All @@ -29,7 +29,7 @@ namespace http {
///
/// @return The associated mime type for the specified extension
///
std::experimental::string_view ext_to_mime_type(const std::experimental::string_view extension) noexcept;
util::sview ext_to_mime_type(util::csview extension) noexcept;

} //< namespace http

Expand Down
12 changes: 6 additions & 6 deletions api/net/http/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class Request : public Message {
/// an empty string otherwise
///
template<typename = void>
std::experimental::string_view query_value(const std::experimental::string_view name) noexcept;
util::sview query_value(util::csview name) noexcept;

///
/// Get the value associated with the name
Expand All @@ -168,7 +168,7 @@ class Request : public Message {
/// an empty string otherwise
///
template<typename = void>
std::experimental::string_view post_value(const std::experimental::string_view name) const noexcept;
util::sview post_value(util::csview name) const noexcept;

///
/// Reset the request message as if it was now
Expand Down Expand Up @@ -226,28 +226,28 @@ class Request : public Message {

///////////////////////////////////////////////////////////////////////////////
template<typename>
inline std::experimental::string_view Request::query_value(const std::experimental::string_view name) noexcept {
inline util::sview Request::query_value(util::csview name) noexcept {
return uri_.query(name.to_string());
}

///////////////////////////////////////////////////////////////////////////////
template<typename>
inline std::experimental::string_view Request::post_value(const std::experimental::string_view name) const noexcept {
inline util::sview Request::post_value(util::csview name) const noexcept {
if ((method() not_eq POST) or name.empty() or body().empty()) {
return {};
}
//---------------------------------
const auto target = body().find(name);
//---------------------------------
if (target == std::experimental::string_view::npos) return {};
if (target == util::sview::npos) return {};
//---------------------------------
auto focal_point = body().substr(target);
//---------------------------------
focal_point = focal_point.substr(0, focal_point.find_first_of('&'));
//---------------------------------
const auto lock_and_load = focal_point.find('=');
//---------------------------------
if (lock_and_load == std::experimental::string_view::npos) return {};
if (lock_and_load == util::sview::npos) return {};
//---------------------------------
return focal_point.substr(lock_and_load + 1);
}
Expand Down
3 changes: 1 addition & 2 deletions api/net/http/response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ namespace http {
/// This class is used to represent an error that occurred
/// from within the operations of class Response
///
class Response_error : public std::runtime_error {
public:
struct Response_error : public std::runtime_error {
using runtime_error::runtime_error;
}; //< class Response_error

Expand Down
5 changes: 2 additions & 3 deletions api/net/http/status_codes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
#ifndef HTTP_STATUS_CODES_HPP
#define HTTP_STATUS_CODES_HPP

#include <experimental/string_view>

#include "status_code_constants.hpp"
#include "../../util/detail/string_view"

namespace http {

std::experimental::string_view code_description(const status_t status_code) noexcept;
util::sview code_description(const status_t status_code) noexcept;

template<typename = void>
inline bool is_informational(const status_t status_code) noexcept {
Expand Down
6 changes: 3 additions & 3 deletions api/net/http/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
#define HTTP_TIME_HPP

#include <ctime>
#include <experimental/string_view>
#include <iomanip>
#include <sstream>
#include <string>

#include "../../util/detail/string_view"

namespace http {
namespace time {
Expand All @@ -49,7 +49,7 @@ std::string from_time_t(const std::time_t time_);
///
/// @note Returns a default initialized {time_t} object if an error occurred
///
std::time_t to_time_t(const std::experimental::string_view time_);
std::time_t to_time_t(util::csview time_);

///
/// Get the current time in {Internet Standard Format}
Expand Down
1 change: 0 additions & 1 deletion api/net/http/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#ifndef HTTP_VERSION_HPP
#define HTTP_VERSION_HPP

#include <string>
#include <sstream>

namespace http {
Expand Down
30 changes: 12 additions & 18 deletions src/net/http/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ bool Header::set_field(std::string field, std::string value) {
}

///////////////////////////////////////////////////////////////////////////////
bool Header::has_field(const std::experimental::string_view field) const noexcept {
bool Header::has_field(util::csview field) const noexcept {
return find(field) not_eq fields_.cend();
}

///////////////////////////////////////////////////////////////////////////////
std::experimental::string_view Header::value(const std::experimental::string_view field) const noexcept {
util::sview Header::value(util::csview field) const noexcept {
if (field.empty()) return field;
const auto it = find(field);
return (it not_eq fields_.cend()) ? it->second : std::experimental::string_view{};
return (it not_eq fields_.cend()) ? it->second : util::sview{};
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -78,9 +78,8 @@ std::size_t Header::size() const noexcept {
}

///////////////////////////////////////////////////////////////////////////////
void Header::erase(const std::experimental::string_view field) noexcept {
void Header::erase(util::csview field) noexcept {
Const_iterator target;
//-----------------------------------
while ((target = find(field)) not_eq fields_.cend()) fields_.erase(target);
}

Expand All @@ -89,29 +88,24 @@ void Header::clear() noexcept {
fields_.clear();
}

size_t Header::content_length() const
{
try
{
///////////////////////////////////////////////////////////////////////////////
size_t Header::content_length() const noexcept {
try {
const auto cl = value(header::Content_Length);

if(cl.empty()) return 0;

return std::stoul(cl.to_string());
}
catch(...)
{
return std::stoul(std::string{cl.data(), cl.length()});
} catch(...) {
return 0;
}
}

bool Header::set_content_length(size_t len)
{
///////////////////////////////////////////////////////////////////////////////
bool Header::set_content_length(const size_t len) {
return set_field(header::Content_Length, std::to_string(len));
}

///////////////////////////////////////////////////////////////////////////////
Header::Const_iterator Header::find(const std::experimental::string_view field) const noexcept {
Header::Const_iterator Header::find(util::csview field) const noexcept {
if (field.empty()) return fields_.cend();
//-----------------------------------
return
Expand Down