@@ -404,10 +404,30 @@ namespace HttpServer
404404 }
405405 }
406406
407+ void Server::sendStatus (const Socket &clientSocket, const std::chrono::milliseconds &timeout, const size_t statusCode) const
408+ {
409+ const std::unordered_map<size_t , std::string> statuses {
410+ {400 , " Bad Request" },
411+ {404 , " Not Found" },
412+ {413 , " Request Entity Too Large" }
413+ };
414+
415+ auto it = statuses.find (statusCode);
416+
417+ if (statuses.cend () != it)
418+ {
419+ const std::string &status = it->second ;
420+
421+ std::string headers (" HTTP/1.1 " + std::to_string (statusCode) + status + " \r\n\r\n " );
422+
423+ clientSocket.nonblock_send (headers, timeout);
424+ }
425+ }
426+
407427 /* *
408428 * Метод для обработки запроса (запускается в отдельном потоке)
409429 */
410- int Server::threadRequestProc (Socket clientSocket)
430+ int Server::threadRequestProc (Socket clientSocket) const
411431 {
412432 int app_exit_code;
413433
@@ -439,10 +459,12 @@ namespace HttpServer
439459
440460 if (std::numeric_limits<size_t >::max () == recv_len)
441461 {
442- #ifdef WIN32
443- std::cout << " Error: " << WSAGetLastError () << std::endl;
444- #elif POSIX
445- std::cout << " Error: " << errno << std::endl;
462+ #ifdef DEBUG
463+ #ifdef WIN32
464+ std::cout << " Error: " << WSAGetLastError () << std::endl;
465+ #elif POSIX
466+ std::cout << " Error: " << errno << std::endl;
467+ #endif
446468 #endif
447469 break ;
448470 }
@@ -465,6 +487,7 @@ namespace HttpServer
465487 // Если не найден конец заголовка
466488 if (std::string::npos == str_end)
467489 {
490+ sendStatus (clientSocket, timeout, 400 );
468491 break ;
469492 }
470493
@@ -555,14 +578,18 @@ namespace HttpServer
555578 size_t delimiter = it_host->second .find (' :' );
556579
557580 // Получить имя (или адрес)
558- std::string host = it_host->second .substr (0 , delimiter);
581+ const std::string host = it_host->second .substr (0 , delimiter);
582+
583+ /* size_t port = 80;
559584
560- /* TODO: port
585+ // Получить номер порта
561586 if (std::string::npos != delimiter)
562587 {
563- host.erase (delimiter);
588+ port = std::stoull(it_host->second.substr (delimiter + 1) );
564589 }*/
565590
591+ // TODO: application check port
592+
566593 // Поиск настроек приложения по имени
567594 ServerApplicationSettings *app_sets = apps_tree.find (host);
568595
@@ -658,24 +685,27 @@ namespace HttpServer
658685 // Разобрать данные на составляющие
659686 if (false == data_variant->parse (clientSocket, timeout, str_buf.substr (headers_end + 2 ), left_bytes, content_params, incoming_data, incoming_files) )
660687 {
661- // TODO: HTTP 400 Bad Request
662-
663688 for (auto it : incoming_files)
664689 {
665690 remove (it.second .getName ().c_str () );
666691 }
667692
693+ // HTTP 400 Bad Request
694+ sendStatus (clientSocket, timeout, 400 );
695+
668696 break ;
669697 }
670698 }
671699 else
672700 {
673- // TODO: HTTP 413 Request Entity Too Large
701+ // HTTP 413 Request Entity Too Large
702+ sendStatus (clientSocket, timeout, 413 );
674703 }
675704 }
676705 else
677706 {
678- // TODO: HTTP 400 Bad Request
707+ // HTTP 400 Bad Request
708+ sendStatus (clientSocket, timeout, 400 );
679709 }
680710 }
681711
@@ -737,22 +767,26 @@ namespace HttpServer
737767 }
738768 else
739769 {
740- // TODO: HTTP 404 Not Found
770+ // HTTP 404 Not Found
771+ sendStatus (clientSocket, timeout, 404 );
741772 }
742773 }
743774 else
744775 {
745- // TODO: HTTP 400 Bad Request
776+ // HTTP 400 Bad Request
777+ sendStatus (clientSocket, timeout, 400 );
746778 }
747779 }
748780 else
749781 {
750- // TODO: HTTP 400 Bad Request
782+ // HTTP 400 Bad Request
783+ sendStatus (clientSocket, timeout, 400 );
751784 }
752785 }
753786 else // Если запрос пустой
754787 {
755- // TODO: HTTP 400 Bad Request
788+ // HTTP 400 Bad Request
789+ sendStatus (clientSocket, timeout, 400 );
756790 break ;
757791 }
758792
0 commit comments