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.
7 #include "base/strings/string_util.h"
8 #include "base/strings/stringprintf.h"
9 #include "net/base/ip_endpoint.h"
10 #include "net/base/net_errors.h"
11 #include "net/server/http_server_request_info.h"
12 #include "net/server/http_server_response_info.h"
13 #include "net/socket/tcp_listen_socket.h"
14 #include "sync/test/fake_server/fake_sync_server_http_handler.h"
16 namespace fake_server {
18 FakeSyncServerHttpHandler::FakeSyncServerHttpHandler() {
21 FakeSyncServerHttpHandler::~FakeSyncServerHttpHandler() {
24 // Note that this must be called from within an IO MessageLoop because it
25 // initializes a net::HttpServer.
26 void FakeSyncServerHttpHandler::Start() {
27 VLOG(1) << "Starting web server";
28 net::TCPListenSocketFactory factory("0.0.0.0", 0);
29 server_ = new net::HttpServer(factory, this);
30 net::IPEndPoint address;
31 int error = server_->GetLocalAddress(&address);
32 CHECK_EQ(net::OK, error) << base::StringPrintf(
33 "Error %d while trying to choose a port: %s",
35 net::ErrorToString(error));
37 LOG(INFO) << base::StringPrintf("Listening on port %d", address.port());
40 void FakeSyncServerHttpHandler::OnHttpRequest(
42 const net::HttpServerRequestInfo& info) {
44 // Hand http requests over to the sync FakeServer implementation to process
45 VLOG(1) << "Request path: " << info.path;
46 int response_code = -1;
48 int server_return_value = fake_sync_server_.HandleCommand(info.data,
51 if (server_return_value == 0) {
52 // A '0' error code indicates a successful request to FakeServer
53 server_->Send(connection_id, net::HttpStatusCode(response_code),
54 response, "text/html");
55 VLOG(1) << "Sync response sent: " << response;
57 // The FakeServer returned a non-0 error code.
58 std::string error_message = base::StringPrintf(
59 "Error processing sync request: error code %d. (%s)",
61 net::ErrorToString(server_return_value));
62 server_->Send500(connection_id, error_message);
63 LOG(ERROR) << error_message;
67 void FakeSyncServerHttpHandler::OnWebSocketRequest(
69 const net::HttpServerRequestInfo& info) {}
71 void FakeSyncServerHttpHandler::OnWebSocketMessage(int connection_id,
72 const std::string& data) {}
74 void FakeSyncServerHttpHandler::OnClose(int connection_id) {}
76 } // namespace fake_server