source file: /home/buildslave/tahoe/edgy/build/src/allmydata/web/common.py
file stats: 154 lines, 148 executed: 96.1% covered
coverage versus previous test: 0 lines added, 0 lines removed
1.
2. from twisted.web import http, server
3. from twisted.python import log
4. from zope.interface import Interface
5. from nevow import loaders, appserver
6. from nevow.inevow import IRequest
7. from nevow.util import resource_filename
8. from allmydata.interfaces import ExistingChildError, NoSuchChildError, \
9. FileTooLargeError, NotEnoughSharesError, NoSharesError
10. from allmydata.mutable.common import UnrecoverableFileError
11. from allmydata.util import abbreviate # TODO: consolidate
12.
13. class IOpHandleTable(Interface):
14. pass
15.
16. def getxmlfile(name):
17. return loaders.xmlfile(resource_filename('allmydata.web', '%s' % name))
18.
19. def boolean_of_arg(arg):
20. # TODO: ""
21. assert arg.lower() in ("true", "t", "1", "false", "f", "0", "on", "off")
22. return arg.lower() in ("true", "t", "1", "on")
23.
24. def parse_replace_arg(replace):
25. if replace.lower() == "only-files":
26. return replace
27. else:
28. return boolean_of_arg(replace)
29.
30. def get_root(ctx_or_req):
31. req = IRequest(ctx_or_req)
32. # the addSlash=True gives us one extra (empty) segment
33. depth = len(req.prepath) + len(req.postpath) - 1
34. link = "/".join([".."] * depth)
35. return link
36.
37. def get_arg(ctx_or_req, argname, default=None, multiple=False):
38. """Extract an argument from either the query args (req.args) or the form
39. body fields (req.fields). If multiple=False, this returns a single value
40. (or the default, which defaults to None), and the query args take
41. precedence. If multiple=True, this returns a tuple of arguments (possibly
42. empty), starting with all those in the query args.
43. """
44. req = IRequest(ctx_or_req)
45. results = []
46. if argname in req.args:
47. results.extend(req.args[argname])
48. if req.fields and argname in req.fields:
49. results.append(req.fields[argname].value)
50. if multiple:
51. return tuple(results)
52. if results:
53. return results[0]
54. return default
55.
56. def abbreviate_time(data):
57. # 1.23s, 790ms, 132us
58. if data is None:
59. return ""
60. s = float(data)
61. if s >= 10:
62. return abbreviate.abbreviate_time(data)
63. if s >= 1.0:
64. return "%.2fs" % s
65. if s >= 0.01:
66. return "%dms" % (1000*s)
67. if s >= 0.001:
68. return "%.1fms" % (1000*s)
69. return "%dus" % (1000000*s)
70.
71. def abbreviate_rate(data):
72. # 21.8kBps, 554.4kBps 4.37MBps
73. if data is None:
74. return ""
75. r = float(data)
76. if r > 1000000:
77. return "%1.2fMBps" % (r/1000000)
78. if r > 1000:
79. return "%.1fkBps" % (r/1000)
80. return "%dBps" % r
81.
82. def abbreviate_size(data):
83. # 21.8kB, 554.4kB 4.37MB
84. if data is None:
85. return ""
86. r = float(data)
87. if r > 1000000000:
88. return "%1.2fGB" % (r/1000000000)
89. if r > 1000000:
90. return "%1.2fMB" % (r/1000000)
91. if r > 1000:
92. return "%.1fkB" % (r/1000)
93. return "%dB" % r
94.
95. def plural(sequence_or_length):
96. if isinstance(sequence_or_length, int):
97. length = sequence_or_length
98. else:
99. length = len(sequence_or_length)
100. if length == 1:
101. return ""
102. return "s"
103.
104. def text_plain(text, ctx):
105. req = IRequest(ctx)
106. req.setHeader("content-type", "text/plain")
107. req.setHeader("content-length", len(text))
108. return text
109.
110. class WebError(Exception):
111. def __init__(self, text, code=http.BAD_REQUEST):
112. self.text = text
113. self.code = code
114.
115. # XXX: to make UnsupportedMethod return 501 NOT_IMPLEMENTED instead of 500
116. # Internal Server Error, we either need to do that ICanHandleException trick,
117. # or make sure that childFactory returns a WebErrorResource (and never an
118. # actual exception). The latter is growing increasingly annoying.
119.
120. def should_create_intermediate_directories(req):
121. t = get_arg(req, "t", "").strip()
122. return bool(req.method in ("PUT", "POST") and
123. t not in ("delete", "rename", "rename-form", "check"))
124.
125. def humanize_failure(f):
126. # return text, responsecode
127. if f.check(ExistingChildError):
128. return ("There was already a child by that name, and you asked me "
129. "to not replace it.", http.CONFLICT)
130. if f.check(NoSuchChildError):
131. name = f.value.args[0]
132. return ("No such child: %s" % name.encode("utf-8"), http.NOT_FOUND)
133. if f.check(NotEnoughSharesError):
134. t = ("NotEnoughSharesError: This indicates that some "
135. "servers were unavailable, or that shares have been "
136. "lost to server departure, hard drive failure, or disk "
137. "corruption. You should perform a filecheck on "
138. "this object to learn more.\n\nThe full error message is:\n"
139. "%s") % str(f.value)
140. return (t, http.GONE)
141. if f.check(NoSharesError):
142. t = ("NoSharesError: no shares could be found. "
143. "Zero shares usually indicates a corrupt URI, or that "
144. "no servers were connected, but it might also indicate "
145. "severe corruption. You should perform a filecheck on "
146. "this object to learn more.\n\nThe full error message is:\n"
147. "%s") % str(f.value)
148. return (t, http.GONE)
149. if f.check(UnrecoverableFileError):
150. t = ("UnrecoverableFileError: the directory (or mutable file) could "
151. "not be retrieved, because there were insufficient good shares. "
152. "This might indicate that no servers were connected, "
153. "insufficient servers were connected, the URI was corrupt, or "
154. "that shares have been lost due to server departure, hard drive "
155. "failure, or disk corruption. You should perform a filecheck on "
156. "this object to learn more.")
157. return (t, http.GONE)
158. if f.check(WebError):
159. return (f.value.text, f.value.code)
160. if f.check(FileTooLargeError):
161. return (f.getTraceback(), http.REQUEST_ENTITY_TOO_LARGE)
162. return (str(f), None)
163.
164. class MyExceptionHandler(appserver.DefaultExceptionHandler):
165. def simple(self, ctx, text, code=http.BAD_REQUEST):
166. req = IRequest(ctx)
167. req.setResponseCode(code)
168. #req.responseHeaders.setRawHeaders("content-encoding", [])
169. #req.responseHeaders.setRawHeaders("content-disposition", [])
170. req.setHeader("content-type", "text/plain;charset=utf-8")
171. if isinstance(text, unicode):
172. text = text.encode("utf-8")
173. req.setHeader("content-length", str(len(text)))
174. req.write(text)
175. # TODO: consider putting the requested URL here
176. req.finishRequest(False)
177.
178. def renderHTTP_exception(self, ctx, f):
179. try:
180. text, code = humanize_failure(f)
181. except:
182. log.msg("exception in humanize_failure")
183. log.msg("argument was %s" % (f,))
184. log.err()
185. text, code = str(f), None
186. if code is not None:
187. return self.simple(ctx, text, code)
188. if f.check(server.UnsupportedMethod):
189. # twisted.web.server.Request.render() has support for transforming
190. # this into an appropriate 501 NOT_IMPLEMENTED or 405 NOT_ALLOWED
191. # return code, but nevow does not.
192. req = IRequest(ctx)
193. method = req.method
194. return self.simple(ctx,
195. "I don't know how to treat a %s request." % method,
196. http.NOT_IMPLEMENTED)
197. req = IRequest(ctx)
198. accept = req.getHeader("accept")
199. if not accept:
200. accept = "*/*"
201. if "*/*" in accept or "text/*" in accept or "text/html" in accept:
202. super = appserver.DefaultExceptionHandler
203. return super.renderHTTP_exception(self, ctx, f)
204. # use plain text
205. traceback = f.getTraceback()
206. return self.simple(ctx, traceback, http.INTERNAL_SERVER_ERROR)
207.
208. class NeedOperationHandleError(WebError):
209. pass
210.
211. class RenderMixin:
212.
213. def renderHTTP(self, ctx):
214. request = IRequest(ctx)
215.
216. # if we were using regular twisted.web Resources (and the regular
217. # twisted.web.server.Request object) then we could implement
218. # render_PUT and render_GET. But Nevow's request handler
219. # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy
220. # some code from the Resource.render method that Nevow bypasses, to
221. # do the same thing.
222. m = getattr(self, 'render_' + request.method, None)
223. if not m:
224. from twisted.web.server import UnsupportedMethod
225. raise UnsupportedMethod(getattr(self, 'allowedMethods', ()))
226. return m(ctx)