diff --git a/.ccls b/.ccls deleted file mode 100644 index 275509f..0000000 --- a/.ccls +++ /dev/null @@ -1,7 +0,0 @@ -clang++ --Iincludes -%c --Wall --Wextra --Werror --std=c++98 diff --git a/.gitignore b/.gitignore index 9eaf432..3b39428 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ webserv +*.o +.ccls +.vscode \ No newline at end of file diff --git a/Makefile b/Makefile index 6de5e18..0c7df57 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CXX = clang++ SANFLAGS = -fsanitize=address # SANFLAGS = -fsanitize=leak -CXXFLAGS = -Wall -Wextra -Werror -g -std=c++98 $(SANFLAGS) +CXXFLAGS = -Wall -Wextra -Werror -g -std=c++98 $(SANFLAGS) SRC = $(wildcard ./src/*.cpp) diff --git a/includes/Server.hpp b/includes/Server.hpp new file mode 100644 index 0000000..3bab8d0 --- /dev/null +++ b/includes/Server.hpp @@ -0,0 +1,41 @@ +#ifndef SERVER_HPP +#define SERVER_HPP + +#include "webserv.hpp" + +#define BUFFSIZE 1024 + +class Server +{ +private: + int _port; + int _serverSocket; + int _clientSocket; + // int _bufferSize; + struct sockaddr_in _addres; + std::string _ip; + +private: + void checkError(int fd, std::string str); + void sendFile(std::string str); + void sendHeader(void); + void printRed(std::string str); + void printYellow(std::string str); + void printBlue(std::string str); + void printPink(std::string str); + void printGreen(std::string str); + void printTurguoise(std::string str); + +public: + Server(); + Server(std::string path); + + void readConfig(void); + void setupConfig(void); + void start(void); + void end(void); + ~Server(); +}; + + +#endif \ No newline at end of file diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 180a976..e1e0a8f 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -12,5 +12,8 @@ #include #include #include +#include "Server.hpp" +#include +#include #endif diff --git a/src/Server.cpp b/src/Server.cpp new file mode 100644 index 0000000..a90ecfa --- /dev/null +++ b/src/Server.cpp @@ -0,0 +1,141 @@ +#include "webserv.hpp" +//----------------------------------------------Constructors----------------------------------------------------------------------------------- +Server::Server() +{ + +} + +Server::Server(std::string path) +{ + (void)path; +} +//---------------------------------------------Color printing---------------------------------------------------------------------------------- + +void Server::printRed(std::string str) +{ + std::cout << "\033[31m" << str << "\033[0m"; +} + +void Server::printGreen(std::string str) +{ + std::cout << "\033[32m" << str << "\033[0m"; +} + +void Server::printYellow(std::string str) +{ + std::cout << "\033[33m" << str << "\033[0m"; +} + +void Server::printBlue(std::string str) +{ + std::cout << "\033[34m" << str << "\033[0m"; +} + +void Server::printPink(std::string str) +{ + std::cout << "\033[35m" << str << "\033[0m"; +} + +void Server::printTurguoise(std::string str) +{ + std::cout << "\033[36m" << str << "\033[0m"; +} + +//---------------------------------------------Send-------------------------------------------------------------------------------------------- + +void Server::sendHeader(void) +{ + std::string tmp; + const char *header; + + tmp = "HTTP/1.1 200 OK\r\nDate: Mon, 27 Jul 2009 12:28:53 GMT\r\nServer: Apache/2.2.14 (Win32)\r\nLast-Modified: Wed, 22 Jul 2009 19:15:56 GMT\r\nContent-Length: 6196\r\nContent-Type: text/html\r\nConnection: Closed\r\n\r\n"; + header = tmp.c_str(); + printTurguoise("Send Header\n"); + printYellow(tmp); + send(_clientSocket, header, tmp.length(), 0); +} + +void Server::sendFile(std::string str) +{ + char const *path = str.c_str(); + std::ifstream file(path); + char buff[BUFFSIZE + 1] = {0}; + + if (!file.good()) + { + printRed("Send ERROR: bad file: "); + printRed(str); + std::cout << std::endl; + } + sendHeader(); + while (!file.eof()) + { + file.read(buff, BUFFSIZE); + send(_clientSocket, buff, file.gcount(), 0); + } +} + +//---------------------------------------------Configuration----------------------------------------------------------------------------------- +void Server::readConfig(void) +{ + +} + +void Server::setupConfig(void) +{ + this->_ip = "127.0.0.1"; + this->_port = 8080; +} + +void Server::checkError(int fd, std::string str) +{ + if (fd < 0) + { + printRed("Server ERROR: "); + printRed(str); + std::cout << std::endl; + exit(1); + } + else + { + printGreen("Server SUCCESS: "); + printGreen(str); + std::cout << std::endl; + } +} + +void Server::start(void) +{ + char buff[BUFFSIZE + 1] = {0}; + int opt = 1; + socklen_t addrlen; + + _serverSocket = socket(AF_INET, SOCK_STREAM, 0); + checkError(_serverSocket, "Initialize Server socket"); + checkError(setsockopt(_serverSocket, SOL_SOCKET, SO_REUSEADDR + | SO_REUSEPORT, &opt, sizeof(opt)), "Set socket options"); + _addres.sin_family = AF_INET; + _addres.sin_addr.s_addr = INADDR_ANY; + _addres.sin_port = htons(_port); + checkError(bind(_serverSocket, (struct sockaddr *)&_addres, sizeof(_addres)), "Bind socket"); + checkError(listen(_serverSocket, 3), "Listen socket"); + addrlen = sizeof(_addres); + _clientSocket = accept(_serverSocket, (struct sockaddr *)&_addres, (socklen_t *)&addrlen); + checkError(_clientSocket, "Initialize Client socket"); + checkError(recv(_clientSocket, buff, BUFFSIZE, 0), "Receive msg from client"); + printTurguoise("Receive Header\n"); + printYellow(buff); + sendFile("www/index.html"); + close(_clientSocket); + close(_serverSocket); +} + +void Server::end(void) +{ + +} + +Server::~Server() +{ +} + diff --git a/src/main.cpp b/src/main.cpp index 6636bec..2504e23 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,96 +1,15 @@ -#include #include "webserv.hpp" -#include -#include - -#include - -#define PORT 8080 -#define BUFSIZE 2048 - -int main(int argc, char **argv) +int main(int argc, char **argv) { (void)argc; (void)argv; - int socketfd; - int opt = 1; - int new_socket; - - char buffer[BUFSIZE + 1] = {0}; - char sendbuffer[BUFSIZE + 1] = {0}; - - struct sockaddr_in address; - - std::cout << "jopa" << std::endl; - - if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) <= 0) - { - std::cerr << "socket bad" << std::endl; - return (1); - } - if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) < 0) - { - std::cerr << "setsockopt" << std::endl; - return (1); - } - - address.sin_family = AF_INET; - address.sin_addr.s_addr = INADDR_ANY; - address.sin_port = htons(PORT); - - if (bind(socketfd, (struct sockaddr *)&address, sizeof(address)) < 0) - { - std::cerr << "bind" << std::endl; - return (1); - } - - if (listen(socketfd, 3) < 0) - { - std::cerr << "listen" << std::endl; - return (1); - } - - socklen_t addrlen = sizeof(address); - if ((new_socket = accept(socketfd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) - { - perror("accept error"); - throw std::runtime_error("accept error"); - } - - int val_read = recv(new_socket, buffer, BUFSIZE, 0); - std::cout << "read value: " << val_read << std::endl; - std::cout << "buffer: \n\"" << buffer << "\"" << std::endl; - - /* HEADER */ - - /* std::string protocol = "HTTP/2 200 OK\r\nserver: jopa\r\n"; */ - /* std::string type = "Content-Type: text/html\r\n"; */ - /* std::string listen */ - - /* std::string tmp = protocol + type + "\r\n"; */ - std::string tmp = "HTTP/1.1 200 OK\r\nDate: Mon, 27 Jul 2009 12:28:53 GMT\r\nServer: Apache/2.2.14 (Win32)\r\nLast-Modified: Wed, 22 Jul 2009 19:15:56 GMT\r\nContent-Length: 6196\r\nContent-Type: text/html\r\nConnection: Closed\r\n\r\n"; - const char *payload = tmp.c_str(); - - std::cout << "<<<" << payload << ">>>" << std::endl; - send(new_socket, payload, tmp.length(), 0); - - std::ifstream page("www/index.html"); - if (!page.good()) - { - throw std::runtime_error("file bad"); - } - - while (!page.eof()) - { - page.read(sendbuffer, BUFSIZE); - send(new_socket, sendbuffer, page.gcount(), 0); - } - - close(new_socket); - while(1); - close(socketfd); - + Server server; + + server.readConfig(); + server.setupConfig(); + server.start(); + return (0); }