diff --git a/src/Makefile b/src/Makefile
index 94f0dae..9bf6330 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,6 +1,9 @@
CXX = g++5
CXXFLAGS = -g3 -O0 -std=c++11 -pedantic -Wall -I/usr/local/include -I../../pikotools
+
+#CXX = clang++
+#CXXFLAGS = -g3 -O0 -fsanitize=address -std=c++11 -pedantic -Wall -I/usr/local/include -I../../pikotools
o=$(patsubst %.cpp,%.o,$(wildcard *.cpp))
name=webserver
@@ -31,6 +34,7 @@ webserver: $(o)
clean:
rm -f $(name)
+ rm -f *.o
cleanall: clean
diff --git a/src/headers_parser.cpp b/src/headers_parser.cpp
index 7a48628..2ee1005 100644
--- a/src/headers_parser.cpp
+++ b/src/headers_parser.cpp
@@ -32,12 +32,18 @@ void HeadersParser::ParseHeaders(Client & client)
ParseHeaderValue(client);
TrimWhiteAtEnd(tmp_value);
- std::wcout << L"wczytalem naglowek" << std::endl;
- std::wcout << tmp_header << L"|" << std::endl;
- std::wcout << tmp_value << L"|" << std::endl;
if( tmp_header.size() > 0 )
+ {
+ std::wcout << L"header parsed: ";
+ std::wcout << L"\"" << tmp_header << L"\"=\"";
+ std::wcout << tmp_value << L"\"" << std::endl;
client.in.Add(tmp_header, tmp_value);
+ }
+ else
+ {
+ std::wcout << "skiping empty name for a header" << std::endl;
+ }
}
else
{
@@ -234,9 +240,9 @@ bool HeadersParser::ParseHeaderKey(Client & client)
if( c >= 32 && c < 127 )
{
- // allow only asci characters
+ // allow only ascii characters
- tmp_header += c;
+ tmp_header += ToLower(c);
}
header_index += 1;
diff --git a/src/server.cpp b/src/server.cpp
index d6dbf00..f313427 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -147,7 +147,7 @@ int Server::AddSocketsToSet()
{
FD_SET(client_tab[i].socket, &read_set);
- if( client_tab[i].answer_generated )
+ if( client_tab[i].answer_generated && !client_tab[i].output_buffer.empty() )
FD_SET(client_tab[i].socket, &write_set);
if( client_tab[i].socket > fd_max )
@@ -169,7 +169,7 @@ void Server::AddNewClient()
struct sockaddr_in client_addr;
socklen_t len;
int client_socket;
-int optval;
+//int optval;
memset(&client_addr, 0, sizeof(client_addr));
len = sizeof(client_addr);
@@ -281,10 +281,8 @@ void Server::ReadInputFromClient(Client & client)
std::cout << client.input_buffer;
std::cout << "----------------------------------------" << std::endl;
- std::cout << "closing connection, is it correct here?" << std::endl;
- client.close_connection = true; // is it correct here? should we send an answer before?
- //RemoveClientSocket(client.socket);
- // do not use client reference anymore
+ // the client has terminated connection
+ client.close_connection = true;
}
else
{
@@ -331,8 +329,7 @@ void Server::WriteOutputToClient(Client & client)
if( client.output_buffer_sent == client.output_buffer.size() )
{
- client.output_buffer.clear();
- client.output_buffer_sent = 0;
+ client.PrepareToNewRequest();
}
}
}
@@ -372,7 +369,16 @@ void Server::CheckHeaders(Client & client, size_t input_buffer_index)
CreateAnswer(client);
client.answer_generated = true;
- std::cout << "answer generated, closing connection" << std::endl;
+ if( client.http_version == http_version_1_0 )
+ {
+ std::cout << "answer generated, closing connection for http 1.0" << std::endl;
+ client.close_connection = true;
+ }
+ }
+
+ if( client.http_method != http_method_get )
+ {
+ // at the moment only get method
client.close_connection = true;
}
@@ -444,21 +450,44 @@ bool Server::SelectMethodName(Client & client, const std::wstring & method_name)
void Server::CreateAnswer(Client & client)
{
- std::wstring a;
+ std::wstring a, c;
- a = L"HTTP/1.0 200 OK\r\n";
- a += L"Content-Type: text/html; charset=UTF-8\r\n";
- a += L"\r\n";
- a += L"hello world from my webserwer, your requested: " + client.url;
- a += L"\r\n";
+ c = L"hello world from my webserver, your requested: " + client.url;
+ c += L"\r\n";
if( client.url == L"/quit" )
{
- a += L"
bye bye";
+ c += L"
bye bye";
close_server = true;
}
+ std::string c_ascii;
+ PT::WideToUTF8(c,c_ascii);
+
+
+ a = L"HTTP/";
+
+ if( client.http_version == http_version_1_0 )
+ a += L"1.0";
+ else
+ a += L"1.1";
+
+ a += L" 200 OK\r\n";
+ a += L"Content-Type: text/html; charset=UTF-8\r\n";
+
+
+
+ wchar_t buf[32];
+ swprintf(buf, sizeof(buf)/sizeof(wchar_t), L"%d", c_ascii.size());
+
+ a += L"Content-Length: ";
+ a += buf;
+ a += L"\r\n";
+ a += L"\r\n";
+
+
PT::WideToUTF8(a, client.output_buffer);
+ client.output_buffer += c_ascii;
}