OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cloud_print/gcp20/prototype/privet_http_server.h" | 5 #include "cloud_print/gcp20/prototype/privet_http_server.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "net/base/ip_endpoint.h" | 10 #include "net/base/ip_endpoint.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/base/url_util.h" | 12 #include "net/base/url_util.h" |
13 #include "net/socket/tcp_server_socket.h" | 13 #include "net/socket/tcp_listen_socket.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const int kDeviceBusyTimeout = 30; // in seconds | 18 const int kDeviceBusyTimeout = 30; // in seconds |
19 const int kPendingUserActionTimeout = 5; // in seconds | 19 const int kPendingUserActionTimeout = 5; // in seconds |
20 | 20 |
21 const char kPrivetInfo[] = "/privet/info"; | 21 const char kPrivetInfo[] = "/privet/info"; |
22 const char kPrivetRegister[] = "/privet/register"; | 22 const char kPrivetRegister[] = "/privet/register"; |
23 const char kPrivetCapabilities[] = "/privet/capabilities"; | 23 const char kPrivetCapabilities[] = "/privet/capabilities"; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 98 } |
99 | 99 |
100 PrivetHttpServer::~PrivetHttpServer() { | 100 PrivetHttpServer::~PrivetHttpServer() { |
101 Shutdown(); | 101 Shutdown(); |
102 } | 102 } |
103 | 103 |
104 bool PrivetHttpServer::Start(uint16 port) { | 104 bool PrivetHttpServer::Start(uint16 port) { |
105 if (server_) | 105 if (server_) |
106 return true; | 106 return true; |
107 | 107 |
108 scoped_ptr<net::ServerSocket> server_socket( | 108 net::TCPListenSocketFactory factory("0.0.0.0", port); |
109 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 109 server_ = new net::HttpServer(factory, this); |
110 server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); | 110 net::IPEndPoint address; |
111 server_.reset(new net::HttpServer(server_socket.Pass(), this)); | |
112 | 111 |
113 net::IPEndPoint address; | |
114 if (server_->GetLocalAddress(&address) != net::OK) { | 112 if (server_->GetLocalAddress(&address) != net::OK) { |
115 NOTREACHED() << "Cannot start HTTP server"; | 113 NOTREACHED() << "Cannot start HTTP server"; |
116 return false; | 114 return false; |
117 } | 115 } |
118 | 116 |
119 VLOG(1) << "Address of HTTP server: " << address.ToString(); | 117 VLOG(1) << "Address of HTTP server: " << address.ToString(); |
120 return true; | 118 return true; |
121 } | 119 } |
122 | 120 |
123 void PrivetHttpServer::Shutdown() { | 121 void PrivetHttpServer::Shutdown() { |
124 if (!server_) | 122 if (!server_) |
125 return; | 123 return; |
126 | 124 |
127 server_.reset(NULL); | 125 server_ = NULL; |
128 } | 126 } |
129 | 127 |
130 void PrivetHttpServer::OnHttpRequest(int connection_id, | 128 void PrivetHttpServer::OnHttpRequest(int connection_id, |
131 const net::HttpServerRequestInfo& info) { | 129 const net::HttpServerRequestInfo& info) { |
132 VLOG(1) << "Processing HTTP request: " << info.path; | 130 VLOG(1) << "Processing HTTP request: " << info.path; |
133 GURL url("http://host" + info.path); | 131 GURL url("http://host" + info.path); |
134 | 132 |
135 if (!ValidateRequestMethod(connection_id, url.path(), info.method)) | 133 if (!ValidateRequestMethod(connection_id, url.path(), info.method)) |
136 return; | 134 return; |
137 | 135 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 delegate_->GetRegistrationServerError(&description); | 494 delegate_->GetRegistrationServerError(&description); |
497 *current_response = CreateErrorWithDescription("server_error", | 495 *current_response = CreateErrorWithDescription("server_error", |
498 description); | 496 description); |
499 break; | 497 break; |
500 } | 498 } |
501 | 499 |
502 default: | 500 default: |
503 NOTREACHED(); | 501 NOTREACHED(); |
504 }; | 502 }; |
505 } | 503 } |
OLD | NEW |