diff --git a/.vscode/settings.json b/.vscode/settings.json index fe45693..c79ea77 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } } \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..009c9d5 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ + +# A simple API library for C++ + +API implementation, parsing, validation, definition, etc. + diff --git a/lib/api.hpp b/lib/api.hpp index b118e9e..059d321 100644 --- a/lib/api.hpp +++ b/lib/api.hpp @@ -5,6 +5,7 @@ #include #include #include +#include using namespace std; diff --git a/src/api.cpp b/src/api.cpp index b354080..ed08767 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -48,21 +48,28 @@ bool api::validate() { void api::parse() { - for (uint i=0; ioptions.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; ikeys.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"; -} \ No newline at end of file +} diff --git a/test/test.cpp b/test/test.cpp index a050777..5084d54 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -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"]; -} \ No newline at end of file +} diff --git a/test/test.o b/test/test.o index 2896458..52da175 100755 Binary files a/test/test.o and b/test/test.o differ