Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / sync / test / fake_server / fake_sync_server_http_handler.cc
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() {
19 }
20
21 FakeSyncServerHttpHandler::~FakeSyncServerHttpHandler() {
22 }
23
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",
34         error,
35         net::ErrorToString(error));
36
37   LOG(INFO) << base::StringPrintf("Listening on port %d", address.port());
38 }
39
40 void FakeSyncServerHttpHandler::OnHttpRequest(
41     int connection_id,
42     const net::HttpServerRequestInfo& info) {
43
44   // Hand http requests over to the sync FakeServer implementation to process
45   VLOG(1) << "Request path: " << info.path;
46   int response_code = -1;
47   std::string response;
48   int server_return_value = fake_sync_server_.HandleCommand(info.data,
49                                                             &response_code,
50                                                             &response);
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;
56   } else {
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)",
60         server_return_value,
61         net::ErrorToString(server_return_value));
62     server_->Send500(connection_id, error_message);
63     LOG(ERROR) << error_message;
64   }
65 }
66
67 void FakeSyncServerHttpHandler::OnWebSocketRequest(
68       int connection_id,
69       const net::HttpServerRequestInfo& info) {}
70
71 void FakeSyncServerHttpHandler::OnWebSocketMessage(int connection_id,
72                                   const std::string& data) {}
73
74 void FakeSyncServerHttpHandler::OnClose(int connection_id) {}
75
76 } // namespace fake_server