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

Method handleRequest

Libraries/Http/HttpAsyncFileServer.cpp:80–138  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78 return Result(true);
79}
80
81Result HttpAsyncFileServer::handleRequest(HttpAsyncFileServer::Stream& stream, HttpConnection& connection)
82{
83 StringSpan filePath;
84 const Result safePath = Internal::extractSafeFilePath(connection.request.getRequestTarget(), filePath);
85 if (not safePath)
86 {
87 return Internal::sendEmptyResponse(connection.response, 400);
88 }
89
90 const bool uploadRequest = connection.request.isMultipart() or
91 connection.request.getParser().method == HttpParser::Method::HttpPOST or
92 connection.request.getParser().method == HttpParser::Method::HttpPUT;
93 if (uploadRequest)
94 {
95 if (not options.enableUploads)
96 {
97 return Internal::sendEmptyResponse(connection.response, 403);
98 }
99 if (options.maxUploadBytes > 0 and
100 connection.request.getBodyFramingKind() == HttpBodyFramingKind::ContentLength and
101 connection.request.getBodyBytesRemaining() > options.maxUploadBytes)
102 {
103 return Internal::sendEmptyResponse(connection.response, 413);
104 }
105 }
106
107 if (connection.request.isMultipart())
108 {
109 return postMultipart(stream, connection);
110 }
111
112 FileSystem fileSystem;
113 SC_TRY(fileSystem.init(directory.view()));
114 switch (connection.request.getParser().method)
115 {
116 case HttpParser::Method::HttpPOST:
117 case HttpParser::Method::HttpPUT: return putFile(stream, connection, filePath);
118 case HttpParser::Method::HttpGET: return getFile(stream, connection, filePath, true, true);
119 case HttpParser::Method::HttpHEAD: return getFile(stream, connection, filePath, false, true);
120 case HttpParser::Method::HttpOPTIONS:
121 SC_TRY(connection.response.startResponse(204));
122 SC_TRY(connection.response.addHeader("Allow", "GET, HEAD, PUT, POST, OPTIONS"));
123 SC_TRY(connection.response.addHeader("Server", "SC"));
124 SC_TRY(connection.response.addContentLength(0));
125 SC_TRY(connection.response.sendHeaders());
126 SC_TRY(connection.response.end());
127 break;
128 default: {
129 SC_TRY(connection.response.startResponse(405));
130 SC_TRY(connection.response.addHeader("Allow", "GET, HEAD, PUT, POST, OPTIONS"));
131 SC_TRY(connection.response.addHeader("Server", "SC"));
132 SC_TRY(connection.response.addContentLength(0));
133 SC_TRY(connection.response.sendHeaders());
134 SC_TRY(connection.response.end());
135 }
136 break;
137 }

Callers 8

customMimeLookupMethod · 0.45
uploadPolicyMethod · 0.45
uploadsDisabledMethod · 0.45
httpFileServerTestMethod · 0.45
putSpanBodyMethod · 0.45
requestOptionsMethod · 0.45
putStreamBodyMethod · 0.45
multipartUploadMethod · 0.45

Calls 9

isMultipartMethod · 0.80
startResponseMethod · 0.80
addContentLengthMethod · 0.80
sendHeadersMethod · 0.80
ResultClass · 0.50
initMethod · 0.45
viewMethod · 0.45
addHeaderMethod · 0.45
endMethod · 0.45

Tested by 8

customMimeLookupMethod · 0.36
uploadPolicyMethod · 0.36
uploadsDisabledMethod · 0.36
httpFileServerTestMethod · 0.36
putSpanBodyMethod · 0.36
requestOptionsMethod · 0.36
putStreamBodyMethod · 0.36
multipartUploadMethod · 0.36