~ubuntu-branches/ubuntu/natty/dulwich/natty

« back to all changes in this revision

Viewing changes to dulwich/web.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2010-10-16 19:49:12 UTC
  • mfrom: (1.2.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20101016194912-klojff5k9a37wiff
Tags: 0.6.2-1
* Build older versions of Python modules as well. Closes: #593751
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
HTTP_OK = '200 OK'
49
49
HTTP_NOT_FOUND = '404 Not Found'
50
50
HTTP_FORBIDDEN = '403 Forbidden'
 
51
HTTP_ERROR = '500 Internal Server Error'
51
52
 
52
53
 
53
54
def date_time_string(timestamp=None):
54
 
    # Based on BaseHTTPServer.py in python2.5
 
55
    # From BaseHTTPRequestHandler.date_time_string in BaseHTTPServer.py in the
 
56
    # Python 2.6.5 standard library, following modifications:
 
57
    #  - Made a global rather than an instance method.
 
58
    #  - weekdayname and monthname are renamed and locals rather than class
 
59
    #    variables.
 
60
    # Copyright (c) 2001-2010 Python Software Foundation; All Rights Reserved
55
61
    weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
56
62
    months = [None,
57
63
              'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
100
106
        f.close()
101
107
    except IOError:
102
108
        f.close()
103
 
        yield req.not_found('Error reading file')
 
109
        yield req.error('Error reading file')
104
110
    except:
105
111
        f.close()
106
112
        raise
128
134
    try:
129
135
        data = object_store[sha].as_legacy_object()
130
136
    except IOError:
131
 
        yield req.not_found('Error reading object')
 
137
        yield req.error('Error reading object')
 
138
        return
132
139
    req.cache_forever()
133
140
    req.respond(HTTP_OK, 'application/x-git-loose-object')
134
141
    yield data
159
166
            yield req.forbidden('Unsupported service %s' % service)
160
167
            return
161
168
        req.nocache()
162
 
        req.respond(HTTP_OK, 'application/x-%s-advertisement' % service)
163
 
        output = StringIO()
164
 
        proto = ReceivableProtocol(StringIO().read, output.write)
 
169
        write = req.respond(HTTP_OK, 'application/x-%s-advertisement' % service)
 
170
        proto = ReceivableProtocol(StringIO().read, write)
165
171
        handler = handler_cls(backend, [url_prefix(mat)], proto,
166
172
                              stateless_rpc=True, advertise_refs=True)
167
173
        handler.proto.write_pkt_line('# service=%s\n' % service)
168
174
        handler.proto.write_pkt_line(None)
169
175
        handler.handle()
170
 
        yield output.getvalue()
171
176
    else:
172
177
        # non-smart fallback
173
178
        # TODO: select_getanyfile() (see http-backend.c)
230
235
        yield req.forbidden('Unsupported service %s' % service)
231
236
        return
232
237
    req.nocache()
233
 
    req.respond(HTTP_OK, 'application/x-%s-response' % service)
 
238
    write = req.respond(HTTP_OK, 'application/x-%s-response' % service)
234
239
 
235
 
    output = StringIO()
236
240
    input = req.environ['wsgi.input']
237
241
    # This is not necessary if this app is run from a conforming WSGI server.
238
242
    # Unfortunately, there's no way to tell that at this point.
239
243
    # TODO: git may used HTTP/1.1 chunked encoding instead of specifying
240
244
    # content-length
241
 
    if 'CONTENT_LENGTH' in req.environ:
242
 
        input = _LengthLimitedFile(input, int(req.environ['CONTENT_LENGTH']))
243
 
    proto = ReceivableProtocol(input.read, output.write)
 
245
    content_length = req.environ.get('CONTENT_LENGTH', '')
 
246
    if content_length:
 
247
        input = _LengthLimitedFile(input, int(content_length))
 
248
    proto = ReceivableProtocol(input.read, write)
244
249
    handler = handler_cls(backend, [url_prefix(mat)], proto, stateless_rpc=True)
245
250
    handler.handle()
246
 
    yield output.getvalue()
247
251
 
248
252
 
249
253
class HTTPGitRequest(object):
255
259
    def __init__(self, environ, start_response, dumb=False, handlers=None):
256
260
        self.environ = environ
257
261
        self.dumb = dumb
258
 
        self.handlers = handlers and handlers or DEFAULT_HANDLERS
 
262
        self.handlers = handlers
259
263
        self._start_response = start_response
260
264
        self._cache_headers = []
261
265
        self._headers = []
272
276
            self._headers.append(('Content-Type', content_type))
273
277
        self._headers.extend(self._cache_headers)
274
278
 
275
 
        self._start_response(status, self._headers)
 
279
        return self._start_response(status, self._headers)
276
280
 
277
281
    def not_found(self, message):
278
282
        """Begin a HTTP 404 response and return the text of a message."""
288
292
        self.respond(HTTP_FORBIDDEN, 'text/plain')
289
293
        return message
290
294
 
 
295
    def error(self, message):
 
296
        """Begin a HTTP 500 response and return the text of a message."""
 
297
        self._cache_headers = []
 
298
        logger.error('Error: %s', message)
 
299
        self.respond(HTTP_ERROR, 'text/plain')
 
300
        return message
 
301
 
291
302
    def nocache(self):
292
303
        """Set the response to never be cached by the client."""
293
304
        self._cache_headers = [
329
340
    def __init__(self, backend, dumb=False, handlers=None):
330
341
        self.backend = backend
331
342
        self.dumb = dumb
332
 
        self.handlers = handlers
 
343
        self.handlers = dict(DEFAULT_HANDLERS)
 
344
        if handlers is not None:
 
345
            self.handlers.update(handlers)
333
346
 
334
347
    def __call__(self, environ, start_response):
335
348
        path = environ['PATH_INFO']