Fix API parse method for special cases

dev
marcelb 2 years 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", "streambuf": "cpp",
"tuple": "cpp", "tuple": "cpp",
"type_traits": "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 <string>
#include <vector> #include <vector>
#include <map> #include <map>
#include <sstream>
using namespace std; using namespace std;

@ -48,21 +48,28 @@ bool api::validate() {
void api::parse() { void api::parse() {
for (uint i=0; i<def->options.size(); i++) { // Extract the query string from the API request
if (body.find("/"+def->options[i]+"/") < body.length()) { size_t queryStart = body.find('?');
option = def->options[i]; 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++) { size_t queryStringStart = (queryStart != string::npos) ? queryStart + 1 : 0;
string key = def->keys[i]+"="; string queryString = body.substr(queryStringStart, protocolEnd - queryStringStart);
string value;
if (body.find(key) < body.length()) { // Parse the query string and extract key-value pairs
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()); 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() { void api::format() {
@ -84,4 +91,4 @@ void api::format() {
body.pop_back(); body.pop_back();
body += " HTTP/1.1"; body += " HTTP/1.1";
} }

@ -17,9 +17,9 @@ int main() {
// cout << myDef.val_matrix["delete"].empty(); // 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"]; cout << myApi.object["value"];
} }

Binary file not shown.
Loading…
Cancel
Save