Skip to content

Commit 77ef3ab

Browse files
committed
Added HeadersIn
1 parent 3f4bc90 commit 77ef3ab

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

nginx_module/src/http_request.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ use crate::{
1818
bindings::{
1919
ngx_alloc_chain_link, ngx_buf_t, ngx_chain_s, ngx_hash_find, ngx_hash_key,
2020
ngx_http_core_main_conf_t, ngx_http_core_module, ngx_http_core_run_phases,
21-
ngx_http_get_indexed_variable, ngx_http_headers_out_t, ngx_http_output_filter,
22-
ngx_http_parse_multi_header_lines, ngx_http_request_t, ngx_http_send_header,
23-
ngx_http_variable_t, ngx_list_push, ngx_list_t, ngx_module_t, ngx_pcalloc,
24-
ngx_posted_events, ngx_table_elt_t,
21+
ngx_http_get_indexed_variable, ngx_http_headers_in_t, ngx_http_headers_out_t,
22+
ngx_http_output_filter, ngx_http_parse_multi_header_lines, ngx_http_request_t,
23+
ngx_http_send_header, ngx_http_variable_t, ngx_list_part_t, ngx_list_push, ngx_list_t,
24+
ngx_module_t, ngx_pcalloc, ngx_posted_events, ngx_table_elt_t,
2525
},
2626
connection::Connection,
2727
ngx_post_event, ngx_str_t,
@@ -39,6 +39,13 @@ pub struct HttpRequestAndContext<'a, Ctx>(ngx_http_request_t, PhantomData<&'a Ct
3939

4040
pub struct HttpRequest<'a>(ngx_http_request_t, PhantomData<&'a ()>);
4141

42+
pub struct HeadersIn<'a>(ngx_http_headers_in_t, PhantomData<&'a ()>);
43+
pub struct HeadersInIter<'a> {
44+
part: *const ngx_list_part_t,
45+
elt_idx: usize,
46+
_phantom: PhantomData<&'a ()>,
47+
}
48+
4249
impl<'a, Ctx: Default> HttpRequestAndContext<'a, Ctx> {
4350
///
4451
/// # Safety
@@ -250,6 +257,10 @@ impl<'a> HttpRequest<'a> {
250257
&mut self.0.headers_out
251258
}
252259

260+
pub fn headers_in(&self) -> &HeadersIn<'a> {
261+
unsafe { &*(&self.0.headers_in as *const ngx_http_headers_in_t as *const HeadersIn) }
262+
}
263+
253264
// TODO:esavier its not named uri but it returns only path
254265
// TODO:consider either helper or change results to Strings
255266
pub fn unparsed_uri(&self) -> NgxStr {
@@ -511,3 +522,44 @@ impl<'a> HttpRequest<'a> {
511522
(&self.0 as *const ngx_http_request_t).cast_mut()
512523
}
513524
}
525+
526+
impl<'a> HeadersIn<'a> {
527+
pub fn iter(&self) -> HeadersInIter<'a> {
528+
HeadersInIter {
529+
part: &self.0.headers.part,
530+
elt_idx: 0,
531+
_phantom: PhantomData,
532+
}
533+
}
534+
}
535+
536+
impl<'a> Iterator for HeadersInIter<'a> {
537+
type Item = (NgxStr<'a>, NgxStr<'a>);
538+
539+
fn next(&mut self) -> Option<Self::Item> {
540+
if self.part.is_null() {
541+
None
542+
} else {
543+
unsafe {
544+
let len = (*self.part).nelts;
545+
if self.elt_idx >= len {
546+
return None;
547+
}
548+
let elem_base = (*self.part).elts as *const ngx_table_elt_t;
549+
let elem = elem_base.add(self.elt_idx);
550+
let result = (
551+
NgxStr::from_raw((*elem).key),
552+
NgxStr::from_raw((*elem).value),
553+
);
554+
555+
self.elt_idx += 1;
556+
if self.elt_idx >= len {
557+
self.elt_idx = 0;
558+
self.part = (*self.part).next;
559+
}
560+
561+
Some(result)
562+
}
563+
}
564+
}
565+
}

0 commit comments

Comments
 (0)