Return if raw is empty, fix out of range in http parsers and disable API validate

dev
marcelb 1 year ago
parent f6bb632af2
commit 3a7b5bd688
  1. 4
      lib/api.hpp
  2. 6
      lib/http.hpp
  3. 32
      src/api.cpp
  4. 23
      src/http.cpp
  5. 16
      test/test.cpp
  6. BIN
      test/test.o

@ -35,9 +35,11 @@ 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 putheaders(const map<string, string> _headers); void setheaders(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

@ -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 string("[ERROR] The API is not correct ");
} // }
} }
api::api(defapi* _def, const http_request _req) { api::api(defapi* _def, const http_request _req) {
@ -63,18 +63,20 @@ 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;
// api validacija ključeva // isValidate = false;
for (uint i=0; i<def->val_matrix[path].size(); i++) { // break;
def->val_matrix[path][i]; // }
if (params[def->val_matrix[path][i]].empty()) { // }
isValidate = false;
break;
}
}
return isValidate; // return isValidate;
} // }

@ -20,10 +20,15 @@ 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("/")-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()) { while (!_headers.empty()) {
string key, value; string key, value;
key = _headers.substr(0, _headers.find(": ")); 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")) 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()) 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(); mold();
} }
void http_request::putheaders(const map<string, string> _headers) { void http_request::setheaders(const map<string, string> _headers) {
headers = _headers; headers = _headers;
mold(); mold();
} }
@ -100,11 +105,15 @@ 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")); string _headers = raw.substr(raw.find("\r\n")+2, raw.find("\r\n\r\n")-raw.find("\r\n")-2);
while (!_headers.empty()) { while (!_headers.empty()) {
string key, value; string key, value;
key = _headers.substr(0, _headers.find(": ")); key = _headers.substr(0, _headers.find(": "));
@ -116,9 +125,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")); 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()) 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);
} }

@ -12,7 +12,13 @@ int main() {
myApi.necessary("update", {"id"}); myApi.necessary("update", {"id"});
myApi.necessary("update", {"value"}); 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 /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");
@ -35,13 +41,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;
} }

Binary file not shown.
Loading…
Cancel
Save