Fix API parse method for special cases

dev
marcelb 1 year ago
parent a6d188960f
commit de2ba47180
  1. 30
      .vscode/settings.json
  2. 5
      README.md
  3. 1
      lib/api.hpp
  4. 31
      src/api.cpp
  5. 4
      test/test.cpp
  6. BIN
      test/test.o

@ -16,6 +16,34 @@
"streambuf": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp"
"utility": "cpp",
"*.tcc": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"algorithm": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"system_error": "cpp",
"iostream": "cpp",
"limits": "cpp",
"numbers": "cpp",
"stdexcept": "cpp",
"typeinfo": "cpp"
}
}

@ -0,0 +1,5 @@
# A simple API library for C++
API implementation, parsing, validation, definition, etc.

@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include <map>
#include <sstream>
using namespace std;

@ -48,21 +48,28 @@ bool api::validate() {
void api::parse() {
for (uint i=0; i<def->options.size(); i++) {
if (body.find("/"+def->options[i]+"/") < body.length()) {
option = def->options[i];
}
// Extract the query string from the API request
size_t queryStart = body.find('?');
size_t protocolEnd = body.find("HTTP/");
if (queryStart == string::npos || (protocolEnd != string::npos && queryStart > protocolEnd)) {
// cout << "No object found in the API request." << endl;
return;
}
for (uint i=0; i<def->keys.size(); i++) {
string key = def->keys[i]+"=";
string value;
if (body.find(key) < body.length()) {
value = body.substr(body.find(key)+key.length(), body.find('&',body.find(key)+key.length())-body.find(key)-key.length() < body.find(' ',body.find(key)+key.length())-body.find(key)-key.length() ? body.find('&',body.find(key)+key.length())-body.find(key)-key.length() : body.find(' ',body.find(key)+key.length())-body.find(key)-key.length());
size_t queryStringStart = (queryStart != string::npos) ? queryStart + 1 : 0;
string queryString = body.substr(queryStringStart, protocolEnd - queryStringStart);
// Parse the query string and extract key-value pairs
istringstream iss(queryString);
string parameter;
while (getline(iss, parameter, '&')) {
size_t equalSignPos = parameter.find('=');
if (equalSignPos != string::npos) {
string key = parameter.substr(0, equalSignPos);
string value = parameter.substr(equalSignPos + 1);
object[key] = value;
}
object[def->keys[i]] = value;
}
}
void api::format() {
@ -84,4 +91,4 @@ void api::format() {
body.pop_back();
body += " HTTP/1.1";
}
}

@ -17,9 +17,9 @@ int main() {
// cout << myDef.val_matrix["delete"].empty();
api myApi(&myDef, "GET /update/?id=1&value=true HTTP/1.1");
api myApi(&myDef, "GET /update/?id=1&value=tru eHTTP/1.1\n");
cout << myApi.object["value"];
}
}

Binary file not shown.
Loading…
Cancel
Save