blob: 610ef0df4a2acf9781e3b55beff5b9583401ca72 [file] [log] [blame]
Paul Stewart188a84a2012-01-20 16:28:15 -08001// 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 Shienbrood3e20a232012-02-16 11:35:56 -050011#include <base/callback.h>
Paul Stewartf582b502012-04-04 21:39:22 -070012#include <base/cancelable_callback.h>
Paul Stewart188a84a2012-01-20 16:28:15 -080013#include <base/memory/ref_counted.h>
14#include <base/memory/scoped_ptr.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050015#include <base/memory/weak_ptr.h>
Paul Stewart188a84a2012-01-20 16:28:15 -080016
17#include "shill/byte_string.h"
18#include "shill/refptr_types.h"
19#include "shill/shill_time.h"
20
21namespace shill {
22
23class AsyncConnection;
24class DNSClient;
Paul Stewartbdb02e62012-02-22 16:24:33 -080025class Error;
Paul Stewart188a84a2012-01-20 16:28:15 -080026class EventDispatcher;
27class HTTPURL;
28class InputData;
29class IOHandler;
30class IPAddress;
31class Sockets;
32
33// The HTTPRequest class implements facilities for performing
34// a simple "GET" request and returning the contents via a
35// callback.
36class 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 Stewartbdb02e62012-02-22 16:24:33 -080058 // 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 Stewart188a84a2012-01-20 16:28:15 -080061 //
62 // When the transaction completes, |result_callback| will be called with
Paul Stewartbdb02e62012-02-22 16:24:33 -080063 // 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 Stewart188a84a2012-01-20 16:28:15 -080066 // 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 Stewartbdb02e62012-02-22 16:24:33 -080072 virtual Result Start(
73 const HTTPURL &url,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050074 const base::Callback<void(const ByteString &)> &read_event_callback,
75 const base::Callback<void(Result, const ByteString &)> &result_callback);
Paul Stewart188a84a2012-01-20 16:28:15 -080076
77 // Stop the current HTTPRequest. No callback is called as a side
78 // effect of this function.
Paul Stewarte6927402012-01-23 16:11:30 -080079 virtual void Stop();
Paul Stewart188a84a2012-01-20 16:28:15 -080080
Paul Stewartbdb02e62012-02-22 16:24:33 -080081 // 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 Stewarte6927402012-01-23 16:11:30 -080084 virtual const ByteString &response_data() const { return response_data_; }
Paul Stewart188a84a2012-01-20 16:28:15 -080085
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 Stewartbdb02e62012-02-22 16:24:33 -080099 void GetDNSResult(const Error &error, const IPAddress &address);
Paul Stewart188a84a2012-01-20 16:28:15 -0800100 void OnConnectCompletion(bool success, int fd);
101 void ReadFromServer(InputData *data);
102 void SendStatus(Result result);
103 void StartIdleTimeout(int timeout_seconds, Result timeout_result);
104 void TimeoutTask();
105 void WriteToServer(int fd);
106
107 ConnectionRefPtr connection_;
108 EventDispatcher *dispatcher_;
109 Sockets *sockets_;
110
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500111 base::WeakPtrFactory<HTTPRequest> weak_ptr_factory_;
112 base::Callback<void(bool, int)> connect_completion_callback_;
113 base::Callback<void(const Error &, const IPAddress &)> dns_client_callback_;
114 base::Callback<void(InputData *)> read_server_callback_;
115 base::Callback<void(int)> write_server_callback_;
116 base::Callback<void(Result, const ByteString &)> result_callback_;
117 base::Callback<void(const ByteString &)> read_event_callback_;
Paul Stewart188a84a2012-01-20 16:28:15 -0800118 scoped_ptr<IOHandler> read_server_handler_;
119 scoped_ptr<IOHandler> write_server_handler_;
120 scoped_ptr<DNSClient> dns_client_;
121 scoped_ptr<AsyncConnection> server_async_connection_;
122 std::string server_hostname_;
123 int server_port_;
124 int server_socket_;
Paul Stewartf582b502012-04-04 21:39:22 -0700125 base::CancelableClosure timeout_closure_;
Paul Stewart188a84a2012-01-20 16:28:15 -0800126 Result timeout_result_;
127 ByteString request_data_;
128 ByteString response_data_;
129 bool is_running_;
130
131 DISALLOW_COPY_AND_ASSIGN(HTTPRequest);
132};
133
134} // namespace shill
135
136#endif // SHILL_HTTP_REQUEST_