iFun Engine API  1.0.0-b2601
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
http_util.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 
9 #ifndef INCLUDE_FUNAPI_NETWORK_HTTP_UTIL_H_
10 #define INCLUDE_FUNAPI_NETWORK_HTTP_UTIL_H_
11 
12 #include <boost/algorithm/string/predicate.hpp>
13 #include <funapi/types.h>
14 #include <funapi/utility/misc.h>
15 
16 #include <map>
17 
18 
19 namespace fun {
20 namespace http {
21 
23 typedef std::multimap<string, string, impl::util::CaseInsensitiveLess> Header;
24 
26 typedef std::map<string, string, impl::util::CaseInsensitiveLess> GetParameter;
27 
29 typedef std::map<string, std::vector<string>,
30  impl::util::CaseInsensitiveLess> GetParameter2;
31 
34 enum Method {
35  kUnknownMethod = 0,
36  kGet,
37  kHead,
38  kPost,
39  kPut,
40  kDelete,
41  kTrace
42 };
43 
44 
46 enum Version {
47  kUnknownVersion = 0,
48  kHttp0,
49  kHttp1
50 };
51 
52 
54 enum Protocol {
55  kHttp = 0,
56  kHttps
57 };
58 
59 
62 enum StatusCode {
63  kContinue = 100,
64  kSwitchingProtocols = 101,
65  kOk = 200,
66  kCreated = 201,
67  kAccepted = 202,
68  kNonAuthoriativeInformation = 203,
69  kNoContent = 204,
70  kResetContent = 205,
71  kPartialContent = 206,
72  kMultipleChoices = 300,
73  kMovedPermanently = 301,
74  kFound = 302,
75  kSeeOther = 303,
76  kNotModified = 304,
77  kUseProxy = 305,
78  kTemporaryRedirect = 307,
79  kBadRequest = 400,
80  kUnauthorized = 401,
81  kPaymentRequired = 402,
82  kForbidden = 403,
83  kNotFound = 404,
84  kMethodNotAllowed = 405,
85  kNotAcceptable = 406,
86  kProxyAuthenticationRequired = 407,
87  kRequestTimeout = 408,
88  kConflict = 409,
89  kGone = 410,
90  kLengthRequired = 411,
91  kPreconditionFailed = 412,
92  kRequestEntityTooLarge = 413,
93  kRequestUriTooLong = 414,
94  kUnsupportedMediaType = 415,
95  kRequestRangeNotSatisfiable = 416,
96  kExpectationFailed = 417,
97  kTooManyRequests = 429,
98  kInternalServerError = 500,
99  kNotImplemented = 501,
100  kBadGateway = 502,
101  kServiceUnavailable = 503,
102  kGatewayTimeout = 504,
103  kHttpVersionNotSupported = 505
104 };
105 
106 
108 struct Request {
109  DECLARE_CLASS_PTR(Request);
110 
111  Method method;
112  string request_uri;
113  Version version;
114  GetParameter get_parameter;
115  Header header;
116  string body;
117 };
118 
119 
121 struct Request2 {
122  DECLARE_CLASS_PTR(Request);
123 
124  Method method;
125  string request_uri;
126  Version version;
127  GetParameter2 get_parameter;
128  Header header;
129  string body;
130 };
131 
132 
134 struct Response {
135  DECLARE_CLASS_PTR(Response);
136 
137  Version version;
138  uint32_t status_code;
139  string status_message;
140  Header header;
141  string body;
142 };
143 
144 
158 class Uri {
159  public:
160 
162  Uri();
163 
166  explicit Uri(const string &uri_string);
167 
169  Uri(const Uri &rhs);
170 
173  bool Parse(const string &uri_string);
174 
177  const string &protocol() const;
178 
181  const string &host() const;
182 
185  uint16_t port() const;
186 
189  const string &path() const;
190 
193  const string &query_string() const;
194 
197  const string &raw() const;
198 
199  private:
200  void Reset();
201 
202  string protocol_;
203  string host_;
204  uint16_t port_;
205  string path_;
206  string query_string_;
207  string url_string_;
208 };
209 
210 
214 const string &GetMethodString(const Method &method);
215 
219 const string &GetStatusCodeString(int status_code);
220 
225 bool Escape(const string &plain, string *out);
226 
231 bool Unescape(const string &url_encoded, string *out);
232 
233 } // namespace http
234 } // namespace fun
235 
236 #endif // INCLUDE_FUNAPI_NETWORK_HTTP_UTIL_H_
Data structure to hold HTTP request.
Definition: http_util.h:121
Method method
HTTP method.
Definition: http_util.h:111
Header header
header fields
Definition: http_util.h:128
string body
request body
Definition: http_util.h:129
const string & raw() const
Returns the original URI.
const string & protocol() const
Returns protocol of a given URI.
Version version
HTTP version.
Definition: http_util.h:137
GetParameter get_parameter
Parameter if GET method.
Definition: http_util.h:114
uint16_t port() const
Returns port number of a given URI.
uint32_t status_code
Response result.
Definition: http_util.h:138
const string & path() const
Returns path part of a given URI.
bool Parse(const string &uri_string)
Parses the given URI.
string body
request body
Definition: http_util.h:116
string body
response body
Definition: http_util.h:141
Data structure to hold HTTP response.
Definition: http_util.h:134
Class to parse an URI string.
Definition: http_util.h:158
string request_uri
Request URI.
Definition: http_util.h:125
const string & host() const
Returns host part of a given URI.
string status_message
message matching status_code
Definition: http_util.h:139
const string & query_string() const
Returns query string part of a given URI.
Header header
header fields
Definition: http_util.h:115
Version version
HTTP version.
Definition: http_util.h:126
string request_uri
Request URI.
Definition: http_util.h:112
Uri()
Default constructor. Holds empty URI.
Header header
header fields
Definition: http_util.h:140
Version version
HTTP version.
Definition: http_util.h:113
Method method
HTTP method.
Definition: http_util.h:124
GetParameter2 get_parameter
Parameter if GET method.
Definition: http_util.h:127
Data structure to hold HTTP request.
Definition: http_util.h:108