Skip to content

Commit 5e35e0c

Browse files
authored
Merge pull request #1291 from AndreasAakesson/dev
TCP and CMake
2 parents c93832f + 46d4677 commit 5e35e0c

10 files changed

Lines changed: 55 additions & 20 deletions

File tree

api/net/http/message.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,28 @@ class Message {
178178
/// Set the content of the buffer holding intermediate information
179179
///
180180
void set_private_field(const char* base, const size_t length) noexcept;
181+
182+
/**
183+
* @brief Whether the headers are complete or not.
184+
*
185+
* @return True if complete, False if not
186+
*/
187+
inline bool headers_complete() const noexcept;
188+
189+
/**
190+
* @brief Sets the status whether the headers are complete or not
191+
*
192+
* @param[in] complete Indicates if complete
193+
*/
194+
inline void set_headers_complete(const bool complete) noexcept;
181195
private:
182196
///
183197
/// Class data members
184198
///
185199
Header header_fields_;
186200
Message_body message_body_;
187201
util::sview field_;
202+
bool headers_complete_;
188203
}; //< class Message
189204

190205
/**--v----------- Helper Functions -----------v--**/
@@ -204,6 +219,15 @@ inline size_t Message::content_length() const {
204219
inline bool Message::set_content_length(const size_t len) {
205220
return header_fields_.set_content_length(len);
206221
}
222+
223+
inline void Message::set_headers_complete(const bool complete) noexcept {
224+
headers_complete_ = complete;
225+
}
226+
227+
inline bool Message::headers_complete() const noexcept {
228+
return headers_complete_;
229+
}
230+
207231
/**--^-------- Inline Implementations --------^--**/
208232

209233
} //< namespace http

api/net/http/parse.hpp

Whitespace-only changes.

api/net/ip4/packet_ip4.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,14 @@ namespace net {
208208
void init(Protocol proto = Protocol::HOPOPT) noexcept {
209209
Expects(size() == 0);
210210
auto& hdr = ip_header();
211+
std::memset(&ip_header(), 0, sizeof(ip4::Header));
211212
hdr.version_ihl = 0x45;
212-
hdr.ds_ecn = 0;
213-
hdr.id = 0;
214-
hdr.frag_off_flags = 0;
213+
//hdr.ds_ecn = 0;
214+
//hdr.id = 0;
215+
//hdr.frag_off_flags = 0;
215216
hdr.ttl = DEFAULT_TTL;
216217
hdr.protocol = static_cast<uint8_t>(proto);
217-
hdr.check = 0;
218+
//hdr.check = 0;
218219
hdr.tot_len = 0x1400; // Big-endian 20
219220
increment_data_end(sizeof(IP4::header));
220221
}

etc/service.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ endforeach()
146146

147147
# add all extra libs
148148
foreach(LIBR ${LIBRARIES})
149+
# if relative path but not local, use includeos lib.
150+
if(NOT IS_ABSOLUTE ${LIBR} AND NOT EXISTS ${LIBR})
151+
set(OS_LIB "$ENV{INCLUDEOS_PREFIX}/includeos/lib/${LIBR}")
152+
if(EXISTS ${OS_LIB})
153+
message(STATUS "Cannot find local ${LIBR}; using ${OS_LIB} instead")
154+
set(LIBR ${OS_LIB})
155+
endif()
156+
endif()
149157
get_filename_component(LNAME ${LIBR} NAME_WE)
150158
add_library(libr_${LNAME} STATIC IMPORTED)
151159
set_target_properties(libr_${LNAME} PROPERTIES LINKER_LANGUAGE CXX)

lib/mana/src/server.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ void Server::process_route(Request_ptr req, Response_ptr res) {
7373
parsed_route.job(req, res);
7474
}
7575
catch (const Router_error& err) {
76-
printf("<Server> Router_error: %s - Responding with 404.\n", err.what());
7776
res->send_code(http::Not_Found, true);
7877
}
7978
}

src/net/http/client_connection.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace http {
8787
else
8888
{
8989
// this is the case when Status line is received, but not yet headers.
90-
if(res_->header().is_empty() && req_->method() != HEAD)
90+
if(not res_->headers_complete() && req_->method() != HEAD)
9191
{
9292
*res_ << data;
9393
res_->parse();
@@ -111,13 +111,18 @@ namespace http {
111111
try
112112
{
113113
const unsigned conlen = std::stoul(header.value(header::Content_Length).to_string());
114+
const unsigned body_size = res_->body().size();
114115
debug2("<http::Connection> [%s] Data: %u ConLen: %u Body:%u\n",
115-
req_->uri().to_string().to_string().c_str(), data.size(), conlen, res_->body().size());
116+
req_->uri().to_string().to_string().c_str(), data.size(), conlen, body_size);
116117
// risk buffering forever if no timeout
117-
if(conlen == res_->body().size())
118+
if(body_size == conlen)
118119
{
119120
end_response();
120121
}
122+
else if(body_size > conlen)
123+
{
124+
end_response({Error::INVALID});
125+
}
121126
}
122127
catch(...)
123128
{ end_response({Error::INVALID}); }

src/net/http/message.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace http {
2121

2222
///////////////////////////////////////////////////////////////////////////////
2323
Message::Message(const std::size_t limit) noexcept
24-
: header_fields_{limit}
24+
: header_fields_{limit}, headers_complete_{false}
2525
{}
2626

2727
///////////////////////////////////////////////////////////////////////////////

src/net/http/request.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ static http_parser_settings settings;
2828
__attribute__((constructor))
2929
static void _GFRGRGRgegerjiuo_()
3030
{
31-
settings.on_message_begin = [](http_parser* parser) {
32-
auto req = reinterpret_cast<Request*>(parser->data);
33-
req->set_method(
34-
http::method::code(
35-
http_method_str(static_cast<http_method>(parser->method))));
36-
return 0;
37-
};
38-
3931
settings.on_url = [](http_parser* parser, const char* at, size_t length) {
4032
auto req = reinterpret_cast<Request*>(parser->data);
4133
req->set_uri(URI{std::string{at, length}});
@@ -63,6 +55,10 @@ static void _GFRGRGRgegerjiuo_()
6355
settings.on_headers_complete = [](http_parser* parser) {
6456
auto req = reinterpret_cast<Request*>(parser->data);
6557
req->set_version(Version{parser->http_major, parser->http_minor});
58+
req->set_method(
59+
http::method::code(
60+
http_method_str(static_cast<http_method>(parser->method))));
61+
req->set_headers_complete(true);
6662
return 0;
6763
};
6864
}

src/net/http/response.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static void riegfjeriugfjreiougf()
5050
auto res = reinterpret_cast<Response*>(parser->data);
5151
res->set_version(Version{parser->http_major, parser->http_minor});
5252
res->set_status_code(static_cast<status_t>(parser->status_code));
53+
res->set_headers_complete(true);
5354
return 0;
5455
};
5556
};

src/net/tcp/tcp.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ void TCP::Port_util::increment_ephemeral()
6767
if(UNLIKELY(! has_free_ephemeral() ))
6868
throw TCP_error{"All ephemeral ports are taken"};
6969

70-
ephemeral_ = (ephemeral_ == port_ranges::DYNAMIC_END)
71-
? port_ranges::DYNAMIC_START
72-
: ephemeral_ + 1;
70+
ephemeral_++;
71+
72+
if(UNLIKELY(ephemeral_ == port_ranges::DYNAMIC_END))
73+
ephemeral_ = port_ranges::DYNAMIC_START;
7374

7475
// TODO: Avoid wrap around, increment ephemeral to next free port.
7576
// while(is_bound(ephemeral_)) ++ephemeral_; // worst case is like 16k iterations :D

0 commit comments

Comments
 (0)