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
1316Socket::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
2325void 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+
3145std::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
4358static void getIncomingVars (std::unordered_multimap<std::string, std::string> ¶ms, 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
214230void freeProtocolData (HttpClient::Response *response)
215231{
216- if (response)
217- {
232+ if (response) {
218233 delete response->prot ;
219234 }
220235}
221236
222237bool 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
0 commit comments