blob: 01040195a1244196d54f2e3c4425ec4b3e3f42fd [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
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
27#ifdef USE_PCRE
28#include <pcre.h>
29#include <pcreposix.h>
Hiroaki Nakamura70351322013-01-13 15:00:42 +090030
31#ifdef USE_PCRE_JIT
32struct jit_regex {
33 pcre *reg;
34 pcre_extra *extra;
35};
36typedef struct jit_regex regex;
37#else /* no PCRE_JIT */
38typedef regex_t regex;
39#endif
40
41#else /* no PCRE */
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <regex.h>
Hiroaki Nakamura70351322013-01-13 15:00:42 +090043typedef regex_t regex;
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#endif
45
46/* what to do when a header matches a regex */
47#define ACT_ALLOW 0 /* allow the request */
48#define ACT_REPLACE 1 /* replace the matching header */
49#define ACT_REMOVE 2 /* remove the matching header */
50#define ACT_DENY 3 /* deny the request */
51#define ACT_PASS 4 /* pass this header without allowing or denying the request */
Willy Tarreaub8750a82006-09-03 09:56:00 +020052#define ACT_TARPIT 5 /* tarpit the connection matching this request */
Willy Tarreaua496b602006-12-17 23:15:24 +010053#define ACT_SETBE 6 /* switch the backend */
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
55struct hdr_exp {
56 struct hdr_exp *next;
Willy Tarreaub17916e2006-10-15 15:17:57 +020057 const regex_t *preg; /* expression to look for */
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
Willy Tarreaub17916e2006-10-15 15:17:57 +020059 const char *replace; /* expression to set instead */
Willy Tarreauf4f04122010-01-28 18:10:50 +010060 void *cond; /* a possible condition or NULL */
Willy Tarreaubaaee002006-06-26 02:48:02 +020061};
62
63extern regmatch_t pmatch[MAX_MATCH];
64
Willy Tarreaub17916e2006-10-15 15:17:57 +020065int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches);
66const char *check_replace_string(const char *str);
67const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
Willy Tarreauf4f04122010-01-28 18:10:50 +010068 int action, const char *replace, void *cond);
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Hiroaki Nakamura70351322013-01-13 15:00:42 +090070static inline int regex_exec(const regex *preg, const char *subject, int length) {
71#ifdef USE_PCRE_JIT
72 return pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0);
73#else
74 return regexec(preg, subject, 0, NULL, 0);
75#endif
76}
77
78static inline void regex_free(regex *preg) {
79#ifdef USE_PCRE_JIT
80 pcre_free_study(preg->extra);
81 pcre_free(preg->reg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +090082#else
83 regfree(preg);
84#endif
85}
86
Willy Tarreau2dd0d472006-06-29 17:53:05 +020087#endif /* _COMMON_REGEX_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +020088
89/*
90 * Local variables:
91 * c-indent-level: 8
92 * c-basic-offset: 8
93 * End:
94 */