4 ms·
Here is how one might adhere to the so-called UNIX philosophy. Utility #1: 580-character shell script to generate HTTP (NB. printf is a built-in) Utility #2:
by 1vuio0pswjnm7 5y ago
Here is how one might adhere to the so-called UNIX philosophy.
Utility #1: 580-character shell script to generate HTTP (NB. printf is a built-in)
Utility #2: TCP client to send HTTP, e.g., netcat
Utility #3: (Optional) TLS proxy if encryption desired, e.g., stunnel^1
1. For more convenience use a proxy that performs DNS name lookups. Alternatively, use TLS-enabled client, e.g., openssl s_client, etc.
Advantages over curl and similar programs: HTTP/1.1 pipelining
For the purpose of an example, the shell script will be called "post". To demonstrate pipelining POST requests, we can send multiple requests to DuckDuckGo over a single TCP connection. TLS proxy is listening on 127.0.0.1:80.
#! /bin/sh
(
y=Connection;n=0;while read x;do
x1=${1#*//};x2=${x1%%/*};x3=${x1#*/};
if test x$x3 = x$x2;then x3="";fi;
x=$(printf "%s" "${x%%=*}=";echo "${x#*=}");
printf "%s\r\n%s\r\n%s\r\n%s\r\n" \
"POST /${x3} HTTP/1.1" \
"Host: ${x2}" \
"Content-Type: application/x-www-form-urlencoded" \
"Content-Length: ${#x}";
if [ $n -gt 1 ];then
printf "%s\r\n\r\n%s\r\n" "$y: keep-alive" "$x";else
printf "%s\r\n\r\n%s\r\n" "$y: close" "$x";fi;
export n=$((n+1));
done;
if [ $n -gt 1 ];then
printf "%s\r\n%s\r\n%s\r\n" \
"GET /robots.txt HTTP/1.0" \
"Host: ${x2}" \
"$y: close";fi;
)
Put the queries in a file
cat > 1.txt
q=one
q=two
q=three
^D
Send the queries
post https://lite.duckduckgo.com/lite < 1.txt|nc -vvn 127.1 80
Send the queries, save the result, then read the result
echo "<base href=https://lite.duckduckgo.com />" > 1.htm
post https://lite.duckduckgo.com/lite < 1.txt|nc -vvn 127.1 80 >> 1.htm
firefox ./1.htm
links -no-connect ./1.htm
Based on personal experience as an end user, I find that using separate utilities is faster and more flexible than curl or similar program mentioned in this thread. For me, 1. storage space for programs, e.g. large scripting language interpreters and/or other large binaries, is in short supply and 2. HTTP/1.1 pipelining is a must-have. Using separate, small utilities 1. conserves space and 2. lets me do pipelining easily. I write many single purpose utilties for own use, including one that replaces the "post" shell script in this comment.