Skip to content

Commit

Permalink
fixup! fix(server): Don't send a copy of the PDU instead of applying …
Browse files Browse the repository at this point in the history
…the response to the original response PDU
  • Loading branch information
pulsastrix committed Nov 28, 2024
1 parent cc0adbc commit 8db7082
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libcoap/src/message/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl CoapResponse {
/// Parses the given [CoapMessage] into a CoapResponse.
///
/// Returns a [MessageConversionError] if the provided PDU cannot be parsed into a response.
pub fn from_message(mut pdu: CoapMessage) -> Result<CoapResponse, MessageConversionError> {
pub fn from_message(pdu: CoapMessage) -> Result<CoapResponse, MessageConversionError> {
let mut location_path = None;
let mut location_query = None;
let mut max_age = None;
Expand Down
4 changes: 2 additions & 2 deletions libcoap/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
mem::{CoapFfiRcCell, DropInnerExclusively},
message::{request::CoapRequest, response::CoapResponse, CoapMessage, CoapMessageCommon},
protocol::{CoapMessageCode, CoapMessageType, CoapRequestCode},
session::{CoapServerSession, CoapSessionCommon},
session::CoapServerSession,
};

// Trait aliases are experimental
Expand Down Expand Up @@ -58,7 +58,7 @@ macro_rules! resource_handler {
if let Ok((mut resource, mut session, incoming_pdu, mut outgoing_pdu)) = handler_data {
($f::<D>)(&mut resource, &mut session, &incoming_pdu, &mut outgoing_pdu);
// TODO error handling
outgoing_pdu.into_message().apply_to_raw_pdu(response_pdu, &session);
let _ = outgoing_pdu.into_message().apply_to_raw_pdu(response_pdu, &session);
}
}
unsafe { CoapRequestHandler::<$t>::from_raw_handler(_coap_method_handler_wrapper::<$t>) }
Expand Down
1 change: 0 additions & 1 deletion libcoap/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::{
use libcoap_rs::{
message::{CoapMessageCommon, CoapRequest, CoapResponse},
protocol::{CoapMessageCode, CoapMessageType, CoapRequestCode, CoapResponseCode},
session::CoapSessionCommon,
CoapContext, CoapRequestHandler, CoapResource,
};
use libcoap_sys::{coap_dtls_set_log_level, coap_log_t, coap_set_log_level};
Expand Down
12 changes: 6 additions & 6 deletions libcoap/tests/dtls_psk_client_server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
#![cfg(feature = "dtls-psk")]
use std::time::Duration;

use libcoap_rs::crypto::psk::PskKey;
use libcoap_rs::crypto::psk::{ClientPskContextBuilder, ServerPskContextBuilder};
use libcoap_rs::session::CoapClientSession;
use libcoap_rs::{
crypto::psk::{ClientPskContextBuilder, PskKey, ServerPskContextBuilder},
message::CoapMessageCommon,
protocol::{CoapMessageCode, CoapResponseCode},
session::CoapSessionCommon,
session::{CoapClientSession, CoapSessionCommon},
CoapContext,
};

Expand All @@ -28,7 +26,7 @@ pub fn dtls_psk_client_server_request() {
let dummy_key = PskKey::new(Some("dtls_test_id"), "dtls_test_key___");
let client_psk_context = ClientPskContextBuilder::new(dummy_key.clone()).build();

let server_handle = common::spawn_test_server(move |mut context| {
let server_handle = common::spawn_test_server(move |mut context, _req_complete| {
let server_psk_context = ServerPskContextBuilder::new(dummy_key.clone()).build();
context.set_psk_context(server_psk_context).unwrap();
context.add_endpoint_dtls(server_address).unwrap();
Expand All @@ -45,7 +43,9 @@ pub fn dtls_psk_client_server_request() {
for response in session.poll_handle(&req_handle) {
assert_eq!(response.code(), CoapMessageCode::Response(CoapResponseCode::Content));
assert_eq!(response.data().unwrap().as_ref(), "Hello World!".as_bytes());
server_handle.join().expect("Test server crashed with failure.");
if let Err(e) = server_handle.join() {
std::panic::resume_unwind(e);
}
return;
}
}
Expand Down
4 changes: 3 additions & 1 deletion libcoap/tests/tcp_client_server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub fn basic_client_server_request() {
for response in session.poll_handle(&req_handle) {
assert_eq!(response.code(), CoapMessageCode::Response(CoapResponseCode::Content));
assert_eq!(response.data().unwrap().as_ref(), "Hello World!".as_bytes());
server_handle.join().unwrap();
if let Err(e) = server_handle.join() {
std::panic::resume_unwind(e);
}
return;
}
}
Expand Down
4 changes: 3 additions & 1 deletion libcoap/tests/udp_client_server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub fn basic_client_server_request() {
for response in session.poll_handle(&req_handle) {
assert_eq!(response.code(), CoapMessageCode::Response(CoapResponseCode::Content));
assert_eq!(response.data().unwrap().as_ref(), "Hello World!".as_bytes());
server_handle.join().unwrap();
if let Err(e) = server_handle.join() {
std::panic::resume_unwind(e);
}
return;
}
}
Expand Down
5 changes: 3 additions & 2 deletions libcoap/tests/udp_observe_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub fn observe_client_server_request() {
context.add_endpoint_udp(server_address).unwrap();

let observe_resource = CoapResource::new("observe-test", (0u8, request_complete), true);
let observe_resource_clone = observe_resource.clone();
observe_resource.set_get_observable(true);
observe_resource.set_method_handler(
CoapRequestCode::Get,
Expand Down Expand Up @@ -81,7 +80,9 @@ pub fn observe_client_server_request() {
assert_eq!(response.code(), CoapMessageCode::Response(CoapResponseCode::Content));
assert_eq!(response.data().unwrap().as_ref(), [expected_request_response]);
if expected_request_response != 0u8 {
server_handle.join().unwrap();
if let Err(e) = server_handle.join() {
std::panic::resume_unwind(e);
}
return;
}
if update_request_handle.is_none() {
Expand Down

0 comments on commit 8db7082

Please sign in to comment.