1
2
3
4 import sys
5 import pygwt
6 from __pyjamas__ import JS
7 if sys.platform not in ['mozilla', 'ie6', 'opera', 'oldmoz', 'safari']:
8 from __pyjamas__ import get_main_frame
9 import pyjd
10
11 handlers = {}
12
14 - def __init__(self, htr, mode, user, pwd, url, postData=None, handler=None,
15 return_xml=False, content_type=None, headers = None):
17
19 return self.htr.asyncImpl(self.mode, self.user, self.pwd, self.url,
20 self.postData, self.handler, self.return_xml,
21 self.content_type, self.headers)
22
23
25 - def asyncGet(self, url, handler, returnxml=False,
26 content_type=None, headers=None, user=None, pwd=None):
27 postData = None
28 if not hasattr(handler, 'onCompletion'):
29 raise RuntimeError("Invalid call to asyncGet: handler is not a valid request handler")
30 self.asyncImpl('GET', user, pwd, url, postData, handler,
31 returnxml, content_type, headers)
32
33 - def asyncPost(self, url, postData, handler, returnxml=False,
34 content_type=None, headers=None, user=None, pwd=None):
35 if not hasattr(handler, 'onCompletion'):
36 raise RuntimeError("Invalid call to asyncPost: handler is not a valid request handler")
37 return self.asyncImpl('POST', user, pwd, url, postData, handler,
38 returnxml, content_type, headers)
39
40 - def asyncDelete(self, url, handler, returnxml=False,
41 content_type=None, headers=None, user=None, pwd=None):
42 postData = None
43 if not hasattr(handler, 'onCompletion'):
44 raise RuntimeError("Invalid call to asyncDelete: handler is not a valid request handler")
45 return self.asyncImpl('DELETE', user, pwd, url, postData, handler,
46 returnxml, content_type, headers)
47
48 - def asyncPut(self, url, postData, handler, returnxml=False,
49 content_type=None, headers=None, user=None, pwd=None):
50 if not hasattr(handler, 'onCompletion'):
51 raise RuntimeError("Invalid call to asyncPut: handler is not a valid request handler")
52 return self.asyncImpl('PUT', user, pwd, url, postData, handler,
53 returnxml, content_type, headers)
54
57
59 return get_main_frame().getXmlHttpRequest()
60
61 - def onLoad(self, sender, event, ignorearg):
62 xmlHttp = event.target
63 localHandler = handlers.get(xmlHttp)
64 del handlers[xmlHttp]
65 responseText = xmlHttp.responseText
66 status = xmlHttp.status
67 handler = None
68 xmlHttp = None
69
70 if status == 0:
71 print "HACK ALERT! webkit wrapper returns 0 not 200!"
72 if status == 200 or status == 0:
73 localHandler.onCompletion(responseText)
74 else :
75 localHandler.onError(responseText, status)
76
78 try:
79 xmlHttp = get_main_frame().gobject_wrap(xmlHttp)
80 except:
81 pass
82 print xmlHttp.readyState
83 if xmlHttp.readyState != 4:
84 return
85
86 localHandler = handlers.get(xmlHttp)
87 del handlers[xmlHttp]
88 responseText = xmlHttp.responseText
89 print "headers", xmlHttp.getAllResponseHeaders()
90 status = xmlHttp.status
91 handler = None
92 xmlHttp = None
93 print "status", status
94 print "local handler", localHandler
95
96 if status == 0:
97 print "HACK ALERT! webkit wrapper returns 0 not 200!"
98 if status == 200 or status == 0:
99 localHandler.onCompletion(responseText)
100 else :
101 localHandler.onError(responseText, status)
102
104
105 uri = pygwt.getModuleBaseURL()
106 if url[0] == '/':
107
108 sep = uri.find('://')
109 if not uri.startswith('file://'):
110
111 slash = uri.find('/', sep+3)
112 if slash > 0:
113 uri = uri[:slash]
114
115 return "%s%s" % (uri, url)
116
117 else:
118 if url[:7] != 'file://' and url[:7] != 'http://' and \
119 url[:8] != 'https://':
120 slash = uri.rfind('/')
121 return uri[:slash+1] + url
122
123 return url
124
125 - def asyncImpl(self, method, user, pwd, url, postData, handler,
126 returnxml=False, content_type=None, headers=None):
127 if headers is None:
128 headers = {}
129 if user and pwd and not "Authorization" in headers:
130 import base64
131 headers["Authorization"] = 'Basic %s' % (base64.b64encode('%s:%s' % (user, pwd)))
132
133 if postData is not None and not "Content-Length" in headers:
134 headers["Content-Length"] = str(len(postData))
135 if content_type is not None:
136 headers["Content-Type"] = content_type
137 if not "Content-Type" in headers:
138 if returnxml:
139 headers["Content-Type"] = "application/xml; charset=utf-8"
140 else:
141 headers["Content-Type"] = "text/plain; charset=utf-8"
142
143
144
145
146
147 mf = get_main_frame()
148 xmlHttp = self.doCreateXmlHTTPRequest()
149 url = self._convertUrlToAbsolute(url)
150 print "xmlHttp", method, user, pwd, url, postData, handler, dir(xmlHttp)
151
152 if mf.platform == 'webkit' or mf.platform == 'mshtml':
153 xmlHttp.open(method, url, True, '', '')
154 else:
155
156
157 print url, xmlHttp.open(method, url)
158 for h in headers:
159 if isinstance(headers[h], str):
160 xmlHttp.setRequestHeader(h, headers[h])
161 else:
162 hval = ';'.join([str(i) for i in headers[h]])
163 xmlHttp.setRequestHeader(h, hval)
164
165
166
167
168
169
170 if mf.platform == 'webkit' or mf.platform == 'mshtml':
171 mf._addXMLHttpRequestEventListener(xmlHttp, "onreadystatechange",
172 self.onReadyStateChange)
173 else:
174 mf._addXMLHttpRequestEventListener(xmlHttp, "load",
175 self.onLoad)
176 handlers[xmlHttp] = handler
177 xmlHttp.send(postData)
178
179 return True
180
181
182
183 handler = None
184 xmlHttp = None
185 localHandler.onError(str(e))
186 return False
187