PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/include/SFML/Network/Http.hpp

http://github.com/LaurentGomila/SFML
C++ Header | 482 lines | 101 code | 48 blank | 333 comment | 0 complexity | ee1c0bbdf0fe9ab5d9ace931524bda65 MD5 | raw file
  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2019 Laurent Gomila (laurent@sfml-dev.org)
  5. //
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it freely,
  11. // subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented;
  14. // you must not claim that you wrote the original software.
  15. // If you use this software in a product, an acknowledgment
  16. // in the product documentation would be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such,
  19. // and must not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. ////////////////////////////////////////////////////////////
  24. #ifndef SFML_HTTP_HPP
  25. #define SFML_HTTP_HPP
  26. ////////////////////////////////////////////////////////////
  27. // Headers
  28. ////////////////////////////////////////////////////////////
  29. #include <SFML/Network/Export.hpp>
  30. #include <SFML/Network/IpAddress.hpp>
  31. #include <SFML/Network/TcpSocket.hpp>
  32. #include <SFML/System/NonCopyable.hpp>
  33. #include <SFML/System/Time.hpp>
  34. #include <map>
  35. #include <string>
  36. namespace sf
  37. {
  38. ////////////////////////////////////////////////////////////
  39. /// \brief A HTTP client
  40. ///
  41. ////////////////////////////////////////////////////////////
  42. class SFML_NETWORK_API Http : NonCopyable
  43. {
  44. public:
  45. ////////////////////////////////////////////////////////////
  46. /// \brief Define a HTTP request
  47. ///
  48. ////////////////////////////////////////////////////////////
  49. class SFML_NETWORK_API Request
  50. {
  51. public:
  52. ////////////////////////////////////////////////////////////
  53. /// \brief Enumerate the available HTTP methods for a request
  54. ///
  55. ////////////////////////////////////////////////////////////
  56. enum Method
  57. {
  58. Get, //!< Request in get mode, standard method to retrieve a page
  59. Post, //!< Request in post mode, usually to send data to a page
  60. Head, //!< Request a page's header only
  61. Put, //!< Request in put mode, useful for a REST API
  62. Delete //!< Request in delete mode, useful for a REST API
  63. };
  64. ////////////////////////////////////////////////////////////
  65. /// \brief Default constructor
  66. ///
  67. /// This constructor creates a GET request, with the root
  68. /// URI ("/") and an empty body.
  69. ///
  70. /// \param uri Target URI
  71. /// \param method Method to use for the request
  72. /// \param body Content of the request's body
  73. ///
  74. ////////////////////////////////////////////////////////////
  75. Request(const std::string& uri = "/", Method method = Get, const std::string& body = "");
  76. ////////////////////////////////////////////////////////////
  77. /// \brief Set the value of a field
  78. ///
  79. /// The field is created if it doesn't exist. The name of
  80. /// the field is case-insensitive.
  81. /// By default, a request doesn't contain any field (but the
  82. /// mandatory fields are added later by the HTTP client when
  83. /// sending the request).
  84. ///
  85. /// \param field Name of the field to set
  86. /// \param value Value of the field
  87. ///
  88. ////////////////////////////////////////////////////////////
  89. void setField(const std::string& field, const std::string& value);
  90. ////////////////////////////////////////////////////////////
  91. /// \brief Set the request method
  92. ///
  93. /// See the Method enumeration for a complete list of all
  94. /// the availale methods.
  95. /// The method is Http::Request::Get by default.
  96. ///
  97. /// \param method Method to use for the request
  98. ///
  99. ////////////////////////////////////////////////////////////
  100. void setMethod(Method method);
  101. ////////////////////////////////////////////////////////////
  102. /// \brief Set the requested URI
  103. ///
  104. /// The URI is the resource (usually a web page or a file)
  105. /// that you want to get or post.
  106. /// The URI is "/" (the root page) by default.
  107. ///
  108. /// \param uri URI to request, relative to the host
  109. ///
  110. ////////////////////////////////////////////////////////////
  111. void setUri(const std::string& uri);
  112. ////////////////////////////////////////////////////////////
  113. /// \brief Set the HTTP version for the request
  114. ///
  115. /// The HTTP version is 1.0 by default.
  116. ///
  117. /// \param major Major HTTP version number
  118. /// \param minor Minor HTTP version number
  119. ///
  120. ////////////////////////////////////////////////////////////
  121. void setHttpVersion(unsigned int major, unsigned int minor);
  122. ////////////////////////////////////////////////////////////
  123. /// \brief Set the body of the request
  124. ///
  125. /// The body of a request is optional and only makes sense
  126. /// for POST requests. It is ignored for all other methods.
  127. /// The body is empty by default.
  128. ///
  129. /// \param body Content of the body
  130. ///
  131. ////////////////////////////////////////////////////////////
  132. void setBody(const std::string& body);
  133. private:
  134. friend class Http;
  135. ////////////////////////////////////////////////////////////
  136. /// \brief Prepare the final request to send to the server
  137. ///
  138. /// This is used internally by Http before sending the
  139. /// request to the web server.
  140. ///
  141. /// \return String containing the request, ready to be sent
  142. ///
  143. ////////////////////////////////////////////////////////////
  144. std::string prepare() const;
  145. ////////////////////////////////////////////////////////////
  146. /// \brief Check if the request defines a field
  147. ///
  148. /// This function uses case-insensitive comparisons.
  149. ///
  150. /// \param field Name of the field to test
  151. ///
  152. /// \return True if the field exists, false otherwise
  153. ///
  154. ////////////////////////////////////////////////////////////
  155. bool hasField(const std::string& field) const;
  156. ////////////////////////////////////////////////////////////
  157. // Types
  158. ////////////////////////////////////////////////////////////
  159. typedef std::map<std::string, std::string> FieldTable;
  160. ////////////////////////////////////////////////////////////
  161. // Member data
  162. ////////////////////////////////////////////////////////////
  163. FieldTable m_fields; //!< Fields of the header associated to their value
  164. Method m_method; //!< Method to use for the request
  165. std::string m_uri; //!< Target URI of the request
  166. unsigned int m_majorVersion; //!< Major HTTP version
  167. unsigned int m_minorVersion; //!< Minor HTTP version
  168. std::string m_body; //!< Body of the request
  169. };
  170. ////////////////////////////////////////////////////////////
  171. /// \brief Define a HTTP response
  172. ///
  173. ////////////////////////////////////////////////////////////
  174. class SFML_NETWORK_API Response
  175. {
  176. public:
  177. ////////////////////////////////////////////////////////////
  178. /// \brief Enumerate all the valid status codes for a response
  179. ///
  180. ////////////////////////////////////////////////////////////
  181. enum Status
  182. {
  183. // 2xx: success
  184. Ok = 200, //!< Most common code returned when operation was successful
  185. Created = 201, //!< The resource has successfully been created
  186. Accepted = 202, //!< The request has been accepted, but will be processed later by the server
  187. NoContent = 204, //!< The server didn't send any data in return
  188. ResetContent = 205, //!< The server informs the client that it should clear the view (form) that caused the request to be sent
  189. PartialContent = 206, //!< The server has sent a part of the resource, as a response to a partial GET request
  190. // 3xx: redirection
  191. MultipleChoices = 300, //!< The requested page can be accessed from several locations
  192. MovedPermanently = 301, //!< The requested page has permanently moved to a new location
  193. MovedTemporarily = 302, //!< The requested page has temporarily moved to a new location
  194. NotModified = 304, //!< For conditional requests, means the requested page hasn't changed and doesn't need to be refreshed
  195. // 4xx: client error
  196. BadRequest = 400, //!< The server couldn't understand the request (syntax error)
  197. Unauthorized = 401, //!< The requested page needs an authentication to be accessed
  198. Forbidden = 403, //!< The requested page cannot be accessed at all, even with authentication
  199. NotFound = 404, //!< The requested page doesn't exist
  200. RangeNotSatisfiable = 407, //!< The server can't satisfy the partial GET request (with a "Range" header field)
  201. // 5xx: server error
  202. InternalServerError = 500, //!< The server encountered an unexpected error
  203. NotImplemented = 501, //!< The server doesn't implement a requested feature
  204. BadGateway = 502, //!< The gateway server has received an error from the source server
  205. ServiceNotAvailable = 503, //!< The server is temporarily unavailable (overloaded, in maintenance, ...)
  206. GatewayTimeout = 504, //!< The gateway server couldn't receive a response from the source server
  207. VersionNotSupported = 505, //!< The server doesn't support the requested HTTP version
  208. // 10xx: SFML custom codes
  209. InvalidResponse = 1000, //!< Response is not a valid HTTP one
  210. ConnectionFailed = 1001 //!< Connection with server failed
  211. };
  212. ////////////////////////////////////////////////////////////
  213. /// \brief Default constructor
  214. ///
  215. /// Constructs an empty response.
  216. ///
  217. ////////////////////////////////////////////////////////////
  218. Response();
  219. ////////////////////////////////////////////////////////////
  220. /// \brief Get the value of a field
  221. ///
  222. /// If the field \a field is not found in the response header,
  223. /// the empty string is returned. This function uses
  224. /// case-insensitive comparisons.
  225. ///
  226. /// \param field Name of the field to get
  227. ///
  228. /// \return Value of the field, or empty string if not found
  229. ///
  230. ////////////////////////////////////////////////////////////
  231. const std::string& getField(const std::string& field) const;
  232. ////////////////////////////////////////////////////////////
  233. /// \brief Get the response status code
  234. ///
  235. /// The status code should be the first thing to be checked
  236. /// after receiving a response, it defines whether it is a
  237. /// success, a failure or anything else (see the Status
  238. /// enumeration).
  239. ///
  240. /// \return Status code of the response
  241. ///
  242. ////////////////////////////////////////////////////////////
  243. Status getStatus() const;
  244. ////////////////////////////////////////////////////////////
  245. /// \brief Get the major HTTP version number of the response
  246. ///
  247. /// \return Major HTTP version number
  248. ///
  249. /// \see getMinorHttpVersion
  250. ///
  251. ////////////////////////////////////////////////////////////
  252. unsigned int getMajorHttpVersion() const;
  253. ////////////////////////////////////////////////////////////
  254. /// \brief Get the minor HTTP version number of the response
  255. ///
  256. /// \return Minor HTTP version number
  257. ///
  258. /// \see getMajorHttpVersion
  259. ///
  260. ////////////////////////////////////////////////////////////
  261. unsigned int getMinorHttpVersion() const;
  262. ////////////////////////////////////////////////////////////
  263. /// \brief Get the body of the response
  264. ///
  265. /// The body of a response may contain:
  266. /// \li the requested page (for GET requests)
  267. /// \li a response from the server (for POST requests)
  268. /// \li nothing (for HEAD requests)
  269. /// \li an error message (in case of an error)
  270. ///
  271. /// \return The response body
  272. ///
  273. ////////////////////////////////////////////////////////////
  274. const std::string& getBody() const;
  275. private:
  276. friend class Http;
  277. ////////////////////////////////////////////////////////////
  278. /// \brief Construct the header from a response string
  279. ///
  280. /// This function is used by Http to build the response
  281. /// of a request.
  282. ///
  283. /// \param data Content of the response to parse
  284. ///
  285. ////////////////////////////////////////////////////////////
  286. void parse(const std::string& data);
  287. ////////////////////////////////////////////////////////////
  288. /// \brief Read values passed in the answer header
  289. ///
  290. /// This function is used by Http to extract values passed
  291. /// in the response.
  292. ///
  293. /// \param in String stream containing the header values
  294. ///
  295. ////////////////////////////////////////////////////////////
  296. void parseFields(std::istream &in);
  297. ////////////////////////////////////////////////////////////
  298. // Types
  299. ////////////////////////////////////////////////////////////
  300. typedef std::map<std::string, std::string> FieldTable;
  301. ////////////////////////////////////////////////////////////
  302. // Member data
  303. ////////////////////////////////////////////////////////////
  304. FieldTable m_fields; //!< Fields of the header
  305. Status m_status; //!< Status code
  306. unsigned int m_majorVersion; //!< Major HTTP version
  307. unsigned int m_minorVersion; //!< Minor HTTP version
  308. std::string m_body; //!< Body of the response
  309. };
  310. ////////////////////////////////////////////////////////////
  311. /// \brief Default constructor
  312. ///
  313. ////////////////////////////////////////////////////////////
  314. Http();
  315. ////////////////////////////////////////////////////////////
  316. /// \brief Construct the HTTP client with the target host
  317. ///
  318. /// This is equivalent to calling setHost(host, port).
  319. /// The port has a default value of 0, which means that the
  320. /// HTTP client will use the right port according to the
  321. /// protocol used (80 for HTTP). You should leave it like
  322. /// this unless you really need a port other than the
  323. /// standard one, or use an unknown protocol.
  324. ///
  325. /// \param host Web server to connect to
  326. /// \param port Port to use for connection
  327. ///
  328. ////////////////////////////////////////////////////////////
  329. Http(const std::string& host, unsigned short port = 0);
  330. ////////////////////////////////////////////////////////////
  331. /// \brief Set the target host
  332. ///
  333. /// This function just stores the host address and port, it
  334. /// doesn't actually connect to it until you send a request.
  335. /// The port has a default value of 0, which means that the
  336. /// HTTP client will use the right port according to the
  337. /// protocol used (80 for HTTP). You should leave it like
  338. /// this unless you really need a port other than the
  339. /// standard one, or use an unknown protocol.
  340. ///
  341. /// \param host Web server to connect to
  342. /// \param port Port to use for connection
  343. ///
  344. ////////////////////////////////////////////////////////////
  345. void setHost(const std::string& host, unsigned short port = 0);
  346. ////////////////////////////////////////////////////////////
  347. /// \brief Send a HTTP request and return the server's response.
  348. ///
  349. /// You must have a valid host before sending a request (see setHost).
  350. /// Any missing mandatory header field in the request will be added
  351. /// with an appropriate value.
  352. /// Warning: this function waits for the server's response and may
  353. /// not return instantly; use a thread if you don't want to block your
  354. /// application, or use a timeout to limit the time to wait. A value
  355. /// of Time::Zero means that the client will use the system default timeout
  356. /// (which is usually pretty long).
  357. ///
  358. /// \param request Request to send
  359. /// \param timeout Maximum time to wait
  360. ///
  361. /// \return Server's response
  362. ///
  363. ////////////////////////////////////////////////////////////
  364. Response sendRequest(const Request& request, Time timeout = Time::Zero);
  365. private:
  366. ////////////////////////////////////////////////////////////
  367. // Member data
  368. ////////////////////////////////////////////////////////////
  369. TcpSocket m_connection; //!< Connection to the host
  370. IpAddress m_host; //!< Web host address
  371. std::string m_hostName; //!< Web host name
  372. unsigned short m_port; //!< Port used for connection with host
  373. };
  374. } // namespace sf
  375. #endif // SFML_HTTP_HPP
  376. ////////////////////////////////////////////////////////////
  377. /// \class sf::Http
  378. /// \ingroup network
  379. ///
  380. /// sf::Http is a very simple HTTP client that allows you
  381. /// to communicate with a web server. You can retrieve
  382. /// web pages, send data to an interactive resource,
  383. /// download a remote file, etc. The HTTPS protocol is
  384. /// not supported.
  385. ///
  386. /// The HTTP client is split into 3 classes:
  387. /// \li sf::Http::Request
  388. /// \li sf::Http::Response
  389. /// \li sf::Http
  390. ///
  391. /// sf::Http::Request builds the request that will be
  392. /// sent to the server. A request is made of:
  393. /// \li a method (what you want to do)
  394. /// \li a target URI (usually the name of the web page or file)
  395. /// \li one or more header fields (options that you can pass to the server)
  396. /// \li an optional body (for POST requests)
  397. ///
  398. /// sf::Http::Response parse the response from the web server
  399. /// and provides getters to read them. The response contains:
  400. /// \li a status code
  401. /// \li header fields (that may be answers to the ones that you requested)
  402. /// \li a body, which contains the contents of the requested resource
  403. ///
  404. /// sf::Http provides a simple function, SendRequest, to send a
  405. /// sf::Http::Request and return the corresponding sf::Http::Response
  406. /// from the server.
  407. ///
  408. /// Usage example:
  409. /// \code
  410. /// // Create a new HTTP client
  411. /// sf::Http http;
  412. ///
  413. /// // We'll work on http://www.sfml-dev.org
  414. /// http.setHost("http://www.sfml-dev.org");
  415. ///
  416. /// // Prepare a request to get the 'features.php' page
  417. /// sf::Http::Request request("features.php");
  418. ///
  419. /// // Send the request
  420. /// sf::Http::Response response = http.sendRequest(request);
  421. ///
  422. /// // Check the status code and display the result
  423. /// sf::Http::Response::Status status = response.getStatus();
  424. /// if (status == sf::Http::Response::Ok)
  425. /// {
  426. /// std::cout << response.getBody() << std::endl;
  427. /// }
  428. /// else
  429. /// {
  430. /// std::cout << "Error " << status << std::endl;
  431. /// }
  432. /// \endcode
  433. ///
  434. ////////////////////////////////////////////////////////////