Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions api/net/http/client.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2016 Oslo and Akershus University College of Applied Sciences
// Copyright 2016-2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,26 +19,24 @@
#ifndef HTTP_CLIENT_HPP
#define HTTP_CLIENT_HPP

#include "common.hpp"
#include "request.hpp"
#include "response.hpp"
// http
#include "client_connection.hpp"

#include <net/tcp/tcp.hpp>
#include "connection.hpp"
#include "error.hpp"
#include <vector>
#include <map>

namespace http {

class Client {
public:
using TCP = net::TCP;
using Host = net::tcp::Socket;
using TCP = net::TCP;
using Host = net::tcp::Socket;

using Connection_set = std::vector<std::unique_ptr<Connection>>;
using Connection_mapset = std::map<Host, Connection_set>;
using Connection_set = std::vector<std::unique_ptr<Client_connection>>;
using Connection_mapset = std::map<Host, Connection_set>;

using timeout_duration = Connection::timeout_duration;
using timeout_duration = Client_connection::timeout_duration;

const static timeout_duration DEFAULT_TIMEOUT; // client.cpp, 5s
constexpr static size_t DEFAULT_BUFSIZE = 2048;
Expand Down Expand Up @@ -153,10 +151,11 @@ namespace http {
inline void post(Host host, std::string path, Header_set hfields, const std::string& data, Response_handler cb, Options options = {});

private:
TCP& tcp_;
Connection_mapset conns_;
friend class Client_connection;

bool keep_alive_ = false;
TCP& tcp_;
Connection_mapset conns_;
bool keep_alive_ = false;

void resolve(const std::string& host, ResolveCallback);

Expand All @@ -172,9 +171,9 @@ namespace http {
/** Add data and content length */
void add_data(Request&, const std::string& data);

Connection& get_connection(const Host host);
Client_connection& get_connection(const Host host);

void close(Connection&);
void close(Client_connection&);

}; // < class Client

Expand Down
69 changes: 69 additions & 0 deletions api/net/http/client_connection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2016-2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#ifndef HTTP_CLIENT_CONNECTION_HPP
#define HTTP_CLIENT_CONNECTION_HPP

// http
#include "common.hpp"
#include "connection.hpp"
#include "error.hpp"

#include <util/timer.hpp>

namespace http {

class Client;

class Client_connection : public Connection {
public:
using timeout_duration = std::chrono::milliseconds;

public:
explicit Client_connection(Client&, TCP_conn);

bool available() const
{ return on_response_ == nullptr && keep_alive_; }

bool occupied() const
{ return !available(); }

void send(Request_ptr, Response_handler, const size_t bufsize, timeout_duration = timeout_duration::zero());

private:
Client& client_;
Response_handler on_response_;
Timer timer_;
timeout_duration timeout_dur_;

void send_request(const size_t bufsize);

void recv_response(buffer_t buf, size_t len);

void end_response(Error err = Error::NONE);

void timeout_request()
{ end_response(Error::TIMEOUT); }

void close();

}; // < class Client_connection

} // < namespace http

#endif // < HTTP_CLIENT_CONNECTION_HPP
79 changes: 32 additions & 47 deletions api/net/http/connection.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2016 Oslo and Akershus University College of Applied Sciences
// Copyright 2016-2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,74 +19,59 @@
#ifndef HTTP_CONNECTION_HPP
#define HTTP_CONNECTION_HPP

#include "common.hpp"
// http
#include "request.hpp"
#include "response.hpp"
#include "error.hpp"

#include <net/tcp/connection.hpp>
#include <map>
#include <vector>
#include <delegate>
#include <util/timer.hpp>

namespace http {

class Connection {
public:
using TCP_conn_ptr = net::tcp::Connection_ptr;
using Peer = net::tcp::Socket;
using buffer_t = net::tcp::buffer_t;
using Close_handler = delegate<void(Connection&)>;
using timeout_duration = std::chrono::milliseconds;
using TCP_conn = net::tcp::Connection_ptr;
using Peer = net::tcp::Socket;
using buffer_t = net::tcp::buffer_t;

public:

explicit Connection(TCP_conn_ptr, Close_handler);
inline explicit Connection(TCP_conn tcpconn, bool keep_alive = true);

template <typename TCP>
explicit Connection(TCP&, Peer, Close_handler);

bool available() const
{ return on_response_ == nullptr && keep_alive_; }

bool occupied() const
{ return !available(); }
explicit Connection(TCP&, Peer);

void send(Request_ptr, Response_handler, const size_t bufsize, timeout_duration = timeout_duration::zero());

net::tcp::port_t local_port() const
net::tcp::port_t local_port() const noexcept
{ return (tcpconn_) ? tcpconn_->local_port() : 0; }

Peer peer() const
Peer peer() const noexcept
{ return (tcpconn_) ? tcpconn_->remote() : Peer(); }
//bool operator==(const Connection& other)
//{ return this == &other; }
//{ return tcpconn_->local_port() == other.tcpconn_->local_port(); }

private:
TCP_conn_ptr tcpconn_;
void timeout()
{ tcpconn_->is_closing() ? tcpconn_->abort() : tcpconn_->close(); }

protected:
TCP_conn tcpconn_;
Request_ptr req_;
Response_ptr res_;
Close_handler on_close_;
Response_handler on_response_;
Timer timer_;

timeout_duration timeout_dur_;
bool keep_alive_;

void send_request(const size_t bufsize);

void recv_response(buffer_t buf, size_t len);

void end_response(Error err = Error::NONE);

void timeout_request()
{ end_response(Error::TIMEOUT); }

void close();
bool keep_alive_;

}; // < class Connection

inline Connection::Connection(TCP_conn tcpconn, bool keep_alive)
: tcpconn_{std::move(tcpconn)},
req_{nullptr},
res_{nullptr},
keep_alive_{keep_alive}
{
Ensures(tcpconn_ != nullptr);
debug("<http::Connection> Created %u -> %s %p\n", local_port(), peer().to_string().c_str(), this);
}

template <typename TCP>
Connection::Connection(TCP& tcp, Peer addr)
: Connection(tcp.connect(addr))
{
}

} // < namespace http

#endif // < HTTP_CONNECTION_HPP
1 change: 0 additions & 1 deletion diskimagebuild/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ set (CMAKE_CXX_STANDARD 14)

set(SOURCES main.cpp filetree.cpp writer.cpp)

include_directories(../api)
include_directories(../mod/GSL)
add_executable(diskbuilder ${SOURCES})

Expand Down
62 changes: 62 additions & 0 deletions diskimagebuild/fat_internal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Slim copy of <api/fs/fat_internal.hpp>

#pragma once
#ifndef FS_FAT_INTERNAL_HPP
#define FS_FAT_INTERNAL_HPP

#include <cstdint>

// Attribute masks
static const uint8_t ATTR_READ_ONLY = 0x01;
static const uint8_t ATTR_HIDDEN = 0x02;
static const uint8_t ATTR_SYSTEM = 0x04;
static const uint8_t ATTR_VOLUME_ID = 0x08;
static const uint8_t ATTR_DIRECTORY = 0x10;
static const uint8_t ATTR_ARCHIVE = 0x20;

// Mask for the last longname entry
static const uint8_t LAST_LONG_ENTRY = 0x40;

struct cl_dir
{
uint8_t shortname[11];
uint8_t attrib;
uint8_t pad1[8];
uint16_t cluster_hi;
uint32_t modified;
uint16_t cluster_lo;
uint32_t filesize;

} __attribute__((packed));

struct cl_long
{
uint8_t index;
uint16_t first[5];
uint8_t attrib;
uint8_t entry_type;
uint8_t checksum;
uint16_t second[6];
uint16_t zero;
uint16_t third[2];

} __attribute__((packed));

#endif
2 changes: 1 addition & 1 deletion diskimagebuild/writer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "filetree.hpp"

#include "../api/fs/mbr.hpp"
#include "../api/fs/fat_internal.hpp"
#include "fat_internal.hpp"
#include <cassert>
#include <cstring>

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ set(OS_OBJECTS
net/super_stack.cpp
net/http/header.cpp net/http/header_fields.cpp net/http/message.cpp net/http/request.cpp
net/http/response.cpp net/http/status_codes.cpp net/http/time.cpp net/http/version.cpp
net/http/mime_types.cpp net/http/client.cpp net/http/connection.cpp net/http/cookie.cpp
net/http/mime_types.cpp net/http/client.cpp net/http/client_connection.cpp net/http/cookie.cpp
fs/disk.cpp fs/filesystem.cpp fs/mbr.cpp fs/path.cpp
fs/fat.cpp fs/fat_async.cpp fs/fat_sync.cpp fs/memdisk.cpp
posix/fd.cpp posix/tcp_fd.cpp posix/udp_fd.cpp posix/unistd.cpp posix/fcntl.cpp posix/syslog.cpp
Expand Down
10 changes: 5 additions & 5 deletions src/net/http/client.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2016 Oslo and Akershus University College of Applied Sciences
// Copyright 2016-2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -197,7 +197,7 @@ namespace http {
stack.resolve(host, cb);
}

Connection& Client::get_connection(const Host host)
Client_connection& Client::get_connection(const Host host)
{
// return/create a set for the given host
auto& cset = conns_[host];
Expand All @@ -210,17 +210,17 @@ namespace http {
}

// no non-occupied connections, emplace a new one
cset.push_back(std::make_unique<Connection>(tcp_.connect(host), Connection::Close_handler{this, &Client::close}));
cset.push_back(std::make_unique<Client_connection>(*this, tcp_.connect(host)));
return *cset.back();
}

void Client::close(Connection& c)
void Client::close(Client_connection& c)
{
debug("<http::Client> Closing %u:%s %p\n", c.local_port(), c.peer().to_string().c_str(), &c);
auto& cset = conns_.at(c.peer());

cset.erase(std::remove_if(cset.begin(), cset.end(),
[port = c.local_port()] (const std::unique_ptr<Connection>& conn)->bool
[port = c.local_port()] (const std::unique_ptr<Client_connection>& conn)->bool
{
return conn->local_port() == port;
}));
Expand Down
Loading