| 1 |
#include "Message.h" |
| 2 |
#include <wx/tokenzr.h> |
| 3 |
|
| 4 |
namespace |
| 5 |
{ |
| 6 |
#define HEADER_CONTENT_LENGTH "Content-Length" |
| 7 |
|
| 8 |
#define STATE_NORMAL 0 |
| 9 |
#define STATE_DOUBLE_QUOTES 1 |
| 10 |
#define STATE_ESCAPE 2 |
| 11 |
|
| 12 |
/// Since clangd might return a binary characters it breaks the wxString conversion |
| 13 |
/// This means that the "Content-Lenght" value might point us into an invalid position |
| 14 |
/// inside the position (i.e. broken json mssage) |
| 15 |
/// So we dont rely on the header length, but instead we count the chars ourself |
| 16 |
int FindCompleteMessage(const wxString& jsonMsg, int startIndex) |
| 17 |
{ |
| 18 |
if(jsonMsg[startIndex] != '{') { |
| 19 |
return wxNOT_FOUND; |
| 20 |
} |
| 21 |
int depth = 1; |
| 22 |
int msglen = 1; // skip the '{' |
| 23 |
int state = STATE_NORMAL; |
| 24 |
size_t strLen = jsonMsg.length(); |
| 25 |
for(size_t i = (startIndex + 1); i < strLen; ++i, ++msglen) { |
| 26 |
wxChar ch = jsonMsg[i]; |
| 27 |
switch(state) { |
| 28 |
case STATE_NORMAL: |
| 29 |
switch(ch) { |
| 30 |
case '{': |
| 31 |
case '[': |
| 32 |
++depth; |
| 33 |
break; |
| 34 |
case ']': |
| 35 |
case '}': |
| 36 |
--depth; |
| 37 |
if(depth == 0) { |
| 38 |
return (msglen + 1); // include this char |
| 39 |
} |
| 40 |
break; |
| 41 |
case '"': |
| 42 |
state = STATE_DOUBLE_QUOTES; |
| 43 |
break; |
| 44 |
default: |
| 45 |
break; |
| 46 |
} |
| 47 |
break; |
| 48 |
case STATE_DOUBLE_QUOTES: |
| 49 |
switch(ch) { |
| 50 |
case '\\': |
| 51 |
state = STATE_ESCAPE; |
| 52 |
break; |
| 53 |
case '"': |
| 54 |
state = STATE_NORMAL; |
| 55 |
break; |
| 56 |
default: |
| 57 |
break; |
| 58 |
} |
| 59 |
break; |
| 60 |
case STATE_ESCAPE: |
| 61 |
switch(ch) { |
| 62 |
default: |
| 63 |
state = STATE_DOUBLE_QUOTES; |
| 64 |
break; |
| 65 |
} |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
return wxNOT_FOUND; |
| 70 |
} |
| 71 |
|
| 72 |
int ReadHeaders(const wxString& message, wxStringMap_t& headers) |
| 73 |
{ |
| 74 |
int where = message.Find("\r\n\r\n"); |
| 75 |
if(where == wxNOT_FOUND) { |
| 76 |
return wxNOT_FOUND; |
| 77 |
} |
| 78 |
wxString headerSection = message.Mid(0, where); // excluding the "\r\n\r\n" |
| 79 |
wxArrayString lines = ::wxStringTokenize(headerSection, "\n", wxTOKEN_STRTOK); |
| 80 |
for(wxString& header : lines) { |
| 81 |
header.Trim().Trim(false); |
| 82 |
wxString name = header.BeforeFirst(':'); |
| 83 |
wxString value = header.AfterFirst(':'); |
| 84 |
headers.insert({ name.Trim().Trim(false), value.Trim().Trim(false) }); |
| 85 |
} |
| 86 |
|
| 87 |
// return the headers section + the separator |
| 88 |
return (where + 4); |
| 89 |
} |
| 90 |
|
| 91 |
} // namespace |
| 92 |
LSP::Message::Message() {} |
| 93 |
|
| 94 |
LSP::Message::~Message() {} |
| 95 |
|
| 96 |
JSONItem LSP::Message::ToJSON(const wxString& name) const |
| 97 |
{ |
| 98 |
JSONItem json = JSONItem::createObject(name); |
| 99 |
json.addProperty("jsonrpc", m_jsonrpc); |
| 100 |
return json; |
| 101 |
} |
| 102 |
|
| 103 |
void LSP::Message::FromJSON(const JSONItem& json) { m_jsonrpc = json.namedObject("jsonrpc").toString(); } |
| 104 |
|
| 105 |
int LSP::Message::GetNextID() |
| 106 |
{ |
| 107 |
static int requestId = 0; |
| 108 |
return ++requestId; |
| 109 |
} |
| 110 |
|
| 111 |
std::unique_ptr<JSON> LSP::Message::GetJSONPayload(wxString& network_buffer) |
| 112 |
{ |
| 113 |
// Strip the headers |
| 114 |
wxStringMap_t headers; |
| 115 |
int headersSize = ReadHeaders(network_buffer, headers); |
| 116 |
if(headersSize == wxNOT_FOUND) { |
| 117 |
return nullptr; |
| 118 |
} |
| 119 |
|
| 120 |
if(headers.count(HEADER_CONTENT_LENGTH) == 0) { |
| 121 |
return nullptr; |
| 122 |
} |
| 123 |
|
| 124 |
int msglen = FindCompleteMessage(network_buffer, headersSize); |
| 125 |
if(msglen == wxNOT_FOUND) { |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
|
| 129 |
if((headersSize + msglen) > (int)network_buffer.length()) { |
| 130 |
return nullptr; |
| 131 |
} |
| 132 |
wxString json_str = network_buffer.Mid(0, headersSize + msglen); |
| 133 |
|
| 134 |
// Remove the message from the network buffer |
| 135 |
network_buffer.Remove(0, headersSize + msglen); |
| 136 |
|
| 137 |
// remove the headers section from the message and construct a JSON object |
| 138 |
json_str.Remove(0, headersSize); |
| 139 |
std::unique_ptr<JSON> json(new JSON(json_str)); |
| 140 |
if(json->isOk()) { |
| 141 |
return json; |
| 142 |
} |
| 143 |
return nullptr; |
| 144 |
} |