thttp_message.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2010-2015 Mamadou DIOP.
  3. *
  4. *
  5. * This file is part of Open Source Doubango Framework.
  6. *
  7. * DOUBANGO is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * DOUBANGO is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with DOUBANGO.
  19. *
  20. */
  21. /**@file thttp_message.h
  22. * @brief Represents a HTTP message. A HTTP message is either a request from a client to a server, or a
  23. * response from a server to a client.
  24. *
  25. */
  26. #ifndef THTTP_MESSAGE_H
  27. #define THTTP_MESSAGE_H
  28. #include "tinyhttp_config.h"
  29. #include "tinyhttp/thttp_url.h"
  30. //#include "tinyhttp/headers/thttp_header_Call_ID.h"
  31. //#include "tinyhttp/headers/thttp_header_Contact.h"
  32. #include "tinyhttp/headers/thttp_header_Content_Length.h"
  33. #include "tinyhttp/headers/thttp_header_Content_Type.h"
  34. //#include "tinyhttp/headers/thttp_header_CSeq.h"
  35. //#include "tinyhttp/headers/thttp_header_Expires.h"
  36. //#include "tinyhttp/headers/thttp_header_From.h"
  37. //#include "tinyhttp/headers/thttp_header_P_Access_Network_Info.h"
  38. //#include "tinyhttp/headers/thttp_header_Via.h"
  39. #include "tsk_object.h"
  40. #include "tsk_buffer.h"
  41. /**@ingroup thttp_message_group
  42. * @def THTTP_MESSAGE_VERSION_10
  43. * HTTP version 1.0.
  44. */
  45. /**@ingroup thttp_message_group
  46. * @def THTTP_MESSAGE_VERSION_11
  47. * HTTP version 1.1.
  48. */
  49. /**@ingroup thttp_message_group
  50. * @def THTTP_MESSAGE_VERSION_20
  51. * HTTP version 2.0.
  52. */
  53. /**@ingroup thttp_message_group
  54. * @def THTTP_MESSAGE_VERSION_DEFAULT
  55. * Default HTTP version. Default value is @ref THTTP_MESSAGE_VERSION_11.
  56. */
  57. THTTP_BEGIN_DECLS
  58. #define THTTP_MESSAGE_VERSION_10 "HTTP/1.0"
  59. #define THTTP_MESSAGE_VERSION_11 "HTTP/1.1"
  60. #define THTTP_MESSAGE_VERSION_20 "HTTP/2.0"
  61. #define THTTP_MESSAGE_VERSION_DEFAULT THTTP_MESSAGE_VERSION_11
  62. /**@ingroup thttp_message_group
  63. * @def THTTP_MESSAGE_IS_REQUEST
  64. * Checks whether the HTTP message is a request or not.
  65. * @param self A pointer to a valid @ref thttp_message_t object.
  66. * @retval @ref tsk_true if @a self is a request and @a tsk_false otherwise.
  67. */
  68. /**@ingroup thttp_message_group
  69. * @def THTTP_MESSAGE_IS_RESPONSE
  70. * Checks whether the HTTP message is a response or not.
  71. * @param self A pointer to a valid @ref thttp_message_t object.
  72. * @retval @ref tsk_true if @a self is a response and @a tsk_false otherwise.
  73. */
  74. #define THTTP_MESSAGE_IS_REQUEST(self) ((self) ? (self)->type == thttp_request : tsk_false)
  75. #define THTTP_MESSAGE_IS_RESPONSE(self) ((self) ? (self)->type == thttp_response : tsk_false)
  76. /**@ingroup thttp_message_group
  77. * @def THTTP_MESSAGE
  78. * Casts any pointer to a pointer to @ref thttp_message_t.
  79. * @retval A pointer to @ref thttp_message_t.
  80. */
  81. /**@ingroup thttp_message_group
  82. * @def THTTP_MESSAGE_AS_RESPONSE
  83. * Casts any pointer to a pointer to @ref thttp_response_t.
  84. * @retval A pointer to @ref thttp_response_t.
  85. */
  86. /**@ingroup thttp_message_group
  87. * @def THTTP_MESSAGE_AS_REQUEST
  88. * Casts any pointer to a pointer to @ref thttp_request_t.
  89. * @retval A pointer to @ref thttp_request_t.
  90. */
  91. #define THTTP_MESSAGE(self) ((thttp_message_t*)(self))
  92. #define THTTP_MESSAGE_AS_RESPONSE(self) ((thttp_response_t*)(self))
  93. #define THTTP_MESSAGE_AS_REQUEST(self) ((thttp_request_t*)(self))
  94. /**@ingroup thttp_message_group
  95. *@def THTTP_RESPONSE_CODE
  96. * Gets the status code of the response.
  97. * @param self A pointer to a @ref thttp_response_t object.
  98. * @retval The status code (short).
  99. */
  100. /**@ingroup thttp_message_group
  101. *@def THTTP_RESPONSE_PHRASE
  102. * Gets the reason phrase of the response.
  103. * @param self A pointer to a @ref thttp_response_t object.
  104. * @retval The phrase (const char*).
  105. */
  106. #define THTTP_RESPONSE_CODE(self) (THTTP_MESSAGE_IS_RESPONSE((self)) ? (self)->line.response.status_code : 0)
  107. #define THTTP_RESPONSE_PHRASE(self) ((self)->line.response.reason_phrase)
  108. /**@ingroup thttp_message_group
  109. *@def THTTP_REQUEST_METHOD
  110. * Gets the method of the request.
  111. * @param self A pointer to a @ref thttp_request_t object.
  112. * @retval The method (const char*).
  113. */
  114. /**@ingroup thttp_message_group
  115. *@def THTTP_REQUEST_URL
  116. * Gets the URL of the request.
  117. * @param self A pointer to a @ref thttp_request_t object.
  118. * @retval The Url (@ref thttp_url_t).
  119. */
  120. #define THTTP_REQUEST_METHOD(self) ((self) ? (self)->line.request.method : tsk_null)
  121. #define THTTP_REQUEST_URL(self) ((self) ? (self)->line.request.url : tsk_null)
  122. /**@ingroup thttp_message_group
  123. *@def THTTP_MESSAGE_CONTENT_LENGTH
  124. * Gets the content length.
  125. * @param self A pointer to a @ref thttp_message_t object.
  126. * @retval The content length (uint32_t).
  127. */
  128. /**@ingroup thttp_message_group
  129. *@def THTTP_MESSAGE_CONTENT
  130. * Gets the content value.
  131. * @param self A pointer to a @ref thttp_message_t object.
  132. * @retval A pointer to the content (void*).
  133. */
  134. /**@ingroup thttp_message_group
  135. *@def THTTP_MESSAGE_HAS_CONTENT
  136. * Checks whether the message has a content or not.
  137. * @param self A pointer to a @ref thttp_message_t object.
  138. * @retval @ref tsk_true if the message has a content and @a tsk_false otherwise.
  139. */
  140. #define THTTP_MESSAGE_CONTENT_LENGTH(self) (uint32_t)(((self) && (self)->Content_Length) ? (self)->Content_Length->length : 0)
  141. #define THTTP_MESSAGE_CONTENT(self) (THTTP_MESSAGE_HAS_CONTENT(self) ? (self)->Content->data : 0)
  142. #define THTTP_MESSAGE_HAS_CONTENT(self) ((self) && (self)->Content)
  143. #define THTTP_RESPONSE_IS(self, code) (THTTP_RESPONSE_CODE((self)) == code)
  144. #define THTTP_RESPONSE_IS_NXX(self, N) (N##00<= THTTP_RESPONSE_CODE((self)) && THTTP_RESPONSE_CODE((self)) <= N##99)
  145. #define THTTP_RESPONSE_IS_1XX(self) THTTP_RESPONSE_IS_NXX(self, 1)
  146. #define THTTP_RESPONSE_IS_2XX(self) THTTP_RESPONSE_IS_NXX(self, 2)
  147. #define THTTP_RESPONSE_IS_3XX(self) THTTP_RESPONSE_IS_NXX(self, 3)
  148. #define THTTP_RESPONSE_IS_4XX(self) THTTP_RESPONSE_IS_NXX(self, 4)
  149. #define THTTP_RESPONSE_IS_5XX(self) THTTP_RESPONSE_IS_NXX(self, 5)
  150. #define THTTP_RESPONSE_IS_6XX(self) THTTP_RESPONSE_IS_NXX(self, 6)
  151. #define THTTP_RESPONSE_IS_23456(self) (200<= THTTP_RESPONSE_CODE((self)) && THTTP_RESPONSE_CODE((self)) <= 699)
  152. /**Defines the message type (Request or Response).
  153. **/
  154. typedef enum thttp_message_type_e {
  155. thttp_unknown,
  156. thttp_request,
  157. thttp_response
  158. }
  159. thttp_message_type_t;
  160. /**Represents a HTTP message. A HTTP message is either a request from a client to a server,
  161. * or a response from a server to a client.
  162. **/
  163. typedef struct thttp_message_s {
  164. TSK_DECLARE_OBJECT;
  165. char *http_version; /**< The HTTP version. Only 'HTTP/1.1' is supported. */
  166. thttp_message_type_t type; /**< The type of this HTTP message. */
  167. /* Request-Line */
  168. union {
  169. struct {
  170. char *method;
  171. thttp_url_t *url;
  172. } request;
  173. struct {
  174. short status_code;
  175. char *reason_phrase;
  176. } response;
  177. } line;
  178. /*== MOST COMMON HEADERS. */
  179. thttp_header_Content_Type_t *Content_Type;
  180. thttp_header_Content_Length_t *Content_Length;
  181. tsk_buffer_t *Content;
  182. /*== OTHER HEADERS*/
  183. thttp_headers_L_t *headers;
  184. }
  185. thttp_message_t;
  186. typedef thttp_message_t thttp_request_t; /**< HTTP request message. */
  187. typedef thttp_message_t thttp_response_t; /**< HTTP response message. */
  188. //
  189. TINYHTTP_API int thttp_message_add_header(thttp_message_t *self, const thttp_header_t *hdr);
  190. TINYHTTP_API int thttp_message_add_headers(thttp_message_t *self, const thttp_headers_L_t *headers);
  191. TINYHTTP_API int thttp_message_add_headers_2(thttp_message_t *self, ...);
  192. TINYHTTP_API int thttp_message_add_content(thttp_message_t *self, const char* content_type, const void* content, tsk_size_t size);
  193. TINYHTTP_API int thttp_message_append_content(thttp_message_t *self, const void* content, tsk_size_t size);
  194. #if defined(__SYMBIAN32__) && 0
  195. static void THTTP_MESSAGE_ADD_HEADER(thttp_message_t *self, ...)
  196. {
  197. va_list ap;
  198. thttp_header_t *header;
  199. const tsk_object_def_t *objdef;
  200. va_start(ap, self);
  201. objdef = va_arg(ap, const tsk_object_def_t*);
  202. header = tsk_object_new_2(objdef, &ap);
  203. va_end(ap);
  204. thttp_message_add_header(self, header);
  205. tsk_object_unref(header);
  206. }
  207. #else
  208. #define THTTP_MESSAGE_ADD_HEADER(self, objdef, ...) \
  209. { \
  210. thttp_header_t *header = (thttp_header_t *)tsk_object_new(objdef, ##__VA_ARGS__); \
  211. thttp_message_add_header(self, header); \
  212. tsk_object_unref(header); \
  213. }
  214. #endif
  215. TINYHTTP_API const thttp_header_t *thttp_message_get_headerAt(const thttp_message_t *self, thttp_header_type_t type, tsk_size_t index);
  216. TINYHTTP_API const thttp_header_t *thttp_message_get_header(const thttp_message_t *self, thttp_header_type_t type);
  217. TINYHTTP_API const thttp_header_t *thttp_message_get_headerByName(const thttp_message_t *self, const char* name);
  218. TINYHTTP_API int thttp_message_serialize(const thttp_message_t *self, tsk_buffer_t *output);
  219. TINYHTTP_API char* thttp_message_tostring(const thttp_message_t *self);
  220. TINYHTTP_API thttp_request_t *thttp_request_new(const char* method, const thttp_url_t *request_url);
  221. TINYHTTP_API thttp_response_t *thttp_response_new(short status_code, const char* reason_phrase, const thttp_request_t *request);
  222. TINYHTTP_API thttp_message_t* thttp_message_create();
  223. TINYHTTP_API thttp_request_t* thttp_request_create(const char* method, const thttp_url_t* url);
  224. TINYHTTP_GEXTERN const tsk_object_def_t *thttp_message_def_t;
  225. THTTP_END_DECLS
  226. #endif /* THTTP_MESSAGE_H */