Skip to content

Commit 76bee97

Browse files
authored
Merge pull request #13 from sahandevs/add-more-helpers
add headers_out.add
2 parents 8e5808c + 941306b commit 76bee97

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nginx"
3-
version = "0.12.0"
3+
version = "0.13.0"
44
authors = ["Navid <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
readme = "README.md"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This crate provides [nginx](https://nginx.org/) bindings for Rust. Currently, on
1010

1111
```toml
1212
[dependencies]
13-
nginx = "0.12"
13+
nginx = "0.13"
1414
```
1515

1616
**Note:** In order to build the crate, `clang` must be installed.

src/helpers.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bindings::{
2-
ngx_array_t, ngx_http_headers_in_t, ngx_list_part_t, ngx_list_push, ngx_list_t, ngx_palloc,
3-
ngx_pool_t, ngx_str_t, ngx_table_elt_t, u_char,
2+
ngx_array_t, ngx_http_headers_in_t, ngx_http_headers_out_t, ngx_list_part_t, ngx_list_push,
3+
ngx_list_t, ngx_palloc, ngx_pool_t, ngx_str_t, ngx_table_elt_t, u_char,
44
};
55
use std::convert::{From, TryFrom};
66
use std::ffi::OsStr;
@@ -84,17 +84,36 @@ impl ngx_http_headers_in_t {
8484

8585
pub fn add(&mut self, pool: *mut ngx_pool_t, key: &str, value: &str) -> Option<()> {
8686
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.headers) as _ };
87-
unsafe { table.as_mut() }.map(|table| {
88-
table.hash = 1;
89-
table.key.len = key.len() as _;
90-
table.key.data = str_to_uchar(pool, key);
91-
table.value.len = value.len() as _;
92-
table.value.data = str_to_uchar(pool, value);
93-
table.lowcase_key = str_to_uchar(pool, String::from(key).to_ascii_lowercase().as_str());
94-
})
87+
add_to_ngx_table(table, pool, key, value)
9588
}
9689
}
9790

91+
impl ngx_http_headers_out_t {
92+
pub fn add(&mut self, pool: *mut ngx_pool_t, key: &str, value: &str) -> Option<()> {
93+
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.headers) as _ };
94+
add_to_ngx_table(table, pool, key, value)
95+
}
96+
}
97+
98+
fn add_to_ngx_table(
99+
table: *mut ngx_table_elt_t,
100+
pool: *mut ngx_pool_t,
101+
key: &str,
102+
value: &str,
103+
) -> Option<()> {
104+
if table.is_null() {
105+
return None;
106+
}
107+
unsafe { table.as_mut() }.map(|table| {
108+
table.hash = 1;
109+
table.key.len = key.len() as _;
110+
table.key.data = str_to_uchar(pool, key);
111+
table.value.len = value.len() as _;
112+
table.value.data = str_to_uchar(pool, value);
113+
table.lowcase_key = str_to_uchar(pool, String::from(key).to_ascii_lowercase().as_str());
114+
})
115+
}
116+
98117
impl IntoIterator for ngx_http_headers_in_t {
99118
type Item = Header;
100119
type IntoIter = ListIterator;

0 commit comments

Comments
 (0)