Arduino Giga r1 WiFiServer/WiFiClient stuck after multiple fast reloads

OK so I am trying to build a simple webserver for the arduino giga r1, since I cant find a webserver that has the request GET, POST and DELETE already made. the problem is that when I visit the site after the arduino is launched, that everything works perfectly fine. also if I reload fast like 1 times per second it is ok. but when I reload to often at that speed the arduino gets stuck. It doesnt crash, it just refuses to display the page then again, but when I input a diffrent URI, the diffrent URI gets loaded and displayed, but when I go back the original page which didnt respond also now doesnt respond.

Thats the code. The serial.println() are for displaying where it gets stuck:

#include <WiFi.h>
#include <vector>
#include <iostream>
#include <string>
#include <Request.hpp>
#include <DigitalOut.h>
#include <FATFileSystem.h>
#include <Arduino_USBHostMbed5.h>
#include <sstream>

char ssid[] = "Arduino Wifi";
char pass[] = "passwort";

int status = WL_IDLE_STATUS;
std::vector<String> con_head, con_body;

WiFiServer server(80);
USBHostMSD msd;
mbed::FATFileSystem usb("usb");
Request rq;
bool connected = false;
bool found = false;


// ------------------------ Helper functions  ------------------------ \\'

String get_status_over_code(int status_code);

String content_over_code(int status_code) {
  switch (status_code) {
    case 301:
      return "Moved Permanently:The requested resource has been permanently moved to the URL in the Location header";
    case 400:
      return "Bad Request:The server would not process the request due to something the server considered to be a client error";
    case 404:
      return "Not Found:The server cannot find the requested resource";
    case 406:
      return "Not Acceptable:The server could not produce a response matching the list of acceptable values defined in the request's proactive content negotiation headers and that the server was unwilling to supply a default representation";
    default:
      break;
  }
  return "";
}

String error_page_content(int status_code) {
  std::string result = content_over_code(status_code).c_str();
  String title, description;
  int index = 0;

  if ((index = result.find_first_of(":")) > 1) {
    title = result.c_str();
    description = result.c_str();
    title.remove(index, title.length());
    description.remove(0, index + 1);
  } else {
    return "";
  }
  String file;
  file += "<!DOCTYPE html>";
  file += "\n";
  file += "<html lang=\"en\">";
  file += "\n";
  file += "<head>";
  file += "\n";
  file += "<meta charset=\"UTF-8\">";
  file += "\n";
  file += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">";
  file += "\n";
  file += "<title>Error ";
  file += status_code;
  file += "</title>";
  file += "\n";
  file += "</head>";
  file += "\n";
  file += "\n";
  file += "<body>";
  file += "\n";
  file += "    <h1>" + title + "</h1>";
  file += "\n";
  file += "    <h3>" + description + "</h3>";
  file += "\n";
  file += "    <hr>";
  file += "\n";
  file += "    <h3><i>Webserv (Arduino os) Server at 192.168.3.1 Port 80</i></h3>";
  file += "\n";
  file += "</body>";
  file += "\n";
  file += "</html>";
  file += "\n";
  return file;
}

String error_page(int status_code) {
  String status_code2;
  String returned;
  String response;

  if ((returned = error_page_content(status_code)) == "")
    return "";
  if ((status_code2 = get_status_over_code(status_code)) == "")
    return "";
  response += "HTTP/1.1 ";
  response += String(status_code);
  response += " " + status_code2;
  response += "\r\n";
  response += "Content-Type: text/html\r\n";
  response += "Content-Length: " + returned.length();
  response += "\r\n\r\n";
  response += returned;
  return response;
}

// Umwandeln ohne derzeitigen for loop
String get_file_content_type(String uri, bool head) {
	size_t pos2 = 0;
	size_t pos = 0;
	bool found = false;
  int size = con_head.size();
  int size2 = con_body.size();

	if (head) {
  Serial.println("AA");
		std::string extension;
		for (int tmp = 0; tmp < size; tmp++) {
			if (uri.indexOf(con_head[tmp]) > -1) {
				found = true;
				extension = con_head[tmp].c_str();
				break;
			}
			pos++;
		}
		if (!found)
			return "";
    for (int tmp = 0; tmp < size; tmp++) {
      if (pos == pos2)
        return con_body[tmp].c_str();
      pos2++;
    }
  Serial.println("AB");
	} else {
  Serial.println("AC");
		std::string extension2;
    for (int tmp = 0; tmp < size; tmp++) {
			if (uri.indexOf(con_body[tmp]) > -1) {
				found = true;
				extension2 = con_body[tmp].c_str();
				break;
			}
			pos++;
		}
		if (!found)
			return "";
		for (int tmp = 0; tmp < size; tmp++) {
      if (pos == pos2)
        return con_head[tmp].c_str();
      pos2++;
    }
  Serial.println("AD");
	}
	return "";
}

void get_file_header_type() {
	con_head.push_back(".html");
	con_head.push_back(".htm");
	con_head.push_back(".py");
	con_head.push_back(".sh");
	con_body.push_back("text/html");
	con_body.push_back("text/html");
	con_body.push_back("text/x-python");
	con_body.push_back("text/html");
	con_head.push_back(".css");
	con_body.push_back("text/css");
	con_head.push_back(".csv");
	con_body.push_back("text/csv");
	con_head.push_back(".js");
	con_head.push_back(".mjs");
	con_body.push_back("application/x-javascript");
	con_body.push_back("text/javascript");
	con_head.push_back(".ics");
	con_body.push_back("text/calendar");
	con_head.push_back(".txt");
	con_body.push_back("text/plain");
	con_head.push_back(".xml");
	con_body.push_back("text/xml");
	con_head.push_back(".ico");
	con_body.push_back("image/x-icon");
	con_head.push_back(".png");
	con_body.push_back("image/png");
	con_head.push_back(".jpg");
	con_head.push_back(".jpeg");
	con_body.push_back("image/jpg");
	con_body.push_back("image/jpg");
	con_head.push_back(".gif");
	con_body.push_back("image/gif");
	con_head.push_back(".apng");
	con_body.push_back("image/apng");
	con_head.push_back(".avif");
	con_body.push_back("image/avif");
	con_head.push_back(".bmp");
	con_body.push_back("image/bmp");
	con_head.push_back(".svg");
	con_body.push_back("image/svg+xml");
	con_head.push_back(".tif");
	con_head.push_back(".tiff");
	con_body.push_back("image/tiff");
	con_body.push_back("image/tiff");
	con_head.push_back(".webp");
	con_body.push_back("image/webp");
	con_head.push_back(".aac");
	con_body.push_back("audio/aac");
	con_head.push_back(".oga");
	con_head.push_back(".opus");
	con_body.push_back("audio/ogg");
	con_body.push_back("audio/ogg");
	con_head.push_back(".wav");
	con_body.push_back("audio/wav");
	con_head.push_back(".mid");
	con_head.push_back(".midi");
	con_body.push_back("audio/x-midi");
	con_body.push_back("audio/x-midi");
	con_head.push_back(".mp3");
	con_body.push_back("audio/mpeg");
	con_head.push_back(".weba");
	con_body.push_back("audio/webm");
	con_head.push_back(".avi");
	con_body.push_back("video/x-msvideo");
	con_head.push_back(".mp4");
	con_body.push_back("video/mp4");
	con_head.push_back(".mpeg");
	con_body.push_back("video/mpeg");
	con_head.push_back(".ogv");
	con_body.push_back("video/ogg");
	con_head.push_back(".ts");
	con_body.push_back("video/mp2t");
	con_head.push_back(".webm");
	con_body.push_back("video/webm");
	con_head.push_back(".abw");
	con_body.push_back("application/x-abiword");
	con_head.push_back(".arc");
	con_body.push_back("application/x-freearc");
	con_head.push_back(".azw");
	con_body.push_back("application/vnd.amazon.ebook");
	con_head.push_back(".bin");
	con_body.push_back("application/octet-stream");
	con_head.push_back(".bz2");
	con_body.push_back("application/x-bzip2");
	con_head.push_back(".bz");
	con_body.push_back("application/x-bzip");
	con_head.push_back(".cda");
	con_body.push_back("application/x-cdf");
	con_head.push_back(".csh");
	con_body.push_back("application/x-csh");
	con_head.push_back(".docx");
	con_body.push_back("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
	con_head.push_back(".doc");
	con_body.push_back("application/msword");
	con_head.push_back(".eot");
	con_body.push_back("application/vnd.ms-fontobject");
	con_head.push_back(".epub");
	con_body.push_back("application/equb+zip");
	con_head.push_back(".gz");
	con_body.push_back("application/gzip");
	con_head.push_back(".jar");
	con_body.push_back("application/java-archive");
	con_head.push_back(".jsonld");
	con_body.push_back("application/ld+json");
	con_head.push_back(".json");
	con_body.push_back("application/json");
	con_head.push_back(".mpkg");
	con_body.push_back("application/vnd.apple.installer+xml");
	con_head.push_back(".odp");
	con_body.push_back("application/vnd.oasis.opendocument.presentation");
	con_head.push_back(".ods");
	con_body.push_back("application/vnd.oasis.opendocument.spreadsheet");
	con_head.push_back(".odt");
	con_body.push_back("application/vnd.oasis.opendocument.text");
	con_head.push_back(".ogx");
	con_body.push_back("application/ogg");
	con_head.push_back(".pdf");
	con_body.push_back("application/pdf");
	con_head.push_back(".php");
	con_body.push_back("application/x-http-php");
	con_head.push_back(".pptx");
	con_body.push_back("application/vnd.openxmlformats-officedocument.presentationml.presentation");
	con_head.push_back(".ppt");
	con_body.push_back("application/vnd.ms-powerpoint");
	con_head.push_back(".rar");
	con_body.push_back("application/vnd.rar");
	con_head.push_back(".rtf");
	con_body.push_back("application/rtf");
	con_head.push_back(".sh");
	con_body.push_back("application/x-sh");
	con_head.push_back(".tar");
	con_body.push_back("application/x-tar");
	con_head.push_back(".vsd");
	con_body.push_back("application/vnd.visio");
	con_head.push_back(".xhtml");
	con_body.push_back("application/xhtml+xml");
	con_head.push_back(".xlsx");
	con_body.push_back("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
	con_head.push_back(".xls");
	con_body.push_back("application/vnd.ms-excel");
	con_head.push_back(".xml");
	con_body.push_back("application/xml");
	con_head.push_back(".xul");
	con_body.push_back("application/vnd.mozilla.xul+xml");
	con_head.push_back(".zip");
	con_body.push_back("application/zip");
	con_head.push_back(".cpp");
	con_body.push_back("text/x-c++src");
	con_head.push_back(".hpp");
	con_body.push_back("text/x-c++hdr");
	con_head.push_back(".c");
	con_body.push_back("text/x-csrc");
	con_head.push_back(".h");
	con_body.push_back("text/x-chdr");
	con_head.push_back(".tpp");
	con_body.push_back("application/octet-stream");
}

String get_status_over_code(int status_code) {
  Serial.println("BA");
  Serial.println(status_code);
  switch (status_code) {
    case 200:
      Serial.println("BB");
      return "OK";
    case 301:
      Serial.println("BC");
      return "Permanently moved";
    case 404:
      Serial.println("BC");
      return "Not Found";
    case 406:
      Serial.println("BD");
      return "Not Acceptable";
    default:
      break;
  }
  Serial.println("BE");
  return "";
}

String read_file(String file) {
  Serial.println("CA");
  String returning;
  char line[512];
  Serial.println(file);
  FILE *f = fopen(("/usb/" + file).c_str(), "rb+");

  if (!f) {
    Serial.println("CB");
    return "";
  }
  while (fgets(line, sizeof(line), f) != nullptr)
    returning += line;
  Serial.println("CC");
  return returning;
}

String get_page(String type, String filepath, int status_code = 200) {
  Serial.println("DA");
  String status_code_str = String(status_code);
  String file_body = read_file(filepath);
  String response;

  if (file_body == "") {
    Serial.println("DB");
    return "";
  }
  String status_code2 = get_status_over_code(status_code);
  Serial.println(status_code2);
  if (status_code2 == "") {
    Serial.println("DC");
    return "";
  }

  //rewrite without std::ostringstream
  Serial.println("G");
  response = "HTTP/1.1 ";
  response += status_code;
  response += " ";
  response += status_code2;
  response += "\r\n";
  Serial.println("G");
  response += "Content-Type: ";
  response += type;
  response += "\r\n";
  Serial.println("G");
  response += "Content-Length: ";
  response += file_body.length();
  response += "\r\n";
  Serial.println("G");
  response += "\r\n";
  Serial.println("G");
  Serial.println(file_body.length());
  Serial.println("G");
  response += file_body.c_str();
  Serial.println("DD");
  return response;
}

int send_and_return(WiFiClient& client, String type, String filename, int status = 200) {
  Serial.println("EA");

  if (status == 200) {
    String content = get_page(type, filename, status);
    if (content == "") {
      Serial.println("EB");
      return -1;
    }
    client.println(content);
    return 200;
  }
  String content = get_page(type, filename, status);
  if (content == "") {
    Serial.println("EC");
    return -1;
  }
  Serial.println("ED");
  client.println(content);
  return status;
}

bool is_directory(String uri) {
  Serial.println("FA");
  std::string uri_std_str = uri.c_str();
  DIR* dir = opendir(uri.c_str());

  if (dir != NULL) {
    closedir(dir);
    if (uri_std_str.find_first_of("/") == uri_std_str.length() - 1) {
  Serial.println("FB");
      return false;
    }
  Serial.println("FC");
    return true;
  }
  Serial.println("FD");
  return false;
}

void send_redirect(WiFiClient& client, String& redirect_url) {
  String response = 
        "HTTP/1.1 301 Moved Permanently\r\n"
        "Location: " + redirect_url + "\r\n"
        "Content-Length: 0\r\n"
		    "Connection: close\r\n\r\n";
  client.println(response);
}

// ------------------------ Helper functions  ------------------------ \\'


// ------------------------ GET POST DELETE  ------------------------ \\'

int GET(Request& request, WiFiClient& client) {
  Serial.println("GA");
  String uri = request.get_URI().c_str();
  String content;
  mbed::File file;

  if (uri.length() < 2) {
    Serial.println("GB");
    return send_and_return(client, "text/html", "index.html");
  }
  uri.remove(0, 1);
  if (is_directory(uri)) {
    Serial.println("GC");
    uri += "/";
    send_redirect(client, uri);
    return 301;
  }
  String test = read_file(uri);
  if (test == "") {
    Serial.println("GD");
    client.println(error_page(404));
    return 404;
  }
  content = get_page(get_file_content_type(uri, true), uri);
  if (content.length() == 0) { 
    Serial.println("GF");
    client.println(error_page(406));
    return 406;
  }
  Serial.println("GG");
  client.println(content);
  return 200;
}

int POST(Request& request, WiFiClient& client) {
  Serial.println("HA");
  String uri = rq.get_URI().c_str();
  std::string file_name;
  std::string type;
  String file_type;
  String content;
  int tmp;

  if (!rq.is_body()) {
    Serial.println("HB");
    return send_and_return(client, "text/html", "error_pages/400_error_page.html", 400);
  }
  std::vector<std::string> head = rq.get_head();
  uri.remove(0, 1);
  for (tmp = 0; tmp < head.size(); tmp++) {
    String temp = head[tmp].c_str();
    if (temp.indexOf("Content-Type") > 1) {
      type = temp.c_str();
      break;
    }
  }
  if (!is_directory(type.c_str())) {
    Serial.println("HC");
    return send_and_return(client, "text/html", "error_pages/404_error_page.html", 404);
  }
  file_type = get_file_content_type(rq.get_type(type).c_str(), false);
	if (file_type.length() == 0) {
    Serial.println("HD");
		return send_and_return(client, "text/html", "error_pages/400_error_page.html", 400);
  }
	file_name = rq.get_file_name(type);
	if (file_name.length() == 0) {
    Serial.println("HE");
		return send_and_return(client, "text/html", "error_pages/400_error_page.html", 400);
  }
  Serial.println("HF");
  return 200;
}

int DELETE(Request& request, WiFiClient& client) {
  String uri = rq.get_URI().c_str();
  String content;

  uri.remove(0, 1);
  content = read_file(uri);
  if (content == "")
    return send_and_return(client, "text/html", "error_pages/404_error_page.html", 404);
  usb.remove(uri.c_str());
  return 200;
}

// ------------------------ GET POST DELETE  ------------------------ \\'



void setup() {
  Serial.begin(112000);
  pinMode(PA_15, OUTPUT);
  digitalWrite(PA_15, HIGH);
  while (!Serial) {
    ;
  }

  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    while (true);
  }

  status = WiFi.beginAP(ssid, pass);
  if (status != WL_AP_LISTENING)
    Serial.println("Cant create access point");
  delay(1000);
  server.begin();
  Serial.print("IP: ");
  Serial.println(WiFi.localIP());
  Serial.println("--------------------------");
  if (msd.connect()) {
    int err = usb.mount(&msd);
    if (err) {
      connected = true; 
    } else {
      connected = false;
    }
    found = true;
  } else {
    found = false;
  }
  get_file_header_type();
}

void loop() {
  // if (!found) {
  //   if (!msd.connect())
  //     continue;
  //   found = true;
  // }
  if (msd.connect()) {
    if (usb.mount(&msd)) {
      bool stopped = false;
      WiFiClient client = server.available();
      if (client) {
        String str;
        boolean currentLineIsBlank = true;
        while (client.connected()) {
          if (client.available()) {
            char c = client.read();
            str += c;
            if (c == '\n' && currentLineIsBlank) {
              rq = str.c_str();
              if (rq.get_method() == "GET") {
                if (GET(rq, client) == -1) {  //if get returns -1, then return a bad request
                  client.stop();
                  stopped = true;
                  break;
                }
              }
              else if (rq.get_method() == "POST")
                POST(rq, client);
              else if (rq.get_method() == "DELETE")
                DELETE(rq, client);
              break;
            }
            if (c == '\n') {
              currentLineIsBlank = true;
            } else if (c != '\r') {
              currentLineIsBlank = false;
            }
          }
        }
        if (!stopped)
          client.stop();
      }
      // delay(2000);
    }
  }
}

If you try to debug it and compile on your own, you also need the request header and source file. Header:

#pragma once
#include <map>
#include <vector>
#include <sstream>

class Request {
    public:
        Request();
        Request(std::string request);
        Request(const Request& copy);
        Request& operator=(const Request& copy);
        Request& operator=(const std::string& copy);
        ~Request();
        std::string find(std::string to_find, std::string request);
        std::string find(std::string to_find, std::string request, char delim);
        std::string get_method();
        std::string get_URI();
        std::string get_HTTP();
        std::string get_HOST();
        std::string get_sec_ch_ua_platform();
        std::string get_user_agent();
        std::string get_sec_ch_ua();
        std::string get_sec_ch_ua_mobile();
        std::string get_update_insecure_requests();
        std::string get_sec_fetch_dest();
        std::string get_sec_fetch_mode();
        std::string get_sec_fetch_site();
        std::string get_sec_fetch_user();
        std::string get_accept();
        std::string get_connection();
        std::string get_accept_language();
        std::string get_accept_encoding();
        std::string get_type(std::string header);
        std::string get_file_name(std::string header);
        std::vector<std::string> get_head();
        std::vector<std::string> get_body();
        std::string get_header_variable_name(std::string header);
        bool is_body();
        int get_content_length();
        void set_uri(std::string uri);
    private:
        std::string remove_string(std::string remove);
        std::string get_body(std::string request);
        std::map<std::string, std::string> request;
        std::vector<std::string> req_head;
        std::vector<std::string> req_body;
        bool body;
};

And here the source file:

#include "Request.hpp"

Request::Request() {
    this->request["Method"] = "";
    this->request["URI"] = "";
    this->request["HTTP"] = "";
    this->request["Host"] = "";
    this->request["Connection"] = "";
    this->request["Content-Length"] = "";
    this->request["sec-ch-ua"] = "";
    this->request["sec-ch-ua-mobile"] = "";
    this->request["sec-ch-ua-platform"] = "";
    this->request["Upgrade-Insecure-Requests"] = ""; 
    this->request["User-Agent"] = "";
    this->request["Accept"] = "";
    this->request["Sec-Fetch-Site"] = "";
    this->request["Sec-Fetch-Mode"] = "";
    this->request["Sec-Fetch-User"] = "";
    this->request["Sec-Fetch-Dest"] = "";
    this->request["Accept-Encoding"] = "";
    this->request["Accept-Language"] = "";
    this->request["Body"] = "";
    this->request["Secondary-Method"] = "";
}

Request::Request(std::string request) {
    std::istringstream stream(request);
    std::stringstream stream2(request);
    std::string temp;

    stream >> temp;
    this->request["Method"] = temp;
    stream >> temp;
    this->request["URI"] = temp;
    stream >> temp;
    this->request["HTTP"] = temp;
    this->request["Host"] = find("Host:", request);
    this->request["Content-Length"] = find("Content-Length:", request);
    this->request["Connection"] = find("Connection:", request);
    this->request["sec-ch-ua"] = find("sec-ch-ua:", request);
    this->request["sec-ch-ua-mobile"] = find("sec-ch-ua-mobile:", request);
    this->request["sec-ch-ua-platform"] = find("sec-ch-ua-platform:", request);
    this->request["Upgrade-Insecure-Requests"] = find("Upgrade-Insecure-Requests:", request);
    this->request["User-Agent"] = find("User-Agent:", request);
    this->request["Accept"] = find("Accept:", request);
    this->request["Sec-Fetch-Site"] = find("Sec-Fetch-Site:", request);
    this->request["Sec-Fetch-Mode"] = find("Sec-Fetch-Mode:", request);
    this->request["Sec-Fetch-User"] = find("Sec-Fetch-User:", request);
    this->request["Sec-Fetch-Dest"] = find("Sec-Fetch-Dest:", request);
    this->request["Accept-Encoding"] = find("Accept-Encoding:", request);
    this->request["Accept-Language"] = find("Accept-Language:", request);
    if (get_body(request) == "👎") {
        body = false;
    } else {
        body = true;
        std::size_t pos = 0;
        std::vector<std::size_t> pos_v;
        std::vector<std::size_t> pos2;
        for (std::vector<std::string>::iterator it = req_head.begin(); it != req_head.end(); ++it, pos++) {
            if (it->find("filename") != std::string::npos) {
                std::string fname = *it;
                fname.erase(0, fname.find("filename=") + 10);
                fname.erase(fname.find("\""));
                if (fname.length() != 0) {
                    pos_v.push_back(pos);
                }
                break;
            }
        }
        pos = 0;
        std::vector<std::size_t>::iterator vector_it = pos_v.begin();
        bool iterate = true;
        if (vector_it == pos_v.end())
            iterate = false;
        for (std::vector<std::string>::iterator it = req_body.begin(); it != req_body.end(); ++it, pos++) {
            if (iterate) {
                if (*vector_it == pos && vector_it != pos_v.end()) {
                    vector_it++;
                    continue;
                }
            }
            if (it->length() == 0) {
                body = false;
            }
        }
    }
}

Request::Request(const Request& copy) {
    this->request = copy.request;
}

Request& Request::operator=(const Request& copy) {
    this->request = copy.request;
    return *this;
}

Request& Request::operator=(const std::string& request) {
    std::istringstream stream(request);
    std::stringstream stream2(request);
    std::string temp;

    stream >> temp;
    this->request["Method"] = temp;
    stream >> temp;
    this->request["URI"] = temp;
    stream >> temp;
    this->request["HTTP"] = temp;
    this->request["Host"] = find("Host:", request);
    this->request["Content-Length"] = find("Content-Length:", request);
    this->request["Connection"] = find("Connection:", request);
    this->request["sec-ch-ua"] = find("sec-ch-ua:", request);
    this->request["sec-ch-ua-mobile"] = find("sec-ch-ua-mobile:", request);
    this->request["sec-ch-ua-platform"] = find("sec-ch-ua-platform:", request);
    this->request["Upgrade-Insecure-Requests"] = find("Upgrade-Insecure-Requests:", request);
    this->request["User-Agent"] = find("User-Agent:", request);
    this->request["Accept"] = find("Accept:", request);
    this->request["Sec-Fetch-Site"] = find("Sec-Fetch-Site:", request);
    this->request["Sec-Fetch-Mode"] = find("Sec-Fetch-Mode:", request);
    this->request["Sec-Fetch-User"] = find("Sec-Fetch-User:", request);
    this->request["Sec-Fetch-Dest"] = find("Sec-Fetch-Dest:", request);
    this->request["Accept-Encoding"] = find("Accept-Encoding:", request);
    this->request["Accept-Language"] = find("Accept-Language:", request);
    if (get_body(request) == "👎") {
        body = false;
    } else {
        body = true;
        std::size_t pos = 0;
        std::vector<std::size_t> pos_v;
        std::vector<std::size_t> pos2;
        for (std::vector<std::string>::iterator it = req_head.begin(); it != req_head.end(); ++it, pos++) {
            if (it->find("filename") != std::string::npos) {
                std::string fname = *it;
                fname.erase(0, fname.find("filename=") + 10);
                fname.erase(fname.find("\""));
                if (fname.length() != 0) {
                    pos_v.push_back(pos);
                }
                break;
            }
        }
        pos = 0;
        std::vector<std::size_t>::iterator vector_it = pos_v.begin();
        bool iterate = true;
        if (vector_it == pos_v.end())
            iterate = false;
        for (std::vector<std::string>::iterator it = req_body.begin(); it != req_body.end(); ++it, pos++) {
            if (iterate) {
                if (*vector_it == pos && vector_it != pos_v.end()) {
                    vector_it++;
                    continue;
                }
            }
            if (it->length() == 0) {
                body = false;
            }
        }
    }
    return *this;
}

Request::~Request() {}

std::string Request::get_type(std::string header) {
    header.erase(0, header.find("Content-Type:") + 14);
    return header;
}

std::string Request::get_file_name(std::string header) {
    header.erase(0, header.find("filename=") + 10);
    header.erase(header.find("\""), header.length());
    if (header.find(".")) {
        header.erase(header.find("."), header.length());
    }
    return header;
}

std::string Request::get_header_variable_name(std::string header) {
    header.erase(0, header.find("name=") + 6);
    header.erase(header.find("\""));
    return header;
}

std::string Request::get_body(std::string request) {
    std::string req;
    std::string boundary;
    std::size_t pos = request.find("\r\n\r\n");

    req_body.clear();
    req_head.clear();
    if (request.find("boundary=") == std::string::npos)
        return "👎";
    req = request.erase(0, request.find("boundary=") + 9);
    boundary = "--" + req;
    boundary.erase(boundary.find("\n") - 1, boundary.length());
    req.erase(0, req.find(boundary) + boundary.length() + 2);

    bool done = false;
    std::vector<std::string> temp;
    std::size_t current, end;
    std::string remaining = request;
    std::string req_single;
    while (!done) {
        std::string head, body;
        remaining.erase(0, remaining.find(boundary) + boundary.length() + 2);
        req_single = remaining;
        current = req_single.find(boundary);
        end = req_single.find(boundary + "--");
        if (end == current)
            done = true;
        req_single.erase(req_single.find(boundary) - 2);
        head = req_single;
        head.erase(head.find("\r\n\r\n"));
        body = req_single;
        body.erase(0, body.find("\r\n\r\n") + 4);
        req_head.push_back(head);
        req_body.push_back(body);
    }
    return "👍";
}

std::string Request::find(std::string to_find, std::string request) {
    std::stringstream stream(request);
    std::string line;
    bool found = false;

    while (std::getline(stream, line)) {
        if (line.find(to_find) != std::string::npos) {
            found = true;
            break;
        }
    }
    if (found)
        line.erase(0, to_find.length() + 1);
    else
        line.clear();
    return line;
}

std::string Request::find(std::string to_find, std::string request, char delim) {
    std::string newString = find(to_find, request);
    std::size_t pos;

    for (pos = 0; pos < newString.length(); pos++) {
        if (newString[pos] == '&')
            break;
    }
    return newString.substr(0, pos);
}

std::string Request::get_method() {
    return request["Method"];
}

std::string Request::get_URI() {
    return request["URI"];
}

std::string Request::get_HTTP() {
    return request["HTTP"];
}

std::string Request::get_HOST() {
    return request["Host"];
}

std::string Request::get_accept() {
    return request["Accept"];
}

std::string Request::get_connection() {
    return request["Connection"];
}

std::string Request::get_user_agent() {
    return request["User-Agent"];
}

std::string Request::get_sec_fetch_dest() {
    return request["Sec-Fetch-Dest"];
}

std::string Request::get_sec_fetch_mode() {
    return request["Sec-Fetch-Mode"];
}

std::string Request::get_sec_fetch_site() {
    return request["Sec-Fetch-Site"];
}

std::string Request::get_sec_fetch_user() {
    return request["Sec-Fetch-User"];
}

std::string Request::get_accept_language() {
    return request["Accept-Language"];
}

std::string Request::get_accept_encoding() {
    return request["Accept-Encoding"];
}

std::string Request::get_sec_ch_ua_platform() {
    return request["sec-ch-ua-platform"];
}

std::string Request::get_sec_ch_ua() {
    return request["sec-ch-ua"];
}

std::string Request::get_sec_ch_ua_mobile() {
    return request["sec-ch-ua-mobile"];
}

std::string Request::get_update_insecure_requests() {
    return request["Upgrade-Insecure-Requests"];
}

std::vector<std::string> Request::get_head() {
    return req_head;
}

std::vector<std::string> Request::get_body() {
    return req_body;
}

bool Request::is_body() {
    return body;
}

int Request::get_content_length() {
    if (request.find("Content-Length") == request.end())
        return 0;
    std::stringstream ss;
    ss << request["Content-Length"];
    int length;
    ss >> length;

    if (!ss.good())
        return -1;
    return length;
}

void Request::set_uri(std::string uri) {
    request["URI"] = uri;
}

I just worked on the GET method, so If you see errors in the POST or DELETE request, this doesn't affect the program, since they are never called so far.

Thanks for helping me out

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.