CGI gzip compression module

Simple CGI output module. Uses gzip compression on the output stream if the client accepts it.

Last modified
Lines 239
Indexable Yes

Parent directory Download CGIread sitemap Main page

Quick links: done init overload_test write_b write_body write_h write_head

  1. #!/usr/bin/python
  2. import os
  3. import sys
  4. import subprocess
  5. '''
  6. compressout
  7. ===========
  8. Simple CGI output compression module.
  9. Not all webservers support compressing the output stream of CGI scripts.
  10. This script determines whether or not a client accept gzip compression and
  11. compresses the output stream of the script.
  12. NOTICE: The `cgitb` module will write to stdout if the script crashes,
  13. you should use a browser that does not accept gzip, when you are
  14. testing your scripts.
  15. NOTICE: This module uses two global variable: `use_gzip` and `body`.
  16. It's supposed to be used like an object, but rather than using a class,
  17. this is imported "pre-created" or so to say.
  18. NOTICE: There are two constants:
  19.   `max_load_avg_1min`:
  20.     This is the maximum allowed load average under one minute.
  21.     If the one minute load average exceeds this value, compressout will
  22.     return a 503 response to the client and abort the process.
  23.   `http503_body`:
  24.     A plain text error message to send if the server is overloaded.
  25. You should modify these constants to fit your own needs.
  26. Example / TL;DR
  27. ===============
  28. import compressout
  29. compressout.init()
  30. compressout.write_head('Content-Type: text/plain\r\n')
  31. compressout.write_head('Foo: test\r\nBar: test\r\n')
  32. # Blank line required for terminating the HEAD section
  33. compressout.write_head('\r\n')
  34. compressout.write_body('Hello world!\n')
  35. compressout.write_body('Bye.\n')
  36. compressout.done()
  37. Functions
  38. =========
  39. init(write_headers=True)
  40. ------------------------
  41.     Initialize the module.  This function will detect if the client
  42.     supports gzip.
  43.     If `write_headers` is True, the function writes a
  44.     'Vary: Content-Encoding' header and (if gzip is used) a
  45.     'Content-Encdoing: gzip' header.
  46. write_head(s) and write_h(s)
  47. ----------------------------
  48.     
  49.     This function is used to print all HTTP headers **and the blank line
  50.     separating the head from the body**.
  51.     Write `s` to standard output, will never go through gzip.
  52. write_body(s) and write_b(s)
  53. ----------------------------
  54.     Write part of body.
  55.     NOTICE: You need to have printed the blank line after the headers
  56.     with the `write_h` (or `write_head`) fuction.
  57.     
  58.     If gzip is supported by the client
  59.     ----------------------------------
  60.     
  61.         `s` will be appended to a local buffer which the `done` function
  62.         will compress and print.
  63.     
  64.     If gzip is not supported
  65.     ------------------------
  66.     
  67.         `s` will go straight to stdout. The `done` function won't do
  68.         anything.
  69. done()
  70. ------
  71.     Done writing output.
  72.     This function will invoke gzip.
  73. Dos and don'ts
  74. ==============
  75.     * Try to call `init` and `done` at convenient locations like on the
  76.       "outside" of a main function, i.e. don't repeat yourself by calling
  77.       these two functions everywhere in your code.
  78.     * Never call `write_head` after any call to `write_body`.
  79.     * Always call `done` when your done.
  80.     * Use only compressout to write output, otherwise you'll have a mess.
  81.     * NOTICE: The `cgitb` module will write to stdout if the script
  82.       crashes, you should use a browser that does not accept gzip,
  83.       when you are testing your scripts.  Eg, lwp-request.
  84.       `GET http://example.com/ | less` is excellent for debuggin.
  85. '''
  86. ### GLOBALS ###
  87. use_gzip = None     # Whether or not to compress the body
  88. body = ''           # The body is stored here if it is to be compressed
  89. ### END GLOBALS ###
  90. ### CONSTANTS -- Configure for your own needs ###
  91. # If the load average of the last one minute exceeds the hard coded value,
  92. # this script will return a 503 response and abort the process.
  93. max_load_avg_1min = 3.5
  94. http503_body = '''
  95. Service temporarily unavailable!
  96. Wait at least two minutes before trying again.
  97. Re-attempting prematurely may result in banning your IP address.
  98. -- END --
  99. '''
  100. def overload_test(too_late=False):
  101.     '''
  102.     '''
  103.     if os.getloadavg()[0] > max_load_avg_1min:
  104.         if not too_late:
  105.             sys.stdout.write('Status: 503\n')
  106.             sys.stdout.write('Content-Type: text/plain\n')
  107.             sys.stdout.write('Retry-After: 90\n')
  108.             sys.stdout.write(http503_body)
  109.             sys.stdout.flush()
  110.         os.abort()
  111.             
  112. def init(write_headers=True):
  113.     '''
  114.     Initialize the module.  This function will detect if the client
  115.     support gzip.
  116.     If `write_headers`, write a 'Vary' and (if used)
  117.     'Content-Encoding' header.
  118.     '''
  119.     
  120.     global use_gzip
  121.     global body
  122.     
  123.     # This is the only place where sending a 503 message will work.
  124.     # write_h:
  125.     #   - Message body may need to be compressed.
  126.     #   - Possibility of conflicting Status headers.
  127.     # write_b:
  128.     #   - Message body may need to be compressed.
  129.     #   - Message body may be application/xhtml+xml
  130.     # done:
  131.     #   - Message body needs to be compressed if `use_gzip`.
  132.     #   - Body has already been written if not `use_gzip`.
  133.     overload_test(too_late=False)
  134.         
  135.     use_gzip = 'gzip' in os.getenv('HTTP_ACCEPT_ENCODING', '')
  136.     body = ''
  137.     
  138.     if write_headers:
  139.         sys.stdout.write('Vary: Accept-Encoding\n')
  140.         if use_gzip:
  141.             sys.stdout.write('Content-Encoding: gzip\n')
  142. def write_head(s):
  143.     write_h(s)
  144. def write_h(s):
  145.     '''
  146.     Write part of header.
  147.     Write `s` to standard output, will never go through gzip.
  148.     '''
  149.     overload_test(too_late=True)    
  150.     sys.stdout.write(s)
  151. def write_body(s):
  152.     write_b(s)
  153. def write_b(s):
  154.     '''
  155.     Write part of body.
  156.     
  157.     gzip is supported by the client
  158.     -------------------------------
  159.     
  160.         `s` will be appended to a local buffer
  161.         which `done` will compress and print.
  162.     
  163.     gzip is not supported
  164.     ---------------------
  165.     
  166.         `s` will go straight to stdout.
  167.     '''
  168.     
  169.     global body
  170.     overload_test(too_late=True)
  171.     
  172.     if use_gzip:
  173.         body += s
  174.     else:
  175.         sys.stdout.write(s)
  176. def done():
  177.     '''
  178.     Done writing output.
  179.     This function will invoke gzip.
  180.     '''
  181.     
  182.     overload_test(too_late=True)
  183.     if use_gzip:
  184.         gzip = subprocess.Popen(
  185.             ['gzip'],
  186.             stdin=subprocess.PIPE,
  187.             stdout=subprocess.PIPE
  188.         )
  189.         sys.stdout.write(gzip.communicate(body)[0])