diff --git a/api/net/http/client.hpp b/api/net/http/client.hpp index 5f8964ae39..8580f2fcdf 100644 --- a/api/net/http/client.hpp +++ b/api/net/http/client.hpp @@ -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"); @@ -19,12 +19,10 @@ #ifndef HTTP_CLIENT_HPP #define HTTP_CLIENT_HPP -#include "common.hpp" -#include "request.hpp" -#include "response.hpp" +// http +#include "client_connection.hpp" + #include -#include "connection.hpp" -#include "error.hpp" #include #include @@ -32,13 +30,13 @@ 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>; - using Connection_mapset = std::map; + using Connection_set = std::vector>; + using Connection_mapset = std::map; - 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; @@ -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); @@ -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 diff --git a/api/net/http/client_connection.hpp b/api/net/http/client_connection.hpp new file mode 100644 index 0000000000..28d3f4f86e --- /dev/null +++ b/api/net/http/client_connection.hpp @@ -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 + +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 diff --git a/api/net/http/connection.hpp b/api/net/http/connection.hpp index ae6656d955..6e6feb092b 100644 --- a/api/net/http/connection.hpp +++ b/api/net/http/connection.hpp @@ -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"); @@ -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 -#include -#include -#include -#include 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; - 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 - 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(" Created %u -> %s %p\n", local_port(), peer().to_string().c_str(), this); + } + + template + Connection::Connection(TCP& tcp, Peer addr) + : Connection(tcp.connect(addr)) + { + } + } // < namespace http #endif // < HTTP_CONNECTION_HPP diff --git a/diskimagebuild/CMakeLists.txt b/diskimagebuild/CMakeLists.txt index b48ef7e516..45de2934d6 100644 --- a/diskimagebuild/CMakeLists.txt +++ b/diskimagebuild/CMakeLists.txt @@ -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}) diff --git a/diskimagebuild/fat_internal.hpp b/diskimagebuild/fat_internal.hpp new file mode 100644 index 0000000000..4861a7e53d --- /dev/null +++ b/diskimagebuild/fat_internal.hpp @@ -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 + +#pragma once +#ifndef FS_FAT_INTERNAL_HPP +#define FS_FAT_INTERNAL_HPP + +#include + +// 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 diff --git a/diskimagebuild/writer.cpp b/diskimagebuild/writer.cpp index 2d6ace5a9a..f8d10e2b26 100644 --- a/diskimagebuild/writer.cpp +++ b/diskimagebuild/writer.cpp @@ -1,7 +1,7 @@ #include "filetree.hpp" #include "../api/fs/mbr.hpp" -#include "../api/fs/fat_internal.hpp" +#include "fat_internal.hpp" #include #include diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0ce7f05af4..5366e9c7e4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/net/http/client.cpp b/src/net/http/client.cpp index 785a4e3f6d..90b93a008e 100644 --- a/src/net/http/client.cpp +++ b/src/net/http/client.cpp @@ -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"); @@ -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]; @@ -210,17 +210,17 @@ namespace http { } // no non-occupied connections, emplace a new one - cset.push_back(std::make_unique(tcp_.connect(host), Connection::Close_handler{this, &Client::close})); + cset.push_back(std::make_unique(*this, tcp_.connect(host))); return *cset.back(); } - void Client::close(Connection& c) + void Client::close(Client_connection& c) { debug(" 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& conn)->bool + [port = c.local_port()] (const std::unique_ptr& conn)->bool { return conn->local_port() == port; })); diff --git a/src/net/http/connection.cpp b/src/net/http/client_connection.cpp similarity index 76% rename from src/net/http/connection.cpp rename to src/net/http/client_connection.cpp index 61efc9bcdb..de6bb172e4 100644 --- a/src/net/http/connection.cpp +++ b/src/net/http/client_connection.cpp @@ -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 2017 Oslo and Akershus University College of Applied Sciences // and Alfred Bratterud // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,33 +15,25 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include +#include +#include + #include namespace http { - Connection::Connection(TCP_conn_ptr tcpconn, Close_handler on_close) - : tcpconn_{std::move(tcpconn)}, - req_{nullptr}, - res_{nullptr}, - on_close_{std::move(on_close)}, + Client_connection::Client_connection(Client& client, TCP_conn tcpconn) + : Connection{std::move(tcpconn)}, + client_(client), on_response_{nullptr}, - timer_({this, &Connection::timeout_request}), - timeout_dur_{timeout_duration::zero()}, - keep_alive_{true} + timer_({this, &Client_connection::timeout_request}), + timeout_dur_{timeout_duration::zero()} { - debug(" Created %u -> %s %p\n", local_port(), peer().to_string().c_str(), this); // setup close event - tcpconn_->on_close({this, &Connection::close}); - } - template - Connection::Connection(TCP& tcp, Peer addr, Close_handler on_close) - : Connection(tcp.connect(addr), std::move(on_close)) - { + tcpconn_->on_close({this, &Client_connection::close}); } - void Connection::send(Request_ptr req, Response_handler on_res, const size_t bufsize, timeout_duration timeout) + void Client_connection::send(Request_ptr req, Response_handler on_res, const size_t bufsize, timeout_duration timeout) { Expects(available()); req_ = std::move(req); @@ -56,16 +48,16 @@ namespace http { send_request(bufsize); } - void Connection::send_request(const size_t bufsize) + void Client_connection::send_request(const size_t bufsize) { keep_alive_ = (req_->header().value(header::Connection) != "close"); - tcpconn_->on_read(bufsize, {this, &Connection::recv_response}); + tcpconn_->on_read(bufsize, {this, &Client_connection::recv_response}); tcpconn_->write(req_->to_string()); } - void Connection::recv_response(buffer_t buf, size_t len) + void Client_connection::recv_response(buffer_t buf, size_t len) { if(len == 0) { end_response({Error::NO_REPLY}); @@ -138,7 +130,7 @@ namespace http { } } - void Connection::end_response(Error err) + void Client_connection::end_response(Error err) { // move response to a copy in case of callback result in new request Ensures(on_response_); @@ -158,7 +150,7 @@ namespace http { tcpconn_->close(); } - void Connection::close() + void Client_connection::close() { // if the user already if(on_response_ != nullptr) @@ -169,7 +161,7 @@ namespace http { callback(Error::CLOSING, std::move(res_)); } - on_close_(*this); + client_.close(*this); } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c6fccf929d..efc564731f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -119,7 +119,7 @@ set(OS_SOURCES ${SRC}/net/dns/dns.cpp ${SRC}/net/ethernet/ethernet.cpp ${SRC}/net/http/client.cpp - ${SRC}/net/http/connection.cpp + ${SRC}/net/http/client_connection.cpp ${SRC}/net/http/cookie.cpp ${SRC}/net/http/header.cpp ${SRC}/net/http/header_fields.cpp diff --git a/vmbuild/CMakeLists.txt b/vmbuild/CMakeLists.txt index 554b8d2375..0c43ca8f38 100644 --- a/vmbuild/CMakeLists.txt +++ b/vmbuild/CMakeLists.txt @@ -9,7 +9,7 @@ set(ELF_SYMS_SOURCES elf_syms.cpp) set(CMAKE_CXX_FLAGS "-std=c++14 -Wall -Wextra -O3") # TODO: write scripts that automatically find include directories -include_directories(. ./../api ./../mod/GSL/) +include_directories(. ./../mod/GSL/) add_executable(vmbuild ${SOURCES}) add_executable(elf_syms ${ELF_SYMS_SOURCES}) diff --git a/vmbuild/vmbuild.cpp b/vmbuild/vmbuild.cpp index 4453c94f4f..dc21e27aa0 100644 --- a/vmbuild/vmbuild.cpp +++ b/vmbuild/vmbuild.cpp @@ -25,7 +25,7 @@ #include #include -#include +#include "../api/boot/multiboot.h" #include #include "elf.h" #include "elf_binary.hpp"