Google App Engine: Won’t serve static assets with below error:

Posted on

Question :

Google App Engine: Won’t serve static assets with below error:

Google is useless in this case, it seems I am the first to encounter this error :/ Works fine on my Mac, but using the same files on a Windows 8 rig gives the following error in the logs when trying to request static assets like CSS files and images. Here is a snippet of the error:

INFO     2014-06-08 14:42:28,431 module.py:639] default: "GET /css/rootStyles.css HTTP/1.1" 200 5454
ERROR    2014-06-08 14:42:28,431 module.py:714] Request to '/css/rootStyles.css' failed
Traceback (most recent call last):
  File "C:Program Files (x86)Googlegoogle_appenginegoogleappenginetoolsdevappserver2module.py", line 710, in _handle_request
    return handler.handle(match, environ, wrapped_start_response)
  File "C:Program Files (x86)Googlegoogle_appenginegoogleappenginetoolsdevappserver2static_files_handler.py", line 369, in handle
    return self._handle_path(full_path, environ, start_response)
  File "C:Program Files (x86)Googlegoogle_appenginegoogleappenginetoolsdevappserver2static_files_handler.py", line 182, in _handle_path
    start_response('200 OK', headers)
  File "C:Program Files (x86)Googlegoogle_appenginegoogleappenginetoolsdevappserver2module.py", line 640, in wrapped_start_response
    return start_response(status, response_headers, exc_info)
  File "C:Program Files (x86)Googlegoogle_appenginelibcherrypycherrypywsgiserverwsgiserver2.py", line 2155, in start_response
    raise TypeError("WSGI response header value %r is not of type str." % v)
TypeError: WSGI response header value u'text/css' is not of type str.
INFO     2014-06-08 14:42:28,433 module.py:639] default: "GET /css/rootStyles.css HTTP/1.1" 500 -

My app.yml file looks like this:

application: foobarbaz
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon.ico
  static_files: favicon.ico
  upload: favicon.ico
- url: /img
  static_dir: img
- url: /font
  static_dir: font
- url: /css
  static_dir: css

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: latest

Answer #1:

Weird. Another person had a static files 500 error on Windows yesterday. This may be a GAE bug, where the mimetype guess is sent as a python unicode string in the header. Try adding mime_type: "text/css" to your - url: /css, as follows:

- url: /css
  static_dir: css
  mime_type: "text/css"

in app.yaml. I don’t think this will solve the problem, but will help diagnose. You will most likely get another error on the img and font headers.

You can use wildcard mapping for the others:

- url: /img/(.*.gif)$
  static_files: img/1
  upload: img/.*.gif$
  mime_type: "image/gif"

- url: /img/(.*.png)$
  static_files: img/1
  upload: img/.*.png$
  mime_type: "image/x-png"

- url: /img/(.*.jpg)$
  static_files: img/1
  upload: img/.*.jpg$
  mime_type: "image/jpeg"

and similar for fonts. Like I said, this may just be a patch to a bug in GAE for Windows. I don’t fully understand the handshake between the request and response, but you could experiment with adding the following as the first <meta> tags in your template, to see if that improves the communication:

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta charset="UTF-8">
Answered By: GAEfan

Answer #2:

I had the same issues using GAE SDK 1.9.6 on Windows 8.1 using python 2.7.7 (I only use the python version and haven’t experienced this using Go SDK).

I just updated to python 2.7.8 and it seems that the issues are resolved.

Just wanted to share this out here, and see if other’s can confirm this. I’ve also added this result to the bug that @Bradley had filed. Hope this helps.

Answered By: Babak K

Answer #3:

As a workaround I edited
.googleappenginetoolsdevappserver2adminstatic_file_handler.py
at

content_type, _ = mimetypes.guess_type(asset_path)
assert content_type, (

to

content_type, _ = mimetypes.guess_type(asset_path)
content_type = str(content_type)
assert content_type, (

of course with appropiate indentation

This is just for admin console.
You could also try it on
.googleappenginetoolsdevappserver2static_file_handler.py

from
return self._url_map.mime_type
to
return str(self._url_map.mime_type)
I guess but I haven’t really try that.

Answered By: Curious Sam

Answer #4:

Started a bug in the issue tracker for GAE. See here: https://code.google.com/p/googleappengine/issues/detail?id=11001
In the meantime, the temporary patch in comme #4 on that thread works.

googleappenginetoolsdevappserver2static_files_handler.py

line 166, 167, 168, need add str to self._get_mime_type(full_path)

if user_headers.Get('Content-type') is None:
    #headers.append(('Content-type', self._get_mime_type(full_path)))<br />
    headers.append(('Content-type', str(self._get_mime_type(full_path))))<br /><br />
    # 2014-06-09 fix on Win7 "TypeError: WSGI response header value u'text/html' is not of type str"
Answered By: Brad Reed

Answer #5:

I got rid of this problem by downgrading python from 2.7.7 to 2.7.6. Maybe you guys will be able to spot the change that caused this behavior in the 2.7.7 release notes.

Answered By: Adrian

Answer #6:

I had the same issue ‘TypeError: WSGI response header value y text/css’ is not a byte string’. I could not see CSS style, it gives me status 500.
I was using python 2.7.7 and I change it to 2.7.6 and all works fine!

Answered By: VivianaSotR

Answer #7:

Error 500 (without further elaboration; no tracebacks or whatever) is also reported if your /var is full.

Answered By: user7610

Leave a Reply

Your email address will not be published. Required fields are marked *