Version 1.8.0.0
[socat.git] / proxyecho.sh
blobf5d0706718a5c39453f7c837830217d09166bdfb
1 #! /usr/bin/env bash
2 # source: proxyecho.sh
3 # Copyright Gerhard Rieger and contributors (see file CHANGES)
4 # Published under the GNU General Public License V.2, see file COPYING
6 # perform primitive simulation of a proxy server with echo function via stdio.
7 # accepts and answers correct HTTP CONNECT requests, but then just echoes data.
8 # it is required for test.sh
9 # for TCP, use this script as:
10 # socat TCP-L:8080,reuseaddr,crlf SYSTEM:"proxyecho.sh"
12 # 20230423 GR Added option -V to require particular HTTP version
14 if type socat >/dev/null 2>&1; then
15 SOCAT=socat
16 else
17 SOCAT=./socat
20 case `uname` in
21 HP-UX|OSF1)
22 CAT="$SOCAT -u stdin stdout"
25 CAT=cat
27 esac
29 SPACES=" " REQVER=1.0
30 while [ -n "$1" ]; do
31 case "$1" in
32 -w) n="$2"; while [ "$n" -gt 0 ]; do SPACES="$SPACES "; n=$((n-1)); done
33 shift ;;
34 -V) shift; REQVER="$1" ;;
35 #-s) STAT="$2"; shift ;;
36 esac
37 shift
38 done
40 # read and parse HTTP request
41 read l
42 if ! echo "$l" |egrep '^CONNECT +[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+ +HTTP/[1-3].[0-9]$' >/dev/null
43 then
44 echo "HTTP/1.0${SPACES}500 Bad Request"
45 echo
46 exit
48 if ! echo "$l" |egrep '^CONNECT +[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+ +HTTP/'"$REQVER"'$' >/dev/null
49 then
50 echo "HTTP/1.0${SPACES}426 Upgrade Required"
51 echo
52 exit
55 # read more headers until empty line
56 while [ -n "$l" ]; do
57 read l
58 done
60 # send status
61 echo "HTTP/$REQVER${SPACES}200 OK"
62 # send empty line
63 echo
65 # perform echo function
66 exec $CAT