I’m trying to write a basic HTTP file server using sockets in C and have two main problems.
First, when I try to access “ip:port/” through a browser, I get the index.html file but it’s in complete gibberish. However, when I access “ip:port/not_in_directory” my little custom 404 message comes back just fine. If I set the default file to a existing pdf file, gibberish comes back as well (length of pdf gibberish).
Secondly, when I try to access “ip:port/file_that_exists”, I get my 404 instead of the correct file. While debugging, I do see that scratch_pad has the correct file name, with no excess or missing characters, but !access(scratch_pad, R_OK) still comes out to be 0. Is there some necessary cast even though access() takes a char* for the file name?
EDIT: Second issue has been solved (I was using a pointer to deallocated buffer as file name). However, everything that’s picked up by the browser is gibberish. I’m removing more non-essential code.
#define FILE_NOT_FOUND "HTTP/1.0 404 FILE NOT FOUND\r\n"
#define FILE_FOUND "HTTP/1.0 200 OK\r\n"
#define SERVER_NAME "Server: Test Server\r\n"
#define CONTENT_TYPE "Content-Type: text/html\r\n"
#define DATE "Date: "
#define MESSAGE_BREAK "\r\n"
#define NOT_FOUND_HTML "<!DOCTYPE html>\n<html>\n<title>404 Not Found</title>\n<body>\n<p>Nope</p>\n</body>\n</html>\r\n"
if(!access(pulled_name, R_OK | W_OK)) {
printf("file found on server\n");
server_file = fopen(pulled_name, "rb+");
fseek(server_file, 0L, SEEK_END);
file_size = ftell(server_file);
fseek(server_file, 0l, SEEK_SET);
printf("opened successfully and set file_size to %i\n", file_size);
getting_time = time(NULL);
c_time = localtime(&getting_time);
current_time = asctime(c_time);
printf("set time for message\n");
message = malloc((int) strlen(FILE_FOUND)
+ (int) strlen(SERVER_NAME)
+ (int) strlen(CONTENT_TYPE)
+ (int) strlen(DATE)
+ (int) strlen(current_time)
+ (int) strlen(MESSAGE_BREAK));
printf("malloc'ed message\n");
message_size = (int)(strlen(message)*sizeof(char));
memset(message, 0, message_size);
strcpy(message, FILE_FOUND);
strcat(message, SERVER_NAME);
strcat(message, CONTENT_TYPE);
strcat(message, DATE);
strcat(message, current_time);
strcat(message, MESSAGE_BREAK);
printf("built message\n");
printf("set message_size to %i\n", message_size);
while(total_sent < message_size) {
n = send(new_socket_fd, message, message_size, 0);
if (n==-1) {
printf("error sending message\n");
break;
}
total_sent += n;
printf("sent %i\n", total_sent);
}
free(message);
total_sent = 0;
while(total_sent < file_size) {
n = send(new_socket_fd, server_file, file_size, 0);
if (n==-1) {
printf("error sending file\n");
break;
}
total_sent += n;
printf("sent %i\n", total_sent);
}
fclose(server_file);
close(new_socket_fd);
printf("closed client connection!\n");
There several instances of this mistake:
when the type of the first argument is a
char*, which means the buffer will not be filled with null characters but only the firstsizeof(char*)bytes (typically 4 or 8). In this case, it means thatbuffermay not be null terminated (asrecv()does not append null characters). Bothprintf()andstrtok()require string arguments to be null terminated. It is common to see garbage characters printed after a string with a call toprintf("%s", p);with a non-null terminated string.Fix all
memset()calls by explicitly specifying the size of the buffer pointed to by the first argument:The
sizeof(char)is guaranteed to be1so it can be omitted from themalloc()argument.Other misuse of
memset():'0'is a the character zero, not nullsizeof(message)is thesizeof(char*)change to: