Skip to content

Commit cfc1e38

Browse files
committed
Fully wrap placeholder with packed length
1 parent 307861f commit cfc1e38

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

placeholder.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
set -e
3-
LENGTH=$(head -c 4 | od -A d --endian=little -N 4 -i | head -n 1 | awk -F ' ' '{print $2}')
3+
LENGTH=$(head -c 4 | perl -ne 'print unpack("L", $_)')
44
declare -gr REQUEST=$(head -c $LENGTH)
55
VERSION=3000000
66

@@ -43,7 +43,9 @@ function error()
4343
ERROR=$(cat)
4444
fi
4545
[ -n "$2" ] && CODE="$2" || CODE=1
46-
jq -n '.status = "error"' | jq --arg message "$ERROR" --arg code "$CODE" '.message = $message | .code = ($code|tonumber)'
46+
OUTPUT="$(jq -n '.status = "error"' | jq --arg message "$ERROR" --arg code "$CODE" '.message = $message | .code = ($code|tonumber)')"
47+
perl -e "print pack('L', ${#OUTPUT})"
48+
echo -n "$OUTPUT"
4749
}
4850

4951
# Wrap a shell command and bail with a valid response message on error
@@ -59,7 +61,9 @@ function wrap()
5961
error "$@" $EXIT
6062
fi
6163
else
62-
jq -n --arg version $VERSION --arg response "$STDOUT" '.status = "ok" | .version = $version | .response = ($response | if .[:1] == "{" then ( . | fromjson) else . end)'
64+
OUTPUT="$(jq -n --arg version $VERSION --arg response "$STDOUT" '.status = "ok" | .version = $version | .response = ($response | if .[:1] == "{" then ( . | fromjson) else . end)')"
65+
perl -e "print pack('L', ${#OUTPUT})"
66+
echo -n "$OUTPUT"
6367
fi
6468
return $EXIT
6569
}
@@ -156,15 +160,14 @@ function run()
156160
}
157161

158162
# Ensure dependencies are present
159-
require awk
160163
require cat
161164
require envsubst
162165
require find
163166
require gpg
164167
require grep
165168
require head
166169
require jq
167-
require od
170+
require perl
168171
require sed
169172
require tac
170173

0 commit comments

Comments
 (0)