start adding support for HTTP 1.0 (keep connection)

git-svn-id: svn://ttmath.org/publicrep/libscorpiohttpserver/trunk@1057 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2017-03-13 20:27:45 +00:00
parent 103f4cbc12
commit eafb671c15
3 changed files with 60 additions and 21 deletions

View File

@ -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

View File

@ -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;

View File

@ -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"<br><br>bye bye";
c += L"<br><br>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;
}