@@ -89,30 +89,21 @@ macro_rules! http_variable_get {
89
89
/// requests.
90
90
///
91
91
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
92
- #[ repr( transparent) ]
93
- pub struct Request ( ngx_http_request_t ) ;
92
+ pub struct Request ( foreign_types:: Opaque ) ;
94
93
95
- impl < ' a > From < & ' a Request > for * const ngx_http_request_t {
96
- fn from ( request : & ' a Request ) -> Self {
97
- & request. 0 as * const _
98
- }
99
- }
100
-
101
- impl < ' a > From < & ' a mut Request > for * mut ngx_http_request_t {
102
- fn from ( request : & ' a mut Request ) -> Self {
103
- & request. 0 as * const _ as * mut _
104
- }
94
+ unsafe impl foreign_types:: ForeignTypeRef for Request {
95
+ type CType = ngx_http_request_t ;
105
96
}
106
97
107
98
impl AsRef < ngx_http_request_t > for Request {
108
99
fn as_ref ( & self ) -> & ngx_http_request_t {
109
- & self . 0
100
+ unsafe { & * self . as_ptr ( ) }
110
101
}
111
102
}
112
103
113
104
impl AsMut < ngx_http_request_t > for Request {
114
105
fn as_mut ( & mut self ) -> & mut ngx_http_request_t {
115
- & mut self . 0
106
+ unsafe { & mut * self . as_ptr ( ) }
116
107
}
117
108
}
118
109
@@ -123,20 +114,21 @@ impl Request {
123
114
///
124
115
/// The caller has provided a valid non-null pointer to a valid `ngx_http_request_t`
125
116
/// which shares the same representation as `Request`.
117
+ #[ inline]
126
118
pub unsafe fn from_ngx_http_request < ' a > ( r : * mut ngx_http_request_t ) -> & ' a mut Request {
127
- & mut * r . cast :: < Request > ( )
119
+ ForeignTypeRef :: from_ptr_mut ( r )
128
120
}
129
121
130
122
/// Is this the main request (as opposed to a subrequest)?
131
123
pub fn is_main ( & self ) -> bool {
132
- let main = self . 0 . main . cast ( ) ;
124
+ let main = self . as_ref ( ) . main . cast ( ) ;
133
125
core:: ptr:: eq ( self , main)
134
126
}
135
127
136
128
/// Request pool.
137
129
pub fn pool ( & self ) -> Pool {
138
130
// SAFETY: This request is allocated from `pool`, thus must be a valid pool.
139
- unsafe { Pool :: from_ngx_pool ( self . 0 . pool ) }
131
+ unsafe { Pool :: from_ngx_pool ( self . as_ref ( ) . pool ) }
140
132
}
141
133
142
134
/// Returns the result as an `Option` if it exists, otherwise `None`.
@@ -147,17 +139,17 @@ impl Request {
147
139
/// [`ngx_http_upstream_t`] is best described in
148
140
/// <https://nginx.org/en/docs/dev/development_guide.html#http_load_balancing>
149
141
pub fn upstream ( & self ) -> Option < * mut ngx_http_upstream_t > {
150
- if self . 0 . upstream . is_null ( ) {
142
+ if self . as_ref ( ) . upstream . is_null ( ) {
151
143
return None ;
152
144
}
153
- Some ( self . 0 . upstream )
145
+ Some ( self . as_ref ( ) . upstream )
154
146
}
155
147
156
148
/// Pointer to a [`ngx_connection_t`] client connection object.
157
149
///
158
150
/// [`ngx_connection_t`]: https://nginx.org/en/docs/dev/development_guide.html#connection
159
151
pub fn connection ( & self ) -> * mut ngx_connection_t {
160
- self . 0 . connection
152
+ self . as_ref ( ) . connection
161
153
}
162
154
163
155
/// Pointer to a [`ngx_log_t`].
@@ -169,7 +161,7 @@ impl Request {
169
161
170
162
/// Get Module context pointer
171
163
fn get_module_ctx_ptr ( & self , module : & ngx_module_t ) -> * mut c_void {
172
- unsafe { * self . 0 . ctx . add ( module. ctx_index ) }
164
+ unsafe { * self . as_ref ( ) . ctx . add ( module. ctx_index ) }
173
165
}
174
166
175
167
/// Get Module context
@@ -183,9 +175,9 @@ impl Request {
183
175
/// Sets the value as the module's context.
184
176
///
185
177
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
186
- pub fn set_module_ctx ( & self , value : * mut c_void , module : & ngx_module_t ) {
178
+ pub fn set_module_ctx ( & mut self , value : * mut c_void , module : & ngx_module_t ) {
187
179
unsafe {
188
- * self . 0 . ctx . add ( module. ctx_index ) = value;
180
+ * self . as_mut ( ) . ctx . add ( module. ctx_index ) = value;
189
181
} ;
190
182
}
191
183
@@ -210,77 +202,81 @@ impl Request {
210
202
///
211
203
/// [request body]: https://nginx.org/en/docs/dev/development_guide.html#http_request_body
212
204
pub fn discard_request_body ( & mut self ) -> Status {
213
- unsafe { Status ( ngx_http_discard_request_body ( & mut self . 0 ) ) }
205
+ unsafe { Status ( ngx_http_discard_request_body ( self . as_ptr ( ) ) ) }
214
206
}
215
207
216
208
/// Client HTTP [User-Agent].
217
209
///
218
210
/// [User-Agent]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
219
211
pub fn user_agent ( & self ) -> Option < & NgxStr > {
220
- if !self . 0 . headers_in . user_agent . is_null ( ) {
221
- unsafe { Some ( NgxStr :: from_ngx_str ( ( * self . 0 . headers_in . user_agent ) . value ) ) }
212
+ if !self . as_ref ( ) . headers_in . user_agent . is_null ( ) {
213
+ unsafe {
214
+ Some ( NgxStr :: from_ngx_str (
215
+ ( * self . as_ref ( ) . headers_in . user_agent ) . value ,
216
+ ) )
217
+ }
222
218
} else {
223
219
None
224
220
}
225
221
}
226
222
227
223
/// Set HTTP status of response.
228
224
pub fn set_status ( & mut self , status : HTTPStatus ) {
229
- self . 0 . headers_out . status = status. into ( ) ;
225
+ self . as_mut ( ) . headers_out . status = status. into ( ) ;
230
226
}
231
227
232
228
/// Add header to the `headers_in` object.
233
229
///
234
230
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
235
231
pub fn add_header_in ( & mut self , key : & str , value : & str ) -> Option < ( ) > {
236
232
let table: * mut ngx_table_elt_t =
237
- unsafe { ngx_list_push ( & mut self . 0 . headers_in . headers ) as _ } ;
238
- unsafe { add_to_ngx_table ( table, self . 0 . pool , key, value) }
233
+ unsafe { ngx_list_push ( & mut self . as_mut ( ) . headers_in . headers ) as _ } ;
234
+ unsafe { add_to_ngx_table ( table, self . as_ref ( ) . pool , key, value) }
239
235
}
240
236
241
237
/// Add header to the `headers_out` object.
242
238
///
243
239
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
244
240
pub fn add_header_out ( & mut self , key : & str , value : & str ) -> Option < ( ) > {
245
241
let table: * mut ngx_table_elt_t =
246
- unsafe { ngx_list_push ( & mut self . 0 . headers_out . headers ) as _ } ;
247
- unsafe { add_to_ngx_table ( table, self . 0 . pool , key, value) }
242
+ unsafe { ngx_list_push ( & mut self . as_mut ( ) . headers_out . headers ) as _ } ;
243
+ unsafe { add_to_ngx_table ( table, self . as_ref ( ) . pool , key, value) }
248
244
}
249
245
250
246
/// Set response body [Content-Length].
251
247
///
252
248
/// [Content-Length]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
253
249
pub fn set_content_length_n ( & mut self , n : usize ) {
254
- self . 0 . headers_out . content_length_n = n as off_t ;
250
+ self . as_mut ( ) . headers_out . content_length_n = n as off_t ;
255
251
}
256
252
257
253
/// Send the output header.
258
254
///
259
255
/// Do not call this function until all output headers are set.
260
256
pub fn send_header ( & mut self ) -> Status {
261
- unsafe { Status ( ngx_http_send_header ( & mut self . 0 ) ) }
257
+ unsafe { Status ( ngx_http_send_header ( self . as_ptr ( ) ) ) }
262
258
}
263
259
264
260
/// Flag indicating that the output does not require a body.
265
261
///
266
262
/// For example, this flag is used by `HTTP HEAD` requests.
267
263
pub fn header_only ( & self ) -> bool {
268
- self . 0 . header_only ( ) != 0
264
+ self . as_ref ( ) . header_only ( ) != 0
269
265
}
270
266
271
267
/// request method
272
268
pub fn method ( & self ) -> Method {
273
- Method :: from_ngx ( self . 0 . method )
269
+ Method :: from_ngx ( self . as_ref ( ) . method )
274
270
}
275
271
276
272
/// path part of request only
277
273
pub fn path ( & self ) -> & NgxStr {
278
- unsafe { NgxStr :: from_ngx_str ( self . 0 . uri ) }
274
+ unsafe { NgxStr :: from_ngx_str ( self . as_ref ( ) . uri ) }
279
275
}
280
276
281
277
/// full uri - containing path and args
282
278
pub fn unparsed_uri ( & self ) -> & NgxStr {
283
- unsafe { NgxStr :: from_ngx_str ( self . 0 . unparsed_uri ) }
279
+ unsafe { NgxStr :: from_ngx_str ( self . as_ref ( ) . unparsed_uri ) }
284
280
}
285
281
286
282
/// Send the [response body].
@@ -290,13 +286,13 @@ impl Request {
290
286
///
291
287
/// [response body]: https://nginx.org/en/docs/dev/development_guide.html#http_request_body
292
288
pub fn output_filter ( & mut self , body : & mut ngx_chain_t ) -> Status {
293
- unsafe { Status ( ngx_http_output_filter ( & mut self . 0 , body) ) }
289
+ unsafe { Status ( ngx_http_output_filter ( self . as_ptr ( ) , body) ) }
294
290
}
295
291
296
292
/// Perform internal redirect to a location
297
293
pub fn internal_redirect ( & self , location : & str ) -> Status {
298
294
assert ! ( !location. is_empty( ) , "uri location is empty" ) ;
299
- let uri_ptr = unsafe { & mut ngx_str_t:: from_str ( self . 0 . pool , location) as * mut _ } ;
295
+ let uri_ptr = unsafe { & mut ngx_str_t:: from_str ( self . as_ref ( ) . pool , location) as * mut _ } ;
300
296
301
297
// FIXME: check status of ngx_http_named_location or ngx_http_internal_redirect
302
298
if location. starts_with ( '@' ) {
@@ -326,7 +322,7 @@ impl Request {
326
322
ngx_int_t ,
327
323
) -> ngx_int_t ,
328
324
) -> Status {
329
- let uri_ptr = unsafe { & mut ngx_str_t:: from_str ( self . 0 . pool , uri) as * mut _ } ;
325
+ let uri_ptr = unsafe { & mut ngx_str_t:: from_str ( self . as_ref ( ) . pool , uri) as * mut _ } ;
330
326
// -------------
331
327
// allocate memory and set values for ngx_http_post_subrequest_t
332
328
let sub_ptr = self
@@ -377,13 +373,13 @@ impl Request {
377
373
/// Iterate over headers_in
378
374
/// each header item is (&str, &str) (borrowed)
379
375
pub fn headers_in_iterator ( & self ) -> NgxListIterator < ' _ > {
380
- unsafe { list_iterator ( & self . 0 . headers_in . headers ) }
376
+ unsafe { list_iterator ( & self . as_ref ( ) . headers_in . headers ) }
381
377
}
382
378
383
379
/// Iterate over headers_out
384
380
/// each header item is (&str, &str) (borrowed)
385
381
pub fn headers_out_iterator ( & self ) -> NgxListIterator < ' _ > {
386
- unsafe { list_iterator ( & self . 0 . headers_out . headers ) }
382
+ unsafe { list_iterator ( & self . as_ref ( ) . headers_out . headers ) }
387
383
}
388
384
}
389
385
@@ -392,21 +388,21 @@ impl crate::http::HttpModuleConfExt for Request {
392
388
unsafe fn http_main_conf_unchecked < T > ( & self , module : & ngx_module_t ) -> Option < NonNull < T > > {
393
389
// SAFETY: main_conf[module.ctx_index] is either NULL or allocated with ngx_p(c)alloc and
394
390
// explicitly initialized by the module
395
- NonNull :: new ( ( * self . 0 . main_conf . add ( module. ctx_index ) ) . cast ( ) )
391
+ NonNull :: new ( ( * self . as_ref ( ) . main_conf . add ( module. ctx_index ) ) . cast ( ) )
396
392
}
397
393
398
394
#[ inline]
399
395
unsafe fn http_server_conf_unchecked < T > ( & self , module : & ngx_module_t ) -> Option < NonNull < T > > {
400
396
// SAFETY: srv_conf[module.ctx_index] is either NULL or allocated with ngx_p(c)alloc and
401
397
// explicitly initialized by the module
402
- NonNull :: new ( ( * self . 0 . srv_conf . add ( module. ctx_index ) ) . cast ( ) )
398
+ NonNull :: new ( ( * self . as_ref ( ) . srv_conf . add ( module. ctx_index ) ) . cast ( ) )
403
399
}
404
400
405
401
#[ inline]
406
402
unsafe fn http_location_conf_unchecked < T > ( & self , module : & ngx_module_t ) -> Option < NonNull < T > > {
407
403
// SAFETY: loc_conf[module.ctx_index] is either NULL or allocated with ngx_p(c)alloc and
408
404
// explicitly initialized by the module
409
- NonNull :: new ( ( * self . 0 . loc_conf . add ( module. ctx_index ) ) . cast ( ) )
405
+ NonNull :: new ( ( * self . as_ref ( ) . loc_conf . add ( module. ctx_index ) ) . cast ( ) )
410
406
}
411
407
}
412
408
@@ -417,7 +413,7 @@ impl crate::http::HttpModuleConfExt for Request {
417
413
impl fmt:: Debug for Request {
418
414
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
419
415
f. debug_struct ( "Request" )
420
- . field ( "request_" , & self . 0 )
416
+ . field ( "request_" , & self . as_ref ( ) )
421
417
. finish ( )
422
418
}
423
419
}
0 commit comments