-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refact: Webserver 클래스, main catch&throw 추가
- Loading branch information
1 parent
2d621a6
commit 556c19f
Showing
5 changed files
with
125 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "Webserver.hpp" | ||
#define BUFFER_SIZE 100 | ||
|
||
Webserver::Webserver(int argc, char** argv, char** envp) | ||
{ | ||
(void)argc; | ||
(void)argv; | ||
(void)envp; | ||
} | ||
|
||
void Webserver::start_server(void) | ||
{ | ||
Socket socket_one(INADDR_ANY, 8000); | ||
size_t connections; // 통신 소켓 | ||
char buffer[BUFFER_SIZE]; | ||
int bytesRead = BUFFER_SIZE - 1; | ||
|
||
/* 테스트 진행 */ | ||
while (1) | ||
{ | ||
bytesRead = BUFFER_SIZE - 1; | ||
connections = socket_one.Accept(connections); | ||
// STUB : Read from the connection | ||
while (bytesRead == BUFFER_SIZE - 1) | ||
{ | ||
ft_memset(buffer, 0, BUFFER_SIZE); // REVIEW : 전체 탐색하는 것들은 성능 개선의 여지가 있음 | ||
bytesRead = read(connections, buffer, BUFFER_SIZE - 1); // request 를 여기서 받아서.. | ||
if (bytesRead == -1) | ||
std::cerr << "Could not read request." << std::endl; | ||
} | ||
if (bytesRead != -1) | ||
{ | ||
// STUB : HttpMessageRequest | ||
HttpMessageRequest request(buffer); | ||
request.Parser(); // request 를 parsing 한 후, | ||
|
||
// STUB : HttpMessageResponse | ||
HttpMessageResponse response(request); // reponse 를 정리한다. | ||
response.SetMessage(); | ||
|
||
// STUB : Send a message to the connection | ||
int len = response.GetMessage().size(); | ||
int ret = send(connections, response.GetMessage().c_str(), len, 0); | ||
} | ||
// Close the connections | ||
close(connections); | ||
} | ||
close(socket_one.GetFd()); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
#include "../Server/Socket.hpp" | ||
#include "../HttpMessage/HttpMessageRequest.hpp" | ||
#include "../HttpMessage/HttpMessageResponse.hpp" | ||
#include <sys/select.h> | ||
#include <iostream> | ||
#include <vector> | ||
// #include "ConfigWebserver.hpp" | ||
// #include "Path.hpp" | ||
|
||
class Webserver | ||
{ | ||
// ConfigWebserver config; | ||
// fd_set to_be_checked; | ||
// fd_set to_be_checked_read; | ||
// fd_set to_be_checked_write; | ||
// std::vector<Server> servers; | ||
|
||
public: | ||
Webserver(int argc, char** argv, char** envp); | ||
void start_server(void); // 서버 시작 | ||
// Webserver(const Path&); // config 파일의 경로를 받아서 초기화 | ||
// void create_server(const std::vector<Config>& config_locations); | ||
|
||
// private: | ||
// void parse_config(const Path& path_config); // config 파일 해석 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,12 @@ | ||
#include "Server/Socket.hpp" | ||
#include "HttpMessage/HttpMessageRequest.hpp" | ||
#include "HttpMessage/HttpMessageResponse.hpp" | ||
#include "Webserver/Webserver.hpp" | ||
|
||
#define BUFFER_SIZE 100 | ||
|
||
int main() | ||
int main(int argc, char** argv, char** env) | ||
{ | ||
// Create a socket (IPv4, TCP) | ||
Socket socket(INADDR_ANY, 80); // Listen 작업까지 되어 있는 소켓 | ||
size_t connections; // 통신 소켓 | ||
char buffer[BUFFER_SIZE]; | ||
int bytesRead = BUFFER_SIZE - 1; | ||
|
||
/* 테스트 진행 */ | ||
while (1) | ||
{ | ||
bytesRead = BUFFER_SIZE - 1; | ||
connections = socket.Accept(connections); | ||
// STUB : Read from the connection | ||
while (bytesRead == BUFFER_SIZE - 1) | ||
{ | ||
ft_memset(buffer, 0, BUFFER_SIZE); // REVIEW : 전체 탐색하는 것들은 성능 개선의 여지가 있음 | ||
bytesRead = read(connections, buffer, BUFFER_SIZE - 1); // request 를 여기서 받아서.. | ||
if (bytesRead == -1) | ||
std::cerr << "Could not read request." << std::endl; | ||
} | ||
if (bytesRead != -1) | ||
{ | ||
// STUB : HttpMessageRequest | ||
HttpMessageRequest request(buffer); | ||
request.Parser(); // request 를 parsing 한 후, | ||
|
||
// STUB : HttpMessageResponse | ||
HttpMessageResponse response(request); // reponse 를 정리한다. | ||
response.SetMessage(); | ||
|
||
// STUB : Send a message to the connection | ||
int len = response.GetMessage().size(); | ||
int ret = send(connections, response.GetMessage().c_str(), len, 0); | ||
} | ||
// Close the connections | ||
close(connections); | ||
try { | ||
Webserver s(argc, argv, env); | ||
s.start_server(); | ||
} | ||
catch(const std::exception &e) { | ||
std::cerr << e.what() << std::endl; | ||
} | ||
close(socket.GetFd()); | ||
} | ||
} |