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/srmv2-cache/proxy.tcl,v 1.5 2002/02/03 04:30:05 lim Exp $
32
33
34 import HTTP_Server
35
36 Class SRMv2_CacheProxy -superclass HTTP_Server -configuration {
37 srmv2CachePort 3468
38 }
39
40
41 SRMv2_CacheProxy public open { {port {}} } {
42 if { $port=={} } {
43 set port [$self get_option srmv2CachePort]
44 }
45 $self next $port
46 }
47
48
49 SRMv2_CacheProxy public handle_request { socket headers data } {
50 # extract the URL
51
52 $self headers_to_array $headers hdr_array
53 set url $hdr_array(url)
54 puts '$url'
55 flush stdout
56 if [regexp {^/[a-zA-Z]*://} $url] {
57 set url [string range $url 1 end]
58 $self start_fetch $url $socket
59 } elseif [regexp {^[a-zA-Z]*://} $url] {
60 $self start_fetch $url $socket
61 } else {
62 # this URL seems relative to myself
63 # we should check the Referer: tag
64
65 if [info exists hdr_array(referer)] {
66 set referer $hdr_array(referer)
67 } else {
68 set referer ""
69 }
70
71 set prefix [$self referer $referer]
72 if { $prefix=={} } {
73 $self instvar last_url_
74 if [info exists last_url_] {
75 set prefix [$self referer $last_url_]
76 }
77 }
78
79 if { $prefix!={} } {
80 set data "<h1>302 Object has moved</h1>"
81 set headers "HTTP/1.0 302 Object has moved\n"
82 append headers "Location: $prefix$url\n"
83 } else {
84 set data "<h1>500 Cannot resolve relative URL</h1>\n\
85 <i>$url</i>"
86 set headers "HTTP/1.0 500 Cannot resolve relative\
87 URL\n"
88 }
89 append headers "Content-type: text/html\n"
90 append headers "Content-length: [string length $data]\n\n"
91
92 puts "---------------------------------------------"
93 puts "sending headers: '$headers'"
94 puts "sending data: '$data'"
95 puts "---------------------------------------------"
96 $socket send $headers
97 $socket send $data
98 $socket close
99 $socket shutdown
100 }
101 }
102
103
104 SRMv2_CacheProxy private referer { url } {
105 set expr {^([a-zA-Z]*)://([^/]*)/([a-zA-Z]*)://([^/]*)/}
106 if ![regexp $expr $url dummy proto1 proxy proto2 server] {
107 set expr {^([a-zA-Z]*)://([^/]*)/}
108 if ![regexp $expr $url dummy proto2 server] {
109 return ""
110 }
111 set proto1 http
112 set proxy "127.0.0.1:[$self port]"
113 }
114
115 return $proto1://$proxy/$proto2://$server
116 }
117
118
119 SRMv2_CacheProxy private start_fetch { url socket } {
120 $self instvar wc_
121 puts "wcp: start_fetch $url"
122 $wc_ get $url $socket
123 }
124
125
126 SRMv2_CacheProxy private done_fetch { url socket filename } {
127 puts "wcp: done_fetch $url"
128
129 set f [open $filename]
130 fconfigure $f -translation binary
131 set buffer ""
132 while { ![eof $f] } {
133 append buffer [read $f 4096]
134 }
135 close $f
136 puts -nonewline [$socket channel] $buffer
137
138 $socket close
139 $socket shutdown
140 }
141
142
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.