Esempio n. 1
0
    def handle_request (self, request):
        path = request.path
        filename = os.path.join (self.doc_root, path[1:])

        if request.method not in ('get', 'head'):
            request.error (405)
            return

        if os.path.isdir (filename):
            filename = os.path.join (filename, 'index.html')

        if not os.path.isfile (filename):
            request.error (404)
        else:
            stat_info = os.stat (filename)
            mtime = stat_info[stat.ST_MTIME]
            file_length = stat_info[stat.ST_SIZE]

            ims = request['if-modified-since']
            if ims:
                length_match = 1
                m = self.crack_if_modified_since.match (ims)
                if m:
                    length = m.group (3)
                    if length:
                        if int(length) != file_length:
                            length_match = 0

                ims_date = parse_http_date (m.group(1))

                if length_match and ims_date:
                    if mtime <= ims_date:
                        request.error (304)
                        return

            ftype, fencoding = mimetypes.guess_type (filename)
            request['Content-Type'] = ftype or 'text/plain'
            request['Last-Modified'] = build_http_date (mtime)

            # Note: these are blocking file operations.
            if request.method == 'get':
                f = open (filename, 'rb')
                block = f.read (self.block_size)
                if not block:
                    request.error (204)  # no content
                else:
                    while 1:
                        request.push (block)
                        block = f.read (self.block_size)
                        if not block:
                            break
                    request.done()
            elif request.method == 'head':
                pass
                request.done()
            else:
                # should be impossible
                request.error (405)
Esempio n. 2
0
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    'AAAAAAAAAAAAAAD/+K///8AH//+iI///QAH//r4g//x3AH//Z6J//UABP/ovgD/458Ef+u+wv/Tn'
    '0R/+79if9OXZH/6gCJ/2BwAf/u/8n/h33R/7Z7kf/ReQH/+qu7//BUW//7vrv//RR3//7r///80d'
    '///pq///8EP//+rH///d9///6j///9Af/w=='
).decode ('base64')

zsample = zlib.compress (sample, 9)

last_modified = build_http_date (time.time())

class favicon_handler:

    def __init__ (self, data=None):
        if data is None:
            self.data = zsample
        else:
            self.data = data

    def match (self, request):
        return request.path == '/favicon.ico'

    def handle_request (self, request):
        if request['if-modified-since']:
            # if we cared, we could check that timestamp.