add listen-timeout to function as an accept timeout
[socat/sam.git] / proxy.sh
blob09b1b062b02967d6fc949b93ad796b304f81863c
1 #! /bin/bash
2 # source: proxy.sh
3 # Copyright Gerhard Rieger 2003-2004
4 # Published under the GNU General Public License V.2, see file COPYING
6 # perform primitive simulation of a proxy server.
7 # accepts and answers correct HTTP CONNECT requests on stdio, and tries to
8 # establish the connection to the given server.
9 # it is required for socats test.sh
10 # for TCP, use this script as:
11 # socat tcp-l:8080,reuseaddr,fork exec:"proxy.sh",nofork
13 if [ -z "$SOCAT" ]; then
14 if type socat >/dev/null 2>&1; then
15 SOCAT=socat
16 else
17 SOCAT="./socat"
21 if [ $(echo "x\c") = "x" ]; then E=""
22 elif [ $(echo -e "x\c") = "x" ]; then E="-e"
23 else
24 echo "cannot suppress trailing newline on echo" >&2
25 exit 1
27 ECHO="echo $E"
28 CR=$($ECHO "\r")
29 #echo "CR=$($ECHO "$CR\c" |od -c)" >&2
31 case `uname` in
32 HP-UX|OSF1)
33 # their cats are too stupid to work with unix domain sockets
34 CAT="$SOCAT -u stdin stdout"
37 CAT=cat
39 esac
41 SPACES=" "
42 while [ -n "$1" ]; do
43 case "$1" in
44 -w) n="$2"; while [ "$n" -gt 0 ]; do SPACES="$SPACES "; n=$((n-1)); done
45 shift ;;
46 #-s) STAT="$2"; shift ;;
47 esac
48 shift
49 done
51 # read and parse HTTP request
52 read l
53 if echo "$l" |egrep '^CONNECT +[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+ +HTTP/1.[01]' >/dev/null
54 then
55 : go on below
56 else
57 $ECHO "HTTP/1.0${SPACES}500 Bad Request$CR"
58 $ECHO "$CR"
59 exit
62 # extract target server name/address
63 s=`echo $l |awk '{print($2);}'`
65 # read more headers until empty line
66 while [ "$l" != "$CR" ]; do
67 read l
68 done
70 # send status
71 $ECHO "HTTP/1.0${SPACES}200 OK$CR"
72 # send empty line
73 $ECHO "$CR"
75 # perform proxy (relay) function
76 exec $SOCAT $SOCAT_OPTS - tcp:$s