http.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. #ifndef _ASTERISK_HTTP_H
  19. #define _ASTERISK_HTTP_H
  20. #include "asterisk/config.h"
  21. #include "asterisk/tcptls.h"
  22. #include "asterisk/linkedlists.h"
  23. /*!
  24. * \file http.h
  25. * \brief Support for Private Asterisk HTTP Servers.
  26. * \note Note: The Asterisk HTTP servers are extremely simple and minimal and
  27. * only support the "GET" method.
  28. *
  29. * \author Mark Spencer <markster@digium.com>
  30. *
  31. * \note In order to have TLS/SSL support, we need the openssl libraries.
  32. * Still we can decide whether or not to use them by commenting
  33. * in or out the DO_SSL macro.
  34. * TLS/SSL support is basically implemented by reading from a config file
  35. * (currently http.conf) the names of the certificate and cipher to use,
  36. * and then run ssl_setup() to create an appropriate SSL_CTX (ssl_ctx)
  37. * If we support multiple domains, presumably we need to read multiple
  38. * certificates.
  39. * When we are requested to open a TLS socket, we run make_file_from_fd()
  40. * on the socket, to do the necessary setup. At the moment the context's name
  41. * is hardwired in the function, but we can certainly make it into an extra
  42. * parameter to the function.
  43. * We declare most of ssl support variables unconditionally,
  44. * because their number is small and this simplifies the code.
  45. *
  46. * \note: the ssl-support variables (ssl_ctx, do_ssl, certfile, cipher)
  47. * and their setup should be moved to a more central place, e.g. asterisk.conf
  48. * and the source files that processes it. Similarly, ssl_setup() should
  49. * be run earlier in the startup process so modules have it available.
  50. */
  51. /*! \brief HTTP Request methods known by Asterisk */
  52. enum ast_http_method {
  53. AST_HTTP_UNKNOWN = -1, /*!< Unknown response */
  54. AST_HTTP_GET = 0,
  55. AST_HTTP_POST,
  56. AST_HTTP_HEAD,
  57. AST_HTTP_PUT,
  58. AST_HTTP_DELETE,
  59. AST_HTTP_OPTIONS,
  60. AST_HTTP_MAX_METHOD, /*!< Last entry in ast_http_method enum */
  61. };
  62. struct ast_http_uri;
  63. /*!
  64. * \brief HTTP Callbacks
  65. *
  66. * \param ser TCP/TLS session object
  67. * \param urih Registered URI handler struct for the URI.
  68. * \param uri Remaining request URI path (also with the get_params removed).
  69. * \param method enum ast_http_method GET, POST, etc.
  70. * \param get_params URI argument list passed with the HTTP request.
  71. * \param headers HTTP request header-name/value pair list
  72. *
  73. * \note Should use the ast_http_send() function for sending content
  74. * allocated with ast_str and/or content from an opened file descriptor.
  75. *
  76. * Status and status text should be sent as arguments to the ast_http_send()
  77. * function to reflect the status of the request (200 or 304, for example).
  78. * Content length is calculated by ast_http_send() automatically.
  79. *
  80. * Static content may be indicated to the ast_http_send() function,
  81. * to indicate that it may be cached.
  82. *
  83. * For a need authentication response, the ast_http_auth() function
  84. * should be used.
  85. *
  86. * For an error response, the ast_http_error() function should be used.
  87. *
  88. * \retval 0 Continue and process the next HTTP request.
  89. * \retval -1 Fatal HTTP connection error. Force the HTTP connection closed.
  90. */
  91. typedef int (*ast_http_callback)(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers);
  92. /*! \brief Definition of a URI handler */
  93. struct ast_http_uri {
  94. AST_LIST_ENTRY(ast_http_uri) entry;
  95. const char *description;
  96. const char *uri;
  97. const char *prefix;
  98. ast_http_callback callback;
  99. unsigned int has_subtree:1;
  100. /*! Structure is malloc'd */
  101. unsigned int mallocd:1;
  102. /*! Data structure is malloc'd */
  103. unsigned int dmallocd:1;
  104. /*! Don't automatically decode URI before passing it to the callback */
  105. unsigned int no_decode_uri:1;
  106. /*! Data to bind to the uri if needed */
  107. void *data;
  108. /*! Key to be used for unlinking if multiple URIs registered */
  109. const char *key;
  110. };
  111. /*! \brief Get cookie from Request headers */
  112. struct ast_variable *ast_http_get_cookies(struct ast_variable *headers);
  113. /*! \brief HTTP authentication information. */
  114. struct ast_http_auth {
  115. /*! Provided userid. */
  116. char *userid;
  117. /*! For Basic auth, the provided password. */
  118. char *password;
  119. };
  120. /*!
  121. * \brief Get HTTP authentication information from headers.
  122. *
  123. * The returned object is AO2 managed, so clean up with ao2_cleanup().
  124. *
  125. * \param headers HTTP request headers.
  126. * \return HTTP auth structure.
  127. * \return \c NULL if no supported HTTP auth headers present.
  128. * \since 12
  129. */
  130. struct ast_http_auth *ast_http_get_auth(struct ast_variable *headers);
  131. /*! \brief Register a URI handler */
  132. int ast_http_uri_link(struct ast_http_uri *urihandler);
  133. /*! \brief Unregister a URI handler */
  134. void ast_http_uri_unlink(struct ast_http_uri *urihandler);
  135. /*! \brief Unregister all handlers with matching key */
  136. void ast_http_uri_unlink_all_with_key(const char *key);
  137. /*!
  138. * \brief Return http method name string
  139. * \since 1.8
  140. */
  141. const char *ast_get_http_method(enum ast_http_method method) attribute_pure;
  142. /*!
  143. * \brief Return mime type based on extension
  144. * \param ftype filename extension
  145. * \return String containing associated MIME type
  146. * \since 1.8
  147. */
  148. const char *ast_http_ftype2mtype(const char *ftype) attribute_pure;
  149. /*!
  150. * \brief Return manager id, if exist, from request headers
  151. * \param headers List of HTTP headers
  152. * \return 32-bit associated manager session identifier
  153. * \since 1.8
  154. */
  155. uint32_t ast_http_manid_from_vars(struct ast_variable *headers) attribute_pure;
  156. /*!
  157. * \brief Generic function for sending HTTP/1.1 response.
  158. * \param ser TCP/TLS session object
  159. * \param method GET/POST/HEAD
  160. * \param status_code HTTP response code (200/401/403/404/500)
  161. * \param status_title English equivalent to the status_code parameter
  162. * \param http_header An ast_str object containing all headers
  163. * \param out An ast_str object containing the body of the response
  164. * \param fd If out is NULL, a file descriptor where the body of the response is held (otherwise -1)
  165. * \param static_content Zero if the content is dynamically generated and should not be cached; nonzero otherwise
  166. *
  167. * \note Function determines the HTTP response header from status_code,
  168. * status_header, and http_header.
  169. *
  170. * Extra HTTP headers MUST be present only in the http_header argument. The
  171. * argument "out" should contain only content of the response (no headers!).
  172. *
  173. * HTTP content can be constructed from the argument "out", if it is not NULL;
  174. * otherwise, the function will read content from FD.
  175. *
  176. * This function calculates the content-length http header itself.
  177. *
  178. * Both the http_header and out arguments will be freed by this function;
  179. * however, if FD is open, it will remain open.
  180. *
  181. * \since 1.8
  182. */
  183. void ast_http_send(struct ast_tcptls_session_instance *ser, enum ast_http_method method,
  184. int status_code, const char *status_title, struct ast_str *http_header,
  185. struct ast_str *out, int fd, unsigned int static_content);
  186. /*!
  187. * \brief Creates and sends a formatted http response message.
  188. * \param ser TCP/TLS session object
  189. * \param status_code HTTP response code (200/401/403/404/500)
  190. * \param status_title English equivalent to the status_code parameter
  191. * \param http_header_data The formatted text to use in the http header
  192. * \param text Additional informational text to use in the
  193. * response
  194. *
  195. * \note Function constructs response headers from the status_code, status_title and
  196. * http_header_data parameters.
  197. *
  198. * The response body is created as HTML content, from the status_code,
  199. * status_title, and the text parameters.
  200. *
  201. * The http_header_data parameter will be freed as a result of calling function.
  202. *
  203. * \since 13.2.0
  204. */
  205. void ast_http_create_response(struct ast_tcptls_session_instance *ser, int status_code,
  206. const char *status_title, struct ast_str *http_header_data, const char *text);
  207. /*! \brief Send http "401 Unauthorized" response and close socket */
  208. void ast_http_auth(struct ast_tcptls_session_instance *ser, const char *realm, const unsigned long nonce, const unsigned long opaque, int stale, const char *text);
  209. /*! \brief Send HTTP error message and close socket */
  210. void ast_http_error(struct ast_tcptls_session_instance *ser, int status, const char *title, const char *text);
  211. /*!
  212. * \brief Return the current prefix
  213. * \param[out] buf destination buffer for previous
  214. * \param[in] len length of prefix to copy
  215. * \since 1.6.1
  216. */
  217. void ast_http_prefix(char *buf, int len);
  218. /*!
  219. * \brief Request the HTTP connection be closed after this HTTP request.
  220. * \since 12.4.0
  221. *
  222. * \param ser HTTP TCP/TLS session object.
  223. *
  224. * \note Call before ast_http_error() to make the connection close.
  225. *
  226. * \return Nothing
  227. */
  228. void ast_http_request_close_on_completion(struct ast_tcptls_session_instance *ser);
  229. /*!
  230. * \brief Update the body read success status.
  231. * \since 12.4.0
  232. *
  233. * \param ser HTTP TCP/TLS session object.
  234. * \param read_success TRUE if body was read successfully.
  235. *
  236. * \return Nothing
  237. */
  238. void ast_http_body_read_status(struct ast_tcptls_session_instance *ser, int read_success);
  239. /*!
  240. * \brief Read and discard any unread HTTP request body.
  241. * \since 12.4.0
  242. *
  243. * \param ser HTTP TCP/TLS session object.
  244. *
  245. * \retval 0 on success.
  246. * \retval -1 on error.
  247. */
  248. int ast_http_body_discard(struct ast_tcptls_session_instance *ser);
  249. /*!
  250. * \brief Get post variables from client Request Entity-Body, if content type is application/x-www-form-urlencoded.
  251. * \param ser TCP/TLS session object
  252. * \param headers List of HTTP headers
  253. * \return List of variables within the POST body
  254. * \note Since returned list is malloc'd, list should be free'd by the calling function
  255. * \since 1.8
  256. */
  257. struct ast_variable *ast_http_get_post_vars(struct ast_tcptls_session_instance *ser, struct ast_variable *headers);
  258. struct ast_json;
  259. /*!
  260. * \brief Get JSON from client Request Entity-Body, if content type is
  261. * application/json.
  262. * \param ser TCP/TLS session object
  263. * \param headers List of HTTP headers
  264. * \return Parsed JSON content body
  265. * \return \c NULL on error, if no content, or if different content type.
  266. * \since 12
  267. */
  268. struct ast_json *ast_http_get_json(
  269. struct ast_tcptls_session_instance *ser, struct ast_variable *headers);
  270. /*!\brief Parse the http response status line.
  271. *
  272. * \param buf the http response line information
  273. * \param version the expected http version (e.g. HTTP/1.1)
  274. * \param code the expected status code
  275. * \return -1 if version didn't match or status code conversion fails.
  276. * \return status code (>0)
  277. * \since 13
  278. */
  279. int ast_http_response_status_line(const char *buf, const char *version, int code);
  280. /*!\brief Parse a header into the given name/value strings.
  281. *
  282. * \note This modifies the given buffer and the out parameters point (not
  283. * allocated) to the start of the header name and header value,
  284. * respectively.
  285. *
  286. * \param buf a string containing the name/value to point to
  287. * \param name out parameter pointing to the header name
  288. * \param value out parameter pointing to header value
  289. * \return -1 if buf is empty
  290. * \return 0 if buf could be separated into name and value
  291. * \return 1 if name or value portion don't exist
  292. * \since 13
  293. */
  294. int ast_http_header_parse(char *buf, char **name, char **value);
  295. /*!\brief Check if the header and value match (case insensitive) their
  296. * associated expected values.
  297. *
  298. * \param name header name to check
  299. * \param expected_name the expected name of the header
  300. * \param value header value to check
  301. * \param expected_value the expected value of the header
  302. * \return 0 if the name and expected name do not match
  303. * \return -1 if the value and expected value do not match
  304. * \return 1 if the both the name and value match their expected value
  305. * \since 13
  306. */
  307. int ast_http_header_match(const char *name, const char *expected_name,
  308. const char *value, const char *expected_value);
  309. /*!\brief Check if the header name matches the expected header name. If so,
  310. * then check to see if the value can be located in the expected value.
  311. *
  312. * \note Both header and value checks are case insensitive.
  313. *
  314. * \param name header name to check
  315. * \param expected_name the expected name of the header
  316. * \param value header value to check if in expected value
  317. * \param expected_value the expected value(s)
  318. * \return 0 if the name and expected name do not match
  319. * \return -1 if the value and is not in the expected value
  320. * \return 1 if the name matches expected name and value is in expected value
  321. * \since 13
  322. */
  323. int ast_http_header_match_in(const char *name, const char *expected_name,
  324. const char *value, const char *expected_value);
  325. #endif /* _ASTERISK_SRV_H */