bakercp / ofxHTTP

A suite of HTTP tools, including clients and servers for openFrameworks.
MIT License
109 stars 40 forks source link

set GET request timeout #36

Closed jmarsico closed 8 years ago

jmarsico commented 8 years ago

I'm sure there's a way to set a timeout for a GET request... here's what i'm trying, but it's not working out. Any hints? is this possible?

    ofx::HTTP::GetRequest request(url.str(), Poco::Net::HTTPMessage::HTTP_1_1);
    ofx::HTTP::BaseResponse response;

    Poco::Timespan ts(5L, 0L);
    context.getClientSession()->setKeepAlive(true);
    context.getClientSession()->setKeepAliveTimeout(ts);

    try
    {
        // Execute the request and get the response stream.
        std::istream& responseStream = client.execute(request,
                                                      response,
                                                      context);       `
bakercp commented 8 years ago

The keep-alive settings you are messing with are for managing persistent connections (https://en.wikipedia.org/wiki/HTTP_persistent_connection) -- and what I think you are looking for is a socket timeout -- is that correct? Basically, you want the tell the client how long to wait for a response from a slow server?

jmarsico commented 8 years ago

Yeah, it's the socket timeout that I'm looking for. Wasn't sure how to access the socket.

On Monday, January 25, 2016, Christopher Baker notifications@github.com wrote:

The keep-alive settings you are messing with are for managing persistent connections (https://en.wikipedia.org/wiki/HTTP_persistent_connection) -- and what I think you are looking for is a socket timeout -- is that correct? Basically, you want the tell the client how long to wait for a response from a slow server?

— Reply to this email directly or view it on GitHub https://github.com/bakercp/ofxHTTP/issues/36#issuecomment-174595077.

jmarsico.com

bakercp commented 8 years ago

Well, the real answer is I don't have an easy way to set parameters at the socket layer in the settings, though they could be added. I believe the easiest way to do it would be to intercept it here:

std::ostream& BaseClient::send(BaseRequest& request, Context& context)
{
    Context::ClientSession clientSession = context.getClientSession();

    if (clientSession == nullptr)
    {
        throw Poco::Exception("No session available for request.");
    }

    std::ostream& rawRequestStream = clientSession->sendRequest(request);

// AFTER SENDING THE REQUEST< SET THE RECEIVE TIMEOUT
    clientSession->socket().setReceiveTimeout(Poco::Timespan(5,0));

    _pClientProgressRequestStream = std::make_shared<ClientProgressRequestStream>(rawRequestStream,
                                                                                  request,
                                                                                  context,
                                                                                  *this,
                                                                                  _bytesPerProgressUpdate);

    if (_pRequestStreamFilter)
    {
        return _pRequestStreamFilter->requestStreamFilter(*_pClientProgressRequestStream,
                                                          request,
                                                          context);
    }
    else
...

Ideally those settings should be stored in our Context variable and passed in after the send request ... it's just setup to do that at the moment. Feel free to implement it if you think it'd be helpful!

bakercp commented 8 years ago

That said ... that still may not be early enough -- I can't recall if it needs to be set during Socket creation, in which case you'd need to create a socket connection and pass it into the clientSession constructor ... anyway, give the above trick a whirl and we'll go from there ...

jmarsico commented 8 years ago

that makes sense and is consistent with this stackoverflow comment. But it is still just hanging forever for me. I'll dig a bit more, but will probably just end up trying to test connection with ping before making request. Thanks for the help.

bakercp commented 8 years ago

Interesting. Do you have a server / URL you're trying to test it with? If you send it along, I can mess with it. If it's private, you can send it to me offline.

jmarsico commented 8 years ago

it's just a server on a local network, with an ip of 192.168.0.10. I'm trying to test this at home, away from the server. Might the setReceiveTimeout() not work if there is no path to the server?

bakercp commented 8 years ago

Did you get this resolved?

jmarsico commented 8 years ago

i didn't. sorry for not updating. I ended up going a different direction.