diff --git a/CGI/Cookie.php b/CGI/Cookie.php deleted file mode 100644 index 432b5a6..0000000 --- a/CGI/Cookie.php +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - Cookie Example - - - Welcome back, $username!"; - } else { - // The "username" cookie is not set - echo "

Hello, we've set a cookie for you!

"; - } - ?> - - - diff --git a/CGI/a.out b/CGI/a.out deleted file mode 100755 index 1964ded..0000000 Binary files a/CGI/a.out and /dev/null differ diff --git a/CGI/cgi.cpp b/CGI/cgi.cpp index 62d38fa..2c55df3 100755 --- a/CGI/cgi.cpp +++ b/CGI/cgi.cpp @@ -1,54 +1,27 @@ #include "cgi.hpp" std::mapget_env(char *file, request req) { - std::map env; - env["SERVER_SOFTWARE"] = "MyServer/1.0"; - env["SERVER_NAME"] = "localhost"; - env["GATEWAY_INTERFACE"] = "CGI/1.1"; - env["SERVER_PROTOCOL"] = "HTTP/1.1"; - env["SERVER_PORT"] = "80"; env["REQUEST_METHOD"] = req.get_method(); - env["SCRIPT_NAME"] = "/cgi-bin/mycgi"; env["CONTENT_TYPE"] = req.get_header("Content-Type"); env["CONTENT_LENGTH"] = std::to_string(req.get_body().size()); env["QUERY_STRING"] = req.get_query(); - env["REMOTE_ADDR"] = "127.0.0.1"; - env["REMOTE_HOST"] = "localhost"; env["REDIRECT_STATUS"] = "200"; env["HTTP_COOKIE"] = req.get_header("Cookie"); env["SCRIPT_FILENAME"] = file; - - return env; } -void put_cookie(std::string output, Respond &res) -{ - std::string token; - std::size_t pos = output.find("Set-Cookie:"); - if (pos != std::string::npos) - { - pos += 12; - std::size_t end = output.find(";", pos); - if (end != std::string::npos) { - token = output.substr(pos); - } else { - token = output.substr(pos); - } - res.set_header("Set-Cookie", token); - } -} -void set_headers_cgi(std::string output, Respond &res) { +void set_headers_cgi(std::string output, Respond &res) +{ std::stringstream ss(output); std::string line; std::string body; bool headers_finished = false; - put_cookie(output, res); - body =+ ""; + body =+ "\n"; while (std::getline(ss, line)) { if (line == "\r") { headers_finished = true; @@ -56,11 +29,11 @@ void set_headers_cgi(std::string output, Respond &res) { } if (!headers_finished) { std::size_t pos = line.find(": "); - if (pos != std::string::npos) { + if (pos != std::string::npos) + { std::string key = line.substr(0, pos); std::string value = line.substr(pos + 2); - if(key != "Set-Cookie") - res.set_header(key, value); + res.set_header(key, value); } } else @@ -69,6 +42,14 @@ void set_headers_cgi(std::string output, Respond &res) { res.set_response_body(body); } +void free_all(char *file, char *path, char **env, int size) +{ + for (int i = 0; i < size; i++) + free(env[i]); + free (env); + free (file); + free (path); +} std::string run_cgi(request &r, Respond &res) { @@ -85,43 +66,54 @@ std::string run_cgi(request &r, Respond &res) std::map env = get_env(file, r); std::string method = env["REQUEST_METHOD"]; - std::string content_type = env["CONTENT_TYPE"]; - std::string content_length_str = env["CONTENT_LENGTH"]; - std::string query_string = env["QUERY_STRING"]; char **envp = (char **)malloc(sizeof(char *) * (env.size() + 1)); + if(!envp) + { + res.set_status_code(500); + res.set_response_body(res.get_response_status(res.get_status_code())); + free_all(file, path,envp, env.size()); + return res.get_response_status(res.get_status_code()); + } int i = 0; - for (std::map::iterator it = env.begin(); it != env.end(); it++, i++) + for (std::map::iterator it = env.begin(); it != env.end(); it++) { std::string e = it->first + "=" + it->second; - envp[i] = strdup(e.c_str()); + envp[i++] = strdup(e.c_str()); } envp[i] = NULL; int pid = fork(); if (pid == -1) { res.set_status_code(500); - res.set_status_message(res.get_response_status(res.get_status_code())); + res.set_response_body(res.get_response_status(res.get_status_code())); + free_all(file, path,envp, env.size()); return NULL; } else if (pid == 0) { + std::cout << r.get_body() << "\n"; if (method == "POST") { + if (dup2(fdtemp1, STDIN_FILENO) == -1) { - std::cerr << "Error: failed to redirect stdin\n"; - exit(1); + res.set_status_code(500); + res.set_response_body(res.get_response_status(res.get_status_code())); + free_all(file, path,envp, env.size()); + return res.get_response_status(res.get_status_code()); } - std::fprintf(temp1, "%s", r.get_body().c_str()); + write(fdtemp1, r.get_body().c_str(), r.get_body().size()); std::rewind(temp1); } if (dup2(fdtemp, STDOUT_FILENO) == -1) { - std::cerr << "Error: failed to redirect stdout\n"; - exit(1); + res.set_status_code(500); + res.set_response_body(res.get_response_status(res.get_status_code())); + free_all(file, path,envp, env.size()); + return res.get_response_status(res.get_status_code()); } - alarm(2); + alarm(1); execve(cmd[0], cmd, envp); exit(1); } @@ -131,7 +123,9 @@ std::string run_cgi(request &r, Respond &res) if (WIFSIGNALED(status) || status != 0) { res.set_status_code(500); - res.set_status_message(res.get_response_status(res.get_status_code())); + res.set_response_body(res.get_response_status(res.get_status_code())); + free_all(file, path,envp, env.size()); + return res.get_response_status(res.get_status_code()); } char buf[1]; std::string content; @@ -140,12 +134,16 @@ std::string run_cgi(request &r, Respond &res) while ((byt = read(fdtemp, buf, 1)) > 0){ content.append(buf, 1); } - //std::cout << content < -#include -#include -#include -#include -#include -#include "cgi.hpp" - -#define PORT 80 -int main(int ac, char **av) -{ - if(ac != 3) - { - std::cout << "error\n"; - return (1); - } - - int server_fd, new_socket; long valread; - struct sockaddr_in address; - int addrlen = sizeof(address); - - char *hello = "Hello from server"; - - // Creating socket file descriptor - if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) - { - perror("In socket"); - exit(EXIT_FAILURE); - } - - - address.sin_family = AF_INET; - address.sin_addr.s_addr = INADDR_ANY; - address.sin_port = htons( PORT ); - - memset(address.sin_zero, '\0', sizeof address.sin_zero); - - - if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) - { - perror("In bind"); - exit(EXIT_FAILURE); - } - - if (listen(server_fd, 10) < 0) - { - perror("In listen"); - exit(EXIT_FAILURE); - } - - while(1) - { - printf("\n+++++++ Waiting for new connection ++++++++\n\n"); - if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) - { - perror("In accept"); - exit(EXIT_FAILURE); - } - char buffer[30000] = {0}; - valread = read( new_socket , buffer, 30000); - printf("%s\n",buffer ); - //write(new_socket , hello , strlen )); - //printf("------------------Hello message sent-------------------\n"); - // request r(buffer); - //std::cout << "query :::::::::::::::::::::::::::::::::::::::::::::: " < \ No newline at end of file diff --git a/CGI/test.cpp b/CGI/test.cpp deleted file mode 100644 index e83d829..0000000 --- a/CGI/test.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include -#include - -void send_token(std::string output) { - std::string token; - std::size_t pos = output.find("Set-Cookie:"); - if (pos != std::string::npos) { - pos += 12; // length of "Set-Cookie: " - std::size_t end = output.find(";", pos); - if (end != std::string::npos) { - token = output.substr(pos, end - pos); - } else { - token = output.substr(pos); - } - } - std::cout << "Content-Type: text/html\r\n"; - if (!token.empty()) { - std::cout << "Set-Cookie: token=" << token << "\r\n"; - } - std::cout << "\r\n"; - std::cout << "Response body goes here...\n"; -} - -#include - -void extract_path(std::string location_name) { - // Find the position of the path name after the "location" keyword - size_t pos = std::string("location").length(); - for (int i = pos; i < location_name.size(); i++) - { - if (!isspace(location_name[i])) - break; - pos++; - } - int start = pos; - int i; - for (i = pos; i < location_name.size(); i++) - { - if (isspace(location_name[i]) || location_name[i] == '{') - break; - } - int end = i; - //std::cout << location_name[end] << " " << end << "\n"; - std::string path; - //std::cout << location_name << "\n"; - //std::cout << start << " " << end << "\n"; - path = location_name.substr(start, end ); - //std::cout << path << "\n"; - // // Extract the substring from the path name up to the closing curly brace character - // std::string path = location_name.substr(pos); - // size_t end_pos = path.find('}'); - // if (end_pos != std::string::npos) { - // path = path.substr(0, end_pos); - // } - - // // Remove leading and trailing spaces from the path name - // size_t start_pos = path.find_first_not_of(" \t"); - // if (start_pos != std::string::npos) { - // path = path.substr(start_pos); - // } - // size_t trailing_space_pos = path.find_last_not_of(" \t"); - // if (trailing_space_pos != std::string::npos) { - // path = path.substr(0, trailing_space_pos + 1); - // } - - //return path; -} - - -int main() { - std::string location_name = "location /cgi_bin . {"; - extract_path(location_name); // Returns "/cgi_bin" - //std::cout << path <_cookies.push_back(line.substr(pos + 1, line.find("\r\n") - pos - 1)); - else - this->setHeader(key, line.substr(pos + 1, line.find("\r\n") - pos - 1)); - } - else if (line.find("\r\n") != std::string::npos){ - break; - } - } - getline(tmp, this->_body, '\0'); - if (!this->_body.empty()) - this->setStatus(200); - else - this->setStatus(502); -}*/ \ No newline at end of file diff --git a/CGI/test.py b/CGI/test.py deleted file mode 100644 index 8818b9c..0000000 --- a/CGI/test.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python - -import cgi - -print("HTTP/1.1 200 OK") -print("Content-Type: text/html") - -print() -print("") - -form = cgi.FieldStorage() - -if "name" not in form or "email" not in form: - print("

Error: Please enter a name and email.

") -else: - name = form["name"].value - email = form["email"].value - print("

Thank you for your submission, {} ({})!

".format(name, email)) - -print("") \ No newline at end of file diff --git a/CGI/test1.php b/CGI/test1.php deleted file mode 100644 index f9084b2..0000000 --- a/CGI/test1.php +++ /dev/null @@ -1,12 +0,0 @@ - - - - -

Hello, !

-

Your email is:

- - - diff --git a/prs_rsc/server.conf b/config/server.conf similarity index 88% rename from prs_rsc/server.conf rename to config/server.conf index 44380eb..0ec8b10 100644 --- a/prs_rsc/server.conf +++ b/config/server.conf @@ -1,33 +1,37 @@ -server +server { listen 8001; - + listen 8002; + listen 8003; + listen 8004; host 127.0.0.1; - client_max_body_size 10000000; + client_max_body_size 1; root ./www/html; + index index.html; error_page 404 www/html/error_pages/404.html; error_page 500 www/html/error_pages/500.html; - - index index.html; - + error_page 403 www/html/error_pages/403.html; + # server_name yismaili; + # server_name yismail; location / { - autoindex on; + # autoindex on; } location /cgi_bin { - root ./www/html/cgi_bin; allow_Methods GET POST; index hello.html; - autoindex on; path_info .py /usr/bin/python3; path_info .php /php-cgi; + # path_info .php /php-cgi; + #listen 8001; + #autoindex on; } - location /red + location /red { - return 301 https://www.youtube.com/watch?v=_Nbm2yn8WA8&t=30s; + return 301 https://www.youtube.com/watch?v=fDticmXbG0I; } location /delete { @@ -35,6 +39,7 @@ server allow_methods DELETE; index todelete; path_info .py /usr/bin/python3; + } location /upload{ @@ -51,6 +56,7 @@ server allow_methods GET; autoindex off; } + } # server diff --git a/include/http_server.hpp b/include/http_server.hpp index d52d8d6..6012a40 100644 --- a/include/http_server.hpp +++ b/include/http_server.hpp @@ -6,7 +6,7 @@ /* By: yismaili +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/22 14:57:52 by yismaili #+# #+# */ -/* Updated: 2023/05/19 14:16:17 by yismaili ### ########.fr */ +/* Updated: 2023/05/22 22:15:41 by yismaili ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,11 @@ namespace http{ std::vector::iterator find_conf(int sockfd); int parse_header(std::string header, int sockfd); unsigned int getTime(void); + int ifport_dup(int port_); + int ifhost_dup(std::string host_); + int get_server(std::vector conf_); + int ifserver_dup(std::string server_name); + void setIndexOfserver(int sockfd); public: http::sockets sock; std::vector socket_id; @@ -75,6 +80,10 @@ namespace http{ std::size_t body_end; int header_error; std::size_t transfer_encoding_gzip; + std::vector servers_names; + std::vector port; + std::vector host; + bool flag; }; } #endif diff --git a/include/sockets.hpp b/include/sockets.hpp index 26d4523..dbfe23f 100644 --- a/include/sockets.hpp +++ b/include/sockets.hpp @@ -6,7 +6,7 @@ /* By: yismaili +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/27 15:29:15 by yismaili #+# #+# */ -/* Updated: 2023/05/19 14:18:40 by yismaili ### ########.fr */ +/* Updated: 2023/05/22 22:13:50 by yismaili ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ namespace http{ public: sockets(); ~sockets(); - sockets &init_data(int port_, std::string ip_add,int index_); + sockets &init_data(int port_, std::string ip_add, std::vector server_name_, int index_); int const &getSockfd()const; unsigned int getSock_addr_len()const; sockaddr_in &getServ_addr()const; @@ -44,17 +44,23 @@ namespace http{ void setContent_length(int const &content); unsigned int const &getTime_out() const; void setTime_out(unsigned int time); - private: - struct addrinfo hints; + void setIndex(int const index_); + int const &getIndex_tmp() const; + int data_issending; + + public: + struct addrinfo hints; //provides information about a network address, such as the host name, port number, and address family. struct addrinfo *result, *rp; unsigned int sock_addr_len; std::string ip_addr; + int index_tmp; int index; std::size_t content_length; int sockfd; int port; unsigned int time_out; - int header_error; + int header_error; + std::vector server_name; }; } #endif \ No newline at end of file diff --git a/prs_rsc/Data_config.hpp b/prs_rsc/Data_config.hpp index 3c8192c..bdcf6fb 100644 --- a/prs_rsc/Data_config.hpp +++ b/prs_rsc/Data_config.hpp @@ -15,15 +15,4 @@ struct Data_config std::map location; }; - - -struct compters -{ - int c_server_name; - int c_host; - int c_root; - int c_client_max_body_size; -}; - - #endif \ No newline at end of file diff --git a/prs_rsc/location.cpp b/prs_rsc/location.cpp index 5d05202..5280840 100644 --- a/prs_rsc/location.cpp +++ b/prs_rsc/location.cpp @@ -21,7 +21,6 @@ std::string extract_path(std::string location_name) if(location_name[i] == '{') break; } - int end = i; if(flag) { @@ -42,9 +41,12 @@ std::string extract_path(std::string location_name) location::location(Data_config data, std::string location_name) : server(data, 0) { - //std::cout << location_name << "\n"; this->location_name = extract_path(location_name); - //std::cout <<"|||||||||||||"<location_name<<"|"<location_name.empty()) + { + std::cerr << "error : empty location name"<< std::endl; + exit(1); + } } location::~location() diff --git a/prs_rsc/location.hpp b/prs_rsc/location.hpp index 33c2bc6..165242e 100644 --- a/prs_rsc/location.hpp +++ b/prs_rsc/location.hpp @@ -9,25 +9,6 @@ class location : public server { -private: - // std::vector _listen; /*s*/ - // std::vector _server_name; /*s*/ - // std::vector _index; - // std::string _host; /*s*/ - // std::string _root; - // int _client_max_body_size; - // std::map _error_page; - // std::vector _allow_methods; - // bool _autoindex; - // // std::vector _access_log; - // // std::vector _error_log; - // //std::vector _meme_types; - // // std::string _gzip; - // // std::string _ssl_certificate; - // // std::string _ssl_certificate_key; - // // std::string _ssl_protocols; - // // std::string _ssl_ciphers; - // //std::vector location public: std::string location_name; location(Data_config data, std::string location_name); diff --git a/prs_rsc/main.cpp b/prs_rsc/main.cpp index f31b2d6..daaa3a1 100644 --- a/prs_rsc/main.cpp +++ b/prs_rsc/main.cpp @@ -5,7 +5,7 @@ int main(int ac, char **av) { - if (ac != 2) + if (ac > 2) { std::cerr << "Error: could not open config file.\n"; return (1); @@ -13,7 +13,7 @@ int main(int ac, char **av) // Register a custom signal handler for demonstration std::vector servers; std::vector all_ports; - servers = ft_fill_servers(av); + servers = ft_fill_servers(av, ac); all_ports = get_all_ports(servers); // for (size_t i = 0; i < servers.size(); i++) // { diff --git a/prs_rsc/server.cpp b/prs_rsc/server.cpp index 19885bf..f063e9d 100644 --- a/prs_rsc/server.cpp +++ b/prs_rsc/server.cpp @@ -41,25 +41,22 @@ bool isIpAddress(const std::string& ip) { return 1; } -int isDomainName(const std::string& domainName) +bool isValidServerName(const std::string Name) { - - if (domainName.empty()) - return 0; - int i = 0; - while (domainName[i]) + if (Name.empty()) + return false; + if (Name.length() > 255) + return false; + if (Name.front() == '.' || Name.front() == '-' || Name.back() == '.' || Name.back() == '-') + return false; + if (Name.find("..") != std::string::npos || Name.find("--") != std::string::npos) + return false; + for (size_t i = 0 ; i < Name.size(); i++) { - char c = domainName[i]; - if (!std::isalnum(c) && c != '-' && c != '.') - return 0; - i++; + if (!std::isalnum(Name[i]) && Name[i] != '-' && Name[i] != '.') + return false; } - - if (domainName.front() == '-' || domainName.back() == '-') - return 0; - if (domainName.find('.') == std::string::npos) - return 0; - return 1; + return true; } int search_char(std::string str, char c) @@ -164,17 +161,6 @@ int ft_non_alphabetic(std::string value) return (0); } -// void ft_check_index(std::string index_file, std::string line) -// { -// int i = index_file.find_last_of("."); -// if (i == -1) -// ft_error(line, "Error"); -// std::string filedot = index_file.substr(i + 1); -// if (filedot != "html" && filedot != "php" && filedot != "htm" && filedot != "css" -// && filedot != "js" && filedot != "jpg" && filedot != "jpeg" && filedot != "png" && filedot != "gif" -// && filedot != "txt" && filedot != "xml" && filedot != "json") -// ft_error(line, "Error"); -// } void check_methods(std::string method, std::string line) { @@ -258,17 +244,20 @@ server::server(Data_config data, bool check_location) std::istringstream iss(line); iss >> key >> value; key = toLower(key); - if (key == "server_name") + if (key == "server_name" && check_location) { - if(c_server_name) - ft_error(line, "Duplicated"); + // if(c_server_name) + // ft_error(line, "Duplicated"); is_empty_value(value, line); - if (ft_numbers_value(iss) || ft_non_alphabetic(value)) + if (ft_numbers_value(iss) || !isValidServerName(value)) ft_error(line, "Error"); + std::vector::iterator it = std::find(_server_name.begin(), _server_name.end(), value); + if(it != _server_name.end()) + ft_error(line, "duplicate server_name"); c_server_name++; _server_name.push_back(value); } - else if (key == "host") + else if (key == "host" && check_location) { if(c_host) ft_error(line, "Duplicated"); @@ -278,7 +267,7 @@ server::server(Data_config data, bool check_location) c_host++; _host = value; } - else if (key == "listen") + else if (key == "listen" && check_location) { is_empty_value(value, line); int port = ft_number(value, line); @@ -339,7 +328,6 @@ server::server(Data_config data, bool check_location) if (c_index) ft_error(line, "Error duplicated"); is_empty_value(value, line); - //ft_check_index(value, line); _index = value; if (ft_numbers_value(iss)) ft_error(line, "Error"); @@ -391,6 +379,9 @@ server::server(Data_config data, bool check_location) } else if (key == "path_info" && !check_location) { + std::map::const_iterator it = _path_info.find(value); + if (it != _path_info.end()) + ft_error(line, "Error"); is_empty_value(value, line); std::string cgi = toLower(value); if (value != ".py" && value != ".php") @@ -432,17 +423,11 @@ server::server(Data_config data, bool check_location) if (line.size() > 1) { if (!is_world(line, "server")) - { - - std::cout << check_location << std::endl; ft_error(line, "Error"); - } } } else - { ft_error(line, "Error"); - } } if(!c_allow_method && !check_location) @@ -453,13 +438,16 @@ server::server(Data_config data, bool check_location) _host = "127.0.0.1"; if(!c_error_page && check_location) { - _error_page[400] = "/400.html"; - _error_page[401] = "/401.html"; - _error_page[403] = "/403.html"; - _error_page[404] = "/404.html"; - _error_page[405] = "/405.html"; - _error_page[500] = "/500.html"; + _error_page[403] = "www/html/error_pages/403.html"; + _error_page[404] = "www/html/error_pages/404.html"; + _error_page[500] = "www/html/error_pages/500.html"; + _error_page[400] = "www/html/error_pages/400.html"; + _error_page[405] = "www/html/error_pages/405.html"; + _error_page[409] = "www/html/error_pages/409.html"; + _error_page[413] = "www/html/error_pages/413.html"; } + if (!c_server_name && check_location) + _server_name.push_back("hostname"); if(data.location.size()) { diff --git a/prs_rsc/server.hpp b/prs_rsc/server.hpp index d6e1cf2..ba6fca3 100644 --- a/prs_rsc/server.hpp +++ b/prs_rsc/server.hpp @@ -6,12 +6,12 @@ #include #include #include +#include class location; class server { protected: - std::vector _server_name; /*s*/ std::string _index; std::string _host; /*s*/ std::string _root; @@ -32,6 +32,7 @@ class server // std::string _ssl_ciphers; std::map _path_info; public: + std::vector _server_name; /*s*/ std::vector _listen; /*s*/ std::vector _location; server(Data_config data, bool check_location); @@ -60,7 +61,7 @@ int is_world(std::string word, std::string str); int search_char(std::string str, char c); std::string trimString(const std::string& str); int skip_spaces(std::string str); -std::vector ft_fill_servers(char **av); +std::vector ft_fill_servers(char **av, int ac); std::vector get_all_ports(std::vector servers); #endif \ No newline at end of file diff --git a/prs_rsc/server_utils.cpp b/prs_rsc/server_utils.cpp index a406884..d428d6e 100644 --- a/prs_rsc/server_utils.cpp +++ b/prs_rsc/server_utils.cpp @@ -19,32 +19,37 @@ std::string trimString(const std::string& str) { std::string trimmedStr = str; - std::string::iterator it = trimmedStr.begin(); - while (it != trimmedStr.end() && std::isspace(*it)) - ++it; - trimmedStr.erase(trimmedStr.begin(), it); - it = trimmedStr.end(); - while (it != trimmedStr.begin() && std::isspace(*(it - 1))) - --it; - trimmedStr.erase(it, trimmedStr.end()); + size_t pos = 0; + while (pos < trimmedStr.size() && std::isspace(trimmedStr[pos])) + ++pos; + trimmedStr.erase(0, pos); + + size_t end = trimmedStr.size(); + while (end > 0 && std::isspace(trimmedStr[end - 1])) + --end; + trimmedStr.erase(end); + return trimmedStr; } -std::vector ft_fill_servers(char **av) + +std::vector ft_fill_servers(char **av, int ac) { std::vector servers; + std::vector v; + Data_config data; std::string config_file; std::ifstream file; std::string line; std::string map; - std::string previousLine; - Data_config data; - std::vector v; int c = 0; - size_t i = 0, j = 0; int flag = 0; int kws = 0; - config_file = av[1]; + size_t i = 0, j = 0; + if (ac == 2) + config_file = av[1]; + else + config_file = "./config/server.conf"; file.open(config_file); if(file.is_open()) { @@ -57,7 +62,7 @@ std::vector ft_fill_servers(char **av) c += search_char(line, '{'); if (!is_world(&line[i], "location")) kws += search_char(line, '{'); - c -= search_char(line, '}'); + c -= search_char(line, '}'); if (is_world(&line[i], "server")) { j = 1; @@ -123,7 +128,7 @@ std::vector ft_fill_servers(char **av) { if (!line.empty()) { - std::cerr << "error something outside of server\n"; + std::cerr << "erraor something outside of server\n"; exit (1); } } @@ -138,7 +143,7 @@ std::vector ft_fill_servers(char **av) data.location.clear(); } flag++; - } + } } else { @@ -154,10 +159,10 @@ std::vector ft_fill_servers(char **av) for (size_t i = 0; i < v.size(); i++) { c = 0; - server *s = new server(v[i], 1); - for (size_t i = 0; i < s->_location.size(); i++) + server s = server(v[i], 1); + for (size_t i = 0; i < s._location.size(); i++) { - if (s->_location[i].location_name == "/") + if (s._location[i].location_name == "/") { c = 1; break ; @@ -166,11 +171,9 @@ std::vector ft_fill_servers(char **av) if (c != 1) { std::cerr << "Error : missing location root\n"; - delete (s); exit (1); } - servers.push_back(*s); - delete (s); + servers.push_back(s); } return (servers); } diff --git a/prs_rsc/test.c b/prs_rsc/test.c deleted file mode 100644 index c2a909e..0000000 --- a/prs_rsc/test.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include - -int main() -{ - int fd1, fd2; - - fd1 = open("file.txt", O_RDONLY); - fd2 = dup(fd1); - - printf("fd1 = %d\n", fd1); - printf("fd2 = %d\n", fd2); - - close(fd1); - close(fd2); - - return 0; -} diff --git a/request/Makefile b/request/Makefile deleted file mode 100644 index 3646f25..0000000 --- a/request/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -NAME = request - -SRC = request.cpp main.cpp request_utils.cpp - -CC = c++ - -# FLAGS = -std=c++98 -Wall -Wextra -Werror -g -fsanitize=address -FLAGS = -std=c++98 -Wall -Wextra -Werror - -OBJS = $(SRC:.c=.o) - -$(NAME): $(OBJS) - $(CC) $(FLAGS) $(SRC) -o $(NAME) - -all: $(NAME) $(OBJS) - -%.o: %.c - $(CC) $(FLAGS) -c $< -o $@ - -clean: - rm -rf $(NAME) - -fclean: clean - -re: fclean all - -.PHONY: all clean fclean re \ No newline at end of file diff --git a/request/input.txt b/request/input.txt deleted file mode 100644 index d1eb51e..0000000 --- a/request/input.txt +++ /dev/null @@ -1,136 +0,0 @@ -Note for the remaining things i didn't parse yet in the request.cpp -#/******************************************************************/ - -this->_headers["Accept-Charsets"] = ""; -this->_headers["Accept-Language"] = ""; -this->_headers["Allow"] = ""; -this->_headers["Auth-Scheme"] = ""; -this->_headers["Authorization"] = ""; -this->_headers["Content-Language"] = ""; -this->_headers["Content-Length"] = ""; -this->_headers["Content-Location"] = ""; -this->_headers["Content-Type"] = ""; -this->_headers["Date"] = ""; -this->_headers["Host"] = ""; -this->_headers["Last-Modified"] = ""; -this->_headers["Location"] = ""; -this->_headers["Referer"] = ""; -this->_headers["Retry-After"] = ""; -this->_headers["Server"] = ""; -this->_headers["Transfer-Encoding"] = ""; -this->_headers["User-Agent"] = ""; -this->_headers["Www-Authenticate"] = ""; -this->_headers["Connection"] = "Keep-Alive"; - -#/******************************************************************/ -i need to clear the _header before using it - -this->_header.clear(); - -#/******************************************************************/ -i need to make the parsing function return an int like 505 or 404 or 200...etc -200 will be returned if the parsing is successful -404 will be returned if the request is not found -505 will be returned if the version is not supported -400 will be returned if the request is bad - -#/******************************************************************/ -I need to compare the content length if existed with the body request -+ after parsing the config file i need to compare a certain thing of it with the body request also - -#/******************************************************************/ - -request_path is the portion of the URL that comes after the domain name and the optional port number, and it identifies the specific resource being requested. - -For example, if the URL requested is http://example.com/path/to/file.html, then the request_path string would be /path/to/file.html. - -#/******************************************************************/ -i will need this URI to in the file_path ---> chagpt steps: -To send a file as a response to an HTTP/1.1 request, you need to follow these steps: - -Parse the client request: You need to parse the client's request to extract the URI (Uniform Resource Identifier) requested by the client. - -Map the URI to a file path: The URI requested by the client needs to be mapped to a file path on your server. You can do this by using a mapping table or by using a convention. - -Open the file: Once you have the file path, you need to open the file using a file stream. In most programming languages, you can use the "open" function to open a file. - -Read the file: Once the file is open, you need to read the file content into a buffer. The file content can be read in chunks to optimize performance. - -Create the response header: After reading the file content, you need to create the response header. The response header should include the HTTP version, the status code, content type, content length, and other relevant information. - -Send the response header: Once the response header is created, you need to send the response header to the client. - -Send the file content: After sending the response header, you need to send the file content to the client. You can do this by writing the content buffer to the socket or output stream. - -Close the file: After sending the file content, you need to close the file stream. - -Close the connection: After closing the file stream, you need to close the connection. - -#/******************************************************************/ - -i should get the file size - -something like this: -size_t response::getResponseFileSize() -{ - if (_file.good()) - { - size_t currentPos = _file.tellg(); - size_t fileSize = 0; - - _file.seekg(0, _file.end); - fileSize = _file.tellg(); - _file.seekg(currentPos); - return (fileSize); - } - else - return (0); -} - -#/******************************************************************/ - -i need to handle the Date in the respond - -#/******************************************************************/ - -i need to handle the Last-Modified in the respond - -#/******************************************************************/ - -i need to handle the Content-Language in the respond - -#/******************************************************************/ - -i need to handle the Content-Location in the respond - -#/******************************************************************/ - -i need to handle the Content-Type in the respond - -#/******************************************************************/ - -I should handle redirections - -#/******************************************************************/ - -i need to handle the Allow in the respond - -#/******************************************************************/ - -init headers of response - - _headers["Allow"] = ""; - _headers["Content-Language"] = ""; - _headers["Content-Length"] = "0"; - _headers["Content-Location"] = ""; - _headers["Content-Type"] = ""; - _headers["Date"] = ""; - _headers["Last-Modified"] = ""; - _headers["Connection"] = ""; - _headers["Location"] = ""; - _headers["Retry-After"] = ""; - _headers["Server"] = "Webserv"; - _headers["Transfer-Encoding"] = "identity"; - _headers["WwwAuthenticate"] = ""; - _headers["Set-Cookie"] = ""; \ No newline at end of file diff --git a/request/request.cpp b/request/request.cpp index e7d8717..0a9ac6a 100644 --- a/request/request.cpp +++ b/request/request.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* request.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: yismaili +#+ +:+ +#+ */ +/* By: aoumad +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/26 23:05:21 by aoumad #+# #+# */ -/* Updated: 2023/05/14 16:04:23 by yismaili ### ########.fr */ +/* Updated: 2023/05/22 20:41:03 by aoumad ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,8 @@ request::request() : _method(""), _uri(""), _version(""), request::request(std::string request, size_t content_len) : _method(""), _uri(""), _version(""), _body(""), _port(80), _query(""), _content_len(content_len) { + this->_init_request = request; add_header("Content-Length", std::to_string(_content_len)); - this->parse_request(request); return ; } @@ -116,16 +116,16 @@ size_t request::get_content_length() const return (this->_content_len); } -void request::parse_request(std::string request) +int request::parse_request() { - // std::cout << "___________33______" << std::endl; - // std::cout << request << std::endl; - // std::cout << "________55_________" << std::endl; + std::string request = this->_init_request; // Split the request into lines std::vector lines; std::istringstream iss(request); std::string line; + if (request.empty()) + return (2); while (std::getline(iss, line)) { if (line.empty()) @@ -133,16 +133,13 @@ void request::parse_request(std::string request) lines.push_back(line); } // Parse the request line - // std::cout << lines[0] << std::endl; std::istringstream request_line(lines[0]); request_line >> this->_method >> this->_uri >> this->_version; // i need to call a function to check if the request line content is suitable or not if (!ft_check_request_line(this->_method, this->_uri, this->_version)) - { - std::cerr << "Invalid request line" << std::endl; - exit(1); - } + return (2); ft_find_query(); + handleSpecialCharacters(this->_uri); // Parse the headers for (std::vector::const_iterator it = lines.begin() + 1; it != lines.end(); ++it) { @@ -154,33 +151,19 @@ void request::parse_request(std::string request) if (this->_headers.find(key) == this->_headers.end()) this->_headers[key] = value; } - // function that checks if the request is POST or PUT to see if there is no content-length to return error if (ft_check_content_length() == false || ft_check_content_type() == false) - { - std::cerr << "Invalid Content-Length or Content-Type" << std::endl; - exit(1); - } + return (2); // function that checks if the header `connexion` exists or not int rtn = ft_check_connexion(); if (rtn != 1) - { - if (rtn == 2) - std::cerr << "Invalid Connexion header" << std::endl; - else - std::cerr << "Missing Connexion header" << std::endl; - exit(1); - } + return (2); // function that will parse the port from the host // and check if the host is valid or not if (this->get_header("Host") == "") - { - std::cerr << "Invalid Host header" << std::endl; - exit(1); - } + return (2); ft_parse_port(this->get_header("Host")); - // ft_parse_language_charset(); // Parse the request body std::string content_len_str = this->get_header("Content-Length"); @@ -188,38 +171,37 @@ void request::parse_request(std::string request) { size_t content_len = std::stoi(content_len_str); if (content_len > request.size()) - { - std::cerr << "Invalid Content-Length" << std::endl; - exit(1); - } + return (2); this->_body = request.substr(request.size() - content_len); - // std::cout << "S T A R T O F REQUEST B O D Y" << std::endl; - // std::cout << this->_body << std::endl; - // std::cout << "E N D O F REQUEST B O D Y" << std::endl; } else { if (content_len_str == "" || this->_method == "POST") - { - set_body(""); - return ; - } + return (2); } -} - -void request::print_request() -{ - std::cout << "Method: " << this->_method << std::endl; - std::cout << "URI: " << this->_uri << std::endl; - std::cout << "Version: " << this->_version << std::endl; - std::cout << "Headers:" << std::endl; - for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) - std::cout << it->first << ": " << it->second << std::endl; - std::cout << "Body: " << this->_body << std::endl; + return (0); } std::string request::get_boundary() const { return (this->_boundary); +} + +// change the "%20" with a space +void request::handleSpecialCharacters(std::string& uri) { + std::string encodedChars[] = {"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", "%28", "%29", "%2A", "%2B", "%2C", + "%2D", "%2E", "%2F", "%3A", "%3B", "%3C", "%3D", "%3E", "%3F", "%40", "%5B", "%5C", "%5D", + "%5E", "%5F", "%60", "%7B", "%7C", "%7D", "%7E"}; + + std::string specialChars[] = {" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", + "=", ">", "?", "@", "[", "\\", "]", "^", "_", "`", "{", "|", "}", "~"}; + + for (size_t i = 0; i < sizeof(encodedChars) / sizeof(encodedChars[0]); i++) { + std::string::size_type n = 0; + while ((n = uri.find(encodedChars[i], n)) != std::string::npos) { + uri.replace(n, encodedChars[i].length(), specialChars[i]); + n += 1; + } + } } \ No newline at end of file diff --git a/request/request.hpp b/request/request.hpp index ba823e1..e787a18 100644 --- a/request/request.hpp +++ b/request/request.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* request.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: yismaili +#+ +:+ +#+ */ +/* By: aoumad +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/26 18:00:51 by aoumad #+# #+# */ -/* Updated: 2023/05/14 16:01:47 by yismaili ### ########.fr */ +/* Updated: 2023/05/21 19:11:36 by aoumad ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,10 +27,14 @@ # include # include +# include "../respond/respond.hpp" + +class Respond; class request { private: + std::string _init_request; std::string _method; // stores http method of the request (e.g "GET", "POST"..etc) std::string _uri; // stores the URI of the request (e.g "/index.html") std::string _version; // stores the http version of the request (e.g "HTTP/1.1") @@ -68,7 +72,7 @@ class request void add_header(std::string key, std::string value); std::string get_header(std::string key) const; std::map get_headers() const; - void parse_request(std::string request); + int parse_request(); typedef void (request::*encoding_handler)(std::string &body); @@ -84,6 +88,7 @@ class request void ft_parse_language_charset(); void print_request(); void init_parse(); + void handleSpecialCharacters(std::string& uri); }; int ft_check_request_line(std::string method, std::string uri, std::string version); diff --git a/request/request_utils.cpp b/request/request_utils.cpp index 617dc28..fa97d1e 100644 --- a/request/request_utils.cpp +++ b/request/request_utils.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* request_utils.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: yismaili +#+ +:+ +#+ */ +/* By: aoumad +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/28 21:18:13 by aoumad #+# #+# */ -/* Updated: 2023/05/15 20:59:53 by yismaili ### ########.fr */ +/* Updated: 2023/05/22 19:18:22 by aoumad ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,8 @@ int ft_check_request_line(std::string method, std::string uri, std::string version) { - // std::cout << "Method: " << method << std::endl; if (method != "GET" && method != "POST" && method != "DELETE") - { - if (method != "PUT" || method == "OPTIONS" || method == "TRACE" || method != "HEAD") - std::cerr << "Method not implemented" << std::endl; return (0); - } if (uri[0] != '/') return (0); if (version != "HTTP/1.1") @@ -38,7 +33,6 @@ void request::ft_parse_port(std::string host) { port = host.substr(pos + 1); this->_port = atoi(port.c_str()); - // this->_host = host.substr(0, pos); } else this->_port = 80; diff --git a/request/task.txt b/request/task.txt index f2ac5bc..f7ed84d 100644 --- a/request/task.txt +++ b/request/task.txt @@ -1,17 +1,2 @@ -User-Agent -Date -Cache-Control -Server -Location (if snouae didn't handle it) -Accept - -// respond headers -Content-Type -Content-Length -Date -Location -Cache-Control -Connection -Content-Disposition -Allow -Server ==> webserv 1.1 \ No newline at end of file +/*********************************/ +1- std::cout << "Unable to open file" << std::endl; in init_response_body \ No newline at end of file diff --git a/respond/additional_class.hpp b/respond/additional_class.hpp index 580cd68..4075eb8 100644 --- a/respond/additional_class.hpp +++ b/respond/additional_class.hpp @@ -9,7 +9,7 @@ class FormData std::string file_name; bool isValid() const { - return (!name.empty() && !data.empty()); + return (!name.empty() && !data.empty() && !file_name.empty()); } // getters std::string get_name() const diff --git a/respond/method_handling.cpp b/respond/method_handling.cpp index d3a521c..ac517f0 100644 --- a/respond/method_handling.cpp +++ b/respond/method_handling.cpp @@ -3,14 +3,15 @@ /* ::: :::::::: */ /* method_handling.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: yismaili +#+ +:+ +#+ */ +/* By: aoumad +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/09 17:52:50 by aoumad #+# #+# */ -/* Updated: 2023/05/19 12:36:33 by yismaili ### ########.fr */ +/* Updated: 2023/05/22 19:23:01 by aoumad ### ########.fr */ /* */ /* ************************************************************************** */ #include "respond.hpp" +# include void Respond::handle_get_response(std::vector servers) { @@ -20,19 +21,12 @@ void Respond::handle_get_response(std::vector servers) run_cgi(r, *this); return ; } - // std::cout << "___---222222__________@@@@@-______-" << std::endl; // step 3: check if it's a file or not if (ft_check_file() == true) { - ft_handle_file(); + ft_handle_file(servers); return ; } - // else - // { - // std::cout << "___--_------__------_-_-_--_-__-_-_-_-HEREEEEE_--_-_-_--_-_-_-_-" << std::endl; - // handle_error_response(404); - // return ; - // } // step 4 : check the index in the configuration file and render it int rtn_index = ft_handle_index(servers); if (rtn_index == 0) @@ -41,16 +35,15 @@ void Respond::handle_get_response(std::vector servers) { if (ft_handle_autoindex(servers)) { - handle_error_response(403); + handle_error_response(servers, 403); return ; } } else if (rtn_index == 2) { - handle_error_response(404); + handle_error_response(servers, 404); return ; } - // step 5: check if the autoindex if on or off } @@ -59,13 +52,13 @@ void Respond::handle_post_response(std::vector server) // step 1: check if the request body is empty or not if (r.get_body().empty()) { - set_response_body("Body request is missing"); + handle_error_response(server, 400); return ; } if (_is_cgi == false && (server[_server_index]._location[_location_index].get_upload_store().empty() && server[_server_index]._location[_location_index].get_upload() == false)) return ; _upload_store_path = _rooted_path; - _upload_store.append(_upload_store); + _upload_store = server[_server_index]._location[_location_index].get_upload_store(); struct stat st; if (stat(_upload_store_path.c_str(), &st) != 0) { @@ -76,21 +69,31 @@ void Respond::handle_post_response(std::vector server) { if (_is_cgi == true) { - // khasni n7t query homa request body run_cgi(r, *this); + return ; } else { - // need to create a file that has `Key` as it's name and the content of it as `value` handle_urlencoded(); create_decode_files(); + std::string path = r.get_uri(); + std::string::size_type i = r.get_uri().find_last_of('/'); + if (i != std::string::npos) + path = r.get_uri().substr(i); + init_response_body(server, path, server[_server_index]._location[_location_index].get_root()); + return ; } } if (check_post_type() == "form-data") { - handle_form_data(); + handle_form_data(server); + std::string path = r.get_uri(); + std::string::size_type i = r.get_uri().find_last_of('/'); + if (i != std::string::npos) + path = r.get_uri().substr(i); + init_response_body(server, path, server[_server_index]._location[_location_index].get_root()); + return ; } - } void Respond::handle_urlencoded() @@ -99,13 +102,7 @@ void Respond::handle_urlencoded() Url_encoded encoded_form; std::string::size_type pos = 0; std::string::size_type end_pos = 0; - size_t index = 0; - while ((index = line.find("%20", index)) != std::string::npos) - { - std::cout << index << std::endl; - line = line.substr(0, index) + " " + line.substr(index + 3); - index += 1; // Move past the inserted space - } + r.handleSpecialCharacters(line); while (pos != std::string::npos) { end_pos = line.find('&', pos); @@ -126,7 +123,6 @@ void Respond::handle_urlencoded() void Respond::create_decode_files() { - std::string path = _upload_store_path; std::string file_name; std::string file_content; std::ofstream file; @@ -134,8 +130,8 @@ void Respond::create_decode_files() while (it != _url_decode.end()) { - file_name = path; - file_name.append(it->key); + file_name = _upload_store; + file_name += "/" + it->key; file.open(file_name.c_str()); file << it->value; file.close(); @@ -143,7 +139,7 @@ void Respond::create_decode_files() } } -void Respond::handle_form_data() +void Respond::handle_form_data(std::vector server) { // std::cout << r.get_body() << std::endl; // Find the first boundary @@ -159,7 +155,12 @@ void Respond::handle_form_data() break; // Read the data between the boundaries - FormData formData = read_form_data(pos); // escape /r/n + FormData formData = read_form_data(server, pos); // escape /r/n + if (_file_too_large == true) + { + handle_error_response(server, 413); + return ; + } if (formData.isValid()) _form_data.push_back(formData); // Add the form data to the list // std::cout << "pos before: " << pos << std::endl; @@ -167,24 +168,11 @@ void Respond::handle_form_data() break; pos += _boundary.length() + 2; } - // iterat over formData class and print it's attributes - // std::vector::iterator it = _form_data.begin(); - // while (it != _form_data.end()) - // { - // std::cout << "name: " << it->get_name() << std::endl; - // std::cout << "filename: " << it->get_file_name() << std::endl; - // std::cout << "content-type: " << it->get_content_type() << std::endl; - // std::cout << "data: " << it->get_data() << " END"<< std::endl; - // it++; - //} - // } - create_form_data(); } void Respond::create_form_data() { - std::string path = _upload_store_path; std::string file_name; std::string file_content; std::ofstream file; @@ -197,8 +185,8 @@ void Respond::create_form_data() it++; continue; } - file_name = path; - file_name.append(it->get_file_name()); + file_name = _upload_store; + file_name += "/" + it->get_file_name(); file.open(file_name.c_str()); file << it->get_data(); file.close(); @@ -211,7 +199,7 @@ size_t Respond::find_boundary(size_t pos) return (r.get_body().find(_boundary, pos)); } -FormData Respond::read_form_data(size_t pos) +FormData Respond::read_form_data(std::vector servers ,size_t pos) { FormData form_data; std::string line; @@ -226,7 +214,10 @@ FormData Respond::read_form_data(size_t pos) std::string last_boundary = _boundary + "--"; size_t end = r.get_body().find(_boundary, start); if (end == std::string::npos) - return form_data; // Boundary not found + { + std::cout << form_data.get_data() << std::endl; + return (form_data); // Boundary not found + } size_t end_last = r.get_body().find(last_boundary, start); if (end_last != std::string::npos && end_last == end) @@ -259,33 +250,15 @@ FormData Respond::read_form_data(size_t pos) // Process the data content std::string data_content = section.substr(header_end + 4); // Skip the CRLF delimiter + std::cout << (unsigned int)servers[_server_index].get_client_max_body_size() << std::endl; + std::cout << servers[_server_index].get_client_max_body_size() << std::endl; + if (data_content.length() * 8 >= (unsigned int)servers[_server_index].get_client_max_body_size()) + _file_too_large = true; form_data.data = data_content; return (form_data); } -/* -POST /example HTTP/1.1 -Host: example.com -Content-Type: multipart/form-data; boundary=--------------------------1234567890 - -----------------------------1234567890 -Content-Disposition: form-data; name="username" - -johndoe -----------------------------1234567890 -Content-Disposition: form-data; name="profile_pic"; filename="profile.jpg" -Content-Type: image/jpeg - -_root path + path_found + upload_store -----------------------------1234567890 -Content-Disposition: form-data; name="notes"; filename="notes.txt" -Content-Type: text/plain - -(Here goes the text content of the notes file) -----------------------------1234567890-- -*/ - std::string Respond::check_post_type() { if(r.get_header("Content-Type").find("multipart/form-data") != std::string::npos) @@ -296,10 +269,10 @@ std::string Respond::check_post_type() return ("regular"); } -void Respond::handle_delete_response() +void Respond::handle_delete_response(std::vector server) { - // std::cout << "DKHLAAAAAAAAAAT" << std::endl; - // std::cout << "rooted path:" << _rooted_path << std::endl; + std::cout << "DKHLAAAAAAAAAAT" << std::endl; + std::cout << "rooted path:" << _rooted_path << std::endl; if (std::remove(_rooted_path.c_str()) == 0) { _status_code = 200; @@ -310,11 +283,6 @@ void Respond::handle_delete_response() } else { - _status_code = 500; - _status_message = get_response_status(_status_code); - // set_response_body("Error deleting file"); - set_header("Content-Type", r.get_header("Content-Type")); - set_header("Content-Length", std::to_string(_response_body.length())); - set_header("Connection", "keep-alive"); + handle_error_response(server, 409); } } \ No newline at end of file diff --git a/respond/method_utils.cpp b/respond/method_utils.cpp index d31c225..6bd24da 100644 --- a/respond/method_utils.cpp +++ b/respond/method_utils.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* method_utils.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: yismaili +#+ +:+ +#+ */ +/* By: aoumad +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/11 02:14:39 by aoumad #+# #+# */ -/* Updated: 2023/05/19 12:36:58 by yismaili ### ########.fr */ +/* Updated: 2023/05/22 13:44:24 by aoumad ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ int Respond::ft_check_file() return (0); } -void Respond::ft_handle_file() +void Respond::ft_handle_file(std::vector server) { std::string::size_type mime_index = _rooted_path.find_last_of('.'); if (mime_index != std::string::npos) @@ -33,7 +33,7 @@ void Respond::ft_handle_file() std::ifstream file; if (!strcmp(_rooted_path.c_str(), "")) { - handle_error_response(404); + handle_error_response(server, 404); return; } file.open(_rooted_path.c_str()); @@ -45,14 +45,14 @@ void Respond::ft_handle_file() set_status_message(get_response_status(200)); set_header("Content-Type", get_mime_type(_mime_string)); // std::cout << "mime _type: " << get_mime_type(_mime_string) << std::endl; - _headers["Content-Length"] = std::to_string(_response_body.length()); + _headers["Content-Length"] = std::to_string(_response_body.length()); // std::cout << std::to_string(_response_body.length()) << std::endl; _headers["Connection"] = "keep-alive"; set_date(); - set_cache_control("cache"); + set_cache_control("no cache"); } else - handle_error_response(403); + handle_error_response(server, 403); } /* Here's how it works: @@ -65,34 +65,6 @@ In other words, the std::string constructor reads the entire contents of the fil int Respond::ft_handle_index(std::vector server) { std::string index; - // if (server[_server_index]._location[_location_index].get_index().empty()) - // { - // if (server[_server_index].get_index().empty()) - // { - // handle_error_response(403); - // return (1); - // } - // else - // { - // index = server[_server_index].get_index(); - // std::string file = server[_server_index].get_root() + "/" + index; - // _rooted_path = server[_server_index]._location[_location_index].get_root() + _removed_path + index; - // if (ft_handle_index_2(file)) - // return (1); - // } - // } - // else - // { - // index = server[_server_index]._location[_location_index].get_index(); - - // std::string::size_type _mime_index= index.find_last_of('.'); - // if (_mime_index != std::string::npos) - // _mime_string = index.substr(_mime_index + 1); - // std::string file = server[_server_index]._location[_location_index].get_root() + "/" + index; - // _rooted_path = server[_location_index].get_root() + _removed_path + index; - // if (ft_handle_index_2(file)) - // return (1); - // } if (server[_server_index]._location[_location_index].location_name == "/") { if (ft_check_location_index(server)) @@ -107,7 +79,7 @@ int Respond::ft_handle_index(std::vector server) _mime_string = index.substr(_mime_index + 1); std::string file = server[_server_index].get_root() + "/" + index; _rooted_path = server[_server_index]._location[_location_index].get_root() + _removed_path + index; - if (ft_handle_index_2(file)) + if (ft_handle_index_2(server, file)) return (1); } } @@ -118,8 +90,8 @@ int Respond::ft_handle_index(std::vector server) if (_mime_index != std::string::npos) _mime_string = index.substr(_mime_index + 1); std::string file = server[_server_index]._location[_location_index].get_root() + "/" + index; - _rooted_path = server[_location_index].get_root() + _removed_path + index; - if (ft_handle_index_2(file)) + _rooted_path = server[_server_index].get_root() + _removed_path + index; + if (ft_handle_index_2(server, file)) return (2); } } @@ -134,8 +106,8 @@ int Respond::ft_handle_index(std::vector server) if (_mime_index != std::string::npos) _mime_string = index.substr(_mime_index + 1); std::string file = server[_server_index]._location[_location_index].get_root() + "/" + index; - _rooted_path = server[_location_index].get_root() + _removed_path + index; - if (ft_handle_index_2(file)) + _rooted_path = server[_server_index].get_root() + _removed_path + index; + if (ft_handle_index_2(server, file)) return (2); } } @@ -156,7 +128,7 @@ int Respond::ft_check_server_index(std::vector server) return (0); } -int Respond::ft_handle_index_2(std::string index) +int Respond::ft_handle_index_2(std::vector server, std::string index) { std::ifstream file; if (index != "") @@ -168,7 +140,6 @@ int Respond::ft_handle_index_2(std::string index) set_status_code(200); set_status_message(get_response_status(200)); set_header("Content-Type", get_mime_type(_mime_string)); - // std::cout << "mime _type in index: " << get_mime_type(_mime_string) << std::endl; _headers["Content-Length"] = std::to_string(_response_body.length()); _headers["Connection"] = "keep-alive"; set_date(); @@ -177,14 +148,13 @@ int Respond::ft_handle_index_2(std::string index) } else { - //std::cout << "____WWWWW_--------______" << std::endl; - handle_error_response(404); + handle_error_response(server, 404); return (1); } } else { - handle_error_response(404); + handle_error_response(server, 404); return (1); } return (0); @@ -194,14 +164,11 @@ int Respond::ft_handle_autoindex(std::vector server) { if (_path_found == server[_server_index]._location[_location_index].location_name) { - // std::cout << "path found: " << _path_found << std::endl; if (!server[_server_index]._location[_location_index].get_autoindex()) return (1); else { - // std::cout << "___--_------__------_-_-_--_-__-_-_-_-HEREEEEE_--_-_-_--_-_-_-_-" << std::endl; - // std::cout << "rooted path: " << _rooted_path << std::endl; - ft_show_autoindex(); + ft_show_autoindex(server); return (0); } } @@ -220,10 +187,8 @@ void Respond::ft_handle_error(int error_code) set_cache_control("no cache"); } -void Respond::ft_show_autoindex() +void Respond::ft_show_autoindex(std::vector server) { - // if(!check_location) - // _uri = _uri + r.get_uri(); std::string index_html = "\n\n\n"; index_html += "\n"; index_html += "Index of " + _rooted_path + "\n"; @@ -237,7 +202,7 @@ void Respond::ft_show_autoindex() if (dir == NULL) { - handle_error_response(403); + handle_error_response(server, 403); return ; } @@ -251,19 +216,32 @@ void Respond::ft_show_autoindex() if (entry->d_name[0] != '.') { file_name = std::string(entry->d_name); - std::string file_path = _rooted_path + "/" + file_name; + std::string file_path; + if (_path_found[_path_found.size() - 1] == '/') + file_path = _path_found + file_name; + else + file_path = _path_found + "/" + file_name; + std::string match_path; + if (_rooted_path[_rooted_path.size() - 1] == '/') + match_path = _rooted_path + file_name; + else + match_path = _rooted_path + "/" + file_name; - if (stat(file_path.c_str(), &file_stat) < 0) + if (stat(match_path.c_str(), &file_stat) < 0) { - handle_error_response(403); + handle_error_response(server, 403); continue ; } - file_size = std::to_string(file_stat.st_size); - index_html += ""; - index_html += "" + file_name + ""; - index_html += "" + file_size + ""; - index_html += "\n"; + time_t entryTime = file_stat.st_mtime; // Assuming 'time' is of type 'time_t' + std::tm* timeInfo = std::localtime(&entryTime); + char buffer[80]; + std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeInfo); + std::string fileTime(buffer); + + // Now you can use 'fileTime' in your HTML code + index_html += "

" + file_name + ""; + index_html += "\t\t " + file_size + "\t\t" + fileTime + "

\n"; } } closedir(dir); @@ -271,18 +249,35 @@ void Respond::ft_show_autoindex() _response_body = index_html; } -void Respond::handle_error_response(int error_code) +void Respond::handle_error_response(std::vector server, int error_code) { - set_status_code(error_code); - set_status_message(get_response_status(error_code)); - set_header("Content-Type", "text/html"); - set_header("Connection", "keep-alive"); - set_date(); - set_last_modified(); - _response_body = "" + std::to_string(error_code) + " " + _status_message + "

" + std::to_string(error_code) + " " + _status_message + "

You don't have permission to access " + r.get_uri() + " on this server.

"; - set_header("Content-Length", std::to_string(_response_body.length())); - - print_response(); + std::map error_page; + error_page = server[_server_index].get_error_page(); + if (error_page.find(error_code) != error_page.end()) + { + std::string error_path = error_page[error_code]; + std::ifstream file; + file.open(error_path.c_str()); + if (file.is_open()) + { + _response_body = std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + set_header("Content-Type", get_mime_type("html")); + set_header("Content-Length", std::to_string(_response_body.length())); + set_cache_control("no cache"); + } + // file.close(); + } + else + { + set_status_code(error_code); + set_status_message(get_response_status(error_code)); + set_header("Content-Type", "text/html"); + set_header("Connection", "keep-alive"); + set_date(); + _response_body = "" + std::to_string(error_code) + " " + _status_message + "

" + std::to_string(error_code) + " " + _status_message + "

You don't have permission to access " + r.get_uri() + " on this server.

"; + set_header("Content-Length", std::to_string(_response_body.length())); + set_cache_control("no cache"); + } } void Respond::print_response() diff --git a/respond/pairs_def.cpp b/respond/pairs_def.cpp index 8ce4831..a6387b3 100644 --- a/respond/pairs_def.cpp +++ b/respond/pairs_def.cpp @@ -79,7 +79,7 @@ const std::pair response_status[] = std::make_pair(410, "Gone"), std::make_pair(411, "Length Required"), std::make_pair(412, "Precondition Failed"), - std::make_pair(413, "Payload Too Large"), + std::make_pair(413, "File Too Large"), std::make_pair(414, "URI Too Long"), std::make_pair(415, "Unsupported Media Type"), std::make_pair(416, "Range Not Satisfiable"), diff --git a/respond/respond.cpp b/respond/respond.cpp index fb55434..5b66628 100644 --- a/respond/respond.cpp +++ b/respond/respond.cpp @@ -1,12 +1,3 @@ -/* -example of a basic respond -HTTP/1.1 200 OK\r\n -Content-Type: text/html\r\n -Content-Length: 1234\r\n -\r\n -Hello, world! -*/ - #include "respond.hpp" #include "../request/request.hpp" @@ -34,6 +25,18 @@ Respond::Respond(request& req, int index_) : r(req) _last_boundary = false; _mime_string = ""; _pur_uri = r.get_uri(); + _file_too_large = false; +} + +Respond::Respond(std::vector server, int _index, bool rtn_error, request &req) : _rtn_error(rtn_error), r(req) +{ + _server_index = _index; + _http_version = "HTTP/1.1"; + if (_rtn_error == false) + { + handle_error_response(server, 400); + } + return ; } Respond::~Respond() @@ -75,16 +78,6 @@ void Respond::set_date() _headers["Date"] = buffer; } -void Respond::set_last_modified() -{ - struct stat file_stats; - char buffer[80]; - - stat(_rooted_path.c_str(), &file_stats); - std::strftime(buffer, 80, "%a, %d %b %Y %X %Z", localtime(&file_stats.st_mtime)); - _headers["Last-Modified"] = buffer; -} - std::string Respond::get_http_version() { return (_http_version); @@ -131,14 +124,7 @@ std::string Respond::get_document_root() int Respond::ft_parse_root_path(std::vector server) { struct stat file_stats; - // std::cout << "removed path: " << _removed_path << std::endl; - // std::cout << "get root: " << server[_server_index]._location[_location_index].get_root() << std::endl; - // std::cout << "_uriiii: " << _uri << std::endl; - // if (check_location == false) - _rooted_path = server[_server_index]._location[_location_index].get_root() + _removed_path; - // if (check_location == true) - // _rooted_path = server[_server_index]._location[_location_index].get_root() + _uri; - // std::cout << "rooted path: " << _rooted_path << std::endl; + _rooted_path = server[_server_index]._location[_location_index].get_root() + _removed_path; if (!stat(_rooted_path.c_str(), &file_stats)) { _file_cgi = _rooted_path; @@ -172,7 +158,6 @@ void Respond::set_cache_control(std::string cache) std::string Respond::rtn_response() { std::string response; - response = _http_version + " " + std::to_string(_status_code) + " " + _status_message + "\r\n"; for (std::map::iterator it = _headers.begin(); it != _headers.end(); it++) response += it->first + ": " + it->second + "\r\n"; @@ -181,11 +166,12 @@ std::string Respond::rtn_response() return (response); } -void Respond::init_response_body(std::string file, std::string _root) +void Respond::init_response_body(std::vector server ,std::string file, std::string _root) { std::ifstream file_; std::string line; + _response_body = ""; std::string f; f = _root + "/" + file; file_.open(f); @@ -196,5 +182,5 @@ void Respond::init_response_body(std::string file, std::string _root) file_.close(); } else - std::cout << "Unable to open file" << std::endl; + handle_error_response(server, 404); } \ No newline at end of file diff --git a/respond/respond.hpp b/respond/respond.hpp index 229d69e..a924cbc 100644 --- a/respond/respond.hpp +++ b/respond/respond.hpp @@ -6,7 +6,7 @@ /* By: aoumad +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/28 20:49:15 by aoumad #+# #+# */ -/* Updated: 2023/05/18 12:59:53 by aoumad ### ########.fr */ +/* Updated: 2023/05/22 18:40:32 by aoumad ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,11 +33,13 @@ class server; class location; +class request; class Respond { public: Respond(); + Respond(std::vector server, int _index, bool rtn_error, request &req); Respond(request& req, int index_); ~Respond(); @@ -49,7 +51,6 @@ class Respond void set_response_body(std::string body); std::string get_status_line(const std::string &status_code); void set_date(); - void set_last_modified(); void set_cache_control(std::string control); std::string get_http_version(); @@ -85,20 +86,20 @@ class Respond // GET RESPONSE void ft_handle_cgi(); - void ft_handle_file(); + void ft_handle_file(std::vector server); int ft_handle_autoindex(std::vector servers); void ft_check_cgi(); int ft_check_file(); - int ft_handle_index(std::vector servers); - int ft_handle_index_2(std::string index); - void ft_show_autoindex(); + int ft_handle_index(std::vector server); + int ft_handle_index_2(std::vector server, std::string index); + void ft_show_autoindex(std::vector server); // POST RESPONSE std::string check_post_type(); void handle_post_response(std::vector server); - void handle_form_data(); + void handle_form_data(std::vector server); size_t find_boundary(size_t pos); - FormData read_form_data(size_t pos); + FormData read_form_data(std::vector servers, size_t pos); void handle_urlencoded(); void create_form_data(); @@ -128,6 +129,8 @@ class Respond static std::string _uri; std::string _pur_uri; static bool check_location; + bool _rtn_error; + bool _file_too_large; bool _is_cgi; bool _is_allowed_method; @@ -137,21 +140,55 @@ class Respond void handle_get_response(std::vector servers); void print_response(); - void init_response_body(std::string file, std::string _root); + void init_response_body(std::vector server ,std::string file, std::string _root); request& r; void create_decode_files(); // DELETE RESPONSE - void handle_delete_response(); + void handle_delete_response(std::vector server); // ERROR RESPONSE - void handle_error_response(int error_code); + void handle_error_response(std::vector server, int error_code); void ft_handle_error(int error_code); - // DELETE RESPONSE - // void cout_respond(); }; -#endif \ No newline at end of file +#endif + + // void http_sever ::unchunk(int sockfd) + // { + + + // if (header_error == 1) + // { + // request req; + // header_error = 0; + // Respond res(conf, conf_fd[sockfd]->getIndex() ,false, req); + // requist_data[sockfd] = res.rtn_response(); + // // std::cout<setContent_length(requist_data[sockfd].size() - (header_end + 4)); + // } + // int rtn_error; + // request req(requist_data[sockfd], conf_fd[sockfd]->getContent_length()); + // rtn_error = req.parse_request(); + // if (rtn_error == 2) + // { + // Respond res(conf, conf_fd[sockfd]->getIndex() ,false, req); + // requist_data[sockfd] = res.rtn_response(); + // } + // else if (rtn_error == 0) + // { + // Respond res(req, conf_fd[sockfd]->getIndex()); + // requist_data[sockfd] = res.response_root(conf); + // } + // } + // } \ No newline at end of file diff --git a/respond/respond_root.cpp b/respond/respond_root.cpp index bcf45ed..5d13d6c 100644 --- a/respond/respond_root.cpp +++ b/respond/respond_root.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* respond_root.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: yismaili +#+ +:+ +#+ */ +/* By: aoumad +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/29 14:53:31 by aoumad #+# #+# */ -/* Updated: 2023/05/19 12:33:01 by yismaili ### ########.fr */ +/* Updated: 2023/05/22 19:18:41 by aoumad ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,104 +14,32 @@ std::string Respond::response_root(std::vector servers) { - // std::cout << "method: " << r.get_method() << std::endl; - // std::cout << "boundary: " << r.get_boundary() << std::endl; - // std::cout << "content type: " << r.get_header("Content-Type") << std::endl; - // std::cout << "content length: " << r.get_header("Content-Length") << std::endl; - // std::cout << "request body: " << r.get_body() << std::endl; - // std::cout << "r method: " << r.get_method() << std::endl; - // std::cout << "uri: " << r.get_uri() << std::endl; - init_response_body(servers[_server_index].get_index(), servers[_server_index].get_root()); + int rtn_location = 0; // step 1 :check the location - - - // if(_uri != "") - // r.set_uri(_uri + r.get_uri()); - // _uri = ""; - if (ft_parse_location(servers, false)) + rtn_location = ft_parse_location(servers, false); + if (rtn_location) { - // to avoid reload of the page in case r.get_uri == _uri - // std::cout << "uri: " << _uri << std::endl; - // std::cout << "r.get_uri(): " << r.get_uri() << std::endl; - // std::cout << "check location: " << check_location << std::endl; - std::string::size_type index_uri = _uri.find(r.get_uri()); - if (r.get_uri() != _uri) + if (root_location(servers) == 1) { - std::string::size_type index__uri = _uri.find(r.get_uri()); - if (index__uri == std::string::npos) - { - if (check_location == true && index_uri == std::string::npos) - { - //std::cout << "____-_-_2---_-_--_--_--_--_-_" << std::endl; - _uri = _uri.substr(0, _uri.find_last_of('/')); - _uri += r.get_uri(); - r.set_uri(_uri); - _removed_path = ""; - ft_parse_location(servers, true); - // check_location = false; - std::cout << "_uri: " << _uri << std::endl; - } - else - { - //std::cout << "____-_-_1---_-_--_--_--_--_-_" << std::endl; - _uri += r.get_uri(); - r.set_uri(_uri); - _removed_path = ""; - ft_parse_location(servers, true); - std::cout << "_uri: " << _uri << std::endl; - check_location = true; - } - } - else if (index__uri != std::string::npos) - { - // std::cout << "____-_-_NONE---_-_--_--_--_--_-_" << std::endl; - _removed_path = ""; - ft_parse_location(servers, true); - check_location = true; - } + handle_error_response(servers, 404); + return (rtn_response()); } - else if (index_uri == std::string::npos) - { - // std::cout << "____-_-_2---_-_--_--_--_--_-_" << std::endl; - _uri = _uri.substr(_uri.find_last_of('/')); - _uri += r.get_uri(); - r.set_uri(_uri); - _removed_path = ""; - ft_parse_location(servers, true); - check_location = false; - } - else if (index_uri != std::string::npos) - { - // std::cout << "____-_-_3---_-_--_--_--_--_-_" << std::endl; - check_location = true; - } - // else if (root_location(servers) == 1) - // { - // // if (_uri != "") - // handle_error_response(404); - // return (rtn_response()); - // } - } - else - { - // std::cout << "____-_-_4---_-_--_--_--_--_-_" << std::endl; - _uri = r.get_uri(); - check_location = false; } + else if (rtn_location == 2) + return (rtn_response()); // step 2 : check the redirectation if (!ft_parse_url_forwarding(servers)) return (rtn_response()); // step 3 : check the validation of rooted path if (ft_parse_root_path(servers)) { - handle_error_response(_status_code); + handle_error_response(servers, _status_code); return (rtn_response()); } - // std::cout << "our new uri: " << _uri << std::endl; // step 4 : check the allowed methods if (ft_check_allowed_methods(servers)) { - handle_error_response(_status_code); + handle_error_response(servers, _status_code); return (rtn_response()); } // step 5 : check the autoindex @@ -122,9 +50,9 @@ std::string Respond::response_root(std::vector servers) else if (r.get_method() == "POST") handle_post_response(servers); else if (r.get_method() == "DELETE") - handle_delete_response(); + handle_delete_response(servers); else // unsupported http method - handle_error_response(405); + handle_error_response(servers, 405); // rtn response return (!rtn_response().empty() ? rtn_response() : "ERROR in returning response"); @@ -132,19 +60,15 @@ std::string Respond::response_root(std::vector servers) int Respond::exact_location(std::vector server, std::string path) { - for (size_t j = 0; j < server[_server_index]._location.size(); j++) + for (size_t j = 0; j < server[_server_index]._location.size(); j++) + { + if (server[_server_index]._location[j].location_name == path) { - if (server[_server_index]._location[j].location_name == path) - { - // std::cout << "path :" << path << std::endl; - // std::cout << server[_server_index]._location[j].get_root() << std::endl; - // std::cout << server[_server_index]._location[j].get_index() << std::endl; - - _location_index = j; - _path_found = server[_server_index]._location[j].location_name; - return (0); - } + _location_index = j; + _path_found = server[_server_index]._location[j].location_name; + return (0); } + } return (1); } @@ -183,8 +107,6 @@ int Respond::dynamic_location(std::vector server, std::string path) std::string extension = path.substr(pos); if (!prefix_location(server, path)) { - // std::cout << path << std::endl; - //std::cout << "server index: " << _server_index << " and location index: " << _location_index << std::endl; std::map path_info = server[_server_index]._location[_location_index].get_path_info(); for (std::map::const_iterator it = path_info.begin(); it != path_info.end(); ++it) @@ -204,6 +126,11 @@ int Respond::dynamic_location(std::vector server, std::string path) } } } + if (extension == ".php" || extension == ".py") + { + handle_error_response(server, 403); + return (2); + } } return (1); } @@ -216,8 +143,6 @@ int Respond::root_location(std::vector server) { _location_index = j; _path_found = server[_server_index]._location[j].location_name; - // std::cout << "path found: " << _path_found << std::endl; - // std::cout << "root: " << server[_server_index]._location[j].get_root() << std::endl; return (0); } } @@ -227,23 +152,24 @@ int Respond::root_location(std::vector server) int Respond::ft_parse_location(std::vector server, bool flag) { std::string path = ""; + int rtn_cgi = 0; if (flag == false) path = r.get_uri(); else if (flag == true) path = _uri; - // std::cout << "___--_-______-_---______--__---____---_____---------__---_------------" << std::endl; // exact location body code if (exact_location(server, path) == 0) return (0); - // regex location body code - if (dynamic_location(server, path) == 0) + rtn_cgi = dynamic_location(server, path); + if (rtn_cgi == 0) return (0); + else if (rtn_cgi == 2) + return (2); _removed_path = ""; // prefix location body code if (prefix_location(server, path) == 0) return (0); - return (1); } @@ -253,11 +179,6 @@ int Respond::ft_parse_url_forwarding(std::vector server) { if (_path_found == server[_server_index]._location[j].location_name) { - // check for redirection ===== where redirection is make_pair - // std::cout << "___________________-----_______---________----_____--___--__-_-_-_--_--" << std::endl; - // std::cout << server[i]._location[j].get_redirection().first << std::endl; - // std::cout << server[i]._location[j].get_redirection().second << std::endl; - // std::cout << "___________________-----_______---________----_____--___--__-_-_-_--_--" << std::endl; if (!server[_server_index]._location[j].get_redirection().second.empty()) { size_t status_code = server[_server_index]._location[j].get_redirection().first; @@ -265,7 +186,7 @@ int Respond::ft_parse_url_forwarding(std::vector server) set_status_code(status_code); set_status_message(get_response_status(status_code)); set_header("Location", server[_server_index]._location[j].get_redirection().second); - set_cache_control("cache"); + set_cache_control("no cache"); _is_redirection = true; return (0); } @@ -278,8 +199,6 @@ int Respond::ft_check_allowed_methods(std::vector server) { if (_path_found == server[_server_index]._location[_location_index].location_name) { - // get the autoindex - // _autoindex = server[i]._location[j].autoindex; // check for allowed methods std::vector allowed_methods = server[_server_index]._location[_location_index].get_allow_methods(); for (size_t k = 0; k < allowed_methods.size(); k++) diff --git a/server/http_server.cpp b/server/http_server.cpp index 3ea850d..aabfea4 100644 --- a/server/http_server.cpp +++ b/server/http_server.cpp @@ -6,7 +6,7 @@ /* By: yismaili +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/04 18:41:23 by yismaili #+# #+# */ -/* Updated: 2023/05/19 14:21:58 by yismaili ### ########.fr */ +/* Updated: 2023/05/22 22:51:03 by yismaili ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,10 +21,19 @@ namespace http{ { for (size_t j = 0; j < conf_[i]._listen.size(); j++) { - socket_id.push_back(sock.init_data(conf_[i]._listen[j], conf_[i].get_host(), i)); + socket_id.push_back(sock.init_data(conf_[i]._listen[j], conf_[i].get_host(), conf_[i].get_server_name(), i)); + port.push_back(conf_[i]._listen[j]); + host.push_back(conf_[i].get_host()); + std::vector::iterator it = conf_[i]._server_name.begin(); + while (it != conf_[i]._server_name.end()) + { + servers_names.push_back(*it); + it++; + } } } - conf = conf_; + conf = conf_; + flag = false; } http_sever::~http_sever() @@ -57,6 +66,62 @@ namespace http{ gettimeofday(¤t_time, NULL); return (current_time.tv_sec * 1000 + current_time.tv_usec / 1000); } + + int http_sever::ifport_dup(int port_) + { + std::vector::iterator it; + int check = 0; + for (it = port.begin(); it != port.end(); ++it) + { + if (port_ == (*it)) + { + check++; + } + } + if (check != 1 && check != 0) + { + return (1); + } + return (0); + } + + int http_sever::ifserver_dup(std::string server_name) + { + std::vector::iterator it; + int check = 0; + it = servers_names.begin(); + for (it = servers_names.begin(); it != servers_names.end(); ++it) + { + if (server_name == (*it)) + { + check++; + } + } + if (check != 1 && check != 0) + { + return (1); + } + return (0); + } + + int http_sever::ifhost_dup(std::string host_) + { + std::vector::iterator it; + int check = 0; + + for (it = host.begin(); it != host.end(); ++it) + { + if (host_ == (*it)) + { + check++; + } + } + if (check != 1 && check != 0) + { + return (1); + } + return (0); + } void http_sever::run() { @@ -77,18 +142,18 @@ namespace http{ while (true) { i = 0; - // check_rev = 0; // Wait for events on any of the monitored file descriptors - // static int j = 0; poll_ret = poll(&clients[0], clients.size(), 0); //Check for events on server socket while (i < clients.size()) { - if (!is_server(clients[i].fd) && requist_data[clients[i].fd].size() > 0) + if (!is_server(clients[i].fd) && requist_data[clients[i].fd].size() > 0 && conf_fd[clients[i].fd]->data_issending == 0) { if (getTime() - conf_fd[clients[i].fd]->getTime_out() >= 10000) { + // std::cout<<"hey.....i am time out .\n"; header_error = 1; + conf_fd[clients[i].fd]->data_issending = 1; unchunk(clients[i].fd); clients[i].events = POLLOUT; } @@ -106,8 +171,7 @@ namespace http{ { // Accept incoming connection new_socket = accept_connection(clients[i].fd); - int val = fcntl(new_socket, F_GETFL, 0); - fcntl(new_socket, F_SETFL, val | O_NONBLOCK); + fcntl(new_socket, F_SETFL, O_NONBLOCK); conf_fd.insert(std::make_pair(new_socket, find_conf(clients[i].fd))); std::cout <<"\n\033[32mCONNECTION TO ["<getPort()<<"] "<<"ACCEPTED...\033[0m\n"; // Add new socket to poll list @@ -121,15 +185,10 @@ namespace http{ else { header_error = 0; + recv_ret = recv_data(clients[i].fd); if (recv_ret == -2) { - // std::map::iterator it_ = requist_data.find(clients[i].fd); - // requist_data.erase(it_); - // close(clients[i].fd); - // std::vector::iterator it = clients.begin() + i; - // clients.erase(it); - // i--; header_error = 1; unchunk(clients[i].fd); clients[i].events = POLLOUT; @@ -139,23 +198,29 @@ namespace http{ unchunk(clients[i].fd); clients[i].events = POLLOUT; } + else if (recv_ret == -3) + { + std::map::iterator it_ = requist_data.find(clients[i].fd); + requist_data.erase(it_); + close(clients[i].fd); + std::vector::iterator it = clients.begin() + i; + clients.erase(it); + i--; + } } } else if (clients[i].revents & POLLOUT && read_info[clients[i].fd] == true) { - //std::size_t Connection = requist_data[clients[i].fd].find("Connection: keep-alive"); + //std::cout<<"hey ... i am in send function\n"; std::vector::iterator it = clients.begin() + i; std::map::iterator it_read = read_info.find(clients[i].fd); sent_ret = send_data(clients[i].fd); - if (sent_ret == 1){ + if (sent_ret == 1) + { clients[i].events = POLLOUT; } if (sent_ret == 0) { - // if (Connection == std::string::npos) - // { - // close(clients[i].fd); - // } close(clients[i].fd); clients.erase(it); read_info.erase(it_read); @@ -224,10 +289,6 @@ namespace http{ int http_sever ::transfer_encoding_chunked(int sockfd) { - // std::size_t content_length = requist_data[sockfd].find("Content-Length: "); - // std::size_t transfer_encoding = requist_data[sockfd].find("Transfer-Encoding: chunked"); - // std::size_t post_method = requist_data[sockfd].find("GET"); - if (((content_length == std::string::npos && transfer_encoding == std::string::npos ) || (content_length != std::string::npos && transfer_encoding != std::string::npos )) && post_method != std::string::npos) { @@ -266,18 +327,16 @@ namespace http{ content_len = 0; conf_fd[sockfd]->setContent_length(0); conf_fd[sockfd]->setTime_out(getTime()); + conf_fd[sockfd]->data_issending = 0; bytes_received = recv(sockfd, buffer, sizeof(buffer) - 1, 0); if (bytes_received <= 0) { - close(sockfd); - return (-2); + return (-3); } requist_data[sockfd].append(std::string(buffer, bytes_received)); header_end = requist_data[sockfd].find("\r\n\r\n"); if (header_end != std::string::npos) { - // std::size_t content_length = requist_data[sockfd].find("Content-Length: "); - // std::size_t transfer_encoding = requist_data[sockfd].find("Transfer-Encoding: chunked"); ret_parce = parse_header(requist_data[sockfd], sockfd); if (ret_parce == -2 ) { @@ -286,9 +345,6 @@ namespace http{ ret_transfer = transfer_encoding_chunked(sockfd); if (ret_transfer == 2) { - // header_end = requist_data[sockfd].find("\r\n\r\n"); - // content_len = std::strtol(requist_data[sockfd].substr(requist_data[sockfd].find("Content-Length: ") + 16, 9).c_str(), nullptr, 0); - // conf_fd[sockfd]->setContent_length(content_len); if ((content_len + header_end + 4) <= requist_data[sockfd].size()) { read_info[sockfd] = true; @@ -318,27 +374,83 @@ namespace http{ } return (1); } - - void http_sever ::unchunk(int sockfd) + + void print(std::string str) { + for(std::string::iterator it = str.begin(); it != str.end(); it++) + { + if (*it == '\r') + std::cout << "\\r"; + else if (*it == '\n') + std::cout << "\\n" << std::endl; + else + std::cout << *it; + } + } + + void http_sever ::setIndexOfserver(int sockfd) + { + std::vector::iterator it = conf_fd[sockfd]->server_name.begin(); + while (it != conf_fd[sockfd]->server_name.end()) + { + if (ifhost_dup(conf_fd[sockfd]->ip_addr) && ifport_dup(conf_fd[sockfd]->getPort()) && !ifserver_dup(*it)) + { + int host_index = requist_data[sockfd].find("Host") + 6; + int host_end = requist_data[sockfd].find("\r\n", host_index); + std::string cleint_host = requist_data[sockfd].substr(0, host_end); + host_index = cleint_host.find("Host") + 6; + cleint_host = cleint_host.substr(host_index, cleint_host.size()); + for (size_t i = 0; i < conf.size(); i++) + { + for (size_t j = 0; j < conf[i]._listen.size(); j++) + { + if (!std::strcmp(cleint_host.c_str(), conf[i]._server_name[j].c_str())) + { + conf_fd[sockfd]->setIndex(i); + flag = true; + } + } + } + } + it++; + } + } + + void http_sever ::unchunk(int sockfd) + { + if (flag == true) + { + conf_fd[sockfd]->setIndex(conf_fd[sockfd]->getIndex_tmp()); + } + setIndexOfserver(sockfd); if (header_error == 1) { - request req(requist_data[sockfd], conf_fd[sockfd]->getContent_length()); - Respond res(req, conf_fd[sockfd]->getIndex()); - requist_data[sockfd] = res.response_root(conf); + request req; + header_error = 0; + Respond res(conf, conf_fd[sockfd]->getIndex() ,false, req); + requist_data[sockfd] = res.rtn_response(); + read_info[sockfd] = true; } else { - // std::size_t Transfer_encoding = requist_data[sockfd].find("Transfer-Encoding: chunked"); if (transfer_encoding != std::string::npos && transfer_encoding < header_end) { requist_data[sockfd] = join_chunked(requist_data[sockfd], sockfd); - // std::size_t header_end = requist_data[sockfd].find("\r\n\r\n"); conf_fd[sockfd]->setContent_length(requist_data[sockfd].size() - (header_end + 4)); } + int rtn_error; request req(requist_data[sockfd], conf_fd[sockfd]->getContent_length()); - Respond res(req, conf_fd[sockfd]->getIndex()); - requist_data[sockfd] = res.response_root(conf); + rtn_error = req.parse_request(); + if (rtn_error == 2) + { + Respond res(conf, conf_fd[sockfd]->getIndex() ,false, req); + requist_data[sockfd] = res.rtn_response(); + } + else if (rtn_error == 0) + { + Respond res(req, conf_fd[sockfd]->getIndex()); + requist_data[sockfd] = res.response_root(conf); + } } } @@ -349,13 +461,11 @@ namespace http{ std::string body = ""; std::string chunks = ""; std::string subchunk = ""; - // std::size_t header_end; std::size_t pos; - // header_end = data.find("\r\n\r\n"); result.append(data.substr(0, header_end)); result.append("\r\n\r\n"); - chunks = data.substr(data.find("\r\n\r\n") + 4, data.size() - 1); + chunks = data.substr(header_end + 4, data.size() - 1); subchunk = chunks.substr(0, 9); sizeof_chunk = std::strtol(subchunk.c_str(), NULL, 16); pos = 0; @@ -419,6 +529,7 @@ namespace http{ static std::map sent_data; std::string data_to_send; long bytes_sent; + static int check = 0; data_to_send = requist_data[socket].substr(sent_data[socket], 100024); bytes_sent = send(socket, data_to_send.c_str(), data_to_send.size(), 0); @@ -433,19 +544,24 @@ namespace http{ else { // Update the amount of data that has been sent to the socket + conf_fd[socket]->data_issending = 1; sent_data[socket] += bytes_sent; // If all data has been sent, erase the request information and return 0 if (sent_data[socket] >= requist_data[socket].size()) { sent_data[socket] = 0; - std::cout << "\n\033[33mRESPONSE SENDED TO [" << conf_fd[socket]->getPort() << "]...\033[0m" << std::endl; + std::cout << "\n\033[33mRESPONSE SENDED TO [" << conf_fd[socket]->getPort() << "]\033[0m" << std::endl; std::map::iterator it = requist_data.find(socket); requist_data.erase(it); + conf_fd.erase(socket); return (0); } else { + if (check == 0) + std::cout << "\n\033[33mRESPONSE SENDING TO [" << conf_fd[socket]->getPort() << "]...\033[0m" << std::endl; // If there is still data to send, return 1 + check = 1; return (1); } } @@ -458,7 +574,6 @@ namespace http{ if (sockfd_client < 0) { std::cout << "\033[31mError: accepting connection\033[0m\n"; - exit(1); } return (sockfd_client); } diff --git a/server/sockets.cpp b/server/sockets.cpp index 9a0d560..f50ba69 100644 --- a/server/sockets.cpp +++ b/server/sockets.cpp @@ -6,7 +6,7 @@ /* By: yismaili +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/04 18:41:42 by yismaili #+# #+# */ -/* Updated: 2023/05/19 14:22:53 by yismaili ### ########.fr */ +/* Updated: 2023/05/22 22:48:44 by yismaili ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,38 +23,37 @@ namespace http{ index = 0; content_length = 0; time_out = 0; - header_error = 0; + header_error = 0; + data_issending = 0; } sockets::~sockets() { } - //Assign a port to socket - sockets &sockets::init_data(int port_, std::string ip_add, int index_) + + sockets &sockets::init_data(int port_, std::string ip_add, std::vector server_name_, int index_) { std::string port_str; - int ret_getadd; + int ret_getadd; sockfd = -1; port = port_; - sock_addr_len = 0; ip_addr = ip_add; index = index_; - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = 0; - hints.ai_protocol = 0; + index_tmp = index_; + server_name = server_name_; + memset(&hints, 0, sizeof(hints)); // initializes the struct addrinfo variable hints with zeros + hints.ai_family = AF_UNSPEC; // the address family IPv4 and IPv6 addresses for the given hostname + hints.ai_socktype = SOCK_STREAM; // specifies the socket type specifies the socket type port_str = std::to_string(port); ret_getadd = getaddrinfo(ip_add.c_str(), port_str.c_str(), &hints, &result); - - if (ret_getadd != 0) { + if (ret_getadd != 0) + { std::cout << "\033[31mGetaddrinfo error\033[0m\n"; exit(EXIT_FAILURE); - } - - if(start_server() == false){ + } + if(start_server() == false) + { exit(EXIT_FAILURE); } return (*this); @@ -70,6 +69,11 @@ namespace http{ return (index); } + int const &sockets::getIndex_tmp() const + { + return (index_tmp); + } + unsigned int sockets::getSock_addr_len()const { return (sock_addr_len); @@ -93,46 +97,39 @@ namespace http{ { int optval; - optval = 1; + optval = 1; //enabling the SO_REUSEADDR socket option. // Creates a TCP socket - for (rp = result; rp != NULL; rp = rp->ai_next) - { - sockfd = socket(rp->ai_family, rp->ai_socktype,rp->ai_protocol); - if (sockfd < 0){ - std::cout << "\033[31mCreate sockopt failed\033[0m\n"; - return (false); - } - } - - // set options for a socket + sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + if (sockfd < 0) + { + std::cout << "\033[31mCreate sockopt failed\033[0m\n"; + return (false); + } + // set socket option on a socket file descriptor if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) { - //SOL_SOCKET: manipulate the socket-level options + //SOL_SOCKET: represents the socket level option //SO_REUSEADDR: the option name. It is used to enable reuse of local addresses. //&optval: a pointer to the buffer that contains the value of the option you want to set std::cout << "\033[31mSet sockopt failed\033[0m\n"; return (false); } - // sock_addr_len = sizeof(hints); - // set the O_NONBLOCK flag for the socket file descriptor - // fcntl(sockfd, F_SETFL, O_NONBLOCK); - int val = fcntl(sockfd, F_GETFL, 0); - fcntl(sockfd, F_SETFL, val | O_NONBLOCK); + // set the O_NONBLOCK flag for the socket file descriptor to enable non-blocking mode. + fcntl(sockfd, F_SETFL, O_NONBLOCK); //bind a socket with a specific address and port number - // bind a socket with a specific address and port number - if (bind(sockfd, result->ai_addr, result->ai_addrlen) < 0) { - std::cout << "\033[31mBind System failed\033[0m\n"; - return false; + if (bind(sockfd, result->ai_addr, result->ai_addrlen) == 0) + { + // F_SETFL: This flag indicates that we want to set the file status flags. + std::cout << "\n\033[32mLISTENING ON ["<time_out = time; } + void sockets::setIndex(int const index_) + { + this->index = index_; + } } \ No newline at end of file diff --git a/server/test.cpp b/server/test.cpp index bd375f3..98947e5 100644 --- a/server/test.cpp +++ b/server/test.cpp @@ -531,4 +531,3 @@ int main() { return 0; } - diff --git a/www/html/cgi_bin/cookies.php b/www/html/cgi_bin/cookies.php index c35841f..8a0e293 100644 --- a/www/html/cgi_bin/cookies.php +++ b/www/html/cgi_bin/cookies.php @@ -1,27 +1,13 @@ "en", - "USER" => "ADMIN" -); -foreach ($cookies as $name => $value) { - setcookie($name, $value, time() + (86400 * 30), "/"); +// Check if the username cookie is set +if(isset($_COOKIE["username"])) { + $username = $_COOKIE["username"]; + echo "Welcome back, $username!"; +} else { + // Set the username cookie + $username = "webserver"; + setcookie("username", $username, time() + 1); + echo "Hello, $username we've set a cookie for you!"; } - ?> - - - - $value) { - if (!isset($_COOKIE[$name])) - echo "

Cookie = '" . $name . "' is not set!


"; - else { - echo "

Cookie = '" . $name . "' is set!


"; - echo "

Value is: " . $_COOKIE[$name] . "

"; - } - } - ?> - - - \ No newline at end of file diff --git a/www/html/cgi_bin/cookies.py b/www/html/cgi_bin/cookies.py deleted file mode 100644 index 73abf7c..0000000 --- a/www/html/cgi_bin/cookies.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python -from http import cookies -import datetime, os - -values = { 'a': 'a', 'b': 'b' } -expires = datetime.datetime.now() + datetime.timedelta(days=365) - -client_cookie = cookies.SimpleCookie() -string_cookie = os.environ.get('HTTP_COOKIE') -# Load the client's cookies into the cookie object -if string_cookie: - client_cookie.load(string_cookie) - -cookie = cookies.SimpleCookie() -for key in values: - cookie[key] = values[key] - cookie[key]['expires'] = expires.strftime("%a, %d-%b-%Y %H:%M:%S PST") - cookie[key]['path'] = '/' - cookie[key]['HttpOnly'] = True; - - -# Output the HTTP message containing the cookie -print (cookie) -print ('Content-type: text/html\r\n') -print ('\r\n') - -print ('') -if len(client_cookie) == 0: - print ('

cookie: is not set

') -else: - for key in values: - print ('

Cookie: %s = %s

' % (key, values[key])) -print ('') - diff --git a/www/html/cgi_bin/hello.html b/www/html/cgi_bin/hello.html index ad92ea1..c9d4e0a 100644 --- a/www/html/cgi_bin/hello.html +++ b/www/html/cgi_bin/hello.html @@ -1,49 +1,77 @@ + - Document + Location 1 + -
-

python get

-

-

- First Name:
- Last Name: - -
-

-

python post

-

-

- First Name:
- Last Name: - -
-

-

php get

-

-

- First Name:
- Last Name: - -
-

-

php post

-
- First Name:
- Last Name: - +
+

Python GET

+ +
+
+ - -
+

Python POST

+
+
+
+ +
+

PHP GET

+
+
+
+ +
+

PHP POST

+
+
+
+ +
+ - \ No newline at end of file + diff --git a/www/html/cgi_bin/hello.php b/www/html/cgi_bin/hello.php index 73d095f..d1f35d4 100644 --- a/www/html/cgi_bin/hello.php +++ b/www/html/cgi_bin/hello.php @@ -1,13 +1,18 @@ -"; -echo ""; -echo "Test"; -echo ""; -echo ""; -if ($_SERVER['REQUEST_METHOD'] == 'GET') - echo "

hello ". $_GET["first_name"] . " " . $_GET["last_name"] . "

"; -else - echo "

hello ". $_POST["first_name"] . " " . $_POST["last_name"] . "

"; -echo ""; -echo ""; +"; +echo ""; +echo "Test"; +echo ""; +echo ""; +echo "
"; +if ($_SERVER['REQUEST_METHOD'] == 'GET') { + echo "

Hello " . $_GET["first_name"] . " " . $_GET["last_name"] . "

"; +} else { + echo "

Hello " . $_POST["first_name"] . " " . $_POST["last_name"] . "

"; +} +echo "

Thank you for using our service!

"; +echo "Go back"; +echo "
"; +echo ""; +echo ""; ?> \ No newline at end of file diff --git a/www/html/cgi_bin/hello.py b/www/html/cgi_bin/hello.py index c610f83..94c2a8a 100644 --- a/www/html/cgi_bin/hello.py +++ b/www/html/cgi_bin/hello.py @@ -14,6 +14,9 @@ print ("Hello py cgi") print ("") print ("") +print("
") print ("

Hello %s %s

" % (first_name, last_name)) +print ("

Thank you for using our service!

") +print ("Go back") print ("") print ("") \ No newline at end of file diff --git a/www/html/cgi_bin/session.php b/www/html/cgi_bin/session.php index 9e2348b..4a7627d 100644 --- a/www/html/cgi_bin/session.php +++ b/www/html/cgi_bin/session.php @@ -1,13 +1,13 @@ \ No newline at end of file diff --git a/www/html/cgi_bin/session.py b/www/html/cgi_bin/session.py deleted file mode 100644 index 3750575..0000000 --- a/www/html/cgi_bin/session.py +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env python3 -import hashlib, datetime, os -from http import cookies - -expires = datetime.datetime.now() + datetime.timedelta(days=365) - -cookie = cookies.SimpleCookie() -client_cookie = cookies.SimpleCookie() - -string_cookie = os.environ.get('HTTP_COOKIE') -# Load the client's cookies into the cookie object -if string_cookie: - client_cookie.load(string_cookie) - -if not client_cookie.get('sid'): - # The sid will be a hash of the server time - sid = hashlib.sha256(repr(datetime.datetime.now()).encode('utf-8')).hexdigest() - # Set the cookie with the session ID and expiration date - cookie['sid'] = sid - cookie['sid']['expires'] = expires.strftime("%a, %d-%b-%Y %H:%M:%S PST") - cookie['sid']['path'] = '/' - cookie['sid']['HttpOnly'] = True; - -print(cookie) -print('Content-type: text/html\r\n') -print('\r\n') -print('') -if client_cookie.get('sid'): - print('

Welcome back!

') -else: - print('

First time here?

') -print('') \ No newline at end of file diff --git a/www/html/error_pages/201.html b/www/html/error_pages/201.html deleted file mode 100644 index dfb33f2..0000000 --- a/www/html/error_pages/201.html +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - Document - - - -
-

Created - 201 -

-
- -
-
- - diff --git a/www/html/error_pages/204.html b/www/html/error_pages/204.html deleted file mode 100644 index 45766a7..0000000 --- a/www/html/error_pages/204.html +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - Document - - - -
-

No Content - 204 -

-
- -
-
- - diff --git a/www/html/error_pages/400.html b/www/html/error_pages/400.html index 2cc5cf5..74b0283 100644 --- a/www/html/error_pages/400.html +++ b/www/html/error_pages/400.html @@ -1,58 +1,67 @@ - + - - - Document + Error 400 - Bad Request - -
-

Bad Request - 400 -

-
- -
+ +
+
400
+
Oops! Bad Request
+

Sorry, the server couldn't understand your request. Please go back to the homepage and try again.

+ Go to Homepage
- \ No newline at end of file + diff --git a/www/html/error_pages/403.html b/www/html/error_pages/403.html index ba822c2..302a239 100755 --- a/www/html/error_pages/403.html +++ b/www/html/error_pages/403.html @@ -1,58 +1,67 @@ - + - - - Document + Error 403 - Forbidden - -
-

Page is forbidden - 403 -

-
- -
+ +
+
403
+
Oops! Forbidden
+

Sorry, you don't have permission to access this page. Please go back to the homepage.

+ Go to Homepage
- \ No newline at end of file + diff --git a/www/html/error_pages/404.html b/www/html/error_pages/404.html index 080f3aa..c303193 100644 --- a/www/html/error_pages/404.html +++ b/www/html/error_pages/404.html @@ -1,58 +1,67 @@ - + - - - Document + Error 404 - Page Not Found - -
-

Page Not Found - 404 -

-
- -
+ +
+
404
+
Oops! Page Not Found
+

Sorry, the page you are looking for cannot be found. Please check the URL or go back to the homepage.

+ Go to Homepage
- \ No newline at end of file + diff --git a/www/html/error_pages/405.html b/www/html/error_pages/405.html old mode 100755 new mode 100644 index 650e64d..d74f581 --- a/www/html/error_pages/405.html +++ b/www/html/error_pages/405.html @@ -1,58 +1,67 @@ - + - - - Document + Error 405 - Method Not Allowed - -
-

Not allowed - 405 -

-
- -
+ +
+
405
+
Oops! Method Not Allowed
+

Sorry, the requested method is not allowed on this server. Please go back to the homepage and try a different method.

+ Go to Homepage
- \ No newline at end of file + diff --git a/www/html/error_pages/409.html b/www/html/error_pages/409.html new file mode 100644 index 0000000..32a535a --- /dev/null +++ b/www/html/error_pages/409.html @@ -0,0 +1,67 @@ + + + + + Error - Conflict + + + +
+
409
+
Conflict: Unable to delete folder due to existing files or subfolders
+

Please go back to the homepage and try a different resource name.

+ Go to Homepage +
+ + diff --git a/www/html/error_pages/413.html b/www/html/error_pages/413.html index 07d369a..92e0819 100644 --- a/www/html/error_pages/413.html +++ b/www/html/error_pages/413.html @@ -1,58 +1,67 @@ - + - - - Document + Error 413 - Request Entity Too Large - -
-

Content Too Large - 413 -

-
- -
+ +
+
413
+
Oops! Request Entity Too Large
+

Sorry, the request entity sent to the server is too large. Please go back to the homepage and try again with a smaller file or content.

+ Go to Homepage
- \ No newline at end of file + diff --git a/www/html/error_pages/414.html b/www/html/error_pages/414.html deleted file mode 100644 index 89358bf..0000000 --- a/www/html/error_pages/414.html +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - Document - - - -
-

URI Too Long - 414 -

-
- -
-
- - \ No newline at end of file diff --git a/www/html/error_pages/500.html b/www/html/error_pages/500.html index bd07b72..f268dc5 100755 --- a/www/html/error_pages/500.html +++ b/www/html/error_pages/500.html @@ -1,58 +1,67 @@ - + - - - Document + Error 500 - Internal Server Error - -
-

Internal Server Error - 500 -

-
- -
+ +
+
500
+
Oops! Internal Server Error
+

Sorry, there was an internal server error. Please try again later or go back to the homepage.

+ Go to Homepage
- \ No newline at end of file + diff --git a/www/html/error_pages/501.html b/www/html/error_pages/501.html deleted file mode 100644 index e9543d3..0000000 --- a/www/html/error_pages/501.html +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - Document - - - -
-

Not Implemented - 501 -

-
- -
-
- - \ No newline at end of file diff --git a/www/html/error_pages/502.html b/www/html/error_pages/502.html deleted file mode 100644 index ac1a983..0000000 --- a/www/html/error_pages/502.html +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - Document - - - -
-

Bad Gateway - 502 -

-
- -
-
- - \ No newline at end of file diff --git a/www/html/error_pages/505.html b/www/html/error_pages/505.html deleted file mode 100644 index ca449f0..0000000 --- a/www/html/error_pages/505.html +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - Document - - - -
-

HTTP Version Not Supported - 505 -

-
- -
-
- - \ No newline at end of file diff --git a/www/html/index.html b/www/html/index.html index 5f57d12..235888f 100644 --- a/www/html/index.html +++ b/www/html/index.html @@ -1,110 +1,53 @@ - + - - Display all files - + Your Website Title + body { + font-family: Arial, sans-serif; + margin: 0; + padding: 0; + background-color: #f1f1f1; + } + .container { + max-width: 800px; + margin: 0 auto; + padding: 20px; + background-color: #ffffff; + border-radius: 10px; + box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); + } + h1 { + text-align: center; + color: #333333; + } + p { + color: #555555; + line-height: 1.5; + } + .cta-button { + display: inline-block; + padding: 12px 24px; + background-color: #007bff; + color: #ffffff; + font-size: 16px; + text-decoration: none; + border-radius: 5px; + transition: background-color 0.3s ease; + } + .cta-button:hover { + background-color: #0056b3; + } + -
-

Display all files

-
-
-

The file has been uploaded

-
+
+

Welcome to our WEBSERVER!

+

This is the main page of our web server.

+

This is the default page for my website. Start exploring by clicking the button below.

+ Get Started +
+ \ No newline at end of file diff --git a/www/html/start.html b/www/html/start.html new file mode 100644 index 0000000..e58c496 --- /dev/null +++ b/www/html/start.html @@ -0,0 +1,55 @@ + + + + Your Website Title + + + +
+

Welcome to Your Website!

+

This is the main page of your web server.

+

This is the default page for my website. Start exploring by clicking the buttons below.

+ CGI_BIN + REDERICTION + UPLOAD + GET VIDEO +
+ + diff --git a/www/html/upload/Photo on 5-16-23 at 3.09 PM.jpg b/www/html/upload/Photo on 5-16-23 at 3.09 PM.jpg new file mode 100644 index 0000000..bb9e8aa Binary files /dev/null and b/www/html/upload/Photo on 5-16-23 at 3.09 PM.jpg differ diff --git a/www/html/upload/Photo on 5-16-23 at 3.10 PM #3.jpg b/www/html/upload/Photo on 5-16-23 at 3.10 PM #3.jpg new file mode 100644 index 0000000..d22cf7d Binary files /dev/null and b/www/html/upload/Photo on 5-16-23 at 3.10 PM #3.jpg differ diff --git a/www/html/upload/Screen.png b/www/html/upload/Screen.png new file mode 100644 index 0000000..c0223d4 Binary files /dev/null and b/www/html/upload/Screen.png differ diff --git a/www/html/upload/cpp_09.pdf b/www/html/upload/cpp_09.pdf new file mode 100644 index 0000000..c189288 Binary files /dev/null and b/www/html/upload/cpp_09.pdf differ diff --git a/www/html/upload/success-page.htmlPhoto on 5-16-23 at 3.09 PM.jpg b/www/html/upload/success-page.htmlPhoto on 5-16-23 at 3.09 PM.jpg new file mode 100644 index 0000000..bb9e8aa Binary files /dev/null and b/www/html/upload/success-page.htmlPhoto on 5-16-23 at 3.09 PM.jpg differ diff --git a/www/html/upload/success-page.htmlScreen Shot 2023-05-07 at 8.57.50 PM.png b/www/html/upload/success-page.htmlScreen Shot 2023-05-07 at 8.57.50 PM.png new file mode 100644 index 0000000..aa2f3fa Binary files /dev/null and b/www/html/upload/success-page.htmlScreen Shot 2023-05-07 at 8.57.50 PM.png differ diff --git a/www/html/upload/success.html b/www/html/upload/success.html new file mode 100644 index 0000000..8031fd0 --- /dev/null +++ b/www/html/upload/success.html @@ -0,0 +1,53 @@ + + + + + Upload Success + + + +
+

Upload Successful!

+

Your files have been uploaded successfully.

+ Go Back +
+ + diff --git a/www/html/upload/success.htmlPhoto on 5-16-23 at 3.09 PM.jpg b/www/html/upload/success.htmlPhoto on 5-16-23 at 3.09 PM.jpg new file mode 100644 index 0000000..bb9e8aa Binary files /dev/null and b/www/html/upload/success.htmlPhoto on 5-16-23 at 3.09 PM.jpg differ diff --git a/www/html/upload/upload.html b/www/html/upload/upload.html index fd2621d..c2080b1 100644 --- a/www/html/upload/upload.html +++ b/www/html/upload/upload.html @@ -4,105 +4,93 @@ File Uploader - + + box-sizing: border-box; + margin: 0; + padding: 0; + } + + /* Body styles */ + body { + font-family: Arial, sans-serif; + background-color: #f2f2f2; + } + + /* Header styles */ + header { + background-color: #007bff; + color: white; + text-align: center; + padding: 20px; + } + + header h1 { + font-size: 36px; + margin: 0; + } + + header p { + font-size: 18px; + margin: 10px 0; + } + + /* Main styles */ + main { + max-width: 600px; + margin: 0 auto; + padding: 20px; + background-color: white; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); + } + + /* Form styles */ + form { + display: flex; + flex-wrap: wrap; + } + + .form-group { + flex-basis: 100%; + margin-bottom: 10px; + } + + .form-group label { + display: block; + margin-bottom: 5px; + font-weight: bold; + } + + input[type="file"] { + border: 1px solid #ccc; + border-radius: 4px; + padding: 5px; + font-size: 16px; + width: 100%; + margin-bottom: 10px; + } + + button[type="submit"] { + background-color: #007bff; + color: white; + border: none; + padding: 10px 20px; + font-size: 16px; + border-radius: 4px; + cursor: pointer; + transition: background-color 0.3s ease; + } + + button[type="submit"]:hover { + background-color: #0056b3; + } +

File Uploader

-

Select up files to upload

+

Select files to upload