serwer http

16
Serwer Http PROJEKT - SYSTEMY OPERACYJNE 2012/13 MICHAŁ LISZCZ

Upload: mika

Post on 23-Jan-2016

45 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Serwer Http

Serwer HttpPROJEKT - SYSTEMY OPERACYJNE 2012/13

MICHAŁ LISZCZ

Page 2: Serwer Http

Repozytorium

https://github.com/gitprofit/httpsrvr

Page 3: Serwer Http

Serwer WWW

Response

Page 4: Serwer Http

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

Page 5: Serwer Http

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}

Page 6: Serwer Http

Implementacja

o header-only library

o OOP

o opakowanie funkcji systemowych

o plik konfiguracyjny

o dużo std::shared_ptr

o Eclipse

Page 7: Serwer Http

Architektura

Thread File

Net Util

Komunikacja sieciowa Pomocnicze

Wątki Pliki

Page 8: Serwer Http

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

Page 9: Serwer Http

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

Page 10: Serwer Http

server.config

wwwroot: /home/michal/Documents/HttpServer/wwwroot

Server-Name: Systemy Operacyjne 2013

Listen-Port: 8080

Default-Files: index.html index.htm

Page 11: Serwer Http

ServerSocketclass ServerSocket

{

private:

sockaddr_in sockAddr;int sockFD;

public:

ServerSocket(int port);void close();virtual ~ServerSocket();std::shared_ptr<Socket> accept();

};

Page 12: Serwer Http

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

};

Page 13: Serwer Http

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();

};

Page 14: Serwer Http

SocketIstreamclass SocketIstream: public std::istream

{

private:SocketStreambuf sb;

public:SocketIstream(int unixFileDescriptor) :std::istream(&sb), sb(unixFileDescriptor) { }

};

Page 15: Serwer Http

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();

};

Page 16: Serwer Http

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(...);}

};