Class: BinProxy::Connection::Filters::HTTPConnect

Inherits:
Base
  • Object
show all
Includes:
Logger
Defined in:
lib/binproxy/connection/filters.rb

Overview

TODO - lots of copy-paste from SOCKS, could stand to refactor

Instance Attribute Summary

Attributes inherited from Base

#conn

Instance Method Summary collapse

Methods included from Logger

log

Methods inherited from Base

#initialize, #write

Constructor Details

This class inherits a constructor from BinProxy::Connection::Filters::Base

Instance Method Details

#initObject



133
134
135
136
# File 'lib/binproxy/connection/filters.rb', line 133

def init
  @buf = StringIO.new
  @state = :new
end

#read(data) ⇒ Object



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
# File 'lib/binproxy/connection/filters.rb', line 137

def read(data)
  log.debug "HTTPConnect read data #{data.inspect} in state #{@state}"
  return data if @state == :connected
  raise "unexpected data while connecting" if @state == :connecting

  #append, but keep current position
  p = @buf.pos
  @buf << data
  @buf.pos = p

  while line = @buf.gets #XXX assumes we get whole lines
    log.debug "processing line #{line}, state=#{@state}"
    case @state
    when :new
      if m = line.match( %r<\ACONNECT ([\w.-]+):(\d+) HTTP/1.1\r\n\z> )
        @host, @port = m[1], m[2]
        @state = :headers
        log.debug "Got CONNECT message to #{@host}:#{@port}"
      else
        log.warn "expected a CONNECT request, got #{line.inspect}"
      end
    when :headers
      if line == "\r\n"
        log.debug "End of CONNECT headers"
        @state = :connecting
        conn.connect @host, @port
        return nil #XXX TODO confirm that @buf is empty
      else
        log.debug "Extra header on CONNECT: #{line.inspect}"
      end
    else
      log.fatal "HTTPConnect filter in bad state: #{@state}"
    end
  end
  log.debug "loop terminated with line #{line.inspect}"

  #not done with CONNECT yet
  nil
rescue EM::ConnectionError => e
  #synchronous error when connecting upstream, e.g. bogus hostname
  log.warn "Can't connect to '#{m[1]}:#{m[2]}': #{e.message}"
  #TODO -close the connection
  nil
end

#session_closing(reason) ⇒ Object



186
187
188
# File 'lib/binproxy/connection/filters.rb', line 186

def session_closing(reason)
  conn.send_data "HTTP/1.1 502 BINPROXY FAIL\r\n\r\n" if @state == :connecting
end

#upstream_connected(upstream_conn) ⇒ Object



181
182
183
184
185
# File 'lib/binproxy/connection/filters.rb', line 181

def upstream_connected(upstream_conn)
  log.error "unexpected upstream_connected in state #{@state}, conn=#{conn}" unless @state == :connecting
  @state = :connected
  conn.send_data "HTTP/1.1 200 BINPROXY OK\r\n\r\n"
end