Skip to content

Commit 12b6d8d

Browse files
committed
Sending client status codes
1 parent 239149e commit 12b6d8d

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

httpserver.userprefs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
33
<MonoDevelop.Ide.Workbench ActiveDocument="httpserver/Server.cpp">
44
<Files>
5-
<File FileName="httpserver/Server.cpp" Line="32" Column="12" />
6-
<File FileName="httpserver/Server.h" Line="45" Column="1" />
5+
<File FileName="httpserver/Server.cpp" Line="750" Column="9" />
6+
<File FileName="httpserver/Server.h" Line="44" Column="90" />
77
<File FileName="httpserver/Main.cpp" Line="1" Column="1" />
88
<File FileName="httpserver/Main.h" Line="1" Column="1" />
99
<File FileName="httpserver/DataVariantMultipartFormData.h" Line="1" Column="1" />
10-
<File FileName="httpserver/DataVariantMultipartFormData.cpp" Line="11" Column="45" />
10+
<File FileName="httpserver/DataVariantMultipartFormData.cpp" Line="1" Column="1" />
1111
<File FileName="httpserver/ServerRequest.h" Line="1" Column="1" />
1212
<File FileName="httpserver/ServerResponse.h" Line="1" Column="1" />
1313
<File FileName="httpserver/Event.cpp" Line="1" Column="1" />
@@ -16,7 +16,7 @@
1616
<File FileName="httpserver/DataVariantFormUrlencoded.cpp" Line="1" Column="1" />
1717
<File FileName="httpserver/ServerApplicationSettings.h" Line="1" Column="1" />
1818
<File FileName="httpserver/ServerApplicationsTree.h" Line="1" Column="1" />
19-
<File FileName="httpserver/ServerApplicationsTree.cpp" Line="1" Column="1" />
19+
<File FileName="httpserver/ServerApplicationsTree.cpp" Line="76" Column="4" />
2020
<File FileName="httpserver/Module.h" Line="1" Column="1" />
2121
<File FileName="httpserver/Event.h" Line="1" Column="1" />
2222
<File FileName="httpserver/System.h" Line="1" Column="1" />

httpserver/Server.cpp

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

httpserver/Server.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ namespace HttpServer
4141

4242
protected:
4343
int cycleQueue(std::queue<Socket> &);
44-
int threadRequestProc(Socket);
44+
void sendStatus(const Socket &, const std::chrono::milliseconds &, const size_t) const;
45+
int threadRequestProc(Socket) const;
4546
int transferFilePart(const Socket &, const std::chrono::milliseconds &, const std::string &, const time_t, const size_t, const std::string &, const std::string &, const std::string &, const bool) const;
4647
int transferFile(const Socket &, const std::chrono::milliseconds &, const std::string &, const std::unordered_map<std::string, std::string> &, const std::map<std::string, std::string> &, const std::string &, const bool) const;
4748
void parseIncomingVars(std::unordered_multimap<std::string, std::string> &, const std::string &, const size_t, const size_t) const;

httpserver/ServerApplicationsTree.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
#include "ServerApplicationsTree.h"
33

4-
#include <iostream>
5-
64
namespace HttpServer
75
{
86
ServerApplicationsTree::ServerApplicationsTree(): app_sets(nullptr)
@@ -77,15 +75,6 @@ namespace HttpServer
7775
name_parts.push_back(name);
7876
}
7977

80-
// TODO: remove #include<iostream>
81-
82-
for (auto part : name_parts)
83-
{
84-
std::cout << " " << part;
85-
}
86-
87-
std::cout << ";" << std::endl;
88-
8978
addApplication(name_parts, sets);
9079
}
9180

0 commit comments

Comments
 (0)