@@ -17,48 +17,48 @@ pub mod ascii {
17
17
///Errors associated with a header that is invalid.
18
18
#[ derive( Debug , PartialEq ) ]
19
19
pub enum HttpHeaderError {
20
+ /// The header is misformatted.
21
+ InvalidFormat ( String ) ,
22
+ /// The specified header contains illegal characters.
23
+ InvalidUtf8String ( Utf8Error ) ,
24
+ ///The value specified is not valid.
25
+ InvalidValue ( String , String ) ,
20
26
/// The content length specified is longer than the limit imposed by Micro Http.
21
27
SizeLimitExceeded ( String ) ,
28
+ /// The requested feature is not currently supported.
29
+ UnsupportedFeature ( String , String ) ,
22
30
/// The header specified is not supported.
23
31
UnsupportedName ( String ) ,
24
32
/// The value for the specified header is not supported.
25
33
UnsupportedValue ( String , String ) ,
26
- /// The specified header contains illegal characters.
27
- InvalidUtf8String ( Utf8Error ) ,
28
- /// The requested feature is not currently supported.
29
- UnsupportedFeature ( String , String ) ,
30
- /// The header is misformatted.
31
- InvalidFormat ( String ) ,
32
- ///The value specified is not valid.
33
- InvalidValue ( String , String ) ,
34
34
}
35
35
36
36
impl Display for HttpHeaderError {
37
37
fn fmt ( & self , f : & mut Formatter ) -> Result < ( ) , Error > {
38
38
match self {
39
- Self :: SizeLimitExceeded ( inner ) => {
40
- write ! ( f, "Invalid content length. Header : {}" , inner )
39
+ Self :: InvalidFormat ( header_key ) => {
40
+ write ! ( f, "Header is incorrectly formatted. Key : {}" , header_key )
41
41
}
42
- Self :: UnsupportedName ( inner) => write ! ( f, "Unsupported header name. Key: {}" , inner) ,
43
- Self :: UnsupportedValue ( header_key, header_value) => write ! (
44
- f,
45
- "Unsupported value. Key:{}; Value:{}" ,
46
- header_key, header_value
47
- ) ,
48
42
Self :: InvalidUtf8String ( header_key) => {
49
43
write ! ( f, "Header contains invalid characters. Key: {}" , header_key)
50
44
}
45
+ Self :: InvalidValue ( header_name, value) => {
46
+ write ! ( f, "Invalid value. Key:{}; Value:{}" , header_name, value)
47
+ }
48
+ Self :: SizeLimitExceeded ( inner) => {
49
+ write ! ( f, "Invalid content length. Header: {}" , inner)
50
+ }
51
51
Self :: UnsupportedFeature ( header_key, header_value) => write ! (
52
52
f,
53
53
"Unsupported feature. Key: {}; Value: {}" ,
54
54
header_key, header_value
55
55
) ,
56
- Self :: InvalidFormat ( header_key ) => {
57
- write ! ( f , "Header is incorrectly formatted. Key: {}" , header_key )
58
- }
59
- Self :: InvalidValue ( header_name , value) => {
60
- write ! ( f , "Invalid value. Key:{}; Value:{}" , header_name , value )
61
- }
56
+ Self :: UnsupportedName ( inner ) => write ! ( f , "Unsupported header name. Key: {}" , inner ) ,
57
+ Self :: UnsupportedValue ( header_key , header_value ) => write ! (
58
+ f ,
59
+ "Unsupported value. Key:{}; Value:{}" ,
60
+ header_key , header_value
61
+ ) ,
62
62
}
63
63
}
64
64
}
@@ -68,18 +68,18 @@ impl Display for HttpHeaderError {
68
68
pub enum RequestError {
69
69
/// No request was pending while the request body was being parsed.
70
70
BodyWithoutPendingRequest ,
71
+ /// Header specified is either invalid or not supported by this HTTP implementation.
72
+ HeaderError ( HttpHeaderError ) ,
71
73
/// No request was pending while the request headers were being parsed.
72
74
HeadersWithoutPendingRequest ,
73
75
/// The HTTP Method is not supported or it is invalid.
74
76
InvalidHttpMethod ( & ' static str ) ,
75
- /// Request URI is invalid.
76
- InvalidUri ( & ' static str ) ,
77
77
/// The HTTP Version in the Request is not supported or it is invalid.
78
78
InvalidHttpVersion ( & ' static str ) ,
79
- /// Header specified is either invalid or not supported by this HTTP implementation.
80
- HeaderError ( HttpHeaderError ) ,
81
79
/// The Request is invalid and cannot be served.
82
80
InvalidRequest ,
81
+ /// Request URI is invalid.
82
+ InvalidUri ( & ' static str ) ,
83
83
/// Overflow occurred when parsing a request.
84
84
Overflow ,
85
85
/// Underflow occurred when parsing a request.
@@ -93,15 +93,15 @@ impl Display for RequestError {
93
93
f,
94
94
"No request was pending while the request body was being parsed."
95
95
) ,
96
+ Self :: HeaderError ( inner) => write ! ( f, "Invalid header. Reason: {}" , inner) ,
96
97
Self :: HeadersWithoutPendingRequest => write ! (
97
98
f,
98
99
"No request was pending while the request headers were being parsed."
99
100
) ,
100
101
Self :: InvalidHttpMethod ( inner) => write ! ( f, "Invalid HTTP Method: {}" , inner) ,
101
- Self :: InvalidUri ( inner) => write ! ( f, "Invalid URI: {}" , inner) ,
102
102
Self :: InvalidHttpVersion ( inner) => write ! ( f, "Invalid HTTP Version: {}" , inner) ,
103
- Self :: HeaderError ( inner) => write ! ( f, "Invalid header. Reason: {}" , inner) ,
104
103
Self :: InvalidRequest => write ! ( f, "Invalid request." ) ,
104
+ Self :: InvalidUri ( inner) => write ! ( f, "Invalid URI: {}" , inner) ,
105
105
Self :: Overflow => write ! ( f, "Overflow occurred when parsing a request." ) ,
106
106
Self :: Underflow => write ! ( f, "Underflow occurred when parsing a request." ) ,
107
107
}
@@ -111,23 +111,23 @@ impl Display for RequestError {
111
111
/// Errors associated with a HTTP Connection.
112
112
#[ derive( Debug ) ]
113
113
pub enum ConnectionError {
114
- /// The request parsing has failed.
115
- ParseError ( RequestError ) ,
116
- /// Could not perform a stream operation successfully.
117
- StreamError ( std:: io:: Error ) ,
118
114
/// Attempted to read or write on a closed connection.
119
115
ConnectionClosed ,
120
116
/// Attempted to write on a stream when there was nothing to write.
121
117
InvalidWrite ,
118
+ /// The request parsing has failed.
119
+ ParseError ( RequestError ) ,
120
+ /// Could not perform a stream operation successfully.
121
+ StreamError ( std:: io:: Error ) ,
122
122
}
123
123
124
124
impl Display for ConnectionError {
125
125
fn fmt ( & self , f : & mut Formatter ) -> Result < ( ) , Error > {
126
126
match self {
127
- Self :: ParseError ( inner) => write ! ( f, "Parsing error: {}" , inner) ,
128
- Self :: StreamError ( inner) => write ! ( f, "Stream error: {}" , inner) ,
129
127
Self :: ConnectionClosed => write ! ( f, "Connection closed." ) ,
130
128
Self :: InvalidWrite => write ! ( f, "Invalid write attempt." ) ,
129
+ Self :: ParseError ( inner) => write ! ( f, "Parsing error: {}" , inner) ,
130
+ Self :: StreamError ( inner) => write ! ( f, "Stream error: {}" , inner) ,
131
131
}
132
132
}
133
133
}
@@ -151,25 +151,25 @@ impl Display for RouteError {
151
151
/// Errors pertaining to `HttpServer`.
152
152
#[ derive( Debug ) ]
153
153
pub enum ServerError {
154
- /// Epoll operations failed.
155
- IOError ( std:: io:: Error ) ,
156
154
/// Error from one of the connections.
157
155
ConnectionError ( ConnectionError ) ,
158
- /// Server maximum capacity has been reached .
159
- ServerFull ,
156
+ /// Epoll operations failed .
157
+ IOError ( std :: io :: Error ) ,
160
158
/// Overflow occured while processing messages.
161
159
Overflow ,
160
+ /// Server maximum capacity has been reached.
161
+ ServerFull ,
162
162
/// Underflow occured while processing mesagges.
163
163
Underflow ,
164
164
}
165
165
166
166
impl Display for ServerError {
167
167
fn fmt ( & self , f : & mut Formatter ) -> Result < ( ) , Error > {
168
168
match self {
169
- Self :: IOError ( inner) => write ! ( f, "IO error: {}" , inner) ,
170
169
Self :: ConnectionError ( inner) => write ! ( f, "Connection error: {}" , inner) ,
171
- Self :: ServerFull => write ! ( f, "Server is full." ) ,
170
+ Self :: IOError ( inner ) => write ! ( f, "IO error: {}" , inner ) ,
172
171
Self :: Overflow => write ! ( f, "Overflow occured while processing messages." ) ,
172
+ Self :: ServerFull => write ! ( f, "Server is full." ) ,
173
173
Self :: Underflow => write ! ( f, "Underflow occured while processing messages." ) ,
174
174
}
175
175
}
@@ -419,36 +419,26 @@ mod tests {
419
419
assert_eq ! (
420
420
format!(
421
421
"{}" ,
422
- RequestError :: HeaderError ( HttpHeaderError :: SizeLimitExceeded ( "test" . to_string( ) ) )
423
- ) ,
424
- "Invalid header. Reason: Invalid content length. Header: test"
425
- ) ;
426
- assert_eq ! (
427
- format!(
428
- "{}" ,
429
- RequestError :: HeaderError ( HttpHeaderError :: UnsupportedName ( "test" . to_string( ) ) )
422
+ RequestError :: HeaderError ( HttpHeaderError :: InvalidFormat ( "test" . to_string( ) ) )
430
423
) ,
431
- "Invalid header. Reason: Unsupported header name . Key: test"
424
+ "Invalid header. Reason: Header is incorrectly formatted . Key: test"
432
425
) ;
426
+ let value = String :: from_utf8 ( vec ! [ 0 , 159 ] ) ;
433
427
assert_eq ! (
434
428
format!(
435
429
"{}" ,
436
- RequestError :: HeaderError ( HttpHeaderError :: UnsupportedValue (
437
- "test" . to_string( ) ,
438
- "test" . to_string( )
430
+ RequestError :: HeaderError ( HttpHeaderError :: InvalidUtf8String (
431
+ value. unwrap_err( ) . utf8_error( )
439
432
) )
440
433
) ,
441
- "Invalid header. Reason: Unsupported value . Key:test; Value:test "
434
+ "Invalid header. Reason: Header contains invalid characters . Key: invalid utf-8 sequence of 1 bytes from index 1 "
442
435
) ;
443
- let value = String :: from_utf8 ( vec ! [ 0 , 159 ] ) ;
444
436
assert_eq ! (
445
437
format!(
446
438
"{}" ,
447
- RequestError :: HeaderError ( HttpHeaderError :: InvalidUtf8String (
448
- value. unwrap_err( ) . utf8_error( )
449
- ) )
439
+ RequestError :: HeaderError ( HttpHeaderError :: SizeLimitExceeded ( "test" . to_string( ) ) )
450
440
) ,
451
- "Invalid header. Reason: Header contains invalid characters. Key: invalid utf-8 sequence of 1 bytes from index 1 "
441
+ "Invalid header. Reason: Invalid content length. Header: test "
452
442
) ;
453
443
assert_eq ! (
454
444
format!(
@@ -463,36 +453,46 @@ mod tests {
463
453
assert_eq ! (
464
454
format!(
465
455
"{}" ,
466
- RequestError :: HeaderError ( HttpHeaderError :: InvalidFormat ( "test" . to_string( ) ) )
456
+ RequestError :: HeaderError ( HttpHeaderError :: UnsupportedName ( "test" . to_string( ) ) )
467
457
) ,
468
- "Invalid header. Reason: Header is incorrectly formatted. Key: test"
458
+ "Invalid header. Reason: Unsupported header name. Key: test"
459
+ ) ;
460
+ assert_eq ! (
461
+ format!(
462
+ "{}" ,
463
+ RequestError :: HeaderError ( HttpHeaderError :: UnsupportedValue (
464
+ "test" . to_string( ) ,
465
+ "test" . to_string( )
466
+ ) )
467
+ ) ,
468
+ "Invalid header. Reason: Unsupported value. Key:test; Value:test"
469
469
) ;
470
470
}
471
471
472
472
#[ test]
473
473
fn test_display_connection_error ( ) {
474
+ assert_eq ! (
475
+ format!( "{}" , ConnectionError :: ConnectionClosed ) ,
476
+ "Connection closed."
477
+ ) ;
474
478
assert_eq ! (
475
479
format!(
476
480
"{}" ,
477
481
ConnectionError :: ParseError ( RequestError :: InvalidRequest )
478
482
) ,
479
483
"Parsing error: Invalid request."
480
484
) ;
485
+ assert_eq ! (
486
+ format!( "{}" , ConnectionError :: InvalidWrite ) ,
487
+ "Invalid write attempt."
488
+ ) ;
481
489
assert_eq ! (
482
490
format!(
483
491
"{}" ,
484
492
ConnectionError :: StreamError ( std:: io:: Error :: from_raw_os_error( 11 ) )
485
493
) ,
486
494
"Stream error: Resource temporarily unavailable (os error 11)"
487
495
) ;
488
- assert_eq ! (
489
- format!( "{}" , ConnectionError :: ConnectionClosed ) ,
490
- "Connection closed."
491
- ) ;
492
- assert_eq ! (
493
- format!( "{}" , ConnectionError :: InvalidWrite ) ,
494
- "Invalid write attempt."
495
- ) ;
496
496
}
497
497
498
498
#[ test]
@@ -504,7 +504,6 @@ mod tests {
504
504
) ,
505
505
"Connection error: Connection closed."
506
506
) ;
507
- assert_eq ! ( format!( "{}" , ServerError :: ServerFull ) , "Server is full." ) ;
508
507
assert_eq ! (
509
508
format!(
510
509
"{}" ,
@@ -516,6 +515,7 @@ mod tests {
516
515
format!( "{}" , ServerError :: Overflow ) ,
517
516
"Overflow occured while processing messages."
518
517
) ;
518
+ assert_eq ! ( format!( "{}" , ServerError :: ServerFull ) , "Server is full." ) ;
519
519
assert_eq ! (
520
520
format!( "{}" , ServerError :: Underflow ) ,
521
521
"Underflow occured while processing messages."
0 commit comments