-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.c
78 lines (62 loc) · 1.83 KB
/
plugin.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
#include <uwsgi.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include <errno.h>
static int mkdir_p(const char *path) {
/* Adapted from
https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
*/
const size_t len = strlen(path);
char _path[PATH_MAX];
char *p;
errno = 0;
// Copy string so its mutable
if (len > sizeof(_path)-1) {
errno = ENAMETOOLONG;
return -1;
}
strcpy(_path, path);
// Iterate the string
for (p = _path + 1; *p; p++) {
if (*p == '/') {
// Temporarily truncate
*p = '\0';
if (mkdir(_path, S_IRWXU) != 0) {
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}
if (mkdir(_path, S_IRWXU) != 0) {
if (errno != EEXIST)
return -1;
}
return 0;
}
static int router_mkdir_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
struct uwsgi_buffer *ub = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data, ur->data_len);
if (!ub) return UWSGI_ROUTE_BREAK;
// Recursively create the wanted directory structure
// abusing slashes in the URL as directory separators.
// Note: ub->buf is \0 terminated, so it is safe for mkdir()
mkdir_p(ub->buf);
uwsgi_buffer_destroy(ub);
return UWSGI_ROUTE_NEXT;
}
static int router_mkdir(struct uwsgi_route *ur, char *args) {
ur->func = router_mkdir_func;
ur->data = args;
ur->data_len = strlen(args);
return 0;
}
static void register_router_mkdir(void) {
uwsgi_register_router("mkdir", router_mkdir);
}
struct uwsgi_plugin router_mkdir_plugin = {
.name = "router_mkdir",
.on_load = register_router_mkdir,
};