File: stallion.rb

package info (click to toggle)
ruby-em-http-request 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 628 kB
  • ctags: 243
  • sloc: ruby: 3,478; makefile: 2
file content (316 lines) | stat: -rw-r--r-- 9,846 bytes parent folder | download
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# #--
# Includes portion originally Copyright (C)2008 Michael Fellinger
# license See file LICENSE for details
# #--
Encoding.default_external = "UTF-8"
Encoding.default_internal = "UTF-8"


require 'rack'

module Stallion
  class Mount
    def initialize(name, *methods, &block)
      @name, @methods, @block = name, methods, block
    end

    def ride
      @block.call
    end

    def match?(request)
      method = request['REQUEST_METHOD']
      right_method = @methods.empty? or @methods.include?(method)
    end
  end

  class Stable
    attr_reader :request, :response

    def initialize
      @boxes = {}
    end

    def in(path, *methods, &block)
      mount = Mount.new(path, *methods, &block)
      @boxes[[path, methods]] = mount
      mount
    end

    def call(request, response)
      @request, @response = request, response
      @boxes.each do |(path, methods), mount|
        if mount.match?(request)
          mount.ride
        end
      end
    end
  end

  STABLES = {}

  def self.saddle(name = nil)
    STABLES[name] = stable = Stable.new
    yield stable
  end

  def self.run(options = {})
    options = {:Host => "127.0.0.1", :Port => 8090}.merge(options)
    Rack::Handler::WEBrick.run(Rack::Lint.new(self), options)
  end

  def self.call(env)
    request = Rack::Request.new(env)
    response = Rack::Response.new

    STABLES.each do |name, stable|
      stable.call(request, response)
    end

    response.finish
  end
end

Stallion.saddle :spec do |stable|
  stable.in '/' do

    if stable.request.path_info == '/fail'
      stable.response.status = 404

    elsif stable.request.path_info == '/fail_with_nonstandard_response'
      stable.response.status = 420

    elsif stable.request.query_string == 'q=test'
      stable.response.write 'test'

    elsif stable.request.path_info == '/echo_query'
      stable.response["ETag"] = "abcdefg"
      stable.response["Last-Modified"] = "Fri, 13 Aug 2010 17:31:21 GMT"
      stable.response.write stable.request.query_string

    elsif stable.request.path_info == '/echo_headers'
      stable.response["Set-Cookie"] = "test=yes"
      stable.response["X-Forward-Host"] = "proxy.local"
      stable.response.write stable.request.query_string

    elsif stable.request.path_info == '/echo_content_length'
      stable.response.write stable.request.content_length

    elsif stable.request.path_info == '/echo_content_length_from_header'
      stable.response.write "content-length:#{stable.request.env["CONTENT_LENGTH"]}"

    elsif stable.request.head? && stable.request.path_info == '/'
      stable.response.status = 200

    elsif stable.request.delete?
      stable.response.status = 200

    elsif stable.request.put?
      stable.response.write stable.request.body.read

    elsif stable.request.post? || stable.request.patch?
      if stable.request.path_info == '/echo_content_type'
        stable.response["Content-Type"] = stable.request.env["CONTENT_TYPE"] || 'text/html'
        stable.response.write stable.request.env["CONTENT_TYPE"]
      else
        stable.response.write stable.request.body.read
      end

    elsif stable.request.path_info == '/set_cookie'
      stable.response["Set-Cookie"] = "id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
      stable.response.write "cookie set"

    elsif stable.request.path_info == '/set_multiple_cookies'
      stable.response["Set-Cookie"] = [
        "id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;",
        "id=2;"
      ]
      stable.response.write "cookies set"

    elsif stable.request.path_info == '/echo_cookie'
      stable.response.write stable.request.env["HTTP_COOKIE"]

    elsif stable.request.path_info == '/timeout'
      sleep(10)
      stable.response.write 'timeout'

    elsif stable.request.path_info == '/cookie_parrot'
      stable.response.status = 200
      stable.response["Set-Cookie"] = stable.request.env['HTTP_COOKIE']

    elsif stable.request.path_info == '/redirect'
      stable.response.status = 301
      stable.response["Location"] = "/gzip"
      stable.response.write 'redirect'

    elsif stable.request.path_info == '/redirect/created'
      stable.response.status = 201
      stable.response["Location"] = "/"
      stable.response.write  'Hello, World!'

    elsif stable.request.path_info == '/redirect/multiple-with-cookie'
      stable.response.status = 301
      stable.response["Set-Cookie"] = "another_id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
      stable.response["Location"] = "/redirect"
      stable.response.write 'redirect'

    elsif stable.request.path_info == '/redirect/bad'
      stable.response.status = 301
      stable.response["Location"] = "http://127.0.0.1:8090"

    elsif stable.request.path_info == '/redirect/timeout'
      stable.response.status = 301
      stable.response["Location"] = "http://127.0.0.1:8090/timeout"

    elsif stable.request.path_info == '/redirect/head'
      stable.response.status = 301
      stable.response["Location"] = "/"

    elsif stable.request.path_info == '/redirect/middleware_redirects_1'
      stable.response.status = 301
      stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
      stable.response["Location"] = "/redirect/middleware_redirects_2"

    elsif stable.request.path_info == '/redirect/middleware_redirects_2'
      stable.response.status = 301
      stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
      stable.response["Location"] = "/redirect/middleware_redirects_3"

    elsif stable.request.path_info == '/redirect/middleware_redirects_3'
      stable.response.status = 200
      stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]

    elsif stable.request.path_info == '/redirect/nohost'
      stable.response.status = 301
      stable.response["Location"] = "http:/"

    elsif stable.request.path_info == '/redirect/badhost'
      stable.response.status = 301
      stable.response["Location"] = "http://$$$@$!%&^"

    elsif stable.request.path_info == '/redirect/http_no_port'
      stable.response.status = 301
      stable.response["Location"] = "http://host/"

    elsif stable.request.path_info == '/redirect/https_no_port'
      stable.response.status = 301
      stable.response["Location"] = "https://host/"

    elsif stable.request.path_info == '/redirect/http_with_port'
      stable.response.status = 301
      stable.response["Location"] = "http://host:80/"

    elsif stable.request.path_info == '/redirect/https_with_port'
      stable.response.status = 301
      stable.response["Location"] = "https://host:443/"

    elsif stable.request.path_info == '/gzip'
      io = StringIO.new
      gzip = Zlib::GzipWriter.new(io)
      gzip << "compressed"
      gzip.close

      stable.response.write io.string
      stable.response["Content-Encoding"] = "gzip"

    elsif stable.request.path_info == '/gzip-large'
      contents = File.open(File.dirname(__FILE__) + "/fixtures/gzip-sample.gz", 'r') { |f| f.read }

      stable.response.write contents
      stable.response["Content-Encoding"] = "gzip"

    elsif stable.request.path_info == '/deflate'
      deflater = Zlib::Deflate.new(
        Zlib::DEFAULT_COMPRESSION,
        -Zlib::MAX_WBITS, # drop the zlib header which causes both Safari and IE to choke
        Zlib::DEF_MEM_LEVEL,
        Zlib::DEFAULT_STRATEGY
      )
      deflater.deflate("compressed")
      stable.response.write deflater.finish
      stable.response["Content-Encoding"] = "deflate"

    elsif stable.request.env["HTTP_IF_NONE_MATCH"]
      stable.response.status = 304

    elsif stable.request.path_info == '/auth' && stable.request.env["HTTP_AUTHORIZATION"]
      stable.response.status = 200
      stable.response.write stable.request.env["HTTP_AUTHORIZATION"]
    elsif stable.request.path_info == '/authtest'
      auth = "Basic %s" % Base64.encode64(['user', 'pass'].join(':')).split.join
      if auth == stable.request.env["HTTP_AUTHORIZATION"]
        stable.response.status = 200
        stable.response.write 'success'
      else
        stable.response.status = 401
      end
    elsif stable.request.path_info == '/relative-location'
      stable.response.status = 301
      stable.response["Location"] = '/forwarded'
    elsif stable.request.path_info == '/echo-user-agent'
      stable.response.write stable.request.env["HTTP_USER_AGENT"].inspect

    elsif
      stable.response.write  'Hello, World!'
    end

  end
end

Thread.new do
  begin
    Stallion.run :Host => '127.0.0.1', :Port => 8090
  rescue Exception => e
    print e
  end
end

#
# Simple HTTP Proxy server
#
Thread.new do
  server = TCPServer.new('127.0.0.1', 8083)
  loop do
    session = server.accept
    request = ""
    while (data = session.gets) != "\r\n"
      request << data
    end
    parts = request.split("\r\n")
    method, destination, http_version = parts.first.split(' ')
    proxy = parts.find { |part| part =~ /Proxy-Authorization/ }
    if destination =~ /^http:/
      uri = Addressable::URI.parse(destination)
      absolute_path = uri.path + (uri.query ? "?#{uri.query}" : "")
      client = TCPSocket.open(uri.host, uri.port || 80)

      client.write "#{method} #{absolute_path} #{http_version}\r\n"
      parts[1..-1].each do |part|
        client.write "#{part}\r\n"
      end

      client.write "\r\n"
      client.flush
      client.close_write

      # Take the initial line from the upstream response
      session.write client.gets

      if proxy
        session.write "X-Proxy-Auth: #{proxy}\r\n"
      end

      # What (absolute) uri was requested?  Send it back in a header
      session.write "X-The-Requested-URI: #{destination}\r\n"

      while data = client.gets
        session.write data
      end
      session.flush
      client.close
    end
    session.close
  end
end

sleep(1)