http_codec.cc 34.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdexcept>
#include <regex>
#include <string>

#include "codec_stack_builder.hh"

#include "http_codec.hh"

#include "loggers.hh"

#include "LibItsHttp_TypesAndValues.hh"
#include "LibItsHttp_MessageBodyTypes.hh"
#include "LibItsHttp_XmlMessageBodyTypes.hh"

Yann Garcia's avatar
Yann Garcia committed
15
16
#include "http_etsi_ieee1609dot2_codec.hh"

17
18
19
int http_codec::encode (const LibItsHttp__TypesAndValues::HttpMessage& msg, OCTETSTRING& data)
{
  loggers::get_instance().log_msg(">>> http_codec::encode: ", (const Base_Type&)msg);
garciay's avatar
garciay committed
20
  loggers::get_instance().log(">>> http_codec::encode: %p", this);
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

  TTCN_EncDec::clear_error();
  TTCN_Buffer encoding_buffer;

  _ec.reset();

  int result;
  if (msg.ischosen(LibItsHttp__TypesAndValues::HttpMessage::ALT_request)) {
    result = encode_request(msg.request(), encoding_buffer);
  } else if (msg.ischosen(LibItsHttp__TypesAndValues::HttpMessage::ALT_response)) {
    result = encode_response(msg.response(), encoding_buffer);
  } else {
    loggers::get_instance().warning("http_codec::encode: Unbound HttpMessage");
    return -1;
  }

  data = OCTETSTRING(encoding_buffer.get_len(), encoding_buffer.get_data());

  loggers::get_instance().log_msg("<<< http_codec::encode: data=", data);
  return result;
}

int http_codec::decode (const OCTETSTRING& data, LibItsHttp__TypesAndValues::HttpMessage& msg, params* params)
{
  loggers::get_instance().log_msg(">>> http_codec::decode: data=", data);

  TTCN_EncDec::clear_error();
  TTCN_Buffer decoding_buffer(data);
  loggers::get_instance().log_to_hexa("http_codec::decode: decoding_buffer=", decoding_buffer);

  _dc.reset();

  _params = params;

  // Get the first line (e.g. HTTP/1.1 302 Found or POST / HTTP/1.1)
  CHARSTRING message_id;
  if (get_line(decoding_buffer, message_id) == -1) {
    return -1;
  }
  loggers::get_instance().log_msg("http_codec::decode: message_id: ", message_id);
  // Extract parameters
  try {
    std::string str(static_cast<const char*>(message_id));
    std::regex rgx ("\\s*(\\w+)/");
    std::sregex_iterator begin(str.cbegin(), str.cend(), rgx);
    std::smatch m = *begin;
    loggers::get_instance().log("http_codec::decode: %d - %s", m.size(), m[0].str().c_str());
    if (m[0].str().compare("HTTP/") == 0) { // HTTP response
      LibItsHttp__TypesAndValues::Response response;
garciay's avatar
garciay committed
70
      std::regex rgx ("\\s*HTTP/(\\d+)\\.(\\d+)\\s+(\\d+)\\s+([\\w\\s\\t\\v\\f]+)*");
71
72
73
74
75
76
77
78
79
80
81
82
      std::sregex_iterator begin(str.cbegin(), str.cend(), rgx);
      std::smatch m = *begin;
      loggers::get_instance().log("http_codec::decode: Process response: %d", m.size());
      if (m.size() != 5) {
        loggers::get_instance().error("http_codec::decode: Unsupported tag");
        return -1;
      }
      response.version__major() = std::stoi(m[1].str().c_str());
      response.version__minor() = std::stoi(m[2].str().c_str());
      response.statuscode() = std::stoi(m[3].str().c_str());
      response.statustext() = CHARSTRING(m[4].str().c_str());
      LibItsHttp__TypesAndValues::HeaderLines headers;
Yann Garcia's avatar
Yann Garcia committed
83
84
      std::string content_type;
      decode_headers(decoding_buffer, headers, content_type);
85
86
87
      response.header() = headers;
      loggers::get_instance().log_to_hexa("Before decoding Body: ", decoding_buffer);
      LibItsHttp__MessageBodyTypes::HttpMessageBody body;
Yann Garcia's avatar
Yann Garcia committed
88
      if (decode_body(decoding_buffer, body, content_type) == -1) {
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        response.body().set_to_omit();
      } else {
        response.body() = OPTIONAL<LibItsHttp__MessageBodyTypes::HttpMessageBody>(body);
      }
       msg.response() = response;
    } else { // HTTP request
      LibItsHttp__TypesAndValues::Request request;
      std::regex rgx ("\\s*(\\w+)\\s+(.+)\\s+HTTP/(\\d)\\.(\\d)");
      std::sregex_iterator begin(str.cbegin(), str.cend(), rgx);
      std::smatch m = *begin;
      if (m.size() != 5) {
        loggers::get_instance().error("http_codec::decode: Unsupported tag");
        return -1;
      }
      request.method() = CHARSTRING(m[1].str().c_str());
      request.uri() = CHARSTRING(m[2].str().c_str());
      request.version__major() = std::stoi(m[3].str().c_str());
      request.version__minor() = std::stoi(m[4].str().c_str());
      LibItsHttp__TypesAndValues::HeaderLines headers;
Yann Garcia's avatar
Yann Garcia committed
108
109
      std::string content_type;
      decode_headers(decoding_buffer, headers, content_type);
110
111
112
      request.header() = headers;
      OPTIONAL<LibItsHttp__MessageBodyTypes::HttpMessageBody> body;
      body.set_to_omit();
Yann Garcia's avatar
Yann Garcia committed
113
      if (decode_body(decoding_buffer, body, content_type) == -1) {
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        request.body().set_to_omit();
      } else {
        request.body() = body;
      }
      msg.request() = request;
    }

    loggers::get_instance().log_msg("<<< http_codec::decode: ", (const Base_Type&)msg);
    return 0;
  }
  catch(const std::logic_error& e) {
    return -1;
  }
}

int http_codec::encode_request(const LibItsHttp__TypesAndValues::Request& p_request, TTCN_Buffer& p_encoding_buffer)
{
  loggers::get_instance().log_msg(">>> http_codec::encode_request: ", (const Base_Type&)p_request);
garciay's avatar
garciay committed
132
  
133
134
135
136
137
138
139
140
141
  // Encode generic part
  p_encoding_buffer.put_cs(p_request.method());
  p_encoding_buffer.put_c(' ');
  p_encoding_buffer.put_cs(p_request.uri());
  p_encoding_buffer.put_cs(" HTTP/");
  p_encoding_buffer.put_cs(int2str(p_request.version__major()));
  p_encoding_buffer.put_c('.');
  p_encoding_buffer.put_cs(int2str(p_request.version__minor()));
  p_encoding_buffer.put_cs("\r\n");
garciay's avatar
garciay committed
142
  
Yann Garcia's avatar
Yann Garcia committed
143
  // Encode headers excepeted the Content-Length
144
  const LibItsHttp__TypesAndValues::HeaderLines& headers = p_request.header();
garciay's avatar
garciay committed
145
  std::string content_type;
146
147
148
  for (int i = 0; i < headers.size_of(); i++) {
    const LibItsHttp__TypesAndValues::HeaderLine& header = headers[i];
    loggers::get_instance().log_msg("http_codec::encode_request: Processing header ", header.header__name());
Yann Garcia's avatar
Yann Garcia committed
149
    if (std::string(static_cast<const char*>(header.header__name())).compare("Content-Length") == 0) { // Skip it, processed later
vagrant's avatar
vagrant committed
150
      loggers::get_instance().log("http_codec::encode_request: Skip it");
garciay's avatar
garciay committed
151
      continue;
152
    } else {
vagrant's avatar
vagrant committed
153
154
      p_encoding_buffer.put_cs(header.header__name());
      p_encoding_buffer.put_cs(": ");
155
156
157
158
159
      const OPTIONAL<LibItsHttp__TypesAndValues::charstring__list>& o = header.header__value();
      if (o.ispresent()) {
        const LibItsHttp__TypesAndValues::charstring__list& v = dynamic_cast<const OPTIONAL<LibItsHttp__TypesAndValues::charstring__list> &>(o);
        if (v.size_of() > 0) {
          loggers::get_instance().log_msg("http_codec::encode_request: Processing value ", v[0]);
Yann Garcia's avatar
Yann Garcia committed
160
          if (std::string(static_cast<const char*>(header.header__name())).compare("Content-Type") == 0) { // Store it for HTTP body payload encoding
Yann Garcia's avatar
Yann Garcia committed
161
            loggers::get_instance().log("http_codec::encode_request: Storing Content-Type");
garciay's avatar
garciay committed
162
163
164
165
166
            int j = 0;
            while (j < v.size_of()) {
              content_type += v[j++];
            } // End of 'while' statement
          }
167
168
169
          p_encoding_buffer.put_cs(v[0]);
          int j = 1;
          while (j < v.size_of()) {
garciay's avatar
garciay committed
170
171
172
            p_encoding_buffer.put_cs(", ");
            loggers::get_instance().log_msg("http_codec::encode_request: Processing value ", v[j]);
            p_encoding_buffer.put_cs(v[j++]);
173
174
175
176
177
178
          } // End of 'while' statement
        }
      } // else, do not include it
    }
    p_encoding_buffer.put_cs("\r\n");
  } // End of 'for' statement
garciay's avatar
garciay committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  
  // Encode message body
  const OPTIONAL<LibItsHttp__MessageBodyTypes::HttpMessageBody>& v = p_request.body();
  OCTETSTRING os;
  if (v.ispresent()) {
    const LibItsHttp__MessageBodyTypes::HttpMessageBody& body = static_cast<const LibItsHttp__MessageBodyTypes::HttpMessageBody&>(*v.get_opt_value());
    loggers::get_instance().log_msg("http_codec::encode_request: body: ", body);
    if (encode_body(body, os, content_type) == -1) {
      loggers::get_instance().warning("http_codec::encode_request: Failed to encode HTTP body");
      _ec.length = 0;
      _ec.is_content_length_present = 0x00;
    } else {
      _ec.length = os.lengthof();
      _ec.is_content_length_present = 0x01;
    }
    loggers::get_instance().log("http_codec::encode_request: length=%d", _ec.length);
  } else {
    loggers::get_instance().log("http_codec::encode_request: HTTP body field not present");
    _ec.length = 0;
    _ec.is_content_length_present = 0x00;
  }

Yann Garcia's avatar
Yann Garcia committed
201
202
  // Encode Content-Length header
  p_encoding_buffer.put_cs("Content-Length: ");
garciay's avatar
garciay committed
203
  if (_ec.length != 0) {
Yann Garcia's avatar
Yann Garcia committed
204
    loggers::get_instance().log("http_codec::encode_request: Content-Length: %s", static_cast<const char*>(int2str(_ec.length + 2/*Stand for the last CRLF*/)));
vagrant's avatar
vagrant committed
205
    p_encoding_buffer.put_cs(static_cast<const char*>(int2str(_ec.length + 2/*Stand for the last CRLF*/)));
garciay's avatar
garciay committed
206
207
208
209
210
211
    _ec.is_content_length_present = 0x01;
  } else {
    p_encoding_buffer.put_cs("0");
    _ec.is_content_length_present = 0x00;
  }
  p_encoding_buffer.put_cs("\r\n");
212

vagrant's avatar
vagrant committed
213
  // Add message body
214
  p_encoding_buffer.put_cs("\r\n");
garciay's avatar
garciay committed
215
216
  if (_ec.is_content_length_present == 0x01) {
    loggers::get_instance().log_msg("http_codec::encode_request: Add body ", os);
217
    p_encoding_buffer.put_os(os);
218
    p_encoding_buffer.put_cs("\r\n");
219
220
  }

vagrant's avatar
vagrant committed
221
  loggers::get_instance().log_to_hexa("<<< http_codec::encode_request: ", p_encoding_buffer);
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  return 0;
}

int http_codec::encode_response (const LibItsHttp__TypesAndValues::Response& p_response, TTCN_Buffer& p_encoding_buffer) {
  loggers::get_instance().log_msg(">>> http_codec::encode_response: ", (const Base_Type&)p_response);

  // Encode generic part
  p_encoding_buffer.put_cs("HTTP/");
  p_encoding_buffer.put_cs(int2str(p_response.version__major()));
  p_encoding_buffer.put_c('.');
  p_encoding_buffer.put_cs(int2str(p_response.version__minor()));
  p_encoding_buffer.put_cs(" ");
  p_encoding_buffer.put_cs(int2str(p_response.statuscode()));
  p_encoding_buffer.put_cs(" ");
  if (p_response.statustext().lengthof() != 0) {
    p_encoding_buffer.put_cs(p_response.statustext());
  }
  p_encoding_buffer.put_cs("\r\n");
garciay's avatar
garciay committed
240
  
Yann Garcia's avatar
Yann Garcia committed
241
  // Encode headers excepeted the Content-Length
242
  const LibItsHttp__TypesAndValues::HeaderLines& headers = p_response.header();
garciay's avatar
garciay committed
243
  std::string content_type;
244
245
246
247
248
249
  for (int i = 0; i < headers.size_of(); i++) {
    const LibItsHttp__TypesAndValues::HeaderLine& header = headers[i];
    loggers::get_instance().log_msg("http_codec::encode_response: Processing header ", header.header__name());
    p_encoding_buffer.put_cs(header.header__name());
    p_encoding_buffer.put_cs(": ");
    if (std::string(static_cast<const char*>(header.header__name())).compare("Content-Length") == 0) {
garciay's avatar
garciay committed
250
      continue;
251
252
253
254
255
256
    } else {
      const OPTIONAL<LibItsHttp__TypesAndValues::charstring__list>& o = header.header__value();
      if (o.ispresent()) {
        const LibItsHttp__TypesAndValues::charstring__list& v = dynamic_cast<const OPTIONAL<LibItsHttp__TypesAndValues::charstring__list> &>(o);
        if (v.size_of() > 0) {
          loggers::get_instance().log_msg("http_codec::encode_response: Processing value ", v[0]);
Yann Garcia's avatar
Yann Garcia committed
257
          if (std::string(static_cast<const char*>(header.header__name())).compare("Content-Type") == 0) { // Store it for HTTP body payload encoding
garciay's avatar
garciay committed
258
259
260
261
262
            int j = 1;
            while (j < v.size_of()) {
              content_type += v[j++];
            } // End of 'while' statement
          }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
          p_encoding_buffer.put_cs(v[0]);
          int j = 1;
          while (j < v.size_of()) {
                p_encoding_buffer.put_cs(", ");
                loggers::get_instance().log_msg("http_codec::encode_response: Processing value ", v[j]);
                p_encoding_buffer.put_cs(v[j]);
                j += 1;
          } // End of 'while' statement
        }
      } // else, do not include it
    }
    p_encoding_buffer.put_cs("\r\n");
  } // End of 'for' statement

garciay's avatar
garciay committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  // Encode message body
  const OPTIONAL<LibItsHttp__MessageBodyTypes::HttpMessageBody>& v = p_response.body();
  OCTETSTRING os;
  if (v.ispresent()) {
    const LibItsHttp__MessageBodyTypes::HttpMessageBody& body = static_cast<const LibItsHttp__MessageBodyTypes::HttpMessageBody&>(*v.get_opt_value());
    loggers::get_instance().log_msg("http_codec::encode_response: body: ", body);
    if (encode_body(body, os, content_type) == -1) {
      _ec.length = 0;
      _ec.is_content_length_present = 0x00;
    } else {
      _ec.length = os.lengthof();
      _ec.is_content_length_present = 0x01;
    }
    loggers::get_instance().log("http_codec::encode_request: length=%d", _ec.length);
  } else {
    loggers::get_instance().log("http_codec::encode_request: HTTP body field not present");
    _ec.length = 0;
    _ec.is_content_length_present = 0x00;
  }

Yann Garcia's avatar
Yann Garcia committed
297
  // Encode Content-Length header
garciay's avatar
garciay committed
298
299
300
301
302
303
304
  if (_ec.length != 0) {
    p_encoding_buffer.put_cs(int2str(_ec.length + 2/*Stand for the last CRLF*/));
    _ec.is_content_length_present = 0x01;
  } else {
    p_encoding_buffer.put_cs("0");
    _ec.is_content_length_present = 0x00;
  }
Yann Garcia's avatar
Yann Garcia committed
305
  loggers::get_instance().log("http_codec::encode_response: Content-Length: %d - %x", _ec.length, _ec.is_content_length_present);
garciay's avatar
garciay committed
306
307
  p_encoding_buffer.put_cs("\r\n");
  
308
309
310
311
312
  p_encoding_buffer.put_cs("\r\n");
  if (_ec.length != 0) {
    p_encoding_buffer.put_os(os);
    p_encoding_buffer.put_cs("\r\n");
  }
garciay's avatar
garciay committed
313
  
314
315
316
  return 0;
}

Yann Garcia's avatar
Yann Garcia committed
317
int http_codec::decode_headers(TTCN_Buffer& decoding_buffer, LibItsHttp__TypesAndValues::HeaderLines& headers, std::string& p_content_type) {
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  loggers::get_instance().log(">>> http_codec::decode_headers");
  loggers::get_instance().log_to_hexa("http_codec::decode_headers", decoding_buffer);

  CHARSTRING cstr;
  int i = 0;
  while (true) {
    switch(get_line(decoding_buffer, cstr, true)) {
      case 0: {
          loggers::get_instance().log_msg("http_codec::decode_headers: ", cstr);
          LibItsHttp__TypesAndValues::HeaderLine header;
          if (decode_header(cstr, header) == -1) {
            loggers::get_instance().warning("http_codec::decode_headers: Failed to decode header %s", static_cast<const char*>(cstr));
            return -1;
          }
          headers[i++] = header;
Yann Garcia's avatar
Yann Garcia committed
333
334
335
336
337
338
339
340
          if (std::string(static_cast<const char*>(header.header__name())).compare("Content-Type") == 0) {
            if (header.header__value().is_present() != 0) {
              const PreGenRecordOf::PREGEN__RECORD__OF__CHARSTRING& l = static_cast<const PreGenRecordOf::PREGEN__RECORD__OF__CHARSTRING&>(*header.header__value().get_opt_value());
              p_content_type = static_cast<const char*>(l[0]);
            } else {
              p_content_type = "";
            }
          }
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
        }
        break;
      case 1:
        loggers::get_instance().log_msg("<<< http_codec::decode_headers: ", headers);
        return 0;
      case -1:
        loggers::get_instance().warning("http_codec::decode_headers: Failed to decode headers");
        return -1;
    } // End of 'switch' statement
  } // End of 'while' statement
}

int http_codec::decode_header(CHARSTRING& header_line, LibItsHttp__TypesAndValues::HeaderLine& header) {
  loggers::get_instance().log_msg(">>> http_codec::decode_header", header_line);

  try {
    std::string str(static_cast<const char*>(header_line));
    std::regex rgx ("([0-9a-zA-Z-]+)\\:\\s+(.+)(,(.+))*");
    std::sregex_iterator begin(str.cbegin(), str.cend(), rgx);
    std::smatch m = *begin;
    if (m.size() < 5) {
      loggers::get_instance().warning("http_codec::decode_header: Failed to decode header %s", str.c_str());
      return -1;
    }
    loggers::get_instance().log("http_codec::decode_header: %d", m.size());
    header.header__name() = CHARSTRING(m[1].str().c_str());
    LibItsHttp__TypesAndValues::charstring__list v;
    for (unsigned int j = 0; j < m.size(); j++) {
      if (m[j + 2].str().length() == 0) {
        break;
      }
      v[j] = CHARSTRING(m[j + 2].str().c_str());
    } // End of 'for' statement
    header.header__value() = OPTIONAL<LibItsHttp__TypesAndValues::charstring__list>(v);

    if (m[1].str().compare("Content-Length") == 0) {
      // Save the the body length
      loggers::get_instance().log("http_codec::decode_header: decoded Content-Length %s", m[2].str().c_str());
      _dc.length = std::stoi(m[2].str());
Yann Garcia's avatar
Yann Garcia committed
380
381
382
    } else if (m[1].str().compare("Transfer-Encoding") == 0) {
      if (m[2].str().find("chunked") != std::string::npos) {
        _dc.chunked = true;
Yann Garcia's avatar
Yann Garcia committed
383
        loggers::get_instance().log("http_codec::decode_header: decoded Transfer-Encoding %x", _dc.chunked);
Yann Garcia's avatar
Yann Garcia committed
384
      }
385
    }
Yann Garcia's avatar
Yann Garcia committed
386

387
388
389
390
391
392
393
    return 0;
  }
  catch(const std::logic_error& e) {
      return -1;
  }
}

garciay's avatar
garciay committed
394
int http_codec::encode_body(const LibItsHttp__MessageBodyTypes::HttpMessageBody& p_message_body, OCTETSTRING& p_encoding_buffer, const std::string& p_content_type) {
395
  loggers::get_instance().log_msg(">>> http_codec::encode_body: ", (const Base_Type&)p_message_body);
garciay's avatar
garciay committed
396
397
398
399
400
401

  // Sanity check
  if (p_content_type.empty()) {
    loggers::get_instance().warning("http_codec::encode_body: Failed to select a codec for  HTTP body payload");
    return -1;
  }
402

garciay's avatar
garciay committed
403
  if (p_message_body.ischosen(LibItsHttp__MessageBodyTypes::HttpMessageBody::ALT_binary__body)) {
garciay's avatar
garciay committed
404
405
406
407
408
409
    const LibItsHttp__BinaryMessageBodyTypes::BinaryBody& binary_body = p_message_body.binary__body();
    if (binary_body.ischosen(LibItsHttp__BinaryMessageBodyTypes::BinaryBody::ALT_raw)) {
      p_encoding_buffer = OCTETSTRING(binary_body.raw().lengthof(), (unsigned char*)static_cast<const unsigned char*>(binary_body.raw()));
    } else {
      std::map<std::string, std::unique_ptr<codec<Record_Type, Record_Type> > >::const_iterator it;
      bool processed = false;
vagrant's avatar
vagrant committed
410
      if (p_content_type.find("x-its") != std::string::npos) {
411
        loggers::get_instance().log("http_codec::encode_body: Find x-its");
garciay's avatar
garciay committed
412
        it = _codecs.find("http_its"); // TODO Use params
garciay's avatar
garciay committed
413
        if (it != _codecs.cend()) {
garciay's avatar
garciay committed
414
          loggers::get_instance().log("http_codec::encode_body: Call '%s'", it->first.c_str());
415
          _codecs["http_its"]->encode((Record_Type&)binary_body.ieee1609dot2__data(), p_encoding_buffer); // TODO Use params
garciay's avatar
garciay committed
416
          processed = true;
garciay's avatar
garciay committed
417
418
419
420
        }
      } // TODO Add new HTTP message codec here
      if (!processed) {
        loggers::get_instance().warning("http_codec::encode_body: Unsupported HTTP codec, use raw field as default");
garciay's avatar
garciay committed
421
        p_encoding_buffer = OCTETSTRING(0, nullptr);
garciay's avatar
garciay committed
422
423
      }
    }
424
425
426
427
428
429
430
431
  } else if (p_message_body.ischosen(LibItsHttp__MessageBodyTypes::HttpMessageBody::ALT_html__body)) {
    p_encoding_buffer = OCTETSTRING(p_message_body.html__body().lengthof(), (unsigned char*)static_cast<const char*>(p_message_body.html__body()));
  } else if (p_message_body.ischosen(LibItsHttp__MessageBodyTypes::HttpMessageBody::ALT_text__body)) {
    p_encoding_buffer = OCTETSTRING(p_message_body.text__body().lengthof(), (unsigned char*)static_cast<const char*>(p_message_body.text__body()));
  } else if (p_message_body.ischosen(LibItsHttp__MessageBodyTypes::HttpMessageBody::ALT_xml__body)) {
    const LibItsHttp__XmlMessageBodyTypes::XmlBody& xml_body = p_message_body.xml__body();
    if (xml_body.ischosen(LibItsHttp__XmlMessageBodyTypes::XmlBody::ALT_raw)) {
      p_encoding_buffer = OCTETSTRING(xml_body.raw().lengthof(), (unsigned char*)static_cast<const char*>(xml_body.raw()));
garciay's avatar
garciay committed
432
433
434
    } else {
      std::map<std::string, std::unique_ptr<codec<Record_Type, Record_Type> > >::const_iterator it;
      bool processed = false;
Yann Garcia's avatar
Yann Garcia committed
435
      loggers::get_instance().log("http_codec::encode_body: Content-Type:'%s'", p_content_type.c_str());
vagrant's avatar
vagrant committed
436
      if (p_content_type.find("held") != std::string::npos) {
garciay's avatar
garciay committed
437
        it = _codecs.find("held"); // TODO Use params
garciay's avatar
garciay committed
438
439
        if (it != _codecs.cend()) {
          loggers::get_instance().log("http_codec::encode_body: Call 'held_codec'");
garciay's avatar
garciay committed
440
441
          _codecs["held"]->encode((Record_Type&)xml_body, p_encoding_buffer); // TODO Use params
          processed = true;
garciay's avatar
garciay committed
442
        }
vagrant's avatar
vagrant committed
443
      } else if (p_content_type.find("lost") != std::string::npos) {
garciay's avatar
garciay committed
444
        it = _codecs.find("lost"); // TODO Use params
garciay's avatar
garciay committed
445
446
        if (it != _codecs.cend()) {
          loggers::get_instance().log("http_codec::encode_body: Call 'lost_codec'");
garciay's avatar
garciay committed
447
448
          _codecs["lost"]->encode((Record_Type&)xml_body, p_encoding_buffer); // TODO Use params
          processed = true;
garciay's avatar
garciay committed
449
450
451
        }
      } // TODO Add new HTTP message codec here
      if (!processed) {
452
        loggers::get_instance().warning("http_codec::encode_body: Unsupported HTTP codec, use raw field as default");
garciay's avatar
garciay committed
453
        p_encoding_buffer = OCTETSTRING(0, nullptr);
454
455
456
      }
    }
  } else {
garciay's avatar
garciay committed
457
    loggers::get_instance().warning("http_codec::encode_body: Failed to encode HTTP message body");
458
459
460
461
462
463
464
465
466
    return -1;
  }
  loggers::get_instance().log_msg("http_codec::encode_body: HTTP message ", p_encoding_buffer);
  _ec.length = p_encoding_buffer.lengthof();
  loggers::get_instance().log("http_codec::encode_body: HTTP message length: %d", _ec.length);

  return 0;
}

garciay's avatar
garciay committed
467
int http_codec::decode_body(TTCN_Buffer& decoding_buffer, LibItsHttp__MessageBodyTypes::HttpMessageBody& message_body, const std::string& p_content_type) {
468
469
  loggers::get_instance().log(">>> http_codec::decode_body");
  loggers::get_instance().log_to_hexa("http_codec::decode_body", decoding_buffer);
garciay's avatar
garciay committed
470
  loggers::get_instance().log("http_codec::decode_body: # of codecs=%d - %p", _codecs.size(), this);
garciay's avatar
garciay committed
471
  loggers::get_instance().log("http_codec::decode_body: Content-Type=%s", p_content_type.c_str());
472

garciay's avatar
garciay committed
473
  // Sanity checks
474
475
476
  if (decoding_buffer.get_len() - decoding_buffer.get_pos() <= 0) {
    return -1;
  }
garciay's avatar
garciay committed
477
478
479
480
  /* TODO Uncommentif (p_content_type.empty()) {
    loggers::get_instance().warning("http_codec::encode_body: Failed to select a codec for  HTTP body payload");
    return -1;
    }*/
481
482
483

  OCTETSTRING s(decoding_buffer.get_len() - decoding_buffer.get_pos(), decoding_buffer.get_data() + decoding_buffer.get_pos());
  loggers::get_instance().log_msg("http_codec::decode_body: raw body=", s);
484
  
Yann Garcia's avatar
Yann Garcia committed
485
  // Align the payload length with the specified Content-Lenght value
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
  loggers::get_instance().log("http_codec::decode_body: _dc.length=%d - body length=%d", _dc.length, s.lengthof());
  OCTETSTRING body;
  if (_dc.length != 0) {
    const unsigned char* p = static_cast<const unsigned char *>(s);
    if ((unsigned int)s.lengthof() <= _dc.length) {
      body = OCTETSTRING(s.lengthof(), p);
    } else {
      body = OCTETSTRING(_dc.length, p);
    }
  } else {
    loggers::get_instance().warning("http_codec::decode_body: No Conten-Length header, process all remaining bytes");
    body = s;
  }
  loggers::get_instance().log_msg("http_codec::decode_body: Aligned body=", body);
  // Remove CRLF if any
  int counter = 0;
  if ((body[body.lengthof() - 1].get_octet() == 0x0d) || (body[body.lengthof() - 1].get_octet() == 0x0a)) {
    counter += 1;
    if ((body[body.lengthof() - 2].get_octet() == 0x0d) || (body[body.lengthof() - 2].get_octet() == 0x0a)) {
      counter += 1;
    }
  }
Yann Garcia's avatar
Yann Garcia committed
508
  loggers::get_instance().log("http_codec::decode_body: counter=%d", counter);
Yann Garcia's avatar
Yann Garcia committed
509
  loggers::get_instance().log("http_codec::decode_body: body length=%d", body.lengthof());
510
  body = OCTETSTRING(body.lengthof() - counter, static_cast<const unsigned char*>(body));
Yann Garcia's avatar
Yann Garcia committed
511
  if (_dc.chunked){
Yann Garcia's avatar
Yann Garcia committed
512
    counter = 0;
Yann Garcia's avatar
Yann Garcia committed
513
514
515
516
517
518
519
    int prev = 0;
    OCTETSTRING os(0, nullptr);
    do {
      while (counter < body.lengthof()) { // Extract the size of the chunk <chunk size>\r[\n]
        if ((body[counter].get_octet() == '\r') || (body[counter].get_octet() == '\n')) {
          break;
        }
Yann Garcia's avatar
Yann Garcia committed
520
        counter += 1;
Yann Garcia's avatar
Yann Garcia committed
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
      } // End of 'while' statement
      loggers::get_instance().log("http_codec::decode_body: Chunked(0): prev = %d, counter=%d / %d", prev, counter, body.lengthof());
      if (counter < body.lengthof()) {
        int idx = counter - prev;
        OCTETSTRING trunk(idx, static_cast<const unsigned char*>(body));
        loggers::get_instance().log_msg("http_codec::decode_body: trunk: ", trunk);
        std::string str((const char*)static_cast<const unsigned char*>(trunk), idx);
        loggers::get_instance().log("http_codec::decode_body: str: '%s'", str.c_str());
        int len = std::stoi(str, nullptr, 16);//converter::get_instance().string_to_int(str);
        loggers::get_instance().log("http_codec::decode_body: Chunk len: %d", len);
        while (counter < body.lengthof() && ((body[counter].get_octet() == '\r') || (body[counter].get_octet() == '\n'))) { // Skip additional \n
          counter += 1;
        } // End of 'while' statement
        if (counter < body.lengthof()) {
          loggers::get_instance().log("http_codec::decode_body: Chunked (1): prev = %d, counter=%d / %d", prev, counter, body.lengthof());
          os += OCTETSTRING(len, counter + static_cast<const unsigned char*>(body));
          loggers::get_instance().log_msg("http_codec::decode_body: os=", os);
          counter += len;
          loggers::get_instance().log("http_codec::decode_body: Chunked: %02x %02x %02x", body[counter].get_octet(), body[counter + 1].get_octet(), body[counter + 2].get_octet());
          loggers::get_instance().log("http_codec::decode_body: Chunked (2): prev = %d, counter=%d / %d", prev, counter, body.lengthof());
          while (counter < body.lengthof() && ((body[counter].get_octet() == '\r') || (body[counter].get_octet() == '\n'))) { // Skip additional \n
            counter += 1;
          } // End of 'while' statement
          prev = counter;
          loggers::get_instance().log("http_codec::decode_body: Chunked (3): prev = %d, counter=%d / %d", prev, counter, body.lengthof());
        }
Yann Garcia's avatar
Yann Garcia committed
547
      }
Yann Garcia's avatar
Yann Garcia committed
548
549
    } while (counter < body.lengthof()); // Process next chunk if any
    body = os;
Yann Garcia's avatar
Yann Garcia committed
550
  }
551
552
553
554
555
556
557
558
559
560
561
562
563
  loggers::get_instance().log_msg("http_codec::decode_body: Finalised body=", body);
  // Check if HTTP message body contains binary characters
  for (int i = 0; i < body.lengthof(); i++) {
    unsigned char c = body[i].get_octet();
    if (!std::isprint(c) && !std::isspace(c) && !std::ispunct(c)) {
      loggers::get_instance().log("http_codec::decode_body: Byte #%d is not printable: 0x%02x", i, body[i].get_octet());
      _dc.is_binary = 0x01;
      break;
    }
  } // End of 'for' statement
  loggers::get_instance().log("http_codec::decode_body: Binary mode: %x", _dc.is_binary);
  LibItsHttp__MessageBodyTypes::HttpMessageBody v;
  if (_dc.is_binary == 0x01) {
564
    LibItsHttp__BinaryMessageBodyTypes::BinaryBody binary_body;
garciay's avatar
garciay committed
565
    std::map<std::string, std::unique_ptr<codec<Record_Type, Record_Type> > >::const_iterator it;
garciay's avatar
garciay committed
566
567
568
569
    bool processed = false;
    // TODO To be refined adding a string identifier to check which codec to use. E.g. held_code.id() returns "xmlns=\"urn:ietf:params:xml:ns:geopriv:held\">"
    if (p_content_type.find("x-its") != std::string::npos) {
      loggers::get_instance().log("http_codec::decode_body: Find 'x-its'");
garciay's avatar
garciay committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
      it = _codecs.cbegin();//_codecs.find("http_its");
      if (it != _codecs.cend()) {
        /***
            FIXME:
            This code generate a codedump, I don't undertsand the reason.
            The same code works file for Ng112 HELD & LOST codec. Ununderstandable!!!!
            ==> Use a patch
            if (_codecs["http_its"].get() != nullptr) {
            loggers::get_instance().log("http_codec::decode_body: Call 'http_etsi_ieee1609dot2_codec'");
            if (_codecs["http_its"]->decode(body, (Record_Type&)binary_body) == 0) {
            processed = true;
            }
            }*/
        loggers::get_instance().log("http_codec::decode_body: Call '%s'", it->first.c_str());
        http_etsi_ieee1609dot2_codec* codec = new http_etsi_ieee1609dot2_codec();
        if (codec->decode(body, binary_body.ieee1609dot2__data()) == 0) {
          message_body.binary__body() = binary_body;
587
588
          processed = true;
        }
garciay's avatar
garciay committed
589
        delete codec;
garciay's avatar
garciay committed
590
591
592
593
      }
    } // TODO Add new HTTP message codec here
    if (!processed) {
      loggers::get_instance().warning("http_codec::decode_body: Unsupported HTTP codec, use raw field as default");
594
595
      binary_body.raw() = body;
      message_body.binary__body() = binary_body;
garciay's avatar
garciay committed
596
    }
597
598
599
600
  } else {
    // Convert into string
    params p;
    p["decode_str"] = std::string(static_cast<const unsigned char*>(body), body.lengthof() + static_cast<const unsigned char*>(body));
Yann Garcia's avatar
Yann Garcia committed
601
    loggers::get_instance().log("http_codec::decode_body: decode_str: %s", p["decode_str"].c_str());
602
603
604
605
606
    // Try to identify xml
    if (p["decode_str"].find("<?xml version=") != std::string::npos) {
       loggers::get_instance().log("http_codec::decode_body: Find xml message");
       LibItsHttp__XmlMessageBodyTypes::XmlBody xml_body;
       // TODO To be refined adding a string identifier to check which codec to use. E.g. held_code.id() returns "xmlns=\"urn:ietf:params:xml:ns:geopriv:held\">"
Yann Garcia's avatar
Yann Garcia committed
607
608
609
610
       if (
           (p["decode_str"].find("=\"urn:ietf:params:xml:ns:geopriv:held\"") != std::string::npos) ||
           (p["decode_str"].find("=\"urn:ietf:params:xml:ns:pidf\"") != std::string::npos)
           ) {
611
612
613
614
615
616
617
618
619
620
621
622
623
624
         loggers::get_instance().log("http_codec::decode_body: Find 'urn:ietf:params:xml:ns:geopriv:held'");
         if (_codecs["held"].get() != nullptr) {
           loggers::get_instance().log("http_codec::decode_body: Call 'held_codec'");
           if (_codecs["held"]->decode(body, (Record_Type&)xml_body, &p) == -1) {
             loggers::get_instance().warning("http_codec::decode_body: Failed to decode HELD message");
             xml_body.raw() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
           } else {
             loggers::get_instance().log_msg("http_codec::decode_body: Decoded message:", xml_body);
             message_body.xml__body() = xml_body;
           }
         } else {
           loggers::get_instance().warning("http_codec::decode_body: No codec for HELD");
           xml_body.raw() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
         }
garciay's avatar
garciay committed
625
         message_body.xml__body() = xml_body;
Yann Garcia's avatar
Yann Garcia committed
626
       } else if (p["decode_str"].find("=\"urn:ietf:params:xml:ns:lost1\"") != std::string::npos) {
Yann Garcia's avatar
Yann Garcia committed
627
         loggers::get_instance().log("http_codec::decode_body: Find 'urn:ietf:params:xml:ns:lost1'");
garciay's avatar
garciay committed
628
629
630
631
632
633
634
635
636
637
638
639
640
641
         if (_codecs["lost"].get() != nullptr) {
           loggers::get_instance().log("http_codec::decode_body: Call 'lost_codec'");
           if (_codecs["lost"]->decode(body, (Record_Type&)xml_body, &p) == -1) {
             loggers::get_instance().warning("http_codec::decode_body: Failed to decode LOST message");
             xml_body.raw() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
           } else {
             loggers::get_instance().log_msg("http_codec::decode_body: Decoded message:", xml_body);
             message_body.xml__body() = xml_body;
           }
         } else {
           loggers::get_instance().warning("http_codec::decode_body: No codec for LOST");
           xml_body.raw() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
         }
         message_body.xml__body() = xml_body;
Yann Garcia's avatar
Yann Garcia committed
642
643
644
645
646
       } else {
         loggers::get_instance().warning("http_codec::decode_body: No XML codec found");
         xml_body.raw() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
         message_body.xml__body() = xml_body;
       }
647
648
    } else if (p["decode_str"].find("<html>") != std::string::npos) { // Try to identify HTML
      loggers::get_instance().log("http_codec::decode_body: Find html message");
garciay's avatar
garciay committed
649
650
      LibItsHttp__MessageBodyTypes::HtmlBody html_body;
      message_body.html__body() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
    } else {
      loggers::get_instance().log("http_codec::decode_body: Use textBdy as default");
      LibItsHttp__MessageBodyTypes::TextBody text_body;
      message_body.text__body() = CHARSTRING(body.lengthof(), (char*)static_cast<const unsigned char*>(body));
    }
  }
  
  return 0;
}

int http_codec::get_line(TTCN_Buffer& buffer, CHARSTRING& to, const bool concatenate_header_lines) {
    unsigned int i = 0;
    const unsigned char *cc_to = buffer.get_read_data();

    // Sanity checks
    if(buffer.get_read_len() == 0) {
        return -1;
    }

    while (true) {
      // Skip spaces, and empty lines
        for( ; i < buffer.get_read_len() && cc_to[i] != '\0' && cc_to[i] != '\r' && cc_to[i] != '\n'; i++);
        if(i >= buffer.get_read_len()) { // No more characters to process
            to = CHARSTRING("");
            return -1;
        } else if(cc_to[i] == '\n') { // New line found, we don't care is '\r' is missing
        if ((i > 0) && ((i + 1) < buffer.get_read_len()) && concatenate_header_lines && ((cc_to[i + 1] == ' ') || (cc_to[i + 1] == '\t'))) {
          i += 1; // Skip it
        } else {
            to = CHARSTRING(i, (const char*)cc_to);
            buffer.set_pos(buffer.get_pos() + i + 1);
            return i == 0 ? 1 : 0;
        }
      } else {
        if ((i + 1) < buffer.get_read_len() && cc_to[i + 1] != '\n') {
             return -1;
        } else if(i > 0 && (i + 2) < buffer.get_read_len() && concatenate_header_lines && (cc_to[i+2] == ' ' || cc_to[i+2] == '\t')) {
             i += 2;
        } else {
            to = CHARSTRING(i, (const char*)cc_to);
            buffer.set_pos(buffer.get_pos() + i + 2);
            return i == 0 ? 1 : 0;
        }
      }
    } // End of 'while' statement
}

void http_codec::set_payload_codecs(const std::string& p_codecs) {
  loggers::get_instance().log(">>> http_codec::set_payload_codecs: %s", p_codecs.c_str());
  
  // Sanity check
  if (p_codecs.length() == 0) {
    return;
  }
  
  // Extract codecs
  try {
garciay's avatar
garciay committed
708
    std::regex rgx("(\\w+):(\\w+)(;(\\w+):(\\w+))*");
709
710
711
712
713
714
    std::sregex_iterator begin(p_codecs.cbegin(), p_codecs.cend(), rgx);
    std::sregex_iterator end = std::sregex_iterator();
    // E.g. 9 - xml - :held_codec - held_codec - ;html:html_codec - html:html_codec - html - :html_codec - html_codec
    for (std::sregex_iterator it = begin; it != end; ++it) {
      std::smatch m = *it;
      loggers::get_instance().log("http_codec::set_payload_codecs: %d - %s - %s - %s - %s - %s - %s - %s - %s", m.size(), m[1].str().c_str(), m[2].str().c_str(), m[3].str().c_str(), m[4].str().c_str(), m[5].str().c_str(), m[6].str().c_str(), m[7].str().c_str(), m[8].str().c_str());
garciay's avatar
garciay committed
715
716
      for (unsigned int j = 1; j < m.size() - 1; j += 3) { // Exclude m[0]
        loggers::get_instance().log("http_codec::set_payload_codecs: insert (%s, %s), j = %d", m[j].str().c_str(), m[j + 1].str().c_str(), j);
garciay's avatar
garciay committed
717
718
719
        if (m[j].str().empty()) {
          break;
        }
720
        std::string key(m[j].str());
garciay's avatar
garciay committed
721
        _codecs.insert(std::make_pair(key, std::unique_ptr<codec<Record_Type, Record_Type> >(codec_stack_builder::get_instance()->get_codec(m[j + 1].str().c_str()))));
722
723
      } // End of 'for' statement
    } // End of 'for' statement
garciay's avatar
garciay committed
724
    loggers::get_instance().log("http_codec::set_payload_codecs: _codecs length=%d - %p", _codecs.size(), this);
725
726
727
728
729
730
  }
  catch(const std::logic_error& e){
    loggers::get_instance().warning("http_codec::set_payload_codecs: std::logic_error: %s", e.what());
    _codecs.clear();
  }
}