Issue #45 resolved

Content-type is not set when serving static content

Anonymous created an issue

When serving static content, the content-type header is not set. This causes problems with, for example, gzip filter, where exception is raised when displaying images from the static locations.

Comments (5)

  1. Anonymous

    Specify: Content-type is set when serving the file. It's not set when the file has not been modified (server responses with 304 status code)

  2. Anonymous

    Here's the patch that fixes this:

    @@ -237,11 +237,16 @@
     
                 strModifTime = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(modifTime))
     
    -            # Check if browser sent "if-modified-since" in request header
    +            # Get content-type based on filename extension
    +           if i != -1: ext = path[i:]
    +           else: ext = ""
    +           contentType = mimetypes.types_map.get(ext, "text/plain")
    +            
    +           # Check if browser sent "if-modified-since" in request header
                 if cpg.request.headerMap.has_key('If-Modified-Since'):
                     # Check if if-modified-since date is the same as strModifTime
                     if cpg.request.headerMap['If-Modified-Since'] == strModifTime:
    -                    cpg.response.headerMap = {'Status': 304, 'protocolVersion': cpg.configOption.protocolVersion, 'Date': date}
    +                    cpg.response.headerMap = {'Status': 304, 'protocolVersion': cpg.configOption.protocolVersion, 'Date': date, 'Content-Type' : contentType}
                         cpg.response.body = ''
                         sendResponse(wfile)
                         return
    @@ -250,11 +255,6 @@
                 f=open(fname, 'rb')
                 cpg.response.body = f.read()
                 f.close()
    -            # Set content-type based on filename extension
    -            i = path.rfind('.')
    -            if i != -1: ext = path[i:]
    -            else: ext = ""
    -            contentType = mimetypes.types_map.get(ext, "text/plain")
                 cpg.response.headerMap['Content-Type'] = contentType
                 sendResponse(wfile)
                 return
    
  3. Anonymous

    Actually, we should set a content-type if it's a 304 ...

    I fixed the filter to it checks that there is a body before trying to gzip it

  4. Log in to comment