| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include <iostream> | |
| 6 | |
| 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" | |
| 15 | |
| 16 namespace fake_server { | |
| 17 | |
| 18 FakeSyncServerHttpHandler::FakeSyncServerHttpHandler() : requested_port_(0) {} | |
| 19 | |
| 20 FakeSyncServerHttpHandler::FakeSyncServerHttpHandler(int port) | |
| 21 : requested_port_(port) {} | |
| 22 | |
| 23 FakeSyncServerHttpHandler::~FakeSyncServerHttpHandler() {} | |
| 24 | |
| 25 // Note that this must be called from within an IO MessageLoop because it | |
| 26 // initializes a net::HttpServer. | |
| 27 void FakeSyncServerHttpHandler::Start() { | |
| 28 VLOG(1) << "Starting web server"; | |
| 29 net::TCPListenSocketFactory factory("0.0.0.0", requested_port_); | |
| 30 server_ = new net::HttpServer(factory, this); | |
| 31 net::IPEndPoint address; | |
| 32 int error = server_->GetLocalAddress(&address); | |
| 33 CHECK_EQ(net::OK, error) << base::StringPrintf( | |
| 34 "Error %d while trying to choose a port: %s", | |
| 35 error, | |
| 36 net::ErrorToString(error)); | |
| 37 | |
| 38 LOG(INFO) << base::StringPrintf("Listening on port %d", address.port()); | |
| 39 } | |
| 40 | |
| 41 void FakeSyncServerHttpHandler::OnHttpRequest( | |
| 42 int connection_id, | |
| 43 const net::HttpServerRequestInfo& info) { | |
| 44 | |
| 45 // Hand http requests over to the sync FakeServer implementation to process | |
| 46 VLOG(1) << "Request path: " << info.path; | |
| 47 int response_code = -1; | |
| 48 std::string response; | |
| 49 int server_return_value = fake_sync_server_.HandleCommand(info.data, | |
| 50 &response_code, | |
| 51 &response); | |
| 52 if (server_return_value == 0) { | |
| 53 // A '0' error code indicates a successful request to FakeServer | |
| 54 server_->Send(connection_id, net::HttpStatusCode(response_code), | |
| 55 response, "text/html"); | |
| 56 VLOG(1) << "Sync response sent: " << response; | |
| 57 } else { | |
| 58 // The FakeServer returned a non-0 error code. | |
| 59 std::string error_message = base::StringPrintf( | |
| 60 "Error processing sync request: error code %d. (%s)", | |
| 61 server_return_value, | |
| 62 net::ErrorToString(server_return_value)); | |
| 63 server_->Send500(connection_id, error_message); | |
| 64 LOG(ERROR) << error_message; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 void FakeSyncServerHttpHandler::OnWebSocketRequest( | |
| 69 int connection_id, | |
| 70 const net::HttpServerRequestInfo& info) {} | |
| 71 | |
| 72 void FakeSyncServerHttpHandler::OnWebSocketMessage(int connection_id, | |
| 73 const std::string& data) {} | |
| 74 | |
| 75 void FakeSyncServerHttpHandler::OnClose(int connection_id) {} | |
| 76 | |
| 77 } // namespace fake_server | |
| OLD | NEW |