blob: 1cc471b3329c476b102119315daf5c8997eb0ff2 [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
34struct jit_regex {
35 pcre *reg;
36 pcre_extra *extra;
37};
38typedef struct jit_regex regex;
39#else /* no PCRE_JIT */
40typedef regex_t regex;
41#endif
42
43#else /* no PCRE */
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <regex.h>
Hiroaki Nakamura70351322013-01-13 15:00:42 +090045typedef regex_t regex;
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#endif
47
48/* what to do when a header matches a regex */
49#define ACT_ALLOW 0 /* allow the request */
50#define ACT_REPLACE 1 /* replace the matching header */
51#define ACT_REMOVE 2 /* remove the matching header */
52#define ACT_DENY 3 /* deny the request */
53#define ACT_PASS 4 /* pass this header without allowing or denying the request */
Willy Tarreaub8750a82006-09-03 09:56:00 +020054#define ACT_TARPIT 5 /* tarpit the connection matching this request */
Willy Tarreaua496b602006-12-17 23:15:24 +010055#define ACT_SETBE 6 /* switch the backend */
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
57struct hdr_exp {
58 struct hdr_exp *next;
Willy Tarreaub17916e2006-10-15 15:17:57 +020059 const regex_t *preg; /* expression to look for */
Willy Tarreaubaaee002006-06-26 02:48:02 +020060 int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
Willy Tarreaub17916e2006-10-15 15:17:57 +020061 const char *replace; /* expression to set instead */
Willy Tarreauf4f04122010-01-28 18:10:50 +010062 void *cond; /* a possible condition or NULL */
Willy Tarreaubaaee002006-06-26 02:48:02 +020063};
64
65extern regmatch_t pmatch[MAX_MATCH];
66
Willy Tarreaub17916e2006-10-15 15:17:57 +020067int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches);
68const char *check_replace_string(const char *str);
69const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
Willy Tarreauf4f04122010-01-28 18:10:50 +010070 int action, const char *replace, void *cond);
Willy Tarreaubaaee002006-06-26 02:48:02 +020071
Hiroaki Nakamura70351322013-01-13 15:00:42 +090072static inline int regex_exec(const regex *preg, const char *subject, int length) {
73#ifdef USE_PCRE_JIT
74 return pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0);
75#else
76 return regexec(preg, subject, 0, NULL, 0);
77#endif
78}
79
80static inline void regex_free(regex *preg) {
81#ifdef USE_PCRE_JIT
82 pcre_free_study(preg->extra);
83 pcre_free(preg->reg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +090084#else
85 regfree(preg);
86#endif
87}
88
Willy Tarreau2dd0d472006-06-29 17:53:05 +020089#endif /* _COMMON_REGEX_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +020090
91/*
92 * Local variables:
93 * c-indent-level: 8
94 * c-basic-offset: 8
95 * End:
96 */