Commit ded6320e authored by jan.koester's avatar jan.koester
Browse files

urgent

parent 533ba907
Loading
Loading
Loading
Loading
+57 −27
Changes for src/http.cpp: 57 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -1149,11 +1149,29 @@ size_t libhttppp::HttpResponse::parse(const char *data, size_t inlen) {
    HTTPException excep;

    if (inlen < 16) {
        throw excep[HTTPException::Error] << "HttpResponse header too small aborting!";
        excep[HTTPException::Error] << "HttpResponse header too small aborting!";
        throw excep;
    }

    // --- helpers: trim ---
    auto ltrim_inplace = [](std::string& s) {
        size_t i = 0;
        while (i < s.size() && std::isspace(static_cast<unsigned char>(s[i]))) ++i;
        if (i) s.erase(0, i);
    };
    auto rtrim_inplace = [](std::string& s) {
        size_t i = s.size();
        while (i && std::isspace(static_cast<unsigned char>(s[i - 1]))) --i;
        if (i != s.size()) s.erase(i);
    };
    auto trim_inplace = [&](std::string& s) {
        rtrim_inplace(s);
        ltrim_inplace(s);
    };

    // --- 1) find end of header: "\r\n\r\n" ---
    const char* header_end_marker = "\r\n\r\n";
    const char* end_ptr = data + inlen - 4; // Check up to the last 4 bytes
    const char* end_ptr = data + inlen - 4;
    const char* header_end_pos = nullptr;

    for (const char* p = data; p < end_ptr; ++p) {
@@ -1163,51 +1181,55 @@ size_t libhttppp::HttpResponse::parse(const char *data, size_t inlen) {
        }
    }

    if (header_end_pos == nullptr) {
        throw excep[HTTPException::Error] << "HttpHeader end not found!";
    if (!header_end_pos) {
        excep[HTTPException::Error] << "HttpHeader end not found!";
        throw excep;
    }

    size_t helen = header_end_pos - data;
    size_t bytes_consumed = helen + 4;
    size_t helen = static_cast<size_t>(header_end_pos - data);
    size_t bytes_consumed = helen + 4; // include "\r\n\r\n"

    std::string header_str(data, helen);
    size_t line_end = header_str.find("\r\n");

    // --- 2) parse status line ---
    size_t line_end = header_str.find("\r\n");
    if (line_end == std::string::npos) {
        throw excep[HTTPException::Error] << "Can't find end of Status Line!";
        excep[HTTPException::Error] << "Can't find end of Status Line!";
        throw excep;
    }

    std::string status_line = header_str.substr(0, line_end);

    size_t v_start = 0;
    size_t v_end = status_line.find(' ', v_start);

    if (v_end == std::string::npos || status_line.substr(0, 5) != "HTTP/") {
        throw excep[HTTPException::Error] << "HttpResponse no Version Tag found or malformed Status Line!";
        excep[HTTPException::Error] << "HttpResponse no Version Tag found or malformed Status Line!";
        throw excep;
    }

    _Version = status_line.substr(v_start, v_end - v_start);

    // status code
    v_start = v_end + 1;
    v_end = status_line.find(' ', v_start);

    if (v_end == std::string::npos) {
        v_end = status_line.length();
    }

    std::string status_code_str = status_line.substr(v_start, v_end - v_start);
    // you might want to convert this to int, but I'm leaving your original logic

    // reason phrase
    v_start = v_end + 1;
    std::string reason_phrase;
    if (v_start < status_line.length()) {
        reason_phrase = status_line.substr(v_start);
    } else {
        reason_phrase = ""; // Empty Reason Phrase
        reason_phrase.clear();
    }

    _State = reason_phrase;

    size_t cursor = line_end + 2; // Start after Status Line's \r\n
    // --- 3) parse headers ---
    size_t cursor = line_end + 2; // after "\r\n"

    while (cursor < helen) {
        size_t next_line = header_str.find("\r\n", cursor);
@@ -1218,44 +1240,52 @@ size_t libhttppp::HttpResponse::parse(const char *data, size_t inlen) {
        std::string hline = header_str.substr(cursor, next_line - cursor);
        cursor = next_line + 2;

        if (hline.empty()) continue;
        if (hline.empty())
            continue;

        size_t colon = hline.find(':');
        if (colon == std::string::npos) {
            // malformed; skip
            continue;
        }

        std::string key   = hline.substr(0, colon);
        std::string value = (colon + 1 < hline.size()) ? hline.substr(colon + 1) : "";

        auto ltrim_inplace = [](std::string& s) { /* ... defined previously ... */ };
        auto rtrim_inplace = [](std::string& s) { /* ... defined previously ... */ };
        auto trim_inplace = [&](std::string& s) { rtrim_inplace(s); ltrim_inplace(s); };
        std::string value = (colon + 1 < hline.size()) ? hline.substr(colon + 1) : std::string();

        trim_inplace(key);
        trim_inplace(value);

        std::transform(key.begin(), key.end(), key.begin(),
                       [](unsigned char c){ return static_cast<char>(std::tolower(c)); });
        // lowercase key
        for (size_t i = 0; i < key.size(); ++i) {
            key[i] = static_cast<char>(std::tolower(static_cast<unsigned char>(key[i])));
        }

        HeaderData* ncontent = setHeaderData(key.c_str());
        if (!ncontent)
            continue;

        // Split value on commas (HTTP list headers: TE, Accept, etc.)
        size_t oldiv = 0;
        size_t iv = 0;

        while ((iv = value.find(',', oldiv)) != std::string::npos) {
            std::string token = value.substr(oldiv, iv - oldiv);
            trim_inplace(token); // Trim token
            trim_inplace(token);
            if (!token.empty()) {
                ncontent->push_back(token);
            oldiv = iv + 1;
            if (oldiv < value.length() && value[oldiv] == ' ') {
                oldiv++;
            }
            oldiv = iv + 1;
        }

        // last (or only) token
        std::string final_token = value.substr(oldiv);
        trim_inplace(final_token);
        if (!final_token.empty()) {
            ncontent->push_back(final_token);
        }
    }

    // --- 4) populate cached header pointers ---
    _ContentLength    = getHeaderData("content-length");
    _Connection       = getHeaderData("connection");
    _ContentType      = getHeaderData("content-type");