diff options
| author | Glenn Smith <glenn@vector35.com> | 2020-08-21 14:24:01 -0400 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2020-09-14 17:24:08 -0400 |
| commit | 33e31719d37deed58cfe709bf475b80fece8c34a (patch) | |
| tree | 77850624b400089f164ed48c879240f72b9d16f3 /downloadprovider.cpp | |
| parent | 1acc58d472470f96f12ac4dfc78dc221775d18d3 (diff) | |
Custom request types and data transfer for DownloadProvider
Diffstat (limited to 'downloadprovider.cpp')
| -rw-r--r-- | downloadprovider.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/downloadprovider.cpp b/downloadprovider.cpp index 523397df..ce703b81 100644 --- a/downloadprovider.cpp +++ b/downloadprovider.cpp @@ -10,6 +10,8 @@ DownloadInstance::DownloadInstance(DownloadProvider* provider) cb.context = this; cb.destroyInstance = DestroyInstanceCallback; cb.performRequest = PerformRequestCallback; + cb.performCustomRequest = PerformCustomRequestCallback; + cb.freeResponse = PerformFreeResponse; AddRefForRegistration(); m_object = BNInitDownloadInstance(provider->GetObject(), &cb); } @@ -35,6 +37,60 @@ int DownloadInstance::PerformRequestCallback(void* ctxt, const char* url) } +int DownloadInstance::PerformCustomRequestCallback(void* ctxt, const char* method, const char* url, uint64_t headerCount, const char* const* headerKeys, const char* const* headerValues, BNDownloadInstanceResponse** response) +{ + DownloadInstance* instance = (DownloadInstance*)ctxt; + unordered_map<string, string> headers; + for (uint64_t i = 0; i < headerCount; i ++) + { + headers[headerKeys[i]] = headerValues[i]; + } + + Response apiResponse; + int status = instance->PerformCustomRequest(method, url, headers, apiResponse); + + char** keys = new char*[apiResponse.headers.size()]; + char** values = new char*[apiResponse.headers.size()]; + + uint64_t i = 0; + for (const auto& pair : apiResponse.headers) + { + keys[i] = BNAllocString(pair.first.c_str()); + values[i] = BNAllocString(pair.second.c_str()); + i ++; + } + + *response = new BNDownloadInstanceResponse; + (*response)->statusCode = apiResponse.statusCode; + (*response)->headerCount = apiResponse.headers.size(); + (*response)->headerKeys = keys; + (*response)->headerValues = values; + + return status; +} + + +void DownloadInstance::PerformFreeResponse(void* ctxt, BNDownloadInstanceResponse* response) +{ + for (uint64_t i = 0; i < response->headerCount; i ++) + { + BNFreeString(response->headerKeys[i]); + BNFreeString(response->headerValues[i]); + } + + delete [] response->headerKeys; + delete [] response->headerValues; + + delete response; +} + + +uint64_t DownloadInstance::ReadDataCallback(uint8_t* data, uint64_t len) +{ + return BNReadDataForDownloadInstance(m_object, data, len); +} + + uint64_t DownloadInstance::WriteDataCallback(uint8_t* data, uint64_t len) { return BNWriteDataForDownloadInstance(m_object, data, len); @@ -68,6 +124,38 @@ int DownloadInstance::PerformRequest(const string& url, BNDownloadInstanceOutput } +int DownloadInstance::PerformCustomRequest(const string& method, const string& url, const std::unordered_map<std::string, std::string>& headers, Response& response, BNDownloadInstanceInputOutputCallbacks* callbacks) +{ + const char** headerKeys = new const char*[headers.size()]; + const char** headerValues = new const char*[headers.size()]; + + uint64_t i = 0; + for (auto it = headers.begin(); it != headers.end(); ++it) + { + headerKeys[i] = it->first.c_str(); + headerValues[i] = it->second.c_str(); + i ++; + } + + BNDownloadInstanceResponse* bnResponse; + + int result = BNPerformCustomRequest(m_object, method.c_str(), url.c_str(), headers.size(), headerKeys, headerValues, &bnResponse, callbacks); + + response.statusCode = bnResponse->statusCode; + for (uint64_t i = 0; i < bnResponse->headerCount; i ++) + { + response.headers[bnResponse->headerKeys[i]] = bnResponse->headerValues[i]; + } + + BNFreeDownloadInstanceResponse(bnResponse); + + delete [] headerKeys; + delete [] headerValues; + + return result; +} + + void DownloadInstance::DestroyInstance() { ReleaseForRegistration(); @@ -86,6 +174,16 @@ int CoreDownloadInstance::PerformRequest(const std::string& url) } +int CoreDownloadInstance::PerformCustomRequest(const std::string& method, const std::string& url, const std::unordered_map<std::string, std::string>& headers, Response& response) +{ + (void)method; + (void)url; + (void)headers; + response.statusCode = -1; + return -1; +} + + DownloadProvider::DownloadProvider(const string& name): m_nameForRegister(name) { } |
