Skip to content

Commit

Permalink
feat: allow assigning non-UTF-8 data to headers
Browse files Browse the repository at this point in the history
  • Loading branch information
bavshin-f5 committed Nov 26, 2024
1 parent ca43aed commit ae2ee99
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
27 changes: 15 additions & 12 deletions nginx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,21 @@ impl TryFrom<ngx_str_t> for &str {
pub unsafe fn add_to_ngx_table(
table: *mut ngx_table_elt_t,
pool: *mut ngx_pool_t,
key: &str,
value: &str,
key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
) -> Option<()> {
if table.is_null() {
return None;
if let Some(table) = table.as_mut() {
let key = key.as_ref();
table.key = ngx_str_t::from_bytes(pool, key)?;
table.value = ngx_str_t::from_bytes(pool, value.as_ref())?;

table.lowcase_key = ngx_pnalloc(pool, table.key.len).cast();
if table.lowcase_key.is_null() {
return None;
}
table.hash = ngx_hash_strlow(table.lowcase_key, table.key.data, table.key.len);

return Some(());
}
table.as_mut().map(|table| {
table.hash = 1;
table.key.len = key.len();
table.key.data = str_to_uchar(pool, key);
table.value.len = value.len();
table.value.data = str_to_uchar(pool, value);
table.lowcase_key = str_to_uchar(pool, String::from(key).to_ascii_lowercase().as_str());
})
None
}
4 changes: 2 additions & 2 deletions src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ impl Request {
/// Add header to the `headers_in` object.
///
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
pub fn add_header_in(&mut self, key: &str, value: &str) -> Option<()> {
pub fn add_header_in(&mut self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>) -> Option<()> {
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_in.headers) as _ };
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
}

/// Add header to the `headers_out` object.
///
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
pub fn add_header_out(&mut self, key: &str, value: &str) -> Option<()> {
pub fn add_header_out(&mut self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>) -> Option<()> {
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_out.headers) as _ };
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
}
Expand Down

0 comments on commit ae2ee99

Please sign in to comment.