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