use sp_core::{
offchain::{
HttpError, HttpRequestId as RequestId, HttpRequestStatus as RequestStatus, Timestamp,
},
RuntimeDebug,
};
#[cfg(not(feature = "std"))]
use sp_std::prelude::vec;
use sp_std::{prelude::Vec, str};
#[derive(Clone, PartialEq, Eq, RuntimeDebug)]
pub enum Method {
Get,
Post,
Put,
Patch,
Delete,
Other(&'static str),
}
impl AsRef<str> for Method {
fn as_ref(&self) -> &str {
match *self {
Method::Get => "GET",
Method::Post => "POST",
Method::Put => "PUT",
Method::Patch => "PATCH",
Method::Delete => "DELETE",
Method::Other(m) => m,
}
}
}
mod header {
use super::*;
#[derive(Clone, PartialEq, Eq, RuntimeDebug)]
pub struct Header {
name: Vec<u8>,
value: Vec<u8>,
}
impl Header {
pub fn new(name: &str, value: &str) -> Self {
Header { name: name.as_bytes().to_vec(), value: value.as_bytes().to_vec() }
}
pub fn name(&self) -> &str {
unsafe { str::from_utf8_unchecked(&self.name) }
}
pub fn value(&self) -> &str {
unsafe { str::from_utf8_unchecked(&self.value) }
}
}
}
#[derive(Clone, PartialEq, Eq, RuntimeDebug)]
pub struct Request<'a, T = Vec<&'static [u8]>> {
pub method: Method,
pub url: &'a str,
pub body: T,
pub deadline: Option<Timestamp>,
headers: Vec<header::Header>,
}
impl<T: Default> Default for Request<'static, T> {
fn default() -> Self {
Request {
method: Method::Get,
url: "http://localhost",
headers: Vec::new(),
body: Default::default(),
deadline: None,
}
}
}
impl<'a> Request<'a> {
pub fn get(url: &'a str) -> Self {
Self::new(url)
}
}
impl<'a, T> Request<'a, T> {
pub fn post(url: &'a str, body: T) -> Self {
let req: Request = Request::default();
Request { url, body, method: Method::Post, headers: req.headers, deadline: req.deadline }
}
}
impl<'a, T: Default> Request<'a, T> {
pub fn new(url: &'a str) -> Self {
Request::default().url(url)
}
pub fn method(mut self, method: Method) -> Self {
self.method = method;
self
}
pub fn url(mut self, url: &'a str) -> Self {
self.url = url;
self
}
pub fn body(mut self, body: T) -> Self {
self.body = body;
self
}
pub fn add_header(mut self, name: &str, value: &str) -> Self {
self.headers.push(header::Header::new(name, value));
self
}
pub fn deadline(mut self, deadline: Timestamp) -> Self {
self.deadline = Some(deadline);
self
}
}
impl<'a, I: AsRef<[u8]>, T: IntoIterator<Item = I>> Request<'a, T> {
pub fn send(self) -> Result<PendingRequest, HttpError> {
let meta = &[];
let id = sp_io::offchain::http_request_start(self.method.as_ref(), self.url, meta)
.map_err(|_| HttpError::IoError)?;
for header in &self.headers {
sp_io::offchain::http_request_add_header(id, header.name(), header.value())
.map_err(|_| HttpError::IoError)?
}
for chunk in self.body {
sp_io::offchain::http_request_write_body(id, chunk.as_ref(), self.deadline)?;
}
sp_io::offchain::http_request_write_body(id, &[], self.deadline)?;
Ok(PendingRequest { id })
}
}
#[derive(Clone, PartialEq, Eq, RuntimeDebug)]
pub enum Error {
DeadlineReached,
IoError,
Unknown,
}
#[derive(PartialEq, Eq, RuntimeDebug)]
pub struct PendingRequest {
pub id: RequestId,
}
pub type HttpResult = Result<Response, Error>;
impl PendingRequest {
pub fn wait(self) -> HttpResult {
match self.try_wait(None) {
Ok(res) => res,
Err(_) => panic!("Since `None` is passed we will never get a deadline error; qed"),
}
}
pub fn try_wait(
self,
deadline: impl Into<Option<Timestamp>>,
) -> Result<HttpResult, PendingRequest> {
Self::try_wait_all(vec![self], deadline)
.pop()
.expect("One request passed, one status received; qed")
}
pub fn wait_all(requests: Vec<PendingRequest>) -> Vec<HttpResult> {
Self::try_wait_all(requests, None)
.into_iter()
.map(|r| match r {
Ok(r) => r,
Err(_) => panic!("Since `None` is passed we will never get a deadline error; qed"),
})
.collect()
}
pub fn try_wait_all(
requests: Vec<PendingRequest>,
deadline: impl Into<Option<Timestamp>>,
) -> Vec<Result<HttpResult, PendingRequest>> {
let ids = requests.iter().map(|r| r.id).collect::<Vec<_>>();
let statuses = sp_io::offchain::http_response_wait(&ids, deadline.into());
statuses
.into_iter()
.zip(requests.into_iter())
.map(|(status, req)| match status {
RequestStatus::DeadlineReached => Err(req),
RequestStatus::IoError => Ok(Err(Error::IoError)),
RequestStatus::Invalid => Ok(Err(Error::Unknown)),
RequestStatus::Finished(code) => Ok(Ok(Response::new(req.id, code))),
})
.collect()
}
}
#[derive(RuntimeDebug)]
pub struct Response {
pub id: RequestId,
pub code: u16,
headers: Option<Headers>,
}
impl Response {
fn new(id: RequestId, code: u16) -> Self {
Self { id, code, headers: None }
}
pub fn headers(&mut self) -> &Headers {
if self.headers.is_none() {
self.headers = Some(Headers { raw: sp_io::offchain::http_response_headers(self.id) });
}
self.headers.as_ref().expect("Headers were just set; qed")
}
pub fn body(&self) -> ResponseBody {
ResponseBody::new(self.id)
}
}
#[derive(Clone)]
pub struct ResponseBody {
id: RequestId,
error: Option<HttpError>,
buffer: [u8; 4096],
filled_up_to: Option<usize>,
position: usize,
deadline: Option<Timestamp>,
}
#[cfg(feature = "std")]
impl std::fmt::Debug for ResponseBody {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.debug_struct("ResponseBody")
.field("id", &self.id)
.field("error", &self.error)
.field("buffer", &self.buffer.len())
.field("filled_up_to", &self.filled_up_to)
.field("position", &self.position)
.field("deadline", &self.deadline)
.finish()
}
}
impl ResponseBody {
fn new(id: RequestId) -> Self {
ResponseBody {
id,
error: None,
buffer: [0_u8; 4096],
filled_up_to: None,
position: 0,
deadline: None,
}
}
pub fn deadline(&mut self, deadline: impl Into<Option<Timestamp>>) {
self.deadline = deadline.into();
self.error = None;
}
pub fn error(&self) -> &Option<HttpError> {
&self.error
}
}
impl Iterator for ResponseBody {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
if self.error.is_some() {
return None
}
if self.filled_up_to.is_none() {
let result =
sp_io::offchain::http_response_read_body(self.id, &mut self.buffer, self.deadline);
match result {
Err(e) => {
self.error = Some(e);
return None
},
Ok(0) => return None,
Ok(size) => {
self.position = 0;
self.filled_up_to = Some(size as usize);
},
}
}
if Some(self.position) == self.filled_up_to {
self.filled_up_to = None;
return self.next()
}
let result = self.buffer[self.position];
self.position += 1;
Some(result)
}
}
#[derive(Clone, PartialEq, Eq, RuntimeDebug)]
pub struct Headers {
pub raw: Vec<(Vec<u8>, Vec<u8>)>,
}
impl Headers {
pub fn find(&self, name: &str) -> Option<&str> {
let raw = name.as_bytes();
for &(ref key, ref val) in &self.raw {
if &**key == raw {
return str::from_utf8(val).ok()
}
}
None
}
pub fn into_iter(&self) -> HeadersIterator {
HeadersIterator { collection: &self.raw, index: None }
}
}
#[derive(Clone, RuntimeDebug)]
pub struct HeadersIterator<'a> {
collection: &'a [(Vec<u8>, Vec<u8>)],
index: Option<usize>,
}
impl<'a> HeadersIterator<'a> {
pub fn next(&mut self) -> bool {
let index = self.index.map(|x| x + 1).unwrap_or(0);
self.index = Some(index);
index < self.collection.len()
}
pub fn current(&self) -> Option<(&str, &str)> {
self.collection
.get(self.index?)
.map(|val| (str::from_utf8(&val.0).unwrap_or(""), str::from_utf8(&val.1).unwrap_or("")))
}
}
#[cfg(test)]
mod tests {
use super::*;
use sp_core::offchain::{testing, OffchainWorkerExt};
use sp_io::TestExternalities;
#[test]
fn should_send_a_basic_request_and_get_response() {
let (offchain, state) = testing::TestOffchainExt::new();
let mut t = TestExternalities::default();
t.register_extension(OffchainWorkerExt::new(offchain));
t.execute_with(|| {
let request: Request = Request::get("http://localhost:1234");
let pending = request.add_header("X-Auth", "hunter2").send().unwrap();
state.write().fulfill_pending_request(
0,
testing::PendingRequest {
method: "GET".into(),
uri: "http://localhost:1234".into(),
headers: vec![("X-Auth".into(), "hunter2".into())],
sent: true,
..Default::default()
},
b"1234".to_vec(),
None,
);
let mut response = pending.wait().unwrap();
let mut headers = response.headers().into_iter();
assert_eq!(headers.current(), None);
assert_eq!(headers.next(), false);
assert_eq!(headers.current(), None);
let body = response.body();
assert_eq!(body.clone().collect::<Vec<_>>(), b"1234".to_vec());
assert_eq!(body.error(), &None);
})
}
#[test]
fn should_send_huge_response() {
let (offchain, state) = testing::TestOffchainExt::new();
let mut t = TestExternalities::default();
t.register_extension(OffchainWorkerExt::new(offchain));
t.execute_with(|| {
let request: Request = Request::get("http://localhost:1234");
let pending = request.add_header("X-Auth", "hunter2").send().unwrap();
state.write().fulfill_pending_request(
0,
testing::PendingRequest {
method: "GET".into(),
uri: "http://localhost:1234".into(),
headers: vec![("X-Auth".into(), "hunter2".into())],
sent: true,
..Default::default()
},
vec![0; 5923],
None,
);
let response = pending.wait().unwrap();
let body = response.body();
assert_eq!(body.clone().collect::<Vec<_>>(), vec![0; 5923]);
assert_eq!(body.error(), &None);
})
}
#[test]
fn should_send_a_post_request() {
let (offchain, state) = testing::TestOffchainExt::new();
let mut t = TestExternalities::default();
t.register_extension(OffchainWorkerExt::new(offchain));
t.execute_with(|| {
let pending = Request::default()
.method(Method::Post)
.url("http://localhost:1234")
.body(vec![b"1234"])
.send()
.unwrap();
state.write().fulfill_pending_request(
0,
testing::PendingRequest {
method: "POST".into(),
uri: "http://localhost:1234".into(),
body: b"1234".to_vec(),
sent: true,
..Default::default()
},
b"1234".to_vec(),
Some(("Test".to_owned(), "Header".to_owned())),
);
let mut response = pending.wait().unwrap();
let mut headers = response.headers().into_iter();
assert_eq!(headers.current(), None);
assert_eq!(headers.next(), true);
assert_eq!(headers.current(), Some(("Test", "Header")));
let body = response.body();
assert_eq!(body.clone().collect::<Vec<_>>(), b"1234".to_vec());
assert_eq!(body.error(), &None);
})
}
}