ap_expr.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. {* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *}
  16. (**
  17. * @file ap_expr.h
  18. * @brief Expression parser
  19. *
  20. * @defgroup AP_EXPR Expression parser
  21. * @ingroup APACHE_CORE
  22. * @{
  23. *)
  24. //#ifndef AP_EXPR_H
  25. //#define AP_EXPR_H
  26. //#include "httpd.h"
  27. //#include "http_config.h"
  28. //#include "ap_regex.h"
  29. //** A node in the expression parse tree */
  30. //typedef struct ap_expr_node ap_expr_t;
  31. type
  32. ap_expr_node_op_e = (
  33. op_NOP,
  34. op_True, op_False,
  35. op_Not, op_Or, op_And,
  36. op_Comp,
  37. op_EQ, op_NE, op_LT, op_LE, op_GT, op_GE, op_IN,
  38. op_REG, op_NRE,
  39. op_STR_EQ, op_STR_NE, op_STR_LT, op_STR_LE, op_STR_GT, op_STR_GE,
  40. op_Concat,
  41. op_Digit, op_String, op_Regex, op_RegexBackref,
  42. op_Var,
  43. op_ListElement,
  44. {*
  45. * call external functions/operators.
  46. * The info node contains the function pointer and some function specific
  47. * info.
  48. * For Binary operators, the Call node links to the Info node and the
  49. * Args node, which in turn links to the left and right operand.
  50. * For all other variants, the Call node links to the Info node and the
  51. * argument.
  52. *}
  53. op_UnaryOpCall, op_UnaryOpInfo,
  54. op_BinaryOpCall, op_BinaryOpInfo, op_BinaryOpArgs,
  55. op_StringFuncCall, op_StringFuncInfo,
  56. op_ListFuncCall, op_ListFuncInfo
  57. );
  58. ap_expr_node = record {fpc -> from httpd-X-X-X/server/util_expr_private.h}
  59. node_op: ap_expr_node_op_e;
  60. node_arg1: Pointer;
  61. node_arg2: Pointer;
  62. end;
  63. ap_expr_t = ap_expr_node;
  64. Pap_expr_t = ^ap_expr_t;
  65. //** Struct describing a parsed expression */
  66. Pap_expr_info_t = ^ap_expr_info_t;
  67. ap_expr_info_t = record
  68. //** The root of the actual expression parse tree */
  69. root_node: Pap_expr_t;
  70. {** The filename where the expression has been defined (for logging).
  71. * May be NULL
  72. *}
  73. filename: PChar;
  74. //** The line number where the expression has been defined (for logging). */
  75. line_number: cuint;
  76. //** Flags relevant for the expression, see AP_EXPR_FLAG_* */
  77. flags: cuint;
  78. //** The module that is used for loglevel configuration */
  79. module_index: Integer;
  80. end; {ap_expr_info_t}
  81. {** Use ssl_expr compatibility mode (changes the meaning of the comparison
  82. * operators)
  83. *}
  84. const
  85. AP_EXPR_FLAG_SSL_EXPR_COMPAT = 1;
  86. //** Don't add siginificant request headers to the Vary response header */
  87. AP_EXPR_FLAG_DONT_VARY = 2;
  88. {** Don't allow functions/vars that bypass the current request's access
  89. * restrictions or would otherwise leak confidential information.
  90. * Used by e.g. mod_include.
  91. *}
  92. AP_EXPR_FLAG_RESTRICTED = 4;
  93. //** Expression evaluates to a string, not to a bool */
  94. AP_EXPR_FLAG_STRING_RESULT = 8;
  95. {**
  96. * Evaluate a parse tree, simple interface
  97. * @param r The current request
  98. * @param expr The expression to be evaluated
  99. * @param err Where an error message should be stored
  100. * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
  101. * @note err will be set to NULL on success, or to an error message on error
  102. * @note request headers used during evaluation will be added to the Vary:
  103. * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
  104. *}
  105. //AP_DECLARE(int) ap_expr_exec(request_rec *r, const ap_expr_info_t *expr,
  106. // const char **err);
  107. function ap_expr_exec(r: Prequest_rec; const expr: Pap_expr_info_t; err: PPChar): Integer;
  108. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  109. external LibHTTPD name LibNamePrefix + 'ap_expr_exec' + LibSuff12;
  110. {**
  111. * Evaluate a parse tree, with access to regexp backreference
  112. * @param r The current request
  113. * @param expr The expression to be evaluated
  114. * @param nmatch size of the regex match vector pmatch
  115. * @param pmatch information about regex matches
  116. * @param source the string that pmatch applies to
  117. * @param err Where an error message should be stored
  118. * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
  119. * @note err will be set to NULL on success, or to an error message on error
  120. * @note nmatch/pmatch/source can be used both to make previous matches
  121. * available to ap_expr_exec_re and to use ap_expr_exec_re's matches
  122. * later on.
  123. * @note request headers used during evaluation will be added to the Vary:
  124. * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
  125. *}
  126. //AP_DECLARE(int) ap_expr_exec_re(request_rec *r, const ap_expr_info_t *expr,
  127. // apr_size_t nmatch, ap_regmatch_t *pmatch,
  128. // const char **source, const char **err);
  129. function ap_expr_exec_re(r: Prequest_rec; const expr: Pap_expr_info_t;
  130. nmatch: apr_size_t; pmatch: Pap_regmatch_t;
  131. const source: PPChar; const err: PPChar): Integer;
  132. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  133. external LibHTTPD name LibNamePrefix + 'ap_expr_exec_re' + LibSuff24;
  134. //** Context used during evaluation of a parse tree, created by ap_expr_exec */
  135. type
  136. Pap_expr_eval_ctx_t = ^ap_expr_eval_ctx_t;
  137. ap_expr_eval_ctx_t = record
  138. //** the current request */
  139. r : Prequest_rec;
  140. //** the current connection */
  141. c : Pconn_rec;
  142. //** the current connection */
  143. s : Pserver_rec;
  144. //** the pool to use */
  145. p : Papr_pool_t;
  146. //** where to store the error string */
  147. err : PPchar;
  148. //** ap_expr_info_t for the expression */
  149. info : Pap_expr_info_t;
  150. //** regex match information for back references */
  151. re_pmatch : Pap_regmatch_t;
  152. //** size of the vector pointed to by re_pmatch */
  153. re_nmatch : apr_size_t;
  154. //** the string corresponding to the re_pmatch */
  155. re_source : PPchar;
  156. {** A string where the comma separated names of headers are stored
  157. * to be later added to the Vary: header. If NULL, the caller is not
  158. * interested in this information.
  159. *}
  160. vary_this : PPchar;
  161. //** where to store the result string */
  162. result_string : PPchar;
  163. //** Arbitrary context data provided by the caller for custom functions */
  164. data : pointer;
  165. //** The current recursion level */
  166. reclvl : Integer;
  167. end; {ap_expr_eval_ctx_t}
  168. {**
  169. * Evaluate a parse tree, full featured version
  170. * @param ctx The evaluation context with all data filled in
  171. * @return > 0 if expression evaluates to true, == 0 if false, < 0 on error
  172. * @note *ctx->err will be set to NULL on success, or to an error message on
  173. * error
  174. * @note request headers used during evaluation will be added to the Vary:
  175. * response header if ctx->vary_this is set.
  176. *}
  177. //AP_DECLARE(int) ap_expr_exec_ctx(ap_expr_eval_ctx_t *ctx);
  178. function ap_expr_exec_ctx(ctx: Pap_expr_eval_ctx_t): Integer;
  179. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  180. external LibHTTPD name LibNamePrefix + 'ap_expr_exec_ctx' + LibSuff4;
  181. {**
  182. * Evaluate a parse tree of a string valued expression
  183. * @param r The current request
  184. * @param expr The expression to be evaluated
  185. * @param err Where an error message should be stored
  186. * @return The result string, NULL on error
  187. * @note err will be set to NULL on success, or to an error message on error
  188. * @note request headers used during evaluation will be added to the Vary:
  189. * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
  190. *}
  191. //AP_DECLARE(const char *) ap_expr_str_exec(request_rec *r,
  192. // const ap_expr_info_t *expr,
  193. // const char **err);
  194. function ap_expr_str_exec(r: Prequest_rec;
  195. const expr: Pap_expr_info_t;
  196. const err: PPChar): PChar;
  197. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  198. external LibHTTPD name LibNamePrefix + 'ap_expr_str_exec' + LibSuff12;
  199. {**
  200. * Evaluate a parse tree of a string valued expression
  201. * @param r The current request
  202. * @param expr The expression to be evaluated
  203. * @param nmatch size of the regex match vector pmatch
  204. * @param pmatch information about regex matches
  205. * @param source the string that pmatch applies to
  206. * @param err Where an error message should be stored
  207. * @return The result string, NULL on error
  208. * @note err will be set to NULL on success, or to an error message on error
  209. * @note nmatch/pmatch/source can be used both to make previous matches
  210. * available to ap_expr_exec_re and to use ap_expr_exec_re's matches
  211. * later on.
  212. * @note request headers used during evaluation will be added to the Vary:
  213. * response header, unless ::AP_EXPR_FLAG_DONT_VARY is set.
  214. *}
  215. //AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
  216. // const ap_expr_info_t *expr,
  217. // apr_size_t nmatch,
  218. // ap_regmatch_t *pmatch,
  219. // const char **source,
  220. // const char **err);
  221. function ap_expr_str_exec_re(r: Prequest_rec;
  222. const expr: Pap_expr_info_t;
  223. nmatch: apr_size_t;
  224. pmatch: Pap_regmatch_t;
  225. const source: PPChar;
  226. const err: PPChar): PChar;
  227. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  228. external LibHTTPD name LibNamePrefix + 'ap_expr_str_exec_re' + LibSuff24;
  229. {**
  230. * The parser can be extended with variable lookup, functions, and
  231. * and operators.
  232. *
  233. * During parsing, the parser calls the lookup function to resolve a
  234. * name into a function pointer and an opaque context for the function.
  235. * If the argument to a function or operator is constant, the lookup function
  236. * may also parse that argument and store the parsed data in the context.
  237. *
  238. * The default lookup function is the hook ::ap_expr_lookup_default which just
  239. * calls ap_run_expr_lookup. Modules can use it to make functions and
  240. * variables generally available.
  241. *
  242. * An ap_expr consumer can also provide its own custom lookup function to
  243. * modify the set of variables and functions that are available. The custom
  244. * lookup function can in turn call 'ap_run_expr_lookup'.
  245. *}
  246. {** Unary operator, takes one string argument and returns a bool value.
  247. * The name must have the form '-z' (one letter only).
  248. * @param ctx The evaluation context
  249. * @param data An opaque context provided by the lookup hook function
  250. * @param arg The (right) operand
  251. * @return 0 or 1
  252. *}
  253. //typedef int ap_expr_op_unary_t(ap_expr_eval_ctx_t *ctx, const void *data,
  254. // const char *arg);
  255. type
  256. ap_expr_op_unary_t = function(ctx: Pap_expr_eval_ctx_t; const data: Pointer;
  257. const arg: PChar): Integer; cdecl;
  258. {** Binary operator, takes two string arguments and returns a bool value.
  259. * The name must have the form '-cmp' (at least two letters).
  260. * @param ctx The evaluation context
  261. * @param data An opaque context provided by the lookup hook function
  262. * @param arg1 The left operand
  263. * @param arg2 The right operand
  264. * @return 0 or 1
  265. *}
  266. //typedef int ap_expr_op_binary_t(ap_expr_eval_ctx_t *ctx, const void *data,
  267. // const char *arg1, const char *arg2);
  268. ap_expr_op_binary_t = function(ctx: Pap_expr_eval_ctx_t; const data: Pointer;
  269. const arg1: PChar; const arg2: PChar): Integer; cdecl;
  270. {** String valued function, takes a string argument and returns a string
  271. * @param ctx The evaluation context
  272. * @param data An opaque context provided by the lookup hook function
  273. * @param arg The argument
  274. * @return The functions result string, may be NULL for 'empty string'
  275. *}
  276. //typedef const char *(ap_expr_string_func_t)(ap_expr_eval_ctx_t *ctx,
  277. // const void *data,
  278. // const char *arg);
  279. ap_expr_string_func_t = function(ctx: Pap_expr_eval_ctx_t;
  280. const data: Pointer;
  281. const arg: PChar): PChar; cdecl;
  282. {** List valued function, takes a string argument and returns a list of strings
  283. * Can currently only be called following the builtin '-in' operator.
  284. * @param ctx The evaluation context
  285. * @param data An opaque context provided by the lookup hook function
  286. * @param arg The argument
  287. * @return The functions result list of strings, may be NULL for 'empty array'
  288. *}
  289. //typedef apr_array_header_t *(ap_expr_list_func_t)(ap_expr_eval_ctx_t *ctx,
  290. // const void *data,
  291. // const char *arg);
  292. ap_expr_list_func_t = function(ctx: Pap_expr_eval_ctx_t;
  293. const data: Pointer;
  294. const arg: PChar): Papr_array_header_t;
  295. {** Variable lookup function, takes no argument and returns a string
  296. * @param ctx The evaluation context
  297. * @param data An opaque context provided by the lookup hook function
  298. * @return The expanded variable
  299. *}
  300. //typedef const char *(ap_expr_var_func_t)(ap_expr_eval_ctx_t *ctx,
  301. // const void *data);
  302. ap_expr_var_func_t = function(ctx: Pap_expr_eval_ctx_t;
  303. const data: Pointer): PChar; cdecl;
  304. const
  305. AP_EXPR_FUNC_VAR = 0;
  306. AP_EXPR_FUNC_STRING = 1;
  307. AP_EXPR_FUNC_LIST = 2;
  308. AP_EXPR_FUNC_OP_UNARY = 3;
  309. AP_EXPR_FUNC_OP_BINARY = 4;
  310. //** parameter struct passed to the lookup hook functions */
  311. type
  312. Pap_expr_lookup_parms = ^ap_expr_lookup_parms;
  313. ap_expr_lookup_parms = record
  314. //** type of the looked up object */
  315. type_: Integer;
  316. //#define AP_EXPR_FUNC_VAR 0 {fpc -> consts are moved up}
  317. //#define AP_EXPR_FUNC_STRING 1
  318. //#define AP_EXPR_FUNC_LIST 2
  319. //#define AP_EXPR_FUNC_OP_UNARY 3
  320. //#define AP_EXPR_FUNC_OP_BINARY 4
  321. //** name of the looked up object */
  322. name: PChar;
  323. flags: Integer;
  324. pool: Papr_pool_t;
  325. ptemp: Papr_pool_t;
  326. //** where to store the function pointer */
  327. func: PPointer;
  328. //** where to store the function's context */
  329. data: PPointer;
  330. //** where to store the error message (if any) */
  331. err: PPChar;
  332. {** arg for pre-parsing (only if a simple string).
  333. * For binary ops, this is the right argument. *}
  334. arg: PChar;
  335. end; {ap_expr_lookup_parms}
  336. {** Function for looking up the provider function for a variable, operator
  337. * or function in an expression.
  338. * @param parms The parameter struct, also determins where the result is
  339. * stored.
  340. * @return OK on success,
  341. * !OK on failure,
  342. * DECLINED if the requested name is not handled by this function
  343. *}
  344. //typedef int (ap_expr_lookup_fn_t)(ap_expr_lookup_parms *parms);
  345. type
  346. ap_expr_lookup_fn_t = function(parms: ap_expr_lookup_parms): Integer; cdecl;
  347. {** Default lookup function which just calls ap_run_expr_lookup().
  348. * ap_run_expr_lookup cannot be used directly because it has the wrong
  349. * calling convention under Windows.
  350. *}
  351. //AP_DECLARE_NONSTD(int) ap_expr_lookup_default(ap_expr_lookup_parms *parms);
  352. function ap_expr_lookup_default(parms: Pap_expr_lookup_parms): Integer; cdecl;
  353. external LibHTTPD name LibNamePrefix + 'ap_expr_lookup_default';
  354. //AP_DECLARE_HOOK(int, expr_lookup, (ap_expr_lookup_parms *parms))
  355. {macro ignored for now}
  356. {**
  357. * Parse an expression into a parse tree
  358. * @param pool Pool
  359. * @param ptemp temp pool
  360. * @param info The ap_expr_info_t struct (with values filled in)
  361. * @param expr The expression string to parse
  362. * @param lookup_fn The lookup function to use, NULL for default
  363. * @return NULL on success, error message on error.
  364. * A pointer to the resulting parse tree will be stored in
  365. * info->root_node.
  366. *}
  367. //AP_DECLARE(const char *) ap_expr_parse(apr_pool_t *pool, apr_pool_t *ptemp,
  368. // ap_expr_info_t *info, const char *expr,
  369. // ap_expr_lookup_fn_t *lookup_fn);
  370. function ap_expr_parse(pool, ptemp: Papr_pool_t;
  371. info: Pap_expr_info_t;
  372. const expr: PChar;
  373. lookup_fn: ap_expr_lookup_fn_t): PChar;
  374. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  375. external LibHTTPD name LibNamePrefix + 'ap_expr_parse' + LibSuff20;
  376. {**
  377. * High level interface to ap_expr_parse that also creates ap_expr_info_t and
  378. * uses info from cmd_parms to fill in most of it.
  379. * @param cmd The cmd_parms struct
  380. * @param expr The expression string to parse
  381. * @param flags The flags to use, see AP_EXPR_FLAG_*
  382. * @param err Set to NULL on success, error message on error
  383. * @param lookup_fn The lookup function used to lookup vars, functions, and
  384. * operators
  385. * @param module_index The module_index to set for the expression
  386. * @return The parsed expression
  387. * @note Usually ap_expr_parse_cmd() should be used
  388. *}
  389. //AP_DECLARE(ap_expr_info_t *) ap_expr_parse_cmd_mi(const cmd_parms *cmd,
  390. // const char *expr,
  391. // unsigned int flags,
  392. // const char **err,
  393. // ap_expr_lookup_fn_t *lookup_fn,
  394. // int module_index);
  395. function ap_expr_parse_cmd_mi(cmd: Pcmd_parms;
  396. const expr: PChar;
  397. flags: cuint;
  398. const err: PPChar;
  399. lookup_fn: ap_expr_lookup_fn_t;
  400. module_index: Integer): PChar;
  401. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  402. external LibHTTPD name LibNamePrefix + 'ap_expr_parse_cmd_mi' + LibSuff24;
  403. {**
  404. * Convenience wrapper for ap_expr_parse_cmd_mi() that sets
  405. * module_index = APLOG_MODULE_INDEX
  406. *}
  407. //#define ap_expr_parse_cmd(cmd, expr, flags, err, lookup_fn) \
  408. // ap_expr_parse_cmd_mi(cmd, expr, flags, err, lookup_fn, APLOG_MODULE_INDEX)
  409. {**
  410. * Internal initialisation of ap_expr (for httpd internal use)
  411. *}
  412. //void ap_expr_init(apr_pool_t *pool);
  413. //#endif /* AP_EXPR_H */
  414. //** @} */