Skip to content

47 jinhokim implement tiny http server #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 73 additions & 27 deletions jinhokim/Makefile
Original file line number Diff line number Diff line change
@@ -1,39 +1,85 @@
CC = c++
# Define the compiler and flags

CFLAGS = -std=c++98 -Wall -Wextra -Werror -pedantic
CXX := c++
CXXFLAGS := -Wall -Wextra -Werror -std=c++98 -pedantic -march=native -O2 -pipe

GREEN = \033[32m
YELLOW = \033[33m
RESET = \033[0m
# Define the directories

SERVER_SRCS = src/Server.cpp \
src/server_main.cpp
SRC_DIR := src
BUILD_DIR := build
INC_DIR := include

CLIENT_SRCS = src/Client.cpp \
src/client_main.cpp
# Define the source files

all: server client
SERVER_SRCS := $(addprefix $(SRC_DIR)/, server_main.cpp Server.cpp Response.cpp)
CLIENT_SRCS := $(addprefix $(SRC_DIR)/, client_main.cpp Client.cpp)
SERVER_OBJS := $(patsubst %.cpp, $(BUILD_DIR)/%.o, $(SERVER_SRCS))
CLIENT_OBJS := $(patsubst %.cpp, $(BUILD_DIR)/%.o, $(CLIENT_SRCS))
DEPS := $(patsubst %.cpp, $(BUILD_DIR)/%.d, $(SERVER_SRCS) $(CLIENT_SRCS))

server:
@echo "$(YELLOW)Building $@...$(RESET)"
@$(CC) $(SERVER_SRCS) $(CFLAGS) -o $@ $<
@echo "$(GREEN)Done.$(RESET)"
# Define the variables for progress bar

client:
@echo "$(YELLOW)Building $@...$(RESET)"
@$(CC) $(CLIENT_SRCS) $(CFLAGS) -o $@ $<
@echo "$(GREEN)Done.$(RESET)"
TOTAL_FILES := $(shell find $(SRC_DIR) -type f -name '*.cpp' | wc -l)
COMPILED_FILES := 0
STEP := 100

# Define the name

SERVER := server
CLIENT := client

# Define the rules

all:
@$(MAKE) -j $(SERVER)
@$(MAKE) -j $(CLIENT)

$(SERVER) : $(SERVER_OBJS)
@$(CXX) $(CXXFLAGS) $^ -o $@
@printf "\n$(MAGENTA)[WEBSERV] Linking SERVER Success\n$(DEF_COLOR)"

$(CLIENT) : $(CLIENT_OBJS)
@$(CXX) $(CXXFLAGS) $^ -o $@
@printf "\n$(MAGENTA)[WEBSERV] Linking CLIENT Success\n$(DEF_COLOR)"

$(BUILD_DIR)/%.o : %.cpp | dir_guard
@$(CXX) $(CXXFLAGS) -I $(INC_DIR) -c $^ -o $@
$(eval COMPILED_FILES = $(shell expr $(COMPILED_FILES) + 1))
$(eval PROGRESS = $(shell expr $(COMPILED_FILES) "*" $(STEP) / $(TOTAL_FILES)))
@printf " \r"
@printf "$(YELLOW)[WEBSERV] [%02d/%02d] ( %3d %%) Compiling $<\r$(DEF_COLOR)" $(COMPILED_FILES) $(TOTAL_FILES) $(PROGRESS)

dir_guard:
@mkdir -p $(addprefix $(BUILD_DIR)/, $(SRC_DIR))

clean:
@echo "$(YELLOW)Cleaning up...$(RESET)"
@echo "$(GREEN)Done.$(RESET)"
@$(RM) -r $(BUILD_DIR)
@printf "$(BLUE)[WEBSERV]:\tobj. dep. files$(DEF_COLOR)$(GREEN) => Cleaned!\n$(DEF_COLOR)"

fclean: clean
@$(RM) $(SERVER) $(CLIENT)
@printf "$(CYAN)[WEBSERV]:\texec. files$(DEF_COLOR)$(GREEN) => Cleaned!\n$(DEF_COLOR)"

re: fclean
@$(MAKE) all
@printf "$(GREEN)[WEBSERV]Cleaned and Rebuilt everything\n$(DEF_COLOR)"

.PHONY: all clean fclean re dir_guard norm

norm:
@(norminette | grep Error) || (printf "$(GREEN)[WEBSERV]:\tNorminette Success\n$(DEF_COLOR)")

fclean:
make clean
@rm -f server client
# Colors

re:
make fclean
make all
RESET = \033[1;39m
YELLOW = \033[1;33m
GRAY = \033[1;90m
RED = \033[1;91m
GREEN = \033[1;92m
YELLOW = \033[1;93m
BLUE = \033[1;94m
MAGENTA = \033[1;95m
CYAN = \033[1;96m
WHITE = \033[1;97m

.PHONY: all clean fclean re
-include $(DEPS)
Empty file added jinhokim/favicon.ico
Empty file.
Binary file added jinhokim/html/200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions jinhokim/html/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>

<head>
<title>404 Status Page</title>
</head>

<body>
<p>
<h1>404 Not Found</h1><br>
<a href="https://http.cat/404">404 cat</a><br>
</p>
</body>

</html>
Binary file added jinhokim/html/404.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions jinhokim/html/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>

<head>
<title>500 Status Page</title>
</head>

<body>
<p>
<h1>500 Internal Server Error</h1><br>
<a href="https://http.cat/500">500 cat</a><br>
</p>
</body>

</html>
Binary file added jinhokim/html/500.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions jinhokim/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>

<head>
<title>200 Status Page</title>
</head>

<body>
<p>
<h1>Webserv...</h1><br>
<a href="https://http.cat/200">200 cat</a><br>
<img src="html/200.png" alt="200 Cat">
</p>
</body>

</html>
36 changes: 18 additions & 18 deletions jinhokim/include/Client.hpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#ifndef CLIENT_HPP
# define CLIENT_HPP
#define CLIENT_HPP

# include <iostream>
# include <cstring>
# include <cstdlib>
# include <unistd.h>
# include <sys/socket.h>
# include <arpa/inet.h>
# include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>

#include <iostream>

class Client {
public:
Client(int port);
virtual ~Client(void);
public:
Client(int port);
virtual ~Client(void);

int Set(void);
int Run(void);

int Set(void);
int Run(void);
private:
const int port_;
int client_fd_;
sockaddr_in server_address_;
std::string response_;
private:
const int port_;
int client_fd_;
sockaddr_in server_address_;
std::string response_;
};

void CheckArgument(int ac, char** av);
int PrintError(const std::string str);

#endif // CLIENT_HPP
33 changes: 33 additions & 0 deletions jinhokim/include/Response.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef RESPONSE_HPP
#define RESPONSE_HPP

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <iostream>

#define BUFSIZE 10240

class Response {
public:
Response(char *request);
~Response(void);

void ResponseHandler(void);
std::string FindMime(std::string uri);
void FillHeader(int status, long len, std::string type);
void Handle200(int ct_len, const char *local_uri);
void Handle404(void);
void Handle500(void);

void ComposeResponse(int status, const char *status_text, int len,
std::string type);
std::string GetResponse(void);

private:
std::string request_;
std::string response_;
};

#endif // RESPONSE_HPP
85 changes: 36 additions & 49 deletions jinhokim/include/Server.hpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,43 @@
#ifndef SERVER_HPP
# define SERVER_HPP

# include <iostream>
# include <unistd.h>
# include <map>
# include <vector>
# include <netdb.h>
# include <arpa/inet.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <sys/event.h>
# include <sys/time.h>
# include <sys/stat.h>
# include <fcntl.h>

# define BACKLOG 1024
# define BUFSIZE 1024
# define HEADER_FORMAT "HTTP/1.1 %d %s\nContent-Length: %ld\nContent-Type: %s\n\n"
# define NOT_FOUND_CONTENT "<h1>404 Not Found</h1>\n"
# define SERVER_ERROR_CONTENT "<h1>500 Internal Server Error</h1>\n"

/**
* @brief
* Server class, use socket communication for echo server
*/
#define SERVER_HPP

#include <arpa/inet.h>
#include <sys/event.h>
#include <sys/socket.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

#include "Response.hpp"

class Server {
public:
Server(int port);
virtual ~Server(void);

std::string GetIp(void);
void ChangeEvents(std::vector<struct kevent>& change_list, int socket, int16_t filter,
uint16_t flags, uint32_t fflags, intptr_t data, void *udata);
void DisconnectClient(int client_fd, std::map<int, std::string>& clients);

int Set(void);
void SetResponse(void);
int Run(void);
private:
int port_;
int server_fd_;
sockaddr_in address_;
std::string request_;
std::string response_;
public:
Server(int port);
virtual ~Server(void);

void ChangeEvents(int socket, int16_t filter, uint16_t flags, uint32_t fflags,
intptr_t data, void* udata);
void DisconnectClient(int client_fd);

int Set(void);
int Run(void);

int GetServerIdx(int sock);
int IsServer(uintptr_t ident);
int HandleErrorEvent(uintptr_t ident);
int HandleReadEvent(uintptr_t ident);

private:
int port_;
std::vector<int> server_fds_;
std::vector<sockaddr_in> address_;
std::map<int, std::string> clients_;
std::vector<struct kevent> change_list_;
};

void CheckArgument(int ac, char** av);
int PrintError(const std::string str);

void fill_header(char *header, int status, long len, std::string type);
void handle_404(int asock);
void handle_500(int asock);
void find_mime(char *ct_type, char *uri);
void http_handler(int asock);

#endif // SERVER_HPP
13 changes: 0 additions & 13 deletions jinhokim/index.html

This file was deleted.

Loading