Skip to content

Commit

Permalink
retain pcre1 support with two alt. defs (UWSGI_PCRE vs UWSGI_PCRE2)
Browse files Browse the repository at this point in the history
  • Loading branch information
loqs authored and niol committed Jul 29, 2023
1 parent 7ce51ab commit 5f45331
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 39 deletions.
14 changes: 11 additions & 3 deletions core/alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ static struct uwsgi_alarm_instance *uwsgi_alarm_get_instance(char *name) {
}


#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
static int uwsgi_alarm_log_add(char *alarms, char *regexp, int negate) {

struct uwsgi_alarm_log *old_ual = NULL, *ual = uwsgi.alarm_logs;
Expand All @@ -170,7 +170,11 @@ static int uwsgi_alarm_log_add(char *alarms, char *regexp, int negate) {
}

ual = uwsgi_calloc(sizeof(struct uwsgi_alarm_log));
#ifdef UWSGI_PCRE2
if (uwsgi_regexp_build(regexp, &ual->pattern)) {
#else
if (uwsgi_regexp_build(regexp, &ual->pattern, &ual->pattern_extra)) {
#endif
free(ual);
return -1;
}
Expand Down Expand Up @@ -331,7 +335,7 @@ void uwsgi_alarms_init() {
usl = usl->next;
}

#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
// then map log-alarm
usl = uwsgi.alarm_logs_list;
while (usl) {
Expand Down Expand Up @@ -377,14 +381,18 @@ void uwsgi_alarm_trigger_uai(struct uwsgi_alarm_instance *uai, char *msg, size_t
}
}

#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
// check if a log should raise an alarm
void uwsgi_alarm_log_check(char *msg, size_t len) {
if (!uwsgi_strncmp(msg, len, "[uwsgi-alarm", 12))
return;
struct uwsgi_alarm_log *ual = uwsgi.alarm_logs;
while (ual) {
#ifdef UWSGI_PCRE2
if (uwsgi_regexp_match(ual->pattern, msg, len) >= 0) {
#else
if (uwsgi_regexp_match(ual->pattern, ual->pattern_extra, msg, len) >= 0) {
#endif
if (!ual->negate) {
struct uwsgi_alarm_ll *uall = ual->alarms;
while (uall) {
Expand Down
2 changes: 1 addition & 1 deletion core/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ int uwsgi_logic_opt_if_not_hostname(char *key, char *value) {
return 0;
}

#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
int uwsgi_logic_opt_if_hostname_match(char *key, char *value) {
uwsgi.logic_opt_if_failed = 0;
if (uwsgi_regexp_match_pattern(uwsgi.logic_opt_data, uwsgi.hostname)) {
Expand Down
22 changes: 19 additions & 3 deletions core/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ void uwsgi_setup_log_master(void) {
usl = usl->next;
}

#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
// set logger by its id
struct uwsgi_regexp_list *url = uwsgi.log_route;
while (url) {
Expand Down Expand Up @@ -1443,11 +1443,15 @@ int uwsgi_master_log(void) {

ssize_t rlen = read(uwsgi.shared->worker_log_pipe[0], uwsgi.log_master_buf, uwsgi.log_master_bufsize);
if (rlen > 0) {
#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
uwsgi_alarm_log_check(uwsgi.log_master_buf, rlen);
struct uwsgi_regexp_list *url = uwsgi.log_drain_rules;
while (url) {
#ifdef UWSGI_PCRE2
if (uwsgi_regexp_match(url->pattern, uwsgi.log_master_buf, rlen) >= 0) {
#else
if (uwsgi_regexp_match(url->pattern, url->pattern_extra, uwsgi.log_master_buf, rlen) >= 0) {
#endif
return 0;
}
url = url->next;
Expand All @@ -1456,7 +1460,11 @@ int uwsgi_master_log(void) {
int show = 0;
url = uwsgi.log_filter_rules;
while (url) {
#ifdef UWSGI_PCRE2
if (uwsgi_regexp_match(url->pattern, uwsgi.log_master_buf, rlen) >= 0) {
#else
if (uwsgi_regexp_match(url->pattern, url->pattern_extra, uwsgi.log_master_buf, rlen) >= 0) {
#endif
show = 1;
break;
}
Expand All @@ -1469,7 +1477,11 @@ int uwsgi_master_log(void) {
url = uwsgi.log_route;
int finish = 0;
while (url) {
#ifdef UWSGI_PCRE2
if (uwsgi_regexp_match(url->pattern, uwsgi.log_master_buf, rlen) >= 0) {
#else
if (uwsgi_regexp_match(url->pattern, url->pattern_extra, uwsgi.log_master_buf, rlen) >= 0) {
#endif
struct uwsgi_logger *ul_route = (struct uwsgi_logger *) url->custom_ptr;
if (ul_route) {
uwsgi_log_func_do(uwsgi.requested_log_encoders, ul_route, uwsgi.log_master_buf, rlen);
Expand Down Expand Up @@ -1509,11 +1521,15 @@ int uwsgi_master_req_log(void) {

ssize_t rlen = read(uwsgi.shared->worker_req_log_pipe[0], uwsgi.log_master_buf, uwsgi.log_master_bufsize);
if (rlen > 0) {
#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
struct uwsgi_regexp_list *url = uwsgi.log_req_route;
int finish = 0;
while (url) {
#ifdef UWSGI_PCRE2
if (uwsgi_regexp_match(url->pattern, uwsgi.log_master_buf, rlen) >= 0) {
#else
if (uwsgi_regexp_match(url->pattern, url->pattern_extra, uwsgi.log_master_buf, rlen) >= 0) {
#endif
struct uwsgi_logger *ul_route = (struct uwsgi_logger *) url->custom_ptr;
if (ul_route) {
uwsgi_log_func_do(uwsgi.requested_log_req_encoders, ul_route, uwsgi.log_master_buf, rlen);
Expand Down
52 changes: 51 additions & 1 deletion core/regexp.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
#include "uwsgi.h"

extern struct uwsgi_server uwsgi;
Expand All @@ -13,34 +13,64 @@ void uwsgi_opt_pcre_jit(char *opt, char *value, void *foobar) {
#endif
}

#ifdef UWSGI_PCRE2
int uwsgi_regexp_build(char *re, pcre2_code ** pattern) {

int errnbr;
long unsigned int erroff;

*pattern = pcre2_compile((const unsigned char *) re, PCRE2_ZERO_TERMINATED, 0, &errnbr, &erroff, NULL);
#else
int uwsgi_regexp_build(char *re, pcre ** pattern, pcre_extra ** pattern_extra) {

const char *errstr;
int erroff;

*pattern = pcre_compile((const char *) re, 0, &errstr, &erroff, NULL);
#endif
if (!*pattern) {
#ifdef UWSGI_PCRE2
uwsgi_log("pcre error: code %d at offset %d\n", errnbr, erroff);
#else
uwsgi_log("pcre error: %s at offset %d\n", errstr, erroff);
#endif
return -1;
}

#ifdef UWSGI_PCRE2
if (uwsgi.pcre_jit) {
errnbr = pcre2_jit_compile(*pattern, PCRE2_JIT_COMPLETE);
if (errnbr) {
uwsgi_log("pcre JIT compile error code %d\n", errnbr);
return -1;
}
#else
int opt = uwsgi.pcre_jit;

*pattern_extra = (pcre_extra *) pcre_study((const pcre *) *pattern, opt, &errstr);
if (*pattern_extra == NULL && errstr != NULL) {
pcre_free(*pattern);
uwsgi_log("pcre (study) error: %s\n", errstr);
return -1;
#endif
}

return 0;

}

#ifdef UWSGI_PCRE2
int uwsgi_regexp_match(pcre2_code *pattern, const char *subject, int length) {

return pcre2_match(pattern, (const unsigned char *)subject, length, 0, 0, NULL, NULL);
#else
int uwsgi_regexp_match(pcre * pattern, pcre_extra * pattern_extra, char *subject, int length) {

return pcre_exec((const pcre *) pattern, (const pcre_extra *) pattern_extra, subject, length, 0, 0, NULL, 0);
#endif
}

#ifdef UWSGI_PCRE2
int uwsgi_regexp_match_ovec(pcre2_code *pattern, const char *subject, int length, int *ovec, int n) {

int rc;
Expand All @@ -50,28 +80,48 @@ int uwsgi_regexp_match_ovec(pcre2_code *pattern, const char *subject, int length

match_data = pcre2_match_data_create_from_pattern(pattern, NULL);
rc = pcre2_match(pattern, (const unsigned char *)subject, length, 0, 0, match_data, NULL);
#else
int uwsgi_regexp_match_ovec(pcre * pattern, pcre_extra * pattern_extra, char *subject, int length, int *ovec, int n) {
#endif

if (n > 0) {
#ifdef UWSGI_PCRE2
// copy pcre2 output vector to uwsgi output vector
pcre2_ovec = pcre2_get_ovector_pointer(match_data);
for (i=0;i<2*n+1;i++) {
ovec[i] = pcre2_ovec[i];
}
#else
return pcre_exec((const pcre *) pattern, (const pcre_extra *) pattern_extra, subject, length, 0, 0, ovec, (n + 1) * 3);
#endif
}

#ifdef UWSGI_PCRE2
pcre2_match_data_free(match_data);

return rc;
#else
return pcre_exec((const pcre *) pattern, (const pcre_extra *) pattern_extra, subject, length, 0, 0, NULL, 0);
#endif
}

#ifdef UWSGI_PCRE2
int uwsgi_regexp_ovector(pcre2_code *pattern) {
#else
int uwsgi_regexp_ovector(pcre * pattern, pcre_extra * pattern_extra) {
#endif

int n;
#ifdef UWSGI_PCRE2
pcre2_match_data *match_data;

match_data = pcre2_match_data_create_from_pattern(pattern, NULL);
n = pcre2_get_ovector_count(match_data);
pcre2_match_data_free(match_data);
#else
if (pcre_fullinfo((const pcre *) pattern, (const pcre_extra *) pattern_extra, PCRE_INFO_CAPTURECOUNT, &n))
return 0;
#endif

return n;
}
Expand Down
48 changes: 48 additions & 0 deletions core/routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ int uwsgi_apply_routes_do(struct uwsgi_route *routes, struct wsgi_request *wsgi_
subject = *subject2 ;
subject_len = *subject_len2;
}
#ifdef UWSGI_PCRE2
n = uwsgi_regexp_match_ovec(routes->pattern, subject, subject_len, routes->ovector[wsgi_req->async_id], routes->ovn[wsgi_req->async_id]);
#else
n = uwsgi_regexp_match_ovec(routes->pattern, routes->pattern_extra, subject, subject_len, routes->ovector[wsgi_req->async_id], routes->ovn[wsgi_req->async_id]);
#endif
}
else {
int ret = routes->if_func(wsgi_req, routes);
Expand Down Expand Up @@ -506,15 +510,27 @@ void uwsgi_fixup_routes(struct uwsgi_route *ur) {

// fill them if needed... (this is an optimization for route with a static subject)
if (ur->subject && ur->subject_len) {
#ifdef UWSGI_PCRE2
if (uwsgi_regexp_build(ur->orig_route, &ur->pattern)) {
#else
if (uwsgi_regexp_build(ur->orig_route, &ur->pattern, &ur->pattern_extra)) {
#endif
exit(1);
}

int i;
for(i=0;i<uwsgi.cores;i++) {
#ifdef UWSGI_PCRE2
ur->ovn[i] = uwsgi_regexp_ovector(ur->pattern);
#else
ur->ovn[i] = uwsgi_regexp_ovector(ur->pattern, ur->pattern_extra);
#endif
if (ur->ovn[i] > 0) {
#ifdef UWSGI_PCRE2
ur->ovector[i] = uwsgi_calloc(sizeof(int) * (2 * (ur->ovn[i] + 1)));
#else
ur->ovector[i] = uwsgi_calloc(sizeof(int) * (3 * (ur->ovn[i] + 1)));
#endif
}
}
}
Expand Down Expand Up @@ -1484,26 +1500,58 @@ static int uwsgi_route_condition_regexp(struct wsgi_request *wsgi_req, struct uw
ur->condition_ub[wsgi_req->async_id] = uwsgi_routing_translate(wsgi_req, ur, NULL, 0, ur->subject_str, semicolon - ur->subject_str);
if (!ur->condition_ub[wsgi_req->async_id]) return -1;

#ifdef UWSGI_PCRE2
pcre2_code *pattern;
#else
pcre *pattern;
pcre_extra *pattern_extra;
#endif
char *re = uwsgi_concat2n(semicolon+1, ur->subject_str_len - ((semicolon+1) - ur->subject_str), "", 0);
#ifdef UWSGI_PCRE2
if (uwsgi_regexp_build(re, &pattern)) {
#else
if (uwsgi_regexp_build(re, &pattern, &pattern_extra)) {
#endif
free(re);
return -1;
}
free(re);

// a condition has no initialized vectors, let's create them
#ifdef UWSGI_PCRE2
ur->ovn[wsgi_req->async_id] = uwsgi_regexp_ovector(pattern);
#else
ur->ovn[wsgi_req->async_id] = uwsgi_regexp_ovector(pattern, pattern_extra);
#endif
if (ur->ovn[wsgi_req->async_id] > 0) {
ur->ovector[wsgi_req->async_id] = uwsgi_calloc(sizeof(int) * (3 * (ur->ovn[wsgi_req->async_id] + 1)));
}

#ifdef UWSGI_PCRE2
if (uwsgi_regexp_match_ovec(pattern, ur->condition_ub[wsgi_req->async_id]->buf, ur->condition_ub[wsgi_req->async_id]->pos, ur->ovector[wsgi_req->async_id], ur->ovn[wsgi_req->async_id] ) >= 0) {
pcre2_code_free(pattern);
#else
if (uwsgi_regexp_match_ovec(pattern, pattern_extra, ur->condition_ub[wsgi_req->async_id]->buf, ur->condition_ub[wsgi_req->async_id]->pos, ur->ovector[wsgi_req->async_id], ur->ovn[wsgi_req->async_id] ) >= 0) {
pcre_free(pattern);
#ifdef PCRE_STUDY_JIT_COMPILE
pcre_free_study(pattern_extra);
#else
pcre_free(pattern_extra);
#endif
#endif
return 1;
}

#ifdef UWSGI_PCRE2
pcre2_code_free(pattern);
#else
pcre_free(pattern);
#ifdef PCRE_STUDY_JIT_COMPILE
pcre_free_study(pattern_extra);
#else
pcre_free(pattern_extra);
#endif
#endif
return 0;
}

Expand Down
10 changes: 7 additions & 3 deletions core/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,14 @@ static int uwsgi_sni_cb(SSL *ssl, int *ad, void *arg) {

if (uwsgi.subscription_dotsplit) goto end;

#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
struct uwsgi_regexp_list *url = uwsgi.sni_regexp;
while(url) {
#ifdef UWSGI_PCRE2
if (uwsgi_regexp_match(url->pattern, (char *)servername, servername_len) >= 0) {
#else
if (uwsgi_regexp_match(url->pattern, url->pattern_extra, (char *)servername, servername_len) >= 0) {
#endif
SSL_set_SSL_CTX(ssl, url->custom_ptr);
return SSL_TLSEXT_ERR_OK;
}
Expand Down Expand Up @@ -628,7 +632,7 @@ void uwsgi_opt_sni(char *opt, char *value, void *foobar) {
return;
}

#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
if (!strcmp(opt, "sni-regexp")) {
struct uwsgi_regexp_list *url = uwsgi_regexp_new_list(&uwsgi.sni_regexp, v);
url->custom_ptr = ctx;
Expand All @@ -637,7 +641,7 @@ void uwsgi_opt_sni(char *opt, char *value, void *foobar) {
#endif
struct uwsgi_string_list *usl = uwsgi_string_new_list(&uwsgi.sni, v);
usl->custom_ptr = ctx;
#ifdef UWSGI_PCRE
#if defined(UWSGI_PCRE) || defined(UWSGI_PCRE2)
}
#endif

Expand Down
Loading

0 comments on commit 5f45331

Please sign in to comment.