network_fetcher.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2019 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 COMPONENTS_WINHTTP_NETWORK_FETCHER_H_
  5. #define COMPONENTS_WINHTTP_NETWORK_FETCHER_H_
  6. #include <windows.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/containers/flat_map.h"
  13. #include "base/files/file.h"
  14. #include "base/files/file_path.h"
  15. #include "base/memory/scoped_refptr.h"
  16. #include "base/sequence_checker.h"
  17. #include "base/strings/string_piece_forward.h"
  18. #include "components/winhttp/proxy_configuration.h"
  19. #include "components/winhttp/scoped_hinternet.h"
  20. #include "url/gurl.h"
  21. namespace base {
  22. class SingleThreadTaskRunner;
  23. }
  24. namespace winhttp {
  25. // Implements a network fetcher in terms of WinHTTP. The class is ref-counted
  26. // as it is accessed from the main thread and the worker threads in WinHTTP.
  27. class NetworkFetcher : public base::RefCountedThreadSafe<NetworkFetcher> {
  28. public:
  29. using FetchCompleteCallback = base::OnceCallback<void(int response_code)>;
  30. using FetchStartedCallback =
  31. base::OnceCallback<void(int response_code, int64_t content_length)>;
  32. using FetchProgressCallback = base::RepeatingCallback<void(int64_t current)>;
  33. NetworkFetcher(const HINTERNET& session_handle,
  34. scoped_refptr<ProxyConfiguration> proxy_configuration);
  35. NetworkFetcher(const NetworkFetcher&) = delete;
  36. NetworkFetcher& operator=(const NetworkFetcher&) = delete;
  37. void Close();
  38. void PostRequest(
  39. const GURL& url,
  40. const std::string& post_data,
  41. const std::string& content_type,
  42. const base::flat_map<std::string, std::string>& post_additional_headers,
  43. FetchStartedCallback fetch_started_callback,
  44. FetchProgressCallback fetch_progress_callback,
  45. FetchCompleteCallback fetch_complete_callback);
  46. // Downloads the content of the |url| to a file identified by |file_path|.
  47. // The content is written to the file as it is being retrieved from the
  48. // network.
  49. void DownloadToFile(const GURL& url,
  50. const base::FilePath& file_path,
  51. FetchStartedCallback fetch_started_callback,
  52. FetchProgressCallback fetch_progress_callback,
  53. FetchCompleteCallback fetch_complete_callback);
  54. HRESULT QueryHeaderString(const std::wstring& name,
  55. std::wstring* value) const;
  56. HRESULT QueryHeaderInt(const std::wstring& name, int* value) const;
  57. std::string GetResponseBody() const;
  58. HRESULT GetNetError() const;
  59. base::FilePath GetFilePath() const;
  60. // Returns the number of bytes retrieved from the network. This may be
  61. // different than the content length if an error occurred.
  62. int64_t GetContentSize() const;
  63. private:
  64. friend class base::RefCountedThreadSafe<NetworkFetcher>;
  65. using WriteDataCallback = base::RepeatingCallback<void()>;
  66. ~NetworkFetcher();
  67. static void __stdcall WinHttpStatusCallback(HINTERNET handle,
  68. DWORD_PTR context,
  69. DWORD status,
  70. void* info,
  71. DWORD info_len);
  72. DWORD_PTR context() const { return reinterpret_cast<DWORD_PTR>(this); }
  73. void StatusCallback(HINTERNET handle,
  74. uint32_t status,
  75. void* info,
  76. uint32_t info_len);
  77. HRESULT BeginFetch(
  78. const std::string& data,
  79. base::flat_map<std::string, std::string> additional_headers);
  80. ScopedHInternet Connect();
  81. ScopedHInternet OpenRequest();
  82. HRESULT SendRequest(const std::string& data);
  83. void SendRequestComplete();
  84. HRESULT ReceiveResponse();
  85. void HeadersAvailable();
  86. HRESULT ReadData();
  87. void ReadDataComplete(size_t num_bytes_read);
  88. void RequestError(const WINHTTP_ASYNC_RESULT* result);
  89. void CompleteFetch();
  90. void WriteDataToMemory();
  91. void WriteDataToFile();
  92. bool WriteDataToFileBlocking();
  93. void WriteDataToFileComplete(bool is_eof);
  94. SEQUENCE_CHECKER(sequence_checker_);
  95. scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
  96. const HINTERNET& session_handle_; // Owned by NetworkFetcherFactory.
  97. scoped_refptr<ProxyConfiguration> proxy_configuration_;
  98. ScopedHInternet connect_handle_;
  99. ScopedHInternet request_handle_;
  100. // Keeps an outstanding reference count on itself as long as there is a
  101. // valid request handle and the context for the handle is set to this
  102. // instance.
  103. scoped_refptr<NetworkFetcher> self_;
  104. GURL url_;
  105. bool is_https_ = false;
  106. std::string host_;
  107. int port_ = 0;
  108. std::string path_for_request_;
  109. base::WStringPiece verb_;
  110. // The value of Content-Type header, e.g. "application/json".
  111. std::string content_type_;
  112. WriteDataCallback write_data_callback_;
  113. HRESULT net_error_ = S_OK;
  114. std::vector<char> read_buffer_;
  115. int response_code_ = 0;
  116. std::string post_response_body_;
  117. base::FilePath file_path_;
  118. base::File file_;
  119. int64_t content_size_ = 0;
  120. FetchStartedCallback fetch_started_callback_;
  121. FetchProgressCallback fetch_progress_callback_;
  122. FetchCompleteCallback fetch_complete_callback_;
  123. };
  124. } // namespace winhttp
  125. #endif // COMPONENTS_WINHTTP_NETWORK_FETCHER_H_