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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/chromedriver/log_replay/devtools_log_reader.h"
#include <iostream>
#include <string>
#include "base/logging.h"
#include "base/strings/pattern.h"
#include "base/strings/string_util.h"
namespace {
// Parses the word (id=X) and just returns the id number
int GetId(std::istringstream& header_stream) {
int id = 0;
header_stream.ignore(5); // ignore the " (id=" characters
header_stream >> id;
header_stream.ignore(1); // ignore the final parenthesis
return id;
}
void putback(std::istringstream& stream, const std::string& str) {
for (auto it = str.rbegin(); it != str.rend(); ++it) {
stream.putback(*it);
}
}
// Parses the word (session_id=X) and just returns the session_id string
bool GetSessionId(std::istringstream& header_stream, std::string* session_id) {
std::string spaces;
while (base::IsAsciiWhitespace(header_stream.peek())) {
char ch;
header_stream.get(ch);
spaces.push_back(ch);
}
std::string sid; // (session_id=X)
header_stream >> sid;
if (sid.size() < 13 // (session_id=) is also valid
|| sid.find("(session_id=") != 0 || sid.back() != ')') {
putback(header_stream, sid);
putback(header_stream, spaces);
return false;
}
// skip 12 leading characters: (session_id=
// and one trailing character: )
*session_id = sid.substr(12, sid.size() - 12 - 1);
return true;
}
} // namespace
LogEntry::LogEntry(std::istringstream& header_stream) {
error = false;
std::string protocol_type_string;
header_stream >> protocol_type_string; // "HTTP" or "WebSocket"
if (protocol_type_string == "HTTP") {
protocol_type = kHTTP;
} else if (protocol_type_string == "WebSocket") {
protocol_type = kWebSocket;
} else {
error = true;
LOG(ERROR) << "Could not read protocol from log entry header.";
return;
}
std::string event_type_string;
header_stream >> event_type_string;
if (event_type_string == "Response:") {
event_type = kResponse;
} else if (event_type_string == "Command:" ||
event_type_string == "Request:") {
event_type = kRequest;
} else if (event_type_string == "Event:") {
event_type = kEvent;
} else {
error = true;
LOG(ERROR) << "Could not read event type from log entry header.";
return;
}
if (!(protocol_type == kHTTP && event_type == kResponse)) {
header_stream >> command_name;
if (command_name == "") {
error = true;
LOG(ERROR) << "Could not read command name from log entry header";
return;
}
if (protocol_type != kHTTP) {
if (event_type != kEvent) {
id = GetId(header_stream);
if (id == 0) {
error = true;
LOG(ERROR) << "Could not read sequential id from log entry header.";
return;
}
}
session_id.clear();
if (!GetSessionId(header_stream, &session_id)) {
error = true;
LOG(ERROR) << "Could not read session_id from log entry header.";
}
header_stream >> socket_id;
if (socket_id == "") {
error = true;
LOG(ERROR) << "Could not read socket id from log entry header.";
}
}
}
}
LogEntry::~LogEntry() = default;
DevToolsLogReader::DevToolsLogReader(const base::FilePath& log_path)
: log_file(log_path.value().c_str(), std::ios::in) {}
DevToolsLogReader::~DevToolsLogReader() = default;
bool DevToolsLogReader::IsHeader(std::istringstream& header_stream) const {
std::string word;
header_stream >> word; // preamble
if (!base::MatchPattern(word, "[??????????.???][DEBUG]:") &&
!base::MatchPattern(word, "[??????????")) {
return false;
}
header_stream >> word; // "DevTools" for DevTools commands/responses/events
// test for the second half of readable timestamp and read next token
if (base::MatchPattern(word, "????????.??????][DEBUG]:")) {
header_stream >> word;
}
bool result = word == "DevTools";
return result;
}
void DevToolsLogReader::UndoGetNext(std::unique_ptr<LogEntry> next) {
peeked = std::move(next);
}
std::unique_ptr<LogEntry> DevToolsLogReader::GetNext(
LogEntry::Protocol protocol_type) {
if (peeked) {
return std::move(peeked);
}
std::string next_line;
while (true) {
if (log_file.eof())
return nullptr;
std::getline(log_file, next_line);
std::istringstream next_line_stream(next_line);
if (IsHeader(next_line_stream)) {
std::unique_ptr<LogEntry> log_entry =
std::make_unique<LogEntry>(next_line_stream);
if (log_entry->error) {
return nullptr; // helpful error message already logged
}
if (log_entry->protocol_type != protocol_type)
continue;
if (!(log_entry->event_type == LogEntry::kRequest &&
log_entry->protocol_type == LogEntry::kHTTP)) {
log_entry->payload = GetJSONString(next_line_stream);
if (log_entry->payload == "") {
LOG(ERROR) << "Problem parsing JSON from log file";
return nullptr;
}
}
return log_entry;
}
}
}
std::string DevToolsLogReader::GetJSONString(
std::istringstream& header_stream) {
std::string next_line, json;
int opening_char_count = 0;
std::getline(header_stream, next_line);
next_line = next_line.substr(1);
char opening_char = next_line[0];
char closing_char;
switch (opening_char) {
case '{':
closing_char = '}';
break;
case '[':
closing_char = ']';
break;
default:
return next_line; // For rare cases when payload is a string, not a JSON.
}
while (true) {
json += next_line + "\n";
opening_char_count += CountChar(next_line, opening_char, closing_char);
if (opening_char_count == 0)
break;
if (log_file.eof())
return "";
getline(log_file, next_line);
}
return json;
}
int DevToolsLogReader::CountChar(const std::string& line,
char opening_char,
char closing_char) const {
bool in_quote = false;
int total = 0;
for (size_t i = 0; i < line.length(); i++) {
if (!in_quote && line[i] == opening_char)
total++;
if (!in_quote && line[i] == closing_char)
total--;
if (line[i] == '"' && (i == 0 || line[i - 1] != '\\'))
in_quote = !in_quote;
}
return total;
}
|