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 | |
| 11 | #include <base/callback_old.h> |
| 12 | #include <base/memory/ref_counted.h> |
| 13 | #include <base/memory/scoped_ptr.h> |
| 14 | #include <base/task.h> |
| 15 | |
| 16 | #include "shill/byte_string.h" |
| 17 | #include "shill/refptr_types.h" |
| 18 | #include "shill/shill_time.h" |
| 19 | |
| 20 | namespace shill { |
| 21 | |
| 22 | class AsyncConnection; |
| 23 | class DNSClient; |
| 24 | class EventDispatcher; |
| 25 | class HTTPURL; |
| 26 | class InputData; |
| 27 | class IOHandler; |
| 28 | class IPAddress; |
| 29 | class Sockets; |
| 30 | |
| 31 | // The HTTPRequest class implements facilities for performing |
| 32 | // a simple "GET" request and returning the contents via a |
| 33 | // callback. |
| 34 | class HTTPRequest { |
| 35 | public: |
| 36 | enum Result { |
| 37 | kResultUnknown, |
| 38 | kResultInProgress, |
| 39 | kResultDNSFailure, |
| 40 | kResultDNSTimeout, |
| 41 | kResultConnectionFailure, |
| 42 | kResultConnectionTimeout, |
| 43 | kResultRequestFailure, |
| 44 | kResultRequestTimeout, |
| 45 | kResultResponseFailure, |
| 46 | kResultResponseTimeout, |
| 47 | kResultSuccess |
| 48 | }; |
| 49 | |
| 50 | HTTPRequest(ConnectionRefPtr connection, |
| 51 | EventDispatcher *dispatcher, |
| 52 | Sockets *sockets); |
| 53 | virtual ~HTTPRequest(); |
| 54 | |
| 55 | // Start an http GET request to the URL |url|. Whenever any data is |
| 56 | // read from the server, |read_event_callback| is called with the number |
| 57 | // of bytes read. This callback could be called more than once as data |
| 58 | // arrives. All callbacks can access the data as it is read in by |
| 59 | // using the response_data() getter below. |
| 60 | // |
| 61 | // When the transaction completes, |result_callback| will be called with |
| 62 | // the final status from the transaction. |result_callback| will not be |
| 63 | // called if either the Start() call fails or if Stop() is called before |
| 64 | // the transaction completes. |
| 65 | // |
| 66 | // This (Start) function returns a failure result if the request |
| 67 | // failed during initialization, or kResultInProgress if the request |
| 68 | // has started successfully and is now in progress. |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 69 | virtual Result Start(const HTTPURL &url, |
| 70 | Callback1<int>::Type *read_event_callback, |
| 71 | Callback1<Result>::Type *result_callback); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 72 | |
| 73 | // Stop the current HTTPRequest. No callback is called as a side |
| 74 | // effect of this function. |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 75 | virtual void Stop(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 76 | |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 77 | virtual const ByteString &response_data() const { return response_data_; } |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | friend class HTTPRequestTest; |
| 81 | |
| 82 | // Time to wait for connection to remote server. |
| 83 | static const int kConnectTimeoutSeconds; |
| 84 | // Time to wait for DNS server. |
| 85 | static const int kDNSTimeoutSeconds; |
| 86 | // Time to wait for any input from server. |
| 87 | static const int kInputTimeoutSeconds; |
| 88 | |
| 89 | static const char kHTTPRequestTemplate[]; |
| 90 | |
| 91 | bool ConnectServer(const IPAddress &address, int port); |
| 92 | void GetDNSResult(bool result); |
| 93 | void OnConnectCompletion(bool success, int fd); |
| 94 | void ReadFromServer(InputData *data); |
| 95 | void SendStatus(Result result); |
| 96 | void StartIdleTimeout(int timeout_seconds, Result timeout_result); |
| 97 | void TimeoutTask(); |
| 98 | void WriteToServer(int fd); |
| 99 | |
| 100 | ConnectionRefPtr connection_; |
| 101 | EventDispatcher *dispatcher_; |
| 102 | Sockets *sockets_; |
| 103 | |
| 104 | scoped_ptr<Callback2<bool, int>::Type> connect_completion_callback_; |
| 105 | scoped_ptr<Callback1<bool>::Type> dns_client_callback_; |
| 106 | scoped_ptr<Callback1<InputData *>::Type> read_server_callback_; |
| 107 | scoped_ptr<Callback1<int>::Type> write_server_callback_; |
| 108 | Callback1<Result>::Type *result_callback_; |
| 109 | Callback1<int>::Type *read_event_callback_; |
| 110 | ScopedRunnableMethodFactory<HTTPRequest> task_factory_; |
| 111 | CancelableTask *idle_timeout_; |
| 112 | scoped_ptr<IOHandler> read_server_handler_; |
| 113 | scoped_ptr<IOHandler> write_server_handler_; |
| 114 | scoped_ptr<DNSClient> dns_client_; |
| 115 | scoped_ptr<AsyncConnection> server_async_connection_; |
| 116 | std::string server_hostname_; |
| 117 | int server_port_; |
| 118 | int server_socket_; |
| 119 | Result timeout_result_; |
| 120 | ByteString request_data_; |
| 121 | ByteString response_data_; |
| 122 | bool is_running_; |
| 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(HTTPRequest); |
| 125 | }; |
| 126 | |
| 127 | } // namespace shill |
| 128 | |
| 129 | #endif // SHILL_HTTP_REQUEST_ |