Skip to content

[Lib] Add DNS query for request send #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
target = "wasm32-wasi"

[target.wasm32-wasi]
runner = "wasmedge"
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jobs:

steps:
- uses: actions/checkout@v1
- name: Setup Rust Toolchain
run: |
rustup update
rustup target add wasm32-wasi
- name: Build
run: cargo build --verbose
- name: Build with Rusttls
run: cargo build --verbose --no-default-features --features rust-tls
- name: Run tests
run: cargo test --verbose
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/*.rs.bk
.DS_store
.DS_store
__pycache__
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmedge_http_req"
version = "0.8.1"
version = "0.8.2"
license = "MIT"
description = "HTTP client for the WasmEdge network socket API. Derived from the http_req library."
repository = "https://github.com/second-state/wasmedge_http_req"
Expand All @@ -12,4 +12,4 @@ edition = "2018"

[dependencies]
unicase = "^2.6"
wasmedge_wasi_socket = "0.1.0"
wasmedge_wasi_socket = "0.3"
42 changes: 0 additions & 42 deletions benches/bench.rs

This file was deleted.

2,063 changes: 0 additions & 2,063 deletions benches/res.txt

This file was deleted.

6 changes: 5 additions & 1 deletion examples/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use wasmedge_http_req::request;

fn main() {
let mut writer = Vec::new(); //container for body of a response
let res = request::get("https://www.rust-lang.org/learn", &mut writer).unwrap();
let res = request::get(
"http://doc.rust-lang.org/std/string/index.html",
&mut writer,
)
.unwrap();

println!("Status: {} {}", res.status_code(), res.reason());
println!("Headers {}", res.headers());
Expand Down
2 changes: 1 addition & 1 deletion examples/head.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use wasmedge_http_req::request;

fn main() {
let res = request::head("https://www.rust-lang.org/learn").unwrap();
let res = request::head("http://doc.rust-lang.org/std/string/index.html").unwrap();

println!("Status: {} {}", res.status_code(), res.reason());
println!("{:?}", res.headers());
Expand Down
11 changes: 6 additions & 5 deletions examples/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use wasmedge_http_req::request;

fn main() {
let mut writer = Vec::new(); //container for body of a response
const BODY: &[u8; 27] = b"field1=value1&field2=value2";
// let res = request::post("https://httpbin.org/post", BODY, &mut writer).unwrap();
// no https , no dns
let res = request::post("http://18.235.124.214/post", BODY, &mut writer).unwrap();
const BODY: &[u8; 42] = b"{\"field1\" : \"value1\", \"field2\" : \"value2\"}";
// dns is supported
// tls is not supported
// Use tests/server.py for testing
let res = request::post("http://localhost:1234/post", BODY, &mut writer).unwrap();

println!("Status: {} {}", res.status_code(), res.reason());
println!("Headers {}", res.headers());
//println!("{}", String::from_utf8_lossy(&writer));
println!("{}", String::from_utf8_lossy(&writer));
}
6 changes: 3 additions & 3 deletions examples/request_builder_get.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use wasmedge_http_req::{request::RequestBuilder, tls, uri::Uri};
use std::{convert::TryFrom, net::TcpStream};
// use std::{convert::TryFrom, net::TcpStream};
// use wasmedge_http_req::{request::RequestBuilder, tls, uri::Uri};

#[cfg(not(tls))]
fn main(){
fn main() {
unimplemented!()
}

Expand Down
22 changes: 7 additions & 15 deletions src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ where
break;
}

if let Ok(_) = self.reader.read_exact(&mut footer) {
if footer != CR_LF {
self.err = Some(error_malformed_chunked_encoding());
break;
}
if self.reader.read_exact(&mut footer).is_ok() && footer != CR_LF {
self.err = Some(error_malformed_chunked_encoding());
break;
}

self.check_end = false;
Expand Down Expand Up @@ -84,10 +82,7 @@ where
}

match self.err.as_ref() {
Some(v) => Err(Error::new(
v.kind(),
format!("wrapper by chunked: {}", v.to_string()),
)),
Some(v) => Err(Error::new(v.kind(), format!("wrapper by chunked: {}", v))),
None => Ok(consumed),
}
}
Expand Down Expand Up @@ -129,7 +124,7 @@ where
}

fn chunk_header_avaliable(&self) -> bool {
self.reader.buffer().iter().find(|&&c| c == b'\n').is_some()
self.reader.buffer().iter().any(|&c| c == b'\n')
}
}

Expand All @@ -142,10 +137,7 @@ fn error_malformed_chunked_encoding() -> Error {
}

fn is_ascii_space(b: u8) -> bool {
match b {
b' ' | b'\t' | b'\n' | b'\r' => true,
_ => false,
}
matches!(b, b' ' | b'\t' | b'\n' | b'\r')
}

fn parse_hex_uint(data: Vec<u8>) -> Result<usize, &'static str> {
Expand Down Expand Up @@ -193,7 +185,7 @@ fn remove_chunk_extension(v: &mut Vec<u8>) {
}

fn trim_trailing_whitespace(v: &mut Vec<u8>) {
if v.len() == 0 {
if v.is_empty() {
return;
}

Expand Down
13 changes: 0 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
//!Simple HTTP client with built-in HTTPS support.
//!Currently it's in heavy development and may frequently change.
//!
//!## Example
//!Basic GET request
//!```
//!use http_req::request;
//!
//!fn main() {
//! let mut writer = Vec::new(); //container for body of a response
//! let res = request::get("https://doc.rust-lang.org/", &mut writer).unwrap();
//!
//! println!("Status: {} {}", res.status_code(), res.reason());
//!}
//!```
pub mod error;
pub mod request;
pub mod response;
Expand Down
66 changes: 44 additions & 22 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use std::{
time::{Duration, Instant},
};

#[cfg(feature = "std")]
use std::net::{Shutdown, TcpStream};
#[cfg(not(feature = "std"))]
use wasmedge_wasi_socket::{Shutdown, TcpStream};
use wasmedge_wasi_socket::{TcpStream, WasiAddrinfo};

const CR_LF: &str = "\r\n";
const BUF_SIZE: usize = 8 * 1024;
Expand Down Expand Up @@ -88,7 +85,7 @@ where
let mut buf = vec![0u8; num_bytes];

reader.read_exact(&mut buf)?;
writer.write_all(&mut buf)
writer.write_all(&buf)
}

///Reads data from `reader` and checks for specified `val`ue. When data contains specified value
Expand Down Expand Up @@ -615,7 +612,7 @@ impl<'a> Request<'a> {
///let response = Request::new(&uri).send(&mut writer).unwrap();;
///```
pub fn new(uri: &'a Uri) -> Request<'a> {
let mut builder = RequestBuilder::new(&uri);
let mut builder = RequestBuilder::new(uri);
builder.header("Connection", "Close");

Request {
Expand Down Expand Up @@ -888,12 +885,36 @@ impl<'a> Request<'a> {
///let response = Request::new(&uri).send(&mut writer).unwrap();
///```
pub fn send<T: Write>(&self, writer: &mut T) -> Result<Response, error::Error> {
let host = self.inner.uri.host().unwrap_or("");
let mut host: String = self.inner.uri.host().unwrap_or("").to_string();
let port = self.inner.uri.corr_port();

let hints: WasiAddrinfo = WasiAddrinfo::default();
let mut sockaddr = Vec::new();
let mut sockbuff = Vec::new();
let mut ai_canonname = Vec::new();
let query = WasiAddrinfo::get_addrinfo(
&host,
&port.to_string(),
&hints,
10,
&mut sockaddr,
&mut sockbuff,
&mut ai_canonname,
)
.unwrap();
for record in query {
if record.ai_addrlen.ne(&0) && record.ai_family.is_v4() {
host = format!(
"{}.{}.{}.{}",
sockbuff[0][2], sockbuff[0][3], sockbuff[0][4], sockbuff[0][5]
);
break;
}
}
let mut stream = TcpStream::connect((host, port))?;

if self.inner.uri.scheme() == "https" {
return Err(error::Error::Tls)
Err(error::Error::Tls)
} else {
self.inner.send(&mut stream, writer)
}
Expand Down Expand Up @@ -1103,22 +1124,22 @@ mod tests {
.unwrap();
}

#[ignore]
#[test]
fn request_b_send_secure() {
let mut writer = Vec::new();
let uri = Uri::try_from(URI_S).unwrap();
// #[ignore]
// #[test]
// fn request_b_send_secure() {
// let mut writer = Vec::new();
// let uri = Uri::try_from(URI_S).unwrap();

let stream = TcpStream::connect((uri.host().unwrap_or(""), uri.corr_port())).unwrap();
let mut secure_stream = tls::Config::default()
.connect(uri.host().unwrap_or(""), stream)
.unwrap();
// let stream = TcpStream::connect((uri.host().unwrap_or(""), uri.corr_port())).unwrap();
// let mut secure_stream = tls::Config::default()
// .connect(uri.host().unwrap_or(""), stream)
// .unwrap();

RequestBuilder::new(&Uri::try_from(URI_S).unwrap())
.header("Connection", "Close")
.send(&mut secure_stream, &mut writer)
.unwrap();
}
// RequestBuilder::new(&Uri::try_from(URI_S).unwrap())
// .header("Connection", "Close")
// .send(&mut secure_stream, &mut writer)
// .unwrap();
// }

#[test]
fn request_b_parse_msg() {
Expand Down Expand Up @@ -1207,6 +1228,7 @@ mod tests {
assert_eq!(request.inner.timeout, timeout);
}

#[ignore]
#[test]
fn request_connect_timeout() {
let uri = Uri::try_from(URI).unwrap();
Expand Down
Loading