Ejemplo n.º 1
0
 import redis
 redis_conn = redis.StrictRedis()
 cache_inst = RedisNetInfCache(storage_root, logger)
 cache_inst.set_redis_conn(redis_conn)
 f = open(cache_inst.temp_path+"temp", "w")
 f.write("some_text")
 f.close()
 cache_inst2 = RedisNetInfCache(storage_root, logger)
 cache_inst2.set_redis_conn(redis_conn)
 if os.path.isfile(cache_inst2.temp_path+"temp"):
     print"Temporaries not cleared"
     
 #---------------------------------------------------------------------------#
 ni_url = "ni://mumble.org/sha-256-32;uzFqGA"
 ni_name = NIname(ni_url)
 ni_name.validate_ni_url(has_params=True)
 ni_name_uv = NIname(ni_url)
 ni_name_np = NIname("ni:///sha-256")
 ni_name_np.validate_ni_url(has_params=False)
 print(str(ni_name.get_url()))
 md = NetInfMetaData(ni_name.get_canonical_ni_url(), "now",
                     loc1="http://www.example.com",
                     extrameta={ "something" : "else" })
 print(md)
 md1 = NetInfMetaData("ni://abr.org/sha_256_64;fjhaie8978", "now",
                      loc2="https://zzz.mumble.org",
                      extrameta={ "something" : "else" })
 print md1
 
 try:
     m, f, n, i = cache_inst.cache_put(ni_name_uv, md, None)
Ejemplo n.º 2
0
def py_nicl():
    """
    @brief Command line program to generate and validate digests in ni: URLs.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  nicl.py --help

    to see usage and options.
    """
    
    # Options parsing and verification stuff
    usage = "%prog [-g|-w|-v] -n <name> -f <pathname of content file> [-V]\n"
    usage = usage + "       %prog -m -n <name> [-V]\n"
    usage = usage + "       %prog -b -s <suite_number> -f <pathname of content file> [-V]\n"
    usage = usage + "       The name can be either an ni: or nih: scheme URI\n"
    usage = usage + "       Return code: success 0, failure non-zero (-V for more info)\n"
    usage = usage + "       Available hashalg (suite number) options:\n"
    usage = usage + "       %s" % NIname.list_algs()
    parser = OptionParser(usage)
    
    parser.add_option("-g", "--generate", default=False,
                      action="store_true", dest="generate",
                      help="Generate hash based on content file, " + \
                           "and output name with encoded hash after the hashalg string")
    parser.add_option("-w", "--well-known", default=False,
                      action="store_true", dest="well_known",
                      help="Generate hash based on content file, " + \
                           "and output name with encoded hash in the .well_known URL " + \
                           "after the hashalg string. Applies to ni: scheme only.")
    parser.add_option("-v", "--verify", default=False,
                      action="store_true", dest="verify",
                      help="Verify hash in name is correct for content file")
    parser.add_option("-m", "--map", default=False,
                      action="store_true", dest="map_wkn",
                      help="Maps from an ni: name to a .well-known URL")
    parser.add_option("-b", "--binary", default=False,
                      action="store_true", dest="bin",
                      help="Outputs the name in binary format for a given suite number")
    parser.add_option("-V", "--verbose", default=False,
                      action="store_true", dest="verbose",
                      help="Be more long winded.")
    parser.add_option("-n", "--ni-name", dest="ni_name",
                      type="string",
                      help="The ni name template for (-g) or ni name matching (-v) content file.")
    parser.add_option("-f", "--file", dest="file_name",
                      type="string",
                      help="File with content data named by ni name.")
    parser.add_option("-s", "--suite-no", dest="suite_no",
                      type="int",
                      help="Suite number for hash algorithm to use.")

    (opts, args) = parser.parse_args()

    if not (opts.generate or opts.well_known or opts.verify or
            opts.map_wkn or opts.bin ):
        parser.error( "Must specify one of -g/--generate, -w/--well-known, -v/--verify, -m/--map or -b/--binary.")
    if opts.generate or opts.well_known or opts.verify:
        if (opts.ni_name == None) or (opts.file_name == None):
            parser.error("Must specify both name and content file name for -g, -w or -v.")
    if opts.map_wkn:
        if (opts.ni_name == None):
            parser.error("Must specify ni name for -m.")
    if opts.bin:
        if (opts.suite_no == None) or (opts.file_name == None):
            parser.error("Must specify both suite number and content file name for -b.")
    if len(args) != 0:
        parser.error("Too many or unrecognised arguments specified")

    # Execute requested action
    if opts.generate:
        n = NIname(opts.ni_name)
        ret = NIproc.makenif(n, opts.file_name)
        if ret == ni_errs.niSUCCESS:
            if opts.verbose:
                print("Name generated successfully.")
            print "%s" % n.get_url()
            sys.exit(0)
        if opts.verbose:
            print "Name could not be successfully generated."
    elif opts.well_known:
        n = NIname(opts.ni_name)
        if n.get_scheme() == "nih":
            if opts.verbose:
                print "Only applicable to ni: scheme names."
            sys.exit(1)
        ret = NIproc.makenif(n, opts.file_name)
        if ret == ni_errs.niSUCCESS:
            if opts.verbose:
                print("Name generated successfully.")
            print "%s" % n.get_wku_transform()
            sys.exit(0)
        if opts.verbose:
            print "Name could not be successfully generated"
    elif opts.verify:
        n = NIname(opts.ni_name)
        ret = NIproc.checknif(n, opts.file_name)
        if ret == ni_errs.niSUCCESS:
            if opts.verbose:
                print("Name matches content file.")
                print "%s" % n.get_url()
            sys.exit(0)
        if opts.verbose:
            print "Check of name against content failed."
    elif opts.map_wkn:
        n = NIname(opts.ni_name)
        ret = n.validate_ni_url(has_params = True)
        if ret == ni_errs.niSUCCESS:
            if n.get_scheme() == "nih":
                if opts.verbose:
                    print "Only applicable to ni: scheme names."
                sys.exit(1)
            if opts.verbose:
                print("Name validated successfully.")
            print "%s" % n.get_wku_transform()
            sys.exit(0)
        else:
            if opts.verbose:
                print "Name could not be successfully validated."
    elif opts.bin:
        (ret, bin_name) = NIproc.makebnf(opts.suite_no, opts.file_name)
        if ret == ni_errs.niSUCCESS:
            if opts.verbose:
                print("Name generated successfully.")
            print base64.b16encode(str(bin_name))
            sys.exit(0)
        else:
            if opts.verbose:
                print "Name could not be successfully generated."
    else:
        print"Should not have happened"
        sys.exit(2)

    # Print appropriate error message
    if opts.verbose:
        print "Error: %s" % ni_errs_txt[ret]
    sys.exit(1)
        
    sys.exit(0)
Ejemplo n.º 3
0
def py_niget():
    """
    @brief Command line program to perform a NetInf 'get' operation using http
    @brief convergence layer.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  niget.py --help

    to see usage and options.

    Exit code is 0 for success, 1 if HTTP returned something except 200,
    and negative for local errors.
    """
    
    # Options parsing and verification stuff
    usage = "%prog [-q] [-l] [-d] [-m|-v] [-f <pathname of content file>] <ni name>\n" + \
            "<ni name> must include location (netloc) from which to retrieve object."
    parser = OptionParser(usage)
    
    parser.add_option("-f", "--file", dest="file_name",
                      type="string",
                      help="File to hold retrieved content. Defaults to hash code in current directory if not present")
    parser.add_option("-q", "--quiet", default=False,
                      action="store_true", dest="quiet",
                      help="Suppress textual output")
    parser.add_option("-s", "--server", dest="server",
                      type="string",
                      help="hostname:port of server to send the NetInf GET to")
    parser.add_option("-l", "--lax", default=False,
                      action="store_true", dest="lax",
                      help="Store returned content even if digest doesn't validate")
    parser.add_option("-m", "--metadata", default=False,
                      action="store_true", dest="metadata",
                      help="Output returned metadata as JSON string")
    parser.add_option("-v", "--view", default=False,
                      action="store_true", dest="view",
                      help="Pretty print returned metadata.")
    parser.add_option("-d", "--dump", default=False,
                      action="store_true", dest="dump",
                      help="Dump raw HTTP response to stdout.")

    (options, args) = parser.parse_args()

    # Check command line options - -q, -f, -l, -m, -v and -d are optional, <ni name> is mandatory
    if len(args) != 1:
        parser.error("URL <ni name> not specified.")
        sys.exit(-1)
    verbose = not options.quiet

    # Create NIname instance for supplied URL and validate it
    ni_url = NIname(args[0])

    # Must be a complete ni: URL with non-empty params field
    rv = ni_url.validate_ni_url(has_params = True)
    if (rv != ni_errs.niSUCCESS):
        if verbose:
            print("Error: %s is not a complete, valid ni scheme URL: %s" % (ni_url.get_url(), ni_errs_txt[rv]))
        sys.exit(-2)

    # Generate file name for output if not specified
    if (options.file_name == None):
        options.file_name = ni_url.get_digest()
        
    # Generate NetInf form access URL
    if (options.server != None):
        server = options.server
    else:
        server = ni_url.get_netloc()

    http_url = "http://%s/netinfproto/get" % server
    """
    if (http_url == None):
        if verbose:
            print("Error: Unable to generate http: transformed URL for ni URL %s" % ni_urlparse.get_url())
        sys.exit(-3)
    """
    
    # Set up HTTP form data for get request
    form_data = urllib.urlencode({ "URI":   ni_url.get_url(),
                                   "msgid": random.randint(1, 32000),
                                   "ext":   "" })

    # Send POST request to destination server
    try:
        http_object = urllib2.urlopen(http_url, form_data)
    except Exception, e:
        if verbose:
            print("Error: Unable to access http URL %s: %s" % (http_url, str(e)))
        sys.exit(-4)
Ejemplo n.º 4
0
def py_nigetalt():
    """
    @brief Command line program to perform a NetInf 'get' operation using http
    @brief convergence layer.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  nigetalt.py --help

    to see usage and options.

    Exit code is 0 for success, 1 if HTTP returned something except 200,
    and negative for local errors.
    """
    
    # Options parsing and verification stuff
    usage = "%prog [-q] [-l] [-d] [-m|-v] [-f <pathname of content file>] [-w <locator>] <ni name>\n" + \
            "Either <ni name> must include location (netloc) from which to retrieve object, or\n" + \
            "a locator must be given with the -w/--whence option.\n" + \
            "The locator may be prefixed with an HTTP ('http://')or DTN ('dtn://') URI scheme identifier.\n" + \
            "If no scheme identifier is given then HTTP is assumed.  The DTN scheme does not accept ports."
    parser = OptionParser(usage)
    
    parser.add_option("-f", "--file", dest="file_name",
                      type="string",
                      help="File to hold retrieved content. Defaults to hash code in current directory if not present")
    parser.add_option("-w", "--whence", dest="loc",
                      type="string", default=None,
                      help="Locator to which to send NetInf GET request.  May be prefixed with http:// or dtn://")
    parser.add_option("-q", "--quiet", default=False,
                      action="store_true", dest="quiet",
                      help="Suppress textual output")
    parser.add_option("-l", "--lax", default=False,
                      action="store_true", dest="lax",
                      help="Store returned content even if digest doesn't validate")
    parser.add_option("-m", "--metadata", default=False,
                      action="store_true", dest="metadata",
                      help="Output returned metadata as JSON string")
    parser.add_option("-v", "--view", default=False,
                      action="store_true", dest="view",
                      help="Pretty print returned metadata.")

    (options, args) = parser.parse_args()

    # Check command line options - -q, -f, -l, -m, and -v are optional,
    # <ni name> is mandatory
    # -w is optional if <ni name> contains a netloc
    if len(args) != 1:
        parser.error("URL <ni name> not specified.")
        sys.exit(-1)
    verbose = not options.quiet

    # Create NIname instance for supplied URL and validate it
    ni_url = NIname(args[0])

    # Must be a complete ni: URL with non-empty params field
    rv = ni_url.validate_ni_url(has_params = True)
    if (rv != ni_errs.niSUCCESS):
        if verbose:
            print("Error: %s is not a complete, valid ni scheme URL: %s" % (ni_url.get_url(), ni_errs_txt[rv]))
        sys.exit(-2)

    # Generate file name for output if not specified
    if (options.file_name == None):
        options.file_name = ni_url.get_digest()

    # Decide Convergence Layer to use and locator to access
    netloc = ni_url.get_netloc()
    cl = HTTP_SCHEME
    if netloc == "":
        # Must have -w option
        if options.loc is None:
            if verbose:
                print("Error: Must provide a locator either in ni URI or via -w/--whence")
            sys.exit(-3)
        loc = options.loc.lower()
    elif options.loc is not None:
        if verbose:
            print("Warning: -w/--whence locator option overrides netloc in ni URI")
        loc = options.loc.lower()
    else:
        loc = netloc.lower()

    # See if URI scheme was specified
    if loc.startswith(HTTP_SCHEME):
        loc = loc[len(HTTP_SCHEME):]
    elif loc.startswith(DTN_SCHEME):
        loc = loc[len(DTN_SCHEME):]
        cl = DTN_SCHEME
    else:
        ssep = loc.find("://")
        if ssep != -1:
            if verbose:
                print("Error: Convergence Layer for scheme %s is not supported - use dtn or http" %
                      loc[:ssep])
            sys.exit(-4)
        # Default assume HTTP

    # Action the GET according to CL selected
    if cl == HTTP_SCHEME:
        json_report, got_content, faulty = get_via_http(ni_url, loc,
                                                        options.file_name,
                                                        verbose, options.lax)
    else:
        json_report, got_content, faulty = get_via_dtn(ni_url, loc,
                                                       options.file_name,
                                                       verbose, options.lax)

    if options.view:
        print("Returned metadata for %s:" % args[0])
        print json.dumps(json_report, indent = 4)
    elif options.metadata:
        print json.dumps(json_report, separators=(",", ":"))

    if not got_content:
        rv = 1
    elif faulty:
        rv = 2
    else:
        rv = 0

    if verbose and got_content:
        if not faulty:
            print("Content successfully retrieved and placed in file %s." %
                  options.file_name)
        else:
            print("Content retrieved and placed in file %s but digest didn't verify." %
                  options.file_name)
    elif verbose:
        print("Only metadata retrieved")

    sys.exit(rv)
Ejemplo n.º 5
0
def py_nipub():
    """
    @brief Command line program to perform a NetInf 'publish' operation using http
    @brief convergence layer.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  nipub.py --help

    to see usage and options.

    Exit code is 0 for success, 1 if HTTP returned something except 200,
    and negative for local errors.
    """
    
    # Options parsing and verification stuff
    usage = "%%prog %s\n       %%prog %s\n%s\n       %%prog %s\n       %%prog %s\n%s\n%s" % \
            ("[-q] [-e] [-j|-v|-w|-p] -f <pathname of content file> -d <digest alg> [-l <FQDN - locator>]{1,2}",
             "[-q] [-e] [-j|-v|-w|-p] [-f <pathname of content file>] -n <ni name> [-l <FQDN - locator>]{0,2}",
             "          -- publish file via NI URI over HTTP",
             "[-q] [-e] [-j|-v|-w|-p] -u <HTTP URI of content file> -d <digest alg> [-l <FQDN - locator>]{1,2}",
             "[-q] [-e] [-j|-v|-w|-p] [-u <HTTP URI of content file>] -n <ni name> [-l <FQDN - locator>]{0,2}",
             "          -- publish web content via NI URI over HTTP",
             "Send response as HTML document (-w), plain text (-p), or JSON (-v or -j)\n"
             "Unless -q is specified, the response is sent to standard output.\n"
             "For a JSON response, it can either be output as a 'raw' JSON string (-j) or pretty printed (-v).\n"
             "If none of  -j, -v, -w or -p are specified, a raw JSON response will be requested.")
    parser = OptionParser(usage)
    
    parser.add_option("-f", "--file", dest="file_name",
                      type="string",
                      help="Pathname for local file to be published.")
    parser.add_option("-u", "--uri", dest="http_name",
                      type="string",
                      help="HTTP URL for content to be published.")
    parser.add_option("-d", "--digest", dest="hash_alg",
                      type="string",
                      help="Digest algorithm to be used to hash content "
                           "and create NI URI. Defaults to sha-256.")
    parser.add_option("-n", "--name", dest="ni_name",
                      type="string",
                      help="Complete ni name. If specified with a file or "
                           "HTTP URL, the digest generated from the content "
                           "will be checked against th digest in the name.")
    parser.add_option("-e", "--ext", dest="ext",
                      type="string",
                      help="A JSON encoded object to be sent as the 'ext' "
                           "parameter for the Publish message.")
    parser.add_option("-l", "--loc", dest="locs", action="append",
                      type="string",
                      help="An FQDN where NI might be retrieved. Maybe be "
                           "zero to two if -n is present and has a non-empty netloc. "
                           "Otherwise must be one or two. HTTP is sent to first "
                           "loc if no authority in -n.")
    parser.add_option("-q", "--quiet", default=False,
                      action="store_true", dest="quiet",
                      help="Suppress textual output")
    parser.add_option("-j", "--json", default=False,
                      action="store_true", dest="json_raw",
                      help="Request response as JSON string and output raw JSON "
                           "string returned on stdout.")
    parser.add_option("-v", "--view", default=False,
                      action="store_true", dest="json_pretty",
                      help="Request response as JSON string and pretty print "
                           "JSON string returned on stdout.")
    parser.add_option("-w", "--web", default=False,
                      action="store_true", dest="html",
                      help="Request response as HTML document and output HTML "
                           "returned on stdout.")
    parser.add_option("-p", "--plain", default=False,
                      action="store_true", dest="plain",
                      help="Request response as plain text document and output text "
                           "returned on stdout.")


    (options, args) = parser.parse_args()

    # Check command line options:
    # Arguments -q, -e, -w, -p, -j and -v are optional; there must be one of a -n with an authority in it or at least one -l.
    # Either -d or -n must be specified.
    # If -d is specified, there must be either a -f or a -u but not both at once.
    # If -n is specified, one of -f or -u may be specified. No leftover arguments allowed.
    # Specifying more than one of -w, -p, -j and -v is inappropriate.
    if len(args) != 0:
        parser.error("Unrecognized arguments %s supplied." % str(args))
        sys.exit(-1)
    if ((options.locs is not None) and (len(options.locs) > 2)):
        parser.error("Initial version only supports two locators (-l/--loc).")
        sys.exit(-1)
    if ((options.ni_name == None) and (options.locs == None)):
        parser.error("Must specify a locator (-l/--loc) or a name (-n/--name) with a netloc component to define where to send the request.")
        sys.exit(-1)
    if ((options.hash_alg != None) and (options.ni_name != None)):
        parser.error("Cannot specify both digest algorithm to be used (-d) and complete ni name with algorithm and digest (-n).")
        sys.exit(-1)
    if ((options.hash_alg == None) and (options.ni_name == None)):
        parser.error("Must specify either digest algorithm to be used (-d) or complete ni name with algorithm and digest (-n).")
        sys.exit(-1)
    if ((((options.ni_name == None) and (options.file_name == None) and (options.http_name == None))) or
        ((options.file_name != None) and (options.http_name != None))):
        parser.error("Exactly one of -f/--file and -u/--uri must be specified with -d and optionally with -n.")
        sys.exit(-1)
    fc = 0
    for flag in [options.json_raw, options.json_pretty, options.html, options.plain]:
        if flag:
            fc += 1
    if fc > 1:
        parser.error("Should specify at most one response type argument out of -j, -v, -w and -p.")
        sys.exit(-1)

    file_name = None
    
    # **** -u is not implemented yet
    if options.http_name != None:
        target = options.http_name
        print "Web name as source(-u/--uri option) not yet implemented. Exiting"
        sys.exit(-2)

    if options.file_name != None:
        target = options.file_name
        file_name = options.file_name
        full_put = True
    else:
        target = None
        full_put = False
    debug("full_put: %s" %full_put)

    verbose = not options.quiet

    #  If we have a full ni name (-n option) given..
    if options.ni_name is not None:
        # Check the validity of the ni name
        try:
            ni_name = NIname(options.ni_name)
        except Exception, e:
            if verbose:
                print("Error: value of -n/--name option '%s' is not a valid ni name" % options.ni_name)
            sys.exit(-3)
        rv = ni_name.validate_ni_url()
        if rv != ni_errs.niSUCCESS:
            if verbose:
                print("Error: value of -n/--name option '%s' is not a valid ni name" % options.ni_name)
            sys.exit(-3)

        # Extract the scheme and hash algorithm from the name
        scheme = ni_name.get_scheme()
        hash_alg = ni_name.get_alg_name()

        # If the ni name has a netloc in it then that is where to send; if not must have a loc
        nl = ni_name.get_netloc()
        if ((nl == "") and (options.locs == None)):
            print("Error: name (-n/--name) mist have a netloc if no locator options given,")
            sys.exit(-4)
        if nl != "":
            destination = nl
            authority = nl
        else:
            destination = options.locs[0]
            authority = ""
Ejemplo n.º 6
0
			# Extract JSON values from message
			# Check the message is a application/json
			if json_msg.get("Content-type") != "application/json":
				self.loginfo("do_fwd: weird content type: %s" % json_msg.get("Content-type"))
				continue
		
			# Extract the JSON structure
			try:
				json_report = json.loads(json_msg.get_payload())
			except Exception, e:
				self.loginfo("do_fwd: can't decode json: %s" % str(e));
				continue

			curi=NIname(uri)
			curi.validate_ni_url()
			metadata = NetInfMetaData(curi.get_canonical_ni_url())
			self.loginfo("Metadata I got: %s" % str(json_report))
			metadata.insert_resp_metadata(json_report)

			# if I've the role GET_RES and there's locators then 
			# follow those now
			if ct_msg == None and self.check_role(GET_RES):
				self.loginfo("I'm a GET_RES type of node - going to try follow")
				self.loginfo("meta: %s" % str(json_report))
				# check for locators
				locators = metadata.get_loclist()
				self.loginfo("locs: %s" % str(locators))
				# try follow locators
				for loc in locators:
					self.loginfo("GET_RES following: %s" % loc)
Ejemplo n.º 7
0
    def insert_resp_metadata(self, response):
        """
        @brief Insert metadata from GET-RESP message into internal form.
        @param response Either JSON format dict or JSON format string
                        in format of of GET-RESP metadata.
        @retval None.

        The response is converted to a JSON dictionary and/or checked
        to verify that it is in good shape.

        Can be used either with an 'empty' NetInfMetaData instance
        identified by the 'ni' field being the empty string or with
        a previously populated entry.

        For an empty instance, the header fields ('ni', 'ct' and 'size')
        are populated from the response where possible (may be no 'ct' or 'size'
        in the response).

        The remainder of the information is converted into a new 'details'
        entry which is either used as the first entry or appended to the
        list if there are existing entries.
        
        @raises Exceptions if cannot parse response
        """

        if type(response) == StringType:
            resp_dict = json.loads(response)
        elif type(response) == DictType:
            resp_dict = response
            # Check the response is really a JSON dictionary
            js = json.dumps(response)
        else:
            raise TypeError("Parameter 'response' is not a string or dictionary")

        curr_ni = self.get_ni()
        resp_ni_name = NIname(resp_dict["ni"])
        ret = resp_ni_name.validate_ni_url()
        if ret != ni_errs.niSUCCESS:
            raise InvalidNIname("Response ni field '%s' is not a valid ni URI: %s" %
                                (resp_dict["ni"], ni_errs_txt[ret]))

        if curr_ni == "" :
            # Empty metadata case
            self.json_obj["ni"] = resp_ni_name.get_canonical_ni_url()
            if resp_dict.has_key("ct"):
                self.json_obj["ct"] = resp_dict["ct"]
            if resp_dict.has_key("size"):
                self.json_obj["size"] = resp_dict["size"]
            self.json_obj["details"] = []
        else:
            # The metadata is not empty
            # Create validated NIname for the current metadata
            ni_name = NIname(curr_ni)
            # If this fails the metadata database is corrupt
            assert(ni_name.validate_ni_url() == ni_errs.niSUCCESS)
            if ni_name.cmp(resp_ni_name) == 0:
                # Update with data about same ni name
                if resp_dict.has_key("ct") and (resp_dict["ct"] != ""):
                    if self.json_obj["ct"] == "":
                        self.json_obj["ct"] = resp_dict["ct"]
                    elif self.json_obj["ct"] != resp_dict["ct"]:
                        raise MetadataMismatch("Content Type fields are unmatched")
                if resp_dict.has_key("size") and (resp_dict["size"] >= 0):
                    if self.json_obj["size"] == -1:
                        self.json_obj["size"] = resp_dict["size"]
                    elif self.json_obj["size"] != resp_dict["size"]:
                        raise MetadataMismatch("Size fields are unmatched")
            else:
                raise MetadataMismatch("NI name fields are unmatched curr: %s, got: %s" %
                                       (curr_ni, resp_dict["ni"]))

        new_detail = {}
        new_detail["loc"] = []
        for loc_key in ("loc", "loclist"):
            if resp_dict.has_key(loc_key):
                if type(resp_dict[loc_key]) == ListType:
                    new_detail["loc"] = resp_dict[loc_key]
                else:
                    raise TypeError("Response '%s' value is not a list" %
                                    loc_key)
        auth = resp_ni_name.get_netloc()
        if auth is not None and (auth != ""):
            new_detail["loc"].append(auth)

                
        if resp_dict.has_key("metadata"):
            if type(resp_dict["metadata"]) == DictType:
                new_detail["metadata"] = resp_dict["metadata"]
            else:
                raise TypeError("Response metadata is not an object dictionary")


        if resp_dict.has_key("searches"):
            if not new_detail.has_key("metadata"):
                new_detail["metadata"] = {}
            new_detail["metadata"]["search"] = resp_dict["searches"]

        new_detail["ts"] = self.metadata_timestamp_for_now()
        
        self.json_obj["details"].append(new_detail)
        self.curr_detail = new_detail
        return 
Ejemplo n.º 8
0
def py_nipubalt():
    """
    @brief Command line program to perform a NetInf 'publish' operation using http
    @brief convergence layer.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  nipubalt.py --help

    to see usage and options.

    Exit code is 0 for success, 1 if HTTP returned something except 200,
    and negative for local errors.
    """
    
    # Options parsing and verification stuff
    usage = "%%prog %s\n       %%prog %s\n%s\n%s" % \
            ("[-q] [-e] [-j|-v|-w|-p] -f <pathname of content file> -d <digest alg> [-l <FQDN - locator>]{1,2}",
             "[-q] [-e] [-j|-v|-w|-p] [-f <pathname of content file>] -n <ni name> [-l <FQDN - locator>]{0,2}",
             "          -- publish file via NI URI over HTTP and/or DTN",
             "At least one locator must be given either as part of the -n option or via a -l option.\n"
             "Locators given with -l options can optionally be prefixed with the HTTP scheme (http://) or \n"
             "the DTN scheme (dtn://).  If a -l option is given, this is used to determine the initial\n"
             "publication destination and the convergence layer used will be HTPP unless the -l option\n"
             "explicitly gives the DTN scheme prefix.  If there are no -l options but the -n option has\n"
             "a netloc compnent (FQDN or IP address with optional port) the this will be used with the\n"
             "HTTP convergence layer\n"
             "The response will be sent as HTML document (-w), plain text (-p), or JSON (-v or -j)\n"
             "Unless -q is specified, the response is sent to standard output.\n"
             "For a JSON response, it can either be output as a 'raw' JSON string (-j) or pretty printed (-v).\n"
             "If none of  -j, -v, -w or -p are specified, a raw JSON response will be requested.")
    parser = OptionParser(usage)
    
    parser.add_option("-f", "--file", dest="file_name",
                      type="string",
                      help="Pathname for local file to be published.")
    parser.add_option("-d", "--digest", dest="hash_alg",
                      type="string",
                      help="Digest algorithm to be used to hash content "
                           "and create NI URI. Defaults to sha-256.")
    parser.add_option("-n", "--name", dest="ni_name",
                      type="string",
                      help="Complete ni name. If specified with a file or "
                           "HTTP URL, the digest generated from the content "
                           "will be checked against th digest in the name.")
    parser.add_option("-e", "--ext", dest="ext",
                      type="string",
                      help="A JSON encoded object to be sent as the 'ext' "
                           "parameter for the Publish message.")
    parser.add_option("-l", "--loc", dest="locs", action="append",
                      type="string",
                      help="A locator where NI might be retrieved. Maybe be "
                           "zero to two if -n is present and has a non-empty netloc. "
                           "Otherwise must be one or two. HTTP or DTN is sent to first "
                           "loc if present. Otherwise sent to netloc (authority) in -n."
                           "NOTE: this precedence differs from earlier versions of nipub.")
    parser.add_option("-q", "--quiet", default=False,
                      action="store_true", dest="quiet",
                      help="Suppress textual output")
    parser.add_option("-j", "--json", default=False,
                      action="store_true", dest="json_raw",
                      help="Request response as JSON string and output raw JSON "
                           "string returned on stdout.")
    parser.add_option("-v", "--view", default=False,
                      action="store_true", dest="json_pretty",
                      help="Request response as JSON string and pretty print "
                           "JSON string returned on stdout.")
    parser.add_option("-w", "--web", default=False,
                      action="store_true", dest="html",
                      help="Request response as HTML document and output HTML "
                           "returned on stdout.")
    parser.add_option("-p", "--plain", default=False,
                      action="store_true", dest="plain",
                      help="Request response as plain text document and output text "
                           "returned on stdout.")


    (options, args) = parser.parse_args()

    # Check command line options:
    # Arguments -q, -e, -w, -p, -j and -v are optional; there must be one of a -n with an authority in it or at least one -l.
    # If -n option is specified then there must not be a -d.
    # If -d is specified, there must be a -f.
    # If -n is specified, -f may be specified - otherwise only metadata is published. No leftover arguments allowed.
    # Specifying more than one of -w, -p, -j and -v is inappropriate.
    if len(args) != 0:
        parser.error("Unrecognized arguments %s supplied." % str(args))
        sys.exit(-1)
    if ((options.locs is not None) and (len(options.locs) > 2)):
        parser.error("Initial version only supports two locators (-l/--loc).")
        sys.exit(-1)
    if ((options.ni_name == None) and (options.locs == None)):
        parser.error("Must specify a locator (-l/--loc) or a name (-n/--name) with a netloc component to define where to send the request.")
        sys.exit(-1)
    if ((options.hash_alg != None) and (options.ni_name != None)):
        parser.error("Cannot specify both digest algorithm to be used (-d) and complete ni name with algorithm and digest (-n).")
        sys.exit(-1)
    fc = 0
    for flag in [options.json_raw, options.json_pretty, options.html, options.plain]:
        if flag:
            fc += 1
    if fc > 1:
        parser.error("Should specify at most one response type argument out of -j, -v, -w and -p.")
        sys.exit(-1)

    file_name = None
    
    if options.file_name != None:
        file_name = os.path.abspath(options.file_name)
        # Check the file is readable
        if not os.access(file_name, os.R_OK):
            if verbose:
                print("File to be published %s is not readable" % file_name)
            sys.exit(1)
        full_put = True
    else:
        full_put = False
    debug("full_put: %s" %full_put)

    verbose = not options.quiet

    if ((options.locs is not None) and (len(options.locs) > 2)):
        if verbose:
            print "Warning: only first two -l/--loc locators will be published"

    #  If we have a full ni name (-n option) given..
    if options.ni_name is not None:
        # Check the validity of the ni name
        try:
            ni_name = NIname(options.ni_name)
        except Exception, e:
            if verbose:
                print("Error: value of -n/--name option '%s' is not a valid ni name" %
                      options.ni_name)
            sys.exit(-3)
        rv = ni_name.validate_ni_url()
        if rv != ni_errs.niSUCCESS:
            if verbose:
                print("Error: value of -n/--name option '%s' is not a valid ni name" %
                      options.ni_name)
            sys.exit(-3)

        # Extract the scheme and hash algorithm from the name
        scheme = ni_name.get_scheme()
        hash_alg = ni_name.get_alg_name()

        # If there is a -l option, that is where the request is sent.
        nl = ni_name.get_netloc()
        if ((options.locs is None) and (nl == "")) :
            print("Error: name (-n/--name) must have a netloc if no locator options given,")
            sys.exit(-4)
        # NOTE: The following logic ie reversed from earlier versions so that 
        # can force use of DTN convergence layer with a -l option.
        if nl == "":
            # Already checked this exists
            destination = options.locs[0]
        else:
            destination = nl
        authority = nl