add http_header class and bot::http namespace

This commit is contained in:
Syping 2026-03-29 07:32:37 +02:00
parent f12e13ed28
commit e98bd9de6c
14 changed files with 185 additions and 72 deletions

87
src/core/http_headers.cpp Normal file
View file

@ -0,0 +1,87 @@
/*****************************************************************************
* 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_headers.h"
using namespace bot::http;
http_headers::http_headers() {
instance = nullptr;
}
http_headers::http_headers(const std::string &field, const std::string &value) {
instance = nullptr;
add(field, value);
}
http_headers::http_headers(const http_header &header) {
instance = nullptr;
add(header);
}
http_headers::http_headers(const std::initializer_list<http_header> &headers) {
instance = nullptr;
add(headers);
}
http_headers::http_headers(const std::vector<http_header> &headers) {
instance = nullptr;
add(headers);
}
http_headers::~http_headers() {
curl_slist_free_all(instance);
}
void http_headers::add(const std::string &field, const std::string &value) {
const std::string header = field + ": " + value;
curl_slist *headers = curl_slist_append(instance, header.c_str());
if (!headers)
throw std::bad_alloc();
instance = headers;
}
void http_headers::add(const http_header &header) {
add(header.first, header.second);
}
void http_headers::add(const std::initializer_list<http_header> &headers) {
for (const auto &header : headers)
add(header);
}
void http_headers::add(const std::vector<http_header> &headers) {
for (const auto &header : headers)
add(header);
}
void http_headers::remove(const std::string &field) {
const std::string header = field + ":";
curl_slist *headers = curl_slist_append(instance, header.c_str());
if (!headers)
throw std::bad_alloc();
instance = headers;
}
void http_headers::remove(const std::vector<std::string> &fields) {
for (const auto &field : fields)
remove(field);
}
const curl_slist* http_headers::data() const {
return instance;
}

53
src/core/http_headers.h Normal file
View file

@ -0,0 +1,53 @@
/*****************************************************************************
* 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_HEADERS_H
#define HTTP_HEADERS_H
#include <curl/curl.h>
#include <initializer_list>
#include <string>
#include <utility>
#include <vector>
namespace bot {
namespace http {
typedef std::pair<std::string, std::string> http_header;
class http_headers {
public:
http_headers();
http_headers(const std::string &field, const std::string &value);
http_headers(const http_header &header);
http_headers(const std::initializer_list<http_header> &headers);
http_headers(const std::vector<http_header> &headers);
~http_headers();
void add(const std::string &field, const std::string &value);
void add(const http_header &header);
void add(const std::initializer_list<http_header> &headers);
void add(const std::vector<http_header> &headers);
void remove(const std::string &field);
void remove(const std::vector<std::string> &fields);
const curl_slist* data() const;
private:
curl_slist *instance;
};
}
}
#endif // HTTP_HEADERS_H

View file

@ -17,6 +17,7 @@
*****************************************************************************/
#include "http_request.h"
using namespace bot::http;
http_request::http_request() {
instance = curl_easy_init();
@ -24,56 +25,23 @@ http_request::http_request() {
throw std::bad_alloc();
}
const http_response http_request::get(const std::string &url, const dpp::http_headers &headers) {
const http_response http_request::get(const std::string &url, const 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) {
curl_slist *new_header_slist = curl_slist_append(header_slist, std::string(header.first + ": " + header.second).c_str());
if (!new_header_slist) {
curl_slist_free_all(header_slist);
throw std::bad_alloc();
}
header_slist = new_header_slist;
}
}
if (header_slist)
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, header_slist);
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, headers.data());
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;
}
const http_response http_request::post(const std::string &url, const std::string &content, const std::string &content_type, const dpp::http_headers &headers) {
const http_response http_request::post(const std::string &url, const std::string &content, const http_headers &headers) {
http_response response;
curl_easy_setopt(instance, CURLOPT_URL, url.c_str());
curl_slist *header_slist = nullptr;
if (!content_type.empty()) {
curl_slist *new_header_slist = curl_slist_append(header_slist, std::string("Content-Type: " + content_type).c_str());
if (!new_header_slist) {
curl_slist_free_all(header_slist);
throw std::bad_alloc();
}
header_slist = new_header_slist;
}
if (!headers.empty()) {
for (const auto &header : headers) {
curl_slist *new_header_slist = curl_slist_append(header_slist, std::string(header.first + ": " + header.second).c_str());
if (!new_header_slist) {
curl_slist_free_all(header_slist);
throw std::bad_alloc();
}
header_slist = new_header_slist;
}
}
if (header_slist)
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, header_slist);
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, headers.data());
curl_easy_setopt(instance, CURLOPT_POSTFIELDS, content.data());
curl_easy_setopt(instance, CURLOPT_POSTFIELDSIZE, content.size());
curl_easy_setopt(instance, CURLOPT_WRITEDATA, &response.content);
@ -82,7 +50,6 @@ const http_response http_request::post(const std::string &url, const std::string
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;
}

View file

@ -20,21 +20,24 @@
#define HTTP_REQUEST_H
#include <curl/curl.h>
#include <cstdint>
#include <dpp/httpsclient.h>
#include "http_headers.h"
#include "http_response.h"
class http_request
{
public:
http_request();
~http_request();
const http_response get(const std::string &url, const dpp::http_headers &headers = {});
const 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);
namespace bot {
namespace http {
class http_request {
public:
http_request();
~http_request();
const http_response get(const std::string &url, const http_headers &headers = {});
const http_response post(const std::string &url, const std::string &content, const 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;
};
private:
static size_t writer(char *source, size_t size, size_t nmemb, std::string *target);
CURL *instance;
};
}
}
#endif // HTTP_REQUEST_H

View file

@ -20,10 +20,13 @@
#define HTTP_RESPONSE_H
#include <string>
struct http_response
{
std::string content;
long status;
};
namespace bot {
namespace http {
struct http_response {
std::string content;
long status;
};
}
}
#endif // HTTP_RESPONSE_H

View file

@ -17,9 +17,10 @@
*****************************************************************************/
#include <dpp/json.h>
#include <dpp/httpsclient.h>
#include <iostream>
#include "../../core/http_request.h"
#include "deepl.h"
using namespace bot::http;
using namespace bot::translator;
using namespace std::chrono_literals;
@ -43,7 +44,8 @@ const std::vector<language> deepl::get_languages()
try {
http_request request;
http_response response = request.get(http_request::legacy_url(m_hostname, 443, "/v2/languages?type=target", true), { {"Authorization", "DeepL-Auth-Key " + m_apiKey} });
http_response response = request.get(http_request::legacy_url(m_hostname, 443, "/v2/languages?type=target", true),
{{"Authorization", "DeepL-Auth-Key " + m_apiKey}});
if (response.status == 200) {
const dpp::json json_response = dpp::json::parse(response.content);
if (json_response.is_array()) {
@ -91,7 +93,8 @@ const std::string deepl::translate(const std::string &text, const std::string &s
try {
http_request request;
http_response response = request.post(http_request::legacy_url(m_hostname, 443, "/v2/translate", true), json_body.dump(), "application/json", { {"Authorization", "DeepL-Auth-Key " + m_apiKey} });
http_response response = request.post(http_request::legacy_url(m_hostname, 443, "/v2/translate", true), json_body.dump(),
{{"Authorization", "DeepL-Auth-Key " + m_apiKey}, {"Content-Type", "application/json"}});
if (response.status == 200) {
const dpp::json json_response = dpp::json::parse(response.content);
if (json_response.is_object()) {

View file

@ -19,7 +19,6 @@
#ifndef TRANSLATOR_DEEPL_H
#define TRANSLATOR_DEEPL_H
#include <dpp/cluster.h>
#include "../../core/translator.h"
namespace bot {

View file

@ -17,9 +17,10 @@
*****************************************************************************/
#include <dpp/json.h>
#include <dpp/httpsclient.h>
#include <iostream>
#include "../../core/http_request.h"
#include "libretranslate.h"
using namespace bot::http;
using namespace bot::translator;
using namespace std::chrono_literals;
@ -77,10 +78,6 @@ const std::vector<language> libretranslate::get_languages()
const std::string libretranslate::translate(const std::string &text, const std::string &source, const std::string &target)
{
const dpp::http_headers http_headers = {
{"Content-Type", "application/json"}
};
dpp::json json_body = {
{"q", text},
{"source", source.empty() ? "auto" : source},
@ -93,7 +90,8 @@ const std::string libretranslate::translate(const std::string &text, const std::
try {
http_request request;
http_response response = request.post(http_request::legacy_url(m_hostname, m_port, m_url + "translate", m_tls), json_body.dump(), "application/json");
http_response response = request.post(http_request::legacy_url(m_hostname, m_port, m_url + "translate", m_tls), json_body.dump(),
{{"Content-Type", "application/json"}});
if (response.status == 200) {
const dpp::json json_response = dpp::json::parse(response.content);
if (json_response.is_object()) {

View file

@ -20,7 +20,6 @@
#define TRANSLATOR_LIBRETRANSLATE_H
#include <cstdint>
#include <dpp/cluster.h>
#include "../../core/translator.h"
namespace bot {

View file

@ -17,10 +17,11 @@
*****************************************************************************/
#include <dpp/json.h>
#include <dpp/httpsclient.h>
#include <dpp/utility.h>
#include <iostream>
#include "../../core/http_request.h"
#include "lingvatranslate.h"
using namespace bot::http;
using namespace bot::translator;
using namespace std::chrono_literals;

View file

@ -20,7 +20,6 @@
#define TRANSLATOR_LINGVATRANSLATE_H
#include <cstdint>
#include <dpp/cluster.h>
#include "../../core/translator.h"
namespace bot {

View file

@ -17,10 +17,11 @@
*****************************************************************************/
#include <dpp/json.h>
#include <dpp/httpsclient.h>
#include <dpp/utility.h>
#include <iostream>
#include "../../core/http_request.h"
#include "mozhi.h"
using namespace bot::http;
using namespace bot::translator;
using namespace std::chrono_literals;

View file

@ -20,7 +20,6 @@
#define TRANSLATOR_MOZHI_H
#include <cstdint>
#include <dpp/cluster.h>
#include "../../core/translator.h"
namespace bot {