diff --git a/src/client.rs b/src/client.rs index dc07d7edf..36bf91b0d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -902,7 +902,7 @@ impl CoAPClient { self.block1_size = block1_max_bytes; } - fn parse_coap_url(url: &str) -> IoResult<(String, u16, String, Option>)> { + fn parse_coap_url(url: &str) -> IoResult<(String, u16, String, Vec>)> { let url_params = match Url::parse(url) { Ok(url_params) => url_params, Err(_) => return Err(Error::new(ErrorKind::InvalidInput, "url error")), @@ -925,7 +925,10 @@ impl CoAPClient { 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)); } @@ -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"); } @@ -1077,7 +1086,7 @@ mod test { "/hello", Method::Get, None, - None, + vec![], Some(domain.to_string()), ) .build(), @@ -1108,7 +1117,7 @@ mod test { "/validate", Method::Post, Some(b"world".to_vec()), - None, + vec![], Some(domain.to_string()), ) .build(), diff --git a/src/dtls.rs b/src/dtls.rs index 1d597aeca..d81c5036d 100644 --- a/src/dtls.rs +++ b/src/dtls.rs @@ -373,7 +373,7 @@ mod test { "/hello", Method::Get, None, - None, + vec![], Some(domain.to_string()), ) .build(), @@ -409,7 +409,7 @@ mod test { "/hello", Method::Get, None, - None, + vec![], Some(domain.to_string()), ) .build(), @@ -478,7 +478,7 @@ mod test { "/hello", Method::Get, None, - None, + vec![], Some(domain.to_string()), ) .build(), @@ -534,7 +534,7 @@ mod test { "/hello_dtls", Method::Get, None, - None, + vec![], Some(domain.to_string()), ) .build(), @@ -593,7 +593,7 @@ mod test { "/hello", Method::Get, None, - None, + vec![], Some(domain.to_string()), ) .build(), @@ -676,7 +676,7 @@ mod test { "/hello", Method::Get, None, - None, + vec![], Some(domain.to_string()), ) .build(), diff --git a/src/request.rs b/src/request.rs index 4180c4a14..d839de986 100644 --- a/src/request.rs +++ b/src/request.rs @@ -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, @@ -10,7 +10,7 @@ pub struct RequestBuilder<'a> { path: &'a str, method: Method, data: Option>, - queries: Option>, + queries: Vec>, domain: String, confirmable: bool, token: Option>, @@ -22,7 +22,7 @@ impl<'a> RequestBuilder<'a> { path, method, data: None, - queries: None, + queries: vec![], token: None, domain: "".to_string(), confirmable: true, @@ -36,7 +36,7 @@ impl<'a> RequestBuilder<'a> { path: &'a str, method: Method, payload: Option>, - query: Option>, + query: Vec>, domain: Option, ) -> Self { let new_self = Self::new(path, method); @@ -54,7 +54,7 @@ impl<'a> RequestBuilder<'a> { self } /// set the queries of the request. - pub fn queries(mut self, queries: Option>) -> Self { + pub fn queries(mut self, queries: Vec>) -> Self { self.queries = queries; self } @@ -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(), @@ -119,7 +119,7 @@ pub mod test { "/", Method::Put, Some(b"hello, world!".to_vec()), - None, + vec![], None, ) .build(); @@ -131,7 +131,7 @@ pub mod test { "/", Method::Put, None, - None, + vec![], Some("example.com".to_string()), ) .build(); @@ -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)