-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPServer.hpp
104 lines (81 loc) · 2.79 KB
/
HTTPServer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#pragma once
#include <iostream>
#include <sstream>
#include <sys/socket.h>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <fstream>
#include <poll.h>
#include <unordered_map>
#include <vector>
#include <memory>
#include "Poll.hpp"
#include "Client.hpp"
#include "Server.hpp"
#include "Socket.hpp"
#include "HelperFuncs.hpp"
#include "Configuration.hpp"
#include "Logger.hpp"
#include "ConfigShared.hpp"
class HTTPServer : public ConfigShared {
public:
HTTPServer(Configuration &config, const Logger& l);
~HTTPServer();
std::vector<Server>::iterator getServerMutableIterator( void );
std::vector<Server>::iterator getServerMutableEnd( void );
std::vector<Server>& getServerVector( void );
std::vector<Socket>::iterator getSocketIterator( void );
std::vector<Socket>::iterator getSocketEnd( void );
std::vector<Client>& getClientVector( void );
std::vector<Client>::iterator getClientIterator( void );
std::vector<Client>::iterator getClientEnd( void );
bool getContinue( void );
void stopServer( void );
void startListening( void );
void doPollLoop( void );
inline static std::unique_ptr<HTTPServer> instance;
Logger l;
private:
bool continuing = true;
// A Socket is a file descriptor on which we listen for new clients.
// It is a separate entity from servers because multiple servers
// may listen on the same socket, or a different subset of them.
std::vector<Socket> sockets;
// A Server is a virtual server registered in the config that listens
// on at least one of our Sockets and describes how it wants to answer
// requests. When a Request comes in, it's matched to a Server, which
// constructs a Response for it.
std::vector<Server> servers;
// A Client is a currently open connection on which we might receive
// Requests, which we need to answer with a Response.
//
// Each Client handles one Request at a time, meaning its response
// will be built inside the Client class.
std::vector<Client> clients;
void setupSockets( void );
// Poll stuff
std::vector<pollfd> poll_vector;
std::unordered_map<int,ReadFileDescriptor*> read_fd_pointers;
std::unordered_map<int,WriteFileDescriptor*> write_fd_pointers;
void assemblePollQueue( void );
void runPoll( void );
void handleEvents( void );
void handleTimeouts( void );
void cleanUpPoll( void );
void addReadFileDescriptorToPoll(ReadFileDescriptor* read_fd);
void addWriteFileDescriptorToPoll(WriteFileDescriptor* read_fd);
public:
class Exception : public std::exception {
private:
const std::string _message;
public:
Exception(const std::string& reason) :
_message("HTTPServer Exception : " + reason) { }
virtual const char* what() const throw() {
return _message.c_str();
}
};
};