Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions include/Translate.awk
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions include/Translators/GoogleTranslate.awk
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions include/Utils.awk
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down