@@ -170,17 +170,11 @@ impl<'a> Forwarded<'a> {
170
170
input = forwarded. parse_for ( input) ?;
171
171
}
172
172
173
- loop {
174
- match input. chars ( ) . next ( ) {
175
- Some ( ';' ) => {
176
- input = & input[ 1 ..] ;
177
- }
178
- None => return Ok ( forwarded) ,
179
- _ => return Err ( ParseError :: new ( "unexpected character after forwarded-pair" ) ) ,
180
- }
181
-
173
+ while !input. is_empty ( ) {
182
174
input = forwarded. parse_forwarded_pair ( input) ?;
183
175
}
176
+
177
+ Ok ( forwarded)
184
178
}
185
179
186
180
fn parse_forwarded_pair ( & mut self , input : & ' a str ) -> Result < & ' a str , ParseError > {
@@ -218,7 +212,11 @@ impl<'a> Forwarded<'a> {
218
212
_ => { /* extensions are allowed in the spec */ }
219
213
}
220
214
221
- Ok ( rest)
215
+ if rest. starts_with ( ';' ) {
216
+ Ok ( & rest[ 1 ..] )
217
+ } else {
218
+ Ok ( rest)
219
+ }
222
220
}
223
221
224
222
fn parse_for ( & mut self , input : & ' a str ) -> Result < & ' a str , ParseError > {
@@ -246,8 +244,13 @@ impl<'a> Forwarded<'a> {
246
244
rest = rest[ 1 ..] . trim_start ( ) ;
247
245
}
248
246
247
+ Some ( ';' ) => {
248
+ rest = & rest[ 1 ..] ;
249
+ break ;
250
+ }
251
+
249
252
// reached the end of the for section or the input
250
- None | Some ( ';' ) => break ,
253
+ None => break ,
251
254
252
255
// bail
253
256
_ => return Err ( ParseError :: new ( "unexpected character after for= section" ) ) ,
@@ -537,13 +540,13 @@ mod tests {
537
540
let err = Forwarded :: parse ( "by=proxy.com;for=client;host=example.com;host" ) . unwrap_err ( ) ;
538
541
assert_eq ! (
539
542
err. to_string( ) ,
540
- "unable to parse forwarded header: unexpected character after forwarded-pair"
543
+ "unable to parse forwarded header: parse error in forwarded-pair"
541
544
) ;
542
545
543
546
let err = Forwarded :: parse ( "by;for;host;proto" ) . unwrap_err ( ) ;
544
547
assert_eq ! (
545
548
err. to_string( ) ,
546
- "unable to parse forwarded header: unexpected character after forwarded-pair"
549
+ "unable to parse forwarded header: parse error in forwarded-pair"
547
550
) ;
548
551
549
552
let err = Forwarded :: parse ( "for=for, key=value" ) . unwrap_err ( ) ;
@@ -571,7 +574,7 @@ mod tests {
571
574
response. append_header ( "forwarded" , "uh oh" ) ;
572
575
assert_eq ! (
573
576
Forwarded :: from_headers( & response) . unwrap_err( ) . to_string( ) ,
574
- "unable to parse forwarded header: unexpected character after forwarded-pair"
577
+ "unable to parse forwarded header: parse error in forwarded-pair"
575
578
) ;
576
579
577
580
let response = Response :: new ( 200 ) ;
@@ -609,6 +612,9 @@ mod tests {
609
612
assert_eq ! ( forwarded. forwarded_for( ) , vec![ ";" , "," , "\" " , "unquoted" ] ) ;
610
613
assert_eq ! ( forwarded. by( ) , Some ( ";proto=https" ) ) ;
611
614
assert ! ( forwarded. proto( ) . is_none( ) ) ;
615
+
616
+ let forwarded = Forwarded :: parse ( "proto=https" ) . unwrap ( ) ;
617
+ assert_eq ! ( forwarded. proto( ) , Some ( "https" ) ) ;
612
618
}
613
619
614
620
#[ test]
0 commit comments