Resolved most warning messages when compiled with -Wextra or -Weverything. Related to #44 and #45

This commit is contained in:
eidheim 2016-05-02 13:12:57 +02:00
commit 79dca558d2
7 changed files with 33 additions and 29 deletions

View file

@ -14,6 +14,8 @@ namespace SimpleWeb {
template <class socket_type> template <class socket_type>
class ClientBase { class ClientBase {
public: public:
virtual ~ClientBase() {}
class Response { class Response {
friend class ClientBase<socket_type>; friend class ClientBase<socket_type>;
@ -43,7 +45,7 @@ namespace SimpleWeb {
private: private:
boost::asio::streambuf content_buffer; 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="", 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="/"; corrected_path="/";
content.seekp(0, std::ios::end); content.seekp(0, std::ios::end);
size_t content_length=content.tellp(); auto content_length=content.tellp();
content.seekp(0, std::ios::beg); content.seekp(0, std::ios::beg);
boost::asio::streambuf write_buffer; boost::asio::streambuf write_buffer;
@ -135,7 +137,7 @@ namespace SimpleWeb {
} }
else { else {
host=host_port.substr(0, host_end); 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); asio_endpoint=boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port);
@ -191,7 +193,7 @@ namespace SimpleWeb {
boost::asio::streambuf streambuf; boost::asio::streambuf streambuf;
std::ostream content(&streambuf); std::ostream content(&streambuf);
size_t length; std::streamsize length;
std::string buffer; std::string buffer;
do { do {
size_t bytes_transferred = boost::asio::read_until(*socket, response->content_buffer, "\r\n"); size_t bytes_transferred = boost::asio::read_until(*socket, response->content_buffer, "\r\n");
@ -199,16 +201,16 @@ namespace SimpleWeb {
getline(response->content, line); getline(response->content, line);
bytes_transferred-=line.size()+1; bytes_transferred-=line.size()+1;
line.pop_back(); 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) { if((2+length)>num_additional_bytes) {
boost::asio::read(*socket, response->content_buffer, boost::asio::read(*socket, response->content_buffer,
boost::asio::transfer_exactly(2+length-num_additional_bytes)); 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); response->content.read(&buffer[0], length);
content.write(&buffer[0], length); content.write(&buffer[0], length);
@ -257,4 +259,4 @@ namespace SimpleWeb {
}; };
} }
#endif /* CLIENT_HTTP_HPP */ #endif /* CLIENT_HTTP_HPP */

View file

@ -28,7 +28,7 @@ namespace SimpleWeb {
asio_context.load_verify_file(verify_file); asio_context.load_verify_file(verify_file);
socket=std::make_shared<HTTPS>(asio_io_service, asio_context); socket=std::make_shared<HTTPS>(asio_io_service, asio_context);
}; }
private: private:
boost::asio::ssl::context asio_context; boost::asio::ssl::context asio_context;
@ -49,4 +49,4 @@ namespace SimpleWeb {
}; };
} }
#endif /* CLIENT_HTTPS_HPP */ #endif /* CLIENT_HTTPS_HPP */

View file

@ -102,7 +102,7 @@ int main() {
if(ifs) { if(ifs) {
ifs.seekg(0, ios::end); ifs.seekg(0, ios::end);
size_t length=ifs.tellg(); auto length=ifs.tellg();
ifs.seekg(0, ios::beg); ifs.seekg(0, ios::beg);
@ -111,14 +111,14 @@ int main() {
//read and send 128 KB at a time //read and send 128 KB at a time
const size_t buffer_size=131072; const size_t buffer_size=131072;
vector<char> buffer(buffer_size); vector<char> buffer(buffer_size);
size_t read_length; streamsize read_length;
try { try {
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) { while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
response.write(&buffer[0], read_length); response.write(&buffer[0], read_length);
response.flush(); response.flush();
} }
} }
catch(const exception &e) { catch(const exception &) {
cerr << "Connection interrupted, closing file" << endl; cerr << "Connection interrupted, closing file" << endl;
} }

View file

@ -102,7 +102,7 @@ int main() {
if(ifs) { if(ifs) {
ifs.seekg(0, ios::end); ifs.seekg(0, ios::end);
size_t length=ifs.tellg(); auto length=ifs.tellg();
ifs.seekg(0, ios::beg); ifs.seekg(0, ios::beg);
@ -111,14 +111,14 @@ int main() {
//read and send 128 KB at a time //read and send 128 KB at a time
const size_t buffer_size=131072; const size_t buffer_size=131072;
vector<char> buffer(buffer_size); vector<char> buffer(buffer_size);
size_t read_length; streamsize read_length;
try { try {
while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) { while((read_length=ifs.read(&buffer[0], buffer_size).gcount())>0) {
response.write(&buffer[0], read_length); response.write(&buffer[0], read_length);
response.flush(); response.flush();
} }
} }
catch(const exception &e) { catch(const exception &) {
cerr << "Connection interrupted, closing file" << endl; cerr << "Connection interrupted, closing file" << endl;
} }

View file

@ -17,6 +17,8 @@ namespace SimpleWeb {
template <class socket_type> template <class socket_type>
class ServerBase { class ServerBase {
public: public:
virtual ~ServerBase() {}
class Response : public std::ostream { class Response : public std::ostream {
friend class ServerBase<socket_type>; friend class ServerBase<socket_type>;
private: private:
@ -101,7 +103,7 @@ namespace SimpleWeb {
remote_endpoint_address=socket.lowest_layer().remote_endpoint().address().to_string(); remote_endpoint_address=socket.lowest_layer().remote_endpoint().address().to_string();
remote_endpoint_port=socket.lowest_layer().remote_endpoint().port(); 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; boost::asio::ip::tcp::acceptor acceptor;
std::vector<std::thread> threads; std::vector<std::thread> threads;
size_t timeout_request; long timeout_request;
size_t timeout_content; 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), config(port, num_threads), acceptor(io_service),
timeout_request(timeout_request), timeout_content(timeout_send_or_receive) {} timeout_request(timeout_request), timeout_content(timeout_send_or_receive) {}
virtual void accept()=0; 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)); 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->expires_from_now(boost::posix_time::seconds(seconds));
timer->async_wait([socket](const boost::system::error_code& ec){ timer->async_wait([socket](const boost::system::error_code& ec){
@ -217,7 +219,7 @@ namespace SimpleWeb {
return timer; 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)); 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->expires_from_now(boost::posix_time::seconds(seconds));
timer->async_wait(request->strand.wrap([socket](const boost::system::error_code& ec){ timer->async_wait(request->strand.wrap([socket](const boost::system::error_code& ec){
@ -266,7 +268,7 @@ namespace SimpleWeb {
try { try {
content_length=stoull(it->second); content_length=stoull(it->second);
} }
catch(const std::exception &e) { catch(const std::exception &) {
return; return;
} }
if(content_length>num_additional_bytes) { if(content_length>num_additional_bytes) {
@ -367,7 +369,7 @@ namespace SimpleWeb {
try { try {
resource_function(response, request); resource_function(response, request);
} }
catch(const std::exception& e) { catch(const std::exception&) {
return; return;
} }
@ -375,7 +377,7 @@ namespace SimpleWeb {
try { try {
response.flush(); response.flush();
} }
catch(const std::exception &e) { catch(const std::exception &) {
return; return;
} }
} }
@ -385,7 +387,7 @@ namespace SimpleWeb {
try { try {
http_version=stof(request->http_version); http_version=stof(request->http_version);
} }
catch(const std::exception &e) { catch(const std::exception &) {
return; return;
} }
@ -408,7 +410,7 @@ namespace SimpleWeb {
template<> template<>
class Server<HTTP> : public ServerBase<HTTP> { class Server<HTTP> : public ServerBase<HTTP> {
public: 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) {} ServerBase<HTTP>::ServerBase(port, num_threads, timeout_request, timeout_content) {}
private: private:

View file

@ -11,7 +11,7 @@ namespace SimpleWeb {
class Server<HTTPS> : public ServerBase<HTTPS> { class Server<HTTPS> : public ServerBase<HTTPS> {
public: public:
Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file, 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()) : const std::string& verify_file=std::string()) :
ServerBase<HTTPS>::ServerBase(port, num_threads, timeout_request, timeout_content), ServerBase<HTTPS>::ServerBase(port, num_threads, timeout_request, timeout_content),
context(boost::asio::ssl::context::sslv23) { context(boost::asio::ssl::context::sslv23) {

View file

@ -98,7 +98,7 @@ public:
} }
}; };
int main(int argc, char** argv) { int main() {
ServerTest serverTest; ServerTest serverTest;
if(!serverTest.parse_request_test()) { if(!serverTest.parse_request_test()) {