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

Last improvements #40

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c1c6ce3
draft CGI but need to split it up and implement with poll
Nov 9, 2023
04305c5
added comments in handle connection for structure cgi
Nov 9, 2023
9a80ae9
took some steps in the right direction
Nov 15, 2023
3f21c8b
cgi send and receive almost there
Nov 16, 2023
5746c4e
parseURIForCGI is finished and did some things in CGI cpp as well and…
Nov 17, 2023
af2315f
update parseURIForCGI
Nov 22, 2023
a690ab7
added getters for the parameters of execve (startCGI), also made a se…
Nov 22, 2023
35a723c
added getBodyLength method to HTTPRequest class
Nov 22, 2023
cce7485
server back running but the file manager is giving problems
Nov 23, 2023
9321a8b
managePost now functional with the brains of sala
Nov 23, 2023
cf2e772
some improvements on the CGI, currently stuck on parseURIForCGI metho…
Nov 23, 2023
61c2e95
stuck at the executable but big steps are taken
Nov 24, 2023
f38b390
some improvents, new focus: CGI::receive
Nov 29, 2023
21759b6
improvents cgi
Nov 30, 2023
66077eb
python file and get_cgi text file
Nov 30, 2023
ba44ce6
added fcntl to GCI start
Nov 30, 2023
422ec6d
fixed findClientByFd, next step is using this in handleExistingConnec…
Nov 30, 2023
bdd718a
added getters for serverToCgiFd and cgiToServerFd and now giving Clie…
Dec 1, 2023
513717a
now using the pipes from the client in the cgi, it compiles so commit
Dec 1, 2023
c828151
it seems to work, need to look into the filemanager because there is …
Dec 1, 2023
45e89f7
added FileManager::manageCgi and improved CGI::receive
Dec 6, 2023
6c771c7
cgi works!
Dec 6, 2023
3c627be
now at CGI::write
Dec 6, 2023
6f68d8d
small adjustments
Dec 7, 2023
0b43658
the cgi is f*cking working!
Dec 7, 2023
cad2502
a lot of cleaning up in CGI.cpp
Dec 7, 2023
1e1a3e2
did some cleaning up for the client.cpp
Dec 7, 2023
d325322
did some cleaning up on HTTPServer.cpp
Dec 7, 2023
5164449
some additions and cleaning up
Dec 7, 2023
5070531
post_cgi also working with additional path and query_string, and got …
Dec 8, 2023
a8c0357
added a comment for mr Marty
Dec 8, 2023
f66ddb2
throwing client exceptions in the cgi's child process is now being ha…
Dec 13, 2023
7af6bbf
fixed a stuped copy pasta mistake
Dec 13, 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
17 changes: 17 additions & 0 deletions data/www/test_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
import select

def main():


print('From cgi: waiting on the servers message', file=sys.stderr)

print(sys.stdin.readlines())

# if input_data.strip():
# sys.stdout.write(input_data)

print('From cgi: after writing to the server', file=sys.stderr)

if __name__ == "__main__":
main()
37 changes: 34 additions & 3 deletions include/CGI.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
#ifndef CGI_HPP
#define CGI_HPP

#include <ClientState.hpp>
#include <HTTPRequest.hpp>
#include <string>
#include <memory>
#include <unordered_map>

#define READ_END 0
#define WRITE_END 1

// Common gateway interface
class Client;
class Poll;

class CGI
{
private:
private:
pid_t _pid;
size_t _bodyBytesWritten;
std::string _executable;
std::string _pathInfo;
std::string _subPathInfo;
std::string _queryString;

public:
CGI();
CGI(const CGI &src) = delete;
CGI &operator=(const CGI &rhs) = delete;
~CGI();
void execute();

ClientState start(Poll &poll, Client &client, size_t body_length,
std::unordered_map<int, std::shared_ptr<int>> &active_pipes);
ClientState parseURIForCGI(std::string requestTarget);
void execute(std::string executable);
bool fileExists(const std::string& filePath);
bool isExecutable(const std::string& filePath);

const std::string& getExecutable(void) const;
void setExecutable(std::string executable);
const pid_t& getPid(void) const;

ClientState send(Client &client ,std::string body, size_t bodyLength);
size_t getBufferSize(size_t bodyLength);
ClientState receive(Client &client);

std::string body;
int pipe_fd[2];
};

#endif
27 changes: 20 additions & 7 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <HTTPRequest.hpp>
#include <HTTPResponse.hpp>
#include <Socket.hpp>
#include <memory>
#include <unordered_map>

class Client
{
Expand All @@ -16,16 +18,27 @@ class Client
const Client &operator=(const Client &other) = delete;
~Client();

ClientState handleConnection(short events);
ClientState handleConnection(short events, Poll &poll, Client &client,
std::unordered_map<int, std::shared_ptr<int>> &active_pipes);
int getFD(void) const;
int *getCgiToServerFd(void);
int *getServerToCgiFd(void);
HTTPRequest &getRequest(void);
void setState(ClientState state);

bool cgiBodyIsSent;
bool cgiHasBeenRead;
bool KO;

private:
HTTPRequest _request;
HTTPResponse _response;
FileManager _file_manager;
CGI _cgi;
Socket _socket;
ClientState _state;
HTTPRequest _request;
HTTPResponse _response;
FileManager _file_manager;
CGI _cgi;
Socket _socket;
ClientState _state;
int _serverToCgiFd[2];
int _cgiToServerFd[2];
};

#endif
5 changes: 4 additions & 1 deletion include/ClientState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
enum class ClientState
{
Receiving,
CGI_Start,
CGI_Write,
CGI_Read,
Loading,
Sending,
Done,
Error,
Unkown,
Unknown,
};

#endif
9 changes: 7 additions & 2 deletions include/FileManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
class FileManager
{
private:
std::string _response;
std::fstream _request_target;
std::string _response;
std::fstream _request_target;
size_t _bytes_sent;
// CGI _cgi;

public:
FileManager();
Expand All @@ -26,10 +28,13 @@ class FileManager
ClientState loadErrorPage(void);
ClientState manage(HTTPMethod method, const std::string &filename,
const std::string &body);
ClientState manageCgi(std::string http_version, const std::string &body);
ClientState manageGet(void);
ClientState managePost(const std::string &body);
ClientState manageDelete(const std::string &reqest_target_path);

// CGI APPEND FUNCTION

const std::string &getResponse(void) const;
};

Expand Down
12 changes: 9 additions & 3 deletions include/HTTPRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ class HTTPRequest
const std::string &getBody(void) const;
ClientState receive(int fd);

const std::string &getExecutable(void) const;
void setCGIToTrue(void);
const bool &CGITrue(void) const;
const size_t &getBodyLength(void) const;

private:
ssize_t _bytes_read;
size_t _content_length;
HTTPMethod _methodType;
ssize_t _bytes_read;
size_t _content_length;
HTTPMethod _methodType;
std::string _http_request;
std::string _request_target;
std::string _http_version;
std::string _body;
std::unordered_map<std::string, std::string> _headers;
bool _cgi;

size_t parseStartLine(size_t &i);
size_t parseHeaders(size_t &i);
Expand Down
8 changes: 7 additions & 1 deletion include/HTTPServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ class HTTPServer
Poll _poll;
std::unordered_map<int, std::shared_ptr<Server>> _active_servers;
std::unordered_map<int, std::shared_ptr<Client>> _active_clients;
std::unordered_map<int, std::shared_ptr<int>> _active_pipes;

void setupServers(void);
void handleActivePollFDs();
void handleNewConnection(int fd);
void handleExistingConnection(const pollfd &poll_fd);
void handleExistingConnection(const pollfd &poll_fd, Poll &poll, Client &client,
std::unordered_map<int, std::shared_ptr<int>> &active_pipes);
Client &findClientByFd(int targetFd);
Client &getClientByPipeFd(int pipe_fd);
void handlePipeConnection(const pollfd &poll_fd, Poll &poll, Client &client,
std::unordered_map<int, std::shared_ptr<int>> &active_pipes);
};

#endif
3 changes: 3 additions & 0 deletions include/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <netinet/in.h>
#include <string>

// MAX_PENDING_CONNECTION SHOULDN'T BE 1
// WHY 10? WE CAN ALSO SET IT ON 1024, THAT'S
// MORE SAFE, CONCERNING THE AVAILABILITY (siege -b)
#define MAX_PENDING_CONNECTIONS 10

typedef struct sockaddr_in t_sockaddr_in;
Expand Down
2 changes: 0 additions & 2 deletions out.txt
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
['hello world!\n', 'goodbye world!\n']
Content-type: text/html
Loading
Loading