Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

16.5 KB
Raw Blame
#include "network/http/HttpClient.h"

#include "core/logging/Logger.h"

#include <QJsonArray>
#include <QJsonObject>
#include <QStringList>
#include <QTimer>
#include <QUrl>

#include <chrono>

namespace fincept {

namespace {
/// URL with the query string removed, for logging.
///
/// Several callers put a live secret in the query because HttpClient's auth
/// headers are shared/global and cannot be set per request — Opsgenie
/// (`?apiKey=`), Gotify (`?token=`), ntfy (`?auth=`), and any saved data
/// mapping whose template carries `&apikey=`/`&token=`. Logging the raw URL
/// therefore writes those secrets to fincept.log in plaintext at the default
/// log level. The error path in handle_reply() already strips the query for
/// exactly this reason; the request path must do the same. Fixed here, once,
/// rather than at each call site.
///
/// The query is not the only place a secret lives: a Telegram bot token is in
/// the *path* (`/bot<token>/sendMessage`), and Slack/Discord/Teams/Mattermost
/// webhook paths are themselves the credential. So for any host other than the
/// Fincept API host only `scheme://host[:port]/<redacted>` is logged; the path
/// of a Fincept API call is kept (it carries ids, never secrets). User-info
/// (`https://user:pass@host`) is always dropped.
QString http_log_url(const QString& url, const QString& base_url) {
    const QUrl parsed(url);
    const QString host = parsed.host();
    if (host.isEmpty()) {
        // A relative Fincept API path ("/user/login?x=1"); anything else with no
        // host is not a URL we can reason about — never echo it.
        if (!url.startsWith(QLatin1Char('/')))
            return QStringLiteral("<invalid url>");
        const int q = static_cast<int>(url.indexOf(QLatin1Char('?')));
        return q < 0 ? url : url.left(q) + QStringLiteral("?<redacted>");
    }

    const QString base_host = QUrl(base_url).host();
    if (base_host.isEmpty() || host.compare(base_host, Qt::CaseInsensitive) != 0) {
        return parsed.adjusted(QUrl::RemoveUserInfo | QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment)
                   .toString() +
               QStringLiteral("/<redacted>");
    }

    const QUrl stripped = parsed.adjusted(QUrl::RemoveUserInfo | QUrl::RemoveQuery | QUrl::RemoveFragment);
    return parsed.hasQuery() ? stripped.toString() + QStringLiteral("?<redacted>") : stripped.toString();
}

/// True for an absolute http(s) URL (the only absolute form HttpClient sends).
bool http_is_absolute_url(const QString& url) {
    return url.startsWith(QLatin1String("http://"), Qt::CaseInsensitive) ||
           url.startsWith(QLatin1String("https://"), Qt::CaseInsensitive);
}

/// A request is only well-formed if it is an absolute http(s) URL or a Fincept
/// API path ("/user/login"). build_request() resolves a relative path against the
/// Fincept base URL and attaches the user's X-API-Key / X-Session-Token to it, so
/// a scheme-less third-party target ("hooks.slack.com/services/..." typed
/// without "https://") must NOT fall into that branch — it would POST the
/// webhook payload plus the user's Fincept credentials to the Fincept backend.
bool http_is_sendable_url(const QString& url) {
    return http_is_absolute_url(url) || url.startsWith(QLatin1Char('/'));
}

/// Deliver a "bad URL" failure through the same async callback path as a real
/// reply (queued, never re-entrant into the caller). Nothing about the URL is
/// echoed: it may be a webhook secret.
void http_reject_bad_url(const QObject* receiver, HttpClient::JsonCallback callback) {
    LOG_WARN("HTTP", "Rejected request: URL must be absolute http(s):// or an API path starting with '/'");
    QTimer::singleShot(0, receiver, [cb = std::move(callback)]() {
        cb(Result<QJsonDocument>::err("Invalid URL: expected http:// or https:// (or an API path starting with '/')"));
    });
}

/// Error text for a failed response: `"HTTP_<status>: <server message>"`, or a
/// bare `"HTTP_<status>"` when the body carries nothing usable. Keeping the
/// status as a machine-readable prefix lets AuthApi/UserApi keep switching on
/// it (see HttpClient::status_from_error) while the server's own wording still
/// reaches the user.
///
/// Body shapes seen in practice: the Fincept backend sends `{"error": ...}` or
/// `{"message": ...}`; FastAPI sends `{"detail": "..."}` and, for request
/// validation, `{"detail": [{"loc": [...], "msg": "..."}]}`; third-party
/// webhooks send whatever they like.
std::string server_message(const QJsonDocument& doc, int status) {
    const QString prefix = QStringLiteral("HTTP_%1").arg(status);
    if (!doc.isObject())
        return prefix.toStdString();

    const QJsonObject obj = doc.object();
    QString msg;
    for (const auto* key : {"error", "message", "detail"}) {
        const QJsonValue v = obj.value(QLatin1String(key));
        if (!v.isString())
            continue;
        msg = v.toString().trimmed();
        if (!msg.isEmpty())
            break;
    }

    // Pydantic/FastAPI field-level validation errors (typically 422).
    if (msg.isEmpty() && obj.value(QLatin1String("detail")).isArray()) {
        QStringList parts;
        for (const auto& item : obj.value(QLatin1String("detail")).toArray()) {
            const QJsonObject entry = item.toObject();
            const QString m = entry.value(QLatin1String("msg")).toString().trimmed();
            if (m.isEmpty())
                continue;
            // loc is ["body", "field_name"] — take the last meaningful segment.
            QString field;
            const QJsonArray loc = entry.value(QLatin1String("loc")).toArray();
            for (qsizetype i = loc.size() - 1; i >= 0; --i) {
                QString seg = loc.at(i).toString();
                if (seg.isEmpty() || seg == QLatin1String("body"))
                    continue;
                seg[0] = seg[0].toUpper();
                seg.replace('_', ' ');
                field = seg;
                break;
            }
            parts << (field.isEmpty() ? m : field + QStringLiteral(": ") + m);
        }
        msg = parts.join(QStringLiteral("; "));
    }

    if (msg.isEmpty())
        return prefix.toStdString();
    return (prefix + QStringLiteral(": ") + msg).toStdString();
}
} // namespace

int HttpClient::status_from_error(const std::string& error) {
    const QString e = QString::fromStdString(error);
    if (!e.startsWith(QLatin1String("HTTP_")))
        return 0;
    const qsizetype colon = e.indexOf(QLatin1Char(':'), 5);
    const QString digits = (colon < 0) ? e.mid(5) : e.mid(5, colon - 5);
    bool ok = false;
    const int status = digits.toInt(&ok);
    return ok ? status : 0;
}

QString HttpClient::message_from_error(const std::string& error) {
    const QString e = QString::fromStdString(error);
    if (!e.startsWith(QLatin1String("HTTP_")))
        return e; // transport / JSON-parse failures carry their own text
    const qsizetype colon = e.indexOf(QLatin1Char(':'), 5);
    if (colon < 0)
        return {};
    return e.mid(colon + 1).trimmed();
}

HttpClient& HttpClient::instance() {
    static HttpClient s;
    return s;
}

HttpClient::HttpClient() {
    nam_ = new QNetworkAccessManager(this);
    // Qt disables the transfer timeout by default: a half-open TCP connection
    // never emits finished(), so the callback never runs, nothing is logged and
    // there is no cancel path — the only recovery is restarting the app. Every
    // other network site in this codebase sets one (BrokerHttp, NewsService, …).
    nam_->setTransferTimeout(std::chrono::seconds{20});
}

QNetworkRequest HttpClient::build_request(const QString& url, const Headers& extra_headers) const {
    const bool is_relative = !http_is_absolute_url(url);
    const QString full_url = is_relative ? (base_url_ + url) : url;
    QUrl qurl(full_url);
    QNetworkRequest req{qurl};
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    req.setHeader(QNetworkRequest::UserAgentHeader, "FinceptTerminal/4.0");

    // Only attach auth on same-host requests — third-party absolute URLs (Slack/Discord/webhooks)
    // share this singleton and must NOT receive X-API-Key / X-Session-Token.
    const bool same_host = is_relative || [&]() {
        const QUrl base(base_url_);
        return !base.host().isEmpty() && qurl.host().compare(base.host(), Qt::CaseInsensitive) == 0;
    }();

    if (same_host) {
        if (!api_key_.isEmpty()) {
            req.setRawHeader("X-API-Key", api_key_.toUtf8());
        }
        if (!session_token_.isEmpty()) {
            req.setRawHeader("X-Session-Token", session_token_.toUtf8());
        }
    }

    // Applied last so a caller can override any default above for this one
    // request without touching the shared singleton's auth state.
    for (auto it = extra_headers.constBegin(); it != extra_headers.constEnd(); ++it)
        req.setRawHeader(it.key(), it.value());
    return req;
}

void HttpClient::handle_reply(QNetworkReply* reply, JsonCallback callback, const QObject* context) {
    // Connect to caller-provided context so caller destruction auto-disconnects before the lambda fires.
    const QObject* receiver = context ? context : static_cast<const QObject*>(this);
    // Safety net: guarantee the reply is always deleted. If `context` is
    // destroyed before the reply finishes, Qt auto-disconnects the callback
    // lambda below and its reply->deleteLater() would never run → the reply
    // leaks until app exit. deleteLater() is idempotent, so a double-call when
    // the lambda also runs is harmless.
    connect(reply, &QNetworkReply::finished, reply, &QObject::deleteLater);
    connect(reply, &QNetworkReply::finished, receiver, [reply, cb = std::move(callback), base_url = base_url_]() {
        reply->deleteLater();

        QByteArray data = reply->readAll();
        int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

        QJsonParseError parse_err;
        QJsonDocument doc = QJsonDocument::fromJson(data, &parse_err);

        if (reply->error() != QNetworkReply::NoError) {
            // Qt's errorString() for an HTTP failure embeds the full request URL
            // ("Error transferring <url> - server replied: ..."), query and secret
            // path included — scrub it with the same redaction as the URL itself.
            const QString safe_url = http_log_url(reply->url().toString(), base_url);
            QString safe_error = reply->errorString();
            for (const QString& raw : {reply->url().toString(), reply->url().toString(QUrl::FullyEncoded),
                                       reply->request().url().toString(), reply->request().url().toString(QUrl::FullyEncoded)}) {
                if (!raw.isEmpty())
                    safe_error.replace(raw, safe_url);
            }
            LOG_WARN("HTTP", QString("HTTP %1: %2 — %3").arg(status).arg(safe_url).arg(safe_error));

            // 401/403 stay a bare "HTTP_<status>" — session-expiry detection in
            // AuthApi/AuthManager/SessionGuard matches on exactly that string.
            if (status == 401 || status == 403) {
                cb(Result<QJsonDocument>::err(QString("HTTP_%1").arg(status).toStdString()));
                return;
            }

            // No HTTP status at all: a transport failure (DNS, refused, TLS, timeout).
            // Surface Qt's (URL-scrubbed) reason instead of a bare "HTTP_0" that tells
            // the user nothing. status_from_error() still yields 0 for this form.
            if (status == 0) {
                cb(Result<QJsonDocument>::err(QStringLiteral("Network error: %1").arg(safe_error).toStdString()));
                return;
            }

            // Every other HTTP error is an error, including one whose body
            // happens to parse. This used to return ok(doc) so AuthApi could
            // read the server's message off the document — but ~50 other call
            // sites share this client and were told a 400/404/429/500/502/503
            // had succeeded, then parsed the error body as data (DBnomics
            // reported "0 series found" for a 429; MaritimeService cached an
            // empty vessel for 60s). The message is preserved in the error
            // string instead — see server_message() / message_from_error().
            const bool parsed = !data.isEmpty() && parse_err.error == QJsonParseError::NoError;
            cb(Result<QJsonDocument>::err(server_message(parsed ? doc : QJsonDocument{}, status)));
            return;
        }

        if (parse_err.error != QJsonParseError::NoError) {
            cb(Result<QJsonDocument>::err(parse_err.errorString().toStdString()));
            return;
        }
        cb(Result<QJsonDocument>::ok(doc));
    });
}

void HttpClient::get(const QString& url, JsonCallback callback, const QObject* context, const Headers& extra_headers) {
    if (!http_is_sendable_url(url)) {
        http_reject_bad_url(context ? context : this, std::move(callback));
        return;
    }
    LOG_DEBUG("HTTP", "GET " + http_log_url(url, base_url_));
    auto* reply = nam_->get(build_request(url, extra_headers));
    handle_reply(reply, std::move(callback), context);
}

void HttpClient::post(const QString& url, const QJsonObject& body, JsonCallback callback, const QObject* context,
                      const Headers& extra_headers) {
    if (!http_is_sendable_url(url)) {
        http_reject_bad_url(context ? context : this, std::move(callback));
        return;
    }
    LOG_DEBUG("HTTP", "POST " + http_log_url(url, base_url_));
    QJsonDocument doc(body);
    auto* reply = nam_->post(build_request(url, extra_headers), doc.toJson());
    handle_reply(reply, std::move(callback), context);
}

void HttpClient::post_form(const QString& url, const QMap<QString, QString>& fields, JsonCallback callback,
                           const QObject* context, const Headers& extra_headers) {
    if (!http_is_sendable_url(url)) {
        http_reject_bad_url(context ? context : this, std::move(callback));
        return;
    }
    LOG_DEBUG("HTTP", "POST(form) " + http_log_url(url, base_url_));

    // Percent-encode by hand: QUrlQuery leaves '+' alone in a value, and a server
    // decodes a bare '+' as a space — which would turn "+14155238886" into
    // " 14155238886".
    QByteArray encoded;
    for (auto it = fields.constBegin(); it != fields.constEnd(); ++it) {
        if (!encoded.isEmpty())
            encoded += '&';
        encoded += QUrl::toPercentEncoding(it.key()) + '=' + QUrl::toPercentEncoding(it.value());
    }

    QNetworkRequest req = build_request(url, extra_headers);
    if (!extra_headers.contains("Content-Type"))
        req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    auto* reply = nam_->post(req, encoded);
    handle_reply(reply, std::move(callback), context);
}

void HttpClient::put(const QString& url, const QJsonObject& body, JsonCallback callback, const QObject* context,
                     const Headers& extra_headers) {
    if (!http_is_sendable_url(url)) {
        http_reject_bad_url(context ? context : this, std::move(callback));
        return;
    }
    LOG_DEBUG("HTTP", "PUT " + http_log_url(url, base_url_));
    QJsonDocument doc(body);
    auto* reply = nam_->put(build_request(url, extra_headers), doc.toJson());
    handle_reply(reply, std::move(callback), context);
}

void HttpClient::del(const QString& url, JsonCallback callback, const QObject* context, const Headers& extra_headers) {
    if (!http_is_sendable_url(url)) {
        http_reject_bad_url(context ? context : this, std::move(callback));
        return;
    }
    LOG_DEBUG("HTTP", "DELETE " + http_log_url(url, base_url_));
    auto* reply = nam_->deleteResource(build_request(url, extra_headers));
    handle_reply(reply, std::move(callback), context);
}

void HttpClient::del(const QString& url, const QJsonObject& body, JsonCallback callback, const QObject* context,
                     const Headers& extra_headers) {
    if (!http_is_sendable_url(url)) {
        http_reject_bad_url(context ? context : this, std::move(callback));
        return;
    }
    LOG_DEBUG("HTTP", "DELETE " + http_log_url(url, base_url_));
    QJsonDocument doc(body);
    auto* reply =
        nam_->sendCustomRequest(build_request(url, extra_headers), "DELETE", doc.toJson(QJsonDocument::Compact));
    handle_reply(reply, std::move(callback), context);
}

void HttpClient::set_auth_header(const QString& api_key) {
    api_key_ = api_key;
}

void HttpClient::set_session_token(const QString& token) {
    session_token_ = token;
}

void HttpClient::clear_session_token() {
    session_token_.clear();
}

void HttpClient::set_base_url(const QString& base) {
    base_url_ = base;
}

} // namespace fincept