Skip to content

Commit 2f7c727

Browse files
committed
Fix getremoteip
1 parent afe5aff commit 2f7c727

4 files changed

Lines changed: 92 additions & 41 deletions

File tree

vendor/httpserver/include/client_session.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class client_session : public std::enable_shared_from_this<client_session>
126126
bool half_close = false;
127127

128128
unsigned char httpv = 0;
129+
std::string client_ip;
130+
unsigned int client_port = 0;
129131

130132
std::unique_ptr<asio::ip::tcp::socket> socket = nullptr;
131133
std::unique_ptr<asio::ssl::stream<asio::ip::tcp::socket>> sslsocket = nullptr;

vendor/httpserver/src/client_session.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,11 @@ asio::awaitable<void> client_session::async_stop()
516516
{
517517
if (isssl)
518518
{
519-
co_await sslsocket->async_shutdown(asio::use_awaitable);
520-
if (sslsocket->lowest_layer().is_open())
521-
{
522-
sslsocket->lowest_layer().close();
523-
}
519+
// co_await sslsocket->async_shutdown(asio::use_awaitable);
520+
// if (sslsocket->lowest_layer().is_open())
521+
// {
522+
// sslsocket->lowest_layer().close();
523+
// }
524524
}
525525
else
526526
{
@@ -547,18 +547,18 @@ void client_session::stop()
547547
{
548548
if (isssl)
549549
{
550-
asio::error_code ec;
551-
sslsocket->shutdown(ec);
552-
if(ec)
553-
{
554-
iserror = true;
555-
return;
556-
}
557-
558-
if (sslsocket->lowest_layer().is_open())
559-
{
560-
sslsocket->lowest_layer().close();
561-
}
550+
// asio::error_code ec;
551+
// sslsocket->shutdown(ec);
552+
// if(ec)
553+
// {
554+
// iserror = true;
555+
// return;
556+
// }
557+
558+
// if (sslsocket->lowest_layer().is_open())
559+
// {
560+
// sslsocket->lowest_layer().close();
561+
// }
562562
}
563563
else
564564
{
@@ -575,7 +575,10 @@ void client_session::stop()
575575

576576
std::string client_session::getremoteip()
577577
{
578-
std::string client_ip;
578+
if(client_ip.size() > 2)
579+
{
580+
return client_ip;
581+
}
579582
if (iserror)
580583
{
581584
return "";
@@ -592,7 +595,10 @@ std::string client_session::getremoteip()
592595
}
593596
unsigned int client_session::getremoteport()
594597
{
595-
unsigned int client_port = 0;
598+
if(client_port > 2)
599+
{
600+
return client_port;
601+
}
596602
if (iserror)
597603
{
598604
return 0;

vendor/httpserver/src/httpclient.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -485,27 +485,20 @@ bool client::init_http_sock()
485485
auto endpoints = resolver.resolve(host, port);
486486
//socket->connect(endpoints, ec);
487487
asio::connect(*sock, endpoints, ec);
488-
//asio::ip::tcp::resolver::query checkquery(host, port);
489-
// asio::ip::tcp::resolver::iterator iter = resolver.resolve(checkquery);
490-
// asio::ip::tcp::resolver::iterator end;
491488
// asio::ip::tcp::endpoint endpoint;
492-
// if (iter == end)
493-
// {
494-
// error_msg = "resolver " + host + port;
495-
// DEBUG_LOG("%s", error_msg.c_str());
496-
// return false;
497-
// }
498-
// while (iter != end)
499-
// {
500-
// endpoint = *iter++;
501-
// // at here maybe under code
489+
// for (const auto& entry : results) {
490+
// endpoint = entry.endpoint();
491+
// // ... 使用 endpoint
502492
// sock->connect(endpoint, ec);
503493
// if (ec)
504494
// {
505495
// continue;
506496
// }
497+
// else
498+
// {
499+
// break;
500+
// }
507501
// }
508-
// asio::error_code ec;
509502

510503
if (ec)
511504
{

vendor/httpserver/src/server.cpp

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,13 @@ asio::awaitable<void> httpserver::http1_send_file(std::shared_ptr<httppeer> peer
12871287
while (readnum < send_file_obj->content_length)
12881288
{
12891289
send_file_obj->content.resize(4096);
1290+
if(!send_file_obj->fp.get())
1291+
{
1292+
peer_session->isclose = true;
1293+
peer->isclose = true;
1294+
peer->state.keepalive = false;
1295+
break;
1296+
}
12901297
unsigned int nread = fread(&send_file_obj->content[0], 1, 4096, send_file_obj->fp.get());
12911298
if (nread == 0)
12921299
{
@@ -1441,6 +1448,13 @@ asio::awaitable<void> httpserver::http1_send_file_range(std::shared_ptr<httppeer
14411448
while (readnum < send_file_obj->content_length)
14421449
{
14431450
send_file_obj->content.resize(4096);
1451+
if(!send_file_obj->fp.get())
1452+
{
1453+
peer_session->isclose = true;
1454+
peer->isclose = true;
1455+
peer->state.keepalive = false;
1456+
break;
1457+
}
14441458
unsigned int nread = fread(&send_file_obj->content[0], 1, 4096, send_file_obj->fp.get());
14451459
if (nread == 0)
14461460
{
@@ -2663,6 +2677,17 @@ bool httpserver::http2_loop_send_sequence(std::shared_ptr<http2_send_data_t> sq_
26632677
// send file
26642678
if (sq_obj->current_num < sq_obj->content_length)
26652679
{
2680+
if(!sq_obj->fp.get())
2681+
{
2682+
peer->socket_session->http2_send_enddata(peer->stream_id);
2683+
peer->issend = true;
2684+
sq_obj->standby_next = true;
2685+
if (peer->socket_session->http2_need_wakeup)
2686+
{
2687+
peer->socket_session->waituphttp2(this->io_context);
2688+
}
2689+
return false;
2690+
}
26662691
per_size = fread(&sq_obj->cache_data[9], 1, vsize_send, sq_obj->fp.get());
26672692
if (per_size == 0 && vsize_send > 0)
26682693
{
@@ -3239,6 +3264,7 @@ void httpserver::listeners()
32393264
unsigned long long temp_domain = 0;
32403265
SSL_CTX_set_alpn_select_cb(context_.native_handle(), alpn_cb, (void *)temp_domain);
32413266

3267+
unsigned int error_count = 0;
32423268
for (;;)
32433269
{
32443270
try
@@ -3254,17 +3280,30 @@ void httpserver::listeners()
32543280
if (ec_error)
32553281
{
32563282
std::unique_lock<std::mutex> lock(log_mutex);
3257-
error_loglist.emplace_back("https accept ec_error ");
3283+
error_loglist.emplace_back("https accept ec_error\n");
32583284
lock.unlock();
3259-
std::this_thread::sleep_for(std::chrono::nanoseconds(200));
3285+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
3286+
error_count++;
3287+
if(error_count > 128)
3288+
{
3289+
isstop = true;
3290+
}
32603291
continue;
32613292
}
3293+
total_http2_count++;
3294+
//The IP should be available now
3295+
peer_session->getremoteip();
3296+
peer_session->getremoteport();
3297+
32623298
#ifndef BENCHMARK
32633299
std::unique_lock<std::mutex> lock_sock(socket_session_lists_mutex);
32643300
socket_session_lists.push_back(peer_session);
32653301
lock_sock.unlock();
32663302
#endif
3267-
total_http2_count++;
3303+
3304+
3305+
3306+
32683307
co_spawn(this->io_context, sslhandshake(peer_session), asio::detached);
32693308
if (isstop)
32703309
{
@@ -3275,7 +3314,7 @@ void httpserver::listeners()
32753314
catch (...)
32763315
{
32773316
std::unique_lock<std::mutex> lock(log_mutex);
3278-
error_loglist.emplace_back("https accept ec_error ");
3317+
error_loglist.emplace_back("https accept catch\n");
32793318
lock.unlock();
32803319
}
32813320
if (isstop)
@@ -3316,6 +3355,7 @@ void httpserver::listener()
33163355
exit(1);
33173356
}
33183357
DEBUG_LOG("http accept");
3358+
unsigned int error_count = 0;
33193359
for (;;)
33203360
{
33213361
try
@@ -3331,17 +3371,27 @@ void httpserver::listener()
33313371
if (ec)
33323372
{
33333373
std::unique_lock<std::mutex> lock(log_mutex);
3334-
error_loglist.emplace_back("http accept ec_error ");
3374+
error_loglist.emplace_back("http accept ec_error\n");
33353375
lock.unlock();
3336-
std::this_thread::sleep_for(std::chrono::nanoseconds(200));
3376+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
3377+
error_count++;
3378+
if(error_count > 128)
3379+
{
3380+
isstop = true;
3381+
}
33373382
continue;
33383383
}
3384+
total_http1_count++;
3385+
//The IP should be available now
3386+
peer_session->getremoteip();
3387+
peer_session->getremoteport();
3388+
33393389
#ifndef BENCHMARK
33403390
std::unique_lock<std::mutex> lock_sock(socket_session_lists_mutex);
33413391
socket_session_lists.push_back(peer_session);
33423392
lock_sock.unlock();
33433393
#endif
3344-
total_http1_count++;
3394+
33453395
co_spawn(this->io_context, clientpeerfun(peer_session, false), asio::detached);
33463396
if (isstop)
33473397
{
@@ -3352,7 +3402,7 @@ void httpserver::listener()
33523402
catch (...)
33533403
{
33543404
std::unique_lock<std::mutex> lock(log_mutex);
3355-
error_loglist.emplace_back("http accept ec_error ");
3405+
error_loglist.emplace_back("http accept catch\n");
33563406
lock.unlock();
33573407
}
33583408
if (isstop)

0 commit comments

Comments
 (0)