Skip to content

Commit d41d55d

Browse files
committed
Added support of HTTPS (via gnutls)
Changed the build system on QBS (for Linux)
1 parent 4af1db0 commit d41d55d

17 files changed

+527
-439
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
httpserverapp
22
=============
33

4-
Sample http application on C++
4+
Sample application for [C++ http server](https://github.com/awwit/httpserver).
55

6-
For http server on C++
6+
Dependencies
7+
------------
78

8-
See: https://github.com/awwit/httpserver
9+
Common:
10+
11+
* [gnutls](https://www.gnutls.org/)
12+
13+
Linux:
14+
15+
* dl
16+
* pthread
17+
18+
Build
19+
-----
20+
21+
Linux:
22+
23+
```sh
24+
git clone https://github.com/awwit/httpserverapp.git
25+
cd httpserverapp
26+
mkdir build
27+
cd build
28+
qbs build -f ./../projects/qt-creator/httpserverapp.qbs release
29+
```
30+
31+
License
32+
=======
33+
34+
The source codes are licensed under the
35+
[MIT](https://opensource.org/licenses/MIT),
36+
the full text of the license is located in the [LICENSE](LICENSE) file.

projects/qt-creator/httpserverapp.pro

Lines changed: 0 additions & 43 deletions
This file was deleted.

projects/qt-creator/httpserverapp.pro.user

Lines changed: 0 additions & 270 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import qbs
2+
3+
Project {
4+
DynamicLibrary {
5+
name: "httpserverapp"
6+
7+
Depends { name: "cpp" }
8+
cpp.cxxLanguageVersion: "c++11"
9+
10+
cpp.defines: qbs.buildVariant == "debug" ? base : base.concat(["DEBUG"])
11+
12+
cpp.dynamicLibraries: base.concat(["gnutls"])
13+
14+
Properties {
15+
condition: qbs.targetOS.contains("linux")
16+
cpp.defines: outer.concat(["POSIX"])
17+
cpp.dynamicLibraries: outer.concat(["dl", "pthread"])
18+
}
19+
20+
Properties {
21+
condition: qbs.targetOS.contains("windows")
22+
cpp.defines: outer.concat(["WIN32"])
23+
}
24+
25+
files: [
26+
"../../src/FileIncoming.cpp",
27+
"../../src/FileIncoming.h",
28+
"../../src/Main.cpp",
29+
"../../src/Main.h",
30+
"../../src/RawData.h",
31+
"../../src/ServerRequest.h",
32+
"../../src/ServerResponse.h",
33+
"../../src/Socket.cpp",
34+
"../../src/Socket.h",
35+
"../../src/SocketAdapter.cpp",
36+
"../../src/SocketAdapter.h",
37+
"../../src/SocketAdapterDefault.cpp",
38+
"../../src/SocketAdapterDefault.h",
39+
"../../src/SocketAdapterTls.cpp",
40+
"../../src/SocketAdapterTls.h",
41+
"../../src/System.cpp",
42+
"../../src/System.h",
43+
"../../src/Test.cpp",
44+
"../../src/Test.h",
45+
"../../src/Utils.cpp",
46+
"../../src/Utils.h",
47+
]
48+
}
49+
}

src/Main.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,22 @@ DLLEXPORT int application_call(HttpServer::server_request *request, HttpServer::
2828
Utils::parseCookies(it_cookie->second, cookies);
2929
}
3030

31+
// Create socket adapter
32+
uint8_t addr[sizeof(HttpServer::SocketAdapterTls)];
33+
34+
HttpServer::SocketAdapter *socket_adapter;
35+
36+
if (request->tls_session)
37+
{
38+
socket_adapter = new (addr) HttpServer::SocketAdapterTls(request->tls_session);
39+
}
40+
else
41+
{
42+
socket_adapter = new (addr) HttpServer::SocketAdapterDefault(request->socket);
43+
}
44+
3145
HttpServer::ServerRequest proc_request {
32-
HttpServer::Socket(request->socket),
46+
*socket_adapter,
3347
std::string(request->method),
3448
std::string(request->uri_reference),
3549
std::string(request->document_root),
@@ -41,15 +55,15 @@ DLLEXPORT int application_call(HttpServer::server_request *request, HttpServer::
4155
};
4256

4357
HttpServer::ServerResponse proc_response {
44-
HttpServer::Socket(request->socket),
58+
*socket_adapter,
4559
std::map<std::string, std::string>()
4660
};
4761

4862
const std::string absolute_path = proc_request.document_root + proc_request.uri_reference;
4963

5064
int result = EXIT_SUCCESS;
5165

52-
if (System::isFileExists(absolute_path) )
66+
if (std::string::npos == absolute_path.find("/../") && System::isFileExists(absolute_path) )
5367
{
5468
auto it_connection = proc_request.headers.find("Connection");
5569

@@ -65,6 +79,8 @@ DLLEXPORT int application_call(HttpServer::server_request *request, HttpServer::
6579
result = test(proc_request, proc_response);
6680
}
6781

82+
socket_adapter->~SocketAdapter();
83+
6884
if (proc_response.headers.size() )
6985
{
7086
Utils::raw_pair *headers;

src/ServerRequest.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

33
#include "RawData.h"
4-
#include "Socket.h"
4+
#include "SocketAdapterDefault.h"
5+
#include "SocketAdapterTls.h"
56
#include "FileIncoming.h"
67

78
#include <unordered_map>
@@ -11,6 +12,7 @@ namespace HttpServer
1112
struct server_request
1213
{
1314
const System::native_socket_type socket;
15+
const ::gnutls_session_t tls_session;
1416
const char *method;
1517
const char *uri_reference;
1618
const char *document_root;
@@ -27,7 +29,7 @@ namespace HttpServer
2729
/**
2830
* Структура запроса (входные данные)
2931
*
30-
* @member const Socket socket - сокет клиента
32+
* @member const SocketAdapter &socket - сокет клиента
3133
* @member const std::string method - метод применяемый к ресурсу
3234
* @member const std::string uri_reference - ссылка на ресурс
3335
* @member const std::string document_root - корневая директория приложения
@@ -39,7 +41,7 @@ namespace HttpServer
3941
*/
4042
struct ServerRequest
4143
{
42-
const Socket socket;
44+
const SocketAdapter &socket;
4345
const std::string method;
4446
const std::string uri_reference;
4547
const std::string document_root;
@@ -49,4 +51,4 @@ namespace HttpServer
4951
const std::unordered_multimap<std::string, FileIncoming> files;
5052
const std::unordered_multimap<std::string, std::string> cookies;
5153
};
52-
};
54+
};

src/ServerResponse.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "RawData.h"
4+
#include "SocketAdapter.h"
45

56
#include <map>
67
#include <string>
@@ -16,7 +17,7 @@ namespace HttpServer
1617

1718
struct ServerResponse
1819
{
19-
Socket socket;
20+
SocketAdapter &socket;
2021
std::map<std::string, std::string> headers;
2122
};
22-
};
23+
};

0 commit comments

Comments
 (0)