http_handler.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
1// Copyright (c) 2013 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_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
6#define CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/memory/scoped_ptr.h"
12
13namespace base {
14class DictionaryValue;
15}
16
17class CommandExecutor;
18class HttpResponse;
19class Log;
20
21enum HttpMethod {
22  kGet = 0,
23  kPost,
24  kDelete,
25};
26
27struct HttpRequest {
28  HttpRequest(HttpMethod method,
29              const std::string& path,
30              const std::string& body);
31  ~HttpRequest();
32
33  HttpMethod method;
34  std::string path;
35  std::string body;
36};
37
38struct CommandMapping {
39  CommandMapping(HttpMethod method,
40                 const std::string& path_pattern,
41                 const std::string& name);
42  ~CommandMapping();
43
44  HttpMethod method;
45  std::string path_pattern;
46  std::string name;
47};
48
49class HttpHandler {
50 public:
51  typedef std::vector<CommandMapping> CommandMap;
52  static scoped_ptr<CommandMap> CreateCommandMap();
53
54  HttpHandler(Log* log,
55              scoped_ptr<CommandExecutor> executor,
56              scoped_ptr<std::vector<CommandMapping> > commands,
57              const std::string& url_base);
58  ~HttpHandler();
59
60  void Handle(const HttpRequest& request,
61              HttpResponse* response);
62
63  bool ShouldShutdown(const HttpRequest& request);
64
65 private:
66  void HandleInternal(const HttpRequest& request,
67                      HttpResponse* response);
68  bool HandleWebDriverCommand(
69      const HttpRequest& request,
70      const std::string& trimmed_path,
71      HttpResponse* response);
72
73  Log* log_;
74  scoped_ptr<CommandExecutor> executor_;
75  scoped_ptr<CommandMap> command_map_;
76  std::string url_base_;
77};
78
79namespace internal {
80extern const char kNewSessionIdCommand[];
81bool MatchesCommand(HttpMethod method,
82                    const std::string& path,
83                    const CommandMapping& command,
84                    std::string* session_id,
85                    base::DictionaryValue* out_params);
86}  // namespace internal
87
88#endif  // CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
89