-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest-part
More file actions
59 lines (52 loc) · 1.71 KB
/
test-part
File metadata and controls
59 lines (52 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/ksh
. ${0%/*}/common
check_portion() {
local first_byte="${2%%-*}"
local last_byte="${2##*-}"
local sz=$(ls -l "$1" | awk '{print $5}')
local length
if [ -n "$first_byte" ]; then
if [ -z "$last_byte" ]; then
last_byte=$(($sz - 1))
elif [ "$last_byte" -ge "$sz" ]; then
last_byte=$(($sz - 1))
fi
else
if [ -z "$last_byte" ]; then
echo "Incorrect request"
return 0
else
first_byte=$(($sz - $last_byte))
last_byte=$(($sz - 1))
fi
fi
length=$(($last_byte - $first_byte + 1))
local request_file=$(mktemp -p $www)
cp "$1" "$request_file"
local part_file=$(mktemp -p $tmpdir) || { fail "mktemp fail"; return 0; }
dd skip="$first_byte" count="$length" if="$1" bs=1 status=none > "$part_file" || { fail "incorrect range"; return 0; }
local response=$(mktemp -p $tmpdir)
{ printf "GET ${request_file#$www} HTTP/1.1\r\n"
printf "Range: bytes=$2\r\n\r\n"
} | nc -w 1 127.0.0.1 $port | tr -d '\r' >> "$response"
local first_header=$(head -n 1 $response)
local check="HTTP/1.1 206 Partial Content"
test x"$first_header" = x"$check" || { fail "incorrect response header"; return 0; }
response_range=$(awk '/Content-Range:/{print $NF}' "$response")
test "${response_range%%/*}" = "${first_byte}-${last_byte}" || { fail "incorrect range"; return 0; }
sed -i '1,/^$/ d' "$response"
cmp -s "$part_file" "$response" || { fail "files don't match"; return 0; }
echo "Requested $2 bytes of $file: success"
}
file="./tests/home.html"
byte_request="25-30 26- -35"
while getopts "f:r:" OPT; do case $OPT in
f) file=$OPTARG;;
r) byte_request="$OPTARG";;
esac; done
shift $((OPTIND - 1))
run_shellweb
for t in $byte_request; do
echo "Bytes from $file requested: $t"
check_portion "$file" $t || fail
done