summaryrefslogtreecommitdiffstats
path: root/net/tools/quic/test_tools/simple_client.h
blob: ad9c00e956e69128478f2904e9e69bc97f87717a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_TOOLS_QUIC_TEST_TOOLS_SIMPLE_CLIENT_H_
#define NET_TOOLS_QUIC_TEST_TOOLS_SIMPLE_CLIENT_H_

#include <stddef.h>
#include <stdint.h>

#include <string>
#include <vector>

#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/tools/balsa/balsa_frame.h"

namespace net {
namespace test {

class HTTPMessage;

class SimpleClient {
 public:
  virtual ~SimpleClient() {}

  // Clears any outstanding state and sends 'size' bytes from 'buffer' to the
  // server, possibly with multiple send operations.  Returns 'size' on success
  // and -1 on error.  Callers should assume that any return value other than
  // 'size' indicates failure.
  virtual ssize_t Send(const void* buffer, size_t size) = 0;

  // Serialize and send an HTTP request.
  virtual ssize_t SendMessage(const HTTPMessage& message) = 0;

  // Clears any outstanding state, sends 'size' bytes from 'buffer' and waits
  // for a response or an error.
  virtual ssize_t SendAndWaitForResponse(const void* buffer, size_t size) = 0;

  // Clears any outstanding state and sends a simple GET of 'uri' to the
  // server.
  virtual ssize_t SendRequest(const std::string& uri) = 0;

  // The response body is returned as a string.
  virtual std::string SendCustomSynchronousRequest(
      const HTTPMessage& message) = 0;
  virtual std::string SendSynchronousRequest(const std::string& url) = 0;

  // Returns once a complete response or a connection close has been received
  // from the server.
  virtual void WaitForResponse();

  // Waits for some data or response from the server.
  virtual void WaitForInitialResponse();

  // Returns once a complete response or a connection close has been received
  // from the server, or once the timeout expires. -1 for no timeout.
  virtual void WaitForResponseForMs(int timeout_ms) = 0;

  // Waits for some data or response from the server, or once the timeout
  // expires. -1 for no timeout.
  virtual void WaitForInitialResponseForMs(int timeout_ms) = 0;

  // Clears any outstanding state from the last request.
  virtual void ClearPerRequestState() = 0;

  // Closes and reopens the connection to the server.
  virtual void ResetConnection() = 0;

  // Closes the connection to the server.
  virtual void Disconnect() = 0;

  // Both will return 0 on success, -1 otherwise.
  // Sends out RST packet to peer.
  // TODO(yongfa): Probably should be an interface too. LOG(FATAL) here
  // to prevent accidental invocation.
  virtual int ResetSocket();

  virtual int HalfClose();

  // Connects to the server.  This should be done implicitly by Send*
  // functions, but can be done explicitly as well.
  virtual void Connect() = 0;

  // Bind to the specified address.  If set_bind_to_address() is called, this
  // is called automatically on connect, but can be done explicitly to make
  // LocalIPEndPoint() meaningful before actually connecting.
  // Sets *local_address to the actual address bound to, which can be different
  // if the given address has port 0.
  virtual void Bind(IPEndPoint* local_address) = 0;

  virtual void MigrateSocket(const IPAddress& new_host) = 0;

  // Returns the local socket address of the client fd. Call only when
  // connected.
  // To get the local IPAdress, use local_address().host().
  // To get the local port, use local_address.port().
  virtual IPEndPoint local_address() const = 0;

  // Returns the serialized message that would be sent by any of the HTTPMessage
  // functions above.
  virtual std::string SerializeMessage(const HTTPMessage& message) = 0;

  // Sets the IP address to bind to on future Connect()s in case Bind() is not
  // called in advance. If it's set to uninitialized IPAddress, default loopback
  // address will be used.
  virtual IPAddress bind_to_address() const = 0;
  virtual void set_bind_to_address(IPAddress address) = 0;

  // Returns true if the headers have been processed and are available.
  virtual bool response_headers_complete() const = 0;

  // Returns the response headers, if a response was completely framed.
  // Undefined behavior otherwise.
  virtual const BalsaHeaders* response_headers() const = 0;

  // Returns true iff response has been fully received.
  virtual bool response_complete() const = 0;

  // Returns the number of bytes read from the server during this request.
  virtual int64_t response_size() const = 0;

  // Returns the number of header bytes received during this request, if
  // meaningful for the protocol.
  virtual int response_header_size() const;

  // Returns the number of body bytes received during this request, if
  // meaningful for the protocol.
  virtual int64_t response_body_size() const;

  // Returns the response body, if there was one. If there was no response, or
  // if buffer_body() is false, returns an empty string.
  virtual const std::string& response_body() = 0;

  // The address the client is connected to.
  virtual const IPEndPoint& address() const = 0;

  // Returns true if the client is connected, false otherwise.
  virtual bool connected() const = 0;

  // Returns true if the server has informed the client that it is
  // in "lame duck" mode, indicating intent to shut down and
  // requesting that no further connections be established.
  virtual bool ServerInLameDuckMode() const = 0;

  // Return the number of bytes read off the wire by this client.
  virtual size_t bytes_read() const = 0;

  // Returns the number of bytes written to the wire by this client.
  virtual size_t bytes_written() const = 0;

  // Return the number of requests sent.
  virtual size_t requests_sent() const = 0;

  // Instructs the client to populate response_body().
  virtual bool buffer_body() const = 0;
  virtual void set_buffer_body(bool buffer_body) = 0;
};

}  // namespace test
}  // namespace net

#endif  // NET_TOOLS_QUIC_TEST_TOOLS_SIMPLE_CLIENT_H_