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+ )
11+ from .ngx_http cimport (
12+ ngx_http_request_t,
13+ ngx_http_core_run_phases,
14+ ngx_http_get_module_ctx,
15+ ngx_http_set_ctx,
16+ ngx_http_send_header,
17+ ngx_list_push,
18+ ngx_table_elt_t,
19+ ngx_str_set,
20+ ngx_http_output_filter,
21+ ngx_chain_t,
22+ ngx_buf_t,
23+ ngx_calloc_buf,
24+ )
525
626import traceback
727
@@ -17,6 +37,8 @@ cdef class Request:
1737 public str extension
1838 public str unparsed_uri
1939 public str method_name
40+ public str content_type
41+ public str content_length
2042 public str http_protocol
2143
2244 def __init__ (self , *args ):
@@ -37,17 +59,82 @@ cdef class Request:
3759 return self .future.result()
3860 return NGX_AGAIN
3961
62+ def send_header (self ):
63+ return ngx_http_send_header(self .request)
64+
65+ def add_response_header (self , key , value ):
66+ cdef:
67+ ngx_table_elt_t * h
68+ char * cstr
69+ char * csource
70+ bytes key_data, value_data
71+ h = ngx_list_push(& self .request.headers_out.headers)
72+ if h == NULL :
73+ raise MemoryError ()
74+ h.hash = 1
75+
76+ key_data = str (key).encode(' iso8859-1' )
77+ cstr = < char * > ngx_calloc(sizeof(char ) * len (key_data), self .request.connection.log)
78+ h.key.len = len (key_data)
79+ csource = key_data
80+ ngx_memcpy(cstr, csource, len (key_data))
81+ h.key.data = cstr
82+
83+ value_data = str (value).encode(' iso8859-1' )
84+ cstr = < char * > ngx_calloc(sizeof(char ) * len (value_data), self .request.connection.log)
85+ h.value.len = len (value_data)
86+ csource = value_data
87+ ngx_memcpy(cstr, csource, len (value_data))
88+ h.value.data = cstr
89+
90+ def send_response (self , pos ):
91+ cdef:
92+ ngx_chain_t out
93+ ngx_buf_t * b
94+ bytes data = pos
95+ char * cstr = data
96+ b = ngx_calloc_buf(self .request.pool)
97+ if b == NULL :
98+ raise MemoryError
99+ b.last_buf = 1
100+ b.last_in_chain = 1
101+ b.memory = 1
102+ b.pos = cstr
103+ b.last = b.pos + len (data)
104+
105+ out.buf = b
106+ out.next = NULL
107+
108+ return ngx_http_output_filter(self .request, & out)
109+
110+ property response_status :
111+ def __get__ (self ):
112+ return self .request.headers_out.status
113+
114+ def __set__ (self , value ):
115+ self .request.headers_out.status = value
116+
117+ property response_content_length :
118+ def __get__ (self ):
119+ if self .request.headers_out.content_length:
120+ return self .request.headers_out.content_length.value
121+
122+ def __set__ (self , value ):
123+ self .request.headers_out.content_length.value = value
124+
40125 def __repr__ (self ):
41126 return f' Request({self.method_name} {self.uri})'
42127
43128 def __str__ (self ):
44129 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}'''
130+ uri: {self.uri}
131+ args: {self.args}
132+ extension: {self.extension}
133+ unparsed_uri: {self.unparsed_uri}
134+ method_name: {self.method_name}
135+ content_type: {self.content_type}
136+ content_length: {self.content_length}
137+ http_protocol: {self.http_protocol}'''
51138
52139 @staticmethod
53140 cdef Request from_ptr(ngx_http_request_t * request):
@@ -67,6 +154,12 @@ http_protocol: {self.http_protocol}'''
67154 new_req.unparsed_uri = from_nginx_str(request.unparsed_uri)
68155 new_req.method_name = from_nginx_str(request.method_name)
69156 new_req.http_protocol = from_nginx_str(request.http_protocol)
157+ if request.headers_in.content_type:
158+ new_req.content_type = from_nginx_str(
159+ request.headers_in.content_type.value)
160+ if request.headers_in.content_length:
161+ new_req.content_length = from_nginx_str(
162+ request.headers_in.content_length.value)
70163
71164 ngx_http_set_ctx(request, < void * > new_req, ngx_python_module)
72165 return new_req
0 commit comments