Newer
Older
#include "LibItsHttp_TypesAndValues.hh"
#include "http_layer_factory.hh"
#include "codec_stack_builder.hh"
#include "loggers.hh"
#include "converter.hh"
using namespace std; // Required for isnan()
#include "LibItsHttp_TypesAndValues.hh"
#include "LibItsHttp_TestSystem.hh"
http_layer::http_layer(const std::string & p_type, const std::string & param) : t_layer<LibItsHttp__TestSystem::HttpPort>(p_type), _params(), _device_mode{false}, _stream_buffer(0, NULL)
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
{
loggers::get_instance().log(">>> http_layer::http_layer: %s, %s", to_string().c_str(), param.c_str());
// Setup parameters
params::convert(_params, param);
params::const_iterator it = _params.find(params::codecs);
if (it != _params.cend()) {
_codec.set_payload_codecs(it->second);
}
it = _params.find(params::device_mode);
if (it != _params.cend()) {
_device_mode = (1 == converter::get_instance().string_to_int(it->second));
}
it = _params.find(params::method);
if (it == _params.cend()) {
_params[params::method] = "POST";
}
it = _params.find(params::uri);
if (it == _params.cend()) {
_params[params::uri] = "/";
}
it = _params.find(params::host);
if (it == _params.cend()) {
_params[params::host] = "127.0.0.1";
}
it = _params.find(params::content_type);
if (it == _params.cend()) {
_params[params::content_type] = "application/text";
}
}
void http_layer::sendMsg(const LibItsHttp__TypesAndValues::HttpMessage& p_http_message, params& p_param) {
loggers::get_instance().log_msg(">>> http_layer::sendMsg: ", p_http_message);
// Encode HttpMessage
OCTETSTRING data;
_codec.encode(p_http_message, data);
send_data(data, _params);
}
void http_layer::send_data(OCTETSTRING& data, params& params) {
loggers::get_instance().log_msg(">>> http_layer::send_data: ", data);
if (_device_mode) { // Need to build an HTTP packet
loggers::get_instance().log("http_layer::send_data: Build http layer");
TTCN_Buffer buffer;
buffer.put_cs(_params[params::method].c_str());
buffer.put_c(' ');
buffer.put_cs(_params[params::uri].c_str());
buffer.put_cs(" HTTP/1.1\r\n");
buffer.put_cs("Host: ");
buffer.put_cs(_params[params::host].c_str());
buffer.put_cs("\r\n");
buffer.put_cs("Content-type: ");
buffer.put_cs(_params[params::content_type].c_str());
buffer.put_cs("\r\n");
buffer.put_cs("Content-length: ");
buffer.put_cs(static_cast<const char*>(int2str(data.lengthof() + 2/*Stand for the last CRLF*/)));
buffer.put_cs("\r\n\r\n");
buffer.put_os(data);
buffer.put_cs("\r\n");
data = OCTETSTRING(buffer.get_len(), buffer.get_data());
}
loggers::get_instance().log_msg("http_layer::send_data: ", data);
send_to_all_layers(data, params);
}
void http_layer::receive_data(OCTETSTRING& data, params& params)
{
loggers::get_instance().log_msg(">>> http_layer::receive_data: ", data);
YannGarcia
committed
loggers::get_instance().log_msg(">>> http_layer::receive_data: _stream_buffer= ", _stream_buffer);
// Decode HTTP message
LibItsHttp__TypesAndValues::HttpMessage http_message;
YannGarcia
committed
if (!_stream_buffer.is_bound() || (_stream_buffer.lengthof() == 0)) {
if (data.lengthof() < 6) { // 'HTTP/' or 'GET /'
loggers::get_instance().warning("http_layer::receive_data: Inconsistant buffer length");
return;
}
if (_codec.decode(data, http_message, ¶ms) == -1) {
loggers::get_instance().warning("http_layer::receive_data (0): Failed to decode data");
return;
}
YannGarcia
committed
} else { // First chunk is received
loggers::get_instance().warning("http_layer::receive_data: #######################################");
_stream_buffer += data;
if (_codec.decode(_stream_buffer, http_message, ¶ms) == -1) {
loggers::get_instance().warning("http_layer::receive_data (1): Failed to decode data");
return;
}
}
params::const_iterator it = params.find("missing_data");
YannGarcia
committed
if (it != params.cend()) { // Transport layer didn't reassemble chunks, keep current state and wait for the next chunk
// Store data and wait for the next stream
loggers::get_instance().warning("http_layer::receive_data: Missing data, save current stream");
_stream_buffer += data;
return; // Terminate here
} else {
// Reset stream buffer and continue
loggers::get_instance().log("http_layer::receive_data: Reset stream buffer");
_stream_buffer.clean_up();
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
}
if (_device_mode) {
OCTETSTRING os;
if (http_message.ischosen(LibItsHttp__TypesAndValues::HttpMessage::ALT_response)) {
if (http_message.response().body().ispresent()) {
LibItsHttp__MessageBodyTypes::HttpMessageBody& body = static_cast<LibItsHttp__MessageBodyTypes::HttpMessageBody&>(*http_message.response().body().get_opt_value());
if (body.ischosen(LibItsHttp__MessageBodyTypes::HttpMessageBody::ALT_binary__body)) {
LibItsHttp__BinaryMessageBodyTypes::BinaryBody& binary = body.binary__body();
if (binary.ischosen(LibItsHttp__BinaryMessageBodyTypes::BinaryBody::ALT_raw)) {
os = binary.raw();
} else {
loggers::get_instance().warning("http_layer::receive_data: A raw binary payload is expected");
}
} else if (body.ischosen(LibItsHttp__MessageBodyTypes::HttpMessageBody::ALT_html__body)){
// TODO To be done
loggers::get_instance().warning("http_layer::receive_data: Not implemented yet");
} else if (body.ischosen(LibItsHttp__MessageBodyTypes::HttpMessageBody::ALT_xml__body)){
// TODO To be done
loggers::get_instance().warning("http_layer::receive_data: Not implemented yet");
} else if (body.ischosen(LibItsHttp__MessageBodyTypes::HttpMessageBody::ALT_text__body)){
// TODO To be done
loggers::get_instance().warning("http_layer::receive_data: Not implemented yet");
}
receive_to_all_layers(os, params);
} else {
loggers::get_instance().warning("http_layer::receive_data: No body present");
}
} else {
loggers::get_instance().warning("http_layer::receive_data: An HTTP response is expected");
}
} else {
// Pass it to the ports
to_all_upper_ports(http_message, params);
}
}
http_layer_factory http_layer_factory::_f;