Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http response new #31

Merged
merged 37 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
37db3db
http request cpp file
lucienvb Sep 14, 2023
d5ef0a0
http request, now also the cpp file
lucienvb Sep 14, 2023
45675f8
no more errors for now
lucienvb Sep 14, 2023
4f00083
now LOGGER included in the main
lucienvb Sep 14, 2023
d284d79
added comments
lucienvb Sep 14, 2023
97500c7
first steps file manager
lucienvb Sep 14, 2023
ced570f
update
lucienvb Sep 14, 2023
3b5915c
a start for the FileManager
lucienvb Sep 14, 2023
60438de
some adjustments
lucienvb Sep 14, 2023
4614662
minor adjustments
lucienvb Sep 15, 2023
7ddc654
added enum class HTTPMethod and small fixes to be able to compile on …
Sep 20, 2023
68bdee1
played with adding an image to index.html
Sep 20, 2023
70da252
now able to check if the image is in the file system, if so post it. …
Sep 21, 2023
83685da
now also able to save incoming image in file system
Sep 21, 2023
d0e78ba
optimized a bit and added in comments the simplified implementation o…
Sep 21, 2023
24d1c45
some improvements done, when a certain content_type has it's own dire…
Sep 21, 2023
8efb19e
changed draft file to saveData.cpp
Sep 21, 2023
62c76af
draft manageDelete method in place
Sep 21, 2023
617f337
managePost now also functional as method
Sep 22, 2023
b3a8cb6
resolve merge conflicts
Sep 27, 2023
2089b0a
pollfd parser is functional
Sep 28, 2023
d82440b
made some steps with the poll_fd parser
Sep 28, 2023
7f76b2c
Merge branch 'main' into http_request
Sep 29, 2023
064f507
it's funcional to some point
Sep 29, 2023
4694c8c
handleConnection is successful integrated, next step is integrate the…
Sep 29, 2023
ac8b2aa
request parser is working
Sep 29, 2023
f56cfa8
resolve merge conflicts
Sep 29, 2023
f1d3474
minor fixes on file_manager
Sep 29, 2023
6507c46
resolve merge conflicts
Sep 29, 2023
7d60e50
next step is making the status_code in fileManager class functional
Sep 29, 2023
9a4a979
manageDelete now functional
Oct 4, 2023
20c624e
post is functional now
Oct 4, 2023
0b395e4
post works now
Oct 4, 2023
48656e2
get now also functional
Oct 4, 2023
d832138
HTTPResponse class has been set up
Oct 4, 2023
e315886
response seems to be functional, have to do some improvements though
Oct 6, 2023
32cc345
added some comments
Oct 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added data/nose_monkey.webp
Binary file not shown.
77 changes: 76 additions & 1 deletion data/www/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,78 @@
<!DOCTYPE html>
<html>
<head>
<title>Test website</title>
</head>
<body>
<h1 align="center">Do not go gentle into that good night</h1>
<!-- Your poem content here -->

<!-- Form for leaving notes -->
<form id="noteForm">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="note">Note:</label>
<textarea id="note" name="note" required></textarea><br>
<button type="submit">Submit Note</button>
</form>

<div id="notes">
<!-- Display notes here -->
<P ALIGN="CENTER"></P>
<!-- lvanbus: supersu -->
</P>
<!-- IMAGES -->


<!-- text/plain -->
<P ALIGN="LEFT">hallo</P>
<P ALIGN="LEFT">Message</P>
<P ALIGN="LEFT">Message</P>


</div>

<script>
// JavaScript code to handle form submission and note retrieval
const noteForm = document.getElementById('noteForm');
const notesDiv = document.getElementById('notes');

noteForm.addEventListener('submit', async (e) => {
e.preventDefault();

const formData = new FormData(noteForm);
const response = await fetch('/api/notes', {
method: 'POST',
body: formData,
});

if (response.ok) {
// Clear the form and refresh the notes
noteForm.reset();
fetchNotes();
}
});

async function fetchNotes() {
const response = await fetch('/api/notes');
const notes = await response.json();

// Display the notes in the notesDiv
notesDiv.innerHTML = '';
notes.forEach((note) => {
const noteElement = document.createElement('div');
noteElement.textContent = `${note.username}: ${note.note}`;
notesDiv.appendChild(noteElement);
});
}

// Initial fetch of notes
fetchNotes();
</script>
</body>
</html>

<!--
<HTML>
<HEAD>
<TITLE>Do not go gentle into that good night</TITLE>
Expand Down Expand Up @@ -34,4 +109,4 @@ <H1 ALIGN="CENTER">Do not go gentle into that good night</H1>
</P>
<BODY>
</HTML>

-->
40 changes: 40 additions & 0 deletions include/FileManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef FILE_MANAGER_HPP
#define FILE_MANAGER_HPP

// INCLUDES
#include <string>
#include <HTTPRequest.hpp>

// // ENUM
// enum class HTTPMethod {
// GET,
// POST,
// DELETE,
// };

// CLASS
class FileManager {
private:
int _status_code;
std::string _content;

public:
// canonical form: constructors, operator and destructor
FileManager();
FileManager(const FileManager& other) = delete;
void operator=(const FileManager& other) = delete;
~FileManager();

// manage methods
void manage(HTTPMethod method, const std::string& filename, const std::string& body);
void manageGet(const std::string& filename);
void managePost(const std::string& filename, const std::string& body);
void manageDelete(const std::string& filename);

// getters
const std::string& getContent() const;
const int& getStatusCode() const;

};

#endif
74 changes: 74 additions & 0 deletions include/HTTPRequest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef HTTP_REQUEST_HPP
#define HTTP_REQUEST_HPP

// PARSE_HTTP_REQUEST -- request struct? Status:
// 1) parse request line done
// a) Set method type -- enum done
// b) Set path done
// c) Set version done
// 2) parse headers --> map done
// 3) parse body --> ?? done (as string)

// INCLUDE
#include <iostream>
#include <map>

// DEFINES

// ENUM
enum class HTTPMethod {
GET,
POST,
DELETE,
UNKNOWN,
};

// CLASS
class HTTPRequest {
private:
HTTPMethod _methodType;
std::string _path;
std::string _version;
std::map<std::string, std::string> _headers;
std::string _body;

public:
// canonical form: constructors, operator and destructor
HTTPRequest();
HTTPRequest(const HTTPRequest& other);
HTTPRequest& operator=(const HTTPRequest& other);
~HTTPRequest();

// methodType methods
void setMethodType(const std::string& requestLine);
HTTPMethod getMethodType(void) const;

// path methods
void setPath(const std::string& requestLine);
const std::string& getPath(void) const;

// version methods
void setVersion(const std::string& requestLine);
const std::string& getVersion(void) const;

// header methods
void setHeader(const std::string& headerLine);
std::string& getValue(const std::string& key);

// body methods
void setBody(const std::string& body);
const std::string& getBody(void) const;

// parser
void parse(void);

// public variables -- just for now
std::string _http_request_str;
// size_t try out
int _content_length;
int _content_length_cpy;
bool _post_method;
size_t _pos;
};

#endif
44 changes: 44 additions & 0 deletions include/HTTPResponse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef HTTP_RESPONSE_HPP
#define HTTP_RESPONSE_HPP

// INCLUDE
#include <iostream>
#include <map>
#include <HTTPRequest.hpp>

// CLASS
class HTTPResponse {
private:
std::string _http_response_str;
std::string _version;
int _status_code;
std::string _response_phrase;
std::map<std::string, std::string> _headers;
std::string _body;

public:
HTTPResponse(std::string version, int status_code, std::string _body);
HTTPResponse(const HTTPResponse& other) = delete;
HTTPResponse& operator=(const HTTPResponse& other) = delete;
~HTTPResponse();

// version methods
void setVersion(const std::string& requestLine);
const std::string& getVersion(void) const;

// header methods
void setHeader(const std::string& key, const std::string& value);
std::string& getValue(const std::string& key);

// body methods
void setBody(const std::string& body);
const std::string& getBody(void) const;

int getStatusCode(void) const;

std::string getHTTPResponse(void) const;
std::string getResponsePhrase(void) const;
void createHTTPResponse();
};

#endif
10 changes: 9 additions & 1 deletion include/HTTPServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
#include <ConfigParser.hpp>
#include <Defines.hpp>
// #include <ThreadPool.hpp>
#include <HTTPRequest.hpp>
#include <HTTPResponse.hpp>
#include <FileManager.hpp>

#include <arpa/inet.h>
#include <poll.h>
#include <unordered_map>

typedef struct sockaddr_in t_sockaddr_in;
typedef struct sockaddr t_sockaddr;
Expand All @@ -28,10 +32,14 @@ class HTTPServer
void acceptConnection(const pollfd &fd);
void logPollfd(const pollfd &fd) const;

bool is_print(char c);
int get_content_length(std::string search_string);

ConfigParser _parser;
// ThreadPool _thread_pool;
std::vector<pollfd> _fds;
std::vector<int> _server_fds;
std::unordered_map<int, HTTPRequest> _client_request;

public:
HTTPServer(const std::string &config_file_path);
Expand All @@ -45,4 +53,4 @@ class HTTPServer

#endif

/* ************************************************************************** */
/* ************************************************************************** */
1 change: 1 addition & 0 deletions include/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <iostream>
#include <sstream>
#include <string>
#include <chrono>

#include "Color.hpp"

Expand Down
Loading
Loading