Skip to content
Merged
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
21 changes: 15 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ impl<T: ClientTransport + 'static> CoAPClient<T> {
self.block1_size = block1_max_bytes;
}

fn parse_coap_url(url: &str) -> IoResult<(String, u16, String, Option<Vec<u8>>)> {
fn parse_coap_url(url: &str) -> IoResult<(String, u16, String, Vec<Vec<u8>>)> {
let url_params = match Url::parse(url) {
Ok(url_params) => url_params,
Err(_) => return Err(Error::new(ErrorKind::InvalidInput, "url error")),
Expand All @@ -925,7 +925,10 @@ impl<T: ClientTransport + 'static> CoAPClient<T> {

let path = url_params.path().to_string();

let queries = url_params.query().map(|q| q.as_bytes().to_vec());
let queries = url_params
.query()
.map(|q| q.split("&").map(|qi| qi.as_bytes().to_vec()).collect())
.unwrap_or(vec![]);

return Ok((host.to_string(), port, path, queries));
}
Expand Down Expand Up @@ -1034,10 +1037,16 @@ mod test {

#[test]
fn test_parse_queries() {
if let Ok((_, _, _, Some(queries))) =
if let Ok((_, _, _, queries)) =
UdpCoAPClient::parse_coap_url("coap://127.0.0.1/?hello=world&test1=test2")
{
assert_eq!("hello=world&test1=test2".as_bytes().to_vec(), queries);
assert_eq!(
vec![
"hello=world".as_bytes().to_vec(),
"test1=test2".as_bytes().to_vec()
],
queries
);
} else {
error!("Parse Queries failed");
}
Expand Down Expand Up @@ -1077,7 +1086,7 @@ mod test {
"/hello",
Method::Get,
None,
None,
vec![],
Some(domain.to_string()),
)
.build(),
Expand Down Expand Up @@ -1108,7 +1117,7 @@ mod test {
"/validate",
Method::Post,
Some(b"world".to_vec()),
None,
vec![],
Some(domain.to_string()),
)
.build(),
Expand Down
12 changes: 6 additions & 6 deletions src/dtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ mod test {
"/hello",
Method::Get,
None,
None,
vec![],
Some(domain.to_string()),
)
.build(),
Expand Down Expand Up @@ -409,7 +409,7 @@ mod test {
"/hello",
Method::Get,
None,
None,
vec![],
Some(domain.to_string()),
)
.build(),
Expand Down Expand Up @@ -478,7 +478,7 @@ mod test {
"/hello",
Method::Get,
None,
None,
vec![],
Some(domain.to_string()),
)
.build(),
Expand Down Expand Up @@ -534,7 +534,7 @@ mod test {
"/hello_dtls",
Method::Get,
None,
None,
vec![],
Some(domain.to_string()),
)
.build(),
Expand Down Expand Up @@ -593,7 +593,7 @@ mod test {
"/hello",
Method::Get,
None,
None,
vec![],
Some(domain.to_string()),
)
.build(),
Expand Down Expand Up @@ -676,7 +676,7 @@ mod test {
"/hello",
Method::Get,
None,
None,
vec![],
Some(domain.to_string()),
)
.build(),
Expand Down
22 changes: 11 additions & 11 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::net::SocketAddr;
use std::{net::{IpAddr, SocketAddr}, str::FromStr};

pub use coap_lite::{
CoapOption, CoapRequest, MessageClass, MessageType, ObserveOption, Packet,
Expand All @@ -10,7 +10,7 @@ pub struct RequestBuilder<'a> {
path: &'a str,
method: Method,
data: Option<Vec<u8>>,
queries: Option<Vec<u8>>,
queries: Vec<Vec<u8>>,
domain: String,
confirmable: bool,
token: Option<Vec<u8>>,
Expand All @@ -22,7 +22,7 @@ impl<'a> RequestBuilder<'a> {
path,
method,
data: None,
queries: None,
queries: vec![],
token: None,
domain: "".to_string(),
confirmable: true,
Expand All @@ -36,7 +36,7 @@ impl<'a> RequestBuilder<'a> {
path: &'a str,
method: Method,
payload: Option<Vec<u8>>,
query: Option<Vec<u8>>,
query: Vec<Vec<u8>>,
domain: Option<String>,
) -> Self {
let new_self = Self::new(path, method);
Expand All @@ -54,7 +54,7 @@ impl<'a> RequestBuilder<'a> {
self
}
/// set the queries of the request.
pub fn queries(mut self, queries: Option<Vec<u8>>) -> Self {
pub fn queries(mut self, queries: Vec<Vec<u8>>) -> Self {
self.queries = queries;
self
}
Expand Down Expand Up @@ -83,14 +83,14 @@ impl<'a> RequestBuilder<'a> {
let mut request = CoapRequest::new();
request.set_method(self.method);
request.set_path(self.path);
if let Some(queries) = self.queries {
request.message.add_option(CoapOption::UriQuery, queries);
for query in self.queries {
request.message.add_option(CoapOption::UriQuery, query);
}
for (opt, opt_data) in self.options {
assert_ne!(opt, CoapOption::UriQuery, "Use queries instead");
request.message.add_option(opt, opt_data);
}
if self.domain.len() != 0 {
if self.domain.len() != 0 && IpAddr::from_str(&self.domain).is_err() {
request.message.add_option(
CoapOption::UriHost,
self.domain.as_str().as_bytes().to_vec(),
Expand Down Expand Up @@ -119,7 +119,7 @@ pub mod test {
"/",
Method::Put,
Some(b"hello, world!".to_vec()),
None,
vec![],
None,
)
.build();
Expand All @@ -131,7 +131,7 @@ pub mod test {
"/",
Method::Put,
None,
None,
vec![],
Some("example.com".to_string()),
)
.build();
Expand All @@ -151,7 +151,7 @@ pub mod test {
"/",
Method::Put,
None,
b"query=hello".to_vec().into(),
vec![b"query=hello".to_vec()],
Some("example.com".to_string()),
)
.options(options)
Expand Down
Loading