diff --git a/lib/api.hpp b/lib/api.hpp index 2f19b25..cefcf00 100644 --- a/lib/api.hpp +++ b/lib/api.hpp @@ -35,9 +35,11 @@ class api { string url; string body; + // odlazni api zahtjev api(defapi* _def, const string _method, const string _path, const map _params = {}, const string _body = {}); + // dolazni api zahjtev api(defapi* _def, const http_request _req); - bool validate(); + // bool validate(); }; diff --git a/lib/http.hpp b/lib/http.hpp index 6c65132..c051f80 100644 --- a/lib/http.hpp +++ b/lib/http.hpp @@ -30,7 +30,7 @@ class http_request { //bool validate(); void putheader(const string _key, const string _value); - void putheaders(const map _headers); + void setheaders(const map _headers); void parse(); void mold(); @@ -51,8 +51,8 @@ class http_response { void get(const string _raw); //bool validate(); - void putheader(const string _key, const string _value); - void putheaders(const map _headers); + // void putheader(const string _key, const string _value); + // void putheaders(const map _headers); void mold(); // za slanje void parse(); // čitaj http diff --git a/src/api.cpp b/src/api.cpp index f38a1ef..c6e4c2a 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -26,9 +26,9 @@ api::api(defapi* _def, const string _method, const string _path, const mapval_matrix[path].size(); i++) { +// cout << def->val_matrix[path][i] << " " << params[def->val_matrix[path][i]] << endl; +// if (params[def->val_matrix[path][i]].empty()) { +// cout << "Ptazan " << endl; - // api validacija ključeva - for (uint i=0; ival_matrix[path].size(); i++) { - def->val_matrix[path][i]; - if (params[def->val_matrix[path][i]].empty()) { - isValidate = false; - break; - } - } +// isValidate = false; +// break; +// } +// } - return isValidate; -} +// return isValidate; +// } diff --git a/src/http.cpp b/src/http.cpp index dc4e9dd..204a47b 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -20,10 +20,15 @@ http_request::http_request(const string _raw) { } void http_request::parse() { + + if (raw.empty()) { + return; + } + method = raw.substr(0, raw.find(" ")); url = raw.substr(raw.find("/"), raw.find("HTTP/")-raw.find("/")-1); - string _headers = raw.substr(raw.find("\r\n")+2, raw.find("\r\n\r\n")-raw.find("\r\n")); + string _headers = raw.substr(raw.find("\r\n")+2, raw.find("\r\n\r\n")-raw.find("\r\n")-2); while (!_headers.empty()) { string key, value; key = _headers.substr(0, _headers.find(": ")); @@ -34,10 +39,10 @@ void http_request::parse() { } if ((size_t)raw.find("\r\n\r\n") == (size_t)raw.find("\r\n")) - body = raw.substr(raw.find("\r\n\r\n")+4, raw.length()-raw.find("\r\n\r\n")); + body = raw.substr(raw.find("\r\n\r\n")+4, raw.length()-raw.find("\r\n\r\n")-4); else if ((size_t)raw.find("\r\n\r\n") < raw.length()) - body = raw.substr(raw.find("\r\n\r\n")+4, raw.find("\r\n")-raw.find("\r\n\r\n")); + body = raw.substr(raw.find("\r\n\r\n")+4, raw.find("\r\n")-raw.find("\r\n\r\n")-4); } @@ -46,7 +51,7 @@ void http_request::putheader(const string _key, const string _value) { mold(); } -void http_request::putheaders(const map _headers) { +void http_request::setheaders(const map _headers) { headers = _headers; mold(); } @@ -100,11 +105,15 @@ void http_response::mold() { void http_response::parse() { + if (raw.empty()) { + return; + } + string protocol = raw.substr(0, raw.find(" ")); status = raw.substr(raw.find(" ")+1, raw.find(" ",raw.find(" ")+1)-raw.find(" ")-1); if ((size_t)raw.find("\r\n") < raw.length()) { - string _headers = raw.substr(raw.find("\r\n")+2, raw.find("\r\n\r\n")-raw.find("\r\n")); + string _headers = raw.substr(raw.find("\r\n")+2, raw.find("\r\n\r\n")-raw.find("\r\n")-2); while (!_headers.empty()) { string key, value; key = _headers.substr(0, _headers.find(": ")); @@ -116,9 +125,9 @@ void http_response::parse() { } // ne radi za specijalan slučaj kada nema zaglavlja if ((size_t)raw.find("\r\n\r\n") == (size_t)raw.find("\r\n")) - body = raw.substr(raw.find("\r\n\r\n")+4, raw.length()-raw.find("\r\n\r\n")); + body = raw.substr(raw.find("\r\n\r\n")+4, raw.length()-raw.find("\r\n\r\n")-4); else if ((size_t)raw.find("\r\n\r\n") < raw.length()) - body = raw.substr(raw.find("\r\n\r\n")+4, raw.find("\r\n")-raw.find("\r\n\r\n")); + body = raw.substr(raw.find("\r\n\r\n")+4, raw.find("\r\n")-raw.find("\r\n\r\n")-4); } \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp index 5b850ae..3933d46 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -12,7 +12,13 @@ int main() { myApi.necessary("update", {"id"}); myApi.necessary("update", {"value"}); - // api uf(&myApi, "GET", "/delete", {{"id", "4"}}, "bay"); + try { + + api uf(&myApi, "GET", "delete", {make_pair("id", "4")}, "bay"); + + } catch (string err) { + cout << err << endl; + } // http myHttp(&myApi, "GET /fghfhf HTTP/1.1\r\nBaba"); // //http myHttp(&myApi, "GET /hello/?id=4&post=99 HTTP/1.1\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\nHost: www.tutorialspoint.com\r\nAccept-Language: en-us\r\nAccept-Encoding: gzip, deflate\r\nConnection: Keep-Alive\r\n\r\nHELLO WORLD\r\n"); @@ -35,13 +41,13 @@ int main() { // myres.get("HTTP/1.1 200 OK"); - api uf(&myApi, myres); + // api uf(&myApi, myres); - cout << uf.method << " " << uf.path << " " << uf.body << endl;// << myres.raw; + // cout << uf.method << " " << uf.path << " " << uf.body << endl;// << myres.raw; // cout << myres.method << " " << myres.url << " " << myres.body << endl;// << myres.raw; - for(auto i : uf.params) - cout << i.first << " " << i.second << endl; + // for(auto i : uf.params) + // cout << i.first << " " << i.second << endl; } diff --git a/test/test.o b/test/test.o index 430a4ce..4e9e762 100755 Binary files a/test/test.o and b/test/test.o differ