| 1 |
|
| 2 |
#include "LibLsp/JsonRpc/StreamMessageProducer.h" |
| 3 |
#include <cassert> |
| 4 |
|
| 5 |
#include "LibLsp/JsonRpc/stream.h" |
| 6 |
#include "LibLsp/lsp/Markup/string_ref.h" |
| 7 |
|
| 8 |
bool StartsWith(std::string value, std::string start); |
| 9 |
bool StartsWith(std::string value, std::string start) |
| 10 |
{ |
| 11 |
if (start.size() > value.size()) |
| 12 |
{ |
| 13 |
return false; |
| 14 |
} |
| 15 |
return std::equal(start.begin(), start.end(), value.begin()); |
| 16 |
} |
| 17 |
|
| 18 |
using namespace std; |
| 19 |
namespace |
| 20 |
{ |
| 21 |
string JSONRPC_VERSION = "2.0"; |
| 22 |
string CONTENT_LENGTH_HEADER = "Content-Length"; |
| 23 |
string CONTENT_TYPE_HEADER = "Content-Type"; |
| 24 |
string JSON_MIME_TYPE = "application/json"; |
| 25 |
string CRLF = "\r\n"; |
| 26 |
|
| 27 |
} // namespace |
| 28 |
|
| 29 |
void LSPStreamMessageProducer::parseHeader(std::string& line, LSPStreamMessageProducer::Headers& headers) |
| 30 |
{ |
| 31 |
int sepIndex = line.find(':'); |
| 32 |
if (sepIndex >= 0) |
| 33 |
{ |
| 34 |
auto key = line.substr(0, sepIndex); |
| 35 |
if (key == CONTENT_LENGTH_HEADER) |
| 36 |
{ |
| 37 |
headers.contentLength = atoi(line.substr(sepIndex + 1).data()); |
| 38 |
} |
| 39 |
else if (key == CONTENT_TYPE_HEADER) |
| 40 |
{ |
| 41 |
int charsetIndex = line.find("charset="); |
| 42 |
if (charsetIndex >= 0) |
| 43 |
{ |
| 44 |
headers.charset = line.substr(charsetIndex + 8); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
void LSPStreamMessageProducer::listen(MessageConsumer callBack) |
| 51 |
{ |
| 52 |
if (!input) |
| 53 |
{ |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
keepRunning = true; |
| 58 |
bool newLine = false; |
| 59 |
Headers headers; |
| 60 |
string headerBuilder; |
| 61 |
string debugBuilder; |
| 62 |
// Read the content length. It is terminated by the "\r\n" sequence. |
| 63 |
while (keepRunning) |
| 64 |
{ |
| 65 |
if (input->bad()) |
| 66 |
{ |
| 67 |
std::string info = "Input stream is bad."; |
| 68 |
auto what = input->what(); |
| 69 |
if (what.size()) |
| 70 |
{ |
| 71 |
info += "Reason:"; |
| 72 |
info += input->what(); |
| 73 |
} |
| 74 |
MessageIssue issue(info, lsp::Log::Level::SEVERE); |
| 75 |
issueHandler.handle(std::move(issue)); |
| 76 |
return; |
| 77 |
} |
| 78 |
if (input->fail()) |
| 79 |
{ |
| 80 |
std::string info = "Input fail."; |
| 81 |
auto what = input->what(); |
| 82 |
if (what.size()) |
| 83 |
{ |
| 84 |
info += "Reason:"; |
| 85 |
info += input->what(); |
| 86 |
} |
| 87 |
MessageIssue issue(info, lsp::Log::Level::WARNING); |
| 88 |
issueHandler.handle(std::move(issue)); |
| 89 |
if (input->need_to_clear_the_state()) |
| 90 |
{ |
| 91 |
input->clear(); |
| 92 |
} |
| 93 |
else |
| 94 |
{ |
| 95 |
return; |
| 96 |
} |
| 97 |
} |
| 98 |
int c = input->get(); |
| 99 |
if (c == EOF) |
| 100 |
{ |
| 101 |
// End of input stream has been reached |
| 102 |
keepRunning = false; |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
|
| 107 |
debugBuilder.push_back((char)c); |
| 108 |
if (c == '\n') |
| 109 |
{ |
| 110 |
if (newLine) |
| 111 |
{ |
| 112 |
// Two consecutive newlines have been read, which signals the start of the message content |
| 113 |
if (headers.contentLength <= 0) |
| 114 |
{ |
| 115 |
string info = "Unexpected token:" + debugBuilder; |
| 116 |
info = +" (expected Content-Length: sequence);"; |
| 117 |
MessageIssue issue(info, lsp::Log::Level::WARNING); |
| 118 |
issueHandler.handle(std::move(issue)); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
bool result = handleMessage(headers, callBack); |
| 123 |
if (!result) |
| 124 |
{ |
| 125 |
keepRunning = false; |
| 126 |
} |
| 127 |
newLine = false; |
| 128 |
} |
| 129 |
headers.clear(); |
| 130 |
debugBuilder.clear(); |
| 131 |
} |
| 132 |
else if (!headerBuilder.empty()) |
| 133 |
{ |
| 134 |
// A single newline ends a header line |
| 135 |
parseHeader(headerBuilder, headers); |
| 136 |
headerBuilder.clear(); |
| 137 |
} |
| 138 |
newLine = true; |
| 139 |
} |
| 140 |
else if (c != '\r') |
| 141 |
{ |
| 142 |
// Add the input to the current header line |
| 143 |
|
| 144 |
headerBuilder.push_back((char)c); |
| 145 |
newLine = false; |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void LSPStreamMessageProducer::bind(std::shared_ptr<lsp::istream> _in) |
| 152 |
{ |
| 153 |
input = _in; |
| 154 |
} |
| 155 |
|
| 156 |
bool LSPStreamMessageProducer::handleMessage(Headers& headers, MessageConsumer callBack) |
| 157 |
{ |
| 158 |
// Read content. |
| 159 |
auto content_length = headers.contentLength; |
| 160 |
std::string content(content_length, 0); |
| 161 |
auto data = &content[0]; |
| 162 |
input->read(data, content_length); |
| 163 |
if (input->bad()) |
| 164 |
{ |
| 165 |
std::string info = "Input stream is bad."; |
| 166 |
auto what = input->what(); |
| 167 |
if (!what.empty()) |
| 168 |
{ |
| 169 |
info += "Reason:"; |
| 170 |
info += input->what(); |
| 171 |
} |
| 172 |
MessageIssue issue(info, lsp::Log::Level::SEVERE); |
| 173 |
issueHandler.handle(std::move(issue)); |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
if (input->eof()) |
| 178 |
{ |
| 179 |
MessageIssue issue("No more input when reading content body", lsp::Log::Level::INFO); |
| 180 |
issueHandler.handle(std::move(issue)); |
| 181 |
return false; |
| 182 |
} |
| 183 |
if (input->fail()) |
| 184 |
{ |
| 185 |
std::string info = "Input fail."; |
| 186 |
auto what = input->what(); |
| 187 |
if (!what.empty()) |
| 188 |
{ |
| 189 |
info += "Reason:"; |
| 190 |
info += input->what(); |
| 191 |
} |
| 192 |
MessageIssue issue(info, lsp::Log::Level::WARNING); |
| 193 |
issueHandler.handle(std::move(issue)); |
| 194 |
if (input->need_to_clear_the_state()) |
| 195 |
{ |
| 196 |
input->clear(); |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
return false; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
callBack(std::move(content)); |
| 205 |
|
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
/// For lit tests we support a simplified syntax: |
| 210 |
/// - messages are delimited by '// -----' on a line by itself |
| 211 |
/// - lines starting with // are ignored. |
| 212 |
/// This is a testing path, so favor simplicity over performance here. |
| 213 |
|
| 214 |
void DelimitedStreamMessageProducer::listen(MessageConsumer callBack) |
| 215 |
{ |
| 216 |
if (!input) |
| 217 |
{ |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
keepRunning = true; |
| 222 |
|
| 223 |
auto readLine = [&](std::string_ref& lineBuilder) -> bool |
| 224 |
{ |
| 225 |
while (keepRunning) |
| 226 |
{ |
| 227 |
if (input->bad()) |
| 228 |
{ |
| 229 |
std::string info = "Input stream is bad."; |
| 230 |
auto what = input->what(); |
| 231 |
if (what.size()) |
| 232 |
{ |
| 233 |
info += "Reason:"; |
| 234 |
info += input->what(); |
| 235 |
} |
| 236 |
MessageIssue issue(info, lsp::Log::Level::SEVERE); |
| 237 |
issueHandler.handle(std::move(issue)); |
| 238 |
return false; |
| 239 |
} |
| 240 |
if (input->fail()) |
| 241 |
{ |
| 242 |
std::string info = "Input fail."; |
| 243 |
auto what = input->what(); |
| 244 |
if (what.size()) |
| 245 |
{ |
| 246 |
info += "Reason:"; |
| 247 |
info += input->what(); |
| 248 |
} |
| 249 |
MessageIssue issue(info, lsp::Log::Level::WARNING); |
| 250 |
issueHandler.handle(std::move(issue)); |
| 251 |
if (input->need_to_clear_the_state()) |
| 252 |
{ |
| 253 |
input->clear(); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
return false; |
| 258 |
} |
| 259 |
} |
| 260 |
int c = input->get(); |
| 261 |
if (c == EOF) |
| 262 |
{ |
| 263 |
// End of input stream has been reached |
| 264 |
keepRunning = false; |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
if (c == '\n') |
| 269 |
{ |
| 270 |
if (!lineBuilder.empty()) |
| 271 |
{ |
| 272 |
lineBuilder.push_back(static_cast<char>(c)); |
| 273 |
return true; |
| 274 |
} |
| 275 |
} |
| 276 |
else if (c != '\r') |
| 277 |
{ |
| 278 |
// Add the input to the current header line |
| 279 |
|
| 280 |
lineBuilder.push_back((char)c); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
return false; |
| 285 |
}; |
| 286 |
|
| 287 |
auto getMessage = [&](std::string& json) -> bool |
| 288 |
{ |
| 289 |
std::string_ref lineBuilder; |
| 290 |
while (readLine(lineBuilder)) |
| 291 |
{ |
| 292 |
lineBuilder.trim(); |
| 293 |
if (lineBuilder.start_with("//")) |
| 294 |
{ |
| 295 |
// Found a delimiter for the message. |
| 296 |
if (lineBuilder == "// -----") |
| 297 |
{ |
| 298 |
return true; |
| 299 |
} |
| 300 |
} |
| 301 |
json += lineBuilder; |
| 302 |
} |
| 303 |
return false; |
| 304 |
}; |
| 305 |
|
| 306 |
while (true) |
| 307 |
{ |
| 308 |
std::string json; |
| 309 |
if (getMessage(json)) |
| 310 |
{ |
| 311 |
callBack(std::move(json)); |
| 312 |
} |
| 313 |
else |
| 314 |
{ |
| 315 |
return; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
void DelimitedStreamMessageProducer::bind(std::shared_ptr<lsp::istream> _in) |
| 321 |
{ |
| 322 |
input = _in; |
| 323 |
} |