http_connection.h revision 58537e28ecd584eab876aee8be7156509866d23a
1// Copyright (c) 2012 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_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_
6#define NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_
7
8#include "base/basictypes.h"
9#include "base/callback.h"
10#include "base/strings/string_piece.h"
11#include "net/test/embedded_test_server/http_request.h"
12
13namespace net {
14
15class StreamListenSocket;
16
17namespace test_server {
18
19class HttpConnection;
20class HttpResponse;
21
22// Calblack called when a request is parsed. Response should be sent
23// using HttpConnection::SendResponse() on the |connection| argument.
24typedef base::Callback<void(HttpConnection* connection,
25                            scoped_ptr<HttpRequest> request)>
26    HandleRequestCallback;
27
28// Wraps the connection socket. Accepts incoming data and sends responses.
29// If a valid request is parsed, then |callback_| is invoked.
30class HttpConnection {
31 public:
32  HttpConnection(scoped_ptr<StreamListenSocket> socket,
33                 const HandleRequestCallback& callback);
34  ~HttpConnection();
35
36  // Sends the HTTP response to the client.
37  void SendResponse(scoped_ptr<HttpResponse> response) const;
38
39 private:
40  friend class EmbeddedTestServer;
41
42  // Accepts raw chunk of data from the client. Internally, passes it to the
43  // HttpRequestParser class. If a request is parsed, then |callback_| is
44  // called.
45  void ReceiveData(const base::StringPiece& data);
46
47  scoped_ptr<StreamListenSocket> socket_;
48  const HandleRequestCallback callback_;
49  HttpRequestParser request_parser_;
50
51  DISALLOW_COPY_AND_ASSIGN(HttpConnection);
52};
53
54}  // namespace test_server
55}  // namespace net
56
57#endif  // NET_TEST_EMBEDDED_TEST_SERVER_HTTP_CONNECTION_H_
58