简体   繁体   中英

Poco::Net::NoMessageException raised few seconds after HTTP/GET

Duplicate from POCO GitHub issue

I'm building a simple web server using Poco with Visual Studio 2015 on Windows 10 .

A Poco::Net::NoMessageException is raised few seconds (approx. 15 secs) after connecting to this server with a web browser. It will happen again after refreshing the page; its doing this after each connection.

I didn't find anything in documentation, forum, stackoverflow. It would be very helpful to have a description in documentation of how/when/why these exceptions coming from deep inside the library are raised.

I also tried (just for fun) a std::cout << req.getURI() << std::endl; in handleRequest(...) and got a Memory Access Violation at address 0x00000000 with no other code in function.

Here is my code. Preprocessor statements are omitted.

MyApp.c

int main(int argc, char ** argv)
{
    myHTTPServer server;
    return server.run(argc, argv);
}

myHTTPRequestHandlerFactory

// Header
class myHTTPRequestHandlerFactory : public HTTPRequestHandlerFactory
{
    public:
        virtual HTTPRequestHandler* createRequestHandler(const HTTPServerRequest&);
};

// Code
HTTPRequestHandler* myHTTPRequestHandlerFactory::createRequestHandler(const HTTPServerRequest& handler) {
    return new myHTTPRequestHandler;
}

myHTTPRequestHandler

// Header
class myHTTPRequestHandler : public HTTPRequestHandler
{
    public:
        virtual void handleRequest(HTTPServerRequest &, HTTPServerResponse &);
};

// Code
void myHTTPRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response) {
    response.setStatus(HTTPResponse::HTTP_OK);
    //response.setContentLength(1024);
    //response.setChunkedTransferEncoding(true);
    std::ostream& out = response.send();
    out << "<html><head></head><body>POCO POCO POCO</body></html>";
    out.flush();
}

myHTTPServer

// Header
class myHTTPServer : public ServerApplication
{
    protected:
        int main(const std::vector<std::string>& v) {
            HTTPServer server(new myHTTPRequestHandlerFactory, ServerSocket(80), new HTTPServerParams);

            server.start();

            std::cout << "Server started" << std::endl;

            waitForTerminationRequest();

            std::cout << "Server stopped" << std::endl;

            server.stop();

            return Application::EXIT_OK;
        }
};

// No code in .c file

Thanks for helping me out

According to this answer , it's a normal POCO behaviour to raise Poco::Net::NoMessageException exceptions for no exceptional reason and you should safely ignore them.

For the Memory Access Violation , it's normal as it's not allowed to call std::cout in HTTPRequestHandler::handleRequest(...) .

I'm facing the same problem, right after changing the way I handle URLs routings.

I think it may be due to the second request send automagically by Chrome : The favicon. Right after receiving HTML content, the browser interrogates the server again to get the fancy tab icon, if any. Make sure you handle such requests gracefully, with a 404 error if needed.

As a general advice, redirect all unknown requests to a handler that sends the relevant HTTP error code, to ensure you reply to all requests.

You can also log all incoming requests (at least in debug, if you don't want to log sensitive info) and check the Developper Tools section of your browser to check which requests are sent. If you are not calling a URL beginning with https:// on a http server (in which case additionnal exchanges may take place), your browser should not send any other request without logging it in the network tab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM