diff --git a/include/Translate.awk b/include/Translate.awk index e5f171f..53ed39b 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 802eefc..d9044f0 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 7d0c3f3..7f8a28e 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()