summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/dispatch.cc
blob: a24b43e7738ae02b17fb78edaad89d3ad29acc43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/webdriver/dispatch.h"

#include <sstream>
#include <string>

#include "base/json/json_writer.h"
#include "chrome/test/webdriver/commands/command.h"

namespace webdriver {

// The standard HTTP Status codes are implemented below.  Chrome uses
// OK, See Other, Not Found, Method Not Allowed, and Internal Error.
// Internal Error, HTTP 500, is used as a catch all for any issue
// not covered in the JSON protocol.
void SendHttpOk(struct mg_connection* const connection,
                const struct mg_request_info* const request_info,
                const Response& response) {
  const std::string json = response.ToJSON();
  std::ostringstream out;
  out << "HTTP/1.1 200 OK\r\n"
      << "Content-Length: " << strlen(json.c_str()) << "\r\n"
      << "Content-Type: application/json; charset=UTF-8\r\n"
      << "Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept\r\n"
      << "Accept-Ranges: bytes\r\n"
      << "Connection: close\r\n\r\n";
  if (strcmp(request_info->request_method, "HEAD") != 0)
    out << json << "\r\n";
  VLOG(1) << out.str();
  mg_printf(connection, "%s", out.str().c_str());
}

void SendHttpSeeOther(struct mg_connection* const connection,
                      const struct mg_request_info* const request_info,
                      const Response& response) {
  const Value* value = response.value();
  CheckValueType(Value::TYPE_STRING, value);
  std::string location;
  if (!value->GetAsString(&location)) {
    NOTREACHED();
  }

  std::ostringstream out;
  out << "HTTP/1.1 303 See Other\r\n"
      << "Location: " << location << "\r\n"
      << "Content-Type: text/html\r\n"
      << "Content-Length: 0\r\n\r\n";
  VLOG(1) << out.str();
  mg_printf(connection, "%s", out.str().c_str());
}

void SendHttpBadRequest(struct mg_connection* const connection,
                        const struct mg_request_info* const request_info,
                        const Response& response) {
  const std::string json = response.ToJSON();
  std::ostringstream out;
  out << "HTTP/1.1 400 Bad Request\r\n"
      << "Content-Length: " << strlen(json.c_str()) << "\r\n"
      << "Content-Type: application/json; charset=UTF-8\r\n"
      << "Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept\r\n"
      << "Accept-Ranges: bytes\r\n"
      << "Connection: close\r\n\r\n";
  if (strcmp(request_info->request_method, "HEAD") != 0)
    out << json << "\r\n";
  VLOG(1) << out.str();
  mg_printf(connection, "%s", out.str().c_str());
}

void SendHttpNotFound(struct mg_connection* const connection,
                      const struct mg_request_info* const request_info,
                      const Response& response) {
  const std::string json = response.ToJSON();
  std::ostringstream out;
  out << "HTTP/1.1 404 Not Found\r\n"
      << "Content-Length: " << strlen(json.c_str()) << "\r\n"
      << "Content-Type: application/json; charset=UTF-8\r\n"
      << "Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept\r\n"
      << "Accept-Ranges: bytes\r\n"
      << "Connection: close\r\n\r\n";
  if (strcmp(request_info->request_method, "HEAD") != 0)
    out << json << "\r\n";
  VLOG(1) << out.str();
  mg_printf(connection, "%s", out.str().c_str());
}

void SendHttpMethodNotAllowed(struct mg_connection* const connection,
                              const struct mg_request_info* const request_info,
                              const Response& response) {
  const Value* value = response.value();
  CheckValueType(Value::TYPE_LIST, value);

  std::vector<std::string> allowed_methods;
  const ListValue* list_value = static_cast<const ListValue*>(value);
  for (size_t i = 0; i < list_value->GetSize(); ++i) {
    std::string method;
    LOG_IF(WARNING, list_value->GetString(i, &method))
        << "Ignoring non-string value at index " << i;
    allowed_methods.push_back(method);
  }

  std::ostringstream out;
  out << "HTTP/1.1 405 Method Not Allowed\r\n"
      << "Content-Type: text/html\r\n"
      << "Content-Length: 0\r\n"
      << "Allow: " << JoinString(allowed_methods, ',') << "\r\n\r\n";
  VLOG(1) << out.str();
  mg_printf(connection, "%s", out.str().c_str());
}

void SendHttpInternalError(struct mg_connection* const connection,
                           const struct mg_request_info* const request_info,
                           const Response& response) {
  const std::string json = response.ToJSON();
  std::ostringstream out;
  out << "HTTP/1.1 500 Internal Server Error\r\n"
      << "Content-Length: " << strlen(json.c_str()) << "\r\n"
      << "Content-Type: application/json; charset=UTF-8\r\n"
      << "Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept\r\n"
      << "Accept-Ranges: bytes\r\n"
      << "Connection: close\r\n\r\n";
  if (strcmp(request_info->request_method, "HEAD") != 0)
    out << json << "\r\n";
  VLOG(1) << out.str();
  mg_printf(connection, "%s", out.str().c_str());
}

// For every HTTP request from the mongoode server DispatchCommand will
// inspect the HTTP method call requested and execute the proper function
// mapped from the class Command.
void DispatchCommand(Command* const command, const std::string& method,
                     Response* response) {
  if (command->DoesPost() && method == "POST") {
    if (command->Init(response))
      command->ExecutePost(response);
  } else if (command->DoesGet() && method == "GET") {
    if (command->Init(response))
      command->ExecuteGet(response);
  } else if (command->DoesDelete() && method == "DELETE") {
    if (command->Init(response))
      command->ExecuteDelete(response);
  } else {
    ListValue* methods = new ListValue;
    if (command->DoesPost())
      methods->Append(Value::CreateStringValue("POST"));
    if (command->DoesGet()) {
      methods->Append(Value::CreateStringValue("GET"));
      methods->Append(Value::CreateStringValue("HEAD"));
    }
    if (command->DoesDelete())
      methods->Append(Value::CreateStringValue("DELETE"));
    response->set_status(kMethodNotAllowed);
    response->set_value(methods);  // Assumes ownership.
  }
}

void SendResponse(struct mg_connection* const connection,
                  const struct mg_request_info* const request_info,
                  const Response& response) {
  switch (response.status()) {
    case kSuccess:
      SendHttpOk(connection, request_info, response);
      break;

    case kSeeOther:
      SendHttpSeeOther(connection, request_info, response);
      break;

    case kBadRequest:
      SendHttpBadRequest(connection, request_info, response);
      break;

    case kSessionNotFound:
      SendHttpNotFound(connection, request_info, response);
      break;

    case kMethodNotAllowed:
      SendHttpMethodNotAllowed(connection, request_info, response);
      break;

    // All other errors should be treated as generic 500s. The client will be
    // responsible for inspecting the message body for details.
  case kInternalServerError:
  default:
      SendHttpInternalError(connection, request_info, response);
      break;
  }
}

}  // namespace webdriver