diff --git a/plugins/cplusplus/base.cc b/plugins/cplusplus/base.cc new file mode 100644 index 00000000..21fc6bb3 --- /dev/null +++ b/plugins/cplusplus/base.cc @@ -0,0 +1,63 @@ +#include "../../uwsgi.h" + +extern struct uwsgi_server uwsgi; + +class FakeClass { + + public: + char *foobar; + uint16_t foobar_len; + void hello_world(struct wsgi_request *); + +}; + +void FakeClass::hello_world(struct wsgi_request *wsgi_req) { + + wsgi_req->response_size += wsgi_req->socket->proto_write(wsgi_req, (char *) "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n", 44); + wsgi_req->response_size += wsgi_req->socket->proto_write(wsgi_req, foobar, foobar_len); +} + +extern "C" int uwsgi_cplusplus_init(){ + uwsgi_log("Initializing example c++ plugin\n"); + return 0; +} + +extern "C" int uwsgi_cplusplus_request(struct wsgi_request *wsgi_req) { + + FakeClass *fc; + + // empty request ? + if (!wsgi_req->uh.pktsize) { + uwsgi_log( "Invalid request. skip.\n"); + goto clear; + } + + // get uwsgi variables + if (uwsgi_parse_vars(wsgi_req)) { + uwsgi_log("Invalid request. skip.\n"); + goto clear; + } + + fc = new FakeClass(); + // get PATH_INFO + fc->foobar = uwsgi_get_var(wsgi_req, (char *) "PATH_INFO", &fc->foobar_len); + + if (fc->foobar) { + // send output + fc->hello_world(wsgi_req); + } + + delete fc; + +clear: + return UWSGI_OK; +} + + +extern "C" void uwsgi_cplusplus_after_request(struct wsgi_request *wsgi_req) { + uwsgi_log("logging c++ request\n"); +} + + + + diff --git a/plugins/cplusplus/plugin.c b/plugins/cplusplus/plugin.c new file mode 100644 index 00000000..b80c835c --- /dev/null +++ b/plugins/cplusplus/plugin.c @@ -0,0 +1,17 @@ +#include "../../uwsgi.h" + + +int uwsgi_cplusplus_init(void); +int uwsgi_cplusplus_request(struct wsgi_request *); +void uwsgi_cplusplus_after_request(struct wsgi_request *); + +struct uwsgi_plugin cplusplus_plugin = { + + .name = "cplusplus", + .modifier1 = 250, + .init = uwsgi_cplusplus_init, + .request = uwsgi_cplusplus_request, + .after_request = uwsgi_cplusplus_after_request, + +}; + diff --git a/plugins/cplusplus/uwsgiplugin.py b/plugins/cplusplus/uwsgiplugin.py new file mode 100644 index 00000000..07548c63 --- /dev/null +++ b/plugins/cplusplus/uwsgiplugin.py @@ -0,0 +1,6 @@ +NAME='cplusplus' + +CFLAGS = [] +LDFLAGS = [] +LIBS = ['-lstdc++'] +GCC_LIST = ['base.cc', 'plugin'] diff --git a/plugins/example/example_plugin.c b/plugins/example/example_plugin.c index 728afd0d..37378e3b 100644 --- a/plugins/example/example_plugin.c +++ b/plugins/example/example_plugin.c @@ -2,21 +2,33 @@ extern struct uwsgi_server uwsgi; -int uwsgi_init(char *args){ - uwsgi_log("i am the example plugin initialization function with arg: %s\n", args); +int uwsgi_example_init(){ + uwsgi_log("i am the example plugin initialization function\n"); return 0; } -int uwsgi_request(struct wsgi_request *wsgi_req) { +int uwsgi_example_request(struct wsgi_request *wsgi_req) { char *http = "HTTP/1.1 200 Ok\r\nContent-type: text/html\r\n\r\n

Hello World

"; - wsgi_req->response_size += wsgi_req->socket_proto_write(wsgi_req, http, strlen(http)); + wsgi_req->response_size += wsgi_req->socket->proto_write(wsgi_req, http, strlen(http)); return 0; } -void uwsgi_after_request(struct wsgi_request *wsgi_req) { +void uwsgi_example_after_request(struct wsgi_request *wsgi_req) { uwsgi_log("i am the example plugin after request function\n"); } + + +struct uwsgi_plugin example_plugin = { + + .name = "example", + .modifier1 = 250, + .init = uwsgi_example_init, + .request = uwsgi_example_request, + .after_request = uwsgi_example_after_request, + +}; + diff --git a/plugins/example/uwsgiplugin.py b/plugins/example/uwsgiplugin.py index 6884186d..4074a00b 100644 --- a/plugins/example/uwsgiplugin.py +++ b/plugins/example/uwsgiplugin.py @@ -1,3 +1,6 @@ NAME='example' -CFLAGS = '' -LDFLAGS = '' + +CFLAGS = [] +LDFLAGS = [] +LIBS = [] +GCC_LIST = ['example_plugin'] diff --git a/protocol.c b/protocol.c index 1336db05..f9b3def0 100644 --- a/protocol.c +++ b/protocol.c @@ -1337,3 +1337,17 @@ int uwsgi_file_serve(struct wsgi_request *wsgi_req, char *document_root, uint16_ } +char *uwsgi_get_var(struct wsgi_request *wsgi_req, char *key, uint16_t *len) { + + int i; + *len = 0; + for (i = 0; i < wsgi_req->var_cnt; i += 2) { + if (!uwsgi_strncmp(key, strlen(key), wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len)) { + *len = wsgi_req->hvec[i + 1].iov_len; + return wsgi_req->hvec[i + 1].iov_base; + } + } + + return NULL; + +} diff --git a/uwsgi.h b/uwsgi.h index e4765150..00a4b2fb 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -2,6 +2,10 @@ /* indent -i8 -br -brs -brf -l0 -npsl -nip -npcs -npsl -di1 */ +#ifdef __cplusplus +extern "C" { +#endif + #define UMAX16 65536 #define uwsgi_error(x) uwsgi_log("%s: %s [%s line %d]\n", x, strerror(errno), __FILE__, __LINE__); @@ -1464,6 +1468,7 @@ struct uwsgi_worker { }; + char *uwsgi_get_cwd(void); void warn_pipe(void); @@ -2063,3 +2068,9 @@ int uwsgi_file_exists(char *); int uwsgi_signal_registered(uint8_t); int uwsgi_endswith(char *, char *); + +char *uwsgi_get_var(struct wsgi_request *, char *, uint16_t *); + +#ifdef __cplusplus +} +#endif diff --git a/uwsgiconfig.py b/uwsgiconfig.py index 72f8a892..b4512334 100644 --- a/uwsgiconfig.py +++ b/uwsgiconfig.py @@ -627,7 +627,10 @@ def build_plugin(path, uc, cflags, ldflags, libs, name = None): shared_flag = '-dynamiclib -undefined dynamic_lookup' for cfile in up.GCC_LIST: - gcc_list.append(path + '/' + cfile + '.c') + if not cfile.endswith('.c') and not cfile.endswith('.cc'): + gcc_list.append(path + '/' + cfile + '.c') + else: + gcc_list.append(path + '/' + cfile) gccline = "%s -fPIC %s -o %s.so %s %s %s %s" % (GCC, shared_flag, plugin_dest, ' '.join(p_cflags), ' '.join(p_ldflags), ' '.join(gcc_list), ' '.join(p_libs) )