http_response_body_drainer.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.
  4. #ifndef NET_HTTP_HTTP_RESPONSE_BODY_DRAINER_H_
  5. #define NET_HTTP_HTTP_RESPONSE_BODY_DRAINER_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/timer/timer.h"
  10. #include "net/base/net_export.h"
  11. namespace net {
  12. class HttpNetworkSession;
  13. class HttpStream;
  14. class IOBuffer;
  15. class NET_EXPORT_PRIVATE HttpResponseBodyDrainer {
  16. public:
  17. // The size in bytes of the buffer we use to drain the response body that
  18. // we want to throw away. The response body is typically a small page just a
  19. // few hundred bytes long. We set a limit to prevent it from taking too long,
  20. // since we may as well just create a new socket then.
  21. static const int kDrainBodyBufferSize = 16384;
  22. static const int kTimeoutInSeconds = 5;
  23. explicit HttpResponseBodyDrainer(HttpStream* stream);
  24. HttpResponseBodyDrainer(const HttpResponseBodyDrainer&) = delete;
  25. HttpResponseBodyDrainer& operator=(const HttpResponseBodyDrainer&) = delete;
  26. ~HttpResponseBodyDrainer();
  27. // Starts reading the body until completion, or we hit the buffer limit, or we
  28. // timeout. After Start(), |this| will eventually delete itself via
  29. // HttpNetworkSession::RemoveResponseDrainer().
  30. void Start(HttpNetworkSession* session);
  31. private:
  32. enum State {
  33. STATE_DRAIN_RESPONSE_BODY,
  34. STATE_DRAIN_RESPONSE_BODY_COMPLETE,
  35. STATE_NONE,
  36. };
  37. int DoLoop(int result);
  38. int DoDrainResponseBody();
  39. int DoDrainResponseBodyComplete(int result);
  40. void OnIOComplete(int result);
  41. void OnTimerFired();
  42. void Finish(int result);
  43. scoped_refptr<IOBuffer> read_buf_;
  44. const std::unique_ptr<HttpStream> stream_;
  45. State next_state_ = STATE_NONE;
  46. int total_read_ = 0;
  47. base::OneShotTimer timer_;
  48. raw_ptr<HttpNetworkSession> session_ = nullptr;
  49. };
  50. } // namespace net
  51. #endif // NET_HTTP_HTTP_RESPONSE_BODY_DRAINER_H_