eidheim / Simple-Web-Server

A very simple, fast, multithreaded, platform independent HTTP and HTTPS server and client library implemented using C++11 and Boost.Asio. Created to be an easy way to make REST resources available from C++ applications.
MIT License
2.61k stars 751 forks source link

server_http.hpp fails to complie on g++ 7.1 #143

Closed sergeken closed 7 years ago

sergeken commented 7 years ago

compiling server_http.hpp with g++ 7.1 gives the following error messages: ../../Prometheus/src/web_services/server_http.hpp:278:44: error: uninitialized variable ‘content_length’ in ‘constexpr’ function unsigned long long content_length; ^~~~~~

Changing the try-catch block as following fixes the issue.

                if(it!=request->header.end()) {
                    try {
                        unsigned long long content_length = stoull(it->second);
                        if(content_length>num_additional_bytes) {
                            //Set timeout on the following boost::asio::async-read or write function
                            auto timer=this->get_timeout_timer(socket, config.timeout_content);
                            boost::asio::async_read(*socket, request->streambuf,
                                    boost::asio::transfer_exactly(content_length-num_additional_bytes),
                                    [this, socket, request, timer]
                                    (const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
                                if(timer)
                                    timer->cancel();
                                if(!ec)
                                    this->find_resource(socket, request);
                                else if(on_error)
                                    on_error(request, ec);
                            });
                        }
                        else
                            this->find_resource(socket, request);
                    }
                    catch(const std::exception &e) {
                        if(on_error)
                            on_error(request, boost::system::error_code(boost::system::errc::protocol_error, boost::system::generic_category()));
                        return;
                    }
                }
eidheim commented 7 years ago

Thank you. I fixed it in different manner, but your suggestion was just as good. It's just that I prefer to see what can throw in the future.

sergeken commented 7 years ago

This is indeed an alternative approach and my first "version". However, I'm more in favor of late declaration and having variables with limited scope. I was even tempted to write

auto const content_length = stoull(it->second);

Which is very much favored by Herb Sutter and others and my "new" style.

eidheim commented 7 years ago

In general, I agree with you. But it is a help for future me that might wonder why this try block is here. Especially when working with Asio and all its nuts and bolts:)