4
4
// option. This file may not be copied, modified, or distributed
5
5
// except according to those terms.
6
6
7
+ #![ allow( clippy:: unwrap_used) ] // This is example code.
8
+
7
9
//! An HTTP 3 client implementation.
8
10
9
11
use std:: {
@@ -18,7 +20,7 @@ use std::{
18
20
time:: Instant ,
19
21
} ;
20
22
21
- use neqo_common:: { event:: Provider , hex, qdebug, qinfo, qwarn, Datagram , Header } ;
23
+ use neqo_common:: { event:: Provider , hex, qdebug, qerror , qinfo, qwarn, Datagram , Header } ;
22
24
use neqo_crypto:: { AuthenticationStatus , ResumptionToken } ;
23
25
use neqo_http3:: { Error , Http3Client , Http3ClientEvent , Http3Parameters , Http3State , Priority } ;
24
26
use neqo_transport:: {
@@ -200,9 +202,11 @@ impl super::Handler for Handler<'_> {
200
202
qwarn ! ( "Data on unexpected stream: {stream_id}" ) ;
201
203
}
202
204
Some ( handler) => loop {
203
- let ( sz, fin) = client
204
- . read_data ( Instant :: now ( ) , stream_id, & mut self . read_buffer )
205
- . expect ( "Read should succeed" ) ;
205
+ let ( sz, fin) = client. read_data (
206
+ Instant :: now ( ) ,
207
+ stream_id,
208
+ & mut self . read_buffer ,
209
+ ) ?;
206
210
207
211
handler. process_data_readable (
208
212
stream_id,
@@ -270,7 +274,7 @@ trait StreamHandler {
270
274
fin : bool ,
271
275
data : & [ u8 ] ,
272
276
output_read_data : bool ,
273
- ) -> Res < bool > ;
277
+ ) -> Res < ( ) > ;
274
278
fn process_data_writable ( & mut self , client : & mut Http3Client , stream_id : StreamId ) ;
275
279
}
276
280
@@ -291,12 +295,12 @@ impl StreamHandler for DownloadStreamHandler {
291
295
fin : bool ,
292
296
data : & [ u8 ] ,
293
297
output_read_data : bool ,
294
- ) -> Res < bool > {
298
+ ) -> Res < ( ) > {
295
299
if let Some ( out_file) = & mut self . out_file {
296
300
if !data. is_empty ( ) {
297
301
out_file. write_all ( data) ?;
298
302
}
299
- return Ok ( true ) ;
303
+ return Ok ( ( ) ) ;
300
304
} else if !output_read_data {
301
305
qdebug ! ( "READ[{stream_id}]: {} bytes" , data. len( ) ) ;
302
306
} else if let Ok ( txt) = std:: str:: from_utf8 ( data) {
@@ -313,7 +317,7 @@ impl StreamHandler for DownloadStreamHandler {
313
317
}
314
318
}
315
319
316
- Ok ( true )
320
+ Ok ( ( ) )
317
321
}
318
322
319
323
fn process_data_writable ( & mut self , _client : & mut Http3Client , _stream_id : StreamId ) { }
@@ -335,18 +339,19 @@ impl StreamHandler for UploadStreamHandler {
335
339
_fin : bool ,
336
340
data : & [ u8 ] ,
337
341
_output_read_data : bool ,
338
- ) -> Res < bool > {
342
+ ) -> Res < ( ) > {
339
343
if let Ok ( txt) = std:: str:: from_utf8 ( data) {
340
344
let trimmed_txt = txt. trim_end_matches ( char:: from ( 0 ) ) ;
341
- let parsed: usize = trimmed_txt. parse ( ) . unwrap ( ) ;
345
+ let parsed: usize = trimmed_txt. parse ( ) . map_err ( |_| Error :: InvalidInput ) ? ;
342
346
if parsed == self . data . len ( ) {
343
347
let upload_time = Instant :: now ( ) . duration_since ( self . start ) ;
344
348
qinfo ! ( "Stream ID: {stream_id:?}, Upload time: {upload_time:?}" ) ;
345
349
}
350
+ Ok ( ( ) )
346
351
} else {
347
- panic ! ( "Unexpected data [{}]: 0x{}" , stream_id, hex( data) ) ;
352
+ qerror ! ( "Unexpected data [{}]: 0x{}" , stream_id, hex( data) ) ;
353
+ Err ( crate :: client:: Error :: Http3Error ( Error :: InvalidInput ) )
348
354
}
349
- Ok ( true )
350
355
}
351
356
352
357
fn process_data_writable ( & mut self , client : & mut Http3Client , stream_id : StreamId ) {
0 commit comments