forked from awwit/httpserverapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
97 lines (75 loc) · 2.34 KB
/
Main.cpp
File metadata and controls
97 lines (75 loc) · 2.34 KB
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
#include "Main.h"
#include "Test.h"
#include <fstream>
DLLEXPORT bool application_init()
{
return true;
}
DLLEXPORT int application_call(HttpServer::server_request *request, HttpServer::server_response *response)
{
std::unordered_multimap<std::string, std::string> params;
std::unordered_map<std::string, std::string> headers;
std::unordered_multimap<std::string, std::string> data;
std::unordered_multimap<std::string, HttpServer::FileIncoming> files;
std::unordered_multimap<std::string, std::string> cookies;
Utils::rawPairsToStlUnorderedMultimap(params, request->params, request->params_count);
Utils::rawPairsToStlUnorderedMap(headers, request->headers, request->headers_count);
Utils::rawPairsToStlUnorderedMultimap(data, request->data, request->data_count);
Utils::rawFilesInfoToFilesIncoming(files, request->files, request->files_count);
auto it_cookie = headers.find("Cookie");
if (headers.end() != it_cookie)
{
Utils::parseCookies(it_cookie->second, cookies);
}
HttpServer::ServerRequest proc_request {
HttpServer::Socket(request->socket),
std::string(request->method),
std::string(request->uri_reference),
std::string(request->document_root),
std::move(params),
std::move(headers),
std::move(data),
std::move(files),
std::move(cookies)
};
HttpServer::ServerResponse proc_response {
HttpServer::Socket(request->socket),
std::map<std::string, std::string>()
};
std::string absolute_path = proc_request.document_root + proc_request.uri_reference;
int result = EXIT_SUCCESS;
std::ifstream file(absolute_path, std::ifstream::binary);
if (file)
{
auto it_connection = proc_request.headers.find("Connection");
if (proc_request.headers.cend() != it_connection)
{
proc_response.headers["Connection"] = it_connection->second;
}
proc_response.headers["X-Sendfile"] = absolute_path;
}
else
{
result = test(proc_request, proc_response);
}
file.close();
if (proc_response.headers.size() )
{
Utils::raw_pair *headers;
Utils::stlMapToRawPairs(&headers, proc_response.headers);
response->headers_count = proc_response.headers.size();
response->headers = headers;
}
return result;
}
DLLEXPORT void application_clear(Utils::raw_pair headers[], const size_t headers_count)
{
if (headers && headers_count)
{
destroyRawPairs(headers, headers_count);
}
}
DLLEXPORT void application_final()
{
}