file
This commit is contained in:
parent
3e08995072
commit
218e41082c
24
src/file.c
24
src/file.c
|
@ -1,6 +1,28 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "file.h"
|
||||||
|
|
||||||
void get_file_path(char *uri, char *file_path) {
|
void get_file_path(char *uri, char *file_path) {
|
||||||
char *question = strchr(uri, '?');
|
char *question = strrchr(uri, '?');
|
||||||
|
if(question) {
|
||||||
|
*question = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
if(uri[strlen(uri) - 1] == '/') {
|
||||||
|
strncat(uri, "index.html", 11);
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(file_path, "htdocs", 7);
|
||||||
|
printf("%s\n", file_path);
|
||||||
|
strncat(file_path, uri, (int)sizeof(uri));
|
||||||
|
|
||||||
|
const char *dot = strrchr(file_path, '.');
|
||||||
|
if(!dot || dot == file_path) {
|
||||||
|
strncat(file_path, ".html", 6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_mime_type(char *file, char *mime) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
#define MAX_PATH_SIZE 4096
|
||||||
|
|
||||||
|
void get_file_path(char *uri, char *file_path);
|
14
src/server.c
14
src/server.c
|
@ -9,6 +9,7 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
#include "file.h"
|
||||||
|
|
||||||
int send_response(int fd, const char *header, char *content_type, void *body, int content_length) {
|
int send_response(int fd, const char *header, char *content_type, void *body, int content_length) {
|
||||||
const int max_response_size = MAX_RESPONSE_SIZE;
|
const int max_response_size = MAX_RESPONSE_SIZE;
|
||||||
|
@ -107,8 +108,17 @@ int main() {
|
||||||
if (strncmp("GET", method, 7) != 0) {
|
if (strncmp("GET", method, 7) != 0) {
|
||||||
send_response(client_sock_fd, BAD_REQUEST_HEAD, "text/html", NULL, 0);
|
send_response(client_sock_fd, BAD_REQUEST_HEAD, "text/html", NULL, 0);
|
||||||
} else {
|
} else {
|
||||||
char body[100];
|
char file_path[4096];
|
||||||
int body_size = snprintf(body, 100, "%s\r\n", "hello what up bruh");
|
get_file_path(uri, file_path);
|
||||||
|
|
||||||
|
FILE *file = fopen(file_path, "r");
|
||||||
|
if(!file) {
|
||||||
|
send_response(client_sock_fd, NOT_FOUND_HEAD, "text/html", NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// int body_size = snprintf(body, 100, "%s\r\n", "hello what up bruh");
|
||||||
send_response(client_sock_fd, OK_HEAD, "text/html", body, (body_size + 1));
|
send_response(client_sock_fd, OK_HEAD, "text/html", body, (body_size + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue