1// Copyright 2014 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
5#ifndef NET_TOOLS_QUIC_TEST_TOOLS_SIMPLE_CLIENT_H_
6#define NET_TOOLS_QUIC_TEST_TOOLS_SIMPLE_CLIENT_H_
7
8#include <string>
9#include <vector>
10
11#include "net/base/ip_endpoint.h"
12#include "net/tools/balsa/balsa_frame.h"
13
14namespace net {
15namespace tools {
16namespace test {
17
18class HTTPMessage;
19
20class SimpleClient {
21 public:
22  virtual ~SimpleClient() {}
23
24  // Clears any outstanding state and sends 'size' bytes from 'buffer' to the
25  // server, possibly with multiple send operations.  Returns 'size' on success
26  // and -1 on error.  Callers should assume that any return value other than
27  // 'size' indicates failure.
28  virtual ssize_t Send(const void *buffer, size_t size) = 0;
29
30  // Serialize and send an HTTP request.
31  virtual ssize_t SendMessage(const HTTPMessage& message) = 0;
32
33  // Clears any outstanding state, sends 'size' bytes from 'buffer' and waits
34  // for a response or an error.
35  virtual ssize_t SendAndWaitForResponse(const void *buffer, size_t size) = 0;
36
37  // Clears any outstanding state and sends a simple GET of 'uri' to the
38  // server.
39  virtual ssize_t SendRequest(const std::string& uri) = 0;
40
41  // The response body is returned as a string.
42  virtual std::string SendCustomSynchronousRequest(
43      const HTTPMessage& message) = 0;
44  virtual std::string SendSynchronousRequest(const std::string& url) = 0;
45
46  // Returns once a complete response or a connection close has been received
47  // from the server.
48  virtual void WaitForResponse();
49
50  // Waits for some data or response from the server.
51  virtual void WaitForInitialResponse();
52
53  // Returns once a complete response or a connection close has been received
54  // from the server, or once the timeout expires. -1 for no timeout.
55  virtual void WaitForResponseForMs(int timeout_ms) = 0;
56
57  // Waits for some data or response from the server, or once the timeout
58  // expires. -1 for no timeout.
59  virtual void WaitForInitialResponseForMs(int timeout_ms) = 0;
60
61  // Clears any outstanding state from the last request.
62  virtual void ClearPerRequestState() = 0;
63
64  // Closes and reopens the connection to the server.
65  virtual void ResetConnection() = 0;
66
67  // Closes the connection to the server.
68  virtual void Disconnect() = 0;
69
70  // Both will return 0 on success, -1 otherwise.
71  // Sends out RST packet to peer.
72  // TODO(yongfa): Probably should be an interface too. LOG(FATAL) here
73  // to prevent accidental invocation.
74  virtual int ResetSocket();
75
76  virtual int HalfClose();
77
78  // Connects to the server.  This should be done implicitly by Send*
79  // functions, but can be done explicitly as well.
80  virtual void Connect() = 0;
81
82  // Bind to the specified address.  If set_bind_to_address() is called, this
83  // is called automatically on connect, but can be done explicitly to make
84  // LocalIPEndPoint() meaningful before actually connecting.
85  // Sets *local_address to the actual address bound to, which can be different
86  // if the given address has port 0.
87  virtual void Bind(IPEndPoint* local_address) = 0;
88
89  // Returns the local socket address of the client fd. Call only when
90  // connected.
91  // To get the local IPAdress, use LocalSocketAddress().host().
92  // To get the local port, use LocalSocketAddress.port().
93  virtual IPEndPoint LocalSocketAddress() const = 0;
94
95  // Returns the serialized message that would be sent by any of the HTTPMessage
96  // functions above.
97  virtual std::string SerializeMessage(const HTTPMessage& message) = 0;
98
99  // Sets the IP address to bind to on future Connect()s in case Bind() is not
100  // called in advance. If it's set to uninitialized IPAddress, default loopback
101  // address will be used.
102  virtual IPAddressNumber bind_to_address() const = 0;
103  virtual void set_bind_to_address(IPAddressNumber address) = 0;
104
105  // Returns true if the headers have been processed and are available.
106  virtual bool response_headers_complete() const = 0;
107
108  // Returns the response headers, if a response was completely framed.
109  // Undefined behavior otherwise.
110  virtual const BalsaHeaders* response_headers() const = 0;
111
112  // Returns true iff response has been fully received.
113  virtual bool response_complete() const = 0;
114
115  // Returns the number of bytes read from the server during this request.
116  virtual int64 response_size() const = 0;
117
118  // Returns the number of header bytes received during this request, if
119  // meaningful for the protocol.
120  virtual int response_header_size() const;
121
122  // Returns the number of body bytes received during this request, if
123  // meaningful for the protocol.
124  virtual int64 response_body_size() const;
125
126  // Returns the response body, if there was one. If there was no response, or
127  // if buffer_body() is false, returns an empty string.
128  virtual const std::string& response_body() = 0;
129
130  // The address the client is connected to.
131  virtual const IPEndPoint& address() const = 0;
132
133  // Returns true if the client is connected, false otherwise.
134  virtual bool connected() const = 0;
135
136  // Returns true if the server has informed the client that it is
137  // in "lame duck" mode, indicating intent to shut down and
138  // requesting that no further connections be established.
139  virtual bool ServerInLameDuckMode() const = 0;
140
141  // Return the number of bytes read off the wire by this client.
142  virtual size_t bytes_read() const = 0;
143
144  // Returns the number of bytes written to the wire by this client.
145  virtual size_t bytes_written() const = 0;
146
147  // Return the number of requests sent.
148  virtual size_t requests_sent() const = 0;
149
150  // Instructs the client to populate response_body().
151  virtual bool buffer_body() const = 0;
152  virtual void set_buffer_body(bool buffer_body) = 0;
153};
154
155}  // namespace test
156}  // namespace tools
157}  // namespace net
158
159#endif  // NET_TOOLS_QUIC_TEST_TOOLS_SIMPLE_CLIENT_H_
160