1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_HTTP_HTTP_RESPONSE_BODY_DRAINER_H_
6 #define NET_HTTP_HTTP_RESPONSE_BODY_DRAINER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/timer/timer.h"
12 #include "net/base/completion_callback.h"
13 #include "net/base/net_export.h"
14 #include "net/http/http_network_session.h"
21 class NET_EXPORT_PRIVATE HttpResponseBodyDrainer
{
23 // The size in bytes of the buffer we use to drain the response body that
24 // we want to throw away. The response body is typically a small page just a
25 // few hundred bytes long. We set a limit to prevent it from taking too long,
26 // since we may as well just create a new socket then.
27 static const int kDrainBodyBufferSize
= 16384;
28 static const int kTimeoutInSeconds
= 5;
30 explicit HttpResponseBodyDrainer(HttpStreamBase
* stream
);
31 ~HttpResponseBodyDrainer();
33 // Starts reading the body until completion, or we hit the buffer limit, or we
34 // timeout. After Start(), |this| will eventually delete itself. If it
35 // doesn't complete immediately, it will add itself to |session|.
36 void Start(HttpNetworkSession
* session
);
38 // As above, but stop reading once |num_bytes_to_drain| has been reached.
39 void StartWithSize(HttpNetworkSession
* session
, int num_bytes_to_drain
);
43 STATE_DRAIN_RESPONSE_BODY
,
44 STATE_DRAIN_RESPONSE_BODY_COMPLETE
,
48 int DoLoop(int result
);
50 int DoDrainResponseBody();
51 int DoDrainResponseBodyComplete(int result
);
53 void OnIOComplete(int result
);
55 void Finish(int result
);
58 scoped_refptr
<IOBuffer
> read_buf_
;
59 const scoped_ptr
<HttpStreamBase
> stream_
;
62 CompletionCallback user_callback_
;
63 base::OneShotTimer
<HttpResponseBodyDrainer
> timer_
;
64 HttpNetworkSession
* session_
;
66 DISALLOW_COPY_AND_ASSIGN(HttpResponseBodyDrainer
);
71 #endif // NET_HTTP_HTTP_RESPONSE_BODY_DRAINER_H_