Skip to content
Snippets Groups Projects
Commit 2c17d1ef authored by Saif Eddine Askri's avatar Saif Eddine Askri
Browse files

Enhance request handling by improving key generation randomness and adding...

Enhance request handling by improving key generation randomness and adding input tokenization for better request parsing
parent e643b1aa
Branches
Tags
No related merge requests found
......@@ -9,7 +9,7 @@
pid_t pid = getpid();
std::string generateRandomKey() {
return "key" + std::to_string(rand() % 100);
return "key" + std::to_string(rand() % 10000);
}
......@@ -93,7 +93,8 @@ int main(int argc, char* argv[]) {
int sock;
struct sockaddr_in server_address;
char* buffer[bufferSize] = {0};
char buffer[bufferSize] = {0};
// Socket erstellen
sock = socket(AF_INET, SOCK_STREAM, 0);
......
......@@ -46,10 +46,38 @@ struct Request {
// Create a new Request struct
Request* req = new Request();
// Copy the input buffer to a mutable string for tokenization
char* input = strdup(buffer); // Duplicate the buffer
if (!input) {
return nullptr; // Handle memory allocation failure
}
// Tokenize the input string using ',' and ';' as delimiters
char* token = strtok(input, ",;");
if (token) {
// First token is the operation
req->operation = token[0]; // Extract the first character (s, g, or d)
// Second token is the key
token = strtok(nullptr, ",;");
if (token) {
req->key = token;
// Third token is the value (only for store operation)
if (req->operation == 's') {
token = strtok(nullptr, ",;");
if (token) {
req->value = token;
}
}
}
}
// Free the duplicated input string
free(input);
return req;
}
// we assume that this function is slow
std::string processRequest_slow(const std::string& request) {
std::string response;
......@@ -80,7 +108,7 @@ std::string processRequest_slow(const std::string& request) {
response = "Invalid operation!";
break;
}
free(parsedRequest);
return response;
}
//=============================================
......@@ -154,6 +182,7 @@ void handle_client(int client_socket) {
send(client_socket, response.c_str(), response.size(), 0);
}
close(client_socket);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment