Example #1
0
def UrlInHtmlMode(anUrl):
    urlMode = lib_util.GetModeFromUrl(anUrl)
    # sys.stderr.write("UrlInHtmlMode anUrl=%s urlMode=%s\n"%(anUrl,urlMode))
    if urlMode:
        return anUrl
    else:
        return lib_util.AnyUriModed(anUrl, "html")
Example #2
0
    def do_GET(self):
        self.LogMsg("RdfQueue_HTTPRequestHandler do_GET path=" + self.path)

        # Some browsers ask for this.
        if self.path == "/favicon.ico":
            return

        if lib_tabular.ServeFile(self.path):
            self.LogMsg("Tabular file: " + self.path)
            return

        # TODO: Use CGI arguments to return the size.
        if self.path == "/infoqueues":
            self.WriteQueueInformation()
            return

        # ET AUSSI: ON VA SERVIR LES FICHIERS CSV QU'ON CREE.
        # ON LES RECONNAIT CAR YA "Tabular" AU DEBUT.

        try:
            # Maybe some ancillary tasks.
            (entityType, entityId) = self.ParseQuery()

            self.LogMsg("Getting queue entityId=" + entityId)
            try:
                theQ = self.server.m_queues[entityId]
                theFeed = self.server.m_feeder[entityId]
            except KeyError:
                self.LogMsg("Caught when getting queue entityId=" + entityId)
                lib_common.ErrorMessageHtml("No queue for entityId=" +
                                            entityId)

            # TODO: This test is already done in ParseQuery, so it could be simplified.
            self.LogMsg("Getting pid")
            pidFeeder = theFeed.pid
            if not psutil.pid_exists(pidFeeder):
                self.LogMsg("pidFeeder " + str(pidFeeder) + " not there.")
                lib_common.ErrorMessageHtml("pidFeeder " + str(pidFeeder) +
                                            " not there.")
                # TODO: Restart the feeder if it crashed.
                # TODO: Restart the feeder if it crashed.
                # TODO: Restart the feeder if it crashed.
                # OUI MAIS IL Y A DEJA EU UNE TENTATIVE AVEC ParseQuery()

            self.send_response(200)

            grph = cgiEnv.GetGraph()

            self.LogMsg("do_GET Before insertion pidFeeder=%d nbtriples=%d" %
                        (pidFeeder, len(grph)))
            # Add all the triples stored in the shared queue.
            errMsg = None
            while not theQ.empty():
                self.LogMsg("Deserializing pidFeeder=%d szQ=%d" %
                            (pidFeeder, theQ.qsize()))
                triple = theQ.get()
                self.LogMsg("After get pidFeeder=%d szQ=%d" %
                            (pidFeeder, theQ.qsize()))

                # If the object is NOT a triplet (In fact, a tuple) but a string,
                # it can only be an error message, by convention.
                if isinstance(triple, str):
                    errMsg = triple
                    self.LogMsg("Instead of tuple, str=" + errMsg)
                    break

                try:
                    self.server.m_deserial(self.server.m_logFd, grph, triple)
                except Exception:
                    exc = sys.exc_info()[1]
                    self.LogMsg("do_GET deserial:" + str(exc))

            if errMsg != None:
                self.LogMsg("do_GET Found error when reading queue=" + errMsg)
                # TODO: Do we restart the feeder ? How to display this error message ?
            else:
                self.LogMsg("do_GET No error when reading queue")

            self.LogMsg("After insertion pidFeeder=%d nbtriples=%d" %
                        (pidFeeder, len(grph)))

            mode = lib_util.GetModeFromUrl(self.path)

            self.LogMsg("GetModeFromUrl=" + mode)

            topUrl = lib_util.TopUrl(entityType, entityId)

            lib_util.SetDefaultOutput(self.wfile)

            # TODO: How can we pass the edition parameters ? How to change them ?
            # How to store them ? For the moment, the subservers cannot have any parameter.
            # Maybe having parameters will imply restarting the feeder and maybe the subserver.
            lib_common.OutCgiMode(grph,
                                  topUrl,
                                  mode,
                                  self.server.m_page_title,
                                  self.server.m_dot_layout,
                                  errorMsg=errMsg,
                                  isSubServer=True)

            self.LogMsg("End of transmission pidFeeder=" + str(pidFeeder))
        except Exception:
            exc = sys.exc_info()[1]

            # TODO: Send more appropriate error messages !!
            self.send_error(
                404, "go_GET caught:" + str(exc) + " pid=" + str(os.getpid()))

        self.LogMsg("do_GET: Leaving")
Example #3
0
def GetMimeTypeFromUrl(url):
    urlMode = lib_util.GetModeFromUrl(url)
    if urlMode and urlMode.startswith(mimeModePrefix):
        return ModeToMimeType(urlMode)
    else:
        return ""
Example #4
0
def ParseEntityUri(uriWithMode, longDisplay=True, force_entity_ip_addr=None):
    #sys.stderr.write("ParseEntityUri uriWithMode=%s\n"%uriWithMode)

    # Maybe there is a host name before the entity type. It can contain letters, numbers,
    # hyphens, dots etc... but no ":" or "@".
    # THIS CANNOT WORK WITH IPV6 ADDRESSES...
    # WE MAY USE SCP SYNTAX: scp -6 osis@\[2001:db8:0:1\]:/home/osis/test.file ./test.file

    # In the URI, we might have the CGI parameter "&mode=json". It must be removed otherwise
    # it could be taken in entity_id, and the result of EntityToLabel() would be wrong.
    uriWithModeClean = lib_util.UrlNoAmp(uriWithMode)
    uri = lib_util.AnyUriModed(uriWithModeClean, "")
    uriMode = lib_util.GetModeFromUrl(uriWithModeClean)

    uprs = lib_util.survol_urlparse(uri)

    filScript = os.path.basename(uprs.path)
    # sys.stderr.write("ParseEntityUri filScript=%s\n"%filScript)

    # This works for the scripts:
    # entity.py            xid=namespace/type:idGetNamespaceType
    # objtypes_wbem.py     Just extracts the namespace, as it prefixes the type: xid=namespace/type:id

    # See variable lib_util.xidCgiDelimiter="?xid="
    if uprs.query.startswith("xid="):
        # TODO: Maybe the chain contains HTML codes and therefore cannot be parsed.
        # Ex: "xid=%40%2F%3Aoracle_package." == "xid=@/:oracle_package."
        (entity_type, entity_id,
         entity_host) = lib_util.ParseXid(uprs.query[4:])

        entity_graphic_class = entity_type

        entity_label = CalcLabel(entity_host, entity_type, entity_id,
                                 force_entity_ip_addr, filScript)

        # TODO: Consider external_url_to_title, similar logic with different results.
        if longDisplay:
            entity_label = KnownScriptToTitle(filScript, uriMode, entity_host,
                                              entity_label)

    # Maybe an internal script, but not entity.py
    # It has a special entity type as a display parameter
    elif uri.startswith(lib_util.uriRoot):
        # This is a bit of a special case which allows to display something if we know only
        # the type of the entity but its id is undefined. Instead of displaying nothing,
        # this attempts to display all available entities of this given type.
        # source_top/enumerate_process.py etc... Not "." because this has a special role in Python.
        mtch_enumerate = re.match(r"^.*/enumerate_([a-z0-9A-Z_]*)\.py$", uri)
        if mtch_enumerate:
            entity_graphic_class = mtch_enumerate.group(1)
            entity_id = ""
            # TODO: Change this label, not very nice.
            # This indicates that a specific script can list all objects of a given entity type.
            entity_label = entity_graphic_class + " enumeration"
        else:
            entity_graphic_class = "provider_script"
            entity_id = ""

            entity_label = KnownScriptToTitle(filScript, uriMode)

    elif uri.split(':')[0] in ["ftp", "http", "https", "urn", "mail"]:
        # Standard URLs. Example: lib_common.NodeUrl( "http://www.google.com" )
        entity_graphic_class = ""
        entity_id = ""
        # Display the complete URL, otherwise it is not clickable.
        entity_label = uriWithMode  # uri # uri.split('/')[2]

    else:
        entity_graphic_class = ""
        entity_id = "PLAINTEXTONLY"
        entity_label = UriToTitle(uprs)
        # TODO: " " are replaced by "%20". Why ? So change back.
        entity_label = entity_label.replace("%20", " ")

    return (entity_label, entity_graphic_class, entity_id)