blob: 6c9dcfa77a7c29a2c6b757c6dd393ac4cd90be19 [file] [log] [blame]
// Copyright (c) 2012 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.
#include <memory>
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_piece.h"
#include "net/base/completion_once_callback.h"
#include "net/base/io_buffer.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
namespace net {
class StreamSocket;
namespace test_server {
class HttpConnection;
// Calblack called when a request is parsed. Response should be sent
// using HttpConnection::SendResponse() on the |connection| argument.
typedef base::RepeatingCallback<void(HttpConnection* connection,
std::unique_ptr<HttpRequest> request)>
// Wraps the connection socket. Accepts incoming data and sends responses.
// If a valid request is parsed, then |callback_| is invoked.
class HttpConnection {
HttpConnection(std::unique_ptr<StreamSocket> socket,
const HandleRequestCallback& callback);
// Sends the |response_string| to the client and calls |callback| once done.
void SendResponseBytes(const std::string& response_string,
SendCompleteCallback callback);
// Accepts raw chunk of data from the client. Internally, passes it to the
// HttpRequestParser class. If a request is parsed, then |callback_| is
// called.
int ReadData(CompletionOnceCallback callback);
bool ConsumeData(int size);
friend class EmbeddedTestServer;
void SendInternal(base::OnceClosure callback,
scoped_refptr<DrainableIOBuffer> buffer);
void OnSendInternalDone(base::OnceClosure callback,
scoped_refptr<DrainableIOBuffer> buffer,
int rv);
base::WeakPtr<HttpConnection> GetWeakPtr();
std::unique_ptr<StreamSocket> socket_;
const HandleRequestCallback callback_;
HttpRequestParser request_parser_;
scoped_refptr<IOBufferWithSize> read_buf_;
base::WeakPtrFactory<HttpConnection> weak_factory_{this};
} // namespace test_server
} // namespace net