79134423

Date: 2024-10-28 17:11:29
Score: 0.5
Natty:
Report link

So for anyone having this problem in the future. Thanks to Barmar I could solve the issue. If you want to read without error I use this

int read() {
    std::vector<uint8_t> vector;
    int temp = 0;
    int read_status = 1;
    
    memset(buffer, 0, sizeof(buffer));
    while (true) {
        read_status = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
        if (read_status == 0 || read_status == -1)
            break;
        for (int i = 0; i < read_status; i++)
            vector.push_back(buffer[i]);
        for (std::vector<uint8_t>::iterator it = vector.begin(); it != vector.end(); ++it)
            request += *it;
        vector.clear();
        if (!body(request, temp, loop, boundary))
            break;
        temp++;
    }
}

bool body(std::string request, int temp, bool &loop, std::string& boundary) {
    bool found = false;

    if (temp == 0) {
        return true;
    }
    std::stringstream stream(request);
    std::string line;
    if (!loop) {
        while (std::getline(stream, line)) {
            if (line.find("boundary=") != std::string::npos) {
                std::size_t pos = line.find("boundary=");
                line.erase(0, pos + 9);
                boundary = "--" + line;
                boundary.erase(boundary.length() - 1, boundary.length());
                if (boundary[boundary.length() + 1] == 10)
                    loop = true;
                stream.clear();
                line.clear();
                break;
            }
        }
    }
    if (request.find(boundary + "--") != std::string::npos) {
        return false;
    }
    if (boundary.length() == 0) {
        if (request.find("\r\n\r\n") != std::string::npos) {
            return false;
        }
    }
    return true;
}

The body function only works with small buffersize. if you want a bigger buffer size, you have to make a check for the first iteration. I know that it is not cleanly written, but its just a prototype.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Bionic Angel