-
Notifications
You must be signed in to change notification settings - Fork 0
/
curlsh
executable file
·149 lines (123 loc) · 3.3 KB
/
curlsh
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
#!/bin/bash
VERSION=1.0
PROG="$(basename $0)"
RED=$'\033[31m'
GREEN=$'\033[32m'
YELLOW=$'\033[33m'
RST=$'\033[0m'
read -d '' USAGE << END
usage: $PROG HOST [LOCALHOST]
Call curl interactively on LOCALHOST and set HOST as host header.
This program is mainly used to simulate requests to a host and to serve
as a REPL/shell of kinds around cURL.
Features:
* automatically set Host header
* prepend paths with slash
* readline and history support
* in interactive mode, prepend paths with cURL parameters:
--include /foo
will include the HTTP response headers in the answer.
* in interactive mode, append commands to pipe the response to:
/foo | less
will let you read the response in your pager.
/foo.json | jq -r .some_field
will use the power of jq to only print relevant parts of a JSON payload.
END
HOST="$1"
LOCALHOST="${2:-$HOST}"
NPARAM=$#
PIPE_TO=cat
CURL_OPTS="--silent --show-error"
USER_AGENT="curlsh $VERSION (https://github.com/port8000/curlsh)"
HISTFILE="$HOME/.curlsh.history"
ALIAS() {
if [[ $1 == $HOST ]]; then
HOST="$2"
if [[ $NPARAM == 1 ]]; then
LOCALHOST="${3:-$HOST}"
fi
fi
}
if [[ -f $HOME/.curlshrc ]]; then
. "$HOME/.curlshrc"
fi
case $HOST in
"" | "-h" | "-?" | "--help" )
echo "$USAGE" >&2
if [[ ! $HOST ]]; then
exit 1
else
exit 0
fi
;;
--version )
echo "$PROG $VERSION"
echo ""
echo "curl version used:"
curl --version
exit 0
;;
http://* | https://* )
HOST="$(echo "$HOST" | sed 's/^https\?:\/\///')"
;;
esac
# say hello
echo "* connecting to $RED$LOCALHOST$RST pretending to ask $GREEN$HOST$RST"
echo "* keep input empty or write 'exit' to quit"
echo "* write '/' to get homepage"
echo "* prepend additional cURL parameters: '-v /'"
echo " Commonly used:"
echo " * -i, --include : include response headers"
echo " * -k, --insecure : ignore SSL errors"
echo " * -L, --location : follow redirects"
echo " * -v, --verbose : print request headers, too"
echo "* append '| less' to pipe the output through 'less' (or any other program)"
echo ""
# set up history
if [[ $HISTFILE ]]; then
export HISTFILE
touch "$HISTFILE"
history -r
fi
# start main repl
while read -d $'\n' -ep "$GREEN${HOST:0:30}$YELLOW/$RST " _PATH; do
CURRENT_CURL_OPTS=
CURRENT_PIPE_TO="$PIPE_TO"
case $_PATH in
"" | exit | quit | bye )
break 2
;;
esac
history -s -- "$_PATH"
case $_PATH in
*\|* )
# split off pipe command
CURRENT_PIPE_TO="${_PATH##*|}"
# trim whitespace the haXor way:
_PATH="$(echo "${_PATH%|*}" | xargs)"
;;
esac
case $_PATH in
-*\ * )
# split off extra curl parameters
CURRENT_CURL_OPTS="${_PATH% *}"
_PATH="${_PATH##* }"
;;
esac
case $_PATH in
[^/]* )
# prepend slash if missing
_PATH="/$_PATH"
;;
esac
curl --user-agent "$USER_AGENT" \
--header "Host: $HOST" \
$CURL_OPTS \
$CURRENT_CURL_OPTS \
"$LOCALHOST$_PATH" | \
$CURRENT_PIPE_TO
done
# append to history on exit
if [[ $HISTFILE ]]; then
history -a
fi