@@ -22,7 +22,7 @@ void ThreadHandler::_addRequests(
2222
2323 for (const auto & RequestProps : Requests) {
2424 if (_RequestsSorted.contains (RequestProps.ClientFD )) {
25- _RequestsSorted. at ( RequestProps.ClientFD ) .push_back (std::move (RequestProps));
25+ _RequestsSorted[ RequestProps.ClientFD ] .push_back (std::move (RequestProps));
2626 }
2727 else {
2828 ClientRequestDataVec_t ReqPropsVec;
@@ -45,11 +45,18 @@ void ThreadHandler::_checkProcessed()
4545 if (it->second ->join ()) {
4646 DBG (120 , " Joined Thread for ClientFD:" << it->first << " , removing from Map" );
4747
48- _ProcessRequests.erase (
49- _ProcessRequests.begin ()+_ProcessRequestsIndex.at (it->first )
50- );
48+ // - safely check if index exists before erasing
49+ auto indexIter = _ProcessRequestsIndex.find (it->first );
50+ if (indexIter != _ProcessRequestsIndex.end ()) {
51+ _ProcessRequests.erase (
52+ _ProcessRequests.begin ()+indexIter->second
53+ );
54+ _ProcessRequestsIndex.erase (indexIter);
55+ }
56+ else {
57+ ERR (" _ProcessRequestsIndex does not contain ClientFD:" << it->first );
58+ }
5159
52- _ProcessRequestsIndex.erase (it->first );
5360 _ClientThreads.erase (it++);
5461 }
5562 else {++it;};
@@ -138,11 +145,23 @@ void ClientThread::processRequests()
138145 RequestHeaderResult_t Headers;
139146 _parseRequestHeaders (_ClientRequests[i].HTTPPayload , Headers);
140147
141- DBG (120 , " RequestType:'" << BaseProps.at (2 ) << " '" );
142- DBG (120 , " RequestPath:'" << BaseProps.at (1 ) << " '" );
143- DBG (120 , " HTTPVersion:'" << BaseProps.at (0 ) << " '" );
148+ // - validate BaseProps has minimum required elements
149+ if (BaseProps.size () < 3 ) {
150+ ERR (" Invalid HTTP request - insufficient base properties" );
151+ continue ;
152+ }
153+
154+ DBG (120 , " RequestType:'" << BaseProps[2 ] << " '" );
155+ DBG (120 , " RequestPath:'" << BaseProps[1 ] << " '" );
156+ DBG (120 , " HTTPVersion:'" << BaseProps[0 ] << " '" );
144157
145- const string NamespaceID = Headers.at (" Host" );
158+ // - check if Host header exists
159+ auto hostIter = Headers.find (" Host" );
160+ if (hostIter == Headers.end ()) {
161+ ERR (" Missing Host header in request" );
162+ continue ;
163+ }
164+ const string NamespaceID = hostIter->second ;
146165 DBG (120 , " NamespaceID:'" << NamespaceID << " '" );
147166
148167 FileProperties_t FileProps;
@@ -158,13 +177,39 @@ void ClientThread::processRequests()
158177 }
159178 */
160179
161- NamespaceProps_t NamespaceProps = _Namespaces.at (NamespaceID);
180+ // - check if namespace exists
181+ auto namespaceIter = _Namespaces.find (NamespaceID);
182+ if (namespaceIter == _Namespaces.end ()) {
183+ ERR (" Namespace not found for NamespaceID:'" << NamespaceID << " '" );
184+
185+ // - send 404 response for non-existent vhost
186+ stringstream current_date_404;
187+ std::time_t tt_404 = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now ());
188+ struct std ::tm * ptm_404 = std::localtime (&tt_404);
189+ current_date_404 << std::put_time (ptm_404, " %a, %d %b %Y %T" ) << ' \n ' ;
190+
191+ string Response404 = " HTTP/1.1 404 Not Found\n " ;
192+ Response404.append (" Date: " );
193+ Response404.append (current_date_404.str ());
194+ Response404.append (" GMT\n " );
195+ Response404.append (" Server: falcon/0.1\n " );
196+ Response404.append (" Content-Length: 26\n " );
197+ Response404.append (" Content-Type: text/html\n\n " );
198+ Response404.append (" <html>404 Not Found</html>" );
199+
200+ const char * send_buf_404 = Response404.c_str ();
201+ const int rc_404 = write (_ClientRequests[i].ClientFDShared , send_buf_404, strlen (send_buf_404));
202+ if (rc_404 < 0 ) { ERR (" write() err:" << strerror (errno)); }
203+ continue ;
204+ }
205+
206+ NamespaceProps_t NamespaceProps = namespaceIter->second ;
162207
163208 DBG (120 , " NamespacePath:'" << NamespaceProps.FilesystemRef ->Path << " '" );
164209 DBG (120 , " NamespaceBasePath:'" << NamespaceProps.FilesystemRef ->BasePath << " '" );
165210
166- if (NamespaceProps.FilesystemRef ->checkFileExists (BaseProps. at ( 1 ) )) {
167- FileProps = NamespaceProps.FilesystemRef ->getFilePropertiesByFile (BaseProps. at ( 1 ) );
211+ if (NamespaceProps.FilesystemRef ->checkFileExists (BaseProps[ 1 ] )) {
212+ FileProps = NamespaceProps.FilesystemRef ->getFilePropertiesByFile (BaseProps[ 1 ] );
168213
169214 DBG (80 , " ClientFileDescriptor:" << _ClientRequests[i].ClientFD );
170215 DBG (80 , " FileDescriptor:" << FileProps.Filedescriptor );
@@ -174,7 +219,8 @@ void ClientThread::processRequests()
174219 DBG (80 , " JSONConfig:" << NamespaceProps.JSONConfig );
175220
176221 // - if etags match, send 304 not-modified
177- if (Headers.find (" If-None-Match" ) != Headers.end () && Headers.at (" If-None-Match" ) == FileProps.ETag ) {
222+ auto etagIter = Headers.find (" If-None-Match" );
223+ if (etagIter != Headers.end () && etagIter->second == FileProps.ETag ) {
178224 string Response304 = " HTTP/1.1 304 Not Modified\n " ;
179225 Response304.append (" Date: " );
180226 Response304.append (current_date.str ());
0 commit comments