Report abuse

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
    def do_rpc(request, async=false)
      header = {  
       "User-Agent"     =>  USER_AGENT,
       "Content-Type"   => "text/xml",
       "Content-Length" => request.size.to_s, 
       "Connection"     => (async ? "close" : "keep-alive")
      }

      header["Cookie"] = @cookie        if @cookie
      header.update(@http_header_extra) if @http_header_extra

      if @auth != nil
        # add authorization header
        header["Authorization"] = @auth
      end
 
      resp = nil
      @http_last_response = nil

      if async
        # use a new HTTP object for each call 
        Net::HTTP.version_1_2
        http = Net::HTTP.new(@host, @port, @proxy_host, @proxy_port) 
        http.use_ssl = @use_ssl if @use_ssl
        http.read_timeout = @timeout
        http.open_timeout = @timeout
        
        # post request
        http.start {
          resp = http.post2(@path, request, header) 
        }
      else
        # reuse the HTTP object for each call => connection alive is possible
        
        # post request
        resp = @http.post2(@path, request, header) 
      end
  
      @http_last_response = resp

      data = resp.body

      if resp.code == "401"
        # Authorization Required
        raise "Authorization failed.\nHTTP-Error: #{resp.code} #{resp.message}" 
      elsif resp.code[0,1] != "2"
        raise "HTTP-Error: #{resp.code} #{resp.message}" 
      end

      ct = parse_content_type(resp["Content-Type"]).first
      if ct != "text/xml"
        if ct == "text/html"
          raise "Wrong content-type: \n#{data}"
        else
          raise "Wrong content-type"
        end
      end
=begin Wordpress Workaround (hack)
      expected = resp["Content-Length"] || "<unknown>"
      if data.nil? or data.size == 0 
        raise "Wrong size. Was #{data.size}, should be #{expected}" 
      elsif expected.to_i != data.size and resp["Transfer-Encoding"].nil?
        raise "Wrong size. Was #{data.size}, should be #{expected}"
      end
=end
      c = resp["Set-Cookie"]
      @cookie = c if c

      return data
    end