Meta: Symlink docs/relnotes/relnotes-2.0 to NEWS
[link.git] / Include / Link.hpp
blobd2746d4e219c253eaa66fe4aea575bf8b2fed4bb
1 #pragma once
2 #include <string>
3 #include <vector>
4 #include <map>
5 #include <openssl/ssl.h>
6 #include <functional>
8 namespace Link {
10 class Request {
12 public:
13 Request(std::string headers, std::string body);
14 Request(std::string url);
16 Link::Request* SetURL(std::string url),
17 * SetMethod(std::string method),
18 * SetHeader(std::string key, std::string value),
19 * SetCookie(std::string key, std::string value),
20 * SetParam(std::string key, std::string value),
21 * SetBody(std::string body),
22 * SetPath(std::string path),
23 * SetProtocol(std::string protocol),
24 * SetDomain(std::string domain),
25 * SetVersion(std::string version),
26 * SetHeadersRaw(std::string headersRaw),
27 * SetRawHeader(std::string key, std::string value),
28 * SetIP(std::string ip),
29 * SetPort(int port);
31 std::string GetURL(),
32 GetMethod(),
33 GetHeader(std::string key),
34 GetRawHeader(std::string key),
35 GetCookie(std::string key),
36 GetParam(std::string key),
37 GetBody(),
38 GetPath(),
39 GetProtocol(),
40 GetDomain(),
41 GetVersion(),
42 GetIP();
44 std::map<std::string, std::string> GetParams();
46 int GetPort();
48 std::string GetRawHeaders(), GetRawParams(), GetRawBody();
49 private:
50 std::map<std::string, std::string> headers, cookies, params;
51 std::string body, protocol, path, domain, url, method, version, ip;
52 int port;
56 class Response {
58 public:
59 Response();
60 Response(std::string headers, std::string body);
62 Response* SetHeader(std::string key, std::string value),
63 * SetBody(std::string body),
64 * SetHeadersRaw(std::string headersRaw),
65 * SetRawHeader(std::string key, std::string value),
66 * SetStatus(int status),
67 * SetVersion(std::string version),
68 * Close(),
69 * SetInstanceType(std::string type);
71 std::string GetHeader(std::string key),
72 GetBody(),
73 GetHeadersRaw(),
74 GetVersion();
75 int GetStatus();
76 bool isClosed();
77 bool InstanceOf(std::string type);
78 private:
79 std::map<std::string, std::string> headers;
80 std::string body, headersRaw, version, instanceType;
81 bool closed;
82 int status;
85 class Client {
87 public:
88 Client(Request* request);
89 Link::Response* Send();
91 Request* SetRequest(Request* request);
93 Request* GetRequest();
94 Response* GetResponse();
95 int Status;
96 private:
97 int Write(const void* buf, size_t count);
98 int Read(void* buf, size_t count);
99 bool getChunkSize(int& remaining, std::string& body);
100 Request* request;
101 Response* response;
102 SSL* ssl;
103 int sock;
107 class Server {
109 public:
110 Server();
111 Server(int port);
113 Server* SetPort(int port),
114 * Start(),
115 * Stop(),
116 * EnableMultiThreading(),
117 * DisableMultiThreading(),
118 * EnableDebugging(),
119 * EnableSSL(std::string certPath, std::string keyPath),
120 * Get(std::string path, std::function<void(Request*, Response*)> callback),
121 * Post(std::string path, std::function<void(Request*, Response*)> callback),
122 * Route(std::string method, std::string path, std::function<void(Request*, Response*)> callback),
123 * Error(int status, std::function<void(Request*, Response*)> callback),
124 * SetStaticPages(std::string path),
125 * SetStartMessage(std::string message),
126 * Use(std::function<void(Request*, Response*, Server*)> middleware);
128 int GetPort();
129 std::map<std::vector<std::string>, std::function<void(Request*, Response*)>> GetCallbacks();
130 std::vector<std::function<void(Request*, Response*, Server*)>> GetMiddlewares();
131 std::vector<std::string> GetStaticPages();
132 std::map<int, std::function<void(Request*, Response*)>> GetErrors();
133 bool IsRunning(), IsMultiThreaded(), IsSSL(), IsDebugging();
134 std::string GetStaticPagesDirectory();
135 int Status;
136 private:
137 int port, sock;
138 SSL_CTX* ctx;
139 bool running, sslEnabled, multiThreaded, debugging;
140 std::string certPath, keyPath, staticPages, startMessage;
141 std::map<std::vector<std::string>, std::function<void(Request*, Response*)>> callbacks;
142 std::vector<std::function<void(Request*, Response*, Server*)>> middlewares;
143 std::map<int, std::function<void(Request*, Response*)>> errors;
146 class Thread {
148 public:
149 Thread();
150 Thread(Server* server, int sock, bool sslEnabled);
151 Thread(Server* server, SSL* ssl, bool sslEnabled);
152 void SetIP(std::string ip), Run();
153 private:
154 int Write(const void* buf, size_t count);
155 int Read(void* buf, size_t count);
156 Server* server;
157 SSL* ssl;
158 bool sslEnabled;
159 int sock;
160 std::string ip;
163 class Target {
164 public:
165 Target(std::string host, std::string target);
166 Target* AddHost(std::string host);
167 std::vector<std::string> GetHosts();
168 std::string GetTarget();
169 bool Redirects();
170 private:
171 std::vector<std::string> hosts;
172 std::string target;
175 class Proxy {
176 public:
177 Proxy();
178 Proxy(std::vector<Target*> targets);
179 Proxy* AddTarget(Target* target), *Start(), *EnableHTTPRedirects();
180 std::vector<Target*> GetTargets();
181 bool Redirects();
182 private:
183 std::vector<Target*> targets;
184 bool redirects;
187 std::string Status(int status);
188 std::string GetMIMEType(std::string path);