something

This commit is contained in:
Win 2024-05-17 23:12:56 +07:00
parent f2728eaf68
commit 802e974269
3 changed files with 49 additions and 28 deletions

BIN
server Executable file

Binary file not shown.

View File

@ -9,10 +9,10 @@
#include <time.h>
#include "server.h"
#include "file.c"
#define PORT 8080
#define BUFFER_SIZE 1024
/*
TODO: CREATE SENDING RESPONSE FUNCTION
int send_response(int fd, char *header, char *content_type, void *body, int content_length) {
const int max_response_size = 65536;
@ -35,12 +35,14 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont
memcpy(response + response_length, body, content_length);
}
*/
int main() {
char buffer[BUFFER_SIZE];
char resp[] = "HTTP/1.0 200 OK\r\n"
"Server: webserver-c\r\n"
"Content-type: text/html\r\n\r\n"
"<html>hello, world</html>\r\n";
// char resp[] = "HTTP/1.0 200 OK\r\n"
// "Server: webserver-c\r\n"
// "Content-type: text/html\r\n\r\n"
// "<html>hello, world</html>\r\n";
// create a socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
@ -106,30 +108,49 @@ int main() {
// check if request is GET request
if (strncmp("GET", method, 7) != 0) {
const char resp[] = "HTTP/1.0 400 Bad Request\r\n"
"Server: webserver-c\r\n"
"Content-Type: text/html\r\n\r\n";
write(newsockfd, resp, strlen(resp));
} else {
char file_path[BUFFER_SIZE];
get_file_path(uri, file_path);
// const char resp[] = "HTTP/1.0 400 Bad Request\r\n"
// "Server: webserver-c\r\n"
// "Content-Type: text/html\r\n\r\n";
FILE *file = fopen(file_path, "r");
}
char resp[100];
int resp_size = snprintf(resp, 100,
"%s\r\n"
"Server: %s\r\n"
"Content-Type: %s\r\n"
"\r\n",
BAD_REQUEST_HEAD, SERVER_VERSION, "text/html");
// write to the socket
int valwrite = write(newsockfd, resp, strlen(resp));
int valwrite = write(newsockfd, resp, resp_size);
if (valwrite < 0) {
perror("webserver (write)");
continue;
}
} else {
char resp[100];
int resp_size = snprintf(resp, 100,
"%s\r\n"
"Server: %s\r\n"
"Content-Type: %s\r\n"
"\r\n",
OK_HEAD, SERVER_VERSION, "text/html");
int valwrite = write(newsockfd, resp, resp_size);
printf("%s\n", OK_HEAD);
if (valwrite < 0) {
perror("webserver (write)");
continue;
}
}
// write to the socket
// int valwrite = write(newsockfd, resp, strlen(resp));
// if (valwrite < 0) {
// perror("webserver (write)");
// continue;
// }
close(newsockfd);
}
return 0;
}
void get_file_path(char *uri, char *file_path) {
char *question = strchr(route, '?');
}

View File

@ -1,8 +1,8 @@
#define PORT 8080
#define BUFFER_SIZE 1024
const char SERVER_VERSION[] = "winnie-http-server/0.0.1";
static const char SERVER_VERSION[] = "winnie-http-server/0.0.1";
const char BAD_REQUEST_RESP[] = "HTTP/1.1 400 Bad Request\r\n";
const char NOT_FOUND_RESP[] = "HTTP/1.1 404 Not Found\r\n";
const char OK_RESP[] = "HTTP/1.1 200 OK\r\n";
static const char BAD_REQUEST_HEAD[] = "HTTP/1.1 400 Bad Request";
static const char NOT_FOUND_HEAD[] = "HTTP/1.1 404 Not Found";
static const char OK_HEAD[] = "HTTP/1.1 200 OK";