Skip to content

Commit 834abbb

Browse files
committed
Parsing cookies for HTTP/2 protocol. Code styling
1 parent 946e0dc commit 834abbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+681
-796
lines changed

projects/qt-creator/httpserverapp.qbs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ Project {
2626
"../../src/Init.h",
2727
"../../src/application/Test.cpp",
2828
"../../src/application/Test.h",
29-
"../../src/client/protocol/ClientHttp1.cpp",
30-
"../../src/client/protocol/ClientHttp1.h",
31-
"../../src/client/protocol/ClientHttp2.cpp",
32-
"../../src/client/protocol/ClientHttp2.h",
33-
"../../src/client/protocol/ClientProtocol.cpp",
34-
"../../src/client/protocol/ClientProtocol.h",
35-
"../../src/client/protocol/WebSocket.cpp",
36-
"../../src/client/protocol/WebSocket.h",
29+
"../../src/server/protocol/ClientHttp1.cpp",
30+
"../../src/server/protocol/ClientHttp1.h",
31+
"../../src/server/protocol/ClientHttp2.cpp",
32+
"../../src/server/protocol/ClientHttp2.h",
33+
"../../src/server/protocol/ClientProtocol.cpp",
34+
"../../src/server/protocol/ClientProtocol.h",
35+
"../../src/server/protocol/WebSocket.cpp",
36+
"../../src/server/protocol/WebSocket.h",
3737
"../../src/utils/Event.cpp",
3838
"../../src/utils/Event.h",
3939
"../../src/transfer/FileIncoming.cpp",
@@ -45,10 +45,10 @@ Project {
4545
"../../src/transfer/HttpStatusCode.h",
4646
"../../src/Main.cpp",
4747
"../../src/Main.h",
48-
"../../src/client/Request.cpp",
49-
"../../src/client/Request.h",
50-
"../../src/client/Response.cpp",
51-
"../../src/client/Response.h",
48+
"../../src/server/Request.cpp",
49+
"../../src/server/Request.h",
50+
"../../src/server/Response.cpp",
51+
"../../src/server/Response.h",
5252
"../../src/transfer/ProtocolVariant.h",
5353
"../../src/transfer/AppRequest.h",
5454
"../../src/transfer/AppResponse.h",

projects/qt-creator/httpserverapp.qbs.user

Lines changed: 246 additions & 0 deletions
Large diffs are not rendered by default.

src/Init.cpp

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
#include "transfer/http2/Http2.h"
88
#include "utils/Utils.h"
99

10-
#include "client/protocol/ClientHttp1.h"
11-
#include "client/protocol/ClientHttp2.h"
10+
#include "server/protocol/ClientHttp1.h"
11+
#include "server/protocol/ClientHttp2.h"
12+
13+
#include <locale>
14+
#include <codecvt>
1215

1316
Socket::Adapter *createSocketAdapter(Transfer::app_request *request, void *addr)
1417
{
15-
if (request->tls_session)
16-
{
18+
if (request->tls_session) {
1719
return new (addr) Socket::AdapterTls(request->tls_session);
1820
}
1921

@@ -22,46 +24,56 @@ Socket::Adapter *createSocketAdapter(Transfer::app_request *request, void *addr)
2224

2325
void destroySocketAdapter(Socket::Adapter *adapter)
2426
{
25-
if (adapter)
26-
{
27+
if (adapter) {
2728
adapter->~Adapter();
2829
}
2930
}
3031

32+
std::string utf8ToLocal(const std::string &u8str)
33+
{
34+
std::locale loc("");
35+
36+
std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;
37+
std::wstring wstr = conv.from_bytes(u8str);
38+
39+
std::string str(wstr.size(), 0);
40+
std::use_facet<std::ctype<wchar_t> >(loc).narrow(wstr.data(), wstr.data() + wstr.size(), '?', &str.front() );
41+
42+
return str;
43+
}
44+
3145
std::string getClearPath(const std::string &path)
3246
{
3347
const size_t pos = path.find_first_of("?#");
3448

35-
if (std::string::npos == pos)
36-
{
37-
return Utils::urlDecode(path);
38-
}
49+
const std::string clean = Utils::urlDecode(std::string::npos == pos ? path : path.substr(0, pos) );
3950

40-
return Utils::urlDecode(path.substr(0, pos) );
51+
#ifdef WIN32
52+
return utf8ToLocal(clean);
53+
#else
54+
return clean;
55+
#endif
4156
}
4257

4358
static void getIncomingVars(std::unordered_multimap<std::string, std::string> &params, const std::string &uri)
4459
{
4560
const size_t start = uri.find('?');
4661

47-
if (std::string::npos == start)
48-
{
62+
if (std::string::npos == start) {
4963
return;
5064
}
5165

5266
const size_t finish = uri.find('#');
5367

54-
if (finish < start)
55-
{
68+
if (finish < start) {
5669
return;
5770
}
5871

5972
for (size_t var_pos = start + 1, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1)
6073
{
6174
var_end = uri.find('&', var_pos);
6275

63-
if (var_end > finish)
64-
{
76+
if (var_end > finish) {
6577
var_end = std::string::npos;
6678
}
6779

@@ -121,8 +133,7 @@ bool initServerObjects(HttpClient::Request *procRequest, HttpClient::Response *p
121133

122134
auto const it_cookie = headers.find("cookie");
123135

124-
if (headers.cend() != it_cookie)
125-
{
136+
if (headers.cend() != it_cookie) {
126137
Utils::parseCookies(it_cookie->second, cookies);
127138
}
128139

@@ -171,6 +182,12 @@ bool initServerObjects(HttpClient::Request *procRequest, HttpClient::Response *p
171182
src = Utils::unpackContainer(data, src);
172183
src = Utils::unpackFilesIncoming(files, src);
173184

185+
auto const it_cookie = headers.find("cookie");
186+
187+
if (headers.cend() != it_cookie) {
188+
Utils::parseCookies(it_cookie->second, cookies);
189+
}
190+
174191
getIncomingVars(params, path);
175192

176193
Http2::OutStream *stream = new Http2::OutStream(stream_id, settings, Http2::DynamicTable(settings.header_table_size, settings.max_header_list_size, std::move(dynamic_table) ), mtx);
@@ -180,8 +197,7 @@ bool initServerObjects(HttpClient::Request *procRequest, HttpClient::Response *p
180197
break;
181198
}
182199

183-
default:
184-
{
200+
default: {
185201
success = false;
186202
break;
187203
}
@@ -213,64 +229,74 @@ bool initServerObjects(HttpClient::Request *procRequest, HttpClient::Response *p
213229

214230
void freeProtocolData(HttpClient::Response *response)
215231
{
216-
if (response)
217-
{
232+
if (response) {
218233
delete response->prot;
219234
}
220235
}
221236

222237
bool isSwitchingProtocols(const HttpClient::Request &request, HttpClient::Response &response)
223238
{
224-
if (request.prot->getSocket()->get_tls_session() != 0)
225-
{
239+
// Check for https is not set
240+
if (request.prot->getSocket()->get_tls_session() != 0) {
226241
return false;
227242
}
228243

244+
// Check for upgrade to https
245+
/*auto const it_upgrade_insecure = request.headers.find("upgrade-insecure-requests");
246+
247+
if (request.headers.cend() != it_upgrade_insecure) {
248+
if (it_upgrade_insecure->second == "1") {
249+
response.status = Http::StatusCode::MOVED_TEMPORARILY;
250+
response.headers["location"] = "https://" + request.host + request.path;
251+
response.headers["strict-transport-security"] = "max-age=86400";
252+
253+
const std::string headers = "HTTP/1.1 307 Moved Temporarily\r\nLocation: https://" + request.host + request.path + "\r\nStrict-Transport-Security: max-age=86400\r\n\r\n";
254+
255+
response.prot->getSocket()->nonblock_send(headers, std::chrono::milliseconds(5000) );
256+
257+
return true;
258+
}
259+
}*/
260+
261+
// Check if switch protocol to h2c
229262
auto const it_upgrade = request.headers.find("upgrade");
230263

231-
if (request.headers.cend() == it_upgrade)
232-
{
264+
if (request.headers.cend() == it_upgrade) {
233265
return false;
234266
}
235267

236268
auto const it_connection = request.headers.find("connection");
237269

238-
if (request.headers.cend() == it_connection)
239-
{
270+
if (request.headers.cend() == it_connection) {
240271
return false;
241272
}
242273

243274
std::vector<std::string> list = Utils::explode(it_connection->second, ',');
244275

245276
bool is_upgrade = false;
246277

247-
for (auto &item : list)
248-
{
278+
for (auto &item : list) {
249279
Utils::toLower(item);
250280

251-
if ("upgrade" == item)
252-
{
281+
if ("upgrade" == item) {
253282
is_upgrade = true;
254283
break;
255284
}
256285
}
257286

258-
if (false == is_upgrade)
259-
{
287+
if (false == is_upgrade) {
260288
return false;
261289
}
262290

263291
const std::string &upgrade = it_upgrade->second;
264292

265-
if ("h2c" != upgrade)
266-
{
293+
if ("h2c" != upgrade) {
267294
return false;
268295
}
269296

270297
auto const it_settings = request.headers.find("http2-settings");
271298

272-
if (request.headers.cend() == it_settings)
273-
{
299+
if (request.headers.cend() == it_settings) {
274300
return false;
275301
}
276302

src/Init.h

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

3-
#include "client/Request.h"
4-
#include "client/Response.h"
3+
#include "server/Request.h"
4+
#include "server/Response.h"
55
#include "transfer/AppRequest.h"
66
#include "transfer/AppResponse.h"
77

src/Main.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ EXPORT int application_call(Transfer::app_request *request, Transfer::app_respon
2222
HttpClient::Request proc_request;
2323
HttpClient::Response proc_response;
2424

25-
if (false == initServerObjects(&proc_request, &proc_response, request, socket_adapter) )
26-
{
25+
if (false == initServerObjects(&proc_request, &proc_response, request, socket_adapter) == false) {
2726
return EXIT_FAILURE;
2827
}
2928

@@ -54,8 +53,7 @@ EXPORT int application_call(Transfer::app_request *request, Transfer::app_respon
5453

5554
destroySocketAdapter(socket_adapter);
5655

57-
if (proc_response.headers.size() )
58-
{
56+
if (proc_response.headers.size() ) {
5957
response->data_size = Utils::getPackContainerSize(proc_response.headers);
6058
response->response_data = new uint8_t[response->data_size];
6159
Utils::packContainer(reinterpret_cast<uint8_t *>(response->response_data), proc_response.headers);
@@ -68,8 +66,7 @@ EXPORT int application_call(Transfer::app_request *request, Transfer::app_respon
6866

6967
EXPORT void application_clear(void *response_data, size_t response_size)
7068
{
71-
if (response_data && response_size)
72-
{
69+
if (response_data && response_size) {
7370
delete[] reinterpret_cast<uint8_t *>(response_data);
7471
}
7572
}

src/application/Test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,4 @@ namespace Application
145145

146146
return EXIT_SUCCESS;
147147
}
148-
};
148+
}

src/application/Test.h

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

3-
#include "../client/Request.h"
4-
#include "../client/Response.h"
3+
#include "../server/Request.h"
4+
#include "../server/Response.h"
55

66
namespace Application
77
{
88
bool test(HttpClient::Request &, HttpClient::Response &);
9-
};
9+
}
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,18 @@
33

44
namespace HttpClient
55
{
6-
std::string Request::getHeader(const std::string &key) const
7-
{
6+
std::string Request::getHeader(const std::string &key) const {
87
auto it = headers.find(key);
9-
10-
return headers.end() != it ? it->second : "";
8+
return headers.end() != it ? it->second : std::string();
119
}
1210

13-
bool Request::isDataExists(const std::string &key) const
14-
{
11+
bool Request::isDataExists(const std::string &key) const {
1512
return data.cend() != data.find(key);
1613
}
1714

18-
std::string Request::getDataAsString(const std::string &key) const
19-
{
15+
std::string Request::getDataAsString(const std::string &key) const {
2016
auto it = data.find(key);
21-
22-
return data.end() != it ? it->second : "";
17+
return data.end() != it ? it->second : std::string();
2318
}
2419

2520
std::vector<std::string> Request::getDataAsArray(const std::string &key) const
@@ -28,27 +23,23 @@ namespace HttpClient
2823

2924
size_t count = data.count(key);
3025

31-
if (count)
32-
{
26+
if (count) {
3327
auto range = data.equal_range(key);
3428

3529
arr.resize(count);
3630

3731
size_t i = 0;
3832

39-
for (auto it = range.first; it != range.second; ++it)
40-
{
33+
for (auto it = range.first; it != range.second; ++it) {
4134
arr[i++] = it->second;
4235
}
4336
}
4437

4538
return arr;
4639
}
4740

48-
std::string Request::getCookieAsString(const std::string &cookieName) const
49-
{
41+
std::string Request::getCookieAsString(const std::string &cookieName) const {
5042
auto it = cookies.find(cookieName);
51-
52-
return cookies.end() != it ? it->second : "";
43+
return cookies.end() != it ? it->second : std::string();
5344
}
54-
};
45+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ namespace HttpClient
4747

4848
std::string getCookieAsString(const std::string &cookieName) const;
4949
};
50-
};
50+
}

0 commit comments

Comments
 (0)