xref: /aosp_15_r20/external/cronet/net/http/http_response_body_drainer.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_HTTP_HTTP_RESPONSE_BODY_DRAINER_H_
6 #define NET_HTTP_HTTP_RESPONSE_BODY_DRAINER_H_
7 
8 #include <memory>
9 
10 #include "base/memory/raw_ptr.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "base/timer/timer.h"
13 #include "net/base/net_export.h"
14 
15 namespace net {
16 
17 class HttpNetworkSession;
18 class HttpStream;
19 class IOBuffer;
20 
21 class NET_EXPORT_PRIVATE HttpResponseBodyDrainer {
22  public:
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;
29 
30   explicit HttpResponseBodyDrainer(HttpStream* stream);
31   HttpResponseBodyDrainer(const HttpResponseBodyDrainer&) = delete;
32   HttpResponseBodyDrainer& operator=(const HttpResponseBodyDrainer&) = delete;
33   ~HttpResponseBodyDrainer();
34 
35   // Starts reading the body until completion, or we hit the buffer limit, or we
36   // timeout.  After Start(), |this| will eventually delete itself via
37   // HttpNetworkSession::RemoveResponseDrainer().
38   void Start(HttpNetworkSession* session);
39 
40  private:
41   enum State {
42     STATE_DRAIN_RESPONSE_BODY,
43     STATE_DRAIN_RESPONSE_BODY_COMPLETE,
44     STATE_NONE,
45   };
46 
47   int DoLoop(int result);
48 
49   int DoDrainResponseBody();
50   int DoDrainResponseBodyComplete(int result);
51 
52   void OnIOComplete(int result);
53   void OnTimerFired();
54   void Finish(int result);
55 
56   scoped_refptr<IOBuffer> read_buf_;
57   const std::unique_ptr<HttpStream> stream_;
58   State next_state_ = STATE_NONE;
59   int total_read_ = 0;
60   base::OneShotTimer timer_;
61   raw_ptr<HttpNetworkSession> session_ = nullptr;
62 };
63 
64 }  // namespace net
65 
66 #endif // NET_HTTP_HTTP_RESPONSE_BODY_DRAINER_H_
67