blob: 59730cac95ef8e32879be04a8852a256fc7d324e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauf4f04122010-01-28 18:10:50 +01002 * include/common/regex.h
3 * This file defines everything related to regular expressions.
4 *
5 * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_REGEX_H
23#define _COMMON_REGEX_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Thierry FOURNIERe28f1ec2013-10-09 15:23:01 +020025#include <stdlib.h>
26
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
29#ifdef USE_PCRE
30#include <pcre.h>
31#include <pcreposix.h>
Hiroaki Nakamura70351322013-01-13 15:00:42 +090032
33#ifdef USE_PCRE_JIT
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020034#ifndef PCRE_CONFIG_JIT
35#error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT."
36#endif
Hiroaki Nakamura70351322013-01-13 15:00:42 +090037struct jit_regex {
38 pcre *reg;
39 pcre_extra *extra;
40};
41typedef struct jit_regex regex;
42#else /* no PCRE_JIT */
43typedef regex_t regex;
44#endif
45
46#else /* no PCRE */
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <regex.h>
Hiroaki Nakamura70351322013-01-13 15:00:42 +090048typedef regex_t regex;
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#endif
50
51/* what to do when a header matches a regex */
52#define ACT_ALLOW 0 /* allow the request */
53#define ACT_REPLACE 1 /* replace the matching header */
54#define ACT_REMOVE 2 /* remove the matching header */
55#define ACT_DENY 3 /* deny the request */
56#define ACT_PASS 4 /* pass this header without allowing or denying the request */
Willy Tarreaub8750a82006-09-03 09:56:00 +020057#define ACT_TARPIT 5 /* tarpit the connection matching this request */
Willy Tarreaua496b602006-12-17 23:15:24 +010058#define ACT_SETBE 6 /* switch the backend */
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
60struct hdr_exp {
61 struct hdr_exp *next;
Willy Tarreaub17916e2006-10-15 15:17:57 +020062 const regex_t *preg; /* expression to look for */
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
Willy Tarreaub17916e2006-10-15 15:17:57 +020064 const char *replace; /* expression to set instead */
Willy Tarreauf4f04122010-01-28 18:10:50 +010065 void *cond; /* a possible condition or NULL */
Willy Tarreaubaaee002006-06-26 02:48:02 +020066};
67
68extern regmatch_t pmatch[MAX_MATCH];
69
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020070/* "str" is the string that contain the regex to compile.
71 * "regex" is preallocated memory. After the execution of this function, this
72 * struct contain the compiled regex.
73 * "cs" is the case sensitive flag. If cs is true, case sensitive is enabled.
74 * "cap" is capture flag. If cap if true the regex can capture into
75 * parenthesis strings.
76 * "err" is the standar error message pointer.
77 *
78 * The function return 1 is succes case, else return 0 and err is filled.
79 */
80int regex_comp(const char *str, regex *regex, int cs, int cap, char **err);
Willy Tarreaub17916e2006-10-15 15:17:57 +020081int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches);
82const char *check_replace_string(const char *str);
83const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
Willy Tarreauf4f04122010-01-28 18:10:50 +010084 int action, const char *replace, void *cond);
Willy Tarreaubaaee002006-06-26 02:48:02 +020085
Thierry FOURNIERef37a662013-10-15 13:41:44 +020086/* Note that <subject> MUST be at least <length+1> characters long and must
87 * be writable because the function will temporarily force a zero past the
88 * last character.
89 */
90static inline int regex_exec(const regex *preg, char *subject, int length) {
Hiroaki Nakamura70351322013-01-13 15:00:42 +090091#ifdef USE_PCRE_JIT
92 return pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0);
93#else
Thierry FOURNIERef37a662013-10-15 13:41:44 +020094 int match;
95 char old_char = subject[length];
96 subject[length] = 0;
97 match = regexec(preg, subject, 0, NULL, 0);
98 subject[length] = old_char;
99 return match;
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900100#endif
101}
102
103static inline void regex_free(regex *preg) {
104#ifdef USE_PCRE_JIT
105 pcre_free_study(preg->extra);
106 pcre_free(preg->reg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900107#else
108 regfree(preg);
109#endif
110}
111
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200112#endif /* _COMMON_REGEX_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113
114/*
115 * Local variables:
116 * c-indent-level: 8
117 * c-basic-offset: 8
118 * End:
119 */