join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
http_message.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_SERVICES_HTTP_MESSAGE_HPP
26#define JOIN_SERVICES_HTTP_MESSAGE_HPP
27
28// libjoin.
29#include <join/error.hpp>
30#include <join/utils.hpp>
31
32// C++.
33#include <system_error>
34#include <iostream>
35#include <string>
36#include <map>
37
38namespace join
39{
58
62 class HttpCategory : public std::error_category
63 {
64 public:
69 virtual const char* name () const noexcept;
70
76 virtual std::string message (int code) const;
77
84 virtual bool equivalent (const std::error_code& code, int condition) const noexcept;
85 };
86
91 const std::error_category& httpCategory ();
92
98 std::error_code make_error_code (HttpErrc code);
99
105 std::error_condition make_error_condition (HttpErrc code);
106
111 {
112 Head = 1L << 0,
113 Get = 1L << 1,
114 Put = 1L << 2,
115 Post = 1L << 3,
117 Delete = 1L << 4,
118 };
119
127 {
128 return HttpMethod (static_cast<int> (__a) & static_cast<int> (__b));
129 }
130
138 {
139 return HttpMethod (static_cast<int> (__a) | static_cast<int> (__b));
140 }
141
149 {
150 return HttpMethod (static_cast<int> (__a) ^ static_cast<int> (__b));
151 }
152
159 {
160 return HttpMethod (~static_cast<int> (__a));
161 }
162
170 {
171 return __a = __a & __b;
172 }
173
181 {
182 return __a = __a | __b;
183 }
184
192 {
193 return __a = __a ^ __b;
194 }
195
200 {
201 public:
202 // headers map.
203 using HeaderMap = std::map<std::string, std::string, details::lessNoCase>;
204
208 HttpMessage ();
209
214 HttpMessage (const HttpMessage& other);
215
221 HttpMessage& operator= (const HttpMessage& other);
222
227 HttpMessage (HttpMessage&& other);
228
235
239 virtual ~HttpMessage () = default;
240
245 const std::string& version () const;
246
251 void version (const std::string& v);
252
258 bool hasHeader (const std::string& name) const;
259
265 std::string header (const std::string& name) const;
266
272 void header (const std::string& name, const std::string& val);
273
278 void header (const HeaderMap::value_type& h);
279
284 const HeaderMap& headers () const;
285
290 void headers (const HeaderMap& heads);
291
296 std::string dumpHeaders () const;
297
302 size_t contentLength () const;
303
307 virtual void clear ();
308
314 virtual int readHeaders (std::istream& in);
315
321 virtual int writeHeaders (std::ostream& out) const = 0;
322
323 protected:
329 virtual int parseFirstLine (const std::string& line) = 0;
330
336 virtual int parseHeader (const std::string& head);
337
339 static const std::streamsize _maxHeaderLen = 2048;
340
342 std::string _version;
343
346 };
347
352 {
353 public:
354 // parameters map.
355 using ParameterMap = std::map<std::string, std::string>;
356
360 HttpRequest ();
361
367
372 HttpRequest (const HttpRequest& other);
373
379 HttpRequest& operator= (const HttpRequest& other);
380
385 HttpRequest (HttpRequest&& other);
386
393
397 virtual ~HttpRequest () = default;
398
403 HttpMethod method () const;
404
409 std::string methodString () const;
410
415 void method (HttpMethod meth);
416
421 const std::string& path () const;
422
427 void path (const std::string& p);
428
434 bool hasParameter (const std::string& name) const;
435
441 std::string parameter (const std::string& name) const;
442
448 void parameter (const std::string& name, const std::string& val);
449
454 void parameter (const ParameterMap::value_type& param);
455
460 const ParameterMap& parameters () const;
461
466 void parameters (const ParameterMap& params);
467
472 std::string dumpParameters () const;
473
478 std::string query () const;
479
484 std::string urn () const;
485
490 std::string host () const;
491
496 std::string auth () const;
497
502 std::string credentials () const;
503
507 virtual void clear () override;
508
514 virtual int writeHeaders (std::ostream& out) const override;
515
516 protected:
522 virtual int parseFirstLine (const std::string& line) override;
523
529 std::string& decodeUrl (std::string& url);
530
536 std::string& normalize (std::string& path);
537
542 void store (const std::string& query);
543
546
548 std::string _path;
549
552 };
553
558 {
559 public:
563 HttpResponse () = default;
564
569 HttpResponse (const HttpResponse& other);
570
576 HttpResponse& operator= (const HttpResponse& other);
577
582 HttpResponse (HttpResponse&& other);
583
590
594 virtual ~HttpResponse () = default;
595
600 const std::string& status () const;
601
606 const std::string& reason () const;
607
613 void response (const std::string& status, const std::string& reason = {});
614
618 virtual void clear () override;
619
625 virtual int writeHeaders (std::ostream& out) const override;
626
627 protected:
633 virtual int parseFirstLine (const std::string& line) override;
634
636 std::string _status;
637
639 std::string _reason;
640 };
641}
642
643namespace std
644{
646 template <>
647 struct is_error_condition_enum<join::HttpErrc> : public true_type
648 {
649 };
650}
651
652#endif
HTTP API generic error category.
Definition http_message.hpp:63
virtual std::string message(int code) const
translate HTTP API generic error code to human readable error string.
Definition http_message.cpp:49
virtual const char * name() const noexcept
get HTTP API generic error category name.
Definition http_message.cpp:40
virtual bool equivalent(const std::error_code &code, int condition) const noexcept
find equivalent from Errc to system error code.
Definition http_message.cpp:86
HTTP message.
Definition http_message.hpp:200
virtual int parseHeader(const std::string &head)
parse HTTP header.
Definition http_message.cpp:344
size_t contentLength() const
get content length.
Definition http_message.cpp:283
HeaderMap _headers
HTTP headers.
Definition http_message.hpp:345
HttpMessage & operator=(const HttpMessage &other)
assign the HttpMessage instance by copy.
Definition http_message.cpp:148
const std::string & version() const
get HTTP version.
Definition http_message.cpp:185
std::map< std::string, std::string, details::lessNoCase > HeaderMap
Definition http_message.hpp:203
std::string _version
HTTP version.
Definition http_message.hpp:342
virtual int readHeaders(std::istream &in)
read HTTP header from the given input stream.
Definition http_message.cpp:304
virtual int writeHeaders(std::ostream &out) const =0
write HTTP headers to the given output stream.
virtual ~HttpMessage()=default
destroy the HttpMessage instance.
virtual void clear()
clear HTTP message.
Definition http_message.cpp:294
virtual int parseFirstLine(const std::string &line)=0
parse first line.
std::string dumpHeaders() const
dump headers.
Definition http_message.cpp:266
HttpMessage()
create the HttpMessage instance.
Definition http_message.cpp:129
static const std::streamsize _maxHeaderLen
HTTP max header size.
Definition http_message.hpp:339
bool hasHeader(const std::string &name) const
checks if there is a header with the specified name.
Definition http_message.cpp:203
std::string header(const std::string &name) const
get header by name.
Definition http_message.cpp:212
const HeaderMap & headers() const
get headers map.
Definition http_message.cpp:245
HTTP request.
Definition http_message.hpp:352
std::string parameter(const std::string &name) const
get a parameter by name.
Definition http_message.cpp:510
const ParameterMap & parameters() const
get query parameters map.
Definition http_message.cpp:543
HttpMethod _method
HTTP method.
Definition http_message.hpp:545
const std::string & path() const
get path.
Definition http_message.cpp:483
virtual int writeHeaders(std::ostream &out) const override
write HTTP headers to the given output stream.
Definition http_message.cpp:666
std::string dumpParameters() const
dump parameters.
Definition http_message.cpp:564
std::string & normalize(std::string &path)
produce a normalized path (collapse duplicated separator and remove dot segment).
Definition http_message.cpp:794
HttpRequest()
create the HttpRequest instance by default.
Definition http_message.cpp:362
std::string urn() const
get URN.
Definition http_message.cpp:601
std::string credentials() const
get credentials.
Definition http_message.cpp:639
HttpMethod method() const
get request method.
Definition http_message.cpp:442
virtual void clear() override
clear HTTP message.
Definition http_message.cpp:654
bool hasParameter(const std::string &name) const
checks if there is a parameter with the specified name.
Definition http_message.cpp:501
virtual ~HttpRequest()=default
destroy the HttpRequest instance.
std::string host() const
get host.
Definition http_message.cpp:610
virtual int parseFirstLine(const std::string &line) override
parse first line.
Definition http_message.cpp:677
std::string auth() const
get autorization type.
Definition http_message.cpp:629
HttpRequest & operator=(const HttpRequest &other)
assign the HttpRequest instance by copy.
Definition http_message.cpp:395
std::map< std::string, std::string > ParameterMap
Definition http_message.hpp:355
ParameterMap _parameters
HTTP query parameters.
Definition http_message.hpp:551
std::string _path
HTTP path;.
Definition http_message.hpp:548
std::string methodString() const
get request method string.
Definition http_message.cpp:451
void store(const std::string &query)
store parameters received in request.
Definition http_message.cpp:854
std::string & decodeUrl(std::string &url)
decode url (ex. %20 ==> ' ').
Definition http_message.cpp:742
std::string query() const
get query.
Definition http_message.cpp:585
HTTP response.
Definition http_message.hpp:558
virtual int parseFirstLine(const std::string &line) override
parse first line.
Definition http_message.cpp:993
HttpResponse()=default
create the HttpResponse instance.
std::string _reason
HTTP reason.
Definition http_message.hpp:639
void response(const std::string &status, const std::string &reason={})
set HTTP response status.
Definition http_message.cpp:961
HttpResponse & operator=(const HttpResponse &other)
assign the HttpResponse instance by copy.
Definition http_message.cpp:904
virtual ~HttpResponse()=default
destroy the HttpResponse instance.
const std::string & reason() const
get HTTP response reason.
Definition http_message.cpp:952
virtual int writeHeaders(std::ostream &out) const override
write HTTP headers to the given output stream.
Definition http_message.cpp:982
virtual void clear() override
clear HTTP message.
Definition http_message.cpp:971
const std::string & status() const
get HTTP response status.
Definition http_message.cpp:943
std::string _status
HTTP status.
Definition http_message.hpp:636
Definition acceptor.hpp:32
HttpMethod
enumeration of HTTP methods.
Definition http_message.hpp:111
@ Post
Definition http_message.hpp:115
@ Put
Definition http_message.hpp:114
@ Delete
Definition http_message.hpp:117
@ Head
Definition http_message.hpp:112
@ Get
Definition http_message.hpp:113
IpAddress operator^(const IpAddress &a, const IpAddress &b)
perform XOR operation on IP address.
Definition ip_address.cpp:1719
constexpr const JsonReadMode & operator&=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary AND on JsonReadMode.
Definition json.hpp:919
IpAddress operator&(const IpAddress &a, const IpAddress &b)
perform AND operation on IP address.
Definition ip_address.cpp:1665
InterfaceChangeType operator~(InterfaceChangeType a)
perform binary NOT on InterfaceChangeType.
Definition interface_manager.hpp:95
constexpr const JsonReadMode & operator|=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary OR on JsonReadMode.
Definition json.hpp:930
HttpErrc
HTTP API generic error codes.
Definition http_message.hpp:44
const InterfaceChangeType & operator^=(InterfaceChangeType &a, InterfaceChangeType b)
perform binary XOR assignment on InterfaceChangeType.
Definition interface_manager.hpp:128
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ip_address.cpp:1692
std::error_condition make_error_condition(join::Errc code) noexcept
Create an std::error_condition object.
Definition error.cpp:204
const std::error_category & httpCategory()
get error category.
Definition http_message.cpp:101
Definition error.hpp:144