Skip to content

Commit

Permalink
feat(ffi): add hyper_request_set_uri_parts
Browse files Browse the repository at this point in the history
Add a second FFI interface for setting the URI of a request with three
separate schema, authority, and path/query strings, rather than one URI
string.
  • Loading branch information
divergentdave authored and seanmonstar committed Sep 16, 2021
1 parent ea3e228 commit a54689b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
17 changes: 17 additions & 0 deletions capi/include/hyper.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,23 @@ enum hyper_code hyper_request_set_uri(struct hyper_request *req,
const uint8_t *uri,
size_t uri_len);

/*
Set the URI of the request with separate scheme, authority, and
path/query strings.
Each of `scheme`, `authority`, and `path_and_query` should either be
null, to skip providing a component, or point to a UTF-8 encoded
string. If any string pointer argument is non-null, its corresponding
`len` parameter must be set to the string's length.
*/
enum hyper_code hyper_request_set_uri_parts(struct hyper_request *req,
const uint8_t *scheme,
size_t scheme_len,
const uint8_t *authority,
size_t authority_len,
const uint8_t *path_and_query,
size_t path_and_query_len);

/*
Set the preferred HTTP version of the request.
Expand Down
48 changes: 48 additions & 0 deletions src/ffi/http_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,54 @@ ffi_fn! {
}
}

ffi_fn! {
/// Set the URI of the request with separate scheme, authority, and
/// path/query strings.
///
/// Each of `scheme`, `authority`, and `path_and_query` should either be
/// null, to skip providing a component, or point to a UTF-8 encoded
/// string. If any string pointer argument is non-null, its corresponding
/// `len` parameter must be set to the string's length.
fn hyper_request_set_uri_parts(
req: *mut hyper_request,
scheme: *const u8,
scheme_len: size_t,
authority: *const u8,
authority_len: size_t,
path_and_query: *const u8,
path_and_query_len: size_t
) -> hyper_code {
let mut builder = Uri::builder();
if !scheme.is_null() {
let scheme_bytes = unsafe {
std::slice::from_raw_parts(scheme, scheme_len as usize)
};
builder = builder.scheme(scheme_bytes);
}
if !authority.is_null() {
let authority_bytes = unsafe {
std::slice::from_raw_parts(authority, authority_len as usize)
};
builder = builder.authority(authority_bytes);
}
if !path_and_query.is_null() {
let path_and_query_bytes = unsafe {
std::slice::from_raw_parts(path_and_query, path_and_query_len as usize)
};
builder = builder.path_and_query(path_and_query_bytes);
}
match builder.build() {
Ok(u) => {
*unsafe { &mut *req }.0.uri_mut() = u;
hyper_code::HYPERE_OK
},
Err(_) => {
hyper_code::HYPERE_INVALID_ARG
}
}
}
}

ffi_fn! {
/// Set the preferred HTTP version of the request.
///
Expand Down

0 comments on commit a54689b

Please sign in to comment.