Browse Source

basic error handling

master
Walid Bekkal 2 years ago
parent
commit
8cbefa68a8
  1. 2
      includes/webserv.hpp
  2. 1
      public/documents/test1
  3. 1
      public/documents/test2
  4. 2
      public/images/a_file
  5. 2
      public/testsite/basique.html
  6. 3
      public/testsite/basique.php
  7. 0
      public/testsite/monkey.gif
  8. 25
      srcs/webserv.cpp

2
includes/webserv.hpp

@ -10,7 +10,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cerrno> #include <cerrno>
@ -56,7 +55,6 @@ string read_file(string path);
#include "Nodes.hpp" #include "Nodes.hpp"
#include "Token.hpp" #include "Token.hpp"
#include "Parser.hpp" #include "Parser.hpp"
#include "Route.hpp" #include "Route.hpp"
#include "Socket.hpp" #include "Socket.hpp"
#include "Server.hpp" #include "Server.hpp"

1
public/documents/test1

@ -0,0 +1 @@
===||This is a test file||===

1
public/documents/test2

@ -0,0 +1 @@
===||This is a test2 file||===

2
public/images/a_file

@ -1,2 +0,0 @@
jdkslaj\njkdsa;jfd\njdkaj;fjd
jdkslaj\njkdsa;jfd\njdkaj;fjd

2
public/testsite/basique.html

@ -19,7 +19,7 @@
document.querySelector('body').innerHTML = '<button class ="second" style="position: fixed; bottom: 0; left: 0; width: 100%; height: 50px; font-size: 20px; background-color: red; color: white;">CLIQUEZ ICI POUR VOIR VOTRE VRAI CADEAU</button>'; document.querySelector('body').innerHTML = '<button class ="second" style="position: fixed; bottom: 0; left: 0; width: 100%; height: 50px; font-size: 20px; background-color: red; color: white;">CLIQUEZ ICI POUR VOIR VOTRE VRAI CADEAU</button>';
const button2 = document.querySelector('button.second'); const button2 = document.querySelector('button.second');
button2.addEventListener('click', function () { button2.addEventListener('click', function () {
document.querySelector('body').innerHTML = '<img src="../images/monkey.gif" style="width: 100%; height: 100%;">'; document.querySelector('body').innerHTML = '<img src="./monkey.gif" style="width: 100%; height: 100%;">';
}); });
}); });
</script> </script>

3
public/testsite/basique.php

@ -3,6 +3,7 @@
<title>PHP Test</title> <title>PHP Test</title>
</head> </head>
<body> <body>
<?php echo '<p>PHP page</p>'; ?> <?php echo '<p>PHP page</p>'; ?><br>
<p>Copyright © NicoWalid <?php echo date("Y"); ?></p>
</body> </body>
</html> </html>

0
public/images/monkey.gif → public/testsite/monkey.gif

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

25
srcs/webserv.cpp

@ -6,16 +6,21 @@ int Socket::_min_fd = INT_MAX;
int Socket::_amount = 0; int Socket::_amount = 0;
int main(int ac, char **av) { int main(int ac, char **av) {
if (ac <= 2) {
if (ac < 2) { std::string config = "default.json";
cout << "Usage:\n"; if (ac == 2){
cout << "./webserv CONF.json\n"; std::ifstream file(av[1]);
cout << "WARNING: You must use a correct json syntax and a correct " if (file.good())
"server configuration or don't expect it to work correctly."; config = av[1];
} else if (ac == 2) { else {
std::cout << "Error: " << av[1] << " is not a valid file" << std::endl;
return EXIT_FAILURE;
}
config = av[1];
}
cout << "Parsing configuration file from JSON conf file.\n"; cout << "Parsing configuration file from JSON conf file.\n";
cout << "You must be sure the syntax is correct\n"; cout << "You must be sure the syntax is correct\n";
JSONParser parser(av[1]); JSONParser parser(config);
JSONNode *conf = parser.parse(); JSONNode *conf = parser.parse();
cout << "Initialization of server...\n"; cout << "Initialization of server...\n";
Env env(conf); Env env(conf);
@ -27,12 +32,14 @@ int main(int ac, char **av) {
env.set_fds(); env.set_fds();
cout << "|===||===| Waiting some HTTP request... |===||===|\n"; cout << "|===||===| Waiting some HTTP request... |===||===|\n";
int activity = select(Socket::_max_fd + Socket::_amount, int activity = select(Socket::_max_fd + Socket::_amount,
&(Socket::_readfds), NULL, NULL, NULL); &(Socket::_readfds), NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) if ((activity < 0) && (errno != EINTR))
cout << "Select: " << strerror(errno) << "\n"; cout << "Select: " << strerror(errno) << "\n";
cout << "==> Handle requests and answers:\n"; cout << "==> Handle requests and answers:\n";
env.refresh(); env.refresh();
} }
} }
else
cout << "Usage:\n./webserv CONF.json\n";
return (0); return (0);
} }

Loading…
Cancel
Save