forked from georgesofianosgr/HTTP-Web-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·236 lines (201 loc) · 8.13 KB
/
main.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "httpserver.h"
#include "httprequest.h"
#include "httpresponse.h"
#include "filesystemrequesthandler.h"
#include "helprequesthandler.h"
#include <iostream>
#include <string>
#include <sstream>
#include <thread>
#include <system.io.directoryinfo.h>
#include <system.io.fileinfo.h>
#include <system.io.path.h>
#include <imgui.h>
#include <imgui_impl_win32gl2.h>
#include <imgui_applog.h>
#include <GL/gl.h>
static web::HttpServer server;
static ExampleAppLog appLog;
static FileSystemRequestHandler handler;
static HelpRequestHandler helpHandler;
static void startServer(System::IO::FileInfo exe)
{
server.WaitForRequests([exe] (const web::Request request, web::Response & response) -> int {
appLog.AddLog("Request recieved from %s for %s\n", request.ipAddress().c_str(), request._uri.c_str());
if (request._uri.substr(0, 5) == "/help")
{
return helpHandler.ConstructResponse(request, response);
}
return handler.ConstructResponse(request, response);
});
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
TCHAR szFileName[MAX_PATH + 1];
GetModuleFileName(NULL, szFileName, MAX_PATH + 1);
std::string exePath(szFileName);
server.SetLogging([] (const std::string& message) {
appLog.AddLog((message + "\n").c_str());
});
System::IO::FileInfo exe(exePath);
handler.SetRoot(exe.Directory().FullName());
handler.SetLogging([] (const std::string& message) {
appLog.AddLog(message.c_str());
appLog.AddLog("\n");
});
System::IO::FileInfo mimeTypesPath("mimetypes.config");
if (mimeTypesPath.Exists())
{
handler.GetMimeTypes().LoadFromFile(mimeTypesPath.FullName());
}
server.Init();
while (!server.Start())
{
server.SetPortA(server.Port() + 1);
if (server.Port() > 9999)
{
std::cerr << "No available port to listen on" << std::endl;
return 1;
}
}
std::stringstream title;
title << "Server listening @ localhost:" << server.Port();
bool done = false;
if (!ImGui_ImplWin32GL2_Init(title.str().c_str(), 800, 600))
{
std::cerr << "ImGui Init() failed";
return 0;
}
std::thread t(startServer, exe);
t.detach();
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
int windowWidth = 1, windowHeight = 1;
std::unique_ptr<bool[]> mimeTypeSelection(new bool[handler.GetMimeTypes().Data().size()]);
for (std::string::size_type i = 0; i < handler.GetMimeTypes().Data().size(); i++)
{
mimeTypeSelection[i] = false;
}
while (!done)
{
if (!ImGui_ImplWin32GL2_HandleEvents(done))
{
ImGui_ImplWin32GL2_NewFrame(windowWidth, windowHeight);
ImGui::Begin("Test", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
{
ImGui::SetWindowPos(ImVec2(0, 0));
ImGui::SetWindowSize(ImVec2(windowWidth / 2.0f, float(windowHeight)));
if (ImGui::Button("Open URL"))
{
ShellExecute(0, 0, server.LocalUrl().c_str(), 0, 0 , SW_SHOW);
}
ImGui::SameLine();
if (ImGui::Button("Open Webroot"))
{
ShellExecute(0, 0, exe.Directory().FullName().c_str(), 0, 0 , SW_SHOW);
}
ImGui::SameLine();
if (ImGui::Button("Quit"))
{
server.Stop();
done = true;
}
ImGui::SameLine();
if (ImGui::Button("Help"))
{
ShellExecute(0, 0, (server.LocalUrl() + "help").c_str(), 0, 0 , SW_SHOW);
}
ImGui::Text("Webroot @ ");
ImGui::SameLine();
ImGui::Text(exe.Directory().FullName().c_str());
ImGui::Text("Server listening @ ");
ImGui::SameLine();
ImGui::Text(server.LocalUrl().c_str());
appLog.Draw("Request Log");
ImGui::End();
}
ImGui::Begin("Mime types", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
{
ImGui::SetWindowPos(ImVec2(windowWidth / 2.0f, 0));
ImGui::SetWindowSize(ImVec2(windowWidth / 2.0f, float(windowHeight)));
static char Extension[8] = { 0 };
ImGui::InputText("Extension", Extension, 8);
static char Mimetype[64] = { 0 };
ImGui::InputText("Mimetype", Mimetype, 64);
auto mimetypes = handler.GetMimeTypes().Data();
if (ImGui::Button("Add Mimetype"))
{
if (Extension[0] == '.')
{
handler.GetMimeTypes().Add(Extension, Mimetype);
}
}
ImGui::Separator();
if (ImGui::Button("Delete selected"))
{
std::vector<std::string> extensionsToDelete;
int i = 0;
for (auto pair : mimetypes)
{
if (mimeTypeSelection[i++])
{
extensionsToDelete.push_back(pair.first);
std::cout << "removeing " << pair.first << std::endl;
}
}
for (auto r : extensionsToDelete)
{
handler.GetMimeTypes().Remove(r);
}
mimeTypeSelection = std::unique_ptr<bool[]>(new bool[handler.GetMimeTypes().Data().size()]);
for (std::string::size_type i = 0; i < handler.GetMimeTypes().Data().size(); i++)
{
mimeTypeSelection[i] = false;
}
}
ImGui::SameLine();
if (ImGui::Button("Clear selected"))
{
for (std::string::size_type i = 0; i < handler.GetMimeTypes().Data().size(); i++)
{
mimeTypeSelection[i] = false;
}
}
if (ImGui::Button("Save mimetypes"))
{
if (!handler.GetMimeTypes().SaveToFile(mimeTypesPath.FullName()))
{
appLog.AddLog("Failed to save mimetypes");
}
else
{
appLog.AddLog("Mimetypes saved");
}
}
ImGui::Columns(3, "Mime types", false);
ImGui::SetColumnWidth(-1, 80.0f);
ImGui::Text("Ext"); ImGui::NextColumn();
ImGui::SetColumnWidth(-1, 270.0f);
ImGui::Text("Mimetype"); ImGui::NextColumn();
ImGui::SetColumnWidth(-1, 100.0f);
ImGui::Text(""); ImGui::NextColumn();
ImGui::Separator();
int i = 0;
for (auto pair : mimetypes)
{
ImGui::Selectable(pair.first.c_str(), &(mimeTypeSelection[i])); ImGui::NextColumn();
ImGui::Selectable(pair.second.c_str(), &(mimeTypeSelection[i])); ImGui::NextColumn();
ImGui::NextColumn();
i++;
}
ImGui::End();
}
glViewport(0, 0, windowWidth, windowHeight);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui::Render();
ImGui_ImplWin32GL2_EndFrame();
}
}
ImGui_ImplWin32GL2_Shutdown();
return 0;
}