]> granicus.if.org Git - apache/blob - docs/manual/expr.xml
Add REQUEST_STATUS variable to ap_expr
[apache] / docs / manual / expr.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "./style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="./style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  You may obtain a copy of the License at
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
16  Unless required by applicable law or agreed to in writing, software
17  distributed under the License is distributed on an "AS IS" BASIS,
18  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  See the License for the specific language governing permissions and
20  limitations under the License.
21 -->
22
23 <manualpage metafile="expr.xml.meta">
24
25   <title>Expressions in Apache HTTP Server</title>
26
27   <summary>
28     <p>Historically, there are several syntax variants for expressions used to express
29         a condition in the different modules of the Apache HTTP Server.
30         There is some ongoing effort to only use a single variant, called <em>ap_expr</em>,
31         for all configuration directives.
32         This document describes the <em>ap_expr</em> expression parser.
33     </p>
34     <p>The <em>ap_expr</em> expression is intended to replace most other
35         expression variants in HTTPD. For example, the deprecated
36         <directive module="mod_ssl">SSLRequire</directive> expressions can be
37         replaced by <a href="mod/mod_authz_core.html#reqexpr">Require expr</a>.
38     </p>
39   </summary>
40
41 <seealso><directive module="core" type="section">If</directive></seealso>
42 <seealso><directive module="core" type="section">ElseIf</directive></seealso>
43 <seealso><directive module="core" type="section">Else</directive></seealso>
44 <seealso><directive module="mod_rewrite">RewriteCond</directive></seealso>
45 <seealso><directive module="mod_setenvif">SetEnvIfExpr</directive></seealso>
46 <seealso><directive module="mod_filter">FilterProvider</directive></seealso>
47 <seealso><a href="mod/mod_authz_core.html#reqexpr">Require expr</a></seealso>
48 <seealso><directive module="mod_ssl">SSLRequire</directive></seealso>
49 <seealso><module>mod_include</module></seealso>
50
51   <section id="grammar">
52     <title>Grammar in Backus–Naur Form notation</title>
53       <p><a href="http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form">Backus–Naur Form</a> (BNF) is a notation
54       technique for context-free grammars, often used to describe the syntax of languages used in computing.
55       </p>
56 <blockquote>
57 <pre>
58 expr        ::= "<strong>true</strong>" | "<strong>false</strong>"
59               | "<strong>!</strong>" expr
60               | expr "<strong>&amp;&amp;</strong>" expr
61               | expr "<strong>||</strong>" expr
62               | "<strong>(</strong>" expr "<strong>)</strong>"
63               | comp
64
65 comp        ::= stringcomp
66               | integercomp
67               | unaryop word
68               | word binaryop word
69               | word "<strong>in</strong>" "<strong>{</strong>" wordlist "<strong>}</strong>"
70               | word "<strong>in</strong>" listfunction
71               | word "<strong>=~</strong>" regex
72               | word "<strong>!~</strong>" regex
73
74
75 stringcomp  ::= word "<strong>==</strong>" word
76               | word "<strong>!=</strong>" word
77               | word "<strong>&lt;</strong>"  word
78               | word "<strong>&lt;=</strong>" word
79               | word "<strong>&gt;</strong>"  word
80               | word "<strong>&gt;=</strong>" word
81
82 integercomp ::= word "<strong>-eq</strong>" word | word "<strong>eq</strong>" word
83               | word "<strong>-ne</strong>" word | word "<strong>ne</strong>" word
84               | word "<strong>-lt</strong>" word | word "<strong>lt</strong>" word
85               | word "<strong>-le</strong>" word | word "<strong>le</strong>" word
86               | word "<strong>-gt</strong>" word | word "<strong>gt</strong>" word
87               | word "<strong>-ge</strong>" word | word "<strong>ge</strong>" word
88
89 wordlist    ::= word
90               | wordlist "<strong>,</strong>" word
91
92 word        ::= word "<strong>.</strong>" word
93               | digit
94               | "<strong>'</strong>" string "<strong>'</strong>"
95               | "<strong>"</strong>" string "<strong>"</strong>"
96               | variable
97               | rebackref
98               | function
99
100 string      ::= stringpart
101               | string stringpart
102
103 stringpart  ::= cstring
104               | variable
105               | rebackref
106
107 cstring     ::= ...
108 digit       ::= [0-9]+
109
110 variable    ::= "<strong>%{</strong>" varname "<strong>}</strong>"
111               | "<strong>%{</strong>" funcname "<strong>:</strong>" funcargs "<strong>}</strong>"
112
113 rebackref   ::= "<strong>$</strong>" [0-9]
114
115 function     ::= funcname "<strong>(</strong>" word "<strong>)</strong>"
116
117 listfunction ::= listfuncname "<strong>(</strong>" word "<strong>)</strong>"
118 </pre>
119 </blockquote>
120
121 </section>
122
123 <section id="vars">
124     <title>Variables</title>
125
126     <p>The expression parser provides a number of variables of the form
127     <code>%{HTTP_HOST}</code>. Note that the value of a variable may depend
128     on the phase of the request processing in which it is evaluated.  For
129     example, an expression used in an <directive>&lt;If &gt;</directive>
130     directive is evaluated before authentication is done. Therefore,
131     <code>%{REMOTE_USER}</code> will not be set in this case.</p>
132
133     <p>The following variables provide the values of the named HTTP request
134     headers. The values of other headers can be obtained witht the
135     <code>req</code> <a href="#functions">function</a>.</p>
136
137     <table border="1" style="zebra">
138     <columnspec><column width="1"/></columnspec>
139
140     <tr><th>Name</th></tr>
141     <tr><td><code>HTTP_ACCEPT</code></td></tr>
142     <tr><td><code>HTTP_FORWARDED</code></td></tr>
143     <tr><td><code>HTTP_HOST</code></td></tr>
144     <tr><td><code>HTTP_PROXY_CONNECTION</code></td></tr>
145     <tr><td><code>HTTP_REFERER</code></td></tr>
146     <tr><td><code>HTTP_USER_AGENT</code></td></tr>
147
148     </table>
149
150     <p>Other request related variables</p>
151
152     <table border="1" style="zebra">
153     <columnspec><column width=".4"/><column width=".6"/></columnspec>
154
155     <tr><th>Name</th><th>Description</th></tr>
156     <tr><td><code>REQUEST_METHOD</code></td>
157         <td>The HTTP method of the incoming request (e.g.
158             <code>GET</code>)</td></tr>
159     <tr><td><code>REQUEST_SCHEME</code></td>
160         <td>The scheme part of the request's URI</td></tr>
161     <tr><td><code>REQUEST_URI</code></td>
162         <td>The URI of the request</td></tr>
163     <tr><td><code>DOCUMENT_URI</code></td>
164         <td>Same as REQUEST_URI</td></tr>
165     <tr><td><code>REQUEST_FILENAME</code></td>
166         <td>The full local filesystem path to the file or script matching the
167             request, if this has already been determined by the server at the
168             time <code>REQUEST_FILENAME</code> is referenced. Otherwise, such
169             as when used in virtual host context, the same value as
170             <code>REQUEST_URI</code> </td></tr>
171     <tr><td><code>SCRIPT_FILENAME</code></td>
172         <td>Same as <code>REQUEST_FILENAME</code></td></tr>
173     <tr><td><code>LAST_MODIFIED</code></td>
174         <td>The date and time of last modification of the file in the format
175             <code>20101231235959</code>, if this has already been determined by
176             the server at the time <code>LAST_MODIFIED</code> is referenced.
177             </td></tr>
178     <tr><td><code>SCRIPT_USER</code></td>
179         <td>The user name of the owner of the script.</td></tr>
180     <tr><td><code>SCRIPT_GROUP</code></td>
181         <td>The group name of the group of the script.</td></tr>
182     <tr><td><code>PATH_INFO</code></td>
183         <td>The trailing path name information, see
184             <directive module="core">AcceptPathInfo</directive></td></tr>
185     <tr><td><code>QUERY_STRING</code></td>
186         <td>The query string of the current request</td></tr>
187     <tr><td><code>IS_SUBREQ</code></td>
188         <td>"<code>true</code>" if the current request is a subrequest,
189             "<code>false</code>" otherwise</td></tr>
190     <tr><td><code>THE_REQUEST</code></td>
191         <td>The complete request line (e.g.,
192             "<code>GET /index.html HTTP/1.1</code>")</td></tr>
193     <tr><td><code>REMOTE_ADDR</code></td>
194         <td>The IP address of the remote host</td></tr>
195     <tr><td><code>REMOTE_HOST</code></td>
196         <td>The host name of the remote host</td></tr>
197     <tr><td><code>REMOTE_USER</code></td>
198         <td>The name of the authenticated user (if any)</td></tr>
199     <tr><td><code>REMOTE_IDENT</code></td>
200         <td>The user name set by <module>mod_ident</module></td></tr>
201     <tr><td><code>SERVER_NAME</code></td>
202         <td>The <directive module="core">ServerName</directive> of
203             the current vhost</td></tr>
204     <tr><td><code>SERVER_PORT</code></td>
205         <td>The server port of the current vhost, see
206             <directive module="core">ServerName</directive></td></tr>
207     <tr><td><code>SERVER_ADMIN</code></td>
208         <td>The <directive module="core">ServerAdmin</directive> of
209             the current vhost</td></tr>
210     <tr><td><code>SERVER_PROTOCOL</code></td>
211         <td>The protocol used by the request</td></tr>
212     <tr><td><code>DOCUMENT_ROOT</code></td>
213         <td>The <directive module="core">DocumentRoot</directive> of
214             the current vhost</td></tr>
215     <tr><td><code>AUTH_TYPE</code></td>
216         <td>The configured <directive module="mod_authn_core">AuthType</directive>
217             (e.g. "<code>basic</code>")</td></tr>
218     <tr><td><code>CONTENT_TYPE</code></td>
219         <td>The content type of the response</td></tr>
220     <tr><td><code>HANDLER</code></td>
221         <td>The name of the <a href="handler.html">handler</a> creating
222             the response</td></tr>
223     <tr><td><code>HTTPS</code></td>
224         <td>"<code>on</code>" if the request uses https,
225             "<code>off</code>" otherwise</td></tr>
226     <tr><td><code>IPV6</code></td>
227         <td>"<code>on</code>" if the connection uses IPv6,
228             "<code>off</code>" otherwise</td></tr>
229     <tr><td><code>REQUEST_STATUS</code></td>
230         <td>The HTTP error status of the request<td></tr>
231     <tr><td><code>REQUEST_LOG_ID</code></td>
232         <td>The error log id of the request (see
233             <directive module="core">ErrorLogFormat</directive>)</td></tr>
234     <tr><td><code>CONN_LOG_ID</code></td>
235         <td>The error log id of the connection (see
236             <directive module="core">ErrorLogFormat</directive>)</td></tr>
237
238     </table>
239
240     <p>Misc variables</p>
241
242     <table border="1" style="zebra">
243     <columnspec><column width=".4"/><column width=".6"/></columnspec>
244
245     <tr><th>Name</th><th>Description</th></tr>
246     <tr><td><code>TIME_YEAR</code></td>
247         <td>The current year (e.g. <code>2010</code>)</td></tr>
248     <tr><td><code>TIME_MON</code></td>
249         <td>The current month (<code>1</code>, ..., <code>12</code>)</td></tr>
250     <tr><td><code>TIME_DAY</code></td>
251         <td>The current day of the month</td></tr>
252     <tr><td><code>TIME_HOUR</code></td>
253         <td>The hour part of the current time
254             (<code>0</code>, ..., <code>23</code>)</td></tr>
255     <tr><td><code>TIME_MIN</code></td>
256         <td>The minute part of the current time </td></tr>
257     <tr><td><code>TIME_SEC</code></td>
258         <td>The second part of the current time </td></tr>
259     <tr><td><code>TIME_WDAY</code></td>
260         <td>The day of the week (starting with <code>0</code>
261             for Sunday)</td></tr>
262     <tr><td><code>TIME</code></td>
263         <td>The date and time in the format <code>20101231235959</code></td></tr>
264     <tr><td><code>SERVER_SOFTWARE</code></td>
265         <td>The server version string</td></tr>
266     <tr><td><code>API_VERSION</code></td>
267         <td>The date of the API version (module magic number)</td></tr>
268     </table>
269
270     <p>Some modules register additional variables, see e.g. <module>mod_ssl</module>.</p>
271
272 </section>
273
274 <section id="binop">
275     <title>Binary operators</title>
276
277     <p>With the exception of some built-in comparison operators, binary
278     operators have the form "<code>-[a-zA-Z][a-zA-Z0-9_]+</code>", i.e. a
279     minus and at least two characters. The name is not case sensitive.
280     Modules may register additional binary operators.</p>
281
282     <section id="comp">
283     <title>Comparison operators</title>
284
285     <table border="1" style="zebra">
286     <columnspec><column width=".2"/><column width=".2"/><column width=".6"/></columnspec>
287
288     <tr><th>Name</th><th>Alternative</th> <th>Description</th></tr>
289     <tr><td><code>==</code></td>
290         <td><code>=</code></td>
291         <td>String equality</td></tr>
292     <tr><td><code>!=</code></td>
293         <td></td>
294         <td>String inequality</td></tr>
295     <tr><td><code>&lt;</code></td>
296         <td></td>
297         <td>String less than</td></tr>
298     <tr><td><code>&lt;=</code></td>
299         <td></td>
300         <td>String less than or equal</td></tr>
301     <tr><td><code>&gt;</code></td>
302         <td></td>
303         <td>String greater than</td></tr>
304     <tr><td><code>&gt;=</code></td>
305         <td></td>
306         <td>String greater than or equal</td></tr>
307     <tr><td><code>-eq</code></td>
308         <td><code>eq</code></td>
309         <td>Integer equality</td></tr>
310     <tr><td><code>-ne</code></td>
311         <td><code>ne</code></td>
312         <td>Integer inequality</td></tr>
313     <tr><td><code>-lt</code></td>
314         <td><code>lt</code></td>
315         <td>Integer less than</td></tr>
316     <tr><td><code>-le</code></td>
317         <td><code>le</code></td>
318         <td>Integer less than or equal</td></tr>
319     <tr><td><code>-gt</code></td>
320         <td><code>gt</code></td>
321         <td>Integer greater than</td></tr>
322     <tr><td><code>-ge</code></td>
323         <td><code>ge</code></td>
324         <td>Integer greater than or equal</td></tr>
325     </table>
326     </section>
327
328     <section id="binaryother">
329     <title>Other binary operators</title>
330
331     <table border="1" style="zebra">
332     <columnspec><column width=".2"/><column width=".8"/></columnspec>
333
334     <tr><th>Name</th><th>Description</th></tr>
335     <tr><td><code>-ipmatch</code></td>
336         <td>IP address matches address/netmask</td></tr>
337     <tr><td><code>-strmatch</code></td>
338         <td>left string matches pattern given by right string (containing
339             wildcards *, ?, [])</td></tr>
340     <tr><td><code>-strcmatch</code></td>
341         <td>same as <code>-strmatch</code>, but case insensitive</td></tr>
342     <tr><td><code>-fnmatch</code></td>
343         <td>same as <code>-strmatch</code>, but slashes are not matched by
344             wildcards</td></tr>
345     </table>
346     </section>
347
348 </section>
349
350 <section id="unnop">
351     <title>Unary operators</title>
352
353     <p>Unary operators take one argument and have the form
354     "<code>-[a-zA-Z]</code>", i.e. a minus and one character.
355     The name <em>is</em> case sensitive.
356     Modules may register additional unary operators.</p>
357
358     <table border="1" style="zebra">
359     <columnspec><column width=".2"/><column width=".2"/><column width=".6"/></columnspec>
360
361     <tr><th>Name</th><th>Description</th><th>Restricted</th></tr>
362     <tr><td><code>-d</code></td>
363         <td>The argument is treated as a filename.
364             True if the file exists and is a directory</td><td>yes</td></tr>
365     <tr><td><code>-e</code></td>
366         <td>The argument is treated as a filename.
367             True if the file (or dir or special) exists</td><td>yes</td></tr>
368     <tr><td><code>-f</code></td>
369         <td>The argument is treated as a filename.
370             True if the file exists and is regular file</td><td>yes</td></tr>
371     <tr><td><code>-L</code></td>
372         <td>The argument is treated as a filename.
373             True if the file exists and is symlink</td><td>yes</td></tr>
374     <tr><td><code>-h</code></td>
375         <td>The argument is treated as a filename.
376             True if the file exists and is symlink
377             (same as <code>-L</code>)</td><td>yes</td></tr>
378     <tr><td><code>-F</code></td>
379         <td>True if string is a valid file, accessible via all the server's
380             currently-configured access controls for that path. This uses an
381             internal subrequest to do the check, so use it with care - it can
382             impact your server's performance!</td><td></td></tr>
383     <tr><td><code>-U</code></td>
384         <td>True if string is a valid URL, accessible via all the server's
385             currently-configured access controls for that path. This uses an
386             internal subrequest to do the check, so use it with care - it can
387             impact your server's performance!</td><td></td></tr>
388     <tr><td><code>-A</code></td>
389         <td>Alias for <code>-U</code></td><td></td></tr>
390     <tr><td><code>-n</code></td>
391         <td>True if string is not empty</td><td></td></tr>
392     <tr><td><code>-z</code></td>
393         <td>True if string is empty</td><td></td></tr>
394     <tr><td><code>-T</code></td>
395         <td>False if string is empty, "<code>0</code>", "<code>off</code>",
396             "<code>false</code>", or "<code>no</code>" (case insensitive).
397             True otherwise.</td><td></td></tr>
398     <tr><td><code>-R</code></td>
399         <td>Same as "<code>%{REMOTE_ADDR} -ipmatch ...</code>", but more efficient
400         </td><td></td></tr>
401     </table>
402
403     <p>The operators marked as "restricted" are not available in some modules
404     like <module>mod_include</module>.</p>
405 </section>
406
407 <section id="functions">
408     <title>Functions</title>
409
410     <p>Normal string-valued functions take one string as argument and return
411     a string. Functions names are not case sensitive.
412     Modules may register additional functions.</p>
413
414     <table border="1" style="zebra">
415     <columnspec><column width=".2"/><column width=".8"/></columnspec>
416
417     <tr><th>Name</th><th>Description</th><th>Restricted</th></tr>
418     <tr><td><code>req</code>, <code>http</code></td>
419         <td>Get HTTP request header</td><td></td></tr>
420     <tr><td><code>resp</code></td>
421         <td>Get HTTP response header</td><td></td></tr>
422     <tr><td><code>reqenv</code></td>
423         <td>Lookup request environment variable</td><td></td></tr>
424     <tr><td><code>osenv</code></td>
425         <td>Lookup operating system environment variable</td><td></td></tr>
426     <tr><td><code>note</code></td>
427         <td>Lookup request note</td><td></td></tr>
428     <tr><td><code>env</code></td>
429         <td>Return first match of <code>note</code>, <code>reqenv</code>,
430             <code>osenv</code></td><td></td></tr>
431     <tr><td><code>tolower</code></td>
432         <td>Convert string to lower case</td><td></td></tr>
433     <tr><td><code>toupper</code></td>
434         <td>Convert string to uppser case</td><td></td></tr>
435     <tr><td><code>escape</code></td>
436         <td>Escape special characters in %hex encoding</td><td></td></tr>
437     <tr><td><code>unescape</code></td>
438         <td>Unescape %hex encoded string, leaving URL-special characters
439             encoded (XXX: describe better)</td><td></td></tr>
440     <tr><td><code>file</code></td>
441         <td>Read contents from a file</td><td>yes</td></tr>
442     <tr><td><code>filesize</code></td>
443         <td>Return size of a file (or 0 if file does not exist or is not
444             regular file)</td><td>yes</td></tr>
445
446     </table>
447
448     <p>The functions marked as "restricted" are not available in some modules
449     like <module>mod_include</module>.</p>
450
451     <p>In addition to string-valued functions, there are also list-valued functions which
452     take one string as argument and return a wordlist, i.e. a list of strings. The wordlist
453     can be used with the special <code>-in</code> operator.
454     Functions names are not case sensitive.
455     Modules may register additional functions.</p>
456
457     <p>There are no built-in list-valued functions. <module>mod_ssl</module>
458     provides <code>PeerExtList</code>.  See the description of
459     <directive module="mod_ssl">SSLRequire</directive> for details
460     (but <code>PeerExtList</code> is also usable outside
461     of <directive module="mod_ssl">SSLRequire</directive>).</p>
462
463 </section>
464
465 <section id="other">
466     <title>Other</title>
467
468     <table border="1" style="zebra">
469     <columnspec><column width=".2"/><column width=".2"/><column width=".6"/></columnspec>
470
471     <tr><th>Name</th><th>Alternative</th> <th>Description</th></tr>
472     <tr><td><code>-in</code></td>
473         <td><code>in</code></td>
474         <td>string contained in string list</td></tr>
475     <tr><td><code>/regexp/</code></td>
476         <td><code>m#regexp#</code></td>
477         <td>Regular expression (the second form allows different delimiters than /)</td></tr>
478     <tr><td><code>/regexp/i</code></td>
479         <td><code>m#regexp#i</code></td>
480         <td>Case insensitive regular expression</td></tr>
481     <tr><td><code>$0 ... $9</code></td>
482         <td></td>
483         <td>Regular expression backreferences</td></tr>
484     </table>
485
486     <section id="rebackref">
487         <title>Regular expression backreferences</title>
488         <p>The strings <code>$0</code> ... <code>$9</code> allow to reference
489         the capture groups from a previously executed, successfully
490         matching regular expressions. They can normally only be used in the
491         same expression as the matching regex, but some modules allow special
492         uses.</p>
493     </section>
494
495 </section>
496
497 </manualpage>