@@ -63,6 +63,13 @@ where I: AsyncRead + AsyncWrite,
63
63
}
64
64
}
65
65
66
+ fn can_write_continue ( & self ) -> bool {
67
+ match self . state . writing {
68
+ Writing :: Continue ( ..) => true ,
69
+ _ => false ,
70
+ }
71
+ }
72
+
66
73
fn can_read_body ( & self ) -> bool {
67
74
match self . state . reading {
68
75
Reading :: Body ( ..) => true ,
@@ -106,6 +113,10 @@ where I: AsyncRead + AsyncWrite,
106
113
}
107
114
} ;
108
115
self . state . busy ( ) ;
116
+ if head. expecting_continue ( ) {
117
+ let msg = b"HTTP/1.1 100 Continue\r \n \r \n " ;
118
+ self . state . writing = Writing :: Continue ( Cursor :: new ( msg) ) ;
119
+ }
109
120
let wants_keep_alive = head. should_keep_alive ( ) ;
110
121
self . state . keep_alive &= wants_keep_alive;
111
122
let ( body, reading) = if decoder. is_eof ( ) {
@@ -173,6 +184,7 @@ where I: AsyncRead + AsyncWrite,
173
184
}
174
185
175
186
match self . state . writing {
187
+ Writing :: Continue ( ..) |
176
188
Writing :: Body ( ..) |
177
189
Writing :: Ending ( ..) => return ,
178
190
Writing :: Init |
@@ -192,14 +204,15 @@ where I: AsyncRead + AsyncWrite,
192
204
193
205
fn can_write_head ( & self ) -> bool {
194
206
match self . state . writing {
195
- Writing :: Init => true ,
207
+ Writing :: Continue ( .. ) | Writing :: Init => true ,
196
208
_ => false
197
209
}
198
210
}
199
211
200
212
fn can_write_body ( & self ) -> bool {
201
213
match self . state . writing {
202
214
Writing :: Body ( ..) => true ,
215
+ Writing :: Continue ( ..) |
203
216
Writing :: Init |
204
217
Writing :: Ending ( ..) |
205
218
Writing :: KeepAlive |
@@ -229,6 +242,13 @@ where I: AsyncRead + AsyncWrite,
229
242
let wants_keep_alive = head. should_keep_alive ( ) ;
230
243
self . state . keep_alive &= wants_keep_alive;
231
244
let mut buf = Vec :: new ( ) ;
245
+ // if a 100-continue has started but not finished sending, tack the
246
+ // remainder on to the start of the buffer.
247
+ if let Writing :: Continue ( ref pending) = self . state . writing {
248
+ if pending. has_started ( ) {
249
+ buf. extend_from_slice ( pending. buf ( ) ) ;
250
+ }
251
+ }
232
252
let encoder = T :: encode ( head, & mut buf) ;
233
253
//TODO: handle when there isn't enough room to buffer the head
234
254
assert ! ( self . io. buffer( buf) > 0 ) ;
@@ -296,6 +316,15 @@ where I: AsyncRead + AsyncWrite,
296
316
fn write_queued ( & mut self ) -> Poll < ( ) , io:: Error > {
297
317
trace ! ( "Conn::write_queued()" ) ;
298
318
let state = match self . state . writing {
319
+ Writing :: Continue ( ref mut queued) => {
320
+ let n = self . io . buffer ( queued. buf ( ) ) ;
321
+ queued. consume ( n) ;
322
+ if queued. is_written ( ) {
323
+ Writing :: Init
324
+ } else {
325
+ return Ok ( Async :: NotReady ) ;
326
+ }
327
+ }
299
328
Writing :: Body ( ref mut encoder, ref mut queued) => {
300
329
let complete = if let Some ( chunk) = queued. as_mut ( ) {
301
330
let n = try_nb ! ( encoder. encode( & mut self . io, chunk. buf( ) ) ) ;
@@ -355,24 +384,28 @@ where I: AsyncRead + AsyncWrite,
355
384
trace ! ( "Conn::poll()" ) ;
356
385
self . state . read_task . take ( ) ;
357
386
358
- if self . is_read_closed ( ) {
359
- trace ! ( "Conn::poll when closed" ) ;
360
- Ok ( Async :: Ready ( None ) )
361
- } else if self . can_read_head ( ) {
362
- self . read_head ( )
363
- } else if self . can_read_body ( ) {
364
- self . read_body ( )
365
- . map ( |async| async . map ( |chunk| Some ( Frame :: Body {
366
- chunk : chunk
367
- } ) ) )
368
- . or_else ( |err| {
369
- self . state . close_read ( ) ;
370
- Ok ( Async :: Ready ( Some ( Frame :: Error { error : err. into ( ) } ) ) )
371
- } )
372
- } else {
373
- trace ! ( "poll when on keep-alive" ) ;
374
- self . maybe_park_read ( ) ;
375
- Ok ( Async :: NotReady )
387
+ loop {
388
+ if self . is_read_closed ( ) {
389
+ trace ! ( "Conn::poll when closed" ) ;
390
+ return Ok ( Async :: Ready ( None ) ) ;
391
+ } else if self . can_read_head ( ) {
392
+ return self . read_head ( ) ;
393
+ } else if self . can_write_continue ( ) {
394
+ try_nb ! ( self . flush( ) ) ;
395
+ } else if self . can_read_body ( ) {
396
+ return self . read_body ( )
397
+ . map ( |async| async . map ( |chunk| Some ( Frame :: Body {
398
+ chunk : chunk
399
+ } ) ) )
400
+ . or_else ( |err| {
401
+ self . state . close_read ( ) ;
402
+ Ok ( Async :: Ready ( Some ( Frame :: Error { error : err. into ( ) } ) ) )
403
+ } ) ;
404
+ } else {
405
+ trace ! ( "poll when on keep-alive" ) ;
406
+ self . maybe_park_read ( ) ;
407
+ return Ok ( Async :: NotReady ) ;
408
+ }
376
409
}
377
410
}
378
411
}
@@ -482,6 +515,7 @@ enum Reading {
482
515
}
483
516
484
517
enum Writing < B > {
518
+ Continue ( Cursor < & ' static [ u8 ] > ) ,
485
519
Init ,
486
520
Body ( Encoder , Option < Cursor < B > > ) ,
487
521
Ending ( Cursor < & ' static [ u8 ] > ) ,
@@ -503,6 +537,9 @@ impl<B: AsRef<[u8]>, K: fmt::Debug> fmt::Debug for State<B, K> {
503
537
impl < B : AsRef < [ u8 ] > > fmt:: Debug for Writing < B > {
504
538
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
505
539
match * self {
540
+ Writing :: Continue ( ref buf) => f. debug_tuple ( "Continue" )
541
+ . field ( buf)
542
+ . finish ( ) ,
506
543
Writing :: Init => f. write_str ( "Init" ) ,
507
544
Writing :: Body ( ref enc, ref queued) => f. debug_tuple ( "Body" )
508
545
. field ( enc)
0 commit comments