File: proxy.rb

package info (click to toggle)
libwww-mechanize-ruby 0.6.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 660 kB
  • ctags: 723
  • sloc: ruby: 5,475; makefile: 5
file content (30 lines) | stat: -rw-r--r-- 728 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
# This is a simple proxy that assumes the destination server will
# close the connection after sending data, otherwise it will get blocked
# on reads.

require 'rubygems'
require 'eventmachine'
require 'socket'

module HttpProxy
  include Socket::Constants

  def receive_data(data)
    if data =~ /Host: (.*)$/
      (host, port) = $1.chomp.split(/:/)
      port ||= 80
      socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
      puts port.to_i
      puts host
      sockaddr = Socket.pack_sockaddr_in( port.to_i, host )
      socket.connect(sockaddr)
      socket.write(data)
      results = socket.read
      send_data results
    end
  end
end

EventMachine::run {
  EventMachine::start_server "127.0.0.1", 2001, HttpProxy
}