join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
httpmessage.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_SERVICES_HTTPMESSAGE_HPP
26#define JOIN_SERVICES_HTTPMESSAGE_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 <sstream>
36#include <string>
37#include <map>
38
39namespace join
40{
59
63 class HttpCategory : public std::error_category
64 {
65 public:
70 virtual const char* name () const noexcept;
71
77 virtual std::string message (int code) const;
78
85 virtual bool equivalent (const std::error_code& code, int condition) const noexcept;
86 };
87
92 const std::error_category& httpCategory ();
93
99 std::error_code make_error_code (HttpErrc code);
100
106 std::error_condition make_error_condition (HttpErrc code);
107
112 {
113 Head = 1L << 0,
114 Get = 1L << 1,
115 Put = 1L << 2,
116 Post = 1L << 3,
118 Delete = 1L << 4,
119 };
120
128 {
129 return HttpMethod (static_cast<int> (__a) & static_cast<int> (__b));
130 }
131
139 {
140 return HttpMethod (static_cast<int> (__a) | static_cast<int> (__b));
141 }
142
150 {
151 return HttpMethod (static_cast<int> (__a) ^ static_cast<int> (__b));
152 }
153
160 {
161 return HttpMethod (~static_cast<int> (__a));
162 }
163
171 {
172 return __a = __a & __b;
173 }
174
182 {
183 return __a = __a | __b;
184 }
185
193 {
194 return __a = __a ^ __b;
195 }
196
201 {
202 public:
203 // headers map.
204 using HeaderMap = std::map<std::string, std::string, details::lessNoCase>;
205
209 HttpMessage ();
210
215 HttpMessage (const HttpMessage& other);
216
222 HttpMessage& operator= (const HttpMessage& other);
223
228 HttpMessage (HttpMessage&& other);
229
236
240 virtual ~HttpMessage () = default;
241
246 const std::string& version () const;
247
252 void version (const std::string& v);
253
259 bool hasHeader (const std::string& name) const;
260
266 std::string header (const std::string& name) const;
267
273 void header (const std::string& name, const std::string& val);
274
279 void header (const HeaderMap::value_type& h);
280
285 const HeaderMap& headers () const;
286
291 void headers (const HeaderMap& heads);
292
297 std::string dumpHeaders () const;
298
303 size_t contentLength () const;
304
308 virtual void clear ();
309
315 virtual int readHeaders (std::istream& in);
316
322 virtual int writeHeaders (std::ostream& out) const = 0;
323
324 protected:
330 virtual int parseFirstLine (const std::string& line) = 0;
331
337 virtual int parseHeader (const std::string& head);
338
340 static const std::streamsize _maxHeaderLen = 2048;
341
343 std::string _version;
344
347 };
348
353 {
354 public:
355 // parameters map.
356 using ParameterMap = std::map<std::string, std::string>;
357
361 HttpRequest ();
362
368
373 HttpRequest (const HttpRequest& other);
374
380 HttpRequest& operator= (const HttpRequest& other);
381
386 HttpRequest (HttpRequest&& other);
387
394
398 virtual ~HttpRequest () = default;
399
404 HttpMethod method () const;
405
410 std::string methodString () const;
411
416 void method (HttpMethod meth);
417
422 const std::string& path () const;
423
428 void path (const std::string& p);
429
435 bool hasParameter (const std::string& name) const;
436
442 std::string parameter (const std::string& name) const;
443
449 void parameter (const std::string& name, const std::string& val);
450
455 void parameter (const ParameterMap::value_type& param);
456
461 const ParameterMap& parameters () const;
462
467 void parameters (const ParameterMap& params);
468
473 std::string dumpParameters () const;
474
479 std::string query () const;
480
485 std::string urn () const;
486
491 std::string host () const;
492
497 std::string auth () const;
498
503 std::string credentials () const;
504
508 virtual void clear () override;
509
515 virtual int writeHeaders (std::ostream& out) const override;
516
517 protected:
523 virtual int parseFirstLine (const std::string& line) override;
524
530 std::string& decodeUrl (std::string& url);
531
537 std::string& normalize (std::string& path);
538
543 void store (const std::string& query);
544
547
549 std::string _path;
550
553 };
554
559 {
560 public:
564 HttpResponse () = default;
565
570 HttpResponse (const HttpResponse& other);
571
577 HttpResponse& operator= (const HttpResponse& other);
578
583 HttpResponse (HttpResponse&& other);
584
591
595 virtual ~HttpResponse () = default;
596
601 const std::string& status () const;
602
607 const std::string& reason () const;
608
614 void response (const std::string& status, const std::string& reason = {});
615
619 virtual void clear () override;
620
626 virtual int writeHeaders (std::ostream& out) const override;
627
628 protected:
634 virtual int parseFirstLine (const std::string& line) override;
635
637 std::string _status;
638
640 std::string _reason;
641 };
642}
643
644namespace std
645{
647 template <>
648 struct is_error_condition_enum<join::HttpErrc> : public true_type
649 {
650 };
651}
652
653#endif
HTTP API generic error category.
Definition httpmessage.hpp:64
virtual std::string message(int code) const
translate HTTP API generic error code to human readable error string.
Definition httpmessage.cpp:49
virtual const char * name() const noexcept
get HTTP API generic error category name.
Definition httpmessage.cpp:40
virtual bool equivalent(const std::error_code &code, int condition) const noexcept
find equivalent from Errc to system error code.
Definition httpmessage.cpp:86
HTTP message.
Definition httpmessage.hpp:201
virtual int parseHeader(const std::string &head)
parse HTTP header.
Definition httpmessage.cpp:344
size_t contentLength() const
get content length.
Definition httpmessage.cpp:283
HeaderMap _headers
HTTP headers.
Definition httpmessage.hpp:346
HttpMessage & operator=(const HttpMessage &other)
assign the HttpMessage instance by copy.
Definition httpmessage.cpp:148
const std::string & version() const
get HTTP version.
Definition httpmessage.cpp:185
std::map< std::string, std::string, details::lessNoCase > HeaderMap
Definition httpmessage.hpp:204
std::string _version
HTTP version.
Definition httpmessage.hpp:343
virtual int readHeaders(std::istream &in)
read HTTP header from the given input stream.
Definition httpmessage.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 httpmessage.cpp:294
virtual int parseFirstLine(const std::string &line)=0
parse first line.
std::string dumpHeaders() const
dump headers.
Definition httpmessage.cpp:266
HttpMessage()
create the HttpMessage instance.
Definition httpmessage.cpp:129
static const std::streamsize _maxHeaderLen
HTTP max header size.
Definition httpmessage.hpp:340
bool hasHeader(const std::string &name) const
checks if there is a header with the specified name.
Definition httpmessage.cpp:203
std::string header(const std::string &name) const
get header by name.
Definition httpmessage.cpp:212
const HeaderMap & headers() const
get headers map.
Definition httpmessage.cpp:245
HTTP request.
Definition httpmessage.hpp:353
std::string parameter(const std::string &name) const
get a parameter by name.
Definition httpmessage.cpp:510
const ParameterMap & parameters() const
get query parameters map.
Definition httpmessage.cpp:543
HttpMethod _method
HTTP method.
Definition httpmessage.hpp:546
const std::string & path() const
get path.
Definition httpmessage.cpp:483
virtual int writeHeaders(std::ostream &out) const override
write HTTP headers to the given output stream.
Definition httpmessage.cpp:666
std::string dumpParameters() const
dump parameters.
Definition httpmessage.cpp:564
std::string & normalize(std::string &path)
produce a normalized path (collapse duplicated separator and remove dot segment).
Definition httpmessage.cpp:794
HttpRequest()
create the HttpRequest instance by default.
Definition httpmessage.cpp:362
std::string urn() const
get URN.
Definition httpmessage.cpp:601
std::string credentials() const
get credentials.
Definition httpmessage.cpp:639
HttpMethod method() const
get request method.
Definition httpmessage.cpp:442
virtual void clear() override
clear HTTP message.
Definition httpmessage.cpp:654
bool hasParameter(const std::string &name) const
checks if there is a parameter with the specified name.
Definition httpmessage.cpp:501
virtual ~HttpRequest()=default
destroy the HttpRequest instance.
std::string host() const
get host.
Definition httpmessage.cpp:610
virtual int parseFirstLine(const std::string &line) override
parse first line.
Definition httpmessage.cpp:677
std::string auth() const
get autorization type.
Definition httpmessage.cpp:629
HttpRequest & operator=(const HttpRequest &other)
assign the HttpRequest instance by copy.
Definition httpmessage.cpp:395
std::map< std::string, std::string > ParameterMap
Definition httpmessage.hpp:356
ParameterMap _parameters
HTTP query parameters.
Definition httpmessage.hpp:552
std::string _path
HTTP path;.
Definition httpmessage.hpp:549
std::string methodString() const
get request method string.
Definition httpmessage.cpp:451
void store(const std::string &query)
store parameters received in request.
Definition httpmessage.cpp:854
std::string & decodeUrl(std::string &url)
decode url (ex. %20 ==> ' ').
Definition httpmessage.cpp:742
std::string query() const
get query.
Definition httpmessage.cpp:585
HTTP response.
Definition httpmessage.hpp:559
virtual int parseFirstLine(const std::string &line) override
parse first line.
Definition httpmessage.cpp:993
HttpResponse()=default
create the HttpResponse instance.
std::string _reason
HTTP reason.
Definition httpmessage.hpp:640
void response(const std::string &status, const std::string &reason={})
set HTTP response status.
Definition httpmessage.cpp:961
HttpResponse & operator=(const HttpResponse &other)
assign the HttpResponse instance by copy.
Definition httpmessage.cpp:904
virtual ~HttpResponse()=default
destroy the HttpResponse instance.
const std::string & reason() const
get HTTP response reason.
Definition httpmessage.cpp:952
virtual int writeHeaders(std::ostream &out) const override
write HTTP headers to the given output stream.
Definition httpmessage.cpp:982
virtual void clear() override
clear HTTP message.
Definition httpmessage.cpp:971
const std::string & status() const
get HTTP response status.
Definition httpmessage.cpp:943
std::string _status
HTTP status.
Definition httpmessage.hpp:637
Definition acceptor.hpp:32
HttpMethod
enumeration of HTTP methods.
Definition httpmessage.hpp:112
@ Post
Definition httpmessage.hpp:116
@ Put
Definition httpmessage.hpp:115
@ Delete
Definition httpmessage.hpp:118
@ Head
Definition httpmessage.hpp:113
@ Get
Definition httpmessage.hpp:114
IpAddress operator^(const IpAddress &a, const IpAddress &b)
perform XOR operation on IP address.
Definition ipaddress.cpp:1719
constexpr const JsonReadMode & operator&=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary AND on JsonReadMode.
Definition json.hpp:916
IpAddress operator&(const IpAddress &a, const IpAddress &b)
perform AND operation on IP address.
Definition ipaddress.cpp:1665
InterfaceChangeType operator~(InterfaceChangeType a)
perform binary NOT on InterfaceChangeType.
Definition interfacemanager.hpp:95
constexpr const JsonReadMode & operator|=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary OR on JsonReadMode.
Definition json.hpp:927
HttpErrc
HTTP API generic error codes.
Definition httpmessage.hpp:45
const InterfaceChangeType & operator^=(InterfaceChangeType &a, InterfaceChangeType b)
perform binary XOR on InterfaceChangeType.
Definition interfacemanager.hpp:128
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:150
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ipaddress.cpp:1692
std::error_condition make_error_condition(join::Errc code) noexcept
Create an std::error_condition object.
Definition error.cpp:159
const std::error_category & httpCategory()
get error category.
Definition httpmessage.cpp:101
Definition error.hpp:137