Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_REGEX_H |
| 23 | #define _COMMON_REGEX_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Thierry FOURNIER | e28f1ec | 2013-10-09 15:23:01 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | |
| 29 | #ifdef USE_PCRE |
| 30 | #include <pcre.h> |
| 31 | #include <pcreposix.h> |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 32 | #else /* no PCRE */ |
| 33 | #include <regex.h> |
| 34 | #endif |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 35 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 36 | struct my_regex { |
| 37 | #ifdef USE_PCRE |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 38 | #ifdef USE_PCRE_JIT |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 39 | #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 FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 42 | pcre *reg; |
| 43 | pcre_extra *extra; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 44 | #else /* no PCRE_JIT */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 45 | regex_t regex; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 46 | #endif |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 47 | #else /* no PCRE */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 48 | regex_t regex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #endif |
Thierry FOURNIER | 39e258f | 2013-12-06 20:38:46 +0100 | [diff] [blame] | 50 | char *regstr; /* this contain the original string */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 51 | }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | |
| 53 | /* what to do when a header matches a regex */ |
| 54 | #define ACT_ALLOW 0 /* allow the request */ |
| 55 | #define ACT_REPLACE 1 /* replace the matching header */ |
| 56 | #define ACT_REMOVE 2 /* remove the matching header */ |
| 57 | #define ACT_DENY 3 /* deny the request */ |
| 58 | #define ACT_PASS 4 /* pass this header without allowing or denying the request */ |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 59 | #define ACT_TARPIT 5 /* tarpit the connection matching this request */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 60 | #define ACT_SETBE 6 /* switch the backend */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | |
| 62 | struct hdr_exp { |
| 63 | struct hdr_exp *next; |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 64 | const regex_t *preg; /* expression to look for */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 66 | const char *replace; /* expression to set instead */ |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 67 | void *cond; /* a possible condition or NULL */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | extern regmatch_t pmatch[MAX_MATCH]; |
| 71 | |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 72 | /* "str" is the string that contain the regex to compile. |
| 73 | * "regex" is preallocated memory. After the execution of this function, this |
| 74 | * struct contain the compiled regex. |
| 75 | * "cs" is the case sensitive flag. If cs is true, case sensitive is enabled. |
| 76 | * "cap" is capture flag. If cap if true the regex can capture into |
| 77 | * parenthesis strings. |
| 78 | * "err" is the standar error message pointer. |
| 79 | * |
| 80 | * The function return 1 is succes case, else return 0 and err is filled. |
| 81 | */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 82 | int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err); |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 83 | int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches); |
| 84 | const char *check_replace_string(const char *str); |
| 85 | const char *chain_regex(struct hdr_exp **head, const regex_t *preg, |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 86 | int action, const char *replace, void *cond); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 88 | /* Note that <subject> MUST be at least <length+1> characters long and must |
| 89 | * be writable because the function will temporarily force a zero past the |
| 90 | * last character. |
| 91 | */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 92 | static inline int regex_exec(const struct my_regex *preg, char *subject, int length) { |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 93 | #ifdef USE_PCRE_JIT |
| 94 | return pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0); |
| 95 | #else |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 96 | int match; |
| 97 | char old_char = subject[length]; |
| 98 | subject[length] = 0; |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 99 | match = regexec(&preg->regex, subject, 0, NULL, 0); |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 100 | subject[length] = old_char; |
| 101 | return match; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 102 | #endif |
| 103 | } |
| 104 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 105 | static inline void regex_free(struct my_regex *preg) { |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 106 | #ifdef USE_PCRE_JIT |
| 107 | pcre_free_study(preg->extra); |
| 108 | pcre_free(preg->reg); |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 109 | #else |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 110 | regfree(&preg->regex); |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 111 | #endif |
Thierry FOURNIER | 39e258f | 2013-12-06 20:38:46 +0100 | [diff] [blame] | 112 | free(preg->regstr); |
| 113 | preg->regstr = NULL; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 116 | #endif /* _COMMON_REGEX_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * Local variables: |
| 120 | * c-indent-level: 8 |
| 121 | * c-basic-offset: 8 |
| 122 | * End: |
| 123 | */ |