Skip to content

Commit

Permalink
Enhancements to handle CORS correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Dataliberate committed Jan 29, 2020
1 parent 15b27f5 commit ba37425
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
9 changes: 8 additions & 1 deletion sdoapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,8 @@ def emitHTTPHeaders(self, node):
if ENABLE_CORS:
origin = self.request.headers.get('Origin', "*")
self.response.headers.add_header("Access-Control-Allow-Origin", origin) # entire site is public.
self.response.headers.add_header("Access-Control-Allow-Headers", "Accept") #
self.response.headers.add_header("Access-Control-Allow-Credentials", "true") #
# see http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

def setupExtensionLayerlist(self, node):
Expand Down Expand Up @@ -2398,12 +2400,17 @@ def get(self, node): #Services httpd GET request
log.warning("Invalid node name '%s'" % str(node))
self.handle404Failure(node,suggest=False)
return

NotModified = False
matchTag = self.request.headers.get("If-None-Match",None)
unMod = self.request.headers.get("If-Unmodified-Since",None)

#log.info("matchTag '%s' unMod '%s'" % (matchTag,unMod))
#b = StringIO.StringIO()
#for h in sorted(self.request.headers):
# b.write("\n %s %s" % (h,self.request.headers.get(h)))
#log.info("Request Headers:%s" % b.getvalue())
#b.close()

hdrIndex = getHostExt()
if len(hdrIndex):
Expand Down
37 changes: 20 additions & 17 deletions sdocloudstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def buildNameType(self,filename,ftype):
return filename

def buildBucketFile(self,filename,ftype,location):
#log.info("buildBucketFile( %s %s %s )" % (filename,ftype,location))
log.info("buildBucketFile( %s %s %s )" % (filename,ftype,location))

filename = self.buildNameType(filename,ftype)

Expand All @@ -122,24 +122,27 @@ def buildBucketFile(self,filename,ftype,location):
else:
bucketFile = filename

mimetype, contentType = mimetypes.guess_type(bucketFile)

if not mimetype:
if ftype == "html":
mimetype = "text/html; charset=utf-8"
elif ftype == "jsonld" or ftype == "json-ld" :
mimetype = "application/ld+json; charset=utf-8"
elif ftype == "json":
mimetype = "application/json; charset=utf-8"
elif ftype == "ttl":
mimetype = "application/x-turtle; charset=utf-8"
elif ftype == "rdf" or ftype == "xml":
mimetype = "application/rdf+xml; charset=utf-8"
elif ftype == "nt":
mimetype = "text/plain"
elif ftype == "txt":
mimetype = "text/plain"
mimetype = None

if ftype == "html":
mimetype = "text/html; charset=utf-8"
elif ftype == "jsonld" or ftype == "json-ld" :
mimetype = "application/ld+json; charset=utf-8"
elif ftype == "json":
mimetype = "application/json; charset=utf-8"
elif ftype == "ttl":
mimetype = "application/x-turtle; charset=utf-8"
elif ftype == "rdf" or ftype == "xml":
mimetype = "application/rdf+xml; charset=utf-8"
elif ftype == "nt":
mimetype = "text/plain"
elif ftype == "txt":
mimetype = "text/plain"

if not mimetype:
mimetype, contentType = mimetypes.guess_type(bucketFile)
log.info("Guessed mimetype %s contenttype %s" % (mimetype,contentType))
#log.info("buildBucketFile: %s '%s' (%s)" % (bucketFile,mimetype,contentType))

return bucketFile, mimetype
Expand Down

0 comments on commit ba37425

Please sign in to comment.