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_listen_socket.h" | |
14 #include "url/gurl.h" | 13 #include "url/gurl.h" |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 const int kDeviceBusyTimeout = 30; // in seconds | 17 const int kDeviceBusyTimeout = 30; // in seconds |
19 const int kPendingUserActionTimeout = 5; // in seconds | 18 const int kPendingUserActionTimeout = 5; // in seconds |
20 | 19 |
21 const char kPrivetInfo[] = "/privet/info"; | 20 const char kPrivetInfo[] = "/privet/info"; |
22 const char kPrivetRegister[] = "/privet/register"; | 21 const char kPrivetRegister[] = "/privet/register"; |
23 const char kPrivetCapabilities[] = "/privet/capabilities"; | 22 const char kPrivetCapabilities[] = "/privet/capabilities"; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 97 } |
99 | 98 |
100 PrivetHttpServer::~PrivetHttpServer() { | 99 PrivetHttpServer::~PrivetHttpServer() { |
101 Shutdown(); | 100 Shutdown(); |
102 } | 101 } |
103 | 102 |
104 bool PrivetHttpServer::Start(uint16 port) { | 103 bool PrivetHttpServer::Start(uint16 port) { |
105 if (server_) | 104 if (server_) |
106 return true; | 105 return true; |
107 | 106 |
108 net::TCPListenSocketFactory factory("0.0.0.0", port); | 107 server_.reset(net::HttpServer::Create("0.0.0.0", port, this)); |
109 server_ = new net::HttpServer(factory, this); | 108 if (!server_) { |
| 109 NOTREACHED() << "Cannot start HTTP server"; |
| 110 return false; |
| 111 } |
| 112 |
110 net::IPEndPoint address; | 113 net::IPEndPoint address; |
111 | |
112 if (server_->GetLocalAddress(&address) != net::OK) { | 114 if (server_->GetLocalAddress(&address) != net::OK) { |
113 NOTREACHED() << "Cannot start HTTP server"; | 115 NOTREACHED() << "Cannot start HTTP server"; |
114 return false; | 116 return false; |
115 } | 117 } |
116 | 118 |
117 VLOG(1) << "Address of HTTP server: " << address.ToString(); | 119 VLOG(1) << "Address of HTTP server: " << address.ToString(); |
118 return true; | 120 return true; |
119 } | 121 } |
120 | 122 |
121 void PrivetHttpServer::Shutdown() { | 123 void PrivetHttpServer::Shutdown() { |
122 if (!server_) | 124 if (!server_) |
123 return; | 125 return; |
124 | 126 |
125 server_ = NULL; | 127 server_.reset(NULL); |
126 } | 128 } |
127 | 129 |
128 void PrivetHttpServer::OnHttpRequest(int connection_id, | 130 void PrivetHttpServer::OnHttpRequest(int connection_id, |
129 const net::HttpServerRequestInfo& info) { | 131 const net::HttpServerRequestInfo& info) { |
130 VLOG(1) << "Processing HTTP request: " << info.path; | 132 VLOG(1) << "Processing HTTP request: " << info.path; |
131 GURL url("http://host" + info.path); | 133 GURL url("http://host" + info.path); |
132 | 134 |
133 if (!ValidateRequestMethod(connection_id, url.path(), info.method)) | 135 if (!ValidateRequestMethod(connection_id, url.path(), info.method)) |
134 return; | 136 return; |
135 | 137 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 delegate_->GetRegistrationServerError(&description); | 496 delegate_->GetRegistrationServerError(&description); |
495 *current_response = CreateErrorWithDescription("server_error", | 497 *current_response = CreateErrorWithDescription("server_error", |
496 description); | 498 description); |
497 break; | 499 break; |
498 } | 500 } |
499 | 501 |
500 default: | 502 default: |
501 NOTREACHED(); | 503 NOTREACHED(); |
502 }; | 504 }; |
503 } | 505 } |
OLD | NEW |