Skip to content

Commit a54689b

Browse files
divergentdaveseanmonstar
authored andcommitted
feat(ffi): add hyper_request_set_uri_parts
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.
1 parent ea3e228 commit a54689b

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

capi/include/hyper.h

+17
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,23 @@ enum hyper_code hyper_request_set_uri(struct hyper_request *req,
430430
const uint8_t *uri,
431431
size_t uri_len);
432432

433+
/*
434+
Set the URI of the request with separate scheme, authority, and
435+
path/query strings.
436+
437+
Each of `scheme`, `authority`, and `path_and_query` should either be
438+
null, to skip providing a component, or point to a UTF-8 encoded
439+
string. If any string pointer argument is non-null, its corresponding
440+
`len` parameter must be set to the string's length.
441+
*/
442+
enum hyper_code hyper_request_set_uri_parts(struct hyper_request *req,
443+
const uint8_t *scheme,
444+
size_t scheme_len,
445+
const uint8_t *authority,
446+
size_t authority_len,
447+
const uint8_t *path_and_query,
448+
size_t path_and_query_len);
449+
433450
/*
434451
Set the preferred HTTP version of the request.
435452

src/ffi/http_types.rs

+48
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,54 @@ ffi_fn! {
9090
}
9191
}
9292

93+
ffi_fn! {
94+
/// Set the URI of the request with separate scheme, authority, and
95+
/// path/query strings.
96+
///
97+
/// Each of `scheme`, `authority`, and `path_and_query` should either be
98+
/// null, to skip providing a component, or point to a UTF-8 encoded
99+
/// string. If any string pointer argument is non-null, its corresponding
100+
/// `len` parameter must be set to the string's length.
101+
fn hyper_request_set_uri_parts(
102+
req: *mut hyper_request,
103+
scheme: *const u8,
104+
scheme_len: size_t,
105+
authority: *const u8,
106+
authority_len: size_t,
107+
path_and_query: *const u8,
108+
path_and_query_len: size_t
109+
) -> hyper_code {
110+
let mut builder = Uri::builder();
111+
if !scheme.is_null() {
112+
let scheme_bytes = unsafe {
113+
std::slice::from_raw_parts(scheme, scheme_len as usize)
114+
};
115+
builder = builder.scheme(scheme_bytes);
116+
}
117+
if !authority.is_null() {
118+
let authority_bytes = unsafe {
119+
std::slice::from_raw_parts(authority, authority_len as usize)
120+
};
121+
builder = builder.authority(authority_bytes);
122+
}
123+
if !path_and_query.is_null() {
124+
let path_and_query_bytes = unsafe {
125+
std::slice::from_raw_parts(path_and_query, path_and_query_len as usize)
126+
};
127+
builder = builder.path_and_query(path_and_query_bytes);
128+
}
129+
match builder.build() {
130+
Ok(u) => {
131+
*unsafe { &mut *req }.0.uri_mut() = u;
132+
hyper_code::HYPERE_OK
133+
},
134+
Err(_) => {
135+
hyper_code::HYPERE_INVALID_ARG
136+
}
137+
}
138+
}
139+
}
140+
93141
ffi_fn! {
94142
/// Set the preferred HTTP version of the request.
95143
///

0 commit comments

Comments
 (0)