appweb.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. appweb.h -- Embedthis Appweb HTTP Web Server header
  3. Copyright (c) All Rights Reserved. See details at the end of the file.
  4. */
  5. #ifndef _h_APPWEB
  6. #define _h_APPWEB 1
  7. /********************************* Includes ***********************************/
  8. #include "osdep.h"
  9. #include "mpr.h"
  10. #include "http.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /********************************* Tunables ***********************************/
  15. #define MA_UNLOAD_TIMEOUT "5mins" /**< Default module inactivity timeout */
  16. /********************************** Defines ***********************************/
  17. /*
  18. Pack defaults
  19. */
  20. #ifndef ME_COM_CGI
  21. #define ME_COM_CGI 0
  22. #endif
  23. #ifndef ME_COM_EJSCRIPT
  24. #define ME_COM_EJSCRIPT 0
  25. #endif
  26. #ifndef ME_COM_ESP
  27. #define ME_COM_ESP 0
  28. #endif
  29. #ifndef ME_COM_FAST
  30. #define ME_COM_FAST 0
  31. #endif
  32. #ifndef ME_COM_MDB
  33. #define ME_COM_MDB 0
  34. #endif
  35. #ifndef ME_COM_PHP
  36. #define ME_COM_PHP 0
  37. #endif
  38. #ifndef ME_COM_PROXY
  39. #define ME_COM_PROXY 0
  40. #endif
  41. #ifndef ME_COM_SDB
  42. #define ME_COM_SDB 0
  43. #endif
  44. #ifndef ME_COM_SSL
  45. #define ME_COM_SSL 0
  46. #endif
  47. #ifndef ME_COM_TEST
  48. #define ME_COM_TEST 0
  49. #endif
  50. /******************************************************************************/
  51. /*
  52. State flags
  53. */
  54. #define MA_PARSE_NON_SERVER 0x1 /**< Command file being parsed by a utility program */
  55. /**
  56. Current configuration parse state
  57. @stability Evolving
  58. @defgroup MaState MaState
  59. @see MaDirective MaState maAddDirective maArchiveLog maPopState maPushState maTokenize
  60. @stability Evolving
  61. */
  62. typedef struct MaState {
  63. HttpHost *host; /**< Current host */
  64. HttpAuth *auth; /**< Quick alias for route->auth */
  65. HttpRoute *route; /**< Current route */
  66. MprFile *file; /**< Config file handle */
  67. char *key; /**< Current directive being parsed */
  68. char *configDir; /**< Directory containing config file */
  69. char *filename; /**< Config file name */
  70. char *endpoints; /**< Virtual host endpoints */
  71. char *data; /**< Config data (managed) */
  72. int lineNumber; /**< Current line number */
  73. int enabled; /**< True if the current block is enabled */
  74. int flags; /**< Parsing flags */
  75. struct MaState *prev; /**< Previous (inherited) state */
  76. struct MaState *top; /**< Top level state */
  77. struct MaState *current; /**< Current state */
  78. } MaState;
  79. /**
  80. Appweb configuration file directive parsing callback function
  81. @description Directive callbacks are invoked to parse a directive. Directive callbacks are registered using
  82. #maAddDirective.
  83. @param state Current config parse state.
  84. @param key Directive key name
  85. @param value Directive key value
  86. @return Zero if successful, otherwise a negative Mpr error code. See the Appweb log for diagnostics.
  87. @ingroup MaState
  88. @stability Evolving
  89. */
  90. typedef int (MaDirective)(MaState *state, cchar *key, cchar *value);
  91. /**
  92. Define a new appweb configuration file directive
  93. @description The appweb configuration file parse is extensible. New directives can be registered by this call. When
  94. encountered in the config file, the given callback proc will be invoked to parse.
  95. @param directive Directive name
  96. @param proc Directive callback procedure of the type #MaDirective.
  97. @ingroup MaState
  98. @stability Evolving
  99. */
  100. PUBLIC void maAddDirective(cchar *directive, MaDirective proc);
  101. /**
  102. Configure a web server
  103. @description This will configure a web server based on either a configuration file or using the supplied
  104. IP address and port.
  105. @param configFile File name of the Appweb configuration file (appweb.conf) that defines the web server configuration.
  106. @param home Admin directory for the server. This overrides the value in the config file.
  107. @param documents Default directory for web documents to serve. This overrides the value in the config file.
  108. @param ip IP address to listen on. This overrides the value specified in the config file.
  109. @param port Port address to listen on. This overrides the value specified in the config file.
  110. @return Zero if successful, otherwise a negative Mpr error code. See the Appweb log for diagnostics.
  111. @ingroup MaState
  112. @stability Evolving
  113. */
  114. PUBLIC int maConfigureServer(cchar *configFile, cchar *home, cchar *documents, cchar *ip, int port);
  115. /**
  116. Get the argument in a directive
  117. @description Break into arguments. Args may be quoted. An outer quoting of the entire arg is removed.
  118. @param s String to examine
  119. @param tok Next token reference
  120. @return Reference to the next token. (Not allocate
  121. @ingroup MaState
  122. @stability Evolving
  123. */
  124. PUBLIC char *maGetNextArg(char *s, char **tok);
  125. /**
  126. Load an appweb module
  127. @description Load an appweb module. If the module is already loaded, this call will return successfully without
  128. reloading. Modules can be dynamically loaded or may also be pre-loaded using static linking.
  129. @param name User name. Must be defined in the system password file.
  130. @param libname Library path name
  131. @return Zero if successful, otherwise a negative Mpr error code. See the Appweb log for diagnostics.
  132. @ingroup MaState
  133. @stability Evolving
  134. */
  135. PUBLIC int maLoadModule(cchar *name, cchar *libname);
  136. /**
  137. Load default modules
  138. @return Zero if successful, otherwise a negative Mpr error code. See the Appweb log for diagnostics.
  139. @ingroup MaState
  140. @stability Prototype
  141. */
  142. PUBLIC int maLoadModules(void);
  143. /**
  144. Parse an Appweb configuration file
  145. @description Parse the configuration file and configure the server. This creates a default host and route
  146. and then configures the server based on config file directives.
  147. @param path Configuration file pathname.
  148. @return Zero if successful, otherwise a negative Mpr error code. See the Appweb log for diagnostics.
  149. @ingroup MaState
  150. @stability Evolving
  151. */
  152. PUBLIC int maParseConfig(cchar *path);
  153. /**
  154. Parse a configuration file
  155. @param state Current state level object
  156. @param path Filename to parse
  157. @return Zero if successful, otherwise a negative Mpr error code. See the Appweb log for diagnostics.
  158. @ingroup MaState
  159. @stability Prototype
  160. */
  161. PUBLIC int maParseFile(MaState *state, cchar *path);
  162. /**
  163. Pop the state
  164. @description This is used when parsing config files to handle nested include files and block level directives
  165. @param state Current state
  166. @return The next lower level state object
  167. @ingroup MaState
  168. @stability Evolving
  169. */
  170. PUBLIC MaState *maPopState(MaState *state);
  171. /**
  172. Push the state
  173. @description This is used when parsing config files to handle nested include files and block level directives
  174. @param state Current state
  175. @return The state passed as a parameter which becomes the new top level state
  176. @ingroup MaState
  177. @stability Evolving
  178. */
  179. PUBLIC MaState *maPushState(MaState *state);
  180. /**
  181. Tokenize a string based on route data
  182. @description This is a utility routine to parse a string into tokens given a format specifier.
  183. Mandatory tokens can be specified with "%" format specifier. Optional tokens are specified with "?" format.
  184. Values wrapped in quotes will have the outermost quotes trimmed.
  185. @param state Current config parsing state
  186. @param str String to expand
  187. @param fmt Format string specifier
  188. Supported tokens:
  189. <ul>
  190. <li>%B - Boolean. Parses: on/off, true/false, yes/no.</li>
  191. <li>%N - Number. Parses numbers in base 10.</li>
  192. <li>%S - String. Removes quotes.</li>
  193. <li>%P - Path string. Removes quotes and expands ${PathVars}. Resolved relative to host->dir (ServerRoot).</li>
  194. <li>%W - Parse words into a list</li>
  195. <li>%! - Optional negate. Set value to HTTP_ROUTE_NOT present, otherwise zero.</li>
  196. </ul>
  197. @return True if the string can be successfully parsed.
  198. @ingroup MaState
  199. @stability Evolving
  200. */
  201. PUBLIC bool maTokenize(MaState *state, cchar *str, cchar *fmt, ...);
  202. /**
  203. Create and run a simple web server listening on a single IP address.
  204. @description Create a simple web server without using a configuration file. The server is created to listen on
  205. the specified IP address and port. This routine provides a one-line embedding of Appweb. If you want to
  206. use a config file, try the #maRunWebServer instead.
  207. @param ip IP address on which to listen. Set to "0.0.0.0" to listen on all interfaces.
  208. @param port Port number to listen to
  209. @param home Home directory for the web server
  210. @param documents Directory containing the documents to serve.
  211. @return Zero if successful, otherwise a negative Mpr error code. See the Appweb log for diagnostics.
  212. @ingroup MaState
  213. @stability Evolving
  214. */
  215. PUBLIC int maRunSimpleWebServer(cchar *ip, int port, cchar *home, cchar *documents);
  216. /**
  217. Create and run a web server based on a configuration file
  218. @description Create a web server configuration based on the supplied config file. This routine provides
  219. a one-line embedding of Appweb. If you don't want to use a config file, try the #maRunSimpleWebServer
  220. instead.
  221. @param configFile File name of the Appweb configuration file (appweb.conf) that defines the web server configuration.
  222. @return Zero if successful, otherwise a negative Mpr error code. See the Appweb log for diagnostics.
  223. @ingroup MaState
  224. @stability Evolving
  225. */
  226. PUBLIC int maRunWebServer(cchar *configFile);
  227. /**
  228. Save the authorization configuration to a file
  229. AuthFile schema:
  230. User name password abilities...
  231. Role name abilities...
  232. @param auth Auth object allocated by #httpCreateAuth.
  233. @param path Path name of file
  234. @return "Zero" if successful, otherwise a negative MPR error code
  235. @ingroup HttpAuth
  236. @stability Internal
  237. @internal
  238. */
  239. PUBLIC int maWriteAuthFile(HttpAuth *auth, char *path);
  240. /*
  241. Internal
  242. */
  243. #if ME_COM_CGI
  244. PUBLIC int httpCgiInit(Http *http, MprModule *mp);
  245. #endif
  246. #if ME_COM_FAST
  247. PUBLIC int httpFastInit(Http *http, MprModule *mp);
  248. #endif
  249. #if ME_COM_ESP
  250. PUBLIC int httpEspInit(Http *http, MprModule *mp);
  251. #endif
  252. #if ME_COM_PROXY
  253. PUBLIC int httpProxyInit(Http *http, MprModule *mp);
  254. #endif
  255. #if ME_COM_TEST
  256. PUBLIC int httpTestInit(Http *http, MprModule *mp);
  257. #endif
  258. PUBLIC int maTraceDirective(MaState *state, HttpTrace *trace, cchar *key, cchar *value);
  259. PUBLIC int maTraceLogDirective(MaState *state, HttpTrace *trace, cchar *key, cchar *value);
  260. #ifdef __cplusplus
  261. } /* extern C */
  262. #endif
  263. /*
  264. Permit overrides
  265. */
  266. #if ME_CUSTOMIZE
  267. #include "customize.h"
  268. #endif
  269. #endif /* _h_APPWEB */
  270. /*
  271. Copyright (c) Embedthis Software. All Rights Reserved.
  272. This software is distributed under a commercial license. Consult the LICENSE.md
  273. distributed with this software for full details and copyrights.
  274. */