This source file includes following definitions.
- Start
- OnHttpRequest
- OnWebSocketRequest
- OnWebSocketMessage
- OnClose
#include <iostream>
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/server/http_server_request_info.h"
#include "net/server/http_server_response_info.h"
#include "net/socket/tcp_listen_socket.h"
#include "sync/test/fake_server/fake_sync_server_http_handler.h"
namespace fake_server {
FakeSyncServerHttpHandler::FakeSyncServerHttpHandler() : requested_port_(0) {}
FakeSyncServerHttpHandler::FakeSyncServerHttpHandler(int port)
: requested_port_(port) {}
FakeSyncServerHttpHandler::~FakeSyncServerHttpHandler() {}
void FakeSyncServerHttpHandler::Start() {
VLOG(1) << "Starting web server";
net::TCPListenSocketFactory factory("0.0.0.0", requested_port_);
server_ = new net::HttpServer(factory, this);
net::IPEndPoint address;
int error = server_->GetLocalAddress(&address);
CHECK_EQ(net::OK, error) << base::StringPrintf(
"Error %d while trying to choose a port: %s",
error,
net::ErrorToString(error));
LOG(INFO) << base::StringPrintf("Listening on port %d", address.port());
}
void FakeSyncServerHttpHandler::OnHttpRequest(
int connection_id,
const net::HttpServerRequestInfo& info) {
VLOG(1) << "Request path: " << info.path;
int response_code = -1;
std::string response;
int server_return_value = fake_sync_server_.HandleCommand(info.data,
&response_code,
&response);
if (server_return_value == 0) {
server_->Send(connection_id, net::HttpStatusCode(response_code),
response, "text/html");
VLOG(1) << "Sync response sent: " << response;
} else {
std::string error_message = base::StringPrintf(
"Error processing sync request: error code %d. (%s)",
server_return_value,
net::ErrorToString(server_return_value));
server_->Send500(connection_id, error_message);
LOG(ERROR) << error_message;
}
}
void FakeSyncServerHttpHandler::OnWebSocketRequest(
int connection_id,
const net::HttpServerRequestInfo& info) {}
void FakeSyncServerHttpHandler::OnWebSocketMessage(int connection_id,
const std::string& data) {}
void FakeSyncServerHttpHandler::OnClose(int connection_id) {}
}