Exemple #1
0
 def testEscchar(self):
     try:
         from twisted.protocols import _c_urlarg
     except ImportError:
         raise unittest.SkipTest("_c_urlarg module is not available")
     self.failUnlessEqual("!@#+b",
         _c_urlarg.unquote("+21+40+23+b", "+"))
Exemple #2
0
 def testUnquote(self):
     try:
         from twisted.protocols import _c_urlarg
     except ImportError:
         raise unittest.SkipTest("_c_urlarg module is not available")
     # work exactly like urllib.unquote, including stupid things
     # % followed by a non-hexdigit in the middle and in the end
     self.failUnlessEqual(urllib.unquote("%notreally%n"),
         _c_urlarg.unquote("%notreally%n"))
     # % followed by hexdigit, followed by non-hexdigit
     self.failUnlessEqual(urllib.unquote("%1quite%1"),
         _c_urlarg.unquote("%1quite%1"))
     # unquoted text, followed by some quoted chars, ends in a trailing %
     self.failUnlessEqual(urllib.unquote("blah%21%40%23blah%"),
         _c_urlarg.unquote("blah%21%40%23blah%"))
     # Empty string
     self.failUnlessEqual(urllib.unquote(""), _c_urlarg.unquote(""))
Exemple #3
0
def parse_qs(qs, keep_blank_values=0, strict_parsing=0, unquote=unquote):
    """like cgi.parse_qs, only with custom unquote function"""
    d = {}
    items = [s2 for s1 in qs.split("&") for s2 in s1.split(";")]
    for item in items:
        try:
            k, v = item.split("=", 1)
        except ValueError:
            if strict_parsing:
                raise
            continue
        if v or keep_blank_values:
            k = unquote(k.replace("+", " "))
            v = unquote(v.replace("+", " "))
            if k in d:
                d[k].append(v)
            else:
                d[k] = [v]
    return d
Exemple #4
0
def parse_qs(qs, keep_blank_values=0, strict_parsing=0, unquote=unquote):
    """like cgi.parse_qs, only with custom unquote function"""
    d = {}
    items = [s2 for s1 in qs.split("&") for s2 in s1.split(";")]
    for item in items:
        try:
            k, v = item.split("=", 1)
        except ValueError:
            if strict_parsing:
                raise
            continue
        if v or keep_blank_values:
            k = unquote(k.replace("+", " "))
            v = unquote(v.replace("+", " "))
            if k in d:
                d[k].append(v)
            else:
                d[k] = [v]
    return d
Exemple #5
0
 def __init__(self,
              path,
              defaultType="text/html",
              ignoredExts=(),
              registry=None,
              allowExt=0):
     static.File.__init__(self,
                          unquote(path),
                          defaultType=defaultType,
                          ignoredExts=ignoredExts,
                          registry=registry,
                          allowExt=allowExt)  # added for #
Exemple #6
0
 def __init__(self, path, defaultType="text/html", ignoredExts=(), registry=None, allowExt=0):
     static.File.__init__(self, unquote(path), defaultType=defaultType, ignoredExts=ignoredExts, registry=registry, allowExt=allowExt) # added for #
Exemple #7
0
    def process(self):
        # get site from channel
        self.site = self.channel.site

        # get client character encoding
        self.clientEncoding = self.site.guessClientEncoding(self)
        self.path = unquote(self.path)
        self.path = self.path.decode(
            self.clientEncoding).encode(self.site.serverEncoding)
        self.uri = unquote(self.uri)
        self.uri = self.uri.decode(
            self.clientEncoding).encode(self.site.serverEncoding)
        self.uri = urllib.quote(self.uri)

        # for MOVE and COPY
        self.destination = ""
        destination = self.getHeader("destination")
        if destination:
            destination = unquote(destination)
            self.destination = destination.decode(
                self.clientEncoding).encode(self.site.serverEncoding)

        # set various default headers
        self.setHeader('server', version)
        self.setHeader('date', http.datetimeToString())
        if self.method == "GET":
            # for compatibility of twisted.web.server.Request
            self.setHeader('content-type', "text/html")

        # Resource Identification
        self.prepath = []
        self.postpath = string.split(self.path[1:], '/')
        try:
            if self.method in ("MKCOL", "PUT"):
                if (len(self.postpath) == 1) and (self.postpath[0] == ""):
                    # "MKCOL /" recieved
                    resrc = ConflictResource(
                        "creating %s failed" % self.uri)
                else:
                    try:
                        while 1:
                            self.target_name = self.postpath.pop()
                            if self.target_name:
                                break
                        resrc = self.site.getResourceFor(self)
                    except IndexError: # example: "MKCOL ///"
                        resrc = ConflictResource(
                            "creating %s failed" % self.uri)
            else:
                resrc = self.site.getResourceFor(self)

            if (self.method == "DELETE") and (
                not isinstance(resrc, (ErrorPage, NoResource, NoResourceDeprecated))):
                try:
                    site_root = self.site.resource.path.rstrip("/")
                    delete_target = resrc.path.rstrip("/")
                    if site_root == delete_target:
                        resrc = ForbiddenResource(
                            "deleting %s failed" % self.uri)
                except AttributeError:
                    if isinstance(self.site.resource, UserDirectory):
                        # resource is UserDirectory instance
                        # print "resource is UserDirectory instance..."
                        pass
                    else:
                        raise

            self.render(resrc)
        except: # logs error to file and returns 500 Internal Server Error
            self.processingFailed(failure.Failure())