use std::io::Read as _;
use std::sync::Arc;
use facet::Facet;
use ureq::ResponseExt;
use xur_runtime::convert::into_value;
use xur_runtime::{
Context, NativeObject, OneOf, Registry, RunResult, RuntimeError, Value, XurBytes, XurStr, lock,
one_of,
};
use crate::{error_text, result};
one_of! {
enum Body {
Text(XurStr),
Bytes(XurBytes),
}
}
fn configured_rb(rb: &RequestBuilder, change: impl FnOnce(&mut RequestBuilder)) -> RequestBuilder {
let mut rb = rb.clone();
change(&mut rb);
rb
}
pub(crate) fn register(registry: &Registry) {
registry.lazy_module("std::http", |module| {
module
.raw_function_returns::<Response>("get", http_get)
.raw_function_returns::<Response>("post", http_post)
.raw_function_returns::<Response>("put", http_put)
.raw_function_returns::<Response>("patch", http_patch)
.raw_function_returns::<Response>("delete", http_delete)
.raw_function_returns::<Response>("head", http_head)
.raw_function_returns::<RequestBuilder>("request", http_request);
module
.ty::<RequestBuilder>("RequestBuilder")
.method(
"header",
|rb: &RequestBuilder, name: XurStr, value: XurStr| {
configured_rb(rb, |rb| {
rb.headers.push((name.to_string(), value.to_string()))
})
},
)
.method("bearer_auth", |rb: &RequestBuilder, token: XurStr| {
configured_rb(rb, |rb| {
rb.headers
.push(("authorization".into(), format!("Bearer {}", &*token)))
})
})
.method(
"basic_auth",
|rb: &RequestBuilder, user: XurStr, pass: XurStr| {
configured_rb(rb, |rb| {
let encoded = base64_encode(format!("{}:{}", &*user, &*pass).as_bytes());
rb.headers
.push(("authorization".into(), format!("Basic {encoded}")));
})
},
)
.method("body", |rb: &RequestBuilder, body: OneOf<Body>| {
let bytes = match body.0 {
Body::Text(s) => s.as_bytes().to_vec(),
Body::Bytes(b) => lock(&b.0).clone(),
};
configured_rb(rb, |rb| rb.body = Some(bytes))
})
.raw_function_returns::<RequestBuilder>("json_body", rb_json_body)
.method("timeout", |rb: &RequestBuilder, secs: u64| {
configured_rb(rb, |rb| rb.timeout_secs = Some(secs))
})
.method(
"query",
|rb: &RequestBuilder, key: XurStr, value: XurStr| {
configured_rb(rb, |rb| {
rb.query_params.push((key.to_string(), value.to_string()))
})
},
)
.raw_function_returns::<Response>("send", rb_send);
module
.ty::<Response>("Response")
.method("status", |r: &Response| r.status as i64)
.method("status_text", |r: &Response| r.status_text.clone())
.method("url", |r: &Response| r.url.clone())
.method("text", |r: &Response| {
String::from_utf8_lossy(&r.body).into_owned()
})
.raw_function_returns::<Vec<u8>>("bytes", resp_bytes)
.raw_function("json", resp_json)
.method("header", |r: &Response, name: XurStr| -> Option<String> {
let lower = name.to_lowercase();
r.headers
.iter()
.find(|(k, _)| k.to_lowercase() == lower)
.map(|(_, v)| v.clone())
})
.method("headers", |r: &Response| -> Value {
let fields: Vec<(xur_span::Symbol, Value)> = r
.headers
.iter()
.map(|(k, v)| (xur_span::Symbol::intern(k), Value::str(v)))
.collect();
Value::Record(xur_runtime::Record::new(fields).into())
});
});
}
#[derive(Clone, Facet)]
#[facet(opaque)]
struct Response {
status: u16,
status_text: String,
headers: Vec<(String, String)>,
body: Arc<Vec<u8>>,
url: String,
}
impl std::fmt::Display for Response {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Response {{ {} {} }}", self.status, self.status_text)
}
}
fn read_response(resp: ureq::http::Response<ureq::Body>) -> RunResult<Response> {
let status = resp.status().as_u16();
let status_text = resp.status().canonical_reason().unwrap_or("").to_string();
let url = resp.get_uri().to_string();
let mut headers = Vec::new();
for (name, value) in resp.headers() {
headers.push((name.to_string(), value.to_str().unwrap_or("").to_string()));
}
let mut body_bytes = Vec::new();
resp.into_body()
.as_reader()
.read_to_end(&mut body_bytes)
.map_err(|e| RuntimeError::new(format!("failed to read response body: {e}")))?;
Ok(Response {
status,
status_text,
headers,
body: Arc::new(body_bytes),
url,
})
}
fn with_response<R>(value: &Value, f: impl FnOnce(&Response) -> R) -> RunResult<R> {
match value {
Value::Native(obj) => obj
.with(f)
.ok_or_else(|| RuntimeError::new("expected an HTTP Response")),
_ => Err(RuntimeError::new("expected an HTTP Response")),
}
}
fn resp_bytes(_cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let [this] = args else {
return Err(RuntimeError::new("`bytes` takes no arguments"));
};
let bytes = with_response(this, |r| r.body.as_ref().clone())?;
Ok(Value::bytes(bytes))
}
fn resp_json(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let [this] = args else {
return Err(RuntimeError::new("`json` takes no arguments"));
};
let text = with_response(this, |r| String::from_utf8_lossy(&r.body).into_owned())?;
match crate::json::parse_str(cx, &text) {
Ok(value) => Ok(result(cx, Ok(value))),
Err(e) => Ok(result(cx, Err(e.message))),
}
}
#[derive(Clone, Facet)]
#[facet(opaque)]
struct RequestBuilder {
method: String,
url: String,
headers: Vec<(String, String)>,
body: Option<Vec<u8>>,
content_type: Option<String>,
timeout_secs: Option<u64>,
query_params: Vec<(String, String)>,
}
impl std::fmt::Display for RequestBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "RequestBuilder {{ {} {} }}", self.method, self.url)
}
}
fn with_rb(value: &Value, f: impl FnOnce(&RequestBuilder) -> RequestBuilder) -> RunResult<Value> {
match value {
Value::Native(obj) => {
let new = obj
.with(f)
.ok_or_else(|| RuntimeError::new("expected a RequestBuilder"))?;
let new_obj = NativeObject::new(obj.ty, Box::new(new));
Ok(Value::Native(new_obj.into()))
}
_ => Err(RuntimeError::new("expected a RequestBuilder")),
}
}
fn rb_json_body(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let [this, value] = args else {
return Err(RuntimeError::new("`json_body` takes a value to serialize"));
};
let json_str = crate::json::stringify_value(cx, value)?;
with_rb(this, |rb| {
let mut rb = rb.clone();
rb.body = Some(json_str.clone().into_bytes());
rb.content_type = Some("application/json".into());
rb
})
}
fn rb_send(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let [this] = args else {
return Err(RuntimeError::new("`send` takes no arguments"));
};
let rb = match this {
Value::Native(obj) => obj
.with(|rb: &RequestBuilder| rb.clone())
.ok_or_else(|| RuntimeError::new("expected a RequestBuilder"))?,
_ => return Err(RuntimeError::new("expected a RequestBuilder")),
};
match execute_request(&rb) {
Ok(resp) => {
let value = into_value(cx, resp)?;
Ok(result(cx, Ok(value)))
}
Err(e) => Ok(result(cx, Err(e.message))),
}
}
fn execute_request(rb: &RequestBuilder) -> RunResult<Response> {
let config = ureq::Agent::config_builder()
.timeout_global(Some(std::time::Duration::from_secs(
rb.timeout_secs.unwrap_or(30),
)))
.build();
let agent = ureq::Agent::new_with_config(config);
let uses_body = rb.body.is_some() || matches!(rb.method.as_str(), "POST" | "PUT" | "PATCH");
let resp = if uses_body {
let mut req = match rb.method.as_str() {
"PUT" => agent.put(&rb.url),
"PATCH" => agent.patch(&rb.url),
_ => agent.post(&rb.url),
};
for (k, v) in &rb.headers {
req = req.header(k.as_str(), v.as_str());
}
for (k, v) in &rb.query_params {
req = req.query(k.as_str(), v.as_str());
}
if let Some(ct) = &rb.content_type {
req = req.header("content-type", ct.as_str());
}
let body_data = rb.body.clone().unwrap_or_default();
req.send(body_data)
} else {
let mut req = match rb.method.as_str() {
"DELETE" => agent.delete(&rb.url),
"HEAD" => agent.head(&rb.url),
"OPTIONS" => agent.options(&rb.url),
_ => agent.get(&rb.url),
};
for (k, v) in &rb.headers {
req = req.header(k.as_str(), v.as_str());
}
for (k, v) in &rb.query_params {
req = req.query(k.as_str(), v.as_str());
}
req.call()
};
match resp {
Ok(resp) => read_response(resp),
Err(ureq::Error::StatusCode(code)) => {
Err(RuntimeError::new(format!("HTTP error: status {code}")))
}
Err(e) => Err(RuntimeError::new(error_text(e))),
}
}
fn http_get(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let [Value::Str(url)] = args else {
return Err(RuntimeError::new("`get` takes a URL string"));
};
simple_request(cx, "GET", url, None)
}
fn http_post(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let (url, body) = match args {
[Value::Str(url)] => (url, None),
[Value::Str(url), Value::Str(body)] => (url, Some(body.as_bytes().to_vec())),
[Value::Str(url), Value::Bytes(body)] => (url, Some(lock(body).clone())),
_ => return Err(RuntimeError::new("`post` takes (url) or (url, body)")),
};
simple_request(cx, "POST", url, body)
}
fn http_put(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let (url, body) = match args {
[Value::Str(url)] => (url, None),
[Value::Str(url), Value::Str(body)] => (url, Some(body.as_bytes().to_vec())),
[Value::Str(url), Value::Bytes(body)] => (url, Some(lock(body).clone())),
_ => return Err(RuntimeError::new("`put` takes (url) or (url, body)")),
};
simple_request(cx, "PUT", url, body)
}
fn http_patch(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let (url, body) = match args {
[Value::Str(url)] => (url, None),
[Value::Str(url), Value::Str(body)] => (url, Some(body.as_bytes().to_vec())),
[Value::Str(url), Value::Bytes(body)] => (url, Some(lock(body).clone())),
_ => return Err(RuntimeError::new("`patch` takes (url) or (url, body)")),
};
simple_request(cx, "PATCH", url, body)
}
fn http_delete(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let [Value::Str(url)] = args else {
return Err(RuntimeError::new("`delete` takes a URL string"));
};
simple_request(cx, "DELETE", url, None)
}
fn http_head(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let [Value::Str(url)] = args else {
return Err(RuntimeError::new("`head` takes a URL string"));
};
simple_request(cx, "HEAD", url, None)
}
fn http_request(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let [Value::Str(method), Value::Str(url)] = args else {
return Err(RuntimeError::new("`request` takes (method, url) strings"));
};
let rb = RequestBuilder {
method: method.to_uppercase(),
url: url.to_string(),
headers: Vec::new(),
body: None,
content_type: None,
timeout_secs: None,
query_params: Vec::new(),
};
into_value(cx, rb)
}
fn simple_request(
cx: &mut dyn Context,
method: &str,
url: &str,
body: Option<Vec<u8>>,
) -> RunResult<Value> {
let rb = RequestBuilder {
method: method.into(),
url: url.into(),
headers: Vec::new(),
body,
content_type: None,
timeout_secs: None,
query_params: Vec::new(),
};
match execute_request(&rb) {
Ok(resp) => {
let value = into_value(cx, resp)?;
Ok(result(cx, Ok(value)))
}
Err(e) => Ok(result(cx, Err(e.message))),
}
}
fn base64_encode(data: &[u8]) -> String {
const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
for chunk in data.chunks(3) {
let b0 = chunk[0] as u32;
let b1 = chunk.get(1).copied().unwrap_or(0) as u32;
let b2 = chunk.get(2).copied().unwrap_or(0) as u32;
let n = (b0 << 16) | (b1 << 8) | b2;
out.push(CHARS[(n >> 18 & 63) as usize] as char);
out.push(CHARS[(n >> 12 & 63) as usize] as char);
if chunk.len() > 1 {
out.push(CHARS[(n >> 6 & 63) as usize] as char);
} else {
out.push('=');
}
if chunk.len() > 2 {
out.push(CHARS[(n & 63) as usize] as char);
} else {
out.push('=');
}
}
out
}