Class: ODDB::HttpSession

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
src/util/http.rb

Direct Known Subclasses

MedData::Session, Swissreg::Session

Defined Under Namespace

Classes: ResponseWrapper

Constant Summary

HTTP_CLASS =
Net::HTTP
RETRIES =
3
RETRY_WAIT =
10

Instance Method Summary (collapse)

Constructor Details

- (HttpSession) initialize(http_server, port = 80)

A new instance of HttpSession



62
63
64
65
66
67
# File 'src/util/http.rb', line 62

def initialize(http_server, port=80)
  @http_server = http_server
  @http = self.class::HTTP_CLASS.new(@http_server, port)
  @output = ''
  super(@http)
end

Instance Method Details

- (Object) get(*args)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'src/util/http.rb', line 98

def get(*args)
  retries = RETRIES
  begin
    @http.get(*args)
  rescue Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError
    if(retries > 0)
      retries -= 1
      sleep RETRIES - retries
      retry
    else
      raise
    end
  end
end

- (Object) get_headers



112
113
114
115
116
117
118
119
120
121
122
# File 'src/util/http.rb', line 112

def get_headers
  [  
    ['Host', @http_server],
    ['User-Agent', "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; de-de) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.22"],
    ['Accept', 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1'],
    ['Accept-Language', 'de-ch,en-us;q=0.7,en;q=0.3'],       
    ['Accept-Charset', 'UTF-8'],
    ['Keep-Alive', '300'],
    ['Connection', 'keep-alive'],
  ]
end

- (Object) post(path, hash)



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
# File 'src/util/http.rb', line 68

def post(path, hash)
  retries = RETRIES
  headers = post_headers
  begin
    resp = @http.post(path, post_body(hash), headers)
    case resp
    when Net::HTTPOK
      ResponseWrapper.new(resp)
    when Net::HTTPFound
      uri = URI.parse(resp['location'])
      path = (uri.respond_to?(:request_uri)) ? uri.request_uri : uri.to_s
      warn(sprintf("redirecting to: %s", path))
      get(path)
    else
      raise("could not connect to #{@http_server}: #{resp}")
    end
  rescue Errno::ECONNRESET, EOFError
    if(retries > 0)
      retries -= 1
      sleep RETRIES - retries
      retry
    else
      raise
    end
  end
end

- (Object) post_body(data)



123
124
125
126
127
128
# File 'src/util/http.rb', line 123

def post_body(data)
  sorted = data.collect { |pair| 
    pair.collect { |item| CGI.escape(item) }.join('=') 
  }
  sorted.join("&")
end

- (Object) post_headers



94
95
96
97
# File 'src/util/http.rb', line 94

def post_headers
  headers = get_headers
  headers.push(['Content-Type', 'application/x-www-form-urlencoded'])
end