From b8e57d7446356b7c659e335bf1e652f9f09682bf Mon Sep 17 00:00:00 2001 From: tsaitang404 Date: Wed, 3 Jun 2026 14:43:04 +0800 Subject: [PATCH] Translate,Utils,GoogleTranslate: add HTTPS/TLS support via curl gawk's /inet/tcp does not support TLS, causing engines that use HTTPS endpoints (e.g. translate.googleapis.com) to silently return empty responses ("Null response" error). Changes: - GoogleTranslate: switch HttpProtocol to https:// on port 443 - initHttpService: detect https:// and set UseHttps flag, skip gawk TCP - getResponse: when UseHttps, delegate to new curlGetResponse() - postResponse: when UseHttps, delegate to curlPost() - Utils: add curlGetResponse() supporting proxy/cookie/user-agent The fix is generic: any future engine requiring HTTPS only needs to set HttpProtocol = "https://" in its init function. --- include/Translate.awk | 20 ++++++++++++++++++ include/Translators/GoogleTranslate.awk | 4 ++-- include/Utils.awk | 27 +++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/include/Translate.awk b/include/Translate.awk index e5f171f7..53ed39be 100644 --- a/include/Translate.awk +++ b/include/Translate.awk @@ -62,6 +62,14 @@ function initPager() { function initHttpService( inet) { _Init() + if (HttpProtocol == "https://") { + # gawk's /inet/tcp does not support TLS; signal callers to use curl + UseHttps = 1 + HttpPathPrefix = (Option["proxy"] ? HttpProtocol HttpHost : "") + return + } + + UseHttps = 0 inet = "inet" if (Option["ip-version"]) inet = inet Option["ip-version"] @@ -109,6 +117,11 @@ function getResponse(text, sl, tl, hl, content, header, isBody, url, group, status, location) { url = _RequestUrl(text, sl, tl, hl) + # gawk's /inet/tcp has no TLS support; delegate HTTPS requests to curl + if (UseHttps) + return assert(curlGetResponse(HttpProtocol HttpHost url), + "[ERROR] Null response.") + header = "GET " url " HTTP/1.1\r\n" \ "Host: " HttpHost "\r\n" \ "Connection: close\r\n" @@ -169,6 +182,13 @@ function postResponse(text, sl, tl, hl, type, content, contentLength, contentType, group, header, isBody, reqBody, url, status, location, userAgent) { url = _PostRequestUrl(text, sl, tl, hl, type) + + # gawk's /inet/tcp has no TLS support; delegate HTTPS requests to curl + if (UseHttps) { + reqBody = _PostRequestBody(text, sl, tl, hl, type) + return curlPost(HttpProtocol HttpHost url, reqBody) + } + contentType = _PostRequestContentType(text, sl, tl, hl, type) userAgent = _PostRequestUserAgent(text, sl, tl, hl, type) reqBody = _PostRequestBody(text, sl, tl, hl, type) diff --git a/include/Translators/GoogleTranslate.awk b/include/Translators/GoogleTranslate.awk index 802eefc2..d9044f09 100644 --- a/include/Translators/GoogleTranslate.awk +++ b/include/Translators/GoogleTranslate.awk @@ -48,9 +48,9 @@ function genTK(text, } function googleInit() { - HttpProtocol = "http://" + HttpProtocol = "https://" HttpHost = "translate.googleapis.com" - HttpPort = 80 + HttpPort = 443 } function googleRequestUrl(text, sl, tl, hl, qc) { diff --git a/include/Utils.awk b/include/Utils.awk index 7d0c3f3f..7f8a28ec 100644 --- a/include/Utils.awk +++ b/include/Utils.awk @@ -152,6 +152,33 @@ function emacsMe( i, params, el, command) { } } +# Fetch the content of a URL via curl with full header support (proxy, cookie, +# user-agent). Intended for HTTPS requests where gawk /inet/tcp lacks TLS. +# Returns a null string if curl is unavailable or response is empty. +function curlGetResponse(url, command, content, line) { + initCurl() + + if (!Curl) { + l(">> not found: curl") + w("[WARNING] curl is not found.") + return NULLSTR + } + + command = Curl " --location --silent" + if (Option["proxy"]) + command = command " --proxy " parameterize(Option["proxy"]) + if (Option["user-agent"]) + command = command " --user-agent " parameterize(Option["user-agent"]) + if (Cookie) + command = command " --cookie " parameterize(Cookie) + command = command " " parameterize(url) + content = NULLSTR + while ((command |& getline line) > 0) + content = (content ? content "\n" : NULLSTR) line + close(command) + return content +} + # Fetch the content of a URL. Return a null string if failed. function curl(url, output, command, content, line) { initCurl()