Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HTTP connections more robust upon termination by handling Content-Length and Chunks #522

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
44 changes: 42 additions & 2 deletions include/Translate.awk
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,39 @@ function getResponse(text, sl, tl, hl,
l(header) # log request header

content = NULLSTR; isBody = 0
hasChunk = 0; chunkWillEnd = 0; hasContentLength = 0; contentLength = 0
while (1) {
# separate header and body correctly with CRLF, per RFC 2616
print (header "\r\n") |& HttpService
while ((HttpService |& getline) > 0) {
if (isBody)
if (isBody){
content = content ? content "\n" $0 : $0
if (hasContentLength && length(content) >= contentLength){
break
}
if (hasChunk){
if (chunkWillEnd && $0 == "\r") {
break
}
if ($0 == "0\r") {
chunkWillEnd = 1
}
}
}
else if (length($0) <= 1)
isBody = 1
else { # interesting fields in header
match($0, /^HTTP[^ ]* ([^ ]*)/, group)
if (RSTART) status = group[1]
match($0, /^Location: (.*)/, group)
if (RSTART) location = squeeze(group[1]) # squeeze the URL!
if (match(tolower($0), /content-length: ([0-9]+)/, arr)) {
hasContentLength=1
contentLength = arr[1]
}
if (match(tolower($0), /transfer-encoding: chunked/, arr)) {
hasChunk=1
}
}
l(sprintf("%4s bytes > %s", length($0), $0))
}
Expand Down Expand Up @@ -194,18 +214,38 @@ function postResponse(text, sl, tl, hl, type,
l(header) # log request header

content = NULLSTR; isBody = 0
hasChunk = 0; chunkWillEnd = 0; hasContentLength = 0; contentLength = 0
while (1) {
print (header "\r\n" reqBody) |& HttpService
while ((HttpService |& getline) > 0) {
if (isBody)
if (isBody){
content = content ? content "\n" $0 : $0
if (hasContentLength && length(content) >= contentLength){
break
}
if (hasChunk){
if (chunkWillEnd && $0 == "\r") {
break
}
if ($0 == "0\r") {
chunkWillEnd = 1
}
}
}
else if (length($0) <= 1)
isBody = 1
else { # interesting fields in header
match($0, /^HTTP[^ ]* ([^ ]*)/, group)
if (RSTART) status = group[1]
match($0, /^Location: (.*)/, group)
if (RSTART) location = squeeze(group[1]) # squeeze the URL!
if (match(tolower($0), /content-length: ([0-9]+)/, arr)) {
hasContentLength=1
contentLength = arr[1]
}
if (match(tolower($0), /transfer-encoding: chunked/, arr)) {
hasChunk=1
}
}
l(sprintf("%4s bytes > %s", length($0), $0))
}
Expand Down