My favorites | Sign in
Logo
                
Repository:
Checkout | Browse | Changes | Clones |
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/ruby1.8 -w
#
# Copyright:: Copyright 2009 Google Inc.
# Original Author:: Ryan Brown (mailto:ribrdb@google.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'net/https'


module AppEngine
# The URLFetch Service provides a way for user code to execute HTTP requests
# to external URLs.
#
# Chunked and hanging requests are not supported, and all content will be
# returned in a single block.
#
# URLFetch::HTTP also provides a drop-in replacement for Net::HTTP.
# To replace the standard implementation throughout your app you can do:
# require 'appengine-apis/urlfetch'
# Net::HTTP = AppEngine::URLFetch::HTTP
module URLFetch
import com.google.appengine.api.urlfetch.FetchOptions
import com.google.appengine.api.urlfetch.HTTPHeader
import com.google.appengine.api.urlfetch.HTTPMethod
import com.google.appengine.api.urlfetch.HTTPRequest
import com.google.appengine.api.urlfetch.ResponseTooLargeException
import com.google.appengine.api.urlfetch.URLFetchServiceFactory

# Raised if the remote service could not be contacted
class DownloadError < StandardError; end

# Raised if the url cannot be parsed.
class InvalidURLError < StandardError; end

# Raised if the response is too large.
class ResponseTooLargeError < StandardError; end

module_function

# Fetches the given HTTP URL, blocking until the result is returned.
#
# Supported options:
# [:method] GET, POST, HEAD, PUT, or DELETE
# [:payload] POST or PUT payload (implies method is not GET, HEAD,
# or DELETE)
# [:headers]
# HTTP headers to send with the request. May be a Hash or
# Net::HTTPHeaders.
# [:allow_truncated]
# if true, truncate large responses and return them
# without error. otherwise, ResponseTooLargeError will be thrown when a
# response is truncated.
# [:follow_redirects]
# if true (the default), redirects are transparently followed and the
# response (if less than 5 redirects) contains the final destination's
# payload and the response status is 200. You lose, however, the
# redirect chaininformation. If false, you see the HTTP response
# yourself, including the 'Location' header, and redirects are not
# followed.
# [:deadline] Deadline, in seconds, for the request.
#
# Returns a Net::HTTPResponse.
#
# Throws:
# - InvalidURLError if the provided url is malformed.
# - DownloadError if the remote service could not be contacted or the URL
# could not be fetched.
# - ResponseTooLargeError if response truncation has been disabled and the
# response is too large. Some responses are too large to even retrieve
# from the remote server, and in these cases the exception is thrown even
# if response truncation is enabled.
#
def fetch(url, options={})
request = build_urlfetch_request(url, options)
begin
java_response = urlfetch_service.fetch(request)
return convert_urlfetch_body(java_response)
rescue java.lang.IllegalArgumentException => ex
raise ArgumentError, ex.message
rescue java.net.MalformedURLException => ex
raise InvalidURLError, ex.message
rescue java.io.IOException => ex
raise DownloadError, ex.message
rescue ResponseTooLargeException => ex
raise ResponseTooLargeError, ex.message
end
end

def build_urlfetch_request(url, options) # :nodoc:
method = options.delete(:method) || 'GET'
payload = options.delete(:payload)
headers = options.delete(:headers) || {}
truncate = options.delete(:allow_truncated)
follow_redirects = options.delete(:follow_redirects) || true
deadline = options.delete(:deadline)

unless options.empty?
raise ArgumentError, "Unsupported options #{options.inspect}."
end

begin
method = HTTPMethod.value_of(method.to_s.upcase)
rescue java.lang.IllegalArgumentException
raise ArgumentError, "Invalid method #{method.inspect}."
end

if truncate
options = FetchOptions::Builder.allow_truncate
else
options = FetchOptions::Builder.disallow_truncate
end
if follow_redirects
options.follow_redirects
else
options.do_not_follow_redirects
end

options.set_deadline(deadline) if deadline

url = java.net.URL.new(url) unless url.java_kind_of? java.net.URL
request = HTTPRequest.new(url, method, options)

iterator = if headers.respond_to?(:canonical_each)
:canonical_each
else
:each
end

headers.send(iterator) do |name, value|
request.set_header(HTTPHeader.new(name, value))
end

if payload
request.set_payload(payload.to_java_bytes)
end

return request
end

def convert_urlfetch_body(java_response) # :nodoc:
status = java_response.response_code.to_s
klass = Net::HTTPResponse.send(:response_class, status)
mesg = klass.name
mesg = mesg[4, mesg.size]
response = klass.new(nil, status, mesg)
java_response.headers.each do |header|
response.add_field(header.name, header.value)
end
body = if java_response.content
String.from_java_bytes(java_response.content)
else
nil
end
response.urlfetch_body = body
return response
end

def urlfetch_service # :nodoc:
@service ||= URLFetchServiceFactory.getURLFetchService
end

def urlfetch_service=(service) # :nodoc:
@service = service
end


# Deprecated alias for Net::HTTP, for backwards compatibility.
HTTP = Net::HTTP
end
end

module Net # :nodoc:
# Monkey patch Net::HTTP to makes requests using Google App Engine's
# URLFetch Service.
class HTTP
alias connect on_connect

def request(req, body=nil, &block)
begin
proto = use_ssl? ? 'https' : 'http'
url = "#{proto}://#{addr_port}#{req.path}"
options = {
:payload => body,
:follow_redirects => false,
:allow_truncated => true,
:method => req.method,
:headers => req
}
res = AppEngine::URLFetch.fetch(url, options)
end while res.kind_of?(Net::HTTPContinue)
res.reading_body(nil, req.response_body_permitted?) {
yield res if block_given?
}
return res
end
end

class HTTPResponse # :nodoc:
alias stream_check_without_urlfetch stream_check

def stream_check
return if @urlfetch_body
stream_check_without_urlfetch
end

alias read_body_0_without_urlfetch read_body_0

def read_body_0(dest)
if @urlfetch_body
dest << @urlfetch_body
return
else
read_body_0_without_urlfetch(dest)
end
end

def urlfetch_body=(body)
@body_exist = body && self.class.body_permitted?
@urlfetch_body = body || ''
end
end
end
Show details Hide details

Change log

c141a1fe3f by Ryan Brown <rib...@google.com> on Aug 24, 2009   Diff
Add support for URLFetch deadlines
Go to: 
Project members, sign in to write a code review

Older revisions

0b814070ed by ribrdb on Jul 25, 2009   Diff
Monkey patch urlfetch by default
1e1374848b by ribrdb on May 29, 2009   Diff
Fix some typos.
f90412b1e8 by ribrdb on Apr 18, 2009   Diff
Update doc formatting. Use sdoc
template for generating prettier docs
All revisions of this file

File info

Size: 7674 bytes, 234 lines