OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 #ifndef CHROME_TEST_WEBDRIVER_DISPATCH_H_ |
| 6 #define CHROME_TEST_WEBDRIVER_DISPATCH_H_ |
| 7 |
| 8 #include <sstream> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "base/scoped_ptr.h" |
| 14 #include "base/string_util.h" |
| 15 #include "chrome/test/webdriver/utility_functions.h" |
| 16 #include "chrome/test/webdriver/commands/command.h" |
| 17 |
| 18 #include "third_party/mongoose/mongoose.h" |
| 19 |
| 20 namespace webdriver { |
| 21 |
| 22 class Command; |
| 23 |
| 24 // Sends a |response| to a WebDriver command back to the client. |
| 25 // |connection| is the communication pipe to the HTTP server and |
| 26 // |request_info| contains any data sent by the user. |
| 27 void SendResponse(struct mg_connection* const connection, |
| 28 const struct mg_request_info* const request_info, |
| 29 const Response& response); |
| 30 |
| 31 |
| 32 // Serves as a link to the mongoose server to find if the user request |
| 33 // is an HTTP POST, GET, or DELETE and then executes the proper function |
| 34 // calls for the class that inherts from Command. An HTTP CREATE is not |
| 35 // handled and is reserved only for the establishment of a session. |
| 36 void DispatchCommand(Command* const command, const std::string& method, |
| 37 Response* response); |
| 38 |
| 39 // Template function for dispatching commands sent to the WebDriver REST |
| 40 // service. |CommandType| must be a subtype of |webdriver::Command|. |
| 41 template<typename CommandType> |
| 42 void Dispatch(struct mg_connection* connection, |
| 43 const struct mg_request_info* request_info, |
| 44 void* user_data) { |
| 45 Response response; |
| 46 |
| 47 std::string method(request_info->request_method); |
| 48 |
| 49 std::vector<std::string> path_segments; |
| 50 std::string uri(request_info->uri); |
| 51 SplitString(uri, '/', &path_segments); |
| 52 |
| 53 DictionaryValue* parameters = NULL; |
| 54 if ((method == "POST" || method == "PUT") && |
| 55 request_info->post_data_len > 0) { |
| 56 LOG(INFO) << "...parsing request body"; |
| 57 std::string json(request_info->post_data, request_info->post_data_len); |
| 58 std::string error; |
| 59 if (!ParseJSONDictionary(json, ¶meters, &error)) { |
| 60 response.set_value(Value::CreateStringValue( |
| 61 "Failed to parse command data: " + error + "\n Data: " + json)); |
| 62 response.set_status(kBadRequest); |
| 63 SendResponse(connection, request_info, response); |
| 64 return; |
| 65 } |
| 66 } |
| 67 |
| 68 LOG(INFO) << "Dispatching " << method << " " << uri |
| 69 << std::string(request_info->post_data, |
| 70 request_info->post_data_len) << std::endl; |
| 71 scoped_ptr<CommandType> ptr(new CommandType(path_segments, parameters)); |
| 72 DispatchCommand(ptr.get(), method, &response); |
| 73 SendResponse(connection, request_info, response); |
| 74 } |
| 75 } // namespace webdriver |
| 76 #endif // CHROME_TEST_WEBDRIVER_DISPATCH_H_ |
| 77 |
OLD | NEW |