mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 20:28:46 +02:00
「✨」 feat(server/client): added Client class :D
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */
|
/* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/03/24 15:05:53 by mmoussou ### ########.fr */
|
/* Updated: 2025/04/17 13:10:22 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -22,16 +22,10 @@
|
|||||||
namespace webserv {
|
namespace webserv {
|
||||||
namespace http {
|
namespace http {
|
||||||
|
|
||||||
/*
|
|
||||||
* DOES NOT WORK
|
|
||||||
* still need to do uh things but base is done at least :D
|
|
||||||
*/
|
|
||||||
|
|
||||||
class Errors {
|
class Errors {
|
||||||
public:
|
public:
|
||||||
//static http::Response &getRequest(int error_code);
|
|
||||||
static std::string getResponseBody(int error_code);
|
static std::string getResponseBody(int error_code);
|
||||||
static void setEntry(const std::string &key, int value);
|
static void setEntries(const std::map<int, std::string>);
|
||||||
|
|
||||||
static std::map<int, std::string> message;
|
static std::map<int, std::string> message;
|
||||||
private:
|
private:
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
|
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/04/14 15:00:41 by adjoly ### ########.fr */
|
/* Updated: 2025/04/17 12:46:04 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -18,16 +18,26 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <webserv.hpp>
|
#include <webserv.hpp>
|
||||||
|
|
||||||
|
namespace webserv {
|
||||||
|
namespace server {
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
public:
|
public:
|
||||||
Client(int, sockaddr_in);
|
Client(int, sockaddr_in, config::Config *);
|
||||||
~Client(void);
|
~Client(void);
|
||||||
|
|
||||||
void answer(void);
|
void answer(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void _getRequest(std::string);
|
||||||
|
|
||||||
int _fd;
|
int _fd;
|
||||||
struct sockaddr_in _client_addr;
|
struct sockaddr_in _client_addr;
|
||||||
http::IRequest *_request;
|
http::IRequest *_request;
|
||||||
http::Response *_response;
|
http::Response *_response;
|
||||||
config::Config *_conf;
|
config::Server *_conf;
|
||||||
|
Logger *_log;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // -namespace server
|
||||||
|
} // -namespace webserv
|
||||||
|
105
src/server/Client.cpp
Normal file
105
src/server/Client.cpp
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Client.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2025/04/17 13:10:37 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <server/Client.hpp>
|
||||||
|
|
||||||
|
using namespace server;
|
||||||
|
|
||||||
|
/*class Client {
|
||||||
|
public:
|
||||||
|
Client(int, sockaddr_in, config::Config *);
|
||||||
|
~Client(void);
|
||||||
|
|
||||||
|
void answer(void);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void getRequest(void);
|
||||||
|
|
||||||
|
int _fd;
|
||||||
|
struct sockaddr_in _client_addr;
|
||||||
|
http::IRequest *_request;
|
||||||
|
http::Response *_response;
|
||||||
|
config::Server *_conf;
|
||||||
|
std::string _request_method;
|
||||||
|
};*/
|
||||||
|
|
||||||
|
Client::Client(int fd, sockaddr_in socket, config::Servr *conf, Logger *log)
|
||||||
|
{
|
||||||
|
this->_fd = fd;
|
||||||
|
this->_client_addr = socket;
|
||||||
|
this->_conf = conf;
|
||||||
|
this->_log = log;
|
||||||
|
|
||||||
|
std::string received_data;
|
||||||
|
char buffer[BUFFER_SIZE];
|
||||||
|
ssize_t bytes_received;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
std::memset(buffer, 0, BUFFER_SIZE);
|
||||||
|
bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0);
|
||||||
|
if (bytes_received == -1)
|
||||||
|
{
|
||||||
|
_log->error("failed to receive request");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
received_data += std::string(buffer, bytes_received);
|
||||||
|
}
|
||||||
|
while (buffer[bytes_received]);
|
||||||
|
|
||||||
|
|
||||||
|
this->getRequest(request_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::_getRequest(std::string request_str)
|
||||||
|
{
|
||||||
|
std::string method = request_str.substr(0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
|
||||||
|
|
||||||
|
if (method == "GET")
|
||||||
|
{
|
||||||
|
_log->info("get request received");
|
||||||
|
this->_request = new http::Get(request_str);
|
||||||
|
}
|
||||||
|
else if (method == "DELETE")
|
||||||
|
{
|
||||||
|
_log->info("delete request received");
|
||||||
|
this->_request = new http::Delete(request_str);
|
||||||
|
}
|
||||||
|
else if (method == "POST")
|
||||||
|
{
|
||||||
|
_log->info("post request received");
|
||||||
|
this->_request = new http::Post(request_str);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_log->info("unsupported request received");
|
||||||
|
this->_request = new http::Get();
|
||||||
|
this->_request->setMethod("501");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::answer(void)
|
||||||
|
{
|
||||||
|
std::string response;
|
||||||
|
|
||||||
|
if (this->_request == "GET" || this->_request == "DELETE" || this->_request == "POST")
|
||||||
|
response = this->_request.execute().str();
|
||||||
|
else
|
||||||
|
response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n<html><body><h1>501 Not Implemented</h1></body></html>";
|
||||||
|
send(this->_fd, response.c_str(), response.length(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Client::~Client(void)
|
||||||
|
{
|
||||||
|
delete this->_request;
|
||||||
|
delete this->_response;
|
||||||
|
}
|
90
upload.html
90
upload.html
@ -1,90 +0,0 @@
|
|||||||
<meta charset="utf-8">
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>upload</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- j'ai pas volé le code (c'est faux) -->
|
|
||||||
<!-- putain les pubs spotify c'est chiant -->
|
|
||||||
<!-- je m'en fous de leur bière -->
|
|
||||||
<form id="upload_form" enctype="multipart/form-data" method="post">
|
|
||||||
<input type="file" name="file1" id="file1" onchange="uploadFile()"><br>
|
|
||||||
<progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
|
|
||||||
<h3 id="status"></h3>
|
|
||||||
<p id="loaded_n_total"></p>
|
|
||||||
</form>
|
|
||||||
<button id="abort" hidden>annuler le tranfert</button>
|
|
||||||
<script>
|
|
||||||
function _(el) {
|
|
||||||
return document.getElementById(el);
|
|
||||||
}
|
|
||||||
|
|
||||||
function uploadFile() {
|
|
||||||
bouton = document.getElementById("abort")
|
|
||||||
var file = _("file1").files[0];
|
|
||||||
|
|
||||||
// alert(file.name+" | "+file.size+" | "+file.type);
|
|
||||||
var formdata = new FormData();
|
|
||||||
formdata.append("file1", file);
|
|
||||||
var ajax = new XMLHttpRequest();
|
|
||||||
bouton.onclick = function(){
|
|
||||||
ajax.abort()
|
|
||||||
}
|
|
||||||
bouton.removeAttribute("hidden")
|
|
||||||
ajax.upload.addEventListener("progress", progressHandler, false);
|
|
||||||
ajax.addEventListener("load", completeHandler, false);
|
|
||||||
ajax.addEventListener("error", errorHandler, false);
|
|
||||||
ajax.addEventListener("abort", abortHandler, false);
|
|
||||||
ajax.open("POST", "/upload.html");
|
|
||||||
ajax.send(formdata);
|
|
||||||
startmillis = Date.now()
|
|
||||||
}
|
|
||||||
|
|
||||||
function copy(){
|
|
||||||
window.getSelection().removeAllRanges()
|
|
||||||
range = document.createRange()
|
|
||||||
range.selectNodeContents(_("addr"))
|
|
||||||
window.getSelection().addRange(range)
|
|
||||||
document.execCommand('copy')
|
|
||||||
window.getSelection().removeAllRanges()
|
|
||||||
temp = _("addr").innerHTML
|
|
||||||
_("addr").innerHTML = "copié !"
|
|
||||||
setTimeout(()=>{_("addr").innerHTML = temp},500)
|
|
||||||
}
|
|
||||||
|
|
||||||
function progressHandler(event) {
|
|
||||||
// console.log(Math.round((event.loaded/1024/1024)*100)/100)
|
|
||||||
// console.log((Date.now()-startmillis)/1000+'s')
|
|
||||||
//console.log((event.loaded/1024/1024) /((Date.now()-startmillis)*1000)+"Mo/s")
|
|
||||||
// console.log((event.loaded/1024/1024))
|
|
||||||
|
|
||||||
console.log(`${(event.loaded/1024/1024)} / ${(Date.now()-startmillis)} / 1000`)
|
|
||||||
console.log((event.loaded/1024/1024)/((Date.now()-startmillis)/1000))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
_("loaded_n_total").innerHTML = Math.round((event.loaded/1024/1024)*100)/100 + "Mo / " + Math.round((event.total/1024/1024)*100)/100+"Mo envoyé<br/>"+Math.round((event.loaded/1024/1024)/((Date.now()-startmillis)/1000)*100)/100+"Mo/s en moyenne";
|
|
||||||
var percent = (event.loaded / event.total) * 100;
|
|
||||||
_("progressBar").value = Math.round(percent);
|
|
||||||
_("status").innerHTML = Math.round(percent * 100) / 100 + "% envoyé... veuillez patienter";
|
|
||||||
}
|
|
||||||
|
|
||||||
function completeHandler(event) {
|
|
||||||
_("status").innerHTML = event.target.responseText;
|
|
||||||
_("progressBar").value = 0; //wil clear progress bar after successful upload
|
|
||||||
console.log("fin de l'envoi")
|
|
||||||
_("abort").setAttribute("hidden",true)
|
|
||||||
}
|
|
||||||
|
|
||||||
function errorHandler(event) {
|
|
||||||
_("status").innerHTML = "Upload Failed";
|
|
||||||
}
|
|
||||||
|
|
||||||
function abortHandler(event) {
|
|
||||||
_("status").innerHTML = "envoi annulé";
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
Reference in New Issue
Block a user