blob: 0b3fe9c826e2a6d81bfd4f6ca5b441d40ecffb4a [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
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_HTTP_REQUEST_H_
6#define SHILL_HTTP_REQUEST_H_
Paul Stewart188a84a2012-01-20 16:28:15 -08007
Ben Chancd477322014-10-17 14:19:30 -07008#include <memory>
Paul Stewart188a84a2012-01-20 16:28:15 -08009#include <string>
10#include <vector>
11
Eric Shienbrood3e20a232012-02-16 11:35:56 -050012#include <base/callback.h>
Paul Stewartf582b502012-04-04 21:39:22 -070013#include <base/cancelable_callback.h>
Paul Stewart188a84a2012-01-20 16:28:15 -080014#include <base/memory/ref_counted.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050015#include <base/memory/weak_ptr.h>
Paul Stewart188a84a2012-01-20 16:28:15 -080016
Peter Qiu8d6b5972014-10-28 15:33:34 -070017#include "shill/net/byte_string.h"
18#include "shill/net/shill_time.h"
Paul Stewart188a84a2012-01-20 16:28:15 -080019#include "shill/refptr_types.h"
Paul Stewart188a84a2012-01-20 16:28:15 -080020
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;
Liam McLoughlinf4baef22012-08-01 19:08:25 -070028struct InputData;
Paul Stewart188a84a2012-01-20 16:28:15 -080029class 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,
Paul Stewart8ae18742015-06-16 13:13:10 -070053 EventDispatcher* dispatcher,
54 Sockets* sockets);
Paul Stewart188a84a2012-01-20 16:28:15 -080055 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(
Paul Stewart8ae18742015-06-16 13:13:10 -070073 const HTTPURL& url,
74 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 Stewart8ae18742015-06-16 13:13:10 -070084 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
Paul Stewart8ae18742015-06-16 13:13:10 -070098 bool ConnectServer(const IPAddress& address, int port);
99 void GetDNSResult(const Error& error, const IPAddress& address);
Paul Stewart188a84a2012-01-20 16:28:15 -0800100 void OnConnectCompletion(bool success, int fd);
Paul Stewart8ae18742015-06-16 13:13:10 -0700101 void OnServerReadError(const std::string& error_msg);
102 void ReadFromServer(InputData* data);
Paul Stewart188a84a2012-01-20 16:28:15 -0800103 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_;
Paul Stewart8ae18742015-06-16 13:13:10 -0700109 EventDispatcher* dispatcher_;
110 Sockets* sockets_;
Paul Stewart188a84a2012-01-20 16:28:15 -0800111
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500112 base::WeakPtrFactory<HTTPRequest> weak_ptr_factory_;
113 base::Callback<void(bool, int)> connect_completion_callback_;
Paul Stewart8ae18742015-06-16 13:13:10 -0700114 base::Callback<void(const Error&, const IPAddress&)> dns_client_callback_;
115 base::Callback<void(InputData*)> read_server_callback_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500116 base::Callback<void(int)> write_server_callback_;
Paul Stewart8ae18742015-06-16 13:13:10 -0700117 base::Callback<void(Result, const ByteString&)> result_callback_;
118 base::Callback<void(const ByteString&)> read_event_callback_;
Ben Chancd477322014-10-17 14:19:30 -0700119 std::unique_ptr<IOHandler> read_server_handler_;
120 std::unique_ptr<IOHandler> write_server_handler_;
121 std::unique_ptr<DNSClient> dns_client_;
122 std::unique_ptr<AsyncConnection> server_async_connection_;
Paul Stewart188a84a2012-01-20 16:28:15 -0800123 std::string server_hostname_;
124 int server_port_;
125 int server_socket_;
Paul Stewartf582b502012-04-04 21:39:22 -0700126 base::CancelableClosure timeout_closure_;
Paul Stewart188a84a2012-01-20 16:28:15 -0800127 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
Ben Chanc45688b2014-07-02 23:50:45 -0700137#endif // SHILL_HTTP_REQUEST_H_