|
20 | 20 |
|
21 | 21 | #include "ngx_http_modsecurity_common.h"
|
22 | 22 |
|
| 23 | + |
| 24 | +// FIXME: This function should be replaced, it is being used just for test. |
| 25 | +char * |
| 26 | +str_replace ( const char *string, const char *substr, const char *replacement ){ |
| 27 | + char *tok = NULL; |
| 28 | + char *newstr = NULL; |
| 29 | + char *oldstr = NULL; |
| 30 | + char *head = NULL; |
| 31 | + |
| 32 | + /* if either substr or replacement is NULL, duplicate string a let caller handle it */ |
| 33 | + if ( substr == NULL || replacement == NULL ) return strdup (string); |
| 34 | + newstr = strdup (string); |
| 35 | + head = newstr; |
| 36 | + while ( (tok = strstr ( head, substr ))){ |
| 37 | + oldstr = newstr; |
| 38 | + newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 ); |
| 39 | + /*failed to alloc mem, free old string and return NULL */ |
| 40 | + if ( newstr == NULL ){ |
| 41 | + free (oldstr); |
| 42 | + return NULL; |
| 43 | + } |
| 44 | + memcpy ( newstr, oldstr, tok - oldstr ); |
| 45 | + memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) ); |
| 46 | + memcpy ( newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) ); |
| 47 | + memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 ); |
| 48 | + /* move back head right after the last replacement */ |
| 49 | + head = newstr + (tok - oldstr) + strlen( replacement ); |
| 50 | + free (oldstr); |
| 51 | + } |
| 52 | + return newstr; |
| 53 | +} |
| 54 | + |
| 55 | + |
23 | 56 | void
|
24 | 57 | ngx_http_modsecurity_log(void *log, const char* msg)
|
25 | 58 | {
|
| 59 | + char *new_log = NULL; |
| 60 | + |
26 | 61 | if (log == NULL) {
|
27 | 62 | return;
|
28 | 63 | }
|
29 | 64 |
|
30 |
| - ngx_log_error(NGX_LOG_INFO, (ngx_log_t *)log, 0, msg); |
| 65 | + new_log = str_replace(msg, "%", "_"); |
| 66 | + ngx_log_error(NGX_LOG_INFO, (ngx_log_t *)log, 0, new_log); |
| 67 | + free(new_log); |
31 | 68 | }
|
32 | 69 |
|
33 | 70 |
|
|
0 commit comments