thttp.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file thttp.h
  23. * @brief HTTP (RFC 2616) and HTTP basic/digest authetication (RFC 2617) implementations.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #ifndef TINYHTTP_THTTP_H
  29. #define TINYHTTP_THTTP_H
  30. #include "tinyhttp_config.h"
  31. #include "tinyhttp/thttp_event.h"
  32. #include "tinyhttp/thttp_session.h"
  33. #include "tnet_transport.h"
  34. /**@def THTTP_STACK_SET_NULL()
  35. * Ends stack parameters. Must always be the last one.
  36. */
  37. /**@def THTTP_STACK_SET_LOCAL_IP(STR)
  38. * Sets local IP address to bind to. By default, the stack will bind to "0.0.0.0" or "::" depending on
  39. * whether IPv4 is used or not (IPv6).
  40. * This is a helper macro for @ref thttp_stack_create and @ref thttp_stack_set.
  41. * @param IP_STR The IP address (const char*).
  42. *
  43. * @code
  44. * thttp_stack_create(callback,
  45. * THTTP_STACK_SET_LOCAL_IP("192.168.0.15"),
  46. * THTTP_STACK_SET_NULL());
  47. * @endcode
  48. *
  49. * @sa @ref THTTP_STACK_SET_LOCAL_PORT<br>@ref thttp_stack_create<br>@ref thttp_stack_set
  50. */
  51. /**@def THTTP_STACK_SET_LOCAL_PORT(PORT_INT)
  52. * Sets local Port to bind to. By default, the stack will bind to a random port.
  53. * This is a helper macro for @ref thttp_stack_create and @ref thttp_stack_set.
  54. * @param PORT_INT The Port (int32_t).
  55. *
  56. * @code
  57. * thttp_stack_create(callback,
  58. * THTTP_STACK_SET_LOCAL_PORT(1234),
  59. * THTTP_STACK_SET_NULL());
  60. * @endcode
  61. * @sa @ref THTTP_STACK_SET_LOCAL_IP<br>@ref thttp_stack_create<br>@ref thttp_stack_set
  62. */
  63. /**@def THTTP_STACK_SET_TLS_CERTS(CA_FILE_STR, PUB_FILE_STR, PRIV_FILE_STR)
  64. * Sets TLS certificates (Mutual Authentication). Not mandatory.
  65. * This is a helper macro for @ref thttp_stack_create and @ref thttp_stack_set.
  66. * @param CA_FILE_STR Path to the Certification Authority File.
  67. * @param PUB_FILE_STR Path to the Public key file.
  68. * @param PRIV_FILE_STR Path to the Private key file.
  69. *
  70. * @code
  71. * thttp_stack_create(callback,
  72. * THTTP_STACK_SET_TLS_CERTS("C:\\tls\\ca.pki-crt.pem", "C:\\tls\\pub-crt.pem", "C:\\tls\\priv-key.pem"),
  73. * THTTP_STACK_SET_NULL());
  74. * @endcode
  75. */
  76. THTTP_BEGIN_DECLS
  77. typedef enum thttp_stack_param_type_e {
  78. thttp_pname_null = 0,
  79. #define THTTP_STACK_SET_NULL() thttp_pname_null
  80. /* Network */
  81. thttp_pname_local_ip,
  82. thttp_pname_local_port,
  83. thttp_pname_proxy,
  84. #define THTTP_STACK_SET_LOCAL_IP(IP_STR) thttp_pname_local_ip, (const char*)IP_STR
  85. #define THTTP_STACK_SET_LOCAL_PORT(PORT_INT) thttp_pname_local_port, (int)PORT_INT
  86. #define THTTP_STACK_SET_PROXY(IP_STR, PORT_INT) thttp_pname_proxy, (const char*)IP_STR, (int)PORT_INT
  87. /* Modes */
  88. thttp_pname_mode_client,
  89. thttp_pname_mode_server,
  90. #define THTTP_STACK_SET_MODE_CLIENT() thttp_pname_mode_client
  91. #define THTTP_STACK_SET_MODE_SERVER() thttp_pname_mode_server
  92. /* TLS */
  93. thttp_pname_tls_enabled,
  94. #define THTTP_STACK_SET_TLS_ENABLED(ENABLED_BOOL) thttp_pname_tls_enabled, (tsk_bool_t)ENABLED_BOOL
  95. thttp_pname_tls_certs_verify,
  96. #define THTTP_STACK_SET_TLS_CERTS_VERIFY(CERTS_VERIFY_BOOL) thttp_pname_tls_certs_verify, (tsk_bool_t)CERTS_VERIFY_BOOL
  97. thttp_pname_tls_certs,
  98. #define THTTP_STACK_SET_TLS_CERTS(CA_FILE_STR, PUB_FILE_STR, PRIV_FILE_STR) thttp_pname_tls_certs, (const char*)CA_FILE_STR, (const char*)PUB_FILE_STR, (const char*)PRIV_FILE_STR
  99. /* User Data */
  100. thttp_pname_userdata,
  101. #define THTTP_STACK_SET_USERDATA(USERDATA_PTR) thttp_pname_userdata, (const void*)USERDATA_PTR
  102. }
  103. thttp_stack_param_type_t;
  104. typedef enum thttp_stack_mode_e {
  105. thttp_stack_mode_none,
  106. thttp_stack_mode_client = (0x01 << 0),
  107. thttp_stack_mode_server = (0x01 << 1),
  108. thttp_stack_mode_hybrid = (thttp_stack_mode_client | thttp_stack_mode_server)
  109. }
  110. thttp_stack_mode_t;
  111. /** HTTP/HTTPS Stack.
  112. */
  113. typedef struct thttp_stack_s {
  114. TSK_DECLARE_OBJECT;
  115. tsk_bool_t started;
  116. enum thttp_stack_mode_e mode;
  117. thttp_stack_callback_f callback;
  118. /* Network */
  119. char* local_ip;
  120. int local_port;
  121. char* proxy_ip;
  122. int proxy_port;
  123. tnet_transport_t *transport;
  124. /* TLS */
  125. struct {
  126. tsk_bool_t enabled;
  127. tsk_bool_t verify;
  128. char* ca;
  129. char* pbk;
  130. char* pvk;
  131. } tls;
  132. thttp_sessions_L_t* sessions;
  133. const void* userdata;
  134. TSK_DECLARE_SAFEOBJ;
  135. }
  136. thttp_stack_t;
  137. TINYHTTP_API int thttp_startup();
  138. TINYHTTP_API int thttp_cleanup();
  139. TINYHTTP_API thttp_stack_handle_t *thttp_stack_create(thttp_stack_callback_f callback, ...);
  140. TINYHTTP_API int thttp_stack_start(thttp_stack_handle_t *self);
  141. TINYHTTP_API int thttp_stack_set(thttp_stack_handle_t *self, ...);
  142. TINYHTTP_API const void* thttp_stack_get_userdata(thttp_stack_handle_t *self);
  143. TINYHTTP_API int thttp_stack_stop(thttp_stack_handle_t *self);
  144. TINYHTTP_GEXTERN const tsk_object_def_t *thttp_stack_def_t;
  145. THTTP_END_DECLS
  146. #endif /* TINYHTTP_THTTP_H */