| 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 #ifndef CHROME_TEST_WEBDRIVER_DISPATCH_H_ | 5 #ifndef CHROME_TEST_WEBDRIVER_DISPATCH_H_ |
| 6 #define CHROME_TEST_WEBDRIVER_DISPATCH_H_ | 6 #define CHROME_TEST_WEBDRIVER_DISPATCH_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/logging.h" | |
| 13 #include "chrome/test/webdriver/commands/response.h" | 12 #include "chrome/test/webdriver/commands/response.h" |
| 14 #include "third_party/mongoose/mongoose.h" | 13 #include "third_party/mongoose/mongoose.h" |
| 15 | 14 |
| 16 class DictionaryValue; | 15 class DictionaryValue; |
| 17 | 16 |
| 18 namespace base { | 17 namespace base { |
| 19 class WaitableEvent; | 18 class WaitableEvent; |
| 20 } | 19 } |
| 21 | 20 |
| 22 namespace webdriver { | 21 namespace webdriver { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 void* user_data) { | 61 void* user_data) { |
| 63 std::string method; | 62 std::string method; |
| 64 std::vector<std::string> path_segments; | 63 std::vector<std::string> path_segments; |
| 65 DictionaryValue* parameters = NULL; | 64 DictionaryValue* parameters = NULL; |
| 66 Response response; | 65 Response response; |
| 67 if (internal::ParseRequestInfo(request_info, | 66 if (internal::ParseRequestInfo(request_info, |
| 68 &method, | 67 &method, |
| 69 &path_segments, | 68 &path_segments, |
| 70 ¶meters, | 69 ¶meters, |
| 71 &response)) { | 70 &response)) { |
| 72 std::string post_data(request_info->post_data, request_info->post_data_len); | |
| 73 LOG(INFO) << "Received command, url: " << request_info->uri | |
| 74 << ", method: " << request_info->request_method | |
| 75 << ", postd data: " << post_data; | |
| 76 internal::DispatchHelper( | 71 internal::DispatchHelper( |
| 77 new CommandType(path_segments, parameters), | 72 new CommandType(path_segments, parameters), |
| 78 method, | 73 method, |
| 79 &response); | 74 &response); |
| 80 } | 75 } |
| 76 |
| 81 internal::SendResponse(connection, | 77 internal::SendResponse(connection, |
| 82 request_info->request_method, | 78 request_info->request_method, |
| 83 response); | 79 response); |
| 84 LOG(INFO) << "Sent command response, url: " << request_info->uri; | |
| 85 } | 80 } |
| 86 | 81 |
| 87 class Dispatcher { | 82 class Dispatcher { |
| 88 public: | 83 public: |
| 89 // Creates a new dispatcher that will register all URL callbacks with the | 84 // Creates a new dispatcher that will register all URL callbacks with the |
| 90 // given |context|. Each callback's pattern will be prefixed with the provided | 85 // given |context|. Each callback's pattern will be prefixed with the provided |
| 91 // |root|. | 86 // |root|. |
| 92 Dispatcher(struct mg_context* context, const std::string& root); | 87 Dispatcher(struct mg_context* context, const std::string& root); |
| 93 ~Dispatcher(); | 88 ~Dispatcher(); |
| 94 | 89 |
| 95 // Registers a callback for a WebDriver command using the given URL |pattern|. | 90 // Registers a callback for a WebDriver command using the given URL |pattern|. |
| 96 // The |CommandType| must be a subtype of |webdriver::Command|. | 91 // The |CommandType| must be a subtype of |webdriver::Command|. |
| 97 template<typename CommandType> | 92 template<typename CommandType> |
| 98 void Add(const std::string& pattern); | 93 void Add(const std::string& pattern); |
| 99 | 94 |
| 100 // Registers a callback that will shutdown the server. When any HTTP request | 95 // Registers a callback that will shutdown the server. When any HTTP request |
| 101 // is received at this URL |pattern|, the |shutdown_event| will be signaled. | 96 // is received at this URL |pattern|, the |shutdown_event| will be signaled. |
| 102 void AddShutdown(const std::string& pattern, | 97 void AddShutdown(const std::string& pattern, |
| 103 base::WaitableEvent* shutdown_event); | 98 base::WaitableEvent* shutdown_event); |
| 104 | 99 |
| 105 // Registers a callback for the given pattern that will return a simple | 100 // Registers a callback for the given pattern that will return a simple |
| 106 // "HTTP/1.1 200 OK" message with "ok" in the body. Used for checking the | 101 // "HTTP/1.1 200 OK" message with "ok" in the body. Used for checking the |
| 107 // status of the server. | 102 // status of the server. |
| 108 void AddHealthz(const std::string& pattern); | 103 void AddStatus(const std::string& pattern); |
| 109 | |
| 110 // Registers a callback for the given pattern that will return the current | |
| 111 // WebDriver log contents. | |
| 112 void AddLog(const std::string& pattern); | |
| 113 | 104 |
| 114 // Registers a callback that will always respond with a | 105 // Registers a callback that will always respond with a |
| 115 // "HTTP/1.1 501 Not Implemented" message. | 106 // "HTTP/1.1 501 Not Implemented" message. |
| 116 void SetNotImplemented(const std::string& pattern); | 107 void SetNotImplemented(const std::string& pattern); |
| 117 | 108 |
| 118 // Registers a callback that will respond for all other requests with a | 109 // Registers a callback that will respond for all other requests with a |
| 119 // "HTTP/1.1 403 Forbidden" message. Should be called only after registering | 110 // "HTTP/1.1 403 Forbidden" message. Should be called only after registering |
| 120 // other callbacks. | 111 // other callbacks. |
| 121 void ForbidAllOtherRequests(); | 112 void ForbidAllOtherRequests(); |
| 122 | 113 |
| 123 private: | 114 private: |
| 124 struct mg_context* context_; | 115 struct mg_context* context_; |
| 125 const std::string root_; | 116 const std::string root_; |
| 126 | 117 |
| 127 DISALLOW_COPY_AND_ASSIGN(Dispatcher); | 118 DISALLOW_COPY_AND_ASSIGN(Dispatcher); |
| 128 }; | 119 }; |
| 129 | 120 |
| 130 | 121 |
| 131 template <typename CommandType> | 122 template <typename CommandType> |
| 132 void Dispatcher::Add(const std::string& pattern) { | 123 void Dispatcher::Add(const std::string& pattern) { |
| 133 mg_set_uri_callback(context_, (root_ + pattern).c_str(), | 124 mg_set_uri_callback(context_, (root_ + pattern).c_str(), |
| 134 &Dispatch<CommandType>, NULL); | 125 &Dispatch<CommandType>, NULL); |
| 135 } | 126 } |
| 136 | 127 |
| 137 } // namespace webdriver | 128 } // namespace webdriver |
| 138 | 129 |
| 139 #endif // CHROME_TEST_WEBDRIVER_DISPATCH_H_ | 130 #endif // CHROME_TEST_WEBDRIVER_DISPATCH_H_ |
| OLD | NEW |