Skip to content

Commit

Permalink
support http accept
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Mar 14, 2024
1 parent 6f31d53 commit 0d61a55
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/api/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ impl OwnValue {
OwnValue::Bytes(x) => Value::Bytes(x.as_ref()),
}
}

pub fn as_cow_str(&self) -> Cow<str> {
match self {
OwnValue::String(x) => Cow::Borrowed(x),
_ => Cow::Owned(self.to_string()),
}
}
}

impl Hash for OwnValue {
Expand Down
9 changes: 7 additions & 2 deletions src/api/xell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,14 @@ impl Xell {
}

pub fn write(&self) -> CellWriter {
if self.domain.write_policy.get() == WritePolicy::ReadOnly {
if self.domain.write_policy.get() == WritePolicy::ReadOnly
// allow writing elevation cells, to set elevation parameters
&& !matches!(self.dyn_cell, DynCell::Elevation(_))
{
let err = usererr("cannot write, read-only domain")
.with_path(self.path().unwrap_or_default());
return CellWriter {
dyn_cell_writer: DynCellWriter::from(usererr("cannot write, read-only domain")),
dyn_cell_writer: DynCellWriter::from(err),
domain: Rc::clone(&self.domain),
};
}
Expand Down
27 changes: 26 additions & 1 deletion src/interpretations/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ pub(crate) struct CellWriter {}

implement_try_from_xell!(Cell, Http);

const METHOD_PARAM_NAME: &str = "method";
const HEAD_METHOD: &str = "HEAD";
const GET_METHOD: &str = "GET";

const ACCEPT_HEADER: &str = "accept";

impl Cell {
pub(crate) fn from_cell(cell: Xell, _: &str, params: &ElevateParams) -> Res<Xell> {
let reader = cell.read().err()?;
Expand All @@ -70,11 +76,29 @@ impl Cell {
let url = value_cow.as_ref();

let client = Client::builder().user_agent("hial").build()?;
let request = if params.contains_key(&Value::Str("HEAD")) {
let method = {
if params.contains_key(&Value::Str(HEAD_METHOD)) {
HEAD_METHOD
} else if let Some(method) = params.get(&Value::Str(METHOD_PARAM_NAME)) {
match method.as_cow_str().as_ref() {
HEAD_METHOD => HEAD_METHOD,
_ => GET_METHOD,
}
} else {
GET_METHOD
}
};
let request = if method == HEAD_METHOD {
client.head(url)
} else {
client.get(url)
};
let request = if let Some(accept) = params.get(&Value::Str(ACCEPT_HEADER)) {
request.header(ACCEPT_HEADER, accept.as_cow_str().as_ref())
} else {
request
};
println!("http call request {:?}", request);
let response = request.send()?;

let mut headers = IndexMap::<String, Vec<String>>::new();
Expand All @@ -95,6 +119,7 @@ impl Cell {
if status >= 400 {
warning!("Error: http call failed: {} = {} {}", url, status, reason);
}
println!("http call response {:?}", response);
let response = OwnRc::new(Response {
status,
reason,
Expand Down
2 changes: 1 addition & 1 deletion src/search/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn relation(input: &str) -> NomRes<&str, char> {
}

fn rvalue(input: &str) -> NomRes<&str, Value> {
context("value", alt((value_ident, value_string, value_uint)))(input)
context("value", alt((value_string, value_uint, value_ident)))(input)
}

fn value_ident(input: &str) -> NomRes<&str, Value> {
Expand Down
7 changes: 6 additions & 1 deletion src/search/searcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@ impl<'s> Searcher<'s> {

match pi {
PathItem::Elevation(npi) => {
Self::process_elevation(
let opt_res = Self::process_elevation(
&mut self.stack,
npi,
parent,
path_index,
&mut self.next_max_path_index,
);
if let Some(Err(e)) = opt_res {
return Some(Err(e));
}
}
PathItem::Normal(npi) => {
let group = match npi.relation {
Expand Down Expand Up @@ -183,6 +186,7 @@ impl<'s> Searcher<'s> {
None
}

#[must_use]
fn process_elevation(
stack: &mut Vec<MatchTest>,
epi: &ElevationPathItem,
Expand All @@ -201,6 +205,7 @@ impl<'s> Searcher<'s> {
_ => return Some(userres("bad interpretation selector")),
};
let itp_cell = guard_ok!(itp_cell.err(), err => {
println!("Error while searching: cannot get interpretation cell: {:?}", err);
return Some(Err(err));
});
if !epi.params.is_empty() {
Expand Down

0 comments on commit 0d61a55

Please sign in to comment.