mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-10-04 16:08:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2263c6a618 | ||
|
|
b4ecd504e3 | ||
|
|
f226f29428 | ||
|
|
b87a284390 | ||
|
|
f63dcd0d9c | ||
|
|
7f5563d581 | ||
|
|
d8a61a920e | ||
|
|
5a4900da8e | ||
|
|
677c30057a | ||
|
|
26113e091b | ||
|
|
1b5e98f593 | ||
|
|
0aa5201947 | ||
|
|
e3951baa3f | ||
|
|
9f9dbbab9e | ||
|
|
d546bbadbd | ||
|
|
2b7f25054f | ||
|
|
bb3f6bcf7e | ||
|
|
41da7175d1 | ||
|
|
446763b58a | ||
|
|
3e9337a0fc | ||
|
|
028806165b | ||
|
|
e4be42edbb | ||
|
|
230fa1f3bb | ||
|
|
d74653bbad | ||
|
|
38049b0897 | ||
|
|
b4af646929 | ||
|
|
b21ec73a52 | ||
|
|
c595844f0e |
@@ -0,0 +1,3 @@
|
||||
[uwsgi]
|
||||
main_plugin = gridfs
|
||||
inherit = base
|
||||
+12
-10
@@ -208,18 +208,20 @@ void uwsgi_spawn_daemon(struct uwsgi_daemon *ud) {
|
||||
uwsgi_write_pidfile(ud->pidfile);
|
||||
}
|
||||
|
||||
// /dev/null will became stdin
|
||||
devnull = open("/dev/null", O_RDONLY);
|
||||
if (devnull < 0) {
|
||||
uwsgi_error("/dev/null open()");
|
||||
exit(1);
|
||||
}
|
||||
if (devnull != 0) {
|
||||
if (dup2(devnull, 0) < 0) {
|
||||
uwsgi_error("dup2()");
|
||||
if (!uwsgi.daemons_honour_stdin) {
|
||||
// /dev/null will became stdin
|
||||
devnull = open("/dev/null", O_RDONLY);
|
||||
if (devnull < 0) {
|
||||
uwsgi_error("/dev/null open()");
|
||||
exit(1);
|
||||
}
|
||||
close(devnull);
|
||||
if (devnull != 0) {
|
||||
if (dup2(devnull, 0) < 0) {
|
||||
uwsgi_error("dup2()");
|
||||
exit(1);
|
||||
}
|
||||
close(devnull);
|
||||
}
|
||||
}
|
||||
|
||||
if (setsid() < 0) {
|
||||
|
||||
+4
-3
@@ -865,15 +865,16 @@ int uwsgi_emperor_vassal_start(struct uwsgi_instance *n_ui) {
|
||||
|
||||
uenvs = environ;
|
||||
while (*uenvs) {
|
||||
if (!strncmp(*uenvs, "UWSGI_VASSAL_", 13)) {
|
||||
char *ne = uwsgi_concat2("UWSGI_", *uenvs + 13);
|
||||
char *oe = uwsgi_concat2n(*uenvs, strchr(*uenvs, '=') - *uenvs, "", 0);
|
||||
if (!strncmp(*uenvs, "UWSGI_VASSAL_", 13) && strchr(*uenvs, '=')) {
|
||||
char *oe = uwsgi_concat2n(*uenvs, strchr(*uenvs, '=') - *uenvs, "", 0), *ne;
|
||||
if (unsetenv(oe)) {
|
||||
uwsgi_error("unsetenv()");
|
||||
free(oe);
|
||||
break;
|
||||
}
|
||||
free(oe);
|
||||
|
||||
ne = uwsgi_concat2("UWSGI_", *uenvs + 13);
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_log("putenv %s\n", ne);
|
||||
#endif
|
||||
|
||||
+2
-2
@@ -315,7 +315,7 @@ int uwsgi_add_expires_uri(struct wsgi_request *wsgi_req, struct stat *st) {
|
||||
#endif
|
||||
|
||||
|
||||
char *uwsgi_get_mime_type(char *name, int namelen, int *size) {
|
||||
char *uwsgi_get_mime_type(char *name, int namelen, size_t *size) {
|
||||
|
||||
int i;
|
||||
int count = 0;
|
||||
@@ -429,7 +429,7 @@ static int uwsgi_static_stat(struct wsgi_request *wsgi_req, char *filename, size
|
||||
|
||||
int uwsgi_real_file_serve(struct wsgi_request *wsgi_req, char *real_filename, size_t real_filename_len, struct stat *st) {
|
||||
|
||||
int mime_type_size = 0;
|
||||
size_t mime_type_size = 0;
|
||||
char http_last_modified[49];
|
||||
|
||||
if (uwsgi.threads > 1)
|
||||
|
||||
@@ -3480,6 +3480,7 @@ int uwsgi_kvlist_parse(char *src, size_t len, char list_separator, char kv_separ
|
||||
escaped = 1;
|
||||
}
|
||||
else if (escaped) {
|
||||
*ptr++ = src[i];
|
||||
escaped = 0;
|
||||
}
|
||||
else {
|
||||
|
||||
+16
-15
@@ -166,6 +166,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"single-interpreter", no_argument, 'i', "do not use multiple interpreters (where available)", uwsgi_opt_true, &uwsgi.single_interpreter, 0},
|
||||
{"need-app", no_argument, 0, "exit if no app can be loaded", uwsgi_opt_true, &uwsgi.need_app, 0},
|
||||
{"master", no_argument, 'M', "enable master process", uwsgi_opt_true, &uwsgi.master_process, 0},
|
||||
{"honour-stdin", no_argument, 0, "do not remap stdin to /dev/null", uwsgi_opt_true, &uwsgi.honour_stdin, 0},
|
||||
{"emperor", required_argument, 0, "run the Emperor", uwsgi_opt_add_string_list, &uwsgi.emperor, 0},
|
||||
{"emperor-procname", required_argument, 0, "set the Emperor process name", uwsgi_opt_set_str, &uwsgi.emperor_procname, 0},
|
||||
{"emperor-freq", required_argument, 0, "set the Emperor scan frequency (default 3 seconds)", uwsgi_opt_set_int, &uwsgi.emperor_freq, 0},
|
||||
@@ -613,6 +614,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
|
||||
{"attach-daemon", required_argument, 0, "attach a command/daemon to the master process (the command has to not go in background)", uwsgi_opt_add_daemon, NULL, UWSGI_OPT_MASTER},
|
||||
{"smart-attach-daemon", required_argument, 0, "attach a command/daemon to the master process managed by a pidfile (the command has to daemonize)", uwsgi_opt_add_daemon, NULL, UWSGI_OPT_MASTER},
|
||||
{"smart-attach-daemon2", required_argument, 0, "attach a command/daemon to the master process managed by a pidfile (the command has to NOT daemonize)", uwsgi_opt_add_daemon, NULL, UWSGI_OPT_MASTER},
|
||||
{"daemons-honour-stdin", no_argument, 0, "do not change the stdin of external daemons to /dev/null", uwsgi_opt_true, &uwsgi.daemons_honour_stdin, UWSGI_OPT_MASTER},
|
||||
{"plugins", required_argument, 0, "load uWSGI plugins", uwsgi_opt_load_plugin, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
{"plugin", required_argument, 0, "load uWSGI plugins", uwsgi_opt_load_plugin, NULL, UWSGI_OPT_IMMEDIATE},
|
||||
{"plugins-dir", required_argument, 0, "add a directory to uWSGI plugin search path", uwsgi_opt_add_string_list, &uwsgi.plugins_dir, UWSGI_OPT_IMMEDIATE},
|
||||
@@ -2127,6 +2129,20 @@ int uwsgi_start(void *v_argv) {
|
||||
// automatically fix options
|
||||
sanitize_args();
|
||||
|
||||
|
||||
if (uwsgi.requested_max_fd) {
|
||||
uwsgi.rl.rlim_cur = uwsgi.requested_max_fd;
|
||||
uwsgi.rl.rlim_max = uwsgi.requested_max_fd;
|
||||
if (setrlimit(RLIMIT_NOFILE, &uwsgi.rl)) {
|
||||
uwsgi_error("setrlimit()");
|
||||
}
|
||||
}
|
||||
|
||||
if (!getrlimit(RLIMIT_NOFILE, &uwsgi.rl)) {
|
||||
uwsgi.max_fd = uwsgi.rl.rlim_cur;
|
||||
uwsgi_log_initial("detected max file descriptor number: %lu\n", (unsigned long) uwsgi.max_fd);
|
||||
}
|
||||
|
||||
// start the Emperor if needed
|
||||
if (!uwsgi.early_emperor && uwsgi.emperor) {
|
||||
uwsgi_emperor_start();
|
||||
@@ -2151,19 +2167,6 @@ int uwsgi_start(void *v_argv) {
|
||||
}
|
||||
}
|
||||
|
||||
if (uwsgi.requested_max_fd) {
|
||||
uwsgi.rl.rlim_cur = uwsgi.requested_max_fd;
|
||||
uwsgi.rl.rlim_max = uwsgi.requested_max_fd;
|
||||
if (setrlimit(RLIMIT_NOFILE, &uwsgi.rl)) {
|
||||
uwsgi_error("setrlimit()");
|
||||
}
|
||||
}
|
||||
|
||||
if (!getrlimit(RLIMIT_NOFILE, &uwsgi.rl)) {
|
||||
uwsgi.max_fd = uwsgi.rl.rlim_cur;
|
||||
uwsgi_log_initial("detected max file descriptor number: %lu\n", (unsigned long) uwsgi.max_fd);
|
||||
}
|
||||
|
||||
if (uwsgi.async > 1) {
|
||||
if ((unsigned long) uwsgi.max_fd < (unsigned long) uwsgi.async) {
|
||||
uwsgi_log("- your current max open files limit is %lu, this is lower than requested async cores !!! -\n", (unsigned long) uwsgi.max_fd);
|
||||
@@ -2549,11 +2552,9 @@ next:
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#ifdef UWSGI_MINTERPRETERS
|
||||
if (!uwsgi.single_interpreter && uwsgi.numproc > 0) {
|
||||
uwsgi_log("*** uWSGI is running in multiple interpreter mode ***\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
// check for request plugins, and eventually print a warning
|
||||
int rp_available = 0;
|
||||
|
||||
@@ -74,12 +74,17 @@ static void carbon_post_init() {
|
||||
else if (!u_server->port) {
|
||||
u_server->port = uwsgi_str(p);
|
||||
}
|
||||
else break;
|
||||
else
|
||||
break;
|
||||
p = strtok(NULL, ":");
|
||||
}
|
||||
if (!u_server->hostname || !u_server->port) {
|
||||
uwsgi_log("[carbon] invalid carbon server address (%s)\n", usl->value);
|
||||
usl = usl->next;
|
||||
|
||||
if (u_server->hostname) free(u_server->hostname);
|
||||
if (u_server->port) free(u_server->port);
|
||||
free(u_server);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
+109
-43
@@ -18,7 +18,7 @@ struct uwsgi_cgi {
|
||||
int path_info;
|
||||
} uc ;
|
||||
|
||||
void uwsgi_opt_add_cgi(char *opt, char *value, void *foobar) {
|
||||
static void uwsgi_opt_add_cgi(char *opt, char *value, void *foobar) {
|
||||
|
||||
char *val = strchr(value, '=');
|
||||
if (!val) {
|
||||
@@ -30,7 +30,7 @@ void uwsgi_opt_add_cgi(char *opt, char *value, void *foobar) {
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_opt_add_cgi_maphelper(char *opt, char *value, void *foobar) {
|
||||
static void uwsgi_opt_add_cgi_maphelper(char *opt, char *value, void *foobar) {
|
||||
char *val = strchr(value, '=');
|
||||
if (!val) {
|
||||
uwsgi_log("invalid CGI helper syntax, must be ext=command\n");
|
||||
@@ -66,7 +66,7 @@ struct uwsgi_option uwsgi_cgi_options[] = {
|
||||
|
||||
};
|
||||
|
||||
void uwsgi_cgi_apps() {
|
||||
static void uwsgi_cgi_apps() {
|
||||
|
||||
struct uwsgi_dyn_dict *udd = uc.mountpoint;
|
||||
struct stat st;
|
||||
@@ -123,7 +123,7 @@ void uwsgi_cgi_apps() {
|
||||
|
||||
}
|
||||
|
||||
int uwsgi_cgi_init(){
|
||||
static int uwsgi_cgi_init(){
|
||||
|
||||
void (*cgi_sym)(void);
|
||||
|
||||
@@ -161,7 +161,7 @@ int uwsgi_cgi_init(){
|
||||
|
||||
}
|
||||
|
||||
char *uwsgi_cgi_get_helper(char *filename) {
|
||||
static char *uwsgi_cgi_get_helper(char *filename) {
|
||||
|
||||
struct uwsgi_dyn_dict *helpers = uc.helpers;
|
||||
size_t len = strlen(filename);
|
||||
@@ -179,7 +179,7 @@ char *uwsgi_cgi_get_helper(char *filename) {
|
||||
|
||||
}
|
||||
|
||||
int uwsgi_cgi_parse(struct wsgi_request *wsgi_req, char *buf, size_t len) {
|
||||
static int uwsgi_cgi_parse(struct wsgi_request *wsgi_req, char *buf, size_t len) {
|
||||
|
||||
size_t i;
|
||||
char *key = buf, *value = NULL;
|
||||
@@ -266,7 +266,7 @@ send_body:
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *uwsgi_cgi_get_docroot(char *path_info, uint16_t path_info_len, int *need_free, int *is_a_file, int *discard_base, char **script_name) {
|
||||
static char *uwsgi_cgi_get_docroot(char *path_info, uint16_t path_info_len, int *need_free, int *is_a_file, int *discard_base, char **script_name) {
|
||||
|
||||
struct uwsgi_dyn_dict *udd = uc.mountpoint, *choosen_udd = NULL;
|
||||
int best_found = 0;
|
||||
@@ -323,7 +323,7 @@ char *uwsgi_cgi_get_docroot(char *path_info, uint16_t path_info_len, int *need_f
|
||||
return path;
|
||||
}
|
||||
|
||||
int uwsgi_cgi_walk(struct wsgi_request *wsgi_req, char *full_path, char *docroot, size_t docroot_len, int discard_base, char **path_info) {
|
||||
static int uwsgi_cgi_walk(struct wsgi_request *wsgi_req, char *full_path, char *docroot, size_t docroot_len, int discard_base, char **path_info) {
|
||||
|
||||
// and now start walking...
|
||||
uint16_t i;
|
||||
@@ -380,18 +380,12 @@ int uwsgi_cgi_walk(struct wsgi_request *wsgi_req, char *full_path, char *docroot
|
||||
|
||||
}
|
||||
|
||||
int uwsgi_cgi_request(struct wsgi_request *wsgi_req) {
|
||||
static int uwsgi_cgi_run(struct wsgi_request *, char *, size_t, char *, char *, char *, char *, int, int);
|
||||
|
||||
static int uwsgi_cgi_request(struct wsgi_request *wsgi_req) {
|
||||
|
||||
int i;
|
||||
pid_t cgi_pid;
|
||||
int waitpid_status;
|
||||
char **argv;
|
||||
int nargs = 0;
|
||||
char full_path[PATH_MAX];
|
||||
char tmp_path[PATH_MAX];
|
||||
int cgi_pipe[2];
|
||||
int post_pipe[2];
|
||||
ssize_t len;
|
||||
struct stat cgi_stat;
|
||||
int need_free = 0;
|
||||
int is_a_file = 0;
|
||||
@@ -399,7 +393,6 @@ int uwsgi_cgi_request(struct wsgi_request *wsgi_req) {
|
||||
size_t docroot_len = 0;
|
||||
size_t full_path_len = 0;
|
||||
char *helper = NULL;
|
||||
char *command = NULL;
|
||||
char *path_info = NULL;
|
||||
char *script_name = NULL;
|
||||
|
||||
@@ -533,11 +526,8 @@ int uwsgi_cgi_request(struct wsgi_request *wsgi_req) {
|
||||
return UWSGI_OK;
|
||||
}
|
||||
|
||||
if (is_a_file) {
|
||||
command = docroot;
|
||||
}
|
||||
else {
|
||||
command = full_path;
|
||||
// get the helper
|
||||
if (!is_a_file) {
|
||||
helper = uwsgi_cgi_get_helper(full_path);
|
||||
|
||||
if (helper == NULL) {
|
||||
@@ -551,28 +541,43 @@ int uwsgi_cgi_request(struct wsgi_request *wsgi_req) {
|
||||
}
|
||||
}
|
||||
|
||||
int ret = uwsgi_cgi_run(wsgi_req, docroot, docroot_len, full_path, helper, path_info, script_name, is_a_file, discard_base);
|
||||
if (need_free) free(docroot);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int uwsgi_cgi_run(struct wsgi_request *wsgi_req, char *docroot, size_t docroot_len, char *full_path, char *helper, char *path_info, char *script_name, int is_a_file, int discard_base) {
|
||||
|
||||
int cgi_pipe[2];
|
||||
int post_pipe[2];
|
||||
int nargs = 0;
|
||||
int waitpid_status;
|
||||
ssize_t len;
|
||||
int i;
|
||||
char **argv;
|
||||
|
||||
char *command = full_path;
|
||||
|
||||
if (is_a_file) {
|
||||
command = docroot;
|
||||
}
|
||||
|
||||
if (pipe(cgi_pipe)) {
|
||||
if (need_free)
|
||||
free(docroot);
|
||||
uwsgi_error("pipe()");
|
||||
uwsgi_error("uwsgi_cgi_run()/pipe()");
|
||||
return UWSGI_OK;
|
||||
}
|
||||
|
||||
if (pipe(post_pipe)) {
|
||||
if (need_free)
|
||||
free(docroot);
|
||||
close(cgi_pipe[0]);
|
||||
close(cgi_pipe[1]);
|
||||
uwsgi_error("pipe()");
|
||||
uwsgi_error("uwsgi_cgi_run()/pipe()");
|
||||
return UWSGI_OK;
|
||||
}
|
||||
|
||||
cgi_pid = fork();
|
||||
pid_t cgi_pid = fork();
|
||||
|
||||
if (cgi_pid < 0) {
|
||||
uwsgi_error("fork()");
|
||||
if (need_free)
|
||||
free(docroot);
|
||||
uwsgi_error("uwsgi_cgi_run()/fork()");
|
||||
close(cgi_pipe[0]);
|
||||
close(cgi_pipe[1]);
|
||||
close(post_pipe[0]);
|
||||
@@ -582,9 +587,6 @@ int uwsgi_cgi_request(struct wsgi_request *wsgi_req) {
|
||||
|
||||
if (cgi_pid > 0) {
|
||||
|
||||
if (need_free)
|
||||
free(docroot);
|
||||
|
||||
close(cgi_pipe[1]);
|
||||
close(post_pipe[0]);
|
||||
|
||||
@@ -795,7 +797,7 @@ clear2:
|
||||
}
|
||||
*base = '/';
|
||||
}
|
||||
else {
|
||||
else if (docroot_len > 0) {
|
||||
if (chdir(docroot)) {
|
||||
uwsgi_error("chdir()");
|
||||
}
|
||||
@@ -837,9 +839,8 @@ clear2:
|
||||
escape_shell_arg(arg_copy, arg_copy_len, argv[i]);
|
||||
i++;
|
||||
p = strtok(NULL, "+");
|
||||
}
|
||||
}
|
||||
else {
|
||||
}
|
||||
free(qs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -864,7 +865,7 @@ clear2:
|
||||
}
|
||||
|
||||
if (execvp(argv[0], argv)) {
|
||||
uwsgi_error("execvp()");
|
||||
uwsgi_error("uwsgi_cgi_run()/execvp()");
|
||||
}
|
||||
|
||||
// never here
|
||||
@@ -872,11 +873,73 @@ clear2:
|
||||
}
|
||||
|
||||
|
||||
void uwsgi_cgi_after_request(struct wsgi_request *wsgi_req) {
|
||||
|
||||
static void uwsgi_cgi_after_request(struct wsgi_request *wsgi_req) {
|
||||
log_request(wsgi_req);
|
||||
}
|
||||
|
||||
#ifdef UWSGI_ROUTING
|
||||
static int uwsgi_routing_func_cgi(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_command = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data, ur->data_len);
|
||||
if (!ub_command) return UWSGI_ROUTE_BREAK;
|
||||
struct uwsgi_buffer *ub_helper = NULL;
|
||||
if (ur->data2_len) {
|
||||
ub_helper = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data2, ur->data2_len);
|
||||
if (!ub_helper) {
|
||||
uwsgi_buffer_destroy(ub_command);
|
||||
return UWSGI_ROUTE_BREAK;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!uwsgi_is_file(ub_command->buf)) {
|
||||
uwsgi_404(wsgi_req);
|
||||
uwsgi_buffer_destroy(ub_command);
|
||||
return UWSGI_ROUTE_BREAK;
|
||||
}
|
||||
|
||||
if (access(ub_command->buf, X_OK)) {
|
||||
uwsgi_403(wsgi_req);
|
||||
uwsgi_buffer_destroy(ub_command);
|
||||
return UWSGI_ROUTE_BREAK;
|
||||
}
|
||||
}
|
||||
// we need a NULL suffix-ed copy of the docroot
|
||||
char *docroot = uwsgi_concat2n(wsgi_req->document_root, wsgi_req->document_root_len, "", 0);
|
||||
uwsgi_cgi_run(wsgi_req, wsgi_req->document_root, wsgi_req->document_root_len, ub_command->buf, ub_helper ? ub_helper->buf : NULL, NULL, NULL, 0, 0 );
|
||||
free(docroot);
|
||||
uwsgi_buffer_destroy(ub_command);
|
||||
if (ub_helper) uwsgi_buffer_destroy(ub_helper);
|
||||
return UWSGI_ROUTE_BREAK;
|
||||
}
|
||||
|
||||
static int uwsgi_router_cgi_helper(struct uwsgi_route *ur, char *args) {
|
||||
ur->func = uwsgi_routing_func_cgi;
|
||||
char *space = strchr(args, ' ');
|
||||
if (!space) {
|
||||
uwsgi_log("invalid cgihelper syntax, must be \"cgihelper:helper command\"\n");
|
||||
return -1;
|
||||
}
|
||||
*space = 0;
|
||||
ur->data = space+1;
|
||||
ur->data_len = strlen(space+1);
|
||||
ur->data2 = args;
|
||||
ur->data2_len = strlen(args);
|
||||
return 0;
|
||||
}
|
||||
static int uwsgi_router_cgi(struct uwsgi_route *ur, char *args) {
|
||||
ur->func = uwsgi_routing_func_cgi;
|
||||
ur->data = args;
|
||||
ur->data_len = strlen(args);
|
||||
return 0;
|
||||
}
|
||||
static void uwsgi_cgi_register_router() {
|
||||
uwsgi_register_router("cgi", uwsgi_router_cgi);
|
||||
uwsgi_register_router("cgihelper", uwsgi_router_cgi_helper);
|
||||
}
|
||||
#endif
|
||||
|
||||
struct uwsgi_plugin cgi_plugin = {
|
||||
|
||||
@@ -887,5 +950,8 @@ struct uwsgi_plugin cgi_plugin = {
|
||||
.options = uwsgi_cgi_options,
|
||||
.request = uwsgi_cgi_request,
|
||||
.after_request = uwsgi_cgi_after_request,
|
||||
#ifdef UWSGI_ROUTING
|
||||
.on_load = uwsgi_cgi_register_router,
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
#include <uwsgi.h>
|
||||
|
||||
#include <client/dbclient.h>
|
||||
#include <client/gridfs.h>
|
||||
|
||||
struct uwsgi_gridfs_mountpoint {
|
||||
char *mountpoint;
|
||||
uint16_t mountpoint_len;
|
||||
char *server;
|
||||
char *db;
|
||||
char *timeout_str;
|
||||
int timeout;
|
||||
char *no_mime;
|
||||
char *orig_filename;
|
||||
char *md5;
|
||||
char *etag;
|
||||
char *prefix;
|
||||
char *itemname;
|
||||
uint16_t itemname_len;
|
||||
char *skip_slash;
|
||||
uint16_t prefix_len;
|
||||
};
|
||||
|
||||
struct uwsgi_gridfs {
|
||||
int debug;
|
||||
struct uwsgi_string_list *mountpoints;
|
||||
} ugridfs;
|
||||
|
||||
struct uwsgi_option uwsgi_gridfs_options[] = {
|
||||
{(char *)"gridfs-mount", required_argument, 0, (char *)"mount a gridfs db on the specified mountpoint", uwsgi_opt_add_string_list, &ugridfs.mountpoints, UWSGI_OPT_MIME},
|
||||
{(char *)"gridfs-debug", no_argument, 0, (char *)"report gridfs mountpoint and itemname for each request (debug)", uwsgi_opt_true, &ugridfs.debug, UWSGI_OPT_MIME},
|
||||
{0, 0, 0, 0, 0, 0, 0},
|
||||
};
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
extern struct uwsgi_plugin gridfs_plugin;
|
||||
|
||||
static void uwsgi_gridfs_do(struct wsgi_request *wsgi_req, struct uwsgi_gridfs_mountpoint *ugm, char *itemname, int need_free) {
|
||||
|
||||
try {
|
||||
mongo::scoped_ptr<mongo::ScopedDbConnection> conn( mongo::ScopedDbConnection::getScopedDbConnection(ugm->server, ugm->timeout) );
|
||||
try {
|
||||
mongo::GridFS gridfs((*conn).conn(), ugm->db);
|
||||
mongo::GridFile gfile = gridfs.findFile(itemname);
|
||||
if (need_free) {
|
||||
free(itemname);
|
||||
itemname = NULL;
|
||||
}
|
||||
if (!gfile.exists()) {
|
||||
(*conn).done();
|
||||
uwsgi_404(wsgi_req);
|
||||
return;
|
||||
}
|
||||
uwsgi_response_prepare_headers(wsgi_req, (char *)"200 OK", 6);
|
||||
// first get the content_type (if possibile)
|
||||
std::string filename = gfile.getFilename();
|
||||
if (!ugm->no_mime) {
|
||||
size_t mime_type_len = 0;
|
||||
char *mime_type = uwsgi_get_mime_type((char *)filename.c_str(), filename.length(), &mime_type_len);
|
||||
if (mime_type) {
|
||||
uwsgi_response_add_content_type(wsgi_req, mime_type, mime_type_len);
|
||||
}
|
||||
}
|
||||
if (ugm->orig_filename) {
|
||||
char *filename_header = uwsgi_concat3((char *)"inline; filename=\"", (char *)filename.c_str(), (char *)"\"");
|
||||
uwsgi_response_add_header(wsgi_req, (char *)"Content-Disposition", 19, filename_header, 19 + filename.length());
|
||||
free(filename_header);
|
||||
}
|
||||
uwsgi_response_add_content_length(wsgi_req, gfile.getContentLength());
|
||||
|
||||
if (ugm->etag) {
|
||||
std::string g_md5 = gfile.getMD5();
|
||||
if (!g_md5.empty()) {
|
||||
char *etag = uwsgi_concat3((char *)"\"", (char *)g_md5.c_str(), (char *)"\"");
|
||||
uwsgi_response_add_header(wsgi_req, (char *)"ETag", 4, etag, 2+g_md5.length());
|
||||
free(etag);
|
||||
}
|
||||
}
|
||||
|
||||
if (ugm->md5) {
|
||||
std::string g_md5 = gfile.getMD5();
|
||||
size_t base64_len = 0;
|
||||
char *base64 = uwsgi_base64_encode((char *)g_md5.c_str(), g_md5.length(), &base64_len);
|
||||
uwsgi_response_add_header(wsgi_req, (char *) "Content-MD5", 11, base64, base64_len);
|
||||
free(base64);
|
||||
}
|
||||
|
||||
if (uwsgi_strncmp(wsgi_req->method, wsgi_req->method_len, (char *)"HEAD", 4)) {
|
||||
int nc = gfile.getNumChunks();
|
||||
int i;
|
||||
for(i=0;i<nc;i++) {
|
||||
mongo::GridFSChunk gchunk = gfile.getChunk(i);
|
||||
int chunk_len = 0;
|
||||
const char *chunk = gchunk.data(chunk_len);
|
||||
uwsgi_response_write_body_do(wsgi_req, (char *) chunk, chunk_len);
|
||||
}
|
||||
}
|
||||
(*conn).done();
|
||||
}
|
||||
catch ( mongo::DBException &e ) {
|
||||
uwsgi_log("[uwsgi-gridfs]: %s\n", e.what());
|
||||
(*conn).done();
|
||||
if (need_free && itemname) {
|
||||
free(itemname);
|
||||
itemname = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch ( mongo::DBException &e ) {
|
||||
uwsgi_log("[uwsgi-gridfs]: %s\n", e.what());
|
||||
if (need_free && itemname) {
|
||||
free(itemname);
|
||||
itemname = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static struct uwsgi_gridfs_mountpoint *uwsgi_gridfs_add_mountpoint(char *arg, size_t arg_len) {
|
||||
struct uwsgi_gridfs_mountpoint *ugm = (struct uwsgi_gridfs_mountpoint *) uwsgi_calloc(sizeof(struct uwsgi_gridfs_mountpoint));
|
||||
if (uwsgi_kvlist_parse(arg, arg_len, ',', '=',
|
||||
"mountpoint", &ugm->mountpoint,
|
||||
"server", &ugm->server,
|
||||
"db", &ugm->db,
|
||||
"prefix", &ugm->prefix,
|
||||
"no_mime", &ugm->no_mime,
|
||||
"timeout", &ugm->timeout_str,
|
||||
"orig_filename", &ugm->orig_filename,
|
||||
"skip_slash", &ugm->skip_slash,
|
||||
"md5", &ugm->md5,
|
||||
"etag", &ugm->etag,
|
||||
"itemname", &ugm->itemname,
|
||||
"item", &ugm->itemname,
|
||||
NULL)) {
|
||||
uwsgi_log("invalid gridfs mountpoint syntax\n");
|
||||
free(ugm);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!ugm->db) {
|
||||
uwsgi_log("you need to specify a \"db\" name for gridfs\n");
|
||||
free(ugm);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!ugm->mountpoint) {
|
||||
ugm->mountpoint = (char *)"";
|
||||
}
|
||||
ugm->mountpoint_len = strlen(ugm->mountpoint);
|
||||
|
||||
if (!ugm->server) {
|
||||
ugm->server = (char *)"127.0.0.1:27017";
|
||||
}
|
||||
|
||||
if (ugm->timeout_str) {
|
||||
ugm->timeout = atoi(ugm->timeout_str);
|
||||
}
|
||||
else {
|
||||
ugm->timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
}
|
||||
|
||||
if (ugm->prefix) {
|
||||
ugm->prefix_len = strlen(ugm->prefix);
|
||||
}
|
||||
|
||||
if (ugm->itemname) {
|
||||
ugm->itemname_len = strlen(ugm->itemname);
|
||||
}
|
||||
|
||||
return ugm;
|
||||
}
|
||||
|
||||
extern "C" int uwsgi_gridfs_request(struct wsgi_request *wsgi_req) {
|
||||
// this is the gridfs file
|
||||
char *itemname = NULL;
|
||||
|
||||
/* Standard GridFS request */
|
||||
if (!wsgi_req->uh->pktsize) {
|
||||
uwsgi_log( "Empty GridFS request. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (uwsgi_parse_vars(wsgi_req)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (wsgi_req->appid_len == 0) {
|
||||
if (!uwsgi.ignore_script_name) {
|
||||
wsgi_req->appid = wsgi_req->script_name;
|
||||
wsgi_req->appid_len = wsgi_req->script_name_len;
|
||||
}
|
||||
}
|
||||
|
||||
wsgi_req->app_id = uwsgi_get_app_id(wsgi_req->appid, wsgi_req->appid_len, gridfs_plugin.modifier1);
|
||||
if (ugridfs.debug) {
|
||||
uwsgi_log("[uwsgi-gridfs-debug] app_id = %d\n", wsgi_req->app_id);
|
||||
}
|
||||
|
||||
if (wsgi_req->app_id == -1) {
|
||||
uwsgi_404(wsgi_req);
|
||||
return UWSGI_OK;
|
||||
}
|
||||
|
||||
struct uwsgi_app *ua = &uwsgi_apps[wsgi_req->app_id];
|
||||
|
||||
struct uwsgi_gridfs_mountpoint *ugm = (struct uwsgi_gridfs_mountpoint *) ua->interpreter;
|
||||
|
||||
if (ugm->skip_slash && (wsgi_req->path_info_len > 0 && wsgi_req->path_info[0] == '/')) {
|
||||
itemname = uwsgi_concat2n(ugm->prefix, ugm->prefix_len, wsgi_req->path_info+1, wsgi_req->path_info_len-1);
|
||||
}
|
||||
else {
|
||||
itemname = uwsgi_concat2n(ugm->prefix, ugm->prefix_len, wsgi_req->path_info, wsgi_req->path_info_len);
|
||||
}
|
||||
|
||||
if (ugridfs.debug) {
|
||||
uwsgi_log("[uwsgi-gridfs-debug] itemname = %s\n", itemname);
|
||||
}
|
||||
|
||||
// itemname will be freed here
|
||||
uwsgi_gridfs_do(wsgi_req, ugm, itemname, 1);
|
||||
|
||||
return UWSGI_OK;
|
||||
|
||||
}
|
||||
|
||||
|
||||
extern "C" void uwsgi_gridfs_mount() {
|
||||
struct uwsgi_string_list *usl = ugridfs.mountpoints;
|
||||
while(usl) {
|
||||
if (uwsgi_apps_cnt >= uwsgi.max_apps) {
|
||||
uwsgi_log("ERROR: you cannot load more than %d apps in a worker\n", uwsgi.max_apps);
|
||||
exit(1);
|
||||
}
|
||||
int id = uwsgi_apps_cnt;
|
||||
struct uwsgi_gridfs_mountpoint *ugm = uwsgi_gridfs_add_mountpoint(uwsgi_str(usl->value), usl->len);
|
||||
if (!ugm) exit(1);
|
||||
uwsgi_add_app(id, gridfs_plugin.modifier1, ugm->mountpoint, ugm->mountpoint_len, ugm, ugm);
|
||||
uwsgi_emulate_cow_for_apps(id);
|
||||
uwsgi_log("GridFS mountpoint \"%.*s\" (%d) added: server=%s db=%s\n", ugm->mountpoint_len, ugm->mountpoint, id, ugm->server, ugm->db);
|
||||
usl = usl->next;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef UWSGI_ROUTING
|
||||
static int uwsgi_routing_func_gridfs(struct wsgi_request *wsgi_req, struct uwsgi_route *ur){
|
||||
|
||||
struct uwsgi_gridfs_mountpoint *ugm = (struct uwsgi_gridfs_mountpoint *) ur->data2;
|
||||
|
||||
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
|
||||
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
|
||||
|
||||
struct uwsgi_buffer *ub_itemname = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ugm->itemname, ugm->itemname_len);
|
||||
if (!ub_itemname) return UWSGI_ROUTE_BREAK;
|
||||
if (ugridfs.debug) {
|
||||
uwsgi_log("[uwsgi-gridfs-debug] itemname = %s\n", ub_itemname->buf);
|
||||
}
|
||||
uwsgi_gridfs_do(wsgi_req, ugm, ub_itemname->buf, 0);
|
||||
uwsgi_buffer_destroy(ub_itemname);
|
||||
return UWSGI_ROUTE_BREAK;
|
||||
}
|
||||
|
||||
extern "C" int uwsgi_router_gridfs(struct uwsgi_route *ur, char *args) {
|
||||
ur->func = uwsgi_routing_func_gridfs;
|
||||
ur->data = args;
|
||||
ur->data_len = strlen(args);
|
||||
ur->data2 = uwsgi_gridfs_add_mountpoint((char *)ur->data, ur->data_len);
|
||||
if (!ur->data2) {
|
||||
exit(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <uwsgi.h>
|
||||
|
||||
int uwsgi_gridfs_request(struct wsgi_request *);
|
||||
void uwsgi_gridfs_mount();
|
||||
extern struct uwsgi_option uwsgi_gridfs_options[];
|
||||
|
||||
static void uwsgi_gridfs_log(struct wsgi_request *wsgi_req) {
|
||||
log_request(wsgi_req);
|
||||
}
|
||||
|
||||
#ifdef UWSGI_ROUTING
|
||||
int uwsgi_router_gridfs(struct uwsgi_route *, char *);
|
||||
static void uwsgi_gridfs_register_router() {
|
||||
uwsgi_register_router("gridfs", uwsgi_router_gridfs);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
struct uwsgi_plugin gridfs_plugin = {
|
||||
|
||||
.name = "gridfs",
|
||||
.modifier1 = 25,
|
||||
|
||||
.init_apps = uwsgi_gridfs_mount,
|
||||
.options = uwsgi_gridfs_options,
|
||||
.request = uwsgi_gridfs_request,
|
||||
.after_request = uwsgi_gridfs_log,
|
||||
#ifdef UWSGI_ROUTING
|
||||
.on_load = uwsgi_gridfs_register_router,
|
||||
#endif
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
import os
|
||||
|
||||
NAME='gridfs'
|
||||
|
||||
CFLAGS = ['-I/usr/include/mongo','-I/usr/local/include/mongo']
|
||||
LDFLAGS = []
|
||||
|
||||
LIBS = []
|
||||
if not 'UWSGI_MONGODB_NOLIB' in os.environ:
|
||||
LIBS.append('-lmongoclient')
|
||||
LIBS.append('-lstdc++')
|
||||
LIBS.append('-lboost_thread')
|
||||
LIBS.append('-lboost_system')
|
||||
LIBS.append('-lboost_filesystem')
|
||||
|
||||
GCC_LIST = ['plugin', 'gridfs.cc']
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../../uwsgi.h"
|
||||
#include <uwsgi.h>
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
@@ -29,7 +29,7 @@ struct uwsgi_mongodb_state {
|
||||
struct iovec iovec[13];
|
||||
};
|
||||
|
||||
ssize_t uwsgi_mongodb_logger(struct uwsgi_logger *ul, char *message, size_t len) {
|
||||
static ssize_t uwsgi_mongodb_logger(struct uwsgi_logger *ul, char *message, size_t len) {
|
||||
|
||||
struct uwsgi_mongodb_state *ums = NULL;
|
||||
|
||||
@@ -151,7 +151,7 @@ done:
|
||||
return ret;
|
||||
}
|
||||
|
||||
void uwsgi_mongodblog_register() {
|
||||
static void uwsgi_mongodblog_register() {
|
||||
uwsgi_register_logger("mongodblog", uwsgi_mongodb_logger);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,8 +9,11 @@ extern struct uwsgi_plugin psgi_plugin;
|
||||
XS(XS_input_seek) {
|
||||
|
||||
dXSARGS;
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
psgi_check_args(1);
|
||||
uwsgi_request_body_seek(wsgi_req, SvIV(ST(0)));
|
||||
|
||||
XSRETURN(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -196,8 +196,6 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre
|
||||
goto doh;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// the module contains multiple apps
|
||||
if (PyDict_Check((PyObject *)wi->callable)) {
|
||||
applications = wi->callable;
|
||||
|
||||
@@ -1063,9 +1063,7 @@ void uwsgi_python_init_apps() {
|
||||
#endif
|
||||
|
||||
// setup app loaders
|
||||
#ifdef UWSGI_MINTERPRETERS
|
||||
up.loaders[LOADER_DYN] = uwsgi_dyn_loader;
|
||||
#endif
|
||||
up.loaders[LOADER_UWSGI] = uwsgi_uwsgi_loader;
|
||||
up.loaders[LOADER_FILE] = uwsgi_file_loader;
|
||||
up.loaders[LOADER_PASTE] = uwsgi_paste_loader;
|
||||
|
||||
@@ -268,10 +268,6 @@ struct uwsgi_buffer *uwsgi_python_exception_repr(struct wsgi_request *);
|
||||
struct uwsgi_buffer *uwsgi_python_backtrace(struct wsgi_request *);
|
||||
void uwsgi_python_exception_log(struct wsgi_request *);
|
||||
|
||||
#ifdef UWSGI_PYPY
|
||||
#undef UWSGI_MINTERPRETERS
|
||||
#endif
|
||||
|
||||
#define py_current_wsgi_req() current_wsgi_req();\
|
||||
if (!wsgi_req) {\
|
||||
return PyErr_Format(PyExc_SystemError, "you can call uwsgi api function only from the main callable");\
|
||||
|
||||
@@ -134,6 +134,7 @@ error:
|
||||
if (urcc->key) free(urcc->key);
|
||||
if (urcc->name) free(urcc->name);
|
||||
if (urcc->expires_str) free(urcc->expires_str);
|
||||
free(urcc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
+11
-2
@@ -5,6 +5,9 @@ void uwsgi_v8_apps(void);
|
||||
void uwsgi_v8_configurator(char *, char **);
|
||||
uint16_t uwsgi_v8_rpc(void *, uint8_t, char **, uint16_t *, char *);
|
||||
int uwsgi_v8_signal_handler(uint8_t, void *);
|
||||
void uwsgi_v8_init_thread(int);
|
||||
void uwsgi_v8_enable_threads();
|
||||
int uwsgi_v8_mule(char *);
|
||||
|
||||
static void uwsgi_v8_register(void) {
|
||||
uwsgi_register_configurator(".js", uwsgi_v8_configurator);
|
||||
@@ -12,8 +15,10 @@ static void uwsgi_v8_register(void) {
|
||||
|
||||
extern struct uwsgi_option uwsgi_v8_options[];
|
||||
|
||||
static int uwsgi_v8_request(struct wsgi_request *wsgi_req) {
|
||||
return UWSGI_OK;
|
||||
int uwsgi_v8_request(struct wsgi_request *);
|
||||
|
||||
static void uwsgi_v8_after_request(struct wsgi_request *wsgi_req) {
|
||||
log_request(wsgi_req);
|
||||
}
|
||||
|
||||
struct uwsgi_plugin v8_plugin = {
|
||||
@@ -25,5 +30,9 @@ struct uwsgi_plugin v8_plugin = {
|
||||
.on_load = uwsgi_v8_register,
|
||||
.rpc = uwsgi_v8_rpc,
|
||||
.request = uwsgi_v8_request,
|
||||
.after_request = uwsgi_v8_after_request,
|
||||
.signal_handler = uwsgi_v8_signal_handler,
|
||||
.enable_threads = uwsgi_v8_enable_threads,
|
||||
.init_thread = uwsgi_v8_init_thread,
|
||||
.mule = uwsgi_v8_mule,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
NAME='v8'
|
||||
|
||||
CFLAGS = []
|
||||
CFLAGS = ['-Wno-deprecated-declarations']
|
||||
LDFLAGS = []
|
||||
LIBS = ['-lv8']
|
||||
GCC_LIST = ['plugin', 'v8_uwsgi.cc']
|
||||
GCC_LIST = ['plugin', 'v8_uwsgi.cc', 'v8_commonjs.cc', 'v8_jsgi.cc']
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "v8_uwsgi.h"
|
||||
|
||||
extern struct uwsgi_v8 uv8;
|
||||
|
||||
static v8::Handle<v8::Value> uwsgi_v8_commonjs_require_do(char *filename) {
|
||||
|
||||
size_t len = 0;
|
||||
char *code = uwsgi_open_and_read(filename, &len, 1, NULL);
|
||||
|
||||
// we re-create every time an "exports" object to emulate a local object
|
||||
v8::Local<v8::Object> exports = v8::Object::New();
|
||||
v8::Context::GetCurrent()->Global()->Set(v8::String::New("exports"), exports);
|
||||
|
||||
// we do not use TryCatch as we directly use stderr and simply exit with error code 1
|
||||
v8::Handle<v8::Script> script = v8::Script::Compile( v8::String::New(code), v8::String::New(filename) );
|
||||
free(code);
|
||||
if (script.IsEmpty()) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> result = script->Run();
|
||||
if (result.IsEmpty()) {
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
return exports;
|
||||
}
|
||||
|
||||
static v8::Handle<v8::Value> uwsgi_v8_commonjs_require(const v8::Arguments& args) {
|
||||
if (args.Length() > 0) {
|
||||
v8::String::Utf8Value module_name(args[0]->ToString());
|
||||
// ok lets start searching the module
|
||||
if (uwsgi_is_file(*module_name)) {
|
||||
return uwsgi_v8_commonjs_require_do(*module_name);
|
||||
}
|
||||
|
||||
// try appending .js extension
|
||||
if (!uwsgi_endswith(*module_name, (char *)".js")) {
|
||||
char *tmp_filename = uwsgi_concat2(*module_name, (char *)".js");
|
||||
if (uwsgi_is_file(tmp_filename)) {
|
||||
v8::Handle<v8::Value> ret = uwsgi_v8_commonjs_require_do(tmp_filename);
|
||||
free(tmp_filename);
|
||||
return ret;
|
||||
}
|
||||
free(tmp_filename);
|
||||
}
|
||||
|
||||
// let's start searching in the modules search path
|
||||
struct uwsgi_string_list *usl = uv8.module_paths;
|
||||
while(usl) {
|
||||
char *tmp_filename = uwsgi_concat3(usl->value, (char *)"/", *module_name);
|
||||
if (uwsgi_is_file(tmp_filename)) {
|
||||
v8::Handle<v8::Value> ret = uwsgi_v8_commonjs_require_do(tmp_filename);
|
||||
free(tmp_filename);
|
||||
return ret;
|
||||
}
|
||||
free(tmp_filename);
|
||||
if (!uwsgi_endswith(*module_name, (char *)".js")) {
|
||||
tmp_filename = uwsgi_concat4(usl->value, (char *)"/", *module_name, (char *)".js");
|
||||
if (uwsgi_is_file(tmp_filename)) {
|
||||
v8::Handle<v8::Value> ret = uwsgi_v8_commonjs_require_do(tmp_filename);
|
||||
free(tmp_filename);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
free(tmp_filename);
|
||||
usl = usl->next;
|
||||
}
|
||||
}
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
void uwsgi_v8_add_commonjs(v8::Handle<v8::ObjectTemplate> global) {
|
||||
global->Set(v8::String::New("require"), v8::FunctionTemplate::New(uwsgi_v8_commonjs_require));
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
#include "v8_uwsgi.h"
|
||||
|
||||
extern struct uwsgi_v8 uv8;
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
static v8::Handle<v8::Value> uwsgi_v8_jsgi_body_chunk(const v8::Arguments& args) {
|
||||
|
||||
if (args.Length() > 1) {
|
||||
v8::String::Utf8Value chunk(args[0]->ToString());
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
if (uwsgi_response_write_body_do(wsgi_req, *chunk, args[0]->ToString()->Length())) {
|
||||
return v8::ThrowException(v8::Exception::Error(v8::String::New("unable to send JSGI body")));
|
||||
}
|
||||
}
|
||||
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
static void uwsgi_v8_jsgi_fill_request(struct wsgi_request *wsgi_req, v8::Handle<v8::Object> o) {
|
||||
o->Set(v8::String::New("method"), v8::String::New(wsgi_req->method, wsgi_req->method_len));
|
||||
o->Set(v8::String::New("scriptName"), v8::String::New(wsgi_req->script_name, wsgi_req->script_name_len));
|
||||
o->Set(v8::String::New("pathInfo"), v8::String::New(wsgi_req->path_info, wsgi_req->path_info_len));
|
||||
o->Set(v8::String::New("queryString"), v8::String::New(wsgi_req->query_string, wsgi_req->query_string_len));
|
||||
o->Set(v8::String::New("host"), v8::String::New(wsgi_req->host, wsgi_req->host_len));
|
||||
if (wsgi_req->scheme_len) {
|
||||
o->Set(v8::String::New("scheme"), v8::String::New(wsgi_req->scheme, wsgi_req->scheme_len));
|
||||
}
|
||||
else {
|
||||
o->Set(v8::String::New("scheme"), v8::String::New("http"));
|
||||
}
|
||||
o->Set(v8::String::New("remoteAddr"), v8::String::New(wsgi_req->remote_addr, wsgi_req->remote_addr_len));
|
||||
if (wsgi_req->remote_user_len) {
|
||||
o->Set(v8::String::New("remoteUser"), v8::String::New(wsgi_req->remote_user, wsgi_req->remote_user_len));
|
||||
}
|
||||
o->Set(v8::String::New("serverSoftware"), v8::String::New("uWSGI " UWSGI_VERSION));
|
||||
|
||||
v8::Handle<v8::Value> jsgi = v8::Object::New();
|
||||
if (uwsgi.threads > 1) {
|
||||
jsgi->ToObject()->Set(v8::String::New("multithread"), v8::True());
|
||||
}
|
||||
else {
|
||||
jsgi->ToObject()->Set(v8::String::New("multithread"), v8::False());
|
||||
}
|
||||
|
||||
if (uwsgi.numproc > 1) {
|
||||
jsgi->ToObject()->Set(v8::String::New("multiprocess"), v8::True());
|
||||
}
|
||||
else {
|
||||
jsgi->ToObject()->Set(v8::String::New("multiprocess"), v8::False());
|
||||
}
|
||||
o->Set(v8::String::New("jsgi"), jsgi);
|
||||
}
|
||||
|
||||
extern "C" int uwsgi_v8_request(struct wsgi_request *wsgi_req) {
|
||||
char status_str[11];
|
||||
uint32_t i,l;
|
||||
v8::Handle<v8::Value> status, headers, body;
|
||||
v8::Local<v8::Array> props;
|
||||
v8::Local<v8::Value> key, value;
|
||||
v8::Handle<v8::Function> forEach;
|
||||
|
||||
/* Standard JSGI 3.0 request */
|
||||
if (!wsgi_req->uh->pktsize) {
|
||||
uwsgi_log( "Empty JSGI request. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (uwsgi_parse_vars(wsgi_req)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int core_id = wsgi_req->async_id;
|
||||
|
||||
uv8.isolates[core_id]->Enter();
|
||||
uv8.contexts[core_id]->Enter();
|
||||
v8::HandleScope handle_scope;
|
||||
v8::Handle<v8::Value> argj[1];
|
||||
argj[0] = v8::Object::New();
|
||||
uwsgi_v8_jsgi_fill_request(wsgi_req, argj[0]->ToObject());
|
||||
v8::Handle<v8::Value> result = uv8.jsgi_func[core_id]->Call(uv8.contexts[core_id]->Global(), 1, argj);
|
||||
if (result.IsEmpty()) goto end;
|
||||
if (!result->IsObject()) goto end;
|
||||
|
||||
status = result->ToObject()->Get(v8::String::New("status"));
|
||||
if (status.IsEmpty() || !status->IsNumber()) {
|
||||
uwsgi_log("invalid JSGI response status\n");
|
||||
exit(1);
|
||||
}
|
||||
headers = result->ToObject()->Get(v8::String::New("headers"));
|
||||
if (headers.IsEmpty() || !headers->IsObject()) {
|
||||
uwsgi_log("invalid JSGI response headers\n");
|
||||
exit(1);
|
||||
}
|
||||
body = result->ToObject()->Get(v8::String::New("body"));
|
||||
if (body.IsEmpty() || !body->IsObject()) {
|
||||
uwsgi_log("invalid JSGI response body\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (uwsgi_num2str2(status->Uint32Value(), status_str) != 3) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (uwsgi_response_prepare_headers(wsgi_req, status_str, 3)) goto end;
|
||||
|
||||
props = headers->ToObject()->GetPropertyNames();
|
||||
l = props->Length();
|
||||
|
||||
for(i=0;i<l;i++) {
|
||||
key = props->Get(i);
|
||||
value = headers->ToObject()->Get(key);
|
||||
v8::String::Utf8Value c_key(key->ToString());
|
||||
if (value->IsArray()) {
|
||||
uint32_t opt_l = value->ToObject()->Get(v8::String::New("length"))->ToObject()->Uint32Value();
|
||||
uint32_t j;
|
||||
for(j=0;j<opt_l;j++) {
|
||||
v8::String::Utf8Value c_value(value->ToObject()->Get(j)->ToString());
|
||||
if (uwsgi_response_add_header(wsgi_req, *c_key, strlen(*c_key), *c_value, strlen(*c_value))) goto end;
|
||||
}
|
||||
}
|
||||
else {
|
||||
v8::String::Utf8Value c_value(value->ToString());
|
||||
if (uwsgi_response_add_header(wsgi_req, *c_key, strlen(*c_key), *c_value, strlen(*c_value))) goto end;
|
||||
}
|
||||
}
|
||||
|
||||
// call forEach
|
||||
forEach = v8::Handle<v8::Function>::Cast(body->ToObject()->Get(v8::String::New("forEach")));
|
||||
argj[0] = uv8.jsgi_writer_func[core_id];
|
||||
forEach->Call(body->ToObject(), 1, argj);
|
||||
|
||||
end:
|
||||
while(!v8::V8::IdleNotification()) {};
|
||||
return UWSGI_OK;
|
||||
}
|
||||
|
||||
v8::Persistent<v8::Function> uwsgi_v8_load_jsgi(int core_id, char *filename) {
|
||||
|
||||
uv8.isolates[core_id]->Enter();
|
||||
uv8.contexts[core_id]->Enter();
|
||||
v8::HandleScope handle_scope;
|
||||
|
||||
size_t len = 0;
|
||||
char *code = uwsgi_open_and_read(filename, &len, 1, NULL);
|
||||
// we re-create every time an "exports" object to emulate a local object
|
||||
v8::Local<v8::Object> exports = v8::Object::New();
|
||||
v8::Context::GetCurrent()->Global()->Set(v8::String::New("exports"), exports);
|
||||
|
||||
// we do not use TryCatch as we directly use stderr and simply exit with error code 1
|
||||
v8::Handle<v8::Script> script = v8::Script::Compile( v8::String::New(code), v8::String::New(filename) );
|
||||
free(code);
|
||||
if (script.IsEmpty()) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> result = script->Run();
|
||||
if (result.IsEmpty()) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uv8.jsgi_writer_func[core_id] = v8::Persistent<v8::Function>::New(v8::FunctionTemplate::New(uwsgi_v8_jsgi_body_chunk)->GetFunction());
|
||||
|
||||
v8::Handle<v8::Value> app = exports->Get(v8::String::New("app"));
|
||||
if (!app.IsEmpty() && !app->IsNull() && !app->IsUndefined()) {
|
||||
if (app->ToObject()->IsFunction()) {
|
||||
if (!uv8.jsgi_announced) {
|
||||
uwsgi_log("JSGI 3.0 application loaded from \"exports.app\" in %s\n", filename);
|
||||
uv8.jsgi_announced = -1;
|
||||
}
|
||||
return v8::Persistent<v8::Function>::New(v8::Handle<v8::Function>::Cast(app));
|
||||
}
|
||||
}
|
||||
|
||||
if (!result->IsNull() && !result->IsUndefined() && result->ToObject()->IsFunction()) {
|
||||
if (!uv8.jsgi_announced) {
|
||||
uwsgi_log("JSGI 3.0 application loaded from return value of %s\n", filename);
|
||||
uv8.jsgi_announced = -1;
|
||||
}
|
||||
return v8::Persistent<v8::Function>::New(v8::Handle<v8::Function>::Cast(result));
|
||||
}
|
||||
|
||||
uwsgi_log("unable to find JSGI 3.0 entry point function\n");
|
||||
exit(1);
|
||||
}
|
||||
+165
-70
@@ -1,16 +1,16 @@
|
||||
#include <uwsgi.h>
|
||||
#include <v8.h>
|
||||
#include "v8_uwsgi.h"
|
||||
|
||||
struct uwsgi_v8 {
|
||||
v8::Persistent<v8::Context> *contexts;
|
||||
struct uwsgi_string_list *load;
|
||||
} uv8;
|
||||
struct uwsgi_v8 uv8;
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
extern struct uwsgi_plugin v8_plugin;
|
||||
|
||||
struct uwsgi_option uwsgi_v8_options[] = {
|
||||
{(char *)"v8-load", required_argument, 0, (char *)"load a javascript file", uwsgi_opt_add_string_list, &uv8.load, 0},
|
||||
{(char *)"v8-preemptive", required_argument, 0, (char *)"put v8 in preemptive move (single isolate) with the specified frequency", uwsgi_opt_set_int, &uv8.preemptive, 0},
|
||||
{(char *)"v8-gc-freq", required_argument, 0, (char *)"set the v8 garbage collection frequency", uwsgi_opt_set_64bit, &uv8.gc_freq, 0},
|
||||
{(char *)"v8-module-path", required_argument, 0, (char *)"set the v8 modules search path", uwsgi_opt_add_string_list, &uv8.module_paths, 0},
|
||||
{(char *)"v8-jsgi", required_argument, 0, (char *)"load the specified JSGI 3.0 application", uwsgi_opt_set_str, &uv8.jsgi, 0},
|
||||
{0, 0, 0, 0},
|
||||
};
|
||||
|
||||
@@ -22,10 +22,25 @@ static v8::Handle<v8::Value> uwsgi_v8_api_register_signal(const v8::Arguments& a
|
||||
|
||||
v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::New(v8::Handle<v8::Function>::Cast(args[2]));
|
||||
|
||||
if (uwsgi_register_signal(uwsgi_signal, *signal_kind, *func, v8_plugin.modifier1)) {
|
||||
int core_id = (long) pthread_getspecific(uv8.current_core);
|
||||
struct uwsgi_v8_signal_table *uvst = &uv8.sigtable[uwsgi_signal];
|
||||
|
||||
int need_register = 1;
|
||||
if (uvst->registered == 1) {
|
||||
need_register = 0;
|
||||
}
|
||||
|
||||
uvst->func[core_id] = func;
|
||||
|
||||
if (!need_register) {
|
||||
return v8::True();
|
||||
}
|
||||
|
||||
if (uwsgi_register_signal(uwsgi_signal, *signal_kind, uvst, v8_plugin.modifier1)) {
|
||||
uwsgi_log("[uwsgi-v8] unable to register signal %d\n", uwsgi_signal);
|
||||
return v8::Undefined();
|
||||
}
|
||||
uvst->registered = 1 ;
|
||||
|
||||
return v8::True();
|
||||
}
|
||||
@@ -44,7 +59,33 @@ static v8::Handle<v8::Value> uwsgi_v8_api_register_rpc(const v8::Arguments& args
|
||||
|
||||
v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::New(v8::Handle<v8::Function>::Cast(args[1]));
|
||||
|
||||
if (uwsgi_register_rpc(*name, v8_plugin.modifier1, j_argc, *func)) {
|
||||
int core_id = (long) pthread_getspecific(uv8.current_core);
|
||||
|
||||
// get the rpc slot
|
||||
int i;
|
||||
int found = 0;
|
||||
struct uwsgi_v8_rpc_table *uvrt = NULL;
|
||||
for(i=0;i<(int)uwsgi.rpc_max;i++) {
|
||||
uvrt = &uv8.rpctable[i];
|
||||
if (uvrt->name == NULL) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
// skip already registered funcs
|
||||
else if (!strcmp(uvrt->name, *name)) {
|
||||
uvrt->func[core_id] = func;
|
||||
return v8::True();
|
||||
}
|
||||
}
|
||||
if (!found || !uvrt) {
|
||||
uwsgi_log("[uwsgi-v8] unable to register RPC function \"%s\"\n", *name);
|
||||
return v8::Undefined();
|
||||
}
|
||||
uvrt->name = uwsgi_str(*name);
|
||||
uvrt->func[core_id] = func;
|
||||
|
||||
// we can safely call register_rpc here as it will check for already registered funcs
|
||||
if (uwsgi_register_rpc(*name, v8_plugin.modifier1, j_argc, uvrt)) {
|
||||
uwsgi_log("[uwsgi-v8] unable to register RPC function \"%s\"\n", *name);
|
||||
return v8::Undefined();
|
||||
}
|
||||
@@ -55,11 +96,11 @@ static v8::Handle<v8::Value> uwsgi_v8_api_register_rpc(const v8::Arguments& args
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
static void uwsgi_v8_load_file(v8::Persistent<v8::Context> context, char *filename) {
|
||||
static void uwsgi_v8_load_file(int core_id, char *filename) {
|
||||
|
||||
uv8.isolates[core_id]->Enter();
|
||||
uv8.contexts[core_id]->Enter();
|
||||
v8::HandleScope handle_scope;
|
||||
|
||||
v8::Context::Scope context_scope(context);
|
||||
|
||||
size_t len = 0;
|
||||
char *code = uwsgi_open_and_read(filename, &len, 1, NULL);
|
||||
@@ -78,60 +119,87 @@ static void uwsgi_v8_load_file(v8::Persistent<v8::Context> context, char *filena
|
||||
|
||||
}
|
||||
|
||||
extern "C" int uwsgi_v8_init(){
|
||||
uwsgi_log("Initializing V8 %s environment... (%d Isolates)\n", v8::V8::GetVersion(), uwsgi.cores);
|
||||
uv8.contexts = (v8::Persistent<v8::Context> *) uwsgi_malloc( sizeof(v8::Persistent<v8::Context>*) * uwsgi.cores );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static v8::Handle<v8::Value> uwsgi_v8_api_log(const v8::Arguments& args) {
|
||||
|
||||
if (args.Length() > 0) {
|
||||
v8::String::Utf8Value str(args[0]->ToString());
|
||||
size_t slen = strlen(*str);
|
||||
if ((*str)[slen-1] == '\n') {
|
||||
uwsgi_log("%s", *str);
|
||||
}
|
||||
else {
|
||||
uwsgi_log("%s\n", *str);
|
||||
}
|
||||
}
|
||||
return v8::Undefined();
|
||||
|
||||
if (args.Length() > 0) {
|
||||
v8::String::Utf8Value str(args[0]->ToString());
|
||||
size_t slen = strlen(*str);
|
||||
if ((*str)[slen-1] == '\n') {
|
||||
uwsgi_log("%s", *str);
|
||||
}
|
||||
else {
|
||||
uwsgi_log("%s\n", *str);
|
||||
}
|
||||
}
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
static v8::Persistent<v8::Context> uwsgi_v8_new_isolate(int core) {
|
||||
void uwsgi_v8_add_commonjs(v8::Handle<v8::ObjectTemplate>);
|
||||
|
||||
static v8::Persistent<v8::Context> uwsgi_v8_new_isolate(int core_id) {
|
||||
// create a new isolate
|
||||
v8::Isolate *isolate = v8::Isolate::New();
|
||||
// set as the current isolate
|
||||
isolate->Enter();
|
||||
|
||||
uv8.isolates[core_id] = v8::Isolate::GetCurrent();
|
||||
|
||||
v8::HandleScope handle_scope;
|
||||
if (core > 0) {
|
||||
// create a new isolate
|
||||
v8::Isolate *isolate = v8::Isolate::New();
|
||||
// set as the current isolate
|
||||
v8::Isolate::Scope iscope(isolate);
|
||||
}
|
||||
|
||||
// uWSGI api
|
||||
v8::Handle<v8::ObjectTemplate> uwsgi_api = v8::ObjectTemplate::New();
|
||||
uwsgi_api->Set(v8::String::New("log"), v8::FunctionTemplate::New(uwsgi_v8_api_log));
|
||||
uwsgi_api->Set(v8::String::New("register_rpc"), v8::FunctionTemplate::New(uwsgi_v8_api_register_rpc));
|
||||
uwsgi_api->Set(v8::String::New("register_signal"), v8::FunctionTemplate::New(uwsgi_v8_api_register_signal));
|
||||
|
||||
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
|
||||
// print alias is always handy
|
||||
global->Set(v8::String::New("uwsgi_log"), v8::FunctionTemplate::New(uwsgi_v8_api_log));
|
||||
global->Set(v8::String::New("uwsgi_register_rpc"), v8::FunctionTemplate::New(uwsgi_v8_api_register_rpc));
|
||||
global->Set(v8::String::New("uwsgi_register_signal"), v8::FunctionTemplate::New(uwsgi_v8_api_register_signal));
|
||||
global->Set(v8::String::New("uwsgi"), uwsgi_api);
|
||||
|
||||
uwsgi_v8_add_commonjs(global);
|
||||
|
||||
// create a new context
|
||||
v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
|
||||
return context;
|
||||
return context;
|
||||
}
|
||||
|
||||
extern "C" int uwsgi_v8_init(){
|
||||
int i;
|
||||
uwsgi_log("Initializing V8 %s environment... (%d Isolates)\n", v8::V8::GetVersion(), uwsgi.cores);
|
||||
uv8.isolates = (v8::Isolate **) uwsgi_malloc( sizeof(v8::Isolate *) * uwsgi.cores );
|
||||
uv8.contexts = (v8::Persistent<v8::Context> *) uwsgi_malloc( sizeof(v8::Persistent<v8::Context>) * uwsgi.cores );
|
||||
// allocates rpc and signal tables
|
||||
uv8.rpctable = (struct uwsgi_v8_rpc_table *) uwsgi_calloc(sizeof(struct uwsgi_v8_rpc_table) * uwsgi.rpc_max);
|
||||
for(i=0;i<(int)uwsgi.rpc_max;i++) {
|
||||
uv8.rpctable[i].func = (v8::Persistent<v8::Function>*) uwsgi_calloc(sizeof(v8::Persistent<v8::Function>) * uwsgi.cores);
|
||||
}
|
||||
uv8.sigtable = (struct uwsgi_v8_signal_table *) uwsgi_calloc(sizeof(struct uwsgi_v8_signal_table) * 256);
|
||||
for(i=0;i<256;i++) {
|
||||
uv8.sigtable[i].func = (v8::Persistent<v8::Function>*) uwsgi_calloc(sizeof(v8::Persistent<v8::Function>) * uwsgi.cores);
|
||||
}
|
||||
uv8.jsgi_func = (v8::Persistent<v8::Function> *) uwsgi_calloc( sizeof(v8::Persistent<v8::Function>) * uwsgi.cores );
|
||||
uv8.jsgi_writer_func = (v8::Persistent<v8::Function> *) uwsgi_calloc( sizeof(v8::Persistent<v8::Function>) * uwsgi.cores );
|
||||
|
||||
pthread_key_create(&uv8.current_core, NULL);
|
||||
pthread_setspecific(uv8.current_core, (void *) 0);
|
||||
uv8.contexts[0] = uwsgi_v8_new_isolate(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void uwsgi_v8_apps_do(int core_id) {
|
||||
struct uwsgi_string_list *usl = uv8.load;
|
||||
while(usl) {
|
||||
uwsgi_v8_load_file(core_id, usl->value);
|
||||
usl = usl->next;
|
||||
}
|
||||
|
||||
if (uv8.jsgi) {
|
||||
uv8.jsgi_func[core_id] = uwsgi_v8_load_jsgi(core_id, uv8.jsgi);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void uwsgi_v8_apps() {
|
||||
|
||||
if (!uv8.load) return;
|
||||
|
||||
int i;
|
||||
for(i=0;i<uwsgi.cores;i++) {
|
||||
uv8.contexts[i] = uwsgi_v8_new_isolate(i);
|
||||
struct uwsgi_string_list *usl = uv8.load;
|
||||
while(usl) {
|
||||
uwsgi_v8_load_file(uv8.contexts[i], usl->value);
|
||||
usl = usl->next;
|
||||
}
|
||||
}
|
||||
uwsgi_v8_apps_do(0);
|
||||
}
|
||||
|
||||
extern "C" void uwsgi_v8_configurator(char *filename, char *magic_table[]) {
|
||||
@@ -191,29 +259,33 @@ extern "C" void uwsgi_v8_configurator(char *filename, char *magic_table[]) {
|
||||
|
||||
extern "C" uint16_t uwsgi_v8_rpc(void * func, uint8_t argc, char **argv, uint16_t argvs[], char *buffer) {
|
||||
|
||||
int core_id = (long) pthread_getspecific(uv8.current_core);
|
||||
|
||||
uv8.isolates[core_id]->Enter();
|
||||
uv8.contexts[core_id]->Enter();
|
||||
v8::HandleScope handle_scope;
|
||||
|
||||
v8::Handle<v8::Value> argj[256];
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
v8::Context::Scope context_scope(uv8.contexts[wsgi_req->async_id]);
|
||||
|
||||
|
||||
v8::Persistent<v8::Function> l_func = static_cast<v8::Function*> (func);
|
||||
|
||||
struct uwsgi_v8_rpc_table *uvrt = (struct uwsgi_v8_rpc_table *) func;
|
||||
|
||||
uint8_t i;
|
||||
for(i=0;i<argc;i++) {
|
||||
argj[i] = v8::String::New(argv[i], argvs[i]);
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> result = l_func->Call(l_func, argc, argj);
|
||||
if (result.IsEmpty()) return 0;
|
||||
v8::Persistent<v8::Function> l_func = uvrt->func[core_id];
|
||||
|
||||
v8::Handle<v8::Value> result = l_func->Call(uv8.contexts[core_id]->Global(), argc, argj);
|
||||
if (result.IsEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
v8::Handle<v8::String> robj = result->ToString();
|
||||
|
||||
v8::String::Utf8Value r_value(robj);
|
||||
if (!*robj) return 0;
|
||||
if (!*robj) {
|
||||
return 0;
|
||||
}
|
||||
uint16_t rlen = robj->Length();
|
||||
memcpy(buffer, *r_value, rlen);
|
||||
// call GC every time, could be overkill, we should allow to tune that choice
|
||||
@@ -222,16 +294,39 @@ extern "C" uint16_t uwsgi_v8_rpc(void * func, uint8_t argc, char **argv, uint16_
|
||||
|
||||
}
|
||||
|
||||
extern "C" void uwsgi_v8_init_thread(int core_id) {
|
||||
pthread_setspecific(uv8.current_core, (void *) ((long)core_id));
|
||||
uv8.contexts[core_id] = uwsgi_v8_new_isolate(core_id);
|
||||
uwsgi_v8_apps_do(core_id);
|
||||
}
|
||||
|
||||
extern "C" void uwsgi_v8_enable_threads() {
|
||||
}
|
||||
|
||||
extern "C" int uwsgi_v8_signal_handler(uint8_t sig, void *handler) {
|
||||
int ret = 0;
|
||||
v8::HandleScope handle_scope;
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
v8::Context::Scope context_scope(uv8.contexts[wsgi_req->async_id]);
|
||||
v8::Persistent<v8::Function> l_func = static_cast<v8::Function*> (handler);
|
||||
v8::Handle<v8::Value> argj[1];
|
||||
int core_id = (long) pthread_getspecific(uv8.current_core);
|
||||
|
||||
uv8.isolates[core_id]->Enter();
|
||||
uv8.contexts[core_id]->Enter();
|
||||
v8::HandleScope handle_scope;
|
||||
v8::Handle<v8::Value> argj[1];
|
||||
argj[0] = v8::Number::New(sig);
|
||||
v8::Handle<v8::Value> result = l_func->Call(l_func, 1, argj);
|
||||
struct uwsgi_v8_signal_table *uvst = (struct uwsgi_v8_signal_table *) handler;
|
||||
v8::Persistent<v8::Function> l_func = uvst->func[core_id];
|
||||
v8::Handle<v8::Value> result = l_func->Call(uv8.contexts[core_id]->Global(), 1, argj);
|
||||
if (result.IsEmpty()) ret = -1;
|
||||
while(!v8::V8::IdleNotification()) {};
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern "C" int uwsgi_v8_mule(char *opt) {
|
||||
|
||||
if (uwsgi_endswith(opt, (char *)".js")) {
|
||||
uwsgi_v8_load_file(0, opt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <uwsgi.h>
|
||||
#include <v8.h>
|
||||
|
||||
// as we have isolates in multithread modes, we need to maintain
|
||||
// special tables for the handlers (mules and spooler just run on the core 0)
|
||||
struct uwsgi_v8_signal_table {
|
||||
v8::Persistent<v8::Function> *func;
|
||||
uint8_t registered;
|
||||
};
|
||||
|
||||
struct uwsgi_v8_rpc_table {
|
||||
char *name;
|
||||
v8::Persistent<v8::Function> *func;
|
||||
};
|
||||
|
||||
struct uwsgi_v8 {
|
||||
v8::Persistent<v8::Context> *contexts;
|
||||
v8::Isolate **isolates;
|
||||
char *jsgi;
|
||||
v8::Persistent<v8::Function> *jsgi_func;
|
||||
v8::Persistent<v8::Function> *jsgi_writer_func;
|
||||
int jsgi_announced;
|
||||
struct uwsgi_string_list *load;
|
||||
struct uwsgi_v8_signal_table *sigtable;
|
||||
struct uwsgi_v8_rpc_table *rpctable;
|
||||
pthread_key_t current_core;
|
||||
int preemptive;
|
||||
uint64_t gc_freq;
|
||||
struct uwsgi_string_list *module_paths;
|
||||
};
|
||||
|
||||
v8::Persistent<v8::Function> uwsgi_v8_load_jsgi(int, char *);
|
||||
+1
-1
@@ -2,7 +2,7 @@ Gem::Specification.new do |s|
|
||||
s.name = 'uwsgi'
|
||||
s.license = 'GPL-2'
|
||||
s.version = `python -c "import uwsgiconfig as uc; print uc.uwsgi_version"`.sub(/-dev-.*/,'')
|
||||
s.date = '2013-03-30'
|
||||
s.date = '2013-04-04'
|
||||
s.summary = "uWSGI"
|
||||
s.description = "The uWSGI server for Ruby/Rack"
|
||||
s.authors = ["Unbit"]
|
||||
|
||||
@@ -1863,6 +1863,7 @@ struct uwsgi_server {
|
||||
int threaded_logger;
|
||||
pthread_mutex_t threaded_logger_lock;
|
||||
|
||||
int daemons_honour_stdin;
|
||||
struct uwsgi_daemon *daemons;
|
||||
int daemons_cnt;
|
||||
|
||||
@@ -3947,6 +3948,8 @@ char *uwsgi_get_qs(struct wsgi_request *, char *, uint16_t, uint16_t *);
|
||||
struct uwsgi_route_var *uwsgi_get_route_var(char *, uint16_t);
|
||||
struct uwsgi_route_var *uwsgi_register_route_var(char *, char *(*)(struct wsgi_request *, char *, uint16_t, uint16_t *));
|
||||
|
||||
char *uwsgi_get_mime_type(char *, int, size_t *);
|
||||
|
||||
void uwsgi_check_emperor(void);
|
||||
#ifdef UWSGI_AS_SHARED_LIBRARY
|
||||
int uwsgi_init(int, char **, char **);
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
# uWSGI build system
|
||||
|
||||
uwsgi_version = '1.9.4'
|
||||
uwsgi_version = '1.9.5'
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
Reference in New Issue
Block a user