Skip to content

Commit

Permalink
fix duplicate header (#99)
Browse files Browse the repository at this point in the history
* fix duplicate header
* consistency
  • Loading branch information
GameParrot authored Jul 8, 2024
1 parent 92eda37 commit 87b6d3d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/jni/lib_http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

using namespace std::placeholders;

bool slist_contains(struct curl_slist *list, const char *str) {
struct curl_slist *current = list;
while (current != nullptr) {
if (strcasecmp(current->data, str) == 0) {
return true;
}
current = current->next;
}
return false;
}

HttpClientRequest::HttpClientRequest() {
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
Expand Down Expand Up @@ -55,7 +66,7 @@ void HttpClientRequest::setHttpMethodAndBody(std::shared_ptr<FakeJni::JString> m
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, this->body.size());
}
auto conttype = contentType->asStdString();
if(conttype.length()) {
if(conttype.length() && !slist_contains(header, ("Content-Type: " + conttype).c_str())) {
header = curl_slist_append(header, ("Content-Type: " + conttype).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
}
Expand Down Expand Up @@ -113,7 +124,7 @@ void HttpClientRequest::setHttpMethodAndBody2(std::shared_ptr<FakeJni::JString>
header = curl_slist_append(header, ("Content-Length: " + std::to_string(contentLength)).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
auto conttype = contentType->asStdString();
if(conttype.length()) {
if(conttype.length() && !slist_contains(header, ("Content-Type: " + conttype).c_str())) {
header = curl_slist_append(header, ("Content-Type: " + conttype).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
}
Expand Down

0 comments on commit 87b6d3d

Please sign in to comment.