serwer http
DESCRIPTION
Serwer Http. PROJEKT - Systemy OPERACYJNE 2012/13 Michał LISZCZ. Repozytorium. https://github.com/gitprofit/httpsrvr. Serwer WWW. Http Request. POST /index.html HTTP/1.1\r\n Host: atasoyweb.net\r\n User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1\r\n - PowerPoint PPT PresentationTRANSCRIPT
Serwer HttpPROJEKT - SYSTEMY OPERACYJNE 2012/13
MICHAŁ LISZCZ
Repozytorium
https://github.com/gitprofit/httpsrvr
Serwer WWW
Response
Http Request POST /index.html HTTP/1.1\r\n
Host: atasoyweb.net\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n
Accept-Language: tr-tr,tr;q=0.8,en-us;q=0.5,en;q=0.3\r\n
Accept-Encoding: gzip, deflate\r\n
Connection: keep-alive\r\n
Referer: http://atasoyweb.net/\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Content-Length: 35\r\n\r\n
variable1=value1&variable2=value2
Http Response
HTTP/1.1 200 OK\r\n
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n
Content-Length: {content_length}\r\n
Connection: close\r\n
Content-Type: text/html; charset=UTF-8\r\n\r\n
the content of which length is equal to {content_length}
Implementacja
o header-only library
o OOP
o opakowanie funkcji systemowych
o plik konfiguracyjny
o dużo std::shared_ptr
o Eclipse
Architektura
Thread File
Net Util
Komunikacja sieciowa Pomocnicze
Wątki Pliki
Net
<<Enumeration>>
HttpContentType
values : vector<HttpContentType>extensions : vector<string>TextHtmlImagePng...
<<Enumeration>>
HttpMethod
values : vector<HttpMethod>GETPOST
HttpRequest
- method : HttpMethod- headers : map<string, string>operator[](string) : string
HttpRequestFactory
create(socketDesc) : HttpRequest
HttpResponse
- statusCode : HttpStatusCode- contentType : HttpContentType- rawContent : stringtoString() : string
HttpResponseFactory
fromString(...) : HttpResponsefromFile(...) : HttpResponseerror404() : HttpResponseerror501() : HttpResponse
<<Enumeration>>
HttpStatusCode
values : vector<HttpStatusCode>OKNotFoundNotImplemented
NetException
ServerSocket
accept() : Socketclose()
Socket
read(HttpRequestFactory) : HttpRequestwrite(HttpResponse)
SocketIstreamSocketStreambuf
File
ThreadUtil
Net
File
getContent() : stringgetContentType() : HttpContentType
FileManager
getFile(Config) : File
FileException
Thread
start()run()
Mutex
lock()unlock()
Config
- settings : map<string, string>operator[](string) : string
<<Enumeration>>
Enum<DerivedEnum>
- stringForm : stringtoString() : stringfromString(string) : DerivedEnum
Logger
log(string)
Exception
HttpServer
- serverSocket : ServerSocket- requestFactory : RequestFactory- responseFactory : ResponseFactory- fileManager : FileManager- config : Config- logger : Loggerrun()
RequestHandler
- serverSocket : ServerSocket- requestFactory : requestFactory- responseFactory : responseFactory- fileManager : FileManager- config : Config- logger : Logger
ServerSocket
RequestFactory
ResponseFactory
server.config
wwwroot: /home/michal/Documents/HttpServer/wwwroot
Server-Name: Systemy Operacyjne 2013
Listen-Port: 8080
Default-Files: index.html index.htm
ServerSocketclass ServerSocket
{
private:
sockaddr_in sockAddr;int sockFD;
public:
ServerSocket(int port);void close();virtual ~ServerSocket();std::shared_ptr<Socket> accept();
};
Socketclass Socket
{friend class ServerSocket;
private:
int sockFD;Socket(int sockFD);
public:
void close();virtual ~Socket();
std::shared_ptr<HttpRequest> read(std::shared_ptr<HttpRequestFactory> requestFactory);
void write(std::shared_ptr<HttpResponse> response);
};
SocketStreambufclass SocketStreambuf: public std::streambuf
{friend class SocketIstream;
private:int streamFD;char currChar;char nextChar;bool isEOS;
SocketStreambuf(int unixFileDescriptor);
char peek(); // discard all \r
void next();
virtual int_type underflow();
virtual int_type uflow();
virtual int_type pbackfail(int_type);
virtual std::streamsize showmanyc();
};
SocketIstreamclass SocketIstream: public std::istream
{
private:SocketStreambuf sb;
public:SocketIstream(int unixFileDescriptor) :std::istream(&sb), sb(unixFileDescriptor) { }
};
Threadtemplate<DetachState detachState, CancelType cancelType>
class Thread
{
private:pthread_t threadID = 0;
static void* threadStarter(void* callerThis)
{
// ...
Thread* p = (Thread*) callerThis;
p->run();
}
static void threadCleanup(void* callerThis);
public:
pthread_t getTID();virtual void run() = 0;Thread() = default;virtual ~Thread() { }
void start()
{
// ...
pthread_create(&threadID, &attr,
Thread::threadStarter, this);
}
void cancel();void join();
};
Enumtemplate <typename DerviedEnum>class Enum{protected:
const std::string stringForm;Enum(const std::string& stringForm) : stringForm(stringForm) { }
public:const std::string& toString() const { return stringForm; }
static std::shared_ptr<DerviedEnum> fromString(const std::string& stringForm){
for(auto v : DerviedEnum::values) // CRTPif(v->stringForm == stringForm)return v;
throw Exception(...);}
};