-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzypp-proxy.rb
152 lines (113 loc) · 3.82 KB
/
zypp-proxy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require 'rubygems'
require 'net/http'
require 'thin'
require 'xmlsimple'
class SimpleAdapter
def call(env)
[ 200, { 'Content-Type' => 'text/plain' }, ["hello!\n #{env.to_s}"] ]
end
end
class ForwardAdapter
def initialize(offset, host, port)
@offset = offset
@host = host
@port = port
puts "FWD-INIT offs: #{offset}"
puts "FWD-INIT host: #{host}"
puts "FWD-INIT port: #{port}"
end
def get_or_follow_http(host, port, path)
res_code = 0
loop_count = 0
res = Net::HTTP.new('')
while ( [0, 301, 302, 303, 307].include?(res_code) && loop_count < 12 )
loop_count += 1
req = Net::HTTP::Get.new(path)
res = Net::HTTP.start( host, port) { |http| http.request(req) }
if res.header['location']
host = URI.parse(res.header['location']).host
port = URI.parse(res.header['location']).port
path = URI.parse(res.header['location']).path
end
#res.each {|k, v| puts "#{k} : #{v}" }
res_code = res.code.to_i || 0
puts "<<--CODE #{res_code}-->> "
end
res
end
def call(env)
puts "-"*30
orig_req = Rack::Request.new(env)
#env.each {|k, v| puts "#{k} : #{v}" } if env
path = orig_req.path.sub(@offset, '')
path = '/' if path == ''
#puts " OFFSET: " + @offset + " ::: OLD PATH #{orig_req.path}" + " ::: NEW PATH #{path}"
res = get_or_follow_http( @host, @port, path )
# TODO: cache the delivered files locally and deliver them if they are equal
#res.each {|k, v| puts "#{k} : #{v}" }
#puts "CODE: #{res.code}"
[ res.code.to_i, { 'Content-Type' => res.content_type }, [ res.body ] ]
end
end
class RepoIndexAdapter
def call(env)
orig_req = Rack::Request.new(env)
# TODO: read repos.d/files and create the repoindex.xml dynamically
res_body1 = '<repoindex> </repoindex>'
res_body = XmlSimple.xml_out(REPOS, { 'RootName' => 'repoindex' } )
[ 200, { 'Content-Type' => 'text/xml' }, [ res_body ] ]
end
end
REPOS ={ 'repo' => [
{ 'name' => 'openSUSE-11.1-Update',
'alias' => 'repo-update',
'url' => 'http://localhost:3000/update/11.1/',
'description' => "Foo Bar",
'priority' => 0,
'pub' => 0
},
{ 'name' => 'openSUSE-11.1-Oss',
'alias' => 'repo-oss',
'url' => 'http://localhost:3000/distribution/11.1/repo/oss/',
'description' => "Fara und Foo",
'priority' => 0,
'pub' => 0
}
]
}
# static redirection - later read files in repos.d/ and create URLMap
host = "download.opensuse.org"
port = 80
app = Rack::URLMap.new(
'/' => ForwardAdapter.new('/', host, port),
'/service/repo/repoindex.xml' => RepoIndexAdapter.new()
#'/test' => SimpleAdapter.new,
#'/files' => Rack::File.new('.')
)
Thin::Server.new('0.0.0.0', 3000, app).start!
=begin
# how to write repoindex.xml
# from SMT: NU::RepoIndex.pm
$writer->emptyTag('repo',
'name' => $catalogName,
'alias' => $catalogName, # Alias == Name
'description' => ${$val}{'DESCRIPTION'},
'distro_target' => ${$val}{'TARGET'},
'path' => $LocalRepoPath,
'priority' => 0,
'pub' => 0
);
=end
=begin
# redirect headers
x-as : 29298
location : http://ftp5.gwdg.de/pub/opensuse/update/11.1/rpm/i686/glibc-2.9-2.3_2.10.1.i686.delta.rpm
content-type : text/html; charset=iso-8859-1
server : Apache/2.2.11 (Linux/SUSE)
date : Wed, 22 Jul 2009 16:46:06 GMT
content-length : 364
x-mirrorbrain-realm : country
x-prefix : 195.135.220.0/22
x-mirrorbrain-mirror : ftp5.gwdg.de
CODE: 302
=end