This commit is contained in:
parent
131cd37328
commit
79dca558d2
7 changed files with 33 additions and 29 deletions
|
|
@ -14,6 +14,8 @@ namespace SimpleWeb {
|
|||
template <class socket_type>
|
||||
class ClientBase {
|
||||
public:
|
||||
virtual ~ClientBase() {}
|
||||
|
||||
class Response {
|
||||
friend class ClientBase<socket_type>;
|
||||
|
||||
|
|
@ -43,7 +45,7 @@ namespace SimpleWeb {
|
|||
private:
|
||||
boost::asio::streambuf content_buffer;
|
||||
|
||||
Response(): content(&content_buffer) {};
|
||||
Response(): content(&content_buffer) {}
|
||||
};
|
||||
|
||||
std::shared_ptr<Response> request(const std::string& request_type, const std::string& path="/", boost::string_ref content="",
|
||||
|
|
@ -86,7 +88,7 @@ namespace SimpleWeb {
|
|||
corrected_path="/";
|
||||
|
||||
content.seekp(0, std::ios::end);
|
||||
size_t content_length=content.tellp();
|
||||
auto content_length=content.tellp();
|
||||
content.seekp(0, std::ios::beg);
|
||||
|
||||
boost::asio::streambuf write_buffer;
|
||||
|
|
@ -135,7 +137,7 @@ namespace SimpleWeb {
|
|||
}
|
||||
else {
|
||||
host=host_port.substr(0, host_end);
|
||||
port=(unsigned short)stoul(host_port.substr(host_end+1));
|
||||
port=static_cast<unsigned short>(stoul(host_port.substr(host_end+1)));
|
||||
}
|
||||
|
||||
asio_endpoint=boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port);
|
||||
|
|
@ -191,7 +193,7 @@ namespace SimpleWeb {
|
|||
boost::asio::streambuf streambuf;
|
||||
std::ostream content(&streambuf);
|
||||
|
||||
size_t length;
|
||||
std::streamsize length;
|
||||
std::string buffer;
|
||||
do {
|
||||
size_t bytes_transferred = boost::asio::read_until(*socket, response->content_buffer, "\r\n");
|
||||
|
|
@ -199,16 +201,16 @@ namespace SimpleWeb {
|
|||
getline(response->content, line);
|
||||
bytes_transferred-=line.size()+1;
|
||||
line.pop_back();
|
||||
length=stoull(line, 0, 16);
|
||||
length=stol(line, 0, 16);
|
||||
|
||||
size_t num_additional_bytes=response->content_buffer.size()-bytes_transferred;
|
||||
auto num_additional_bytes=static_cast<std::streamsize>(response->content_buffer.size()-bytes_transferred);
|
||||
|
||||
if((2+length)>num_additional_bytes) {
|
||||
boost::asio::read(*socket, response->content_buffer,
|
||||
boost::asio::transfer_exactly(2+length-num_additional_bytes));
|
||||
}
|
||||
|
||||
buffer.resize(length);
|
||||
buffer.resize(static_cast<size_t>(length));
|
||||
response->content.read(&buffer[0], length);
|
||||
content.write(&buffer[0], length);
|
||||
|
||||
|
|
@ -257,4 +259,4 @@ namespace SimpleWeb {
|
|||
};
|
||||
}
|
||||
|
||||
#endif /* CLIENT_HTTP_HPP */
|
||||
#endif /* CLIENT_HTTP_HPP */
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace SimpleWeb {
|
|||
asio_context.load_verify_file(verify_file);
|
||||
|
||||
socket=std::make_shared<HTTPS>(asio_io_service, asio_context);
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
boost::asio::ssl::context asio_context;
|
||||
|
|
@ -49,4 +49,4 @@ namespace SimpleWeb {
|
|||
};
|
||||
}
|
||||
|
||||
#endif /* CLIENT_HTTPS_HPP */
|
||||
#endif /* CLIENT_HTTPS_HPP */
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ int main() {
|
|||
|
||||
if(ifs) {
|
||||
ifs.seekg(0, ios::end);
|
||||
size_t length=ifs.tellg();
|
||||
auto length=ifs.tellg();
|
||||
|
||||
ifs.seekg(0, ios::beg);
|
||||
|
||||
|
|
@ -111,14 +111,14 @@ int main() {
|
|||
//read and send 128 KB at a time
|
||||
const size_t buffer_size=131072;
|
||||
vector<char> buffer(buffer_size);
|
||||
size_t read_length;
|
||||
streamsize read_length;
|
||||
try {
|
||||
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
|
||||
response.write(&buffer[0], read_length);
|
||||
response.flush();
|
||||
}
|
||||
}
|
||||
catch(const exception &e) {
|
||||
catch(const exception &) {
|
||||
cerr << "Connection interrupted, closing file" << endl;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ int main() {
|
|||
|
||||
if(ifs) {
|
||||
ifs.seekg(0, ios::end);
|
||||
size_t length=ifs.tellg();
|
||||
auto length=ifs.tellg();
|
||||
|
||||
ifs.seekg(0, ios::beg);
|
||||
|
||||
|
|
@ -111,14 +111,14 @@ int main() {
|
|||
//read and send 128 KB at a time
|
||||
const size_t buffer_size=131072;
|
||||
vector<char> buffer(buffer_size);
|
||||
size_t read_length;
|
||||
streamsize read_length;
|
||||
try {
|
||||
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
|
||||
response.write(&buffer[0], read_length);
|
||||
response.flush();
|
||||
}
|
||||
}
|
||||
catch(const exception &e) {
|
||||
catch(const exception &) {
|
||||
cerr << "Connection interrupted, closing file" << endl;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ namespace SimpleWeb {
|
|||
template <class socket_type>
|
||||
class ServerBase {
|
||||
public:
|
||||
virtual ~ServerBase() {}
|
||||
|
||||
class Response : public std::ostream {
|
||||
friend class ServerBase<socket_type>;
|
||||
private:
|
||||
|
|
@ -101,7 +103,7 @@ namespace SimpleWeb {
|
|||
remote_endpoint_address=socket.lowest_layer().remote_endpoint().address().to_string();
|
||||
remote_endpoint_port=socket.lowest_layer().remote_endpoint().port();
|
||||
}
|
||||
catch(const std::exception& e) {}
|
||||
catch(const std::exception&) {}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -195,16 +197,16 @@ namespace SimpleWeb {
|
|||
boost::asio::ip::tcp::acceptor acceptor;
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
size_t timeout_request;
|
||||
size_t timeout_content;
|
||||
long timeout_request;
|
||||
long timeout_content;
|
||||
|
||||
ServerBase(unsigned short port, size_t num_threads, size_t timeout_request, size_t timeout_send_or_receive) :
|
||||
ServerBase(unsigned short port, size_t num_threads, long timeout_request, long timeout_send_or_receive) :
|
||||
config(port, num_threads), acceptor(io_service),
|
||||
timeout_request(timeout_request), timeout_content(timeout_send_or_receive) {}
|
||||
|
||||
virtual void accept()=0;
|
||||
|
||||
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, size_t seconds) {
|
||||
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, long seconds) {
|
||||
std::shared_ptr<boost::asio::deadline_timer> timer(new boost::asio::deadline_timer(io_service));
|
||||
timer->expires_from_now(boost::posix_time::seconds(seconds));
|
||||
timer->async_wait([socket](const boost::system::error_code& ec){
|
||||
|
|
@ -217,7 +219,7 @@ namespace SimpleWeb {
|
|||
return timer;
|
||||
}
|
||||
|
||||
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request, size_t seconds) {
|
||||
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request, long seconds) {
|
||||
std::shared_ptr<boost::asio::deadline_timer> timer(new boost::asio::deadline_timer(io_service));
|
||||
timer->expires_from_now(boost::posix_time::seconds(seconds));
|
||||
timer->async_wait(request->strand.wrap([socket](const boost::system::error_code& ec){
|
||||
|
|
@ -266,7 +268,7 @@ namespace SimpleWeb {
|
|||
try {
|
||||
content_length=stoull(it->second);
|
||||
}
|
||||
catch(const std::exception &e) {
|
||||
catch(const std::exception &) {
|
||||
return;
|
||||
}
|
||||
if(content_length>num_additional_bytes) {
|
||||
|
|
@ -367,7 +369,7 @@ namespace SimpleWeb {
|
|||
try {
|
||||
resource_function(response, request);
|
||||
}
|
||||
catch(const std::exception& e) {
|
||||
catch(const std::exception&) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -375,7 +377,7 @@ namespace SimpleWeb {
|
|||
try {
|
||||
response.flush();
|
||||
}
|
||||
catch(const std::exception &e) {
|
||||
catch(const std::exception &) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
@ -385,7 +387,7 @@ namespace SimpleWeb {
|
|||
try {
|
||||
http_version=stof(request->http_version);
|
||||
}
|
||||
catch(const std::exception &e) {
|
||||
catch(const std::exception &) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -408,7 +410,7 @@ namespace SimpleWeb {
|
|||
template<>
|
||||
class Server<HTTP> : public ServerBase<HTTP> {
|
||||
public:
|
||||
Server(unsigned short port, size_t num_threads=1, size_t timeout_request=5, size_t timeout_content=300) :
|
||||
Server(unsigned short port, size_t num_threads=1, long timeout_request=5, long timeout_content=300) :
|
||||
ServerBase<HTTP>::ServerBase(port, num_threads, timeout_request, timeout_content) {}
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ namespace SimpleWeb {
|
|||
class Server<HTTPS> : public ServerBase<HTTPS> {
|
||||
public:
|
||||
Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file,
|
||||
size_t timeout_request=5, size_t timeout_content=300,
|
||||
long timeout_request=5, long timeout_content=300,
|
||||
const std::string& verify_file=std::string()) :
|
||||
ServerBase<HTTPS>::ServerBase(port, num_threads, timeout_request, timeout_content),
|
||||
context(boost::asio::ssl::context::sslv23) {
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int main() {
|
||||
ServerTest serverTest;
|
||||
|
||||
if(!serverTest.parse_request_test()) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue