File: mod_chm.py

package info (click to toggle)
archmage 0.0.7-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 304 kB
  • ctags: 73
  • sloc: python: 389; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 961 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from mod_python import apache
from mimetypes import guess_type
from archmod.CHM import *

chmfile = None
chmname = None

def handler(req):
    source = req.filename
    pagename = req.path_info

    global chmfile,chmname

    if chmname != source:
        chmfile = CHMFile(source)

    chmname = source

    if pagename:
        try:
    	    page = chmfile.get_entry_by_name(pagename)
        except:
            return apache.HTTP_NOT_FOUND

        if pagename == '/':
            mimetype = 'text/html'
        else:
            mimetype = guess_type(pagename)[0] or 'application/octet-stream'
        
        req.content_type = mimetype
        req.send_http_header()
     
        req.write(page)
    else:
        mimetype = 'application/chm'
        req.content_type = mimetype
        req.send_http_header()
	file = open(source, "rb")
	while 1:
	    tmp = file.read(4096)
	    if len(tmp)==0:
	        break
	    req.write(tmp)
    return apache.OK