| 71 | | class PositionalParametersAware(object): |
|---|
| 72 | | """ |
|---|
| 73 | | Utility class that restores positional parameters functionality that |
|---|
| 74 | | was found in 2.0.0-beta. |
|---|
| 75 | | |
|---|
| 76 | | Use case: |
|---|
| 77 | | |
|---|
| 78 | | from cherrypy.lib import cptools |
|---|
| 79 | | import cherrypy |
|---|
| 80 | | class Root(cptools.PositionalParametersAware): |
|---|
| 81 | | def something(self, name): |
|---|
| 82 | | return "hello, " + name |
|---|
| 83 | | something.exposed |
|---|
| 84 | | cherrypy.root = Root() |
|---|
| 85 | | cherrypy.server.start() |
|---|
| 86 | | |
|---|
| 87 | | Now, fetch http://localhost:8080/something/name_is_here |
|---|
| 88 | | """ |
|---|
| 89 | | def default( self, *args, **kwargs ): |
|---|
| 90 | | # remap parameters to fix positional parameters |
|---|
| 91 | | if len(args) == 0: |
|---|
| 92 | | args = ("index",) |
|---|
| 93 | | m = getattr(self, args[0], None) |
|---|
| 94 | | if m and getattr(m, "exposed", False): |
|---|
| 95 | | return getattr(self, args[0])(*args[1:], **kwargs) |
|---|
| 96 | | else: |
|---|
| 97 | | m = getattr(self, "index", None) |
|---|
| 98 | | if m and getattr(m, "exposed", False): |
|---|
| 99 | | try: |
|---|
| 100 | | return self.index(*args, **kwargs) |
|---|
| 101 | | except TypeError: |
|---|
| 102 | | pass |
|---|
| 103 | | raise cherrypy.NotFound() |
|---|
| 104 | | default.exposed = True |
|---|
| 105 | | |
|---|
| 106 | | |
|---|
| 107 | | weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] |
|---|
| 108 | | monthname = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
|---|
| 109 | | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
|---|
| 110 | | |
|---|
| 111 | | def HTTPDate(dt=None): |
|---|
| 112 | | """Return the given time.struct_time as a string in RFC 1123 format. |
|---|
| 113 | | |
|---|
| 114 | | If no arguments are provided, the current time (as determined by |
|---|
| 115 | | time.gmtime() is used). |
|---|
| 116 | | |
|---|
| 117 | | RFC 2616: "[Concerning RFC 1123, RFC 850, asctime date formats]... |
|---|
| 118 | | HTTP/1.1 clients and servers that parse the date value MUST |
|---|
| 119 | | accept all three formats (for compatibility with HTTP/1.0), |
|---|
| 120 | | though they MUST only generate the RFC 1123 format for |
|---|
| 121 | | representing HTTP-date values in header fields." |
|---|
| 122 | | |
|---|
| 123 | | RFC 1945 (HTTP/1.0) requires the same. |
|---|
| 124 | | |
|---|
| 125 | | """ |
|---|
| 126 | | |
|---|
| 127 | | if dt is None: |
|---|
| 128 | | dt = time.gmtime() |
|---|
| 129 | | |
|---|
| 130 | | year, month, day, hh, mm, ss, wd, y, z = dt |
|---|
| 131 | | # Is "%a, %d %b %Y %H:%M:%S GMT" better or worse? |
|---|
| 132 | | return ("%s, %02d %3s %4d %02d:%02d:%02d GMT" % |
|---|
| 133 | | (weekdayname[wd], day, monthname[month], year, hh, mm, ss)) |
|---|
| 134 | | |
|---|
| 135 | | |
|---|
| 136 | | def getRanges(content_length): |
|---|
| 137 | | """Return a list of (start, stop) indices from a Range header, or None. |
|---|
| 138 | | |
|---|
| 139 | | Each (start, stop) tuple will be composed of two ints, which are suitable |
|---|
| 140 | | for use in a slicing operation. That is, the header "Range: bytes=3-6", |
|---|
| 141 | | if applied against a Python string, is requesting resource[3:7]. This |
|---|
| 142 | | function will return the list [(3, 7)]. |
|---|
| 143 | | """ |
|---|
| 144 | | |
|---|
| 145 | | r = cherrypy.request.headerMap.get('Range') |
|---|
| 146 | | if not r: |
|---|
| 147 | | return None |
|---|
| 148 | | |
|---|
| 149 | | result = [] |
|---|
| 150 | | bytesunit, byteranges = r.split("=", 1) |
|---|
| 151 | | for brange in byteranges.split(","): |
|---|
| 152 | | start, stop = [x.strip() for x in brange.split("-", 1)] |
|---|
| 153 | | if start: |
|---|
| 154 | | if not stop: |
|---|
| 155 | | stop = content_length - 1 |
|---|
| 156 | | start, stop = map(int, (start, stop)) |
|---|
| 157 | | if start >= content_length: |
|---|
| 158 | | # From rfc 2616 sec 14.16: |
|---|
| 159 | | # "If the server receives a request (other than one |
|---|
| 160 | | # including an If-Range request-header field) with an |
|---|
| 161 | | # unsatisfiable Range request-header field (that is, |
|---|
| 162 | | # all of whose byte-range-spec values have a first-byte-pos |
|---|
| 163 | | # value greater than the current length of the selected |
|---|
| 164 | | # resource), it SHOULD return a response code of 416 |
|---|
| 165 | | # (Requested range not satisfiable)." |
|---|
| 166 | | continue |
|---|
| 167 | | if stop < start: |
|---|
| 168 | | # From rfc 2616 sec 14.16: |
|---|
| 169 | | # "If the server ignores a byte-range-spec because it |
|---|
| 170 | | # is syntactically invalid, the server SHOULD treat |
|---|
| 171 | | # the request as if the invalid Range header field |
|---|
| 172 | | # did not exist. (Normally, this means return a 200 |
|---|
| 173 | | # response containing the full entity)." |
|---|
| 174 | | return None |
|---|
| 175 | | result.append((start, stop + 1)) |
|---|
| 176 | | else: |
|---|
| 177 | | if not stop: |
|---|
| 178 | | # See rfc quote above. |
|---|
| 179 | | return None |
|---|
| 180 | | # Negative subscript (last N bytes) |
|---|
| 181 | | result.append((content_length - int(stop), content_length)) |
|---|
| 182 | | |
|---|
| 183 | | if result == []: |
|---|
| 184 | | cherrypy.response.headerMap['Content-Range'] = "bytes */%s" % content_length |
|---|
| 185 | | message = "Invalid Range (first-byte-pos greater than Content-Length)" |
|---|
| 186 | | raise cherrypy.HTTPError(416, message) |
|---|
| 187 | | |
|---|
| 188 | | return result |
|---|
| 189 | | |
|---|
| 190 | | |
|---|
| 191 | | class AcceptValue(object): |
|---|
| 192 | | """A value (with parameters) from an Accept-* request header.""" |
|---|
| 193 | | |
|---|
| 194 | | def __init__(self, value, params=None): |
|---|
| 195 | | self.value = value |
|---|
| 196 | | if params is None: |
|---|
| 197 | | params = {} |
|---|
| 198 | | self.params = params |
|---|
| 199 | | |
|---|
| 200 | | def qvalue(self): |
|---|
| 201 | | val = self.params.get("q", "1") |
|---|
| 202 | | if isinstance(val, AcceptValue): |
|---|
| 203 | | val = val.value |
|---|
| 204 | | return float(val) |
|---|
| 205 | | qvalue = property(qvalue, doc="The qvalue, or priority, of this value.") |
|---|
| 206 | | |
|---|
| 207 | | def __str__(self): |
|---|
| 208 | | p = [";%s=%s" % (k, v) for k, v in self.params.iteritems()] |
|---|
| 209 | | return "%s%s" % (self.value, "".join(p)) |
|---|
| 210 | | |
|---|
| 211 | | def __cmp__(self, other): |
|---|
| 212 | | # If you sort a list of AcceptValue objects, they will be listed in |
|---|
| 213 | | # priority order; that is, the most preferred value will be first. |
|---|
| 214 | | diff = cmp(other.qvalue, self.qvalue) |
|---|
| 215 | | if diff == 0: |
|---|
| 216 | | diff = cmp(str(other), str(self)) |
|---|
| 217 | | return diff |
|---|
| 218 | | |
|---|
| 219 | | |
|---|
| 220 | | def getAccept(headername='Accept'): |
|---|
| 221 | | """Return a list of AcceptValues from an Accept header, or None.""" |
|---|
| 222 | | |
|---|
| 223 | | r = cherrypy.request.headerMap.get(headername) |
|---|
| 224 | | if not r: |
|---|
| 225 | | return None |
|---|
| 226 | | |
|---|
| 227 | | result = [] |
|---|
| 228 | | for capability in r.split(","): |
|---|
| 229 | | # The first "q" parameter (if any) separates the initial |
|---|
| 230 | | # parameter(s) (if any) from the accept-params. |
|---|
| 231 | | atoms = re.split(r'; *q *=', capability, 1) |
|---|
| 232 | | capvalue = atoms.pop(0).strip() |
|---|
| 233 | | if atoms: |
|---|
| 234 | | qvalue = atoms[0].strip() |
|---|
| 235 | | if headername == 'Accept': |
|---|
| 236 | | # The qvalue for an Accept header can have extensions. |
|---|
| 237 | | atoms = [x.strip() for x in qvalue.split(";")] |
|---|
| 238 | | qvalue = atoms.pop(0).strip() |
|---|
| 239 | | ext = {} |
|---|
| 240 | | for atom in atoms: |
|---|
| 241 | | atom = atom.split("=", 1) |
|---|
| 242 | | key = atom.pop(0).strip() |
|---|
| 243 | | if atom: |
|---|
| 244 | | val = atom[0].strip() |
|---|
| 245 | | else: |
|---|
| 246 | | val = "" |
|---|
| 247 | | ext[key] = val |
|---|
| 248 | | qvalue = AcceptValue(qvalue, ext) |
|---|
| 249 | | params = {"q": qvalue} |
|---|
| 250 | | else: |
|---|
| 251 | | params = {} |
|---|
| 252 | | |
|---|
| 253 | | if headername == 'Accept': |
|---|
| 254 | | # The media-range may have parameters (before the qvalue). |
|---|
| 255 | | atoms = [x.strip() for x in capvalue.split(";")] |
|---|
| 256 | | capvalue = atoms.pop(0).strip() |
|---|
| 257 | | for atom in atoms: |
|---|
| 258 | | atom = atom.split("=", 1) |
|---|
| 259 | | key = atom.pop(0).strip() |
|---|
| 260 | | if atom: |
|---|
| 261 | | val = atom[0].strip() |
|---|
| 262 | | else: |
|---|
| 263 | | val = "" |
|---|
| 264 | | params[key] = val |
|---|
| 265 | | |
|---|
| 266 | | result.append(AcceptValue(capvalue, params)) |
|---|
| 267 | | |
|---|
| 268 | | result.sort() |
|---|
| 269 | | return result |
|---|
| 270 | | |
|---|
| 271 | | |
|---|
| 387 | | |
|---|
| 388 | | def validStatus(status): |
|---|
| 389 | | """Return legal HTTP status Code, Reason-phrase and Message. |
|---|
| 390 | | |
|---|
| 391 | | The status arg must be an int, or a str that begins with an int. |
|---|
| 392 | | |
|---|
| 393 | | If status is an int, or a str and no reason-phrase is supplied, |
|---|
| 394 | | a default reason-phrase will be provided. |
|---|
| 395 | | """ |
|---|
| 396 | | |
|---|
| 397 | | if not status: |
|---|
| 398 | | status = 200 |
|---|
| 399 | | |
|---|
| 400 | | status = str(status) |
|---|
| 401 | | parts = status.split(" ", 1) |
|---|
| 402 | | if len(parts) == 1: |
|---|
| 403 | | # No reason supplied. |
|---|
| 404 | | code, = parts |
|---|
| 405 | | reason = None |
|---|
| 406 | | else: |
|---|
| 407 | | code, reason = parts |
|---|
| 408 | | reason = reason.strip() |
|---|
| 409 | | |
|---|
| 410 | | try: |
|---|
| 411 | | code = int(code) |
|---|
| 412 | | except ValueError: |
|---|
| 413 | | raise cherrypy.HTTPError(500, "Illegal response status from server (non-numeric).") |
|---|
| 414 | | |
|---|
| 415 | | if code < 100 or code > 599: |
|---|
| 416 | | raise cherrypy.HTTPError(500, "Illegal response status from server (out of range).") |
|---|
| 417 | | |
|---|
| 418 | | if code not in responseCodes: |
|---|
| 419 | | # code is unknown but not illegal |
|---|
| 420 | | defaultReason, message = "", "" |
|---|
| 421 | | else: |
|---|
| 422 | | defaultReason, message = responseCodes[code] |
|---|
| 423 | | |
|---|
| 424 | | if reason is None: |
|---|
| 425 | | reason = defaultReason |
|---|
| 426 | | |
|---|
| 427 | | return code, reason, message |
|---|
| 428 | | |
|---|
| 429 | | def parseRequestLine(requestLine): |
|---|
| 430 | | """Return (method, path, querystring, protocol) from a requestLine.""" |
|---|
| 431 | | method, path, protocol = requestLine.split() |
|---|
| 432 | | |
|---|
| 433 | | # path may be an abs_path (including "http://host.domain.tld"); |
|---|
| 434 | | # Ignore scheme, location, and fragments (so config lookups work). |
|---|
| 435 | | # [Therefore, this assumes all hosts are valid for this server.] |
|---|
| 436 | | scheme, location, path, params, qs, frag = urlparse(path) |
|---|
| 437 | | if path == "*": |
|---|
| 438 | | # "...the request does not apply to a particular resource, |
|---|
| 439 | | # but to the server itself". See |
|---|
| 440 | | # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2 |
|---|
| 441 | | pass |
|---|
| 442 | | else: |
|---|
| 443 | | if params: |
|---|
| 444 | | params = ";" + params |
|---|
| 445 | | path = path + params |
|---|
| 446 | | |
|---|
| 447 | | # Unquote the path (e.g. "/this%20path" -> "this path"). |
|---|
| 448 | | # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2 |
|---|
| 449 | | # Note that cgi.parse_qs will decode the querystring for us. |
|---|
| 450 | | path = urllib.unquote(path) |
|---|
| 451 | | |
|---|
| 452 | | return method, path, qs, protocol |
|---|
| 453 | | |
|---|
| 454 | | def parseQueryString(queryString, keep_blank_values=True): |
|---|
| 455 | | """Build a paramMap dictionary from a queryString.""" |
|---|
| 456 | | if re.match(r"[0-9]+,[0-9]+", queryString): |
|---|
| 457 | | # Server-side image map. Map the coords to 'x' and 'y' |
|---|
| 458 | | # (like CGI::Request does). |
|---|
| 459 | | pm = queryString.split(",") |
|---|
| 460 | | pm = {'x': int(pm[0]), 'y': int(pm[1])} |
|---|
| 461 | | else: |
|---|
| 462 | | pm = cgi.parse_qs(queryString, keep_blank_values) |
|---|
| 463 | | for key, val in pm.items(): |
|---|
| 464 | | if len(val) == 1: |
|---|
| 465 | | pm[key] = val[0] |
|---|
| 466 | | return pm |
|---|
| 467 | | |
|---|
| 468 | | def paramsFromCGIForm(form): |
|---|
| 469 | | paramMap = {} |
|---|
| 470 | | for key in form.keys(): |
|---|
| 471 | | valueList = form[key] |
|---|
| 472 | | if isinstance(valueList, list): |
|---|
| 473 | | paramMap[key] = [] |
|---|
| 474 | | for item in valueList: |
|---|
| 475 | | if item.filename is not None: |
|---|
| 476 | | value = item # It's a file upload |
|---|
| 477 | | else: |
|---|
| 478 | | value = item.value # It's a regular field |
|---|
| 479 | | paramMap[key].append(value) |
|---|
| 480 | | else: |
|---|
| 481 | | if valueList.filename is not None: |
|---|
| 482 | | value = valueList # It's a file upload |
|---|
| 483 | | else: |
|---|
| 484 | | value = valueList.value # It's a regular field |
|---|
| 485 | | paramMap[key] = value |
|---|
| 486 | | return paramMap |
|---|
| 487 | | |
|---|
| 488 | | |
|---|
| 489 | | class HeaderMap(dict): |
|---|
| 490 | | """A dict subclass for HTTP request and response headers. |
|---|
| 491 | | |
|---|
| 492 | | Each key is changed on entry to str(key).title(). This allows headers |
|---|
| 493 | | to be case-insensitive and avoid duplicates. |
|---|
| 494 | | """ |
|---|
| 495 | | |
|---|
| 496 | | def __getitem__(self, key): |
|---|
| 497 | | return dict.__getitem__(self, str(key).title()) |
|---|
| 498 | | |
|---|
| 499 | | def __setitem__(self, key, value): |
|---|
| 500 | | dict.__setitem__(self, str(key).title(), value) |
|---|
| 501 | | |
|---|
| 502 | | def __delitem__(self, key): |
|---|
| 503 | | dict.__delitem__(self, str(key).title()) |
|---|
| 504 | | |
|---|
| 505 | | def __contains__(self, item): |
|---|
| 506 | | return dict.__contains__(self, str(item).title()) |
|---|
| 507 | | |
|---|
| 508 | | def get(self, key, default=None): |
|---|
| 509 | | return dict.get(self, str(key).title(), default) |
|---|
| 510 | | |
|---|
| 511 | | def has_key(self, key): |
|---|
| 512 | | return dict.has_key(self, str(key).title()) |
|---|
| 513 | | |
|---|
| 514 | | def update(self, E): |
|---|
| 515 | | for k in E.keys(): |
|---|
| 516 | | self[str(k).title()] = E[k] |
|---|
| 517 | | |
|---|
| 518 | | def fromkeys(cls, seq, value=None): |
|---|
| 519 | | newdict = cls() |
|---|
| 520 | | for k in seq: |
|---|
| 521 | | newdict[str(k).title()] = value |
|---|
| 522 | | return newdict |
|---|
| 523 | | fromkeys = classmethod(fromkeys) |
|---|
| 524 | | |
|---|
| 525 | | def setdefault(self, key, x=None): |
|---|
| 526 | | key = str(key).title() |
|---|
| 527 | | try: |
|---|
| 528 | | return self[key] |
|---|
| 529 | | except KeyError: |
|---|
| 530 | | self[key] = x |
|---|
| 531 | | return x |
|---|
| 532 | | |
|---|
| 533 | | def pop(self, key, default): |
|---|
| 534 | | return dict.pop(self, str(key).title(), default) |
|---|