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