Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 | |
| 5 | #ifndef SHILL_HTTP_REQUEST_ |
| 6 | #define SHILL_HTTP_REQUEST_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 11 | #include <base/callback.h> |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 12 | #include <base/cancelable_callback.h> |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 13 | #include <base/memory/ref_counted.h> |
| 14 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | #include <base/memory/weak_ptr.h> |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 16 | |
| 17 | #include "shill/byte_string.h" |
| 18 | #include "shill/refptr_types.h" |
| 19 | #include "shill/shill_time.h" |
| 20 | |
| 21 | namespace shill { |
| 22 | |
| 23 | class AsyncConnection; |
| 24 | class DNSClient; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 25 | class Error; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 26 | class EventDispatcher; |
| 27 | class HTTPURL; |
Liam McLoughlin | f4baef2 | 2012-08-01 19:08:25 -0700 | [diff] [blame] | 28 | struct InputData; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 29 | class IOHandler; |
| 30 | class IPAddress; |
| 31 | class Sockets; |
| 32 | |
| 33 | // The HTTPRequest class implements facilities for performing |
| 34 | // a simple "GET" request and returning the contents via a |
| 35 | // callback. |
| 36 | class HTTPRequest { |
| 37 | public: |
| 38 | enum Result { |
| 39 | kResultUnknown, |
| 40 | kResultInProgress, |
| 41 | kResultDNSFailure, |
| 42 | kResultDNSTimeout, |
| 43 | kResultConnectionFailure, |
| 44 | kResultConnectionTimeout, |
| 45 | kResultRequestFailure, |
| 46 | kResultRequestTimeout, |
| 47 | kResultResponseFailure, |
| 48 | kResultResponseTimeout, |
| 49 | kResultSuccess |
| 50 | }; |
| 51 | |
| 52 | HTTPRequest(ConnectionRefPtr connection, |
| 53 | EventDispatcher *dispatcher, |
| 54 | Sockets *sockets); |
| 55 | virtual ~HTTPRequest(); |
| 56 | |
| 57 | // Start an http GET request to the URL |url|. Whenever any data is |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 58 | // read from the server, |read_event_callback| is called with the |
| 59 | // current contents of the response data coming from the server. |
| 60 | // This callback could be called more than once as data arrives. |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 61 | // |
| 62 | // When the transaction completes, |result_callback| will be called with |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 63 | // the final status from the transaction. It is valid for the callback |
| 64 | // function to destroy this HTTPRequest object, because at this time all |
| 65 | // object state has already been cleaned up. |result_callback| will not be |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 66 | // called if either the Start() call fails or if Stop() is called before |
| 67 | // the transaction completes. |
| 68 | // |
| 69 | // This (Start) function returns a failure result if the request |
| 70 | // failed during initialization, or kResultInProgress if the request |
| 71 | // has started successfully and is now in progress. |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 72 | virtual Result Start( |
| 73 | const HTTPURL &url, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 74 | const base::Callback<void(const ByteString &)> &read_event_callback, |
| 75 | const base::Callback<void(Result, const ByteString &)> &result_callback); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 76 | |
| 77 | // Stop the current HTTPRequest. No callback is called as a side |
| 78 | // effect of this function. |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 79 | virtual void Stop(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 80 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 81 | // Returns the data received so far from the server in the current |
| 82 | // request. This data is available only while the request is active, |
| 83 | // and before the result callback is called. |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 84 | virtual const ByteString &response_data() const { return response_data_; } |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | friend class HTTPRequestTest; |
| 88 | |
| 89 | // Time to wait for connection to remote server. |
| 90 | static const int kConnectTimeoutSeconds; |
| 91 | // Time to wait for DNS server. |
| 92 | static const int kDNSTimeoutSeconds; |
| 93 | // Time to wait for any input from server. |
| 94 | static const int kInputTimeoutSeconds; |
| 95 | |
| 96 | static const char kHTTPRequestTemplate[]; |
| 97 | |
| 98 | bool ConnectServer(const IPAddress &address, int port); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 99 | void GetDNSResult(const Error &error, const IPAddress &address); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 100 | void OnConnectCompletion(bool success, int fd); |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 101 | void OnServerReadError(const Error &error); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 102 | void ReadFromServer(InputData *data); |
| 103 | void SendStatus(Result result); |
| 104 | void StartIdleTimeout(int timeout_seconds, Result timeout_result); |
| 105 | void TimeoutTask(); |
| 106 | void WriteToServer(int fd); |
| 107 | |
| 108 | ConnectionRefPtr connection_; |
| 109 | EventDispatcher *dispatcher_; |
| 110 | Sockets *sockets_; |
| 111 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 112 | base::WeakPtrFactory<HTTPRequest> weak_ptr_factory_; |
| 113 | base::Callback<void(bool, int)> connect_completion_callback_; |
| 114 | base::Callback<void(const Error &, const IPAddress &)> dns_client_callback_; |
| 115 | base::Callback<void(InputData *)> read_server_callback_; |
| 116 | base::Callback<void(int)> write_server_callback_; |
| 117 | base::Callback<void(Result, const ByteString &)> result_callback_; |
| 118 | base::Callback<void(const ByteString &)> read_event_callback_; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 119 | scoped_ptr<IOHandler> read_server_handler_; |
| 120 | scoped_ptr<IOHandler> write_server_handler_; |
| 121 | scoped_ptr<DNSClient> dns_client_; |
| 122 | scoped_ptr<AsyncConnection> server_async_connection_; |
| 123 | std::string server_hostname_; |
| 124 | int server_port_; |
| 125 | int server_socket_; |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 126 | base::CancelableClosure timeout_closure_; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 127 | Result timeout_result_; |
| 128 | ByteString request_data_; |
| 129 | ByteString response_data_; |
| 130 | bool is_running_; |
| 131 | |
| 132 | DISALLOW_COPY_AND_ASSIGN(HTTPRequest); |
| 133 | }; |
| 134 | |
| 135 | } // namespace shill |
| 136 | |
| 137 | #endif // SHILL_HTTP_REQUEST_ |