summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/src/jsonrpc/StreamMessageProducer.cpp
blob: 695c35fe1dcb54aed2e1a69a76d19f6745cdb637 (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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

#include "LibLsp/JsonRpc/StreamMessageProducer.h"
#include <cassert>

#include "LibLsp/JsonRpc/stream.h"
#include "LibLsp/lsp/Markup/string_ref.h"


bool StartsWith(std::string value, std::string start);
bool StartsWith(std::string value, std::string start) {
        if (start.size() > value.size())
                return false;
        return std::equal(start.begin(), start.end(), value.begin());
}

using  namespace std;
namespace
{
        string JSONRPC_VERSION = "2.0";
        string CONTENT_LENGTH_HEADER = "Content-Length";
        string CONTENT_TYPE_HEADER = "Content-Type";
        string JSON_MIME_TYPE = "application/json";
        string CRLF = "\r\n";

}

  void LSPStreamMessageProducer::parseHeader(std::string& line, LSPStreamMessageProducer::Headers& headers)
  {
          int sepIndex = line.find(':');
          if (sepIndex >= 0) {
                  auto key = line.substr(0, sepIndex);
              if(key == CONTENT_LENGTH_HEADER)
              {
                        headers.contentLength = atoi(line.substr(sepIndex + 1).data());
              }
                  else if(key == CONTENT_TYPE_HEADER)
                  {
                          int charsetIndex = line.find("charset=");
                          if (charsetIndex >= 0)
                                  headers.charset = line.substr(charsetIndex + 8);
                  }
          }
  }


void LSPStreamMessageProducer::listen(MessageConsumer callBack)
{
        if(!input)
                return;

        keepRunning = true;
        bool newLine = false;
        Headers headers;
        string headerBuilder ;
        string debugBuilder ;
        // Read the content length. It is terminated by the "\r\n" sequence.
        while (keepRunning)
        {
                if(input->bad())
                {
                        std::string info = "Input stream is bad.";
                        auto what = input->what();
                        if (what.size())
                        {
                                info += "Reason:";
                                info += input->what();
                        }
                        MessageIssue issue(info, lsp::Log::Level::SEVERE);
                        issueHandler.handle(std::move(issue));
                        return;
                }
                if(input->fail())
                {
                        std::string info = "Input fail.";
                        auto what = input->what();
                        if(what.size())
                        {
                                info += "Reason:";
                                info += input->what();
                        }
                        MessageIssue issue(info, lsp::Log::Level::WARNING);
                        issueHandler.handle(std::move(issue));
                        if(input->need_to_clear_the_state())
                                input->clear();
                        else
                        {
                                return;
                        }
                }
                int c = input->get();
                if (c == EOF) {
                        // End of input stream has been reached
                        keepRunning = false;
                }
                else
                {

                    debugBuilder.push_back((char)c);
                    if (c == '\n')
                    {
                        if (newLine) {
                            // Two consecutive newlines have been read, which signals the start of the message content
                            if (headers.contentLength <= 0)
                            {
                                    string info = "Unexpected token:" + debugBuilder;
                                    info = +"  (expected Content-Length: sequence);";
                                     MessageIssue issue(info, lsp::Log::Level::WARNING);
                                     issueHandler.handle(std::move(issue));
                            }
                            else {
                                    bool result = handleMessage(headers,callBack);
                                    if (!result)
                                            keepRunning = false;
                                    newLine = false;
                            }
                            headers.clear();
                            debugBuilder.clear();
                        }
                        else if (!headerBuilder.empty()) {
                            // A single newline ends a header line
                            parseHeader(headerBuilder, headers);
                            headerBuilder.clear();
                        }
                        newLine = true;
                    }
                    else if (c != '\r') {
                        // Add the input to the current header line

                        headerBuilder.push_back((char)c);
                        newLine = false;
                    }
                }
        }

}

void LSPStreamMessageProducer::bind(std::shared_ptr<lsp::istream>_in)
{
        input = _in;
}

bool LSPStreamMessageProducer::handleMessage(Headers& headers ,MessageConsumer callBack)
{
                         // Read content.
        auto content_length = headers.contentLength;
         std::string content(content_length,0);
         auto data = &content[0];
         input->read(data, content_length);
         if (input->bad())
         {
                 std::string info = "Input stream is bad.";
                 auto what = input->what();
                 if (!what.empty())
                 {
                         info += "Reason:";
                         info += input->what();
                 }
                 MessageIssue issue(info, lsp::Log::Level::SEVERE);
                 issueHandler.handle(std::move(issue));
                 return false;
         }

         if (input->eof())
         {
                 MessageIssue issue("No more input when reading content body", lsp::Log::Level::INFO);
                 issueHandler.handle(std::move(issue));
                 return false;
         }
         if (input->fail())
         {
                 std::string info = "Input fail.";
                 auto what = input->what();
                 if (!what.empty())
                 {
                         info += "Reason:";
                         info += input->what();
                 }
                 MessageIssue issue(info, lsp::Log::Level::WARNING);
                 issueHandler.handle(std::move(issue));
                 if (input->need_to_clear_the_state())
                         input->clear();
                 else
                 {
                         return false;
                 }
         }

         callBack(std::move(content));

        return true;
}



/// For lit tests we support a simplified syntax:
/// - messages are delimited by '// -----' on a line by itself
/// - lines starting with // are ignored.
/// This is a testing path, so favor simplicity over performance here.

void DelimitedStreamMessageProducer::listen(MessageConsumer callBack)
{
    if(!input)
        return;

    keepRunning = true;

    auto readLine = [&]( std::string_ref& lineBuilder) -> bool  {
        while (keepRunning)
        {
            if(input->bad())
            {
                std::string info = "Input stream is bad.";
                auto what = input->what();
                if (what.size())
                {
                    info += "Reason:";
                    info += input->what();
                }
                MessageIssue issue(info, lsp::Log::Level::SEVERE);
                issueHandler.handle(std::move(issue));
                return false;
            }
            if(input->fail())
            {
                std::string info = "Input fail.";
                auto what = input->what();
                if(what.size())
                {
                    info += "Reason:";
                    info += input->what();
                }
                MessageIssue issue(info, lsp::Log::Level::WARNING);
                issueHandler.handle(std::move(issue));
                if(input->need_to_clear_the_state())
                    input->clear();
                else
                {
                    return false;
                }
            }
            int c = input->get();
            if (c == EOF) {
                // End of input stream has been reached
                keepRunning = false;
            }
            else
            {
                if (c == '\n')
                {
                    if(!lineBuilder.empty()){
                        lineBuilder.push_back(c);
                        return true;
                    }
                }
                else if (c != '\r') {
                    // Add the input to the current header line

                    lineBuilder.push_back((char)c);
                }
            }
        }
        return false;
    };

    auto getMessage = [&](std::string& json) -> bool {
        std::string_ref lineBuilder ;
        while (readLine(lineBuilder)){
            lineBuilder.trim();
            if(lineBuilder.start_with("//")){
                // Found a delimiter for the message.
                if (lineBuilder == "// -----")
                {
                    return  true;
                }
            }
            json += lineBuilder;
        }
        return false;
    };


    while (true) {
        std::string json;
        if (getMessage(json)) {
            callBack(std::move(json));
        }else{
            return ;
        }
    }
}

void DelimitedStreamMessageProducer::bind(std::shared_ptr<lsp::istream>_in)
{
    input = _in;
}