OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/test/webdriver/dispatch.h" | 5 #include "chrome/test/webdriver/dispatch.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/json/json_reader.h" | |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | |
13 #include "base/message_loop_proxy.h" | 15 #include "base/message_loop_proxy.h" |
14 #include "base/string_split.h" | 16 #include "base/string_split.h" |
15 #include "base/string_util.h" | 17 #include "base/string_util.h" |
16 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
17 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
18 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
19 #include "chrome/test/webdriver/http_response.h" | 21 #include "chrome/test/webdriver/http_response.h" |
20 #include "chrome/test/webdriver/commands/command.h" | 22 #include "chrome/test/webdriver/commands/command.h" |
21 #include "chrome/test/webdriver/session_manager.h" | 23 #include "chrome/test/webdriver/session_manager.h" |
22 #include "chrome/test/webdriver/utility_functions.h" | 24 #include "chrome/test/webdriver/utility_functions.h" |
25 #include "chrome/test/webdriver/webdriver_logging.h" | |
Huyen
2011/06/08 19:55:52
Oh, did you forget to add webdriver_logging to thi
| |
23 | 26 |
24 namespace webdriver { | 27 namespace webdriver { |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
28 bool ForbidsMessageBody(const std::string& request_method, | 31 bool ForbidsMessageBody(const std::string& request_method, |
29 const HttpResponse& response) { | 32 const HttpResponse& response) { |
30 return request_method == "HEAD" || | 33 return request_method == "HEAD" || |
31 response.status() == HttpResponse::kNoContent || | 34 response.status() == HttpResponse::kNoContent || |
32 response.status() == HttpResponse::kNotModified || | 35 response.status() == HttpResponse::kNotModified || |
(...skipping 19 matching lines...) Expand all Loading... | |
52 | 55 |
53 void Shutdown(struct mg_connection* connection, | 56 void Shutdown(struct mg_connection* connection, |
54 const struct mg_request_info* request_info, | 57 const struct mg_request_info* request_info, |
55 void* user_data) { | 58 void* user_data) { |
56 base::WaitableEvent* shutdown_event = | 59 base::WaitableEvent* shutdown_event = |
57 reinterpret_cast<base::WaitableEvent*>(user_data); | 60 reinterpret_cast<base::WaitableEvent*>(user_data); |
58 mg_printf(connection, "HTTP/1.1 200 OK\r\n\r\n"); | 61 mg_printf(connection, "HTTP/1.1 200 OK\r\n\r\n"); |
59 shutdown_event->Signal(); | 62 shutdown_event->Signal(); |
60 } | 63 } |
61 | 64 |
62 void SendStatus(struct mg_connection* connection, | 65 void SendOkWithBody(struct mg_connection* connection, |
63 const struct mg_request_info* request_info, | 66 const std::string& content) { |
Huyen
2011/06/08 19:17:54
line up indentation of parameters.
kkania
2011/06/08 21:10:49
Done.
| |
64 void* user_data) { | 67 const char* response_fmt = "HTTP/1.1 200 OK\r\n" |
65 std::string response = "HTTP/1.1 200 OK\r\n" | 68 "Content-Length:%d\r\n\r\n" |
66 "Content-Length:2\r\n\r\n" | 69 "%s"; |
67 "ok"; | 70 std::string response = base::StringPrintf( |
71 response_fmt, content.length(), content.c_str()); | |
68 mg_write(connection, response.data(), response.length()); | 72 mg_write(connection, response.data(), response.length()); |
69 } | 73 } |
70 | 74 |
75 void SendHealthz(struct mg_connection* connection, | |
76 const struct mg_request_info* request_info, | |
Huyen
2011/06/08 19:17:54
line up indentation of parameters.
kkania
2011/06/08 21:10:49
Done.
| |
77 void* user_data) { | |
78 SendOkWithBody(connection, "ok"); | |
79 } | |
80 | |
81 void SendLog(struct mg_connection* connection, | |
82 const struct mg_request_info* request_info, | |
Huyen
2011/06/08 19:17:54
line up indentation of parameters.
kkania
2011/06/08 21:10:49
Done.
| |
83 void* user_data) { | |
84 std::string content, log; | |
85 if (GetLogContents(&log)) { | |
Huyen
2011/06/08 19:17:54
Does GetLogContents() get all the previous logs? I
kkania
2011/06/08 21:10:49
If I send a message over ~32MB, it fails quickly.
| |
86 // KIWI Another \n before END? | |
Huyen
2011/06/08 19:17:54
Remove KIWI?
kkania
2011/06/08 21:10:49
Done.
| |
87 content = "START ChromeDriver log:\n" + log + "END ChromeDriver log"; | |
88 } else { | |
89 content = "No ChromeDriver log found"; | |
90 } | |
91 SendOkWithBody(connection, content); | |
92 } | |
93 | |
71 void SendNoContentResponse(struct mg_connection* connection, | 94 void SendNoContentResponse(struct mg_connection* connection, |
72 const struct mg_request_info* request_info, | 95 const struct mg_request_info* request_info, |
73 void* user_data) { | 96 void* user_data) { |
74 std::string response = "HTTP/1.1 204 No Content\r\n" | 97 std::string response = "HTTP/1.1 204 No Content\r\n" |
75 "Content-Length:0\r\n" | 98 "Content-Length:0\r\n" |
76 "\r\n"; | 99 "\r\n"; |
77 mg_write(connection, response.data(), response.length()); | 100 mg_write(connection, response.data(), response.length()); |
78 } | 101 } |
79 | 102 |
80 void SendForbidden(struct mg_connection* connection, | 103 void SendForbidden(struct mg_connection* connection, |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 else if (*method == "PUT") | 241 else if (*method == "PUT") |
219 *method = "POST"; | 242 *method = "POST"; |
220 | 243 |
221 std::string uri(request_info->uri); | 244 std::string uri(request_info->uri); |
222 SessionManager* manager = SessionManager::GetInstance(); | 245 SessionManager* manager = SessionManager::GetInstance(); |
223 uri = uri.substr(manager->url_base().length()); | 246 uri = uri.substr(manager->url_base().length()); |
224 | 247 |
225 base::SplitString(uri, '/', path_segments); | 248 base::SplitString(uri, '/', path_segments); |
226 | 249 |
227 if (*method == "POST" && request_info->post_data_len > 0) { | 250 if (*method == "POST" && request_info->post_data_len > 0) { |
228 VLOG(1) << "...parsing request body"; | |
229 std::string json(request_info->post_data, request_info->post_data_len); | 251 std::string json(request_info->post_data, request_info->post_data_len); |
230 std::string error; | 252 std::string error_msg; |
231 if (!ParseJSONDictionary(json, parameters, &error)) { | 253 scoped_ptr<Value> params(base::JSONReader::ReadAndReturnError( |
254 json, true, NULL, &error_msg)); | |
255 if (!params.get()) { | |
232 response->SetError(new Error( | 256 response->SetError(new Error( |
233 kBadRequest, | 257 kBadRequest, |
234 "Failed to parse command data: " + error + "\n Data: " + json)); | 258 "Failed to parse command data: " + error_msg + "\n Data: " + json)); |
235 return false; | 259 return false; |
236 } | 260 } |
261 if (!params->IsType(Value::TYPE_DICTIONARY)) { | |
262 response->SetError(new Error( | |
263 kBadRequest, | |
264 "Data passed in URL must be a dictionary. Data: " + json)); | |
265 return false; | |
266 } | |
267 *parameters = static_cast<DictionaryValue*>(params.release()); | |
237 } | 268 } |
238 VLOG(1) << "Parsed " << method << " " << uri | |
239 << std::string(request_info->post_data, request_info->post_data_len); | |
240 return true; | 269 return true; |
241 } | 270 } |
242 | 271 |
243 void DispatchHelper(Command* command_ptr, | 272 void DispatchHelper(Command* command_ptr, |
244 const std::string& method, | 273 const std::string& method, |
245 Response* response) { | 274 Response* response) { |
246 CHECK(method == "GET" || method == "POST" || method == "DELETE"); | 275 CHECK(method == "GET" || method == "POST" || method == "DELETE"); |
247 scoped_ptr<Command> command(command_ptr); | 276 scoped_ptr<Command> command(command_ptr); |
248 | 277 |
249 if ((method == "GET" && !command->DoesGet()) || | 278 if ((method == "GET" && !command->DoesGet()) || |
(...skipping 26 matching lines...) Expand all Loading... | |
276 } | 305 } |
277 | 306 |
278 Dispatcher::~Dispatcher() {} | 307 Dispatcher::~Dispatcher() {} |
279 | 308 |
280 void Dispatcher::AddShutdown(const std::string& pattern, | 309 void Dispatcher::AddShutdown(const std::string& pattern, |
281 base::WaitableEvent* shutdown_event) { | 310 base::WaitableEvent* shutdown_event) { |
282 mg_set_uri_callback(context_, (root_ + pattern).c_str(), &Shutdown, | 311 mg_set_uri_callback(context_, (root_ + pattern).c_str(), &Shutdown, |
283 shutdown_event); | 312 shutdown_event); |
284 } | 313 } |
285 | 314 |
286 void Dispatcher::AddStatus(const std::string& pattern) { | 315 void Dispatcher::AddHealthz(const std::string& pattern) { |
287 mg_set_uri_callback(context_, (root_ + pattern).c_str(), &SendStatus, NULL); | 316 mg_set_uri_callback(context_, (root_ + pattern).c_str(), &SendHealthz, NULL); |
317 } | |
318 | |
319 void Dispatcher::AddLog(const std::string& pattern) { | |
320 mg_set_uri_callback(context_, (root_ + pattern).c_str(), &SendLog, NULL); | |
288 } | 321 } |
289 | 322 |
290 void Dispatcher::SetNotImplemented(const std::string& pattern) { | 323 void Dispatcher::SetNotImplemented(const std::string& pattern) { |
291 mg_set_uri_callback(context_, (root_ + pattern).c_str(), | 324 mg_set_uri_callback(context_, (root_ + pattern).c_str(), |
292 &SendNotImplementedError, NULL); | 325 &SendNotImplementedError, NULL); |
293 } | 326 } |
294 | 327 |
295 void Dispatcher::ForbidAllOtherRequests() { | 328 void Dispatcher::ForbidAllOtherRequests() { |
296 mg_set_uri_callback(context_, "*", &SendForbidden, NULL); | 329 mg_set_uri_callback(context_, "*", &SendForbidden, NULL); |
297 } | 330 } |
298 | 331 |
299 } // namespace webdriver | 332 } // namespace webdriver |
OLD | NEW |