Skip to content

Commit 8d7e53d

Browse files
authored
Merge pull request #476 from CrowCpp/ws-error-reason
Add error message to Websocket `onerror` handler
2 parents d7b3106 + 454dabd commit 8d7e53d

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

docs/guides/websockets.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ A websocket route differs from a normal route quite a bit. It uses A slightly al
77

88
- `#!cpp onaccept([&](const crow::request& req, void** userdata){handler code goes here})`
99
- `#!cpp onopen([&](crow::websocket::connection& conn){handler code goes here})`
10-
- `#!cpp onmessage([&](crow::websocket::connection& conn, const std::string message, bool is_binary){handler code goes here})`
11-
- `#!cpp onerror([&](crow::websocket::connection& conn){handler code goes here})`
12-
- `#!cpp onclose([&](crow::websocket::connection& conn, const std::string reason){handler code goes here})`
10+
- `#!cpp onmessage([&](crow::websocket::connection& conn, const std::string& message, bool is_binary){handler code goes here})`
11+
- `#!cpp onerror([&](crow::websocket::connection& conn, const std::string& error_message){handler code goes here})`
12+
- `#!cpp onclose([&](crow::websocket::connection& conn, const std::string& reason){handler code goes here})`
1313

1414
!!! note
1515

include/crow/json.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,36 +1836,38 @@ namespace crow
18361836
enum
18371837
{
18381838
start,
1839-
decp,
1839+
decp, // Decimal point
18401840
zero
18411841
} f_state;
18421842
char outbuf[128];
18431843
MSC_COMPATIBLE_SPRINTF(outbuf, "%f", v.num.d);
1844-
char *p = &outbuf[0], *o = nullptr;
1844+
char *p = &outbuf[0], *o = nullptr; // o is the position of the first trailing 0
18451845
f_state = start;
18461846
while (*p != '\0')
18471847
{
18481848
//std::cout << *p << std::endl;
18491849
char ch = *p;
18501850
switch (f_state)
18511851
{
1852-
case start:
1852+
case start: // Loop and lookahead until a decimal point is found
18531853
if (ch == '.')
18541854
{
1855-
if (p + 1 && *(p + 1) == '0') p++;
1855+
char fch = *(p + 1);
1856+
// if the first character is 0, leave it be (this is so that "1.00000" becomes "1.0" and not "1.")
1857+
if (fch != '\0' && fch == '0') p++;
18561858
f_state = decp;
18571859
}
18581860
p++;
18591861
break;
1860-
case decp:
1862+
case decp: // Loop until a 0 is found, if found, record its position
18611863
if (ch == '0')
18621864
{
18631865
f_state = zero;
18641866
o = p;
18651867
}
18661868
p++;
18671869
break;
1868-
case zero:
1870+
case zero: // if a non 0 is found (e.g. 1.00004) remove the earlier recorded 0 position and look for more trailing 0s
18691871
if (ch != '0')
18701872
{
18711873
o = nullptr;
@@ -1875,7 +1877,7 @@ namespace crow
18751877
break;
18761878
}
18771879
}
1878-
if (o != nullptr)
1880+
if (o != nullptr) // if any trailing 0s are found, terminate the string where they begin
18791881
*o = '\0';
18801882
out += outbuf;
18811883
#undef MSC_COMPATIBLE_SPRINTF

include/crow/routing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ namespace crow
508508
std::function<void(crow::websocket::connection&)> open_handler_;
509509
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_;
510510
std::function<void(crow::websocket::connection&, const std::string&)> close_handler_;
511-
std::function<void(crow::websocket::connection&)> error_handler_;
511+
std::function<void(crow::websocket::connection&, const std::string&)> error_handler_;
512512
std::function<bool(const crow::request&, void**)> accept_handler_;
513513
uint64_t max_payload_;
514514
bool max_payload_override_ = false;

include/crow/websocket.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace crow
7575
std::function<void(crow::websocket::connection&)> open_handler,
7676
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler,
7777
std::function<void(crow::websocket::connection&, const std::string&)> close_handler,
78-
std::function<void(crow::websocket::connection&)> error_handler,
78+
std::function<void(crow::websocket::connection&, const std::string&)> error_handler,
7979
std::function<bool(const crow::request&, void**)> accept_handler):
8080
adaptor_(std::move(adaptor)),
8181
handler_(handler),
@@ -315,7 +315,7 @@ namespace crow
315315
adaptor_.shutdown_readwrite();
316316
adaptor_.close();
317317
if (error_handler_)
318-
error_handler_(*this);
318+
error_handler_(*this, "Client connection not masked.");
319319
check_destroy();
320320
#endif
321321
}
@@ -341,7 +341,7 @@ namespace crow
341341
adaptor_.shutdown_readwrite();
342342
adaptor_.close();
343343
if (error_handler_)
344-
error_handler_(*this);
344+
error_handler_(*this, ec.message());
345345
check_destroy();
346346
}
347347
});
@@ -379,7 +379,7 @@ namespace crow
379379
adaptor_.shutdown_readwrite();
380380
adaptor_.close();
381381
if (error_handler_)
382-
error_handler_(*this);
382+
error_handler_(*this, ec.message());
383383
check_destroy();
384384
}
385385
});
@@ -414,7 +414,7 @@ namespace crow
414414
adaptor_.shutdown_readwrite();
415415
adaptor_.close();
416416
if (error_handler_)
417-
error_handler_(*this);
417+
error_handler_(*this, ec.message());
418418
check_destroy();
419419
}
420420
});
@@ -426,7 +426,7 @@ namespace crow
426426
close_connection_ = true;
427427
adaptor_.close();
428428
if (error_handler_)
429-
error_handler_(*this);
429+
error_handler_(*this, "Message length exceeds maximum paylaod.");
430430
check_destroy();
431431
}
432432
else if (has_mask_)
@@ -455,7 +455,7 @@ namespace crow
455455
{
456456
close_connection_ = true;
457457
if (error_handler_)
458-
error_handler_(*this);
458+
error_handler_(*this, ec.message());
459459
adaptor_.shutdown_readwrite();
460460
adaptor_.close();
461461
check_destroy();
@@ -497,7 +497,7 @@ namespace crow
497497
{
498498
close_connection_ = true;
499499
if (error_handler_)
500-
error_handler_(*this);
500+
error_handler_(*this, ec.message());
501501
adaptor_.shutdown_readwrite();
502502
adaptor_.close();
503503
check_destroy();
@@ -685,7 +685,7 @@ namespace crow
685685
std::function<void(crow::websocket::connection&)> open_handler_;
686686
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_;
687687
std::function<void(crow::websocket::connection&, const std::string&)> close_handler_;
688-
std::function<void(crow::websocket::connection&)> error_handler_;
688+
std::function<void(crow::websocket::connection&, const std::string&)> error_handler_;
689689
std::function<bool(const crow::request&, void**)> accept_handler_;
690690
};
691691
} // namespace websocket

0 commit comments

Comments
 (0)