我正在尝试改进一个WebDAV bash上传器,以便在文件已经存在于web服务器上时绕过上传文件。
HEAD请求应该如何处理,并在此之后解析它。
如果解决方案还包括大小验证,则会获得额外的奖励。
发布于 2015-11-27 18:02:43
快速和肮脏:只需检查响应中是否有Content-Length头部?
类似于:
curl -I http://google.com | grep Content-Length
然后可能使用awk解析长度:
curl -I http://google.com | grep Content-Length | awk -F ': ' '{print $2}'
这将提供以字节为单位的内容长度(如果文件存在),否则为空字符串。
发布于 2015-11-27 21:01:05
下面是一些bash代码,它检查文件是否存在于one服务器上,以及它是否与一个磁盘具有相同的大小。
不下载该文件,仅使用HEAD请求进行检查。
TMPFILE=$(tempfile)
FILE=location/some_file_to_upload.txt
MYVAR=http://example.com/$FILE
response=$(curl -n --silent -X HEAD $MYVAR --write-out "\n%{http_code}\n" -D $TMPFILE)
status_code=$(echo "$response" | sed -n '$p')
LOCAL_SIZE=$(stat -c%s "$FILE")
SERVER_SIZE=$(cat $TMPFILE|grep -i "Content-Length" | awk 'BEGIN {RS=""}{gsub(/\r/,"",$0); print $0}' | awk -F ': ' '{print $2}')
if [ -z "$VAR" ]; then
SERVER_SIZE=0
fi
if [ "$status_code" -lt "200" -o "$status_code" -gt "299" -o "$LOCAL_SIZE" -ne "$SERVER_SIZE" ]; then
echo 'file does not exist or is of different size
fi发布于 2015-11-27 18:11:24
status=$(curl --head --silent http://domain/object | head -n 1)
if echo "$status" | grep -q 404
echo "Uploading"
else
echo "Not uploading: $status"
fihttps://stackoverflow.com/questions/33954612
复制相似问题