加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何使用Bash解析HTTP头?

发布时间:2020-12-14 01:50:30 所属栏目:Linux 来源:网络整理
导读:我需要从使用curl的网页标题中获取2个值.我已经能够使用以下方法单独获取值: response1=$(curl -I -s http://www.example.com | grep HTTP/1.1 | awk {'print $2'})response2=$(curl -I -s http://www.example.com | grep Server: | awk {'print $2'}) 但我
我需要从使用curl的网页标题中获取2个值.我已经能够使用以下方法单独获取值:

response1=$(curl -I -s http://www.example.com | grep HTTP/1.1 | awk {'print $2'})
response2=$(curl -I -s http://www.example.com | grep Server: | awk {'print $2'})

但我无法弄清楚如何使用单个curl请求单独grep值,如:

response=$(curl -I -s http://www.example.com)
http_status=$response | grep HTTP/1.1 | awk {'print $2'}
server=$response | grep Server: | awk {'print $2'}

每次尝试都会导致错误消息或空值.我确信这只是一个语法问题.

解决方法

完全bashsolution.演示如何在不需要awk的情况下轻松解析其他标头:

shopt -s extglob # Required to trim whitespace; see below

while IFS=':' read key value; do
    # trim whitespace in "value"
    value=${value##+([[:space:]])}; value=${value%%+([[:space:]])}

    case "$key" in
        Server) SERVER="$value"
                ;;
        Content-Type) CT="$value"
                ;;
        HTTP*) read PROTO STATUS MSG <<< "$key{$value:+:$value}"
                ;;
     esac
done < <(curl -sI http://www.google.com)
echo $STATUS
echo $SERVER
echo $CT

生产:

302
GFE/2.0
text/html; charset=UTF-8

根据RFC-2616,HTTP标头的建模如“Standard for the Format of ARPA Internet Text Messages” (RFC822)所述,其中明确说明了第3.1.2节:

The field-name must be composed of printable ASCII characters
(i.e.,characters that have values between 33. and 126.,
decimal,except colon). The field-body may be composed of any
ASCII characters,except CR or LF. (While CR and/or LF may be
present in the actual text,they are removed by the action of
unfolding the field.)

所以上面的脚本应该捕获任何RFC- [2] 822兼容的头,但有一个明显的例外,即folded headers.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读