-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathngx_python_module.c
147 lines (118 loc) · 4.53 KB
/
ngx_python_module.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <Python.h>
#include "nginx.h"
static ngx_int_t ngx_python_init_process(ngx_cycle_t *cycle);
static void ngx_python_exit_process(ngx_cycle_t *cycle);
static ngx_int_t ngx_python_postconfiguration(ngx_conf_t *cf);
static wchar_t *python_exec = NULL;
static void *ngx_http_wsgi_create_loc_conf(ngx_conf_t *cf);
static char *ngx_handle_app(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_conf_post_t ngx_wsgi_pass_post = { ngx_handle_app };
typedef struct {
ngx_str_t wsgi_pass;
} ngx_wsgi_pass_conf_t;
static ngx_command_t ngx_wsgi_commands[] = {
{ ngx_string("wsgi_pass"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_wsgi_pass_conf_t, wsgi_pass),
&ngx_wsgi_pass_post,
},
ngx_null_command
};
static ngx_http_module_t ngx_python_module_ctx = {
NULL, /* preconfiguration */
ngx_python_postconfiguration, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_wsgi_create_loc_conf, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_python_module = {
NGX_MODULE_V1,
&ngx_python_module_ctx, /* module context */
ngx_wsgi_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_python_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_python_exit_process, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_python_init_process(ngx_cycle_t *cycle) {
if (python_exec == NULL) {
python_exec = Py_DecodeLocale(PYTHON_EXEC, NULL);
if (python_exec == NULL) {
ngx_log_error(NGX_LOG_CRIT, cycle->log, 0,
"Could not decode Python executable path.");
return NGX_ERROR;
}
}
Py_SetProgramName(python_exec);
if (PyImport_AppendInittab("nginx._nginx", PyInit__nginx) == -1) {
ngx_log_error(NGX_LOG_CRIT, cycle->log, 0,
"Could not initialize nginxpy extension.");
return NGX_ERROR;
}
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
"Initializing Python...");
Py_Initialize();
if (PyImport_ImportModule("nginx._nginx") == NULL) {
ngx_log_error(NGX_LOG_CRIT, cycle->log, 0,
"Could not import nginxpy extension.");
return NGX_ERROR;
}
return nginxpy_init_process(cycle);
}
static void
ngx_python_exit_process(ngx_cycle_t *cycle) {
nginxpy_exit_process(cycle);
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
"Finalizing Python...");
if (Py_FinalizeEx() < 0) {
ngx_log_error(NGX_LOG_CRIT, cycle->log, 0,
"Failed to finalize Python!");
}
}
static ngx_int_t
ngx_python_postconfiguration(ngx_conf_t *cf) {
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = nginxpy_post_read;
return NGX_OK;
}
static void *
ngx_http_wsgi_create_loc_conf(ngx_conf_t *cf)
{
ngx_wsgi_pass_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_wsgi_pass_conf_t));
if (conf == NULL) {
return NULL;
}
return conf;
}
static char *
ngx_handle_app(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_wsgi_pass_conf_t *wac = conf;
if (wac->wsgi_pass.len > 0) {
ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "get application %s", wac->wsgi_pass.data);
} else {
ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "no wsgi_pass found");
}
return NGX_CONF_OK;
}