-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprober.sh
executable file
·206 lines (189 loc) · 4.42 KB
/
prober.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
# -*- coding: UTF8 -*-
# ---------------------------------------------
# @author: Guillaume Seren
# @since: 17/06/2015
# source: https://github.com/GuillaumeSeren/spider
# file: prober.sh
# Licence: GPLv3
# ---------------------------------------------
# Variables {{{1
declare -A content_type=(
[text/html]='html'
[text/html\; charset=utf-8]='html'
[text/html\; charset=\"UTF-8\"]='html'
[text/html\; charset=UTF-8]='html'
[text/plain]='text'
[text/plain\;charset=UTF-8]='text'
[text/xml\; charset=UTF-8]='xml'
[application/xml]='xml'
[image/png]='media'
[image/gif]='media'
[image/jpeg]='media'
[text/javascript]='script'
[application/javascript]='script'
[text/css]='style'
[application/rss+xml\; charset=utf-8]='feed'
[application/atom+xml\; charset=utf-8]='feed'
[application/rss+xml\; charset=\"UTF-8\"]='feed'
[application/opensearchdescription+xml\; charset=utf-8]='search'
)
# usage() {{{1
# Return the helping message for the use.
function usage()
{
cat << DOC
usage: "$0" options
Test a URL and return HTTP state.
OPTIONS:
-h Show this message.
-m Select the mode:
'' | http: return http code (default)
content-type: return simplified content-type
-u url (needed)
Sample:
Test a simple url
"$0" -u guillaumeseren.com
DOC
}
# getUrlHttpStatus() {{{1
function getUrlHttpStatus() {
local httpStatus
if [[ -n "$1" ]]; then
httpStatus=$(curl --write-out %{http_code} --silent --output /dev/null "$1")
else
httpStatus=""
fi
echo "$httpStatus"
}
# getContentType() {{{1
# @FIXME: export usefull function in a lib file
# and use addUniqInArray()
function getContentType() {
local key item match
declare -a array
for key in "${!content_type[@]}"
do
match='0'
for item in "${array[@]}";
do
if [[ "${key}" == "${item}" ]]; then
match='1'
fi
done
# Si pas de match ou tableau vide
if [[ "${match}" == "0" ]]; then
array=(${array[@]} "${key}")
fi
done
echo "${array[@]}"
}
# getSimpleContentType() {{{1
# @FIXME: export usefull function in a lib file
# and use addUniqInArray()
function getSimpleContentType() {
local value item match
declare -a array
for value in "${content_type[@]}"
do
match='0'
for item in "${array[@]}";
do
if [[ "${value}" == "${item}" ]]; then
match='1'
fi
done
# Si pas de match ou tableau vide
if [[ "${match}" == "0" ]]; then
array=(${array[@]} ${value})
fi
done
echo "${array[@]}"
}
# getUrlContentType() {{{1
function getUrlContentType() {
local httpContentType
if [[ -n "$1" ]]; then
httpContentType=$(curl --write-out %{content_type} --silent --output /dev/null "$1")
else
httpContentType=""
fi
echo "$httpContentType"
}
# getUrlContentTypeSimple() {{{1
function getUrlContentTypeSimple() {
local contentType
if [[ -n "$1" ]]; then
# Check if the key exist
if [ -n "${content_type[$1] + 1}" ]; then
contentType="${content_type[$1]}"
else
echo "Prober unknown content-type of $1"
fi
else
contentType=""
fi
echo "$contentType"
}
# main() {{{1
function main() {
local result
# check mode
case "$cmdMode" in
'' | 'http')
result="$(getUrlHttpStatus "$cmdUrl")"
;;
'content-type')
result="$(getUrlContentType "$cmdUrl")"
result="$(getUrlContentTypeSimple "$result")"
;;
esac
echo "$result"
}
# GETOPTS {{{1
# Get the param of the script.
while getopts ":u:m:l:h" OPTION
do
flagGetOpts=1
case $OPTION in
h)
usage
exit 1
;;
u)
cmdUrl="$OPTARG"
;;
m)
cmdMode="$OPTARG"
# validate allowed mode
# nil or '' default http content-type
if [[ "$cmdMode" != '' && "$cmdMode" != 'http' && "$cmdMode" != 'content-type' ]]; then
echo "Prober unknown mode : $cmdMode"
exit 4
fi
;;
l)
cmdList="$OPTARG"
# List options
if [[ "$cmdList" == 'simple' ]]; then
# || [[ "$cmdList" == 'full' ]];
getSimpleContentType
elif [[ "$cmdList" == "full" ]]; then
getContentType
fi
;;
?)
echo "commande $1 inconnue"
usage
exit 6
;;
esac
done
# We check if getopts did not find no any param
if [ "$flagGetOpts" == 0 ]; then
echo 'This script cannot be launched without options.'
usage
exit 1
fi
main
# vim: set ft=sh ts=2 sw=2 tw=80 foldmethod=marker et :