Compare commits
No commits in common. "dev" and "v0.1_beta" have entirely different histories.
@ -35,11 +35,9 @@ class api {
|
|||||||
string url;
|
string url;
|
||||||
string body;
|
string body;
|
||||||
|
|
||||||
// odlazni api zahtjev
|
|
||||||
api(defapi* _def, const string _method, const string _path, const map<string, string> _params = {}, const string _body = {});
|
api(defapi* _def, const string _method, const string _path, const map<string, string> _params = {}, const string _body = {});
|
||||||
// dolazni api zahjtev
|
|
||||||
api(defapi* _def, const http_request _req);
|
api(defapi* _def, const http_request _req);
|
||||||
// bool validate();
|
bool validate();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ class http_request {
|
|||||||
|
|
||||||
//bool validate();
|
//bool validate();
|
||||||
void putheader(const string _key, const string _value);
|
void putheader(const string _key, const string _value);
|
||||||
void setheaders(const map<string, string> _headers);
|
void putheaders(const map<string, string> _headers);
|
||||||
void parse();
|
void parse();
|
||||||
void mold();
|
void mold();
|
||||||
|
|
||||||
@ -51,8 +51,8 @@ class http_response {
|
|||||||
void get(const string _raw);
|
void get(const string _raw);
|
||||||
|
|
||||||
//bool validate();
|
//bool validate();
|
||||||
// void putheader(const string _key, const string _value);
|
void putheader(const string _key, const string _value);
|
||||||
// void putheaders(const map<string, string> _headers);
|
void putheaders(const map<string, string> _headers);
|
||||||
|
|
||||||
void mold(); // za slanje
|
void mold(); // za slanje
|
||||||
void parse(); // čitaj http
|
void parse(); // čitaj http
|
||||||
|
32
src/api.cpp
32
src/api.cpp
@ -26,9 +26,9 @@ api::api(defapi* _def, const string _method, const string _path, const map<strin
|
|||||||
|
|
||||||
body = _body;
|
body = _body;
|
||||||
|
|
||||||
// if (!validate()) {
|
if (!validate()) {
|
||||||
// throw string("[ERROR] The API is not correct ");
|
throw "[ERROR] The API is not correct ";
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
api::api(defapi* _def, const http_request _req) {
|
api::api(defapi* _def, const http_request _req) {
|
||||||
@ -63,20 +63,18 @@ api::api(defapi* _def, const http_request _req) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// bool api::validate() {
|
bool api::validate() {
|
||||||
// bool isValidate = true;
|
bool isValidate = true;
|
||||||
// cout << "Validiramo " << endl;
|
|
||||||
// // api validacija ključeva
|
|
||||||
// for (uint i=0; i<def->val_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;
|
|
||||||
|
|
||||||
// isValidate = false;
|
// api validacija ključeva
|
||||||
// break;
|
for (uint i=0; i<def->val_matrix[path].size(); i++) {
|
||||||
// }
|
def->val_matrix[path][i];
|
||||||
// }
|
if (params[def->val_matrix[path][i]].empty()) {
|
||||||
|
isValidate = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// return isValidate;
|
return isValidate;
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
25
src/http.cpp
25
src/http.cpp
@ -20,15 +20,10 @@ http_request::http_request(const string _raw) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void http_request::parse() {
|
void http_request::parse() {
|
||||||
|
|
||||||
if (raw.empty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
method = raw.substr(0, raw.find(" "));
|
method = raw.substr(0, raw.find(" "));
|
||||||
url = raw.substr(raw.find("/"), raw.find("HTTP/")-raw.find("/")-1);
|
url = raw.substr(raw.find("/"), raw.find("HTTP/")-raw.find("/"));
|
||||||
|
|
||||||
string _headers = raw.substr(raw.find("\r\n")+2, raw.find("\r\n\r\n")-raw.find("\r\n")-2);
|
string _headers = raw.substr(raw.find("\r\n")+2, raw.find("\r\n\r\n")-raw.find("\r\n"));
|
||||||
while (!_headers.empty()) {
|
while (!_headers.empty()) {
|
||||||
string key, value;
|
string key, value;
|
||||||
key = _headers.substr(0, _headers.find(": "));
|
key = _headers.substr(0, _headers.find(": "));
|
||||||
@ -39,10 +34,10 @@ void http_request::parse() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((size_t)raw.find("\r\n\r\n") == (size_t)raw.find("\r\n"))
|
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")-4);
|
body = raw.substr(raw.find("\r\n\r\n")+4, raw.length()-raw.find("\r\n\r\n"));
|
||||||
|
|
||||||
else if ((size_t)raw.find("\r\n\r\n") < raw.length())
|
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")-4);
|
body = raw.substr(raw.find("\r\n\r\n")+4, raw.find("\r\n")-raw.find("\r\n\r\n"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +46,7 @@ void http_request::putheader(const string _key, const string _value) {
|
|||||||
mold();
|
mold();
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_request::setheaders(const map<string, string> _headers) {
|
void http_request::putheaders(const map<string, string> _headers) {
|
||||||
headers = _headers;
|
headers = _headers;
|
||||||
mold();
|
mold();
|
||||||
}
|
}
|
||||||
@ -105,15 +100,11 @@ void http_response::mold() {
|
|||||||
|
|
||||||
void http_response::parse() {
|
void http_response::parse() {
|
||||||
|
|
||||||
if (raw.empty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
string protocol = raw.substr(0, raw.find(" "));
|
string protocol = raw.substr(0, raw.find(" "));
|
||||||
status = raw.substr(raw.find(" ")+1, raw.find(" ",raw.find(" ")+1)-raw.find(" ")-1);
|
status = raw.substr(raw.find(" ")+1, raw.find(" ",raw.find(" ")+1)-raw.find(" ")-1);
|
||||||
|
|
||||||
if ((size_t)raw.find("\r\n") < raw.length()) {
|
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")-2);
|
string _headers = raw.substr(raw.find("\r\n")+2, raw.find("\r\n\r\n")-raw.find("\r\n"));
|
||||||
while (!_headers.empty()) {
|
while (!_headers.empty()) {
|
||||||
string key, value;
|
string key, value;
|
||||||
key = _headers.substr(0, _headers.find(": "));
|
key = _headers.substr(0, _headers.find(": "));
|
||||||
@ -125,9 +116,9 @@ void http_response::parse() {
|
|||||||
}
|
}
|
||||||
// ne radi za specijalan slučaj kada nema zaglavlja
|
// ne radi za specijalan slučaj kada nema zaglavlja
|
||||||
if ((size_t)raw.find("\r\n\r\n") == (size_t)raw.find("\r\n"))
|
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")-4);
|
body = raw.substr(raw.find("\r\n\r\n")+4, raw.length()-raw.find("\r\n\r\n"));
|
||||||
|
|
||||||
else if ((size_t)raw.find("\r\n\r\n") < raw.length())
|
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")-4);
|
body = raw.substr(raw.find("\r\n\r\n")+4, raw.find("\r\n")-raw.find("\r\n\r\n"));
|
||||||
|
|
||||||
}
|
}
|
@ -12,13 +12,7 @@ int main() {
|
|||||||
myApi.necessary("update", {"id"});
|
myApi.necessary("update", {"id"});
|
||||||
myApi.necessary("update", {"value"});
|
myApi.necessary("update", {"value"});
|
||||||
|
|
||||||
try {
|
// api uf(&myApi, "GET", "/delete", {{"id", "4"}}, "bay");
|
||||||
|
|
||||||
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 /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");
|
// //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");
|
||||||
@ -41,13 +35,13 @@ int main() {
|
|||||||
// myres.get("HTTP/1.1 200 OK");
|
// 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;
|
// cout << myres.method << " " << myres.url << " " << myres.body << endl;// << myres.raw;
|
||||||
|
|
||||||
|
|
||||||
// for(auto i : uf.params)
|
for(auto i : uf.params)
|
||||||
// cout << i.first << " " << i.second << endl;
|
cout << i.first << " " << i.second << endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
BIN
test/test.o
BIN
test/test.o
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user