11from cpython cimport Py_INCREF, Py_DECREF
2- from .nginx_core cimport ngx_log_error, NGX_LOG_CRIT, NGX_AGAIN, from_nginx_str
3- from .ngx_http cimport ngx_http_request_t, ngx_http_core_run_phases
4- from .ngx_http cimport ngx_http_get_module_ctx, ngx_http_set_ctx
2+ from .nginx_core cimport (
3+ ngx_log_error,
4+ NGX_LOG_CRIT,
5+ NGX_AGAIN,
6+ from_nginx_str,
7+ ngx_calloc,
8+ ngx_free,
9+ ngx_memcpy,
10+ ngx_module_t,
11+ ngx_str_t,
12+ )
13+ from .ngx_http cimport (
14+ ngx_http_request_t,
15+ ngx_http_core_run_phases,
16+ ngx_http_get_module_ctx,
17+ ngx_http_set_ctx,
18+ ngx_http_send_header,
19+ ngx_list_push,
20+ ngx_table_elt_t,
21+ ngx_str_set,
22+ ngx_http_output_filter,
23+ ngx_chain_t,
24+ ngx_buf_t,
25+ ngx_calloc_buf,
26+ )
527
628import traceback
729
@@ -17,6 +39,8 @@ cdef class Request:
1739 public str extension
1840 public str unparsed_uri
1941 public str method_name
42+ public str content_type
43+ public str content_length
2044 public str http_protocol
2145
2246 def __init__ (self , *args ):
@@ -37,17 +61,88 @@ cdef class Request:
3761 return self .future.result()
3862 return NGX_AGAIN
3963
64+ def send_header (self ):
65+ return ngx_http_send_header(self .request)
66+
67+ def add_response_header (self , key , value ):
68+ cdef:
69+ ngx_table_elt_t * h
70+ char * cstr
71+ char * csource
72+ bytes key_data, value_data
73+ h = ngx_list_push(& self .request.headers_out.headers)
74+ if h == NULL :
75+ raise MemoryError ()
76+ h.hash = 1
77+
78+ key_data = str (key).encode(' iso8859-1' )
79+ cstr = < char * > ngx_calloc(sizeof(char ) * len (key_data), self .request.connection.log)
80+ h.key.len = len (key_data)
81+ csource = key_data
82+ ngx_memcpy(cstr, csource, len (key_data))
83+ h.key.data = cstr
84+
85+ value_data = str (value).encode(' iso8859-1' )
86+ cstr = < char * > ngx_calloc(sizeof(char ) * len (value_data), self .request.connection.log)
87+ h.value.len = len (value_data)
88+ csource = value_data
89+ ngx_memcpy(cstr, csource, len (value_data))
90+ h.value.data = cstr
91+
92+ def send_response (self , pos ):
93+ cdef:
94+ ngx_chain_t out
95+ ngx_buf_t * b
96+ bytes data = pos
97+ char * cstr = data
98+ b = ngx_calloc_buf(self .request.pool)
99+ if b == NULL :
100+ raise MemoryError
101+ b.last_buf = 1
102+ b.last_in_chain = 1
103+ b.memory = 1
104+ b.pos = cstr
105+ b.last = b.pos + len (data)
106+
107+ out.buf = b
108+ out.next = NULL
109+
110+ return ngx_http_output_filter(self .request, & out)
111+
112+ def get_app_from_config (self ):
113+ cdef ngx_wsgi_pass_conf_t * conf
114+
115+ conf = < ngx_wsgi_pass_conf_t * > self .request.loc_conf[ngx_python_module.ctx_index]
116+ return from_nginx_str(conf.wsgi_pass)
117+
118+ property response_status :
119+ def __get__ (self ):
120+ return self .request.headers_out.status
121+
122+ def __set__ (self , value ):
123+ self .request.headers_out.status = value
124+
125+ property response_content_length :
126+ def __get__ (self ):
127+ if self .request.headers_out.content_length:
128+ return self .request.headers_out.content_length.value
129+
130+ def __set__ (self , value ):
131+ self .request.headers_out.content_length.value = value
132+
40133 def __repr__ (self ):
41134 return f' Request({self.method_name} {self.uri})'
42135
43136 def __str__ (self ):
44137 return f''' request_line: {self.request_line}
45- uri: {self.uri}
46- args: {self.args}
47- extension: {self.extension}
48- unparsed_uri: {self.unparsed_uri}
49- method_name: {self.method_name}
50- http_protocol: {self.http_protocol}'''
138+ uri: {self.uri}
139+ args: {self.args}
140+ extension: {self.extension}
141+ unparsed_uri: {self.unparsed_uri}
142+ method_name: {self.method_name}
143+ content_type: {self.content_type}
144+ content_length: {self.content_length}
145+ http_protocol: {self.http_protocol}'''
51146
52147 @staticmethod
53148 cdef Request from_ptr(ngx_http_request_t * request):
@@ -67,6 +162,12 @@ http_protocol: {self.http_protocol}'''
67162 new_req.unparsed_uri = from_nginx_str(request.unparsed_uri)
68163 new_req.method_name = from_nginx_str(request.method_name)
69164 new_req.http_protocol = from_nginx_str(request.http_protocol)
165+ if request.headers_in.content_type:
166+ new_req.content_type = from_nginx_str(
167+ request.headers_in.content_type.value)
168+ if request.headers_in.content_length:
169+ new_req.content_length = from_nginx_str(
170+ request.headers_in.content_length.value)
70171
71172 ngx_http_set_ctx(request, < void * > new_req, ngx_python_module)
72173 return new_req
0 commit comments