I have a function that returns a response depending on the method and the path of the request.
When I use strcmp(method, "GET")
it gives me -67
, even if the method is exactly the same (I used printf()
to see the value of method), it only works with path.
char *handle_response(char *method, char *path, char *protocol) {
char *response;
//strcmp
if (strcmp(method, "GET") == 0 && strcmp(path, "/") == 0) {
response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nhola\n";
} else {
printf("Failed strcmp\n");
printf("%s\n", method);
printf("%s\n", path);
printf("%d\n", strcmp(method, "GET"));
printf("%d\n", strcmp(path, "/"));
}
//strstr OK
if (strstr(method, "GET") && strstr(path, "/")) {
response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nhola\n";
} else {
response = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found";
}
return response;
}
I was able to make it work using strstr()
, but it doesn't make any sense to me.
I call the handle_response()
function after parsing the request:
int http_parser(char *client_socket) {
char request[BUFFER_SIZE];
char *method;
char *protocol;
char *path;
strncpy(request, client_socket, sizeof(request));
char *token = strtok(request, " ");
if (token == NULL) {
printf("\nInvalid Request Format!");
return 0;
}
//METODO
method = token;
if (token == NULL) {
printf("Invalid Request Method!\n");
return 0;
}
//PATH
token = strtok(NULL, " ");
path = token;
if (token == NULL) {
printf("Invalid Request Path\n");
return 0;
}
//PROTOCOL
token = strtok(NULL, "\n");
protocol = token;
if (token == NULL) {
printf("Invalid Request Protocol\n");
return 0;
}
//FUNCTION CALLING handling_response()
int client_fd = *client_socket;
void *response = (void *)handle_response(method, path, protocol);
send(client_fd, response, strlen(response), 0);
return 1;
}
Found the rat in my code, it was in my handle_client() function :
// Funcion Manejar los clientes
void handle_client(int client_socket){
char buffer [BUFFER_SIZE]; //buffer for client request
//Guarda la request en el buffer y Cuenta los bytes recibidos con ssize_t
ssize_t bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
char request = client_socket;
if(bytes_received > 0){
http_parser(&request);
}
close(client_socket); }
As you can see, i send the pointer of the char (request) of an integer (client_socket).
Since my http_parser() function is waiting for a pointer of a string, and since the value of "&request" doesn't contain a NULL terminated string, functions like strlcpy and strtok added this value to the request, resulting in the unknown first character of my request string.
Here's the modified version of my code :
char * handle_response( char * method, char * path, char * protocol){
char * response;
//strcmp
if (strcmp(method,"GET") == 0 && strcmp(path,"/") == 0){
response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nhola strcmp\n";
} else {
response = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found";
}
return response;
}
int http_parser(int client_socket, char * request) {
char buffer[BUFFER_SIZE];
//Copy the request to the buffer
strlcpy(buffer,request, sizeof(buffer));
//METODO
char * method = strtok(buffer, " ");
if(method == NULL){
printf("Invalid Request Method!\n");
return 0;
}
//PATH
char * path = strtok(NULL, " ");
if (path == NULL){
printf("Invalid Request Path\n");
return 0;
}
//PROTOCOL
char * protocol = strtok(NULL,"\r\n");
if(protocol == NULL){
printf("Invalid Request Protocol\n");
return 0;
}
const char * response = handle_response(method, path, protocol);
send(client_socket, response, strlen(response), 0);
return 1;
}
// Funcion Manejar los clientes
void handle_client(int client_socket){
char buffer [BUFFER_SIZE]; //buffer for client request
//Guarda la request en el buffer y Cuenta los bytes recibidos con ssize_t
ssize_t bytes_received = recv(client_socket, buffer, sizeof(buffer) -1, 0);
buffer[bytes_received] = '\0'; //NULL Terminate
printf("Request:\n %s \n", buffer);
if(bytes_received > 0){
http_parser(client_socket, buffer);
}
close(client_socket);
}
Thank you for your answers and comments anyways :)