/
GET
executable file
·176 lines (156 loc) · 4.52 KB
/
GET
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
#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Copyright: Baptiste Assmann <bedis9@gmail.com>
# curl wrapper in Bash
#
# simple curl wrapper which allows setting most commonly used option while
# not preventing the user to append his own one.
# Can be used to GET content but also to manipulate JSON queries, with JPOST,
# JGET, JDELETE, JPUT, JUP/JUPLOAD (for upload)
#
# more help here: https://github.com/bedis/curlwrapperinbash
if [ -n "$DEBUG" ]; then
set -x
fi
if [ "$1" = "--install" ]; then
set -e
# check dependency on jq
jq -h >/dev/null 2>&1
if [ $? -eq 127 ]; then
echo "jq can't be found"
exit 1
fi
# install the script in $HOME/bin
if [ ! -d $HOME/bin ]; then
echo "$HOME/bin does not exist"
exit 1
fi
if [ -f $HOME/bin/GET ]; then
echo "$HOME/bin/GET already exists"
exit 1
fi
cp $0 $HOME/bin/GET
# set the symlinks
cd $HOME/bin
ln -s GET JDELETE
ln -s GET JGET
ln -s GET JPOST
ln -s GET JPUT
ln -s GET JUP
ln -s GET JUPLOAD
cd $OLDPATH
exit 0
fi
# update curl's write-out based on request protocol
CHECK=$(echo $@ | fgrep -c -i https)
if [ $CHECK -eq 1 ]; then
OUTPUT="Remote IP: %{remote_ip}
SSL verify result: %{ssl_verify_result}
Timers:
DNS lookup: \t%{time_namelookup}
TCP handshake:\t%{time_connect}
SSL handshake:\t%{time_appconnect}
Total: \t%{time_total}
"
else
OUTPUT="Remote IP: %{remote_ip}
Timers:
DNS lookup: \t%{time_namelookup}
TCP handshake:\t%{time_connect}
Total: \t%{time_total}
"
fi
CHECK=$(echo $@ | fgrep -c -i -- '-o ')
if [ $CHECK -eq 0 ]; then
CONTENT_OUTPUT="-o /dev/null"
else
CONTENT_OUTPUT=""
fi
IFS=µ
case ${0##*/} in
JDELETE)
TMP=$(tempfile)
METHOD=${0##*/J}
echo
echo -e "\e[1mResponse headers:\e[0m"
curl --silent --insecure --dump-header - --request $METHOD \
--header "Content-Type:application/json" \
--output $TMP $@
echo -e "\e[1mJSON content:\e[0m"
jq . $TMP
jq --monochrome-output . $TMP > out.json
rm -f $TMP
;;
JPUT | JPOST)
CHECKD=$(echo $@ | fgrep -c -i -- '--d ')
CHECKDATA=$(echo $@ | fgrep -c -i -- '--data ')
DATA=""
if [ $CHECKD -eq 0 -a $CHECKDATA -eq 0 -a ! -f in.json ]; then
echo "${0##*/} reads its json content from 'in.json' or from '-d' or '--data' flags"
exit 1
fi
TMP=$(tempfile)
METHOD=${0##*/J}
echo
echo -e "\e[1mResponse headers:\e[0m"
if [ $CHECKD -eq 0 -a $CHECKDATA -eq 0 ]; then
curl --silent --insecure --dump-header - --request $METHOD --data @in.json \
--header 'Content-Type:application/json' \
--output $TMP $@
else
curl --silent --insecure --dump-header - --request $METHOD ${DATA} \
--header 'Content-Type:application/json' \
--output $TMP $@
fi
echo -e "\e[1mJSON content:\e[0m"
jq . $TMP
jq --monochrome-output . $TMP > out.json
rm -f $TMP
;;
JUP | JUPLOAD)
if [ ! -f in.raw ]; then
echo "${0##*/} reads its json from 'in.raw'"
exit 1
fi
TMP=$(tempfile)
METHOD="PUT"
echo
echo -e "\e[1mResponse headers:\e[0m"
curl --silent --insecure --dump-header - --request $METHOD --data-binary @in.raw \
--header 'Content-Type:application/octet-stream' \
--output $TMP $@
echo -e "\e[1mJSON content:\e[0m"
jq . $TMP
jq --monochrome-output . $TMP > out.json
rm -f $TMP
;;
JGET)
TMP=$(tempfile)
echo
echo -e "\e[1mResponse headers:\e[0m"
curl --silent --insecure --dump-header - --output $TMP $@
echo -e "\e[1mJSON content:\e[0m"
jq . $TMP
jq --monochrome-output . $TMP > out.json
rm -f $TMP
;;
*)
echo
echo -e "\e[1mResponse headers:\e[0m"
echo -e $(curl $CONTENT_OUTPUT --silent --insecure --dump-header - \
--write-out "\n\n$OUTPUT" \
$@ | sed 's/Remote IP\|Timers\|SSL verify result\|DNS lookup\|TCP handshake\|SSL handshake\|Total/\\e[1m&\\e[0m/')
echo
;;
esac