summaryrefslogtreecommitdiff
path: root/lingucomponent
diff options
context:
space:
mode:
authorHenry Castro <hcastro@collabora.com>2022-11-21 21:34:24 -0400
committerHenry Castro <hcastro@collabora.com>2023-01-31 13:33:50 +0000
commite295f060f25f3a731a93cea23a1662091f4c9763 (patch)
tree8df31d53b3bed6af5a87ab3e8726b09261b8cbb4 /lingucomponent
parenteb5c14f305f9c6355bd49bd65ab9175831453cb3 (diff)
lingucomponent: implement custom request http rest protocol
Request: POST /api/check HTTP/1.1 Host: localhost:8099 Content-Type: application/json Cache-Control: no-cache { "dictionaries": [ "daily", "jungblutt" ], "text-language": "en-DE", "property-sets": [ "base", "daily", "culture" ], "hyphenation": true, "spellchecking-level": 1, "correction-proposals": true, "single-word-mode": false, "message-language": "fr-LU", "message-level": 1, "text": "This is a final throw for the interface to the Duden server." } Signed-off-by: Henry Castro <hcastro@collabora.com> Change-Id: I2a288a7c573014d42df03f7cc12c57a7f788750e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143107 Reviewed-by: Ashod Nakashian <ash@collabora.com> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145597 Tested-by: Jenkins
Diffstat (limited to 'lingucomponent')
-rw-r--r--lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx79
-rw-r--r--lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx2
2 files changed, 77 insertions, 4 deletions
diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
index b868e4744ac7..ae0b493a32e1 100644
--- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
+++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx
@@ -55,6 +55,8 @@ using namespace linguistic;
#define COL_ORANGE Color(0xD1, 0x68, 0x20)
+constexpr OUStringLiteral sDuden = u"duden";
+
namespace
{
Sequence<PropertyValue> lcl_GetLineColorPropertyFromErrorId(const std::string& rErrorId)
@@ -226,11 +228,28 @@ ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading(
}
tools::Long http_code = 0;
+ std::string response_body;
OUString langTag(aLocale.Language + "-" + aLocale.Country);
- OString postData(OUStringToOString(Concat2View("text=" + aText + "&language=" + langTag),
- RTL_TEXTENCODING_UTF8));
- const std::string response_body
- = makeHttpRequest(checkerURL, HTTP_METHOD::HTTP_POST, postData, http_code);
+
+ if (rLanguageOpts.getRestProtocol() == sDuden)
+ {
+ std::stringstream aStream;
+ boost::property_tree::ptree aTree;
+ aTree.put("text-language", langTag.toUtf8().getStr());
+ aTree.put("text", aText.toUtf8().getStr());
+ aTree.put("hyphenation", false);
+ aTree.put("spellchecking-level", 3);
+ aTree.put("correction-proposals", true);
+ boost::property_tree::write_json(aStream, aTree);
+ response_body = makeDudenHttpRequest(checkerURL, HTTP_METHOD::HTTP_POST,
+ aStream.str().c_str(), http_code);
+ }
+ else
+ {
+ OString postData(OUStringToOString(Concat2View("text=" + aText + "&language=" + langTag),
+ RTL_TEXTENCODING_UTF8));
+ response_body = makeHttpRequest(checkerURL, HTTP_METHOD::HTTP_POST, postData, http_code);
+ }
if (http_code != 200)
{
@@ -317,6 +336,58 @@ void LanguageToolGrammarChecker::parseProofreadingJSONResponse(ProofreadingResul
rResult.aErrors = aErrors;
}
+std::string LanguageToolGrammarChecker::makeDudenHttpRequest(std::string_view aURL,
+ HTTP_METHOD method,
+ const OString& aData,
+ tools::Long& nCode)
+{
+ std::unique_ptr<CURL, std::function<void(CURL*)>> curl(curl_easy_init(),
+ [](CURL* p) { curl_easy_cleanup(p); });
+ if (!curl)
+ return {}; // empty string
+
+ std::string sResponseBody;
+ struct curl_slist* pList = nullptr;
+ SvxLanguageToolOptions& rLanguageOpts = SvxLanguageToolOptions::Get();
+ OString sAccessToken = OString::Concat("access_token: ")
+ + OUStringToOString(rLanguageOpts.getApiKey(), RTL_TEXTENCODING_UTF8);
+
+ pList = curl_slist_append(pList, "Cache-Control: no-cache");
+ pList = curl_slist_append(pList, "Content-Type: application/json");
+ if (!sAccessToken.isEmpty())
+ pList = curl_slist_append(pList, sAccessToken.getStr());
+
+ curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, pList);
+ curl_easy_setopt(curl.get(), CURLOPT_FAILONERROR, 1L);
+ curl_easy_setopt(curl.get(), CURLOPT_URL, aURL.data());
+ curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, CURL_TIMEOUT);
+ curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, WriteCallback);
+ curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, static_cast<void*>(&sResponseBody));
+
+ // allow unknown or self-signed certificates
+ if (rLanguageOpts.getSSLVerification() == false)
+ {
+ curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, false);
+ curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, false);
+ }
+
+ if (method == HTTP_METHOD::HTTP_POST)
+ {
+ curl_easy_setopt(curl.get(), CURLOPT_POST, 1L);
+ curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, aData.getStr());
+ }
+
+ CURLcode cc = curl_easy_perform(curl.get());
+ if (cc != CURLE_OK)
+ {
+ SAL_WARN("languagetool",
+ "CURL request returned with error: " << static_cast<sal_Int32>(cc));
+ }
+
+ curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &nCode);
+ return sResponseBody;
+}
+
std::string LanguageToolGrammarChecker::makeHttpRequest(std::string_view aURL, HTTP_METHOD method,
const OString& aPostData,
tools::Long& nStatusCode)
diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx
index 0262be896cd6..1a5da7995716 100644
--- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx
+++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx
@@ -53,6 +53,8 @@ class LanguageToolGrammarChecker
LanguageToolGrammarChecker& operator=(const LanguageToolGrammarChecker&) = delete;
static void parseProofreadingJSONResponse(css::linguistic2::ProofreadingResult& rResult,
std::string_view aJSONBody);
+ static std::string makeDudenHttpRequest(std::string_view aURL, HTTP_METHOD method,
+ const OString& aPostData, tools::Long& nStatusCode);
static std::string makeHttpRequest(std::string_view aURL, HTTP_METHOD method,
const OString& aPostData, tools::Long& nStatusCode);