-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuws_rewrite.c
94 lines (91 loc) · 3.68 KB
/
uws_rewrite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "uws_memory.h"
#include <sys/stat.h>
#include "uws_utils.h"
#include "uws_config.h"
#include "uws_header.h"
#include "uws_status.h"
#include "uws_http.h"
#include "uws_router.h"
static void
split_string(char *src, char **type, char **regexp, char **patch) {
int len = strlen(src);
*type = (char*)uws_calloc(len, sizeof(char));
*regexp = (char*)uws_calloc(len, sizeof(char));
*patch = (char*)uws_calloc(len, sizeof(char));
sscanf(src, "%[^ ]%*[ ]%[^ ]%*[ ]%[^ ]", *type, *regexp, *patch);
}
void rewrite_router(pConnInfo conn_info) {
if(!conn_info->running_server->rewrite.engine || conn_info->running_server->rewrite.rules.total == 0) {
apply_next_router(conn_info);
return;
}
char **rules = conn_info->running_server->rewrite.rules.array;
bool apply_access = false;
bool apply_rewrite = false;
char *type, *regexp, *patch;
char *url = conn_info->request_header->url;
while(*rules != NULL) {
//apply a rule, when
split_string(*rules, &type, ®exp, &patch);
if(conn_info->running_server->rewrite.exist) {
char *path = strlcat(conn_info->running_server->root, conn_info->request_header->url);
struct stat stat_buff;
if(lstat(path, &stat_buff) != -1) {
apply_rewrite = true;
}
uws_free(path);
}
if(!apply_access) {
if(strcmp(type, "allow") == 0) {
if(preg_match(url, regexp)) { //then apply allow rule
if(wildcmp(patch, conn_info->client_ip)) {
apply_access = true;
}
}
} else if(strcmp(type, "deny") == 0) {
if(preg_match(url, regexp)) { //then apply deny rule
if(wildcmp(patch, conn_info->client_ip)) {
conn_info->status_code = 403;
apply_next_router(conn_info);
return;
}
}
}
}
if(!apply_rewrite) {
if(strcmp(type, "dispatch") == 0) {
if(preg_match(url, regexp)) { //then apply dispatch rule
char *new_url = preg_replace(url, regexp, patch);
uws_free(conn_info->request_header->path);
conn_info->request_header->path = new_url;
strcpy(conn_info->request_header->url, new_url);
apply_rewrite = true;
}
} else if(strcmp(type, "redirect-t") == 0) {
if(preg_match(url, regexp)) { //then apply redirect-t rule
char *new_url = preg_replace(url, regexp, patch);
add_header_param("Location", new_url, conn_info->response_header);
uws_free(new_url);
apply_rewrite = true;
apply_access = true;
conn_info->status_code = 302;
apply_next_router(conn_info);
return;
}
} else if(strcmp(type, "redirect-p") == 0){
if(preg_match(url, regexp)) { //then apply redirect-p rule
char *new_url = preg_replace(url, regexp, patch);
add_header_param("Location", new_url, conn_info->response_header);
uws_free(new_url);
apply_rewrite = true;
apply_access = true;
conn_info->status_code = 301;
apply_next_router(conn_info);
return;
}
} else{}
}
uws_free(type); uws_free(regexp); uws_free(patch);
rules++;
}
}