def pubone(file_name,alg,host): """ @brief Do a NetInf PUBLISH for one file @param file_name is the file to do now """ hash_alg=alg scheme="ni" rform="json" ext="{ \"meta\": { \"pubdirs\" : \"yep\" } }" # record start time of this stime=time.time() # Create NIdigester for use with form encoder and StreamingHTTP ni_digester = NIdigester() # Install the template URL built from the scheme, the authority and the digest algorithm rv = ni_digester.set_url((scheme, host, "/%s" % hash_alg)) if rv != ni_errs.niSUCCESS: nilog("Cannot construct valid ni URL: %s" % ni_errs_txt[rv]) return debug(ni_digester.get_url()) # Open the file if possible try: f = open(file_name, "rb") except Exception, e : debug("Cannot open file %s: Error: %s" %(file_name, str(e))) return
if options.html: rform = "html" elif options.plain: rform = "plain" else: rform = "json" debug("Response type requested: %s" % rform) # Where to send the publish request. http_url = "http://%s/netinfproto/publish" % destination debug("Accessing: %s" % http_url) # Handle full_put = True cases - we have a file with the octets in it if full_put: # Create NIdigester for use with form encoder and StreamingHTTP ni_digester = NIdigester() # Install the template URL built from the scheme, the authority and the digest algorithm rv = ni_digester.set_url((scheme, authority, "/%s" % hash_alg)) if rv != ni_errs.niSUCCESS: print("Cannot construct valid ni URL: %s" % ni_errs_txt[rv]) sys.exit(-4) debug(ni_digester.get_url()) # Open the file if possible try: f = open(options.file_name, "rb") except Exception, e : debug("Cannot open file %s: Error: %s" %(options.file_name, str(e))) parser.error("Unable to open file %s: Error: %s" % (options.file_name, str(e))) sys.exit(-5)
def publish_with_http(ni_name, destination, authority, hash_alg, ext, locs, scheme, full_put, file_name, rform, verbose): """ @brief Action a NetInf publish request using the HTTP convergence layer @param ni_name NIname object instance or None - ni URI to publish if given on comand line - otherwise will be constructed @param destination string netloc (FQDN or IP address with optional port) indicating where to send publish request @param authority string netloc component to insert into ni name (may be "") @param hash_alg string name of hash algorithm used for ni URI @param ext string additional information to send with request if any @param locs list of strings with locators to publish @param scheme URI scheme used for ni URI @param full_put boolean True if the file_name with the content was given @param file_name string name of file to publish or None if only doing metadata @param rform string request format of response @param verbose bolean indicates how much error message output is produced @return 2-tuple - target - string the actual ni name published payload - string - the response received on publication """ # Where to send the publish request. http_url = "http://%s/netinfproto/publish" % destination debug("Publishing via: %s" % http_url) # Handle full_put = True cases - we have a file with the octets in it if full_put: # Create NIdigester for use with form encoder and StreamingHTTP ni_digester = NIdigester() # Install the template URL built from the scheme, the authority and the digest algorithm rv = ni_digester.set_url((scheme, authority, "/%s" % hash_alg)) if rv != ni_errs.niSUCCESS: raise PublishFailure("Cannot construct valid ni URL: %s" % ni_errs_txt[rv], -10) debug(ni_digester.get_url()) # Open the file if possible try: f = open(file_name, "rb") except Exception, e : raise PublishFailure("Unable to open file %s: Error: %s" % (file_name, str(e)), -11) # Guess the mimetype of the file m = magic.Magic(mime=True) ctype = m.from_file(file_name) debug("Content-Type: %s" % ctype) if ctype is None: # Guessing didn't work - default ctype = "application/octet-stream" # Set up HTTP form data for publish request # Make parameter for file with digester octet_param = MultipartParam("octets", fileobj=f, filetype=ctype, filename=file_name, digester = ni_digester) # Make dictionary that will dynamically retrieve ni URI when it has been made uri_dict = { "generator": octet_param.get_url, "length": (len(ni_digester.get_url()) + len(";") + ni_digester.get_b64_encoded_length())} param_list = [octet_param, ("URI", uri_dict), ("msgid", str(random.randint(1, 32000))), ("ext", ext), ("fullPut", "yes"), ("rform", rform)]