1 # proxy.tcl --
2 #
3 # FIXME: This file needs a description here.
4 #
5 # Copyright (c) 1998-2002 The Regents of the University of California.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # A. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # B. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 # C. Neither the names of the copyright holders nor the names of its
17 # contributors may be used to endorse or promote products derived from this
18 # software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
24 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #
31 # @(#) $Header: /usr/mash/src/repository/mash/mash-1/tcl/cache/proxy.tcl,v 1.8 2002/02/03 04:25:37 lim Exp $
32
33
34 import HTTP_Server
35
36 Class WebCacheProxy -superclass HTTP_Server -configuration {
37 port 3468
38 }
39
40
41 WebCacheProxy public open { {port {}} } {
42 if { $port=={} } {
43 set port [$self get_option port]
44 }
45 puts "opening proxy on port '$port'"
46 $self next $port
47 }
48
49
50 WebCacheProxy public handle_request { socket hdr_var data } {
51 # extract the URL
52 upvar $hdr_var headers
53
54 set url $headers(url)
55 puts '$url'
56 flush stdout
57 if [regexp {^/[a-zA-Z]*://} $url] {
58 set url [string range $url 1 end]
59 $self start_fetch $url $socket
60 } elseif [regexp {^[a-zA-Z]*://} $url] {
61 $self start_fetch $url $socket
62 } else {
63 # this URL seems relative to myself
64 # we should check the Referer: tag
65
66 if [info exists headers(referer)] {
67 set referer $headers(referer)
68 } else {
69 set referer ""
70 }
71
72 set prefix [$self referer $referer]
73 if { $prefix=={} } {
74 $self instvar last_url_
75 if [info exists last_url_] {
76 set prefix [$self referer $last_url_]
77 }
78 }
79
80 if { $prefix!={} } {
81 set rdata "<h1>302 Object has moved</h1>"
82 set rheaders "HTTP/1.0 302 Object has moved\n"
83 append rheaders "Location: $prefix$url\n"
84 } else {
85 set rdata "<h1>500 Cannot resolve relative URL</h1>\n\
86 <i>$url</i>"
87 set rheaders "HTTP/1.0 500 Cannot resolve relative\
88 URL\n"
89 }
90 append rheaders "Content-type: text/html\n"
91 append rheaders "Content-length: [string length $data]\n\n"
92
93 puts "---------------------------------------------"
94 puts "sending headers: '$rheaders'"
95 puts "sending data: '$rdata'"
96 puts "---------------------------------------------"
97 $socket send $rheaders
98 $socket send $rdata
99 $socket close
100 $socket shutdown
101 delete $socket
102 }
103 return 0
104 }
105
106
107 WebCacheProxy private referer { url } {
108 set expr {^([a-zA-Z]*)://([^/]*)/([a-zA-Z]*)://([^/]*)/}
109 if ![regexp $expr $url dummy proto1 proxy proto2 server] {
110 set expr {^([a-zA-Z]*)://([^/]*)/}
111 if ![regexp $expr $url dummy proto2 server] {
112 return ""
113 }
114 set proto1 http
115 set proxy "127.0.0.1:[$self port]"
116 }
117
118 return $proto1://$proxy/$proto2://$server
119 }
120
121
122 WebCacheProxy private start_fetch { url socket } {
123 $self instvar cache_
124 puts "proxy: start_fetch $url"
125 $cache_ get $url $socket
126 $self set last_url_ $url
127 }
128
129
130 WebCacheProxy private done_fetch { url socket filename } {
131 puts "************************************proxy: done_fetch $url"
132
133 set f [open $filename]
134 fconfigure $f -translation binary
135 set buffer ""
136 while { ![eof $f] } {
137 append buffer [read $f 4096]
138 }
139 close $f
140 puts -nonewline [$socket channel] $buffer
141
142 $socket close
143 $socket shutdown
144 delete $socket
145 }
146
147
148 WebCacheProxy private done_partial_fetch { url socket filename } {
149 puts "proxy: done_partial_fetch $url"
150
151 set f [open $filename]
152 fconfigure $f -translation binary
153 set buffer ""
154 while { ![eof $f] } {
155 append buffer [read $f 4096]
156 }
157 close $f
158 puts -nonewline [$socket channel] $buffer
159
160 # don't close socket, because more data coming
161 }
162
163
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.