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

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