forked from aniani/gmnisrv
Initial pass on regex routing support
All this does is parse the regexes out of the config file. I've vendored libregexp from Bellard's quickjs project, because it's reasonably small and self-contained, and POSIX regexes don't support captures. We're eventually going to want captures for URL rewrites, so this'll do for now.
This commit is contained in:
parent
a26573251b
commit
7e8a953794
3
configure
vendored
3
configure
vendored
@ -10,8 +10,9 @@ gmnisrv() {
|
|||||||
src/log.c \
|
src/log.c \
|
||||||
src/main.c \
|
src/main.c \
|
||||||
src/mime.c \
|
src/mime.c \
|
||||||
src/server.c \
|
src/regexp.c \
|
||||||
src/serve.c \
|
src/serve.c \
|
||||||
|
src/server.c \
|
||||||
src/tls.c \
|
src/tls.c \
|
||||||
src/url.c \
|
src/url.c \
|
||||||
src/util.c
|
src/util.c
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
#define GMNISRV_CONFIG
|
#define GMNISRV_CONFIG
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <openssl/x509.h>
|
#include <openssl/x509.h>
|
||||||
#include <regex.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include "regexp.h"
|
||||||
|
|
||||||
struct gmnisrv_tls {
|
struct gmnisrv_tls {
|
||||||
char *store;
|
char *store;
|
||||||
@ -21,7 +21,7 @@ struct gmnisrv_route {
|
|||||||
char *spec;
|
char *spec;
|
||||||
union {
|
union {
|
||||||
char *path;
|
char *path;
|
||||||
regex_t *regex;
|
uint8_t *regex;
|
||||||
};
|
};
|
||||||
|
|
||||||
char *root;
|
char *root;
|
||||||
|
58
include/regexp-opcode.h
Normal file
58
include/regexp-opcode.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Regular Expression Engine
|
||||||
|
*
|
||||||
|
* Copyright (c) 2017-2018 Fabrice Bellard
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef DEF
|
||||||
|
|
||||||
|
DEF(invalid, 1) /* never used */
|
||||||
|
DEF(char, 3)
|
||||||
|
DEF(char32, 5)
|
||||||
|
DEF(dot, 1)
|
||||||
|
DEF(any, 1) /* same as dot but match any character including line terminator */
|
||||||
|
DEF(line_start, 1)
|
||||||
|
DEF(line_end, 1)
|
||||||
|
DEF(goto, 5)
|
||||||
|
DEF(split_goto_first, 5)
|
||||||
|
DEF(split_next_first, 5)
|
||||||
|
DEF(match, 1)
|
||||||
|
DEF(save_start, 2) /* save start position */
|
||||||
|
DEF(save_end, 2) /* save end position, must come after saved_start */
|
||||||
|
DEF(save_reset, 3) /* reset save positions */
|
||||||
|
DEF(loop, 5) /* decrement the top the stack and goto if != 0 */
|
||||||
|
DEF(push_i32, 5) /* push integer on the stack */
|
||||||
|
DEF(drop, 1)
|
||||||
|
DEF(word_boundary, 1)
|
||||||
|
DEF(not_word_boundary, 1)
|
||||||
|
DEF(back_reference, 2)
|
||||||
|
DEF(backward_back_reference, 2) /* must come after back_reference */
|
||||||
|
DEF(range, 3) /* variable length */
|
||||||
|
DEF(range32, 3) /* variable length */
|
||||||
|
DEF(lookahead, 5)
|
||||||
|
DEF(negative_lookahead, 5)
|
||||||
|
DEF(push_char_pos, 1) /* push the character position on the stack */
|
||||||
|
DEF(bne_char_pos, 5) /* pop one stack element and jump if equal to the character
|
||||||
|
position */
|
||||||
|
DEF(prev, 1) /* go to the previous char */
|
||||||
|
DEF(simple_greedy_quant, 17)
|
||||||
|
|
||||||
|
#endif /* DEF */
|
73
include/regexp.h
Normal file
73
include/regexp.h
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Regular Expression Engine
|
||||||
|
*
|
||||||
|
* Copyright (c) 2017-2018 Fabrice Bellard
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef LIBREGEXP_H
|
||||||
|
#define LIBREGEXP_H
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define LRE_FLAG_GLOBAL (1 << 0)
|
||||||
|
#define LRE_FLAG_IGNORECASE (1 << 1)
|
||||||
|
#define LRE_FLAG_MULTILINE (1 << 2)
|
||||||
|
#define LRE_FLAG_DOTALL (1 << 3)
|
||||||
|
#define LRE_FLAG_UTF16 (1 << 4)
|
||||||
|
#define LRE_FLAG_STICKY (1 << 5)
|
||||||
|
|
||||||
|
#define LRE_FLAG_NAMED_GROUPS (1 << 7) /* named groups are present in the regexp */
|
||||||
|
|
||||||
|
uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size,
|
||||||
|
const char *buf, size_t buf_len, int re_flags, void *opaque);
|
||||||
|
int lre_get_capture_count(const uint8_t *bc_buf);
|
||||||
|
int lre_get_flags(const uint8_t *bc_buf);
|
||||||
|
const char *lre_get_groupnames(const uint8_t *bc_buf);
|
||||||
|
int lre_exec(uint8_t **capture,
|
||||||
|
const uint8_t *bc_buf, const uint8_t *cbuf, int cindex, int clen,
|
||||||
|
int cbuf_type, void *opaque);
|
||||||
|
|
||||||
|
int lre_parse_escape(const uint8_t **pp, int allow_utf16);
|
||||||
|
bool lre_is_space(int c);
|
||||||
|
|
||||||
|
/* JS identifier test */
|
||||||
|
extern uint32_t const lre_id_start_table_ascii[4];
|
||||||
|
extern uint32_t const lre_id_continue_table_ascii[4];
|
||||||
|
|
||||||
|
static inline int lre_js_is_ident_first(int c)
|
||||||
|
{
|
||||||
|
if ((uint32_t)c < 128) {
|
||||||
|
return (lre_id_start_table_ascii[c >> 5] >> (c & 31)) & 1;
|
||||||
|
} else {
|
||||||
|
return !lre_is_space(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int lre_js_is_ident_next(int c)
|
||||||
|
{
|
||||||
|
if ((uint32_t)c < 128) {
|
||||||
|
return (lre_id_continue_table_ascii[c >> 5] >> (c & 31)) & 1;
|
||||||
|
} else {
|
||||||
|
/* ZWNJ and ZWJ are accepted in identifiers */
|
||||||
|
return !lre_is_space(c) || c == 0x200C || c == 0x200D;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* LIBREGEXP_H */
|
4368
include/unicode-table.h
Normal file
4368
include/unicode-table.h
Normal file
File diff suppressed because it is too large
Load Diff
16
src/config.c
16
src/config.c
@ -7,6 +7,7 @@
|
|||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "ini.h"
|
#include "ini.h"
|
||||||
|
#include "regex.h"
|
||||||
|
|
||||||
struct gmnisrv_host *
|
struct gmnisrv_host *
|
||||||
gmnisrv_config_get_host(struct gmnisrv_config *conf, const char *hostname)
|
gmnisrv_config_get_host(struct gmnisrv_config *conf, const char *hostname)
|
||||||
@ -179,6 +180,8 @@ conf_ini_handler(void *user, const char *section,
|
|||||||
conf->hosts = host;
|
conf->hosts = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int bytecode_len;
|
||||||
|
char error_msg[64];
|
||||||
struct gmnisrv_route *route =
|
struct gmnisrv_route *route =
|
||||||
gmnisrv_host_get_route(host, routing, spec);
|
gmnisrv_host_get_route(host, routing, spec);
|
||||||
if (!route) {
|
if (!route) {
|
||||||
@ -194,7 +197,15 @@ conf_ini_handler(void *user, const char *section,
|
|||||||
route->path = strdup(spec);
|
route->path = strdup(spec);
|
||||||
break;
|
break;
|
||||||
case ROUTE_REGEX:
|
case ROUTE_REGEX:
|
||||||
assert(0); // TODO
|
route->regex = lre_compile(&bytecode_len,
|
||||||
|
error_msg, sizeof(error_msg),
|
||||||
|
spec, strlen(spec), 0, NULL);
|
||||||
|
if (!route->regex) {
|
||||||
|
fprintf(stderr, "Error compiling regex '%s': %s\n",
|
||||||
|
spec, error_msg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,7 +310,8 @@ config_finish(struct gmnisrv_config *conf)
|
|||||||
free(route->path);
|
free(route->path);
|
||||||
break;
|
break;
|
||||||
case ROUTE_REGEX:
|
case ROUTE_REGEX:
|
||||||
assert(0); // TODO
|
free(route->regex);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct gmnisrv_route *rnext = route->next;
|
struct gmnisrv_route *rnext = route->next;
|
||||||
|
3044
src/regexp.c
Normal file
3044
src/regexp.c
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user