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