1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
pub use ;
// Load bindings from WIT file.
// wit_bindgen_rust::import!({paths: ["../../wit/core/http.wit"]});
generate!;
use Guest;
use *;
;
use Guest as GuestClient;
use types;
;
/*
use self::httpworld::{
send_http_request as host_send_http_request, HttpMethod, HttpRequest, HttpResponse,
};
pub use self::httpworld::HttpRequestError;
impl From<HttpResponse> for Response<Vec<u8>> {
fn from(value: HttpResponse) -> Self {
let mut builder = Response::builder().status(value.status);
for (key, value) in value.headers.iter() {
builder = builder.header(key, value);
}
match value.body {
Some(data) => builder.body(data).unwrap(),
None => builder.body(Vec::new()).unwrap(),
}
}
}
pub fn send_http_request<T>(req: Request<T>) -> Result<Response<Vec<u8>>, HttpRequestError>
where
T: Into<Vec<u8>>,
{
let method = match *req.method() {
Method::GET => HttpMethod::Get,
Method::POST => HttpMethod::Post,
Method::PUT => HttpMethod::Put,
Method::PATCH => HttpMethod::Patch,
Method::DELETE => HttpMethod::Delete,
Method::OPTIONS => HttpMethod::Options,
Method::HEAD => HttpMethod::Head,
_ => HttpMethod::Get,
};
let mut parsed_headers: Vec<(String, String)> = Vec::new();
for (key, value) in req.headers().iter() {
if let Ok(value) = value.to_str() {
parsed_headers.push((key.to_string(), value.to_string()))
}
}
let headers_slice: Vec<(&str, &str)> = parsed_headers
.iter()
.map(|(k, v)| (k.as_str(), v.as_str()))
.collect::<Vec<(&str, &str)>>();
let uri = req.uri().to_string();
let body: Vec<u8> = req.into_body().into();
let request = HttpRequest {
body: Some(body.as_slice()),
headers: &headers_slice,
method,
params: &[],
uri: &uri,
};
host_send_http_request(request).map(|http_req| http_req.into())
}
*/