blob: cec68c8858b012207586b8417d54537195de65b6 [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>
Thierry FOURNIER799c0422013-12-06 20:36:20 +010032#else /* no PCRE */
33#include <regex.h>
34#endif
Hiroaki Nakamura70351322013-01-13 15:00:42 +090035
Thierry FOURNIER799c0422013-12-06 20:36:20 +010036struct my_regex {
37#ifdef USE_PCRE
Hiroaki Nakamura70351322013-01-13 15:00:42 +090038#ifdef USE_PCRE_JIT
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020039#ifndef PCRE_CONFIG_JIT
40#error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT."
41#endif
Thierry FOURNIER799c0422013-12-06 20:36:20 +010042 pcre *reg;
43 pcre_extra *extra;
Hiroaki Nakamura70351322013-01-13 15:00:42 +090044#else /* no PCRE_JIT */
Thierry FOURNIER799c0422013-12-06 20:36:20 +010045 regex_t regex;
Hiroaki Nakamura70351322013-01-13 15:00:42 +090046#endif
Hiroaki Nakamura70351322013-01-13 15:00:42 +090047#else /* no PCRE */
Thierry FOURNIER799c0422013-12-06 20:36:20 +010048 regex_t regex;
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#endif
Thierry FOURNIER799c0422013-12-06 20:36:20 +010050};
Willy Tarreaubaaee002006-06-26 02:48:02 +020051
52/* what to do when a header matches a regex */
53#define ACT_ALLOW 0 /* allow the request */
54#define ACT_REPLACE 1 /* replace the matching header */
55#define ACT_REMOVE 2 /* remove the matching header */
56#define ACT_DENY 3 /* deny the request */
57#define ACT_PASS 4 /* pass this header without allowing or denying the request */
Willy Tarreaub8750a82006-09-03 09:56:00 +020058#define ACT_TARPIT 5 /* tarpit the connection matching this request */
Willy Tarreaua496b602006-12-17 23:15:24 +010059#define ACT_SETBE 6 /* switch the backend */
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
61struct hdr_exp {
62 struct hdr_exp *next;
Willy Tarreaub17916e2006-10-15 15:17:57 +020063 const regex_t *preg; /* expression to look for */
Willy Tarreaubaaee002006-06-26 02:48:02 +020064 int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
Willy Tarreaub17916e2006-10-15 15:17:57 +020065 const char *replace; /* expression to set instead */
Willy Tarreauf4f04122010-01-28 18:10:50 +010066 void *cond; /* a possible condition or NULL */
Willy Tarreaubaaee002006-06-26 02:48:02 +020067};
68
69extern regmatch_t pmatch[MAX_MATCH];
70
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020071/* "str" is the string that contain the regex to compile.
72 * "regex" is preallocated memory. After the execution of this function, this
73 * struct contain the compiled regex.
74 * "cs" is the case sensitive flag. If cs is true, case sensitive is enabled.
75 * "cap" is capture flag. If cap if true the regex can capture into
76 * parenthesis strings.
77 * "err" is the standar error message pointer.
78 *
79 * The function return 1 is succes case, else return 0 and err is filled.
80 */
Thierry FOURNIER799c0422013-12-06 20:36:20 +010081int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err);
Willy Tarreauc8746532014-05-28 23:05:07 +020082int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches);
Willy Tarreaub17916e2006-10-15 15:17:57 +020083const char *check_replace_string(const char *str);
84const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
Willy Tarreauf4f04122010-01-28 18:10:50 +010085 int action, const char *replace, void *cond);
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +020087/* If the function doesn't match, it returns false, else it returns true.
88 */
89static inline int regex_exec(const struct my_regex *preg, char *subject) {
90#ifdef USE_PCRE_JIT
91 if (pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, 0, NULL, 0) < 0)
92 return 0;
93 return 1;
94#else
95 int match;
96 match = regexec(&preg->regex, subject, 0, NULL, 0);
97 if (match == REG_NOMATCH)
98 return 0;
99 return 1;
100#endif
101}
102
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200103/* Note that <subject> MUST be at least <length+1> characters long and must
104 * be writable because the function will temporarily force a zero past the
105 * last character.
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200106 *
107 * If the function doesn't match, it returns false, else it returns true.
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200108 */
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200109static inline int regex_exec2(const struct my_regex *preg, char *subject, int length) {
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900110#ifdef USE_PCRE_JIT
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200111 if (pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0) < 0)
112 return 0;
113 return 1;
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900114#else
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200115 int match;
116 char old_char = subject[length];
117 subject[length] = 0;
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100118 match = regexec(&preg->regex, subject, 0, NULL, 0);
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200119 subject[length] = old_char;
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200120 if (match == REG_NOMATCH)
121 return 0;
122 return 1;
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900123#endif
124}
125
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200126int regex_exec_match(const struct my_regex *preg, const char *subject,
127 size_t nmatch, regmatch_t pmatch[]);
128int regex_exec_match2(const struct my_regex *preg, char *subject, int length,
129 size_t nmatch, regmatch_t pmatch[]);
130
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100131static inline void regex_free(struct my_regex *preg) {
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900132#ifdef USE_PCRE_JIT
133 pcre_free_study(preg->extra);
134 pcre_free(preg->reg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900135#else
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100136 regfree(&preg->regex);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900137#endif
138}
139
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200140#endif /* _COMMON_REGEX_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141
142/*
143 * Local variables:
144 * c-indent-level: 8
145 * c-basic-offset: 8
146 * End:
147 */