160 likes | 301 Views
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<br> Host: atasoyweb.net<br> User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1<br>
E N D
Serwer Http PROJEKT - Systemy OPERACYJNE 2012/13 Michał LISZCZ
Repozytorium • https://github.com/gitprofit/httpsrvr
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 • header-onlylibrary • OOP • opakowanie funkcji systemowych • plik konfiguracyjny • dużo std::shared_ptr • Eclipse
server.config • wwwroot: /home/michal/Documents/HttpServer/wwwroot • Server-Name: Systemy Operacyjne 2013 • Listen-Port: 8080 • Default-Files: index.html index.htm
ServerSocket classServerSocket { private: sockaddr_insockAddr; intsockFD; public: ServerSocket(int port); voidclose(); virtual ~ServerSocket(); std::shared_ptr<Socket> accept(); };
Socket classSocket { friendclassServerSocket; private: intsockFD; Socket(intsockFD); public: voidclose(); virtual ~Socket(); std::shared_ptr<HttpRequest> read (std::shared_ptr<HttpRequestFactory> requestFactory); voidwrite (std::shared_ptr<HttpResponse> response); };
SocketStreambuf classSocketStreambuf: publicstd::streambuf { friendclassSocketIstream; private: intstreamFD;charcurrChar;charnextChar;boolisEOS; SocketStreambuf(intunixFileDescriptor); char peek(); // discard all \r voidnext(); virtualint_typeunderflow(); virtualint_typeuflow(); virtualint_typepbackfail(int_type); virtualstd::streamsizeshowmanyc(); };
SocketIstream classSocketIstream: publicstd::istream { private: SocketStreambufsb; public: SocketIstream(intunixFileDescriptor) : std::istream(&sb), sb(unixFileDescriptor) { } };
Thread template<DetachStatedetachState, CancelTypecancelType> classThread { private: pthread_tthreadID = 0; staticvoid* threadStarter(void* callerThis) { // ... Thread* p = (Thread*) callerThis; p->run(); } staticvoidthreadCleanup(void* callerThis); public: pthread_tgetTID();virtualvoid run() = 0;Thread() = default;virtual~Thread() { } void start() { // ... pthread_create(&threadID, &attr, Thread::threadStarter, this); } voidcancel();voidjoin(); };
Enum template <typenameDerviedEnum>classEnum{protected: conststd::string stringForm;Enum(conststd::string& stringForm) : stringForm(stringForm) { } public: conststd::string& toString() const { returnstringForm; } staticstd::shared_ptr<DerviedEnum> fromString(conststd::string& stringForm){ for(autov : DerviedEnum::values) // CRTPif(v->stringForm == stringForm)return v; throwException(...);} };