Example #1
0
def _scan_html_file(dictionary, html, fperr=sys.stderr):
    ''' Scan a single html file for class="i18n i18n_foo" tags '''
    if os.path.isfile(html):
        # Skip header and footer that aren't of course well formed
        if html in ("neubot/www/header.html", "neubot/www/footer.html"):
            return

        # Open file and perform SSI substitution
        filep = open(html, "r")
        content = ssi.ssi_replace("neubot/www", filep)

        # Build DOM or fail if the file's not well formed
        fperr.write("XML: parsing %s... " % html)
        document = xml.dom.minidom.parseString(content)
        fperr.write("done\n")

        # Seek of i18n class in DOM
        __process_dom(dictionary, document.documentElement)
Example #2
0
    def process_request(self, stream, request):
        ''' Process a request and generate the response '''

        response = Message()

        if not request.uri.startswith("/"):
            response.compose(code="403", reason="Forbidden",
                             body="403 Forbidden")
            stream.send_response(request, response)
            return

        for prefix, child in self.childs.items():
            if request.uri.startswith(prefix):
                child.process_request(stream, request)
                return

        rootdir = self.conf.get("http.server.rootdir", "")
        if not rootdir:
            response.compose(code="403", reason="Forbidden",
                             body="403 Forbidden")
            stream.send_response(request, response)
            return

        if request.uri == "/":
            response.compose_redirect(stream, "/api/index")
            stream.send_response(request, response)
            return

        if '?' in request.uri:
            request_uri = request.uri.split('?')[0]
        else:
            request_uri = request.uri

        fullpath = utils_path.append(rootdir, request_uri, True)
        if not fullpath:
            response.compose(code="403", reason="Forbidden",
                             body="403 Forbidden")
            stream.send_response(request, response)
            return

        try:
            filep = open(fullpath, "rb")
        except (IOError, OSError):
            logging.error("HTTP: Not Found: %s (WWWDIR: %s)",
                          fullpath, rootdir)
            response.compose(code="404", reason="Not Found",
                             body="404 Not Found")
            stream.send_response(request, response)
            return

        if self.conf.get("http.server.mime", True):
            mimetype, encoding = mimetypes.guess_type(fullpath)

            # Do not attempt SSI if the resource is, say, gzipped
            if not encoding:
                if mimetype == "text/html":
                    ssi = self.conf.get("http.server.ssi", False)
                    if ssi:
                        body = ssi_replace(rootdir, filep)
                        filep = StringIO.StringIO(body)

                #XXX Do we need to enforce the charset?
                if mimetype in ("text/html", "application/x-javascript"):
                    mimetype += "; charset=UTF-8"
            else:
                response["content-encoding"] = encoding

        else:
            mimetype = "text/plain"

        response.compose(code="200", reason="Ok", body=filep,
                         mimetype=mimetype)
        if request.method == "HEAD":
            utils.safe_seek(filep, 0, os.SEEK_END)
        stream.send_response(request, response)