mirror of
https://github.com/Syping/dtranslatebot.git
synced 2026-04-01 13:20:40 +02:00
ported translator to curl, removed EL7 + Windows support
This commit is contained in:
parent
ecece11eac
commit
63399a010d
18 changed files with 250 additions and 101 deletions
94
src/core/http_request.cpp
Normal file
94
src/core/http_request.cpp
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/*****************************************************************************
|
||||
* dtranslatebot Discord Translate Bot
|
||||
* Copyright (C) 2026 Syping
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* This software is provided as-is, no warranties are given to you, we are not
|
||||
* responsible for anything with use of the software, you are self responsible.
|
||||
*****************************************************************************/
|
||||
|
||||
#include "http_request.h"
|
||||
|
||||
http_request::http_request() {
|
||||
instance = curl_easy_init();
|
||||
if (!instance)
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
|
||||
http_response http_request::get(const std::string &url, const dpp::http_headers &headers) {
|
||||
http_response response;
|
||||
curl_easy_setopt(instance, CURLOPT_URL, url.c_str());
|
||||
curl_slist *header_slist = nullptr;
|
||||
if (!headers.empty()) {
|
||||
for (const auto &header : headers) {
|
||||
header_slist = curl_slist_append(header_slist, std::string(header.first + ": " + header.second).c_str());
|
||||
if (!header_slist)
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
}
|
||||
if (!header_slist)
|
||||
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, header_slist);
|
||||
curl_easy_setopt(instance, CURLOPT_WRITEDATA, &response.content);
|
||||
curl_easy_setopt(instance, CURLOPT_WRITEFUNCTION, &writer);
|
||||
CURLcode result = curl_easy_perform(instance);
|
||||
if (result == CURLE_OK)
|
||||
curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status);
|
||||
curl_easy_reset(instance);
|
||||
curl_slist_free_all(header_slist);
|
||||
return response;
|
||||
}
|
||||
|
||||
http_response http_request::post(const std::string &url, const std::string &content, const std::string &content_type, const dpp::http_headers &headers) {
|
||||
http_response response;
|
||||
curl_easy_setopt(instance, CURLOPT_URL, url.c_str());
|
||||
curl_slist *header_slist = nullptr;
|
||||
if (!content_type.empty()) {
|
||||
header_slist = curl_slist_append(header_slist, std::string("Content-Type: " + content_type).c_str());
|
||||
if (!header_slist)
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
if (!headers.empty()) {
|
||||
for (const auto &header : headers) {
|
||||
header_slist = curl_slist_append(header_slist, std::string(header.first + ": " + header.second).c_str());
|
||||
if (!header_slist)
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
}
|
||||
if (!header_slist)
|
||||
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, header_slist);
|
||||
curl_easy_setopt(instance, CURLOPT_POSTFIELDS, content.data());
|
||||
curl_easy_setopt(instance, CURLOPT_POSTFIELDSIZE, content.size());
|
||||
curl_easy_setopt(instance, CURLOPT_WRITEDATA, &response.content);
|
||||
curl_easy_setopt(instance, CURLOPT_WRITEFUNCTION, &writer);
|
||||
CURLcode result = curl_easy_perform(instance);
|
||||
if (result == CURLE_OK)
|
||||
curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status);
|
||||
curl_easy_reset(instance);
|
||||
curl_slist_free_all(header_slist);
|
||||
return response;
|
||||
}
|
||||
|
||||
std::string http_request::legacy_url(const std::string &hostname, uint16_t port, const std::string &url, bool tls) {
|
||||
return (tls ? "https://" : "http://") + hostname + ":" + std::to_string(port) + (url.empty() ? "/" : (url.front() != '/' ? "/" + url : url));
|
||||
}
|
||||
|
||||
http_request::~http_request() {
|
||||
curl_easy_cleanup(instance);
|
||||
}
|
||||
|
||||
size_t http_request::writer(char *source, size_t size, size_t nmemb, std::string *target) {
|
||||
if (target == nullptr)
|
||||
return 0;
|
||||
size_t write_size = size * nmemb;
|
||||
target->append(source, write_size);
|
||||
return write_size;
|
||||
}
|
||||
40
src/core/http_request.h
Normal file
40
src/core/http_request.h
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/*****************************************************************************
|
||||
* dtranslatebot Discord Translate Bot
|
||||
* Copyright (C) 2026 Syping
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* This software is provided as-is, no warranties are given to you, we are not
|
||||
* responsible for anything with use of the software, you are self responsible.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef HTTP_REQUEST_H
|
||||
#define HTTP_REQUEST_H
|
||||
#include <curl/curl.h>
|
||||
#include <cstdint>
|
||||
#include <dpp/httpsclient.h>
|
||||
#include "http_response.h"
|
||||
|
||||
class http_request
|
||||
{
|
||||
public:
|
||||
http_request();
|
||||
~http_request();
|
||||
http_response get(const std::string &url, const dpp::http_headers &headers = {});
|
||||
http_response post(const std::string &url, const std::string &content, const std::string &content_type, const dpp::http_headers &headers = {});
|
||||
static std::string legacy_url(const std::string &hostname, uint16_t port, const std::string &url, bool tls);
|
||||
|
||||
private:
|
||||
static size_t writer(char *source, size_t size, size_t nmemb, std::string *target);
|
||||
CURL *instance;
|
||||
};
|
||||
|
||||
#endif // HTTP_REQUEST_H
|
||||
29
src/core/http_response.h
Normal file
29
src/core/http_response.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*****************************************************************************
|
||||
* dtranslatebot Discord Translate Bot
|
||||
* Copyright (C) 2026 Syping
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* This software is provided as-is, no warranties are given to you, we are not
|
||||
* responsible for anything with use of the software, you are self responsible.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef HTTP_RESPONSE_H
|
||||
#define HTTP_RESPONSE_H
|
||||
#include <string>
|
||||
|
||||
struct http_response
|
||||
{
|
||||
std::string content;
|
||||
long status;
|
||||
};
|
||||
|
||||
#endif // HTTP_RESPONSE_H
|
||||
|
|
@ -21,6 +21,7 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <curl/curl.h>
|
||||
#include "message_queue.h"
|
||||
#include "settings.h"
|
||||
#include "slashcommands.h"
|
||||
|
|
@ -45,6 +46,12 @@ int main(int argc, char* argv[]) {
|
|||
if (!settings.parse_file(args.at(0)))
|
||||
return 1;
|
||||
|
||||
CURLcode result = curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
if (result != CURLE_OK) {
|
||||
std::cerr << "[Error] Failed to initialise curl" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
std::cout << "[Launch] Requesting supported languages..." << std::endl;
|
||||
if (!settings.get_translator()->get_languages().empty()) {
|
||||
|
|
@ -88,5 +95,7 @@ int main(int argc, char* argv[]) {
|
|||
submit_queue.terminate();
|
||||
submit_queue_loop.join();
|
||||
|
||||
curl_global_cleanup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*****************************************************************************
|
||||
* dtranslatebot Discord Translate Bot
|
||||
* Copyright (C) 2023-2024 Syping
|
||||
* Copyright (C) 2023-2026 Syping
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue