Skip to content

Commit

Permalink
publish.midas3.mdwsgi: allow for unicode in filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
RayPlante committed Jul 5, 2024
1 parent 22019cb commit 80d42f4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions python/nistoar/pdr/preserv/bagger/midas3.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from ....nerdm import utils as nerdutils
from ... import def_merge_etcdir, utils, ARK_NAAN, PDR_PUBLIC_SERVER
from .. import (SIPDirectoryError, SIPDirectoryNotFound, AIPValidationError,
ConfigurationException, StateException, PODError,
ConfigurationException, StateException, PODError, NERDError,
PreservationStateError)
from .... import pdr
from .prepupd import UpdatePrepService
Expand Down Expand Up @@ -324,7 +324,7 @@ def _filepaths_in_pod(self):

pod = self._pod_rec()

return [self._distsvcurl.sub('', urllib.unquote(d['downloadURL']))
return [self._distsvcurl.sub('', urllib.unquote(str(d['downloadURL'])).decode('utf8'))
for d in pod.get('distribution',[]) if 'downloadURL' in d]


Expand Down
23 changes: 16 additions & 7 deletions python/nistoar/pdr/publish/midas3/mdwsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
.getChild(pdrsys.subsystem_abbrev) \
.getChild("m3mdserv")

DEF_BASE_PATH = "/midas/"
DEF_BASE_PATH = u"/midas/"

class MIDAS3DataAccessApp(object):
"""
Expand Down Expand Up @@ -114,11 +114,15 @@ def __init__(self, app, wsgienv, start_resp):
self._midascl = app._midascl

def send_error(self, code, message):
if isinstance(message, unicode):
message = message.encode('utf8')
status = "{0} {1}".format(str(code), message)
self._start(status, [], sys.exc_info())
return []

def send_ok(self, message="OK"):
if isinstance(message, unicode):
message = message.encode('utf8')
status = "{0} {1}".format(str(code), message)
self._start(status, [], None)
return []
Expand All @@ -143,9 +147,11 @@ def handle(self):
meth_handler = 'do_'+self._meth

path = self._env.get('PATH_INFO', '/')
if isinstance(path, str):
path = path.decode('utf8')
if '/' not in path:
path += '/'
if not path.startswith(self.app.base_path):
if not path.startswith(self.app.base_path): # path and base_path must both be unicode
return self.send_error(404, "Resource not found")
path = path[len(self.app.base_path):].rstrip('/')

Expand Down Expand Up @@ -278,9 +284,12 @@ def get_metadata(self, dsid):
if not dir:
continue
mdfile = os.path.join(dir, dsid+".json")
if isinstance(mdfile, unicode):
mdfile = mdfile.encode('utf8')
if os.path.isfile(mdfile):
mdata = read_json(mdfile)
log.info("Retrieving metadata record for id=%s from %s", dsid, mdfile)
log.info("Retrieving metadata record for id=%s from %s",
dsid, mdfile.decode('utf8'))

except ValueError as ex:
log.exception("Internal error while parsing JSON file, %s: %s", mdfile, str(ex))
Expand All @@ -296,7 +305,7 @@ def send_auto_readme(self, dsid, withprompts=True, bebrief=False):
if mdata is None:
log.info("Metadata record not found for ID="+dsid)
return self.send_error(404,
"Dataset with ID={0} not being edited".format(dsid))
"Dataset with ID=%s not being edited" % dsid)
except ValueError as ex:
return self.send_error(500, "Internal parsing error")
except Exception as ex:
Expand Down Expand Up @@ -326,7 +335,7 @@ def send_metadata(self, dsid):
if mdata is None:
log.info("Metadata record not found for ID="+dsid)
return self.send_error(404,
"Dataset with ID={0} not being edited".format(dsid))
"Dataset with ID=%s not being edited" % dsid)
except ValueError as ex:
return self.send_error(500, "Internal parsing error")
except Exception as ex:
Expand Down Expand Up @@ -374,13 +383,13 @@ def send_datafile(self, id, filepath):
mdata = self.get_metadata(id)
if mdata is None:
log.info("send_datafile: Metadata record not found for ID="+id)
return self.send_error(404,"Dataset with ID={0} not available".format(id))
return self.send_error(404,"Dataset with ID=%s not available" % id)
sip = MIDASSIP.fromNERD(mdata, self.app.revdir, self.app.upldir)

except SIPDirectoryNotFound as ex:
# shouldn't happen
log.warn("No SIP directories for ID="+dsid)
self.send_error(404,"Dataset with ID={0} not available".format(id))
self.send_error(404,"Dataset with ID=%s not available" % id)
return []
except Exception as ex:
log.exception("Internal error: "+str(ex))
Expand Down
11 changes: 11 additions & 0 deletions python/tests/nistoar/pdr/publish/midas3/test_mdwsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ def test_bad_id(self):
self.assertIn("404", self.resp[0])
self.assertIn('asdifuiad', self.resp[0])

def test_bad_id_unicode(self):
req = {
'PATH_INFO': '/asdifui\xce\xb1d',
'REQUEST_METHOD': 'GET'
}
body = self.svc(req, self.start)

self.assertGreater(len(self.resp), 0)
self.assertIn("404", self.resp[0])
self.assertIn('asdifui\xce\xb1d', self.resp[0])

def test_ark_id(self):
req = {
'PATH_INFO': '/ark:/88434/mds4-29sd17',
Expand Down

0 comments on commit 80d42f4

Please sign in to comment.