Welcome to the "trac"-ing site of soap4r!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

root/tags/RELEASE_1_0_1/lib/SOAPStreamHandler_without_http-access.rb

Revision 9, 4.7 kB (checked in by nakahiro, 8 years ago)

Removed useless 'debug print'.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to author date id revision
Line 
1 =begin
2 SOAP4R
3 Copyright (C) 2000 NAKAMURA Hiroshi.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 2 of the License, or (at your option) any later
8 version.
9
10 This program is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12 PRATICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc., 675 Mass
16 Ave, Cambridge, MA 02139, USA.
17 =end
18
19 require 'SOAP'
20 require 'uri'
21 require 'socket'
22 require 'timeout'
23
24
25 class SOAPStreamHandler
26   public
27
28   attr_reader :endPoint
29
30   def initialize( endPoint )
31     @endPoint = endPoint
32   end
33 end
34
35
36 class SOAPHTTPPostStreamHandler < SOAPStreamHandler
37   public
38  
39   MediaType = 'text/xml'
40
41   NofRetry = 10       # [times]
42   CallTimeout = 300   # [sec]
43   ReadTimeout = 300   # [sec]
44
45   def initialize( endPointUri, proxy = nil )
46     super( endPointUri )
47     @server = endPointUri
48     @proxy = proxy
49   end
50
51   def send( methodNamespace, methodName, soapString )
52     begin
53       s = sendPOST( methodNamespace, methodName, soapString )
54     rescue PostUnavailableError
55       begin
56         s = sendMPOST( methodNamespace, methodName, soapString )
57       rescue MPostUnavailableError
58         raise HTTPStreamError.new( $! )
59       end
60     end
61   end
62
63   private
64
65   def sendPOST( methodNamespace, methodName, soapString )
66     server = URIModule::URI.create( @server )
67
68     retryNo = NofRetry
69     begin
70       if @proxy
71         proxy = URIModule::URI.create( @proxy )
72         s = TCPSocket.new( proxy.host, proxy.port )
73       else
74         s = TCPSocket.new( server.host, server.port )
75       end
76     rescue
77       retryNo -= 1
78       if retryNo > 0
79         puts 'Retrying connection ...' if $DEBUG
80         retry
81       end
82       raise
83     end
84
85     if @proxy
86       absPath = @server if @proxy
87     else
88       absPath = server.path.dup
89       absPath << '?' << server.query if server.query
90     end
91     action = methodNamespace.dup << '#' << methodName
92
93     header = {}
94     begin
95       timeout( CallTimeout ) do
96           postData =<<EOS
97 POST #{ absPath } HTTP/1.0
98 Host: #{ server.host }
99 Connection: close
100 Content-Length: #{ soapString.size }
101 Content-Type: #{ MediaType }
102 SOAPAction: #{ action }
103
104 EOS
105         postData.gsub!( "\n", CRLF )
106
107         postData << soapString
108         s.write postData
109         puts postData if $DEBUG
110
111         raise HTTPStreamError.new( 'Unexpected EOF...' ) if s.eof
112
113         # Parse HTTP header
114         version = nil
115         status = nil
116         reason = nil
117         begin
118           line = s.gets.chop
119           puts line if $DEBUG
120           Regexp.new( '^HTTP/(1.\d)\s+(\d+)\s+(.*)$' ) =~ line
121           version = $1
122           status = $2
123           reason = $3
124
125           lastKey = nil
126           lastValue = nil
127           while !s.eof
128             line = s.gets.chop
129             if ( /^$/ =~ line )
130               header[ lastKey ] = lastValue if lastKey
131               break
132             elsif ( /^([^:]+)\s*:\s*(.*)$/ =~ line )
133               header[ lastKey ] = lastValue if lastKey
134               lastKey = $1.downcase
135               lastValue = $2
136             else
137               lastValue << "\n" << line
138             end
139           end
140         end while ( !s.eof and version == '1.1' and status == '100' )
141
142         if ( status == '405' )
143           # 405: Method Not Allowed
144           raise PostUnavailableError.new( "#{ status }: #{ reason }" )
145         elsif ( status != '200' )
146           raise HTTPStreamError.new( "#{ status }: #{ reason }" )
147         elsif ( !header.has_key?( 'content-type' ))
148           raise HTTPStreamError.new( 'Content-type not found.' )
149         elsif ( /^#{ MediaType }(?:;.*)?/ !~ header[ 'content-type' ] )
150 #          raise HTTPStreamError.new( 'Illegal content-type: ' << header[ 'content-type' ] )
151         end
152       end
153     rescue TimeoutError
154       raise HTTPStreamError.new( 'Call timeout' )
155     end
156
157     receiveString = ''
158     begin
159       timeout( ReadTimeout ) do
160         while !s.eof
161           receiveString << s.gets
162         end
163       end
164     rescue TimeoutError
165       raise HTTPStreamError.new( 'Read timeout' )
166     end
167
168     receiveString
169   end
170
171   def sendMPOST( methodNamespace, methodName, soapString )
172     raise NotImplementError.new()
173
174     s = nil
175     if (( status == '501' ) or ( status == '510' ))
176       # 501: Not Implemented
177       # 510: Not Extended
178       raise MPostUnavailableError.new( "Status: #{ status }, Reason-phrase: #{ reason }" )
179     elsif ( status != '200' )
180       raise HTTPStreamError.new( "#{ status }: #{ reason }" )
181     end
182   end
183
184   private
185
186   CRLF = "\r\n"
187 end
Note: See TracBrowser for help on using the browser.