Skip to content

Commit

Permalink
Opis funkcji Skunk.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Karolina authored and Karolina committed Jun 2, 2012
1 parent 83afb7f commit c8c6b03
Showing 1 changed file with 90 additions and 12 deletions.
102 changes: 90 additions & 12 deletions Skunk.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>



/**
* @file Skunk.cpp
*
* */


/**
* Przekazuje znak zapisany heksadecymalnie do typu char
* @param a - podawany na wejscie string z heksadecymalnym znakiem
*
* */
char CharFromHex (std::string a) char CharFromHex (std::string a)
{ {
std::istringstream Blat (a); std::istringstream Blat (a);
Expand All @@ -13,6 +23,14 @@ Blat >> std::hex >> Z;
return char(Z); // cast to char and return return char(Z); // cast to char and return
} }



/**
* Odczytuje strumien z pola tekstowego formularza widocznego w przegladarce
* i konwertuje go na typ string
*
* @param coded - parametr typu string odczytywany z pola tekstowego
* @return Text - zwraca przekonwertowany string
* */
std::string urldecoder(std::string coded){ std::string urldecoder(std::string coded){
std::string Text = coded; std::string Text = coded;
std::string::size_type Pos; std::string::size_type Pos;
Expand All @@ -29,8 +47,15 @@ std::string urldecoder(std::string coded){
return Text; return Text;
} }



typedef std::map<std::string, std::string> StringMap; typedef std::map<std::string, std::string> StringMap;



/**
* Dodaje nowy widget do serwera
* @param w - nowy widget
* @return zwraca wskaznik na puste miejsce w liscie widgetow
* */
int Skunk::Server::addWidget(Skunk::Widget *w) { int Skunk::Server::addWidget(Skunk::Widget *w) {
widgets_.push_back(w); widgets_.push_back(w);
w->id_ = nextID_; w->id_ = nextID_;
Expand All @@ -41,14 +66,21 @@ int Skunk::Server::addWidget(Skunk::Widget *w) {
return nextID_++; return nextID_++;
} }



/**
* Realizuje metode HTTP GET przy wiswietlaniu wszystkich dostepnych widgetow
* @param env - parametry zapytania HTTP
* @return zwraca odpowiedz serwera
*
* */
CSGI::Response Skunk::Server::get(CSGI::Env& env) { CSGI::Response Skunk::Server::get(CSGI::Env& env) {
CSGI::Response resp; CSGI::Response resp;


resp.status = 200; resp.status = 200;
std::vector<Skunk::Widget *>::iterator it; std::vector<Skunk::Widget *>::iterator it;


resp.content.append("<!DOCTYPE html>\n<html>\n"); resp.content.append("<!DOCTYPE html>\n<html>\n");
resp.content.append("\t<head><title>2012 TIN MF</title></head>\n"); resp.content.append("\t<head><title>2012 TIN SERVER</title></head>\n");
resp.content.append("\t<body>"); resp.content.append("\t<body>");
resp.content.append("\n\t\t<form method='post' action='/'>\n"); resp.content.append("\n\t\t<form method='post' action='/'>\n");


Expand Down Expand Up @@ -80,6 +112,15 @@ CSGI::Response Skunk::Server::get(CSGI::Env& env) {
(void)env; (void)env;
} }



/**
* Parser wartosci kluczowych,uzywany do izolowania odpowiednich
* wartosci z parametrow zapytania HTTP
* @param src - string do parsowania
* @param separator - zadany do parsowania separator, zwykle &
* @return - zwraca sparsowana wartosc kluczowa
*
* */
StringMap parseKeyVals(std::string& src, std::string separator) { StringMap parseKeyVals(std::string& src, std::string separator) {
StringMap ret; StringMap ret;
std::string part, key, val; std::string part, key, val;
Expand All @@ -98,24 +139,54 @@ StringMap parseKeyVals(std::string& src, std::string separator) {
return ret; return ret;
} }



/**
* Parser danych metody POST
* @param env - parametry zapytania HTTP
* @return sparsowane dane POST
* */
StringMap parsePostData(CSGI::Env& env) { StringMap parsePostData(CSGI::Env& env) {
return parseKeyVals(env["csgi.input"], "&"); return parseKeyVals(env["csgi.input"], "&");
} }



/**
* Parser ciasteczek
* @param src - string zrodlowy
* @return - sparsowane ciasteczko (separator: ;)
* */
StringMap parseCookies(std::string& src) { StringMap parseCookies(std::string& src) {
return parseKeyVals(src, ";"); return parseKeyVals(src, ";");
} }



/**
* Sprawdza autoryzacje uzytkownika poprzez parsowanie parametrów zapytania HTTP
* @param env - parametry zapytania HTTP
* @return identyfikator uzytkownika sesji
* */
std::string Skunk::Server::isAuthed(CSGI::Env& env) { std::string Skunk::Server::isAuthed(CSGI::Env& env) {
StringMap cookies = parseCookies(env["HTTP_COOKIE"]); StringMap cookies = parseCookies(env["HTTP_COOKIE"]);
return sessions_[cookies["sessionid"]]; return sessions_[cookies["sessionid"]];
} }



/**
* Usuwa z ciasteczek biezaca sesje
* @param env - parametry zapytania HTTP
*
* */
void Skunk::Server::removeSession(CSGI::Env& env) { void Skunk::Server::removeSession(CSGI::Env& env) {
StringMap cookies = parseCookies(env["HTTP_COOKIE"]); StringMap cookies = parseCookies(env["HTTP_COOKIE"]);
sessions_[cookies["sessionid"]] = ""; sessions_[cookies["sessionid"]] = "";
} }



/**
* Wyswietla strone logowania
* @return odpowiedz HTTP
*
* */
CSGI::Response showLoginScreen() { CSGI::Response showLoginScreen() {
CSGI::Response resp; CSGI::Response resp;


Expand All @@ -124,10 +195,7 @@ CSGI::Response showLoginScreen() {
resp.content.append("<!DOCTYPE html>\n<html>\n"); resp.content.append("<!DOCTYPE html>\n<html>\n");
resp.content.append("\t<head><title>2012 SKUNKS LOGIN</title></head>\n"); resp.content.append("\t<head><title>2012 SKUNKS LOGIN</title></head>\n");
resp.content.append("\t<body>"); resp.content.append("\t<body>");





resp.content.append("<form method='post' action='/'>"); resp.content.append("<form method='post' action='/'>");


resp.content.append("<p style='position:absolute; left:0px; top:0px;'> Username: <input name='user' type='text'/></p><br />"); resp.content.append("<p style='position:absolute; left:0px; top:0px;'> Username: <input name='user' type='text'/></p><br />");
Expand All @@ -139,21 +207,21 @@ CSGI::Response showLoginScreen() {
resp.content.append("\n\t</body>\n</html>\n"); resp.content.append("\n\t</body>\n</html>\n");
resp.headers["Content-Type"] = "text/html"; resp.headers["Content-Type"] = "text/html";
resp.headers["Content-Length"] = itoa(resp.content.length()); resp.headers["Content-Length"] = itoa(resp.content.length());




return resp; return resp;
} }




/**
* Wyswietla ekran dodawania nowego uzytkownika w panelu admina
* @return odpowiedz HTTP
* */
CSGI::Response addNewUser(){ CSGI::Response addNewUser(){


CSGI::Response resp; CSGI::Response resp;
resp.status = 200; resp.status = 200;


resp.content.append("<!DOCTYPE html>\n<html>\n"); resp.content.append("<!DOCTYPE html>\n<html>\n");
resp.content.append("\t<head><title>2012 SKUNKS LOGIN</title></head>\n"); resp.content.append("\t<head><title>2012 SKUNKS ADD NEW USER</title></head>\n");
resp.content.append("\t<body>"); resp.content.append("\t<body>");


resp.content.append("<form method='post' action='/adduser'>"); resp.content.append("<form method='post' action='/adduser'>");
Expand All @@ -175,7 +243,13 @@ CSGI::Response addNewUser(){
} }





/**
* Sprawdza URI oraz typ metody HTTP i przydziela odpowiednie widoki
* w zaleznosci od potrzeb
* URI - dalsza czesc adresu url
* @param env - parametry zapytania HTTP
*
* */
CSGI::Response Skunk::Server::operator()(CSGI::Env& env) { CSGI::Response Skunk::Server::operator()(CSGI::Env& env) {
std::string session = ""; std::string session = "";
std::string username = isAuthed(env); std::string username = isAuthed(env);
Expand Down Expand Up @@ -233,6 +307,10 @@ CSGI::Response Skunk::Server::operator()(CSGI::Env& env) {
return resp; return resp;
} }



/**
* Uruchamia server CSGI
* */
void Skunk::Server::run() { void Skunk::Server::run() {
CSGI::Server srv(this, 8080); CSGI::Server srv(this, 8080);
srv.run(false); srv.run(false);
Expand Down

0 comments on commit c8c6b03

Please sign in to comment.