Skip to content

Memory leak in Net/src/HTTPClientSession.cpp #217

@ScottLuo

Description

@ScottLuo

in function

std::ostream& HTTPClientSession::sendRequest(HTTPRequest& request)
{
delete _pResponseStream;
_pResponseStream = 0;

bool keepAlive = getKeepAlive();
if ((connected() && !keepAlive) || mustReconnect())
{
    close();
    _mustReconnect = false;
}
try
{
    if (!connected())
        reconnect();
    if (!keepAlive)
        request.setKeepAlive(false);
    if (!request.has(HTTPRequest::HOST))
        request.setHost(_host, _port);
    if (!_proxyHost.empty())
    {
        request.setURI(proxyRequestPrefix() + request.getURI());
        proxyAuthenticate(request);
    }
    _reconnect = keepAlive;
    _expectResponseBody = request.getMethod() != HTTPRequest::HTTP_HEAD;
// please insert these 3 line to solve this problem
if (_pRequestStream) {
delete _pRequestStream;
}
    if (request.getChunkedTransferEncoding())
    {
        HTTPHeaderOutputStream hos(*this);
        request.write(hos);
        _pRequestStream = new HTTPChunkedOutputStream(*this);
    }
    else if (request.getContentLength() != HTTPMessage::UNKNOWN_CONTENT_LENGTH)
    {
        Poco::CountingOutputStream cs;
        request.write(cs);
        _pRequestStream = new HTTPFixedLengthOutputStream(*this, request.getContentLength() + cs.chars());
        request.write(*_pRequestStream);
    }
    else if (request.getMethod() != HTTPRequest::HTTP_PUT && request.getMethod() != HTTPRequest::HTTP_POST)
    {
        Poco::CountingOutputStream cs;
        request.write(cs);
        _pRequestStream = new HTTPFixedLengthOutputStream(*this, cs.chars());
        request.write(*_pRequestStream);
    }
    else
    {
        _pRequestStream = new HTTPOutputStream(*this);
        request.write(*_pRequestStream);
    }   
    _lastRequest.update();
    return *_pRequestStream;
}
catch (Exception&)
{
    close();
    throw;
}

}

and function

std::istream& HTTPClientSession::receiveResponse(HTTPResponse& response)
{
delete _pRequestStream;
_pRequestStream = 0;

do
{
    response.clear();
    HTTPHeaderInputStream his(*this);
    try
    {
        response.read(his);
    }
    catch (MessageException&)
    {
        close();
        if (networkException())
            networkException()->rethrow();
        else
            throw;
    }
    catch (Exception&)
    {
        close();
        throw;
    }
}
while (response.getStatus() == HTTPResponse::HTTP_CONTINUE);

_mustReconnect = getKeepAlive() && !response.getKeepAlive();
// please insert these 3 line to solve this problem
if (_pResponseStream) {
delete _pResponseStream;
}
if (!_expectResponseBody)
    _pResponseStream = new HTTPFixedLengthInputStream(*this, 0);
else if (response.getChunkedTransferEncoding())
    _pResponseStream = new HTTPChunkedInputStream(*this);
else if (response.getContentLength() != HTTPMessage::UNKNOWN_CONTENT_LENGTH)
    _pResponseStream = new HTTPFixedLengthInputStream(*this, response.getContentLength());
else
    _pResponseStream = new HTTPInputStream(*this);

return *_pResponseStream;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions