Skip to content

Commit

Permalink
fix HTTP1 send range,change str_cmpcase
Browse files Browse the repository at this point in the history
  • Loading branch information
hggq committed Oct 11, 2024
1 parent 602d3e2 commit 6746f88
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 44 deletions.
8 changes: 4 additions & 4 deletions vendor/httpserver/include/func.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ namespace http
{
bool str_cmp(std::string_view str1, std::string_view str2);
bool str_casecmp(std::string_view str1, std::string_view str2);
bool str_compare(std::string_view str1, std::string_view str2, unsigned int length);
bool str_casecompare(std::string_view str1, std::string_view str2, unsigned int length);
bool str_lastcmp(std::string_view str1, std::string_view str2, unsigned int length);
bool str_caselastcmp(std::string_view str1, std::string_view str2, unsigned int length);
bool str_cmp_pre(std::string_view str1, std::string_view str2, unsigned int length=0);
bool str_casecmp_pre(std::string_view str1, std::string_view str2, unsigned int length=0);
bool str_cmp_last(std::string_view str1, std::string_view str2, unsigned int length=0);
bool str_casecmp_last(std::string_view str1, std::string_view str2, unsigned int length=0);
void get_filename(const std::string &filename, std::string &filename_name, std::string &filename_ext);
std::string get_filename(const std::string &filename);
std::vector<std::string> mb_split(std::string_view, std::string_view);
Expand Down
2 changes: 1 addition & 1 deletion vendor/httpserver/include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class httpserver
std::shared_ptr<client_session> peer_session,
const std::string &filename);

bool http1_send_file_range(unsigned int streamid,
asio::awaitable<void> http1_send_file_range(unsigned int streamid,
std::shared_ptr<httppeer> peer,
std::shared_ptr<client_session> peer_session,
const std::string &filename);
Expand Down
68 changes: 40 additions & 28 deletions vendor/httpserver/src/func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,19 @@ bool str_casecmp(std::string_view str1, std::string_view str2)
}
return true;
}
bool str_compare(std::string_view str1, std::string_view str2, unsigned int length)
bool str_cmp_pre(std::string_view str1, std::string_view str2, unsigned int length)
{
if(length==0)
{
length = str2.size();
}
if(str1.size() < str2.size())
{
return false;
}
for (unsigned int i = 0; i < length; i++)
{
if (i < str1.size() && i < str2.size())
if (i < str2.size())
{
if (str1[i] == str2[i])
{
Expand All @@ -86,24 +94,26 @@ bool str_compare(std::string_view str1, std::string_view str2, unsigned int leng
{
return false;
}
}
if (i == str1.size())
{
return true;
}
if (i == str2.size())
}else
{
return true;
}
return false;
}
return true;
}
bool str_casecompare(std::string_view str1, std::string_view str2, unsigned int length)
bool str_casecmp_pre(std::string_view str1, std::string_view str2, unsigned int length)
{
if(length==0)
{
length = str2.size();
}
if(str1.size() < str2.size())
{
return false;
}
for (unsigned int i = 0; i < length; i++)
{
if (i < str1.size() && i < str2.size())
if (i < str2.size())
{
if (str1[i] == str2[i])
{
Expand All @@ -127,21 +137,19 @@ bool str_casecompare(std::string_view str1, std::string_view str2, unsigned int
}
return false;
}
}
if (i == str1.size())
{
return true;
}
if (i == str2.size())
}else
{
return true;
}
return false;
}
return true;
}
bool str_lastcmp(std::string_view str1, std::string_view str2, unsigned int length)
bool str_cmp_last(std::string_view str1, std::string_view str2, unsigned int length)
{
if(length==0)
{
length = str2.size();
}
int a = (int)str1.size();
int b = (int)str2.size();
if (a == 0)
Expand All @@ -152,17 +160,17 @@ bool str_lastcmp(std::string_view str1, std::string_view str2, unsigned int leng
{
return false;
}
if(a < b)
{
return false;
}
a = a - 1;
b = b - 1;

for (unsigned int i = 0; i < length; i++)
{
a -= i;
b -= i;
if (a < 0)
{
return true;
}
if (b < 0)
{
return true;
Expand All @@ -178,8 +186,12 @@ bool str_lastcmp(std::string_view str1, std::string_view str2, unsigned int leng
}
return true;
}
bool str_caselastcmp(std::string_view str1, std::string_view str2, unsigned int length)
bool str_casecmp_last(std::string_view str1, std::string_view str2, unsigned int length)
{
if(length==0)
{
length = str2.size();
}
int a = (int)str1.size();
int b = (int)str2.size();
if (a == 0)
Expand All @@ -190,17 +202,17 @@ bool str_caselastcmp(std::string_view str1, std::string_view str2, unsigned int
{
return false;
}
if(a < b)
{
return false;
}
a = a - 1;
b = b - 1;

for (unsigned int i = 0; i < length; i++)
{
a -= i;
b -= i;
if (a < 0)
{
return true;
}
if (b < 0)
{
return true;
Expand Down
4 changes: 2 additions & 2 deletions vendor/httpserver/src/httppeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ unsigned char httppeer::get_fileinfo()
{
for (unsigned int k = 0; k < sysconfigpath.sitehostinfos[host_index].action_404_lists.size(); k++)
{
if (pathinfos[0].size() <= sysconfigpath.sitehostinfos[host_index].action_404_lists[k].size() && str_compare(pathinfos[0], sysconfigpath.sitehostinfos[host_index].action_404_lists[k], pathinfos[0].size()))
if (pathinfos[0].size() <= sysconfigpath.sitehostinfos[host_index].action_404_lists[k].size() && str_cmp_pre(sysconfigpath.sitehostinfos[host_index].action_404_lists[k], pathinfos[0], pathinfos[0].size()))
{
sendfilename = sitepath;
sendfilename.append(sysconfigpath.sitehostinfos[host_index].action_404_lists[k]);
Expand Down Expand Up @@ -1090,7 +1090,7 @@ unsigned char httppeer::get_fileinfo()
{
for (unsigned int k = 0; k < sysconfigpath.sitehostinfos[host_index].action_404_lists.size(); k++)
{
if (pathinfos[0].size() > 2 && sysconfigpath.sitehostinfos[host_index].action_404_lists[k].size() > 2 && str_compare(pathinfos[0], sysconfigpath.sitehostinfos[host_index].action_404_lists[k], 3))
if (pathinfos[0].size() > 2 && sysconfigpath.sitehostinfos[host_index].action_404_lists[k].size() > 2 && str_cmp_pre(sysconfigpath.sitehostinfos[host_index].action_404_lists[k], pathinfos[0], 3))
{
unsigned int tempsize = pathinfos.size();
if (tempsize < 20)
Expand Down
13 changes: 7 additions & 6 deletions vendor/httpserver/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,7 @@ asio::awaitable<void> httpserver::http1_send_file(unsigned int streamid,
}
co_return;
}
bool httpserver::http1_send_file_range(unsigned int streamid,
asio::awaitable<void> httpserver::http1_send_file_range(unsigned int streamid,
std::shared_ptr<httppeer> peer,
std::shared_ptr<client_session> peer_session,
const std::string &filename)
Expand Down Expand Up @@ -2317,7 +2317,8 @@ bool httpserver::http1_send_file_range(unsigned int streamid,
htmlcontent = peer->make_http1_header();
htmlcontent.append("\r\n");

peer_session->send_data(htmlcontent);
//peer_session->send_data(htmlcontent);
co_await peer_session->co_send_writer(htmlcontent);
fseek(fp.get(), readnum, SEEK_SET);
try
{
Expand All @@ -2327,8 +2328,8 @@ bool httpserver::http1_send_file_range(unsigned int streamid,
htmlcontent.resize(4096);
unsigned int nread = fread(&htmlcontent[0], 1, 4096, fp.get());
htmlcontent.resize(nread);
peer_session->send_data(htmlcontent);

//peer_session->send_data(htmlcontent);
co_await peer_session->co_send_writer(htmlcontent);
readnum += nread;
}
}
Expand All @@ -2340,7 +2341,7 @@ bool httpserver::http1_send_file_range(unsigned int streamid,
{
http1_send_bad_server(peer, peer_session);
}
return true;
co_return;
}
// bool httpserver::http1_send_body(unsigned int streamid,
// std::shared_ptr<httppeer> peer,
Expand Down Expand Up @@ -2566,7 +2567,7 @@ asio::awaitable<void> httpserver::http1loop(unsigned int stream_id,

if (peer->state.rangebytes)
{
http1_send_file_range(stream_id, peer, peer_session, peer->sendfilename);
co_await http1_send_file_range(stream_id, peer, peer_session, peer->sendfilename);
}
else
{
Expand Down
16 changes: 14 additions & 2 deletions vendor/httpserver/src/serverconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,16 +503,25 @@ bool serverconfig::loadserverglobalconfig()
}
if (map_value["default"]["http2_enable"].size() > 0 && map_value["default"]["http2_enable"][0] == '1')
{
isallnothttp2 = false;
isallnothttp2 = true;
}
else
{
isallnothttp2 = true;
isallnothttp2 = false;
}
server_loaclvar &static_server_var = get_server_global_var();
static_server_var.http2_enable = isallnothttp2;
struct site_host_info_t tempinfo_default;

if (map_value["default"]["global_http2_enable"].size() > 0 && map_value["default"]["global_http2_enable"][0] == '1')
{
isallnothttp2 = true;
}
else
{
isallnothttp2 = false;
}

if (map_value["default"]["controlsopath"].size() > 0)
{
static_server_var.control_so_path = map_value["default"]["controlsopath"];
Expand Down Expand Up @@ -857,6 +866,9 @@ bool serverconfig::loadserverglobalconfig()
struct site_host_info_t tempinfo;
tempinfo = tempinfo_default;
tempinfo.mainhost = first;
tempinfo.certificate_file.clear();
tempinfo.privateKey_file.clear();

for (auto [itemname, itemval] : second)
{
if (itemname == "http2_enable")
Expand Down
11 changes: 10 additions & 1 deletion websockets/include/mywebsockets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ class mywebsockets : public websockets_api
if (peer)
{
std::string outhello;
peer->ws->makeWSText(data, outhello);
if(data=="html")
{
std::string html_data="<h3> Websocket test 测试h3 </h3>";
peer->ws->makeWSText(html_data, outhello);
}
else
{
peer->ws->makeWSText(data, outhello);
}

peer->send(outhello);
}
}
Expand Down

0 comments on commit 6746f88

Please sign in to comment.