-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpinfo.sh
executable file
·103 lines (83 loc) · 2.48 KB
/
httpinfo.sh
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/env bash
# Copyright (C) 2022 Temuri Takalandze.
# License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
#
# Written by Temuri Takalandze.
set -e
function print_help() {
cat <<EOF
Usage: httpinfo [OPTION]... STATUS_CODE
Display information about HTTP status codes.
-v, --version output version information and exit
-h, --help display this help and exit
Example:
httpinfo 200
EOF
}
function print_version() {
cat <<EOF
httpinfo 1.0.3
Copyright (C) 2022 Temuri Takalandze.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Temuri Takalandze.
EOF
}
function print_status_code_description() {
BASE_URL=https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
HTTP_RESPONSE=$(curl -s -o /tmp/httpinfo -w "%{http_code}" "$BASE_URL/$1")
if [ "$HTTP_RESPONSE" -eq 404 ]; then
echo "httpinfo: unknown HTTP Status Code $1"
exit 2
elif [ ! "$HTTP_RESPONSE" -eq 200 ]; then
echo "httpinfo: unable to process request. Code: $HTTP_RESPONSE"
exit 3
fi
TRIMMED_HTML=$(< /tmp/httpinfo tr -d '\n')
rm -f /tmp/httpinfo
ARTICLE_CONTENT=$(echo "$TRIMMED_HTML" | grep -o -P '<article class="main-page-content" lang="en-US">(.*?)<\/article>')
TITLE=$(echo "$ARTICLE_CONTENT" | grep -o -P '(?<=<h1>).*(?=</h1>)')
DESCRIPTION=$(echo "$ARTICLE_CONTENT" | grep -o -P '(?<=<div class="section-content">).*(?=</div><section aria-labelledby="status">)')
echo "$TITLE"
repeat_text ${#TITLE} "="
echo "$DESCRIPTION" | sed -e "s/<[p>]*>/\n\n/g" | sed -e "s/<[^>]*>//g"
echo -e "\n"
repeat_text "$(tput cols)" "-"
echo -e "\n"
echo -e "Read more at: $BASE_URL/$1"
}
function repeat_text() {
for _ in $(seq 1 "$1"); do echo -n "$2"; done
}
POSITIONALS=()
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
print_help
exit 0
;;
-v | --version)
print_version
exit 0
;;
-*)
echo "httpinfo: unknown option '$1'"
echo "Try 'httpinfo --help' for more information."
exit 1
;;
*)
POSITIONALS+=("$1")
shift
;;
esac
done
set -- "${POSITIONALS[@]}"
if [ ! $# -eq 1 ]; then
echo "httpinfo: invalid arguments count"
echo "Try 'httpinfo --help' for more information."
exit 1
fi
print_status_code_description "$1"