PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/FNET/fnet_stack/services/http/fnet_http_prv.h

https://gitlab.com/fuggles/ucos
C Header | 307 lines | 173 code | 39 blank | 95 comment | 1 complexity | fbd8062e135d13abeb3d21c86d920bbc MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /**************************************************************************
  2. *
  3. * Copyright 2011-2015 by Andrey Butok. FNET Community.
  4. * Copyright 2008-2010 by Andrey Butok. Freescale Semiconductor, Inc.
  5. *
  6. ***************************************************************************
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License Version 3
  9. * or later (the "LGPL").
  10. *
  11. * As a special exception, the copyright holders of the FNET project give you
  12. * permission to link the FNET sources with independent modules to produce an
  13. * executable, regardless of the license terms of these independent modules,
  14. * and to copy and distribute the resulting executable under terms of your
  15. * choice, provided that you also meet, for each linked independent module,
  16. * the terms and conditions of the license of that module.
  17. * An independent module is a module which is not derived from or based
  18. * on this library.
  19. * If you modify the FNET sources, you may extend this exception
  20. * to your version of the FNET sources, but you are not obligated
  21. * to do so. If you do not wish to do so, delete this
  22. * exception statement from your version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * and the GNU Lesser General Public License along with this program.
  30. * If not, see <http://www.gnu.org/licenses/>.
  31. *
  32. **********************************************************************/
  33. /*!
  34. *
  35. * @file fnet_http_prv.h
  36. *
  37. * @author Andrey Butok
  38. *
  39. * @brief Private. FNET HTTP Server API.
  40. *
  41. ***************************************************************************/
  42. #ifndef _FNET_HTTP_PRV_H_
  43. #define _FNET_HTTP_PRV_H_
  44. #include "fnet.h"
  45. #if FNET_CFG_HTTP && FNET_CFG_FS
  46. #if FNET_CFG_HTTP_SSI
  47. #include "fnet_http_ssi_prv.h"
  48. #endif
  49. #if FNET_CFG_HTTP_POST
  50. #include "fnet_http_post.h"
  51. #endif
  52. /* Minimum size of the internal buffer */
  53. #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/
  54. #define FNET_HTTP_BUF_SIZE_MIN (20)
  55. #else /* HTTP/0.9*/
  56. #define FNET_HTTP_BUF_SIZE_MIN (10)
  57. #endif
  58. /* Minimum buffer size protection.*/
  59. #if FNET_CFG_HTTP_REQUEST_SIZE_MAX > FNET_HTTP_BUF_SIZE_MIN
  60. #define FNET_HTTP_BUF_SIZE FNET_CFG_HTTP_REQUEST_SIZE_MAX
  61. #else
  62. #define FNET_HTTP_BUF_SIZE FNET_HTTP_BUF_SIZE_MIN
  63. #endif
  64. #if FNET_CFG_DEBUG_HTTP
  65. #define FNET_DEBUG_HTTP FNET_DEBUG
  66. #else
  67. #define FNET_DEBUG_HTTP(...)
  68. #endif
  69. #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/
  70. /************************************************************************
  71. * HTTP 1.0 Reason-Phrase definitions.
  72. * RFC1945: The Reason-Phrase is intended to give a short textual
  73. * description of the Status-Code.
  74. *************************************************************************/
  75. #define FNET_HTTP_REASON_PHRASE_OK "OK"
  76. #define FNET_HTTP_REASON_PHRASE_CREATED "Created"
  77. #define FNET_HTTP_REASON_PHRASE_ACCEPTED "Accepted"
  78. #define FNET_HTTP_REASON_PHRASE_NO_CONTENT "No Content"
  79. #define FNET_HTTP_REASON_PHRASE_MOVED_PERMANENTLY "Moved Permanently"
  80. #define FNET_HTTP_REASON_PHRASE_MOVED_TEMPORARILY "Moved Temporarily"
  81. #define FNET_HTTP_REASON_PHRASE_NOT_MODIFIED "Not Modified"
  82. #define FNET_HTTP_REASON_PHRASE_BAD_REQUEST "Bad Request"
  83. #define FNET_HTTP_REASON_PHRASE_UNAUTHORIZED "Unauthorized"
  84. #define FNET_HTTP_REASON_PHRASE_FORBIDDEN "Forbidden"
  85. #define FNET_HTTP_REASON_PHRASE_NOT_FOUND "Not Found"
  86. #define FNET_HTTP_REASON_PHRASE_INTERNAL_SERVER_ERROR "Internal Server Error"
  87. #define FNET_HTTP_REASON_PHRASE_NOT_IMPLEMENTED "Not Implemented"
  88. #define FNET_HTTP_REASON_PHRASE_BAD_GATEWAY "Bad Gateway"
  89. #define FNET_HTTP_REASON_PHRASE_SERVICE_UNAVAILABLE "Service Unavailable"
  90. /************************************************************************
  91. * HTTP response status structure.
  92. *************************************************************************/
  93. struct fnet_http_status
  94. {
  95. fnet_http_status_code_t code; /* Status-Code.*/
  96. char *phrase; /* An optional Reason-Phrase. May be set to NULL */
  97. };
  98. /************************************************************************
  99. * HTTP version structure.
  100. *************************************************************************/
  101. struct fnet_http_version
  102. {
  103. unsigned char major;
  104. unsigned char minor;
  105. };
  106. #endif /* FNET_CFG_HTTP_VERSION_MAJOR */
  107. /******************************************************************************
  108. * HTTP server states.
  109. ******************************************************************************/
  110. typedef enum
  111. {
  112. FNET_HTTP_STATE_DISABLED = 0, /**< @brief HTTP server service is
  113. * not initialized.
  114. */
  115. FNET_HTTP_STATE_LISTENING = 1, /**< @brief HTTP server is listening
  116. * for client socket.
  117. */
  118. FNET_HTTP_STATE_RX_REQUEST = 2, /**< @brief HTTP server is waiting or receiving
  119. * a HTTP request.
  120. */
  121. #if FNET_CFG_HTTP_POST
  122. FNET_HTTP_STATE_RX = 3, /**< @brief HTTP server is receiving the
  123. * Entity-Body of a HTTP request.
  124. */
  125. #endif
  126. FNET_HTTP_STATE_TX = 4, /**< @brief HTTP server is sending a
  127. * response to a client.
  128. */
  129. FNET_HTTP_STATE_CLOSING = 5 /**< @brief HTTP server is closing
  130. * the socket connection.
  131. */
  132. } fnet_http_state_t;
  133. struct fnet_http_if;
  134. /************************************************************************
  135. * HTTP response parameters structure.
  136. *************************************************************************/
  137. struct fnet_http_response
  138. {
  139. const struct fnet_http_file_handler *send_file_handler;
  140. int (*tx_data)(struct fnet_http_if * http); /* TX state handler.*/
  141. char send_eof; /* Optional EOF flag. It means nomore data for send*/
  142. unsigned long buffer_sent; /* A number of bytes were sent.*/
  143. int status_line_state;
  144. long cookie;
  145. #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/
  146. const struct fnet_http_content_type *send_file_content_type; /* MIME Content-Type.*/
  147. struct fnet_http_status status; /* Status of the response.*/
  148. struct fnet_http_version version; /* Protocol version used for current request.*/
  149. long content_length; /* The total size of the data to send (is -1 if unknown).*/
  150. #endif
  151. #if FNET_CFG_HTTP_AUTHENTICATION_BASIC
  152. const struct fnet_http_auth *auth_entry;
  153. const struct fnet_http_auth_scheme *auth_scheme;
  154. #endif
  155. };
  156. /************************************************************************
  157. * Parsed URI (Uniform Resource Identifier) structure.
  158. *************************************************************************/
  159. struct fnet_http_uri
  160. {
  161. char * path; /* File path (with file extension). */
  162. char * extension; /* File extension. */
  163. char * query; /* Optional query string. */
  164. };
  165. /************************************************************************
  166. * HTTP request parameters structure.
  167. *************************************************************************/
  168. struct fnet_http_request
  169. {
  170. const struct fnet_http_method *method;
  171. struct fnet_http_uri uri;
  172. long content_length;
  173. #if FNET_CFG_HTTP_VERSION_MAJOR /* HTTP/1.x*/
  174. int skip_line;
  175. #endif
  176. };
  177. /************************************************************************
  178. * HTTP session control structure.
  179. *************************************************************************/
  180. struct fnet_http_session_if
  181. {
  182. fnet_http_state_t state; /* Current state.*/
  183. unsigned long state_time; /* Start time used by the state machine for timeout calculation.*/
  184. SOCKET socket_foreign; /* Foreign socket.*/
  185. char buffer[FNET_HTTP_BUF_SIZE+1]; /* Receive/Transmit buffer */
  186. unsigned long buffer_actual_size; /* Size of the actual data in the buffer.*/
  187. union
  188. {
  189. FNET_FS_FILE file_desc;
  190. void * data_ptr;
  191. } send_param;
  192. struct fnet_http_response response; /* Holds the accumulated data for the HTTP 1.0 response header */
  193. struct fnet_http_request request;
  194. };
  195. /************************************************************************
  196. * HTTP interface control structure.
  197. *************************************************************************/
  198. struct fnet_http_if
  199. {
  200. SOCKET socket_listen; /* Listening socket.*/
  201. fnet_poll_desc_t service_descriptor; /* Descriptor of polling service.*/
  202. int enabled;
  203. FNET_FS_DIR root_dir;
  204. FNET_FS_FILE index_file;
  205. const struct fnet_http_file_handler *index_file_handler;
  206. unsigned long send_max; /* Socket maximum tx buffer.*/
  207. struct fnet_http_session_if *session_active;
  208. struct fnet_http_session_if session[FNET_CFG_HTTP_SESSION_MAX];
  209. #if FNET_CFG_HTTP_VERSION_MAJOR
  210. const struct fnet_http_content_type *index_file_content_type; /* MIME Content-Type of Index File.*/
  211. #endif
  212. #if FNET_CFG_HTTP_SSI
  213. struct fnet_http_ssi_if ssi;
  214. #endif
  215. #if FNET_CFG_HTTP_CGI
  216. const struct fnet_http_cgi *cgi_table;
  217. #endif
  218. #if FNET_CFG_HTTP_AUTHENTICATION_BASIC
  219. const struct fnet_http_auth *auth_table;
  220. #endif
  221. #if FNET_CFG_HTTP_POST
  222. const struct fnet_http_post *post_table;
  223. #endif
  224. };
  225. /************************************************************************
  226. * HTTP request handler structure.
  227. *************************************************************************/
  228. struct fnet_http_method
  229. {
  230. const char * token; /* Method token, which will identify protocol.
  231. * It indicates the method to be performed on the resource identified
  232. * by the Request-URI.*/
  233. int (* handle)(struct fnet_http_if * http, struct fnet_http_uri * uri);
  234. int (* receive)(struct fnet_http_if * http);
  235. int (* send)(struct fnet_http_if * http);
  236. void (* close)(struct fnet_http_if * http);
  237. };
  238. /************************************************************************
  239. * File handler structure.
  240. *************************************************************************/
  241. struct fnet_http_file_handler
  242. {
  243. const char * file_extension; /* File extension */
  244. int (*file_handle)(struct fnet_http_if * http, struct fnet_http_uri * uri);
  245. unsigned long (*file_send)(struct fnet_http_if * http);
  246. void (*file_close)(struct fnet_http_if * http);
  247. };
  248. /************************************************************************
  249. * File content type structure.
  250. *************************************************************************/
  251. struct fnet_http_content_type
  252. {
  253. const char * file_extension; /* File extension */
  254. const char * content_type; /* Content type string */
  255. };
  256. extern const struct fnet_http_file_handler fnet_http_cgi_handler;
  257. extern const struct fnet_http_method fnet_http_method_get;
  258. #if FNET_CFG_HTTP_POST
  259. extern const struct fnet_http_method fnet_http_method_post;
  260. #endif
  261. int fnet_http_default_handle (struct fnet_http_if * http, struct fnet_http_uri * uri);
  262. unsigned long fnet_http_default_send (struct fnet_http_if * http);
  263. void fnet_http_default_close (struct fnet_http_if * http);
  264. char *fnet_http_uri_parse(char * in_str, struct fnet_http_uri * uri);
  265. const struct fnet_http_file_handler * fnet_http_find_handler(struct fnet_http_if * http, struct fnet_http_uri * uri);
  266. const struct fnet_http_content_type * fnet_http_find_content_type(struct fnet_http_if * http, struct fnet_http_uri * uri);
  267. #endif /* FNET_CFG_HTTP && FNET_CFG_FS */
  268. #endif /* _FNET_HTTP_PRV_H_ */