MRPT  2.0.1
net_utils.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <string>
13 #include <vector>
14 
15 namespace mrpt
16 {
17 namespace comms
18 {
19 /** A set of useful routines for networking. (in #include
20  * <mrpt/comms/net_utils.h>)
21  * \ingroup mrpt_comms_grp
22  */
23 namespace net
24 {
25 /** \addtogroup mrpt_comms_grp
26  * @{ */
27 
28 using std::string;
29 
30 /** Possible returns from a HTTP request. */
32 {
33  erOk = 0,
38 };
39 
40 /** Perform an HTTP GET operation (version for retrieving the data as a
41  * std::vector<uint8_t>)
42  * \param url Must be a simple string of the form
43  * "http://<servername>/<relative-address>".
44  * \param port The server port, if different from 80.
45  * \param extra_headers If provided, the given extra HTTP headers will be sent.
46  * \param out_errormsg On exit will contain a description of the error or "Ok".
47  * \param out_content The buffer with the retrieved data.
48  * \param out_http_responsecode If provided, will hold the HTTP code, eg: 200,
49  * 404...
50  * \param out_headers If provided, a copy of all the headers returned by the
51  * server will be saved here.
52  * \param auth_user Send a basic HTTP authorization request with the given user
53  * & password.
54  * \param auth_pass Send a basic HTTP authorization request with the given user
55  * & password.
56  *
57  * \return The error or success code.
58  * \sa http_request()
59  */
61  const string& url, std::vector<uint8_t>& out_content, string& out_errormsg,
62  int port = 80, const string& auth_user = string(),
63  const string& auth_pass = string(), int* out_http_responsecode = nullptr,
64  mrpt::system::TParameters<string>* extra_headers = nullptr,
65  mrpt::system::TParameters<string>* out_headers = nullptr,
66  int timeout_ms = 1000);
67 
68 /** Perform an HTTP GET operation (version for retrieving the data as text)
69  * \param url Must be a simple string of the form
70  * "http://<servername>/<relative-address>".
71  * \param port The server port, if different from 80.
72  * \param extra_headers If provided, the given extra HTTP headers will be sent.
73  * \param out_errormsg On exit will contain a description of the error or "Ok".
74  * \param out_content The buffer with the retrieved data.
75  * \param out_http_responsecode If provided, will hold the HTTP code, eg: 200,
76  * 404...
77  * \param out_headers If provided, a copy of all the headers returned by the
78  * server will be saved here.
79  * \param auth_user Send a basic HTTP authorization request with the given user
80  * & password.
81  * \param auth_pass Send a basic HTTP authorization request with the given user
82  * & password.
83  *
84  * \return The error or success code.
85  * \sa http_request()
86  */
88  const string& url, string& out_content, string& out_errormsg, int port = 80,
89  const string& auth_user = string(), const string& auth_pass = string(),
90  int* out_http_responsecode = nullptr,
91  mrpt::system::TParameters<string>* extra_headers = nullptr,
92  mrpt::system::TParameters<string>* out_headers = nullptr,
93  int timeout_ms = 1000);
94 
95 /** Generic function for HTTP GET & POST methods. \sa http_get */
97  const string& http_method, const string& http_send_content,
98  const string& url, std::vector<uint8_t>& out_content, string& out_errormsg,
99  int port = 80, const string& auth_user = string(),
100  const string& auth_pass = string(), int* out_http_responsecode = nullptr,
101  mrpt::system::TParameters<string>* extra_headers = nullptr,
102  mrpt::system::TParameters<string>* out_headers = nullptr,
103  int timeout_ms = 1000);
104 
105 /** Resolve a server address by its name, returning its IP address as a
106  * string - This method has a timeout for the maximum time to wait for
107  * the DNS server. For example: server_name="www.google.com" ->
108  * out_ip="209.85.227.99"
109  *
110  * \return true on success, false on timeout or other error.
111  */
112 bool DNS_resolve_async(
113  const std::string& server_name, std::string& out_ip,
114  const unsigned int timeout_ms = 3000);
115 
116 /** Returns a description of the last Sockets error */
117 std::string getLastSocketErrorStr();
118 
119 /** @brief Ping an IP address
120  *
121  * @param[in] address Address to ping.
122  * @param[in] max_attempts Number of attempts to try and ping.
123  * @param[out] output String containing output information
124  *
125  * @return True if responsive, false otherwise.
126  *
127  * @note { I am redirecting stderr to stdout, so that the overall process
128  * is simplified. Otherwise see:
129  * https://jineshkj.wordpress.com/2006/12/22/how-to-capture-stdin-stdout-and-stderr-of-child-program/
130  * }
131  *
132  */
133 bool Ping(
134  const std::string& address, const int max_attempts,
135  std::string* output_str = nullptr);
136 
137 /** @} */ // end grouping
138 
139 } // namespace net
140 } // namespace comms
141 } // namespace mrpt
std::string getLastSocketErrorStr()
Returns a description of the last Sockets error.
Definition: net_utils.cpp:480
ERRORCODE_HTTP http_get(const string &url, std::vector< uint8_t > &out_content, string &out_errormsg, int port=80, const string &auth_user=string(), const string &auth_pass=string(), int *out_http_responsecode=nullptr, mrpt::system::TParameters< string > *extra_headers=nullptr, mrpt::system::TParameters< string > *out_headers=nullptr, int timeout_ms=1000)
Perform an HTTP GET operation (version for retrieving the data as a std::vector<uint8_t>) ...
Definition: net_utils.cpp:386
bool Ping(const std::string &address, const int max_attempts, std::string *output_str=nullptr)
Ping an IP address.
Definition: net_utils.cpp:498
ERRORCODE_HTTP
Possible returns from a HTTP request.
Definition: net_utils.h:31
bool DNS_resolve_async(const std::string &server_name, std::string &out_ip, const unsigned int timeout_ms=3000)
Resolve a server address by its name, returning its IP address as a string - This method has a timeou...
Definition: net_utils.cpp:404
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
ERRORCODE_HTTP http_request(const string &http_method, const string &http_send_content, const string &url, std::vector< uint8_t > &out_content, string &out_errormsg, int port=80, const string &auth_user=string(), const string &auth_pass=string(), int *out_http_responsecode=nullptr, mrpt::system::TParameters< string > *extra_headers=nullptr, mrpt::system::TParameters< string > *out_headers=nullptr, int timeout_ms=1000)
Generic function for HTTP GET & POST methods.
Definition: net_utils.cpp:69
For usage when passing a dynamic number of (numeric) arguments to a function, by name.
Definition: TParameters.h:54



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020