mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-11 00:08:46 +02:00
「🏗️」 wip(requests/POST): updating only things that does make sense
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/11 22:13:38 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/02/14 17:10:42 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 01:47:24 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -22,10 +22,10 @@ namespace http {
|
||||
|
||||
class IMessage {
|
||||
public:
|
||||
virtual std::multimap<std::string, std::string> getHeaders(void) const;
|
||||
virtual std::map<std::string, std::string> getHeaders(void) const;
|
||||
virtual std::string getBody(void) const;
|
||||
|
||||
virtual void setHeaders(std::multimap<std::string, std::string> const headers);
|
||||
virtual void setHeaders(std::map<std::string, std::string> const headers);
|
||||
virtual void setBody(std::string const body);
|
||||
|
||||
virtual void addHeader(std::string const key, std::string const value);
|
||||
@ -34,7 +34,7 @@ public:
|
||||
virtual std::string str(void) const = 0;
|
||||
|
||||
protected:
|
||||
std::multimap<std::string, std::string> _headers;
|
||||
std::map<std::string, std::string> _headers;
|
||||
std::string _body;
|
||||
|
||||
static const std::map<int, std::string> _response_status_codes;
|
||||
|
29
src/main.cpp
29
src/main.cpp
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/03/19 03:11:14 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 05:24:59 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -27,6 +27,11 @@ void close_socket(int signal)
|
||||
exit(signal);
|
||||
}
|
||||
|
||||
std::string getMethod(std::string &data)
|
||||
{
|
||||
return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// handle ctrl-C to close server socket
|
||||
@ -89,21 +94,33 @@ int main()
|
||||
|
||||
// parse the request
|
||||
std::string received_data(buffer, bytes_received);
|
||||
http::Get request(received_data);
|
||||
|
||||
std::cout << "Received " << request.getMethod() << " request for " << request.getTarget() << std::endl;
|
||||
|
||||
// handle the request
|
||||
std::string response;
|
||||
if (request.getMethod() == "GET" || request.getMethod() == "POST")
|
||||
|
||||
std::cout << getMethod(received_data) << std::endl;
|
||||
|
||||
if (getMethod(received_data) == "GET")
|
||||
{
|
||||
std::cout << "------------ GET REQUEST ------------" << std::endl;
|
||||
http::Get request(received_data);
|
||||
|
||||
response = request.execute().str();
|
||||
}
|
||||
else if (getMethod(received_data) == "POST")
|
||||
{
|
||||
std::cout << "------------ POST REQUEST ------------" << std::endl;
|
||||
http::Post request(received_data);
|
||||
|
||||
response = 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(client_socket, response.c_str(), response.length(), 0);
|
||||
close(client_socket);
|
||||
//close(client_socket);
|
||||
}
|
||||
|
||||
close(server_socket);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/17 14:08:12 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/03/24 15:12:50 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 01:48:20 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/11 23:34:45 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/02/12 00:59:22 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 01:46:52 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
|
||||
using namespace webserv;
|
||||
|
||||
std::multimap<std::string, std::string> http::IMessage::getHeaders(void) const
|
||||
std::map<std::string, std::string> http::IMessage::getHeaders(void) const
|
||||
{
|
||||
return (this->_headers);
|
||||
}
|
||||
@ -24,7 +24,7 @@ std::string http::IMessage::getBody(void) const
|
||||
return (this->_body);
|
||||
}
|
||||
|
||||
void http::IMessage::setHeaders(std::multimap<std::string, std::string> const headers)
|
||||
void http::IMessage::setHeaders(std::map<std::string, std::string> const headers)
|
||||
{
|
||||
this->_headers = headers;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/03/24 16:00:41 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 05:24:17 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -27,7 +27,7 @@ std::string http::IRequest::str(void) const
|
||||
response << this->_method << " " << this->_target << " " << this->_protocol;
|
||||
response << "\r\n";
|
||||
|
||||
for (std::multimap<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
response << it->first << ": " << it->second << "\r\n";
|
||||
|
||||
response << "\r\n";
|
||||
@ -187,7 +187,6 @@ http::Response http::Get::execute(void)
|
||||
response.setBody(std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()));
|
||||
std::stringstream length;
|
||||
length << (file.tellg() - file_start);
|
||||
std::cout << length.str() << std::endl;
|
||||
response.addHeader("Content-Length", length.str());
|
||||
|
||||
response.setProtocol(this->_protocol);
|
||||
@ -345,12 +344,64 @@ void http::Post::parse(std::string const &data)
|
||||
//*/
|
||||
}
|
||||
|
||||
std::string extractFilename(const std::string &header)
|
||||
{
|
||||
size_t start = header.find("filename=\"") + 10;
|
||||
size_t end = header.find("\"", start);
|
||||
return header.substr(start, end - start);
|
||||
}
|
||||
|
||||
void handleMultipartData(const std::string &body, const std::string &boundary)
|
||||
{
|
||||
size_t i = 0;
|
||||
std::string delim = "--" + boundary;
|
||||
delim.erase(delim.size() - 1);
|
||||
|
||||
while ((i = body.find(delim, i)) != std::string::npos)
|
||||
{
|
||||
size_t start = i + delim.length();
|
||||
size_t end = body.find("\r\n\r\n", start);
|
||||
|
||||
if (end != std::string::npos)
|
||||
{
|
||||
std::string part_header = body.substr(start, end - start);
|
||||
std::cout << std::endl<< std::endl<< std::endl<< std::endl<< std::endl;
|
||||
std::string part_content = body.substr(end + 4, body.find(delim, end) - end - 4);
|
||||
|
||||
std::ofstream outfile(extractFilename(part_header).c_str(), std::ios::binary);
|
||||
if (outfile.is_open())
|
||||
{
|
||||
outfile.write(part_content.c_str(), part_content.length());
|
||||
outfile.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "open failed" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
i += delim.length();
|
||||
}
|
||||
}
|
||||
|
||||
http::Response http::Post::execute(void)
|
||||
{
|
||||
http::Response response;
|
||||
|
||||
// uh idk anymore
|
||||
try
|
||||
{
|
||||
handleMultipartData(this->_body, this->getHeaders()["Content-Type"].substr(this->getHeaders()["Content-Type"].find("=", this->getHeaders()["Content-Type"].find(";")) + 1));
|
||||
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(204);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(500);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(http::Errors::getResponseBody(response.getStatusCode()));
|
||||
}
|
||||
return (response);
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/03/24 15:17:52 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/02 01:48:39 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -34,7 +34,7 @@ std::string http::Response::str(void) const
|
||||
response << this->_protocol << " " << this->_status_code << " " << this->_status_text;
|
||||
response << "\r\n";
|
||||
|
||||
for (std::multimap<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
response << it->first << ": " << it->second << "\r\n";
|
||||
|
||||
response << "\r\n";
|
||||
|
90
upload.html
Normal file
90
upload.html
Normal file
@ -0,0 +1,90 @@
|
||||
<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