MCPcopy Create free account
hub / github.com/Pagghiu/SaneCppLibraries / onRequest

Method onRequest

Examples/ApiServer/ApiServer.cpp:52–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

50 }
51
52 void onRequest(HttpConnection& connection)
53 {
54 HttpRouteMatch match;
55 SC_ASSERT_RELEASE(
56 router.match(connection.request.getParser().method, connection.request.getRequestTarget(), {}, match));
57 if (match.status == HttpRouteMatchStatus::NotFound)
58 {
59 SC_ASSERT_RELEASE(notFound(connection));
60 return;
61 }
62 if (match.status == HttpRouteMatchStatus::MethodNotAllowed)
63 {
64 char allowStorage[64];
65 StringSpan allow;
66 SC_ASSERT_RELEASE(router.formatAllowHeader(connection.request.getRequestTarget(), allowStorage, allow));
67 SC_ASSERT_RELEASE(connection.response.sendMethodNotAllowed(allow));
68 return;
69 }
70 if (match.status != HttpRouteMatchStatus::Matched)
71 {
72 SC_ASSERT_RELEASE(connection.sendTextCopy(500, "route error\n"));
73 return;
74 }
75
76 if (match.route == &routes[0])
77 {
78 SC_ASSERT_RELEASE(connection.sendJsonCopy(200, "{\"status\":\"ok\"}"));
79 return;
80 }
81 if (match.route == &routes[1])
82 {
83 HttpRequestTargetView target;
84 SC_ASSERT_RELEASE(target.parse(connection.request.getRequestTarget()));
85
86 StringSpan name;
87 if (not target.getQueryValue("name", name) or name.isEmpty())
88 {
89 name = "world";
90 }
91 String response = StringEncoding::Ascii;
92 SC_ASSERT_RELEASE(StringBuilder::format(response, "{{\"hello\":\"{}\"}}", name));
93 SC_ASSERT_RELEASE(connection.sendJsonCopy(200, response.view()));
94 return;
95 }
96 if (match.route == &routes[2])
97 {
98 receiveEchoBody(connection);
99 return;
100 }
101
102 SC_ASSERT_RELEASE(connection.sendTextCopy(500, "route error\n"));
103 }
104
105 void receiveEchoBody(HttpConnection& connection)
106 {

Callers

nothing calls this directly

Calls 10

formatAllowHeaderMethod · 0.80
sendMethodNotAllowedMethod · 0.80
sendTextCopyMethod · 0.80
sendJsonCopyMethod · 0.80
getQueryValueMethod · 0.80
formatFunction · 0.50
matchMethod · 0.45
parseMethod · 0.45
isEmptyMethod · 0.45
viewMethod · 0.45

Tested by

no test coverage detected