<commit_before>/*
 * Copyright (C) 2009 <NAME>.
 *
 * This file is part of cclive.
 * 
 * cclive is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * cclive is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string>
#include <sstream>
#include <climits>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HOST_W32
#include <windows.h>
#define sleep(n) Sleep(n*1000)
#endif

#include "except.h"
#include "quvi.h"
#include "curl.h"
#include "log.h"
#include "opts.h"
#include "retry.h"

RetryMgr::RetryMgr()
    : retries(0), retryUntilRetrievedFlag(false)
{
}

void
RetryMgr::reset() {
    retries = 0;
    retryUntilRetrievedFlag = false;
}

void
RetryMgr::handle(const QuviException& x) {

    logmgr.cerr(x);

    // Pass unless one of the following curl codes:
    switch (x.getCurlCode()) {
    case CURLE_OPERATION_TIMEDOUT: break;
    default: throw x;
    }

    const long httpcode = x.getHttpCode();

    // Pass if this is an HTTP error of range of 400 .. 500.
    if (httpcode >= 400 && httpcode < 500)
        throw x;

    const Options opts =
        optsmgr.getOptions();

    int maxRetry = opts.retry_arg;

    if (retryUntilRetrievedFlag)
        maxRetry = 0; // Override --retry limit for file downloads

    if (++retries <= opts.retry_arg || maxRetry == 0) {
        std::stringstream b;
        b << "retry #" << retries;

        if (maxRetry > 0)
            b << "/" << maxRetry;

        b << " ... wait "
          << opts.retry_wait_arg
          << " second(s).";

        logmgr.cerr(b.str(), false, false, false);
        sleep(opts.retry_wait_arg);
        logmgr.cerr() << std::endl;

        if (retries == INT_MAX-1)
            retries = 0;
    }
    else
        throw x; // Pass the exception, retries count not within our range
}

void
RetryMgr::setRetryUntilRetrievedFlag() {
    retryUntilRetrievedFlag = true;
}

const bool&
RetryMgr::getRetryUntilRetrievedFlag() const {
    return retryUntilRetrievedFlag;
}


<commit_msg>fix CURLE_RANGE_ERROR handling for file retries.<commit_after>/*
 * Copyright (C) 2009 <NAME>.
 *
 * This file is part of cclive.
 * 
 * cclive is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * cclive is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string>
#include <sstream>
#include <climits>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HOST_W32
#include <windows.h>
#define sleep(n) Sleep(n*1000)
#endif

#include "except.h"
#include "quvi.h"
#include "curl.h"
#include "log.h"
#include "opts.h"
#include "retry.h"

RetryMgr::RetryMgr()
    : retries(0), retryUntilRetrievedFlag(false)
{
}

void
RetryMgr::reset() {
    retries = 0;
    retryUntilRetrievedFlag = false;
}

void
RetryMgr::handle(const QuviException& x) {

    logmgr.cerr(x);

    // Pass unless one of the following curl codes:
    switch (x.getCurlCode()) {
    case CURLE_OPERATION_TIMEDOUT: break;
    default: throw x;
    }

    const long httpcode = x.getHttpCode();

    // Pass if this is an HTTP error of range of 400 .. 500.
    if (httpcode >= 400 && httpcode < 500)
        throw x;

    const Options opts =
        optsmgr.getOptions();

    int maxRetry = opts.retry_arg;

    if (retryUntilRetrievedFlag && x.getCurlCode() != CURLE_RANGE_ERROR)
        maxRetry = 0; // Override --retry limit for file downloads

    if (++retries <= opts.retry_arg || maxRetry == 0) {
        std::stringstream b;
        b << "retry #" << retries;

        if (maxRetry > 0)
            b << "/" << maxRetry;

        b << " ... wait "
          << opts.retry_wait_arg
          << " second(s).";

        logmgr.cerr(b.str(), false, false, false);
        sleep(opts.retry_wait_arg);
        logmgr.cerr() << std::endl;

        if (retries == INT_MAX-1)
            retries = 0;
    }
    else
        throw x; // Pass the exception, retries count not within our range
}

void
RetryMgr::setRetryUntilRetrievedFlag() {
    retryUntilRetrievedFlag = true;
}

const bool&
RetryMgr::getRetryUntilRetrievedFlag() const {
    return retryUntilRetrievedFlag;
}



{'fraction_non_alphanumeric': 0.1147764981456178, 'fraction_numerical': 0.011711887565879368, 'mean_word_length': 2.893617021276596, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '600035', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1671, 'n_words': 535}