OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/gdata/test_servers/http_server.h" |
| 6 |
| 7 #include "base/synchronization/waitable_event.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 9 |
| 10 #include <string> |
| 11 #include <sstream> |
| 12 #include "base/stl_util.h" |
| 13 |
| 14 namespace gdata { |
| 15 namespace test_servers { |
| 16 |
| 17 using content::BrowserThread; |
| 18 |
| 19 // static |
| 20 scoped_refptr<HttpServer> HttpServer::CreateServer( |
| 21 const std::string& address, |
| 22 int port, |
| 23 HttpConnectionDelegate* connection_delegate) { |
| 24 |
| 25 // Launching the server class in the IO thread and waiting for the result. |
| 26 // TODO(mtomasz): Change it to a completely new thread. |
| 27 base::WaitableEvent waiter(false, false); |
| 28 HttpServer* server = NULL; |
| 29 BrowserThread::PostTask( |
| 30 BrowserThread::IO, |
| 31 FROM_HERE, |
| 32 base::Bind(HttpServer::CreateServerOnIOThread, |
| 33 address, |
| 34 port, |
| 35 connection_delegate, |
| 36 &waiter, |
| 37 &server)); |
| 38 waiter.Wait(); |
| 39 |
| 40 return server; |
| 41 } |
| 42 |
| 43 // static |
| 44 void HttpServer::CreateServerOnIOThread( |
| 45 const std::string& address, |
| 46 int port, |
| 47 HttpConnectionDelegate* connection_delegate, |
| 48 base::WaitableEvent* waiter, |
| 49 HttpServer** server_ptr) { |
| 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 51 |
| 52 SocketDescriptor socket_descriptor = HttpServer::CreateAndBind(address, port); |
| 53 HttpServer* server = NULL; |
| 54 if (socket_descriptor != HttpServer::kInvalidSocket) { |
| 55 server = new HttpServer(socket_descriptor, connection_delegate); |
| 56 server->Listen(); |
| 57 } |
| 58 |
| 59 // Return the value and notify the caller (if provided) that the value |
| 60 // is available. |
| 61 *server_ptr = server; |
| 62 if (waiter) |
| 63 waiter->Signal(); |
| 64 } |
| 65 |
| 66 void HttpServer::DropHttpConnection(HttpConnection* connection) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 68 |
| 69 connections_.erase(connection->socket_.get()); |
| 70 delete connection; |
| 71 } |
| 72 |
| 73 HttpServer::HttpServer(SocketDescriptor& socket, |
| 74 HttpConnectionDelegate* connection_delegate) : |
| 75 net::TCPListenSocket(socket, ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 76 connection_delegate_(connection_delegate) { |
| 77 } |
| 78 |
| 79 HttpServer::~HttpServer() { |
| 80 STLDeleteContainerPairSecondPointers(connections_.begin(), |
| 81 connections_.end()); |
| 82 } |
| 83 |
| 84 void HttpServer::DidAccept(StreamListenSocket* server, |
| 85 StreamListenSocket* connection) { |
| 86 HttpConnection* http_connection = new HttpConnection(connection, |
| 87 connection_delegate_); |
| 88 connections_[connection] = http_connection; |
| 89 } |
| 90 |
| 91 void HttpServer::DidRead(StreamListenSocket* connection, |
| 92 const char* data, |
| 93 int length) { |
| 94 HttpConnection* http_connection = FindConnection(connection); |
| 95 if (http_connection == NULL) { |
| 96 LOG(ERROR) << "Unknown connection."; |
| 97 return; |
| 98 } |
| 99 if (!http_connection->ReceiveData(data, length)) { |
| 100 // Error occured during receiving data, or the request has not been |
| 101 // handled by any of request handlers, so we are dropping the connection. |
| 102 DropHttpConnection(http_connection); |
| 103 } |
| 104 } |
| 105 |
| 106 void HttpServer::DidClose(StreamListenSocket* connection) { |
| 107 HttpConnection* http_connection = FindConnection(connection); |
| 108 if (http_connection == NULL) { |
| 109 LOG(ERROR) << "Unknown connection."; |
| 110 return; |
| 111 } |
| 112 delete http_connection; |
| 113 connections_.erase(connection); |
| 114 } |
| 115 |
| 116 HttpConnection* HttpServer::FindConnection(StreamListenSocket* socket) { |
| 117 std::map<StreamListenSocket*, HttpConnection*>::iterator it = |
| 118 connections_.find(socket); |
| 119 if (it == connections_.end()) { |
| 120 return NULL; |
| 121 } |
| 122 return it->second; |
| 123 } |
| 124 |
| 125 } // namespace test_servers |
| 126 } // namespace gdata |
OLD | NEW |