dodo  0.0.1
A C++ library to create containerized Linux services
httpversion.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 /**
20  * @file httpversion.hpp
21  * Defines the dodo::network::protocol::http::HTTPVersion class.
22  */
23 
24 #ifndef dodo_network_protocol_http_httpversion_hpp
25 #define dodo_network_protocol_http_httpversion_hpp
26 
28 #include <string>
29 
30 namespace dodo {
31 
32  namespace network::protocol::http {
33 
34  /**
35  * HHTP version comprising a major and minor number,
36  * convertable from and to string as in HTTP requests.
37  */
38  class HTTPVersion : public HTTPFragment {
39  public:
40  /**
41  * Default constructor to HTTP/1.0
42  */
43  HTTPVersion() : major_(1), minor_(0) {};
44 
45  /**
46  * Construct from major and minor.
47  * @param major the major to set.
48  * @param minor the minor to set.
49  */
50  explicit HTTPVersion( unsigned int major, unsigned int minor ) : major_(major), minor_(minor) {};
51 
52  /**
53  * Convert to string as 'HTTP/major_.minor_'
54  * @return the HTTP VERSION string.
55  */
56  virtual std::string asString() const {
57  std::stringstream ss;
58  ss << std::fixed << "HTTP/" << major_ << "." << minor_;
59  return ss.str();
60  }
61 
62  /**
63  * Construct from current position in the VirtualReadBuffer 'HTTP/major_.minor_'.
64  * @param buffer the VirtualReadBuffer to read from.
65  * @return the HTTPFragment::ParseError, peOk if no error.
66  */
67  virtual ParseResult parse( VirtualReadBuffer& buffer );
68 
69  /**
70  * Return the major version.
71  * @return The major version.
72  */
73  unsigned int getMajor() const { return major_; };
74 
75  /**
76  * Return the minor version.
77  * @return The minor version.
78  */
79  unsigned int getMinor() const { return minor_; };
80 
81  /**
82  * Assignment operator.
83  * @param version The version to assigne.
84  * @return This HTTPVersion.
85  */
86  HTTPVersion& operator=( const HTTPVersion& version ) { major_ = version.major_; minor_ = version.minor_; return *this; };
87 
88  /**
89  * Equality operator.
90  * @param version The HTTPVersion to compare to.
91  * @return True when equal.
92  */
93  bool operator==( const HTTPVersion& version ) const { return major_ == version.major_ && minor_ == version.minor_; };
94 
95  /**
96  * Less-than operator.
97  * @param version The HTTPVersion to compare to.
98  * @return True if this < version.
99  */
100  bool operator<( const HTTPVersion& version ) const { return major_ < version.major_ ||
101  (major_ == version.major_ && minor_ < version.minor_); };
102 
103  /**
104  * Greater than operator.
105  * @param version The HTTPVersion to compare to.
106  * @return True if this > version.
107  */
108  bool operator>( const HTTPVersion& version ) const { return major_ > version.major_ ||
109  (major_ == version.major_ && minor_ > version.minor_); };
110 
111  private:
112  /** The major version. */
113  unsigned int major_;
114  /** The minor version. */
115  unsigned int minor_;
116  };
117 
118  }
119 
120 }
121 
122 #endif
dodo::network::protocol::http::HTTPVersion::parse
virtual ParseResult parse(VirtualReadBuffer &buffer)
Construct from current position in the VirtualReadBuffer 'HTTP/major_.minor_'.
Definition: httpversion.cpp:32
dodo::network::protocol::http::HTTPVersion::HTTPVersion
HTTPVersion()
Default constructor to HTTP/1.0.
Definition: httpversion.hpp:43
dodo::network::protocol::http::HTTPVersion::HTTPVersion
HTTPVersion(unsigned int major, unsigned int minor)
Construct from major and minor.
Definition: httpversion.hpp:50
dodo::network::VirtualReadBuffer
Interface to read individual bytes whilst the implementation can read from an actual source (such as ...
Definition: socketreadbuffer.hpp:44
dodo::network::protocol::http::HTTPVersion::asString
virtual std::string asString() const
Convert to string as 'HTTP/major_.minor_'.
Definition: httpversion.hpp:56
dodo::network::protocol::http::HTTPVersion
HHTP version comprising a major and minor number, convertable from and to string as in HTTP requests.
Definition: httpversion.hpp:38
dodo::network::protocol::http::HTTPVersion::operator<
bool operator<(const HTTPVersion &version) const
Less-than operator.
Definition: httpversion.hpp:100
dodo::network::protocol::http::HTTPVersion::minor_
unsigned int minor_
The minor version.
Definition: httpversion.hpp:115
dodo::network::protocol::http::HTTPFragment
Generic HTTP fragment, either a complete (such as HTTPRequest) or incomplete HTTP fragment (ssuch as ...
Definition: httpfragment.hpp:45
dodo::network::protocol::http::HTTPVersion::getMajor
unsigned int getMajor() const
Return the major version.
Definition: httpversion.hpp:73
dodo
A C++ platform interface to lean Linux services tailored for containerized deployment.
Definition: application.hpp:29
dodo::network::protocol::http::HTTPVersion::operator==
bool operator==(const HTTPVersion &version) const
Equality operator.
Definition: httpversion.hpp:93
dodo::network::protocol::http::HTTPVersion::getMinor
unsigned int getMinor() const
Return the minor version.
Definition: httpversion.hpp:79
httpfragment.hpp
dodo::network::protocol::http::HTTPVersion::operator>
bool operator>(const HTTPVersion &version) const
Greater than operator.
Definition: httpversion.hpp:108
dodo::network::protocol::http::HTTPVersion::operator=
HTTPVersion & operator=(const HTTPVersion &version)
Assignment operator.
Definition: httpversion.hpp:86
dodo::network::protocol::http::HTTPVersion::major_
unsigned int major_
The major version.
Definition: httpversion.hpp:109