]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/common.py
web: don't use %d formatting on a potentially large negative float -- there is a...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / common.py
1
2 import simplejson
3 from twisted.web import http, server
4 from twisted.python import log
5 from zope.interface import Interface
6 from nevow import loaders, appserver
7 from nevow.inevow import IRequest
8 from nevow.util import resource_filename
9 from allmydata.interfaces import ExistingChildError, NoSuchChildError, \
10      FileTooLargeError, NotEnoughSharesError, NoSharesError, \
11      EmptyPathnameComponentError, MustBeDeepImmutableError, \
12      MustBeReadonlyError, MustNotBeUnknownRWError
13 from allmydata.mutable.common import UnrecoverableFileError
14 from allmydata.util import abbreviate
15 from allmydata.util.encodingutil import to_str
16
17 class IOpHandleTable(Interface):
18     pass
19
20 def getxmlfile(name):
21     return loaders.xmlfile(resource_filename('allmydata.web', '%s' % name))
22
23 def boolean_of_arg(arg):
24     # TODO: ""
25     assert arg.lower() in ("true", "t", "1", "false", "f", "0", "on", "off")
26     return arg.lower() in ("true", "t", "1", "on")
27
28 def parse_replace_arg(replace):
29     if replace.lower() == "only-files":
30         return replace
31     else:
32         return boolean_of_arg(replace)
33
34 def get_root(ctx_or_req):
35     req = IRequest(ctx_or_req)
36     # the addSlash=True gives us one extra (empty) segment
37     depth = len(req.prepath) + len(req.postpath) - 1
38     link = "/".join([".."] * depth)
39     return link
40
41 def get_arg(ctx_or_req, argname, default=None, multiple=False):
42     """Extract an argument from either the query args (req.args) or the form
43     body fields (req.fields). If multiple=False, this returns a single value
44     (or the default, which defaults to None), and the query args take
45     precedence. If multiple=True, this returns a tuple of arguments (possibly
46     empty), starting with all those in the query args.
47     """
48     req = IRequest(ctx_or_req)
49     results = []
50     if argname in req.args:
51         results.extend(req.args[argname])
52     if req.fields and argname in req.fields:
53         results.append(req.fields[argname].value)
54     if multiple:
55         return tuple(results)
56     if results:
57         return results[0]
58     return default
59
60 def convert_children_json(nodemaker, children_json):
61     """I convert the JSON output of GET?t=json into the dict-of-nodes input
62     to both dirnode.create_subdirectory() and
63     client.create_directory(initial_children=). This is used by
64     t=mkdir-with-children and t=mkdir-immutable"""
65     children = {}
66     if children_json:
67         data = simplejson.loads(children_json)
68         for (namex, (ctype, propdict)) in data.iteritems():
69             namex = unicode(namex)
70             writecap = to_str(propdict.get("rw_uri"))
71             readcap = to_str(propdict.get("ro_uri"))
72             metadata = propdict.get("metadata", {})
73             # name= argument is just for error reporting
74             childnode = nodemaker.create_from_cap(writecap, readcap, name=namex)
75             children[namex] = (childnode, metadata)
76     return children
77
78 def abbreviate_time(data):
79     # 1.23s, 790ms, 132us
80     if data is None:
81         return ""
82     s = float(data)
83     if s >= 10:
84         return abbreviate.abbreviate_time(data)
85     if s >= 1.0:
86         return "%.2fs" % s
87     if s >= 0.01:
88         return "%.0fms" % (1000*s)
89     if s >= 0.001:
90         return "%.1fms" % (1000*s)
91     return "%.0fus" % (1000000*s)
92
93 def abbreviate_rate(data):
94     # 21.8kBps, 554.4kBps 4.37MBps
95     if data is None:
96         return ""
97     r = float(data)
98     if r > 1000000:
99         return "%1.2fMBps" % (r/1000000)
100     if r > 1000:
101         return "%.1fkBps" % (r/1000)
102     return "%.0fBps" % r
103
104 def abbreviate_size(data):
105     # 21.8kB, 554.4kB 4.37MB
106     if data is None:
107         return ""
108     r = float(data)
109     if r > 1000000000:
110         return "%1.2fGB" % (r/1000000000)
111     if r > 1000000:
112         return "%1.2fMB" % (r/1000000)
113     if r > 1000:
114         return "%.1fkB" % (r/1000)
115     return "%.0fB" % r
116
117 def plural(sequence_or_length):
118     if isinstance(sequence_or_length, int):
119         length = sequence_or_length
120     else:
121         length = len(sequence_or_length)
122     if length == 1:
123         return ""
124     return "s"
125
126 def text_plain(text, ctx):
127     req = IRequest(ctx)
128     req.setHeader("content-type", "text/plain")
129     req.setHeader("content-length", len(text))
130     return text
131
132 class WebError(Exception):
133     def __init__(self, text, code=http.BAD_REQUEST):
134         self.text = text
135         self.code = code
136
137 # XXX: to make UnsupportedMethod return 501 NOT_IMPLEMENTED instead of 500
138 # Internal Server Error, we either need to do that ICanHandleException trick,
139 # or make sure that childFactory returns a WebErrorResource (and never an
140 # actual exception). The latter is growing increasingly annoying.
141
142 def should_create_intermediate_directories(req):
143     t = get_arg(req, "t", "").strip()
144     return bool(req.method in ("PUT", "POST") and
145                 t not in ("delete", "rename", "rename-form", "check"))
146
147 def humanize_failure(f):
148     # return text, responsecode
149     if f.check(EmptyPathnameComponentError):
150         return ("The webapi does not allow empty pathname components, "
151                 "i.e. a double slash", http.BAD_REQUEST)
152     if f.check(ExistingChildError):
153         return ("There was already a child by that name, and you asked me "
154                 "to not replace it.", http.CONFLICT)
155     if f.check(NoSuchChildError):
156         name = f.value.args[0]
157         return ("No such child: %s" % name.encode("utf-8"), http.NOT_FOUND)
158     if f.check(NotEnoughSharesError):
159         t = ("NotEnoughSharesError: This indicates that some "
160              "servers were unavailable, or that shares have been "
161              "lost to server departure, hard drive failure, or disk "
162              "corruption. You should perform a filecheck on "
163              "this object to learn more.\n\nThe full error message is:\n"
164              "%s") % str(f.value)
165         return (t, http.GONE)
166     if f.check(NoSharesError):
167         t = ("NoSharesError: no shares could be found. "
168              "Zero shares usually indicates a corrupt URI, or that "
169              "no servers were connected, but it might also indicate "
170              "severe corruption. You should perform a filecheck on "
171              "this object to learn more.\n\nThe full error message is:\n"
172              "%s") % str(f.value)
173         return (t, http.GONE)
174     if f.check(UnrecoverableFileError):
175         t = ("UnrecoverableFileError: the directory (or mutable file) could "
176              "not be retrieved, because there were insufficient good shares. "
177              "This might indicate that no servers were connected, "
178              "insufficient servers were connected, the URI was corrupt, or "
179              "that shares have been lost due to server departure, hard drive "
180              "failure, or disk corruption. You should perform a filecheck on "
181              "this object to learn more.")
182         return (t, http.GONE)
183     if f.check(MustNotBeUnknownRWError):
184         name = f.value.args[1]
185         immutable = f.value.args[2]
186         if immutable:
187             t = ("MustNotBeUnknownRWError: an operation to add a child named "
188                  "'%s' to a directory was given an unknown cap in a write slot.\n"
189                  "If the cap is actually an immutable readcap, then using a "
190                  "webapi server that supports a later version of Tahoe may help.\n\n"
191                  "If you are using the webapi directly, then specifying an immutable "
192                  "readcap in the read slot (ro_uri) of the JSON PROPDICT, and "
193                  "omitting the write slot (rw_uri), would also work in this "
194                  "case.") % name.encode("utf-8")
195         else:
196             t = ("MustNotBeUnknownRWError: an operation to add a child named "
197                  "'%s' to a directory was given an unknown cap in a write slot.\n"
198                  "Using a webapi server that supports a later version of Tahoe "
199                  "may help.\n\n"
200                  "If you are using the webapi directly, specifying a readcap in "
201                  "the read slot (ro_uri) of the JSON PROPDICT, as well as a "
202                  "writecap in the write slot if desired, would also work in this "
203                  "case.") % name.encode("utf-8")
204         return (t, http.BAD_REQUEST)
205     if f.check(MustBeDeepImmutableError):
206         name = f.value.args[1]
207         t = ("MustBeDeepImmutableError: a cap passed to this operation for "
208              "the child named '%s', needed to be immutable but was not. Either "
209              "the cap is being added to an immutable directory, or it was "
210              "originally retrieved from an immutable directory as an unknown "
211              "cap." % name.encode("utf-8"))
212         return (t, http.BAD_REQUEST)
213     if f.check(MustBeReadonlyError):
214         name = f.value.args[1]
215         t = ("MustBeReadonlyError: a cap passed to this operation for "
216              "the child named '%s', needed to be read-only but was not. "
217              "The cap is being passed in a read slot (ro_uri), or was retrieved "
218              "from a read slot as an unknown cap." % name.encode("utf-8"))
219         return (t, http.BAD_REQUEST)
220     if f.check(WebError):
221         return (f.value.text, f.value.code)
222     if f.check(FileTooLargeError):
223         return (f.getTraceback(), http.REQUEST_ENTITY_TOO_LARGE)
224     return (str(f), None)
225
226 class MyExceptionHandler(appserver.DefaultExceptionHandler):
227     def simple(self, ctx, text, code=http.BAD_REQUEST):
228         req = IRequest(ctx)
229         req.setResponseCode(code)
230         #req.responseHeaders.setRawHeaders("content-encoding", [])
231         #req.responseHeaders.setRawHeaders("content-disposition", [])
232         req.setHeader("content-type", "text/plain;charset=utf-8")
233         if isinstance(text, unicode):
234             text = text.encode("utf-8")
235         req.setHeader("content-length", str(len(text)))
236         req.write(text)
237         # TODO: consider putting the requested URL here
238         req.finishRequest(False)
239
240     def renderHTTP_exception(self, ctx, f):
241         try:
242             text, code = humanize_failure(f)
243         except:
244             log.msg("exception in humanize_failure")
245             log.msg("argument was %s" % (f,))
246             log.err()
247             text, code = str(f), None
248         if code is not None:
249             return self.simple(ctx, text, code)
250         if f.check(server.UnsupportedMethod):
251             # twisted.web.server.Request.render() has support for transforming
252             # this into an appropriate 501 NOT_IMPLEMENTED or 405 NOT_ALLOWED
253             # return code, but nevow does not.
254             req = IRequest(ctx)
255             method = req.method
256             return self.simple(ctx,
257                                "I don't know how to treat a %s request." % method,
258                                http.NOT_IMPLEMENTED)
259         req = IRequest(ctx)
260         accept = req.getHeader("accept")
261         if not accept:
262             accept = "*/*"
263         if "*/*" in accept or "text/*" in accept or "text/html" in accept:
264             super = appserver.DefaultExceptionHandler
265             return super.renderHTTP_exception(self, ctx, f)
266         # use plain text
267         traceback = f.getTraceback()
268         return self.simple(ctx, traceback, http.INTERNAL_SERVER_ERROR)
269
270 class NeedOperationHandleError(WebError):
271     pass
272
273 class RenderMixin:
274
275     def renderHTTP(self, ctx):
276         request = IRequest(ctx)
277
278         # if we were using regular twisted.web Resources (and the regular
279         # twisted.web.server.Request object) then we could implement
280         # render_PUT and render_GET. But Nevow's request handler
281         # (NevowRequest.gotPageContext) goes directly to renderHTTP. Copy
282         # some code from the Resource.render method that Nevow bypasses, to
283         # do the same thing.
284         m = getattr(self, 'render_' + request.method, None)
285         if not m:
286             from twisted.web.server import UnsupportedMethod
287             raise UnsupportedMethod(getattr(self, 'allowedMethods', ()))
288         return m(ctx)