Skip to content

Commit 792d9db

Browse files
committed
Adding debugging for restwrapper test
1 parent 5d1604d commit 792d9db

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

eventsource-client/src/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ where
397397
loop {
398398
let this = self.as_mut().project();
399399
if let Some(event) = this.event_parser.get_event() {
400+
trace!("We got an event {:?}", event);
400401
return match event {
401402
SSE::Event(ref evt) => {
402403
*this.last_event_id = evt.id.clone();

eventsource-client/src/event_parser.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use pin_project::pin_project;
66

77
use super::error::{Error, Result};
88

9-
#[derive(Default, PartialEq)]
9+
#[derive(Default, PartialEq, Debug)]
1010
struct EventData {
1111
pub event_type: String,
1212
pub data: String,
@@ -83,17 +83,19 @@ pub struct Event {
8383
pub retry: Option<u64>,
8484
}
8585

86-
const LOGIFY_MAX_CHARS: usize = 100;
86+
// const LOGIFY_MAX_CHARS: usize = 100;
8787
fn logify(bytes: &[u8]) -> &str {
8888
let stringified = from_utf8(bytes).unwrap_or("<bad utf8>");
89-
if stringified.len() <= LOGIFY_MAX_CHARS {
90-
stringified
91-
} else {
92-
&stringified[..LOGIFY_MAX_CHARS - 1]
93-
}
89+
stringified
90+
// if stringified.len() <= LOGIFY_MAX_CHARS {
91+
// stringified
92+
// } else {
93+
// &stringified[..LOGIFY_MAX_CHARS - 1]
94+
// }
9495
}
9596

9697
fn parse_field(line: &[u8]) -> Result<Option<(&str, &str)>> {
98+
trace!("Parse field {:?}", logify(line));
9799
if line.is_empty() {
98100
return Err(Error::InvalidLine(
99101
"should never try to parse an empty line (probably a bug)".into(),
@@ -125,10 +127,12 @@ fn parse_field(line: &[u8]) -> Result<Option<(&str, &str)>> {
125127
}
126128

127129
fn parse_key(key: &[u8]) -> Result<&str> {
130+
trace!("Parse key {:?}", logify(key));
128131
from_utf8(key).map_err(|e| Error::InvalidLine(format!("malformed key: {:?}", e)))
129132
}
130133

131134
fn parse_value(value: &[u8]) -> Result<&str> {
135+
trace!("Parse value {:?}", logify(value));
132136
from_utf8(value).map_err(|e| Error::InvalidLine(format!("malformed value: {:?}", e)))
133137
}
134138

@@ -199,10 +203,18 @@ impl EventParser {
199203
//
200204
// Returns the event for dispatch if it is complete.
201205
fn parse_complete_lines_into_event(&mut self) -> Result<()> {
206+
trace!(
207+
"Preparing to parse_complete_lines_into_event {:?} {:?}",
208+
self.event_data,
209+
self.last_event_id
210+
);
211+
202212
loop {
213+
trace!("loop");
203214
let mut seen_empty_line = false;
204215

205216
while let Some(line) = self.complete_lines.pop_front() {
217+
trace!("line {:?}", line);
206218
if line.is_empty() && self.event_data.is_some() {
207219
seen_empty_line = true;
208220
break;
@@ -211,6 +223,8 @@ impl EventParser {
211223
}
212224

213225
if let Some((key, value)) = parse_field(&line)? {
226+
trace!("key {:}, value {:?}", key, value);
227+
214228
let id = &self.last_event_id;
215229
let event_data = self
216230
.event_data
@@ -259,6 +273,7 @@ impl EventParser {
259273
);
260274

261275
if let Some(event_data) = event_data {
276+
trace!("event_data {:?}", event_data);
262277
match Option::<SSE>::try_from(event_data) {
263278
Err(e) => return Err(e),
264279
Ok(None) => (),
@@ -281,13 +296,15 @@ impl EventParser {
281296
// incomplete lines from previous chunks.
282297
fn decode_and_buffer_lines(&mut self, chunk: Bytes) {
283298
let mut lines = chunk.split_inclusive(|&b| b == b'\n' || b == b'\r');
299+
trace!("decode_and_buffer_lines {:?}", lines);
284300
// The first and last elements in this split are special. The spec requires lines to be
285301
// terminated. But lines may span chunks, so:
286302
// * the last line, if non-empty (i.e. if chunk didn't end with a line terminator),
287303
// should be buffered as an incomplete line
288304
// * the first line should be appended to the incomplete line, if any
289305

290306
if let Some(incomplete_line) = self.incomplete_line.as_mut() {
307+
trace!("incomplete_line {:?}", incomplete_line);
291308
if let Some(line) = lines.next() {
292309
trace!(
293310
"extending line from previous chunk: {:?}+{:?}",
@@ -301,24 +318,31 @@ impl EventParser {
301318
// terminator, but also where the entire line is a terminator.
302319
match line.last().unwrap() {
303320
b'\r' => {
321+
trace!("\\r branch");
304322
incomplete_line.extend_from_slice(&line[..line.len() - 1]);
305323
let il = self.incomplete_line.take();
306324
self.complete_lines.push_back(il.unwrap());
307325
self.last_char_was_cr = true;
308326
}
309327
b'\n' => {
328+
trace!("\\n branch");
310329
incomplete_line.extend_from_slice(&line[..line.len() - 1]);
311330
let il = self.incomplete_line.take();
312331
self.complete_lines.push_back(il.unwrap());
313332
}
314-
_ => incomplete_line.extend_from_slice(line),
333+
_ => {
334+
trace!("other branch");
335+
incomplete_line.extend_from_slice(line);
336+
}
315337
};
316338
}
317339
}
318340
}
319341

320342
let mut lines = lines.peekable();
343+
trace!("lines {:?}", lines);
321344
while let Some(line) = lines.next() {
345+
trace!("line {:?}", line);
322346
if let Some(actually_complete_line) = self.incomplete_line.take() {
323347
// we saw the next line, so the previous one must have been complete after all
324348
trace!(

0 commit comments

Comments
 (0)