예제 #1
0
    def json_application(self, http, about, json):
        u"try to dispatch requests to actions, produce a JSON object"
        try:
            status = self.json_actions.get(http.uri_about[-1],
                                           json_noop)(self, http, about, json)
        except:
            json[u"exception"] = self.loginfo_traceback()
            return http(500, CONTENT_TYPE_JSON,
                        producer.Simple(json_encode(json)))

        return http(status, CONTENT_TYPE_JSON,
                    producer.Simple(json_encode(json)))
예제 #2
0
def mailto(mail_from, rcpt_to, body, headers=None, continuation=None):
    if type(body) == str:
        body = producer.Simple(body)
    domains = {}
    for address in rcpt_to:
        domains.setdefault(address.split('@', 1)[1], []).append(address)
    if len(domains) == 1:
        connect(domains.keys()[0])(mail_from, rcpt_to, body,
                                   headers).finalization = continuation
        return

    for domain, rcpt_to in domains.items():
        connect(domain)(mail_from, rcpt_to, producer.Tee(body),
                        headers).finalization = continuation
예제 #3
0
    def __call__(self, response, headers=(), body=None):
        self.response = response
        head = (
            '%s %d %s\r\n'
            'Date: %s\r\n'
            'Server: Allegra\r\n' %
            (self.request[2], response, RESPONSES[response], self.time
             or strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime(time()))
             )  # supports async HTTP formated time
        )
        # Complete the HTTP response producer
        self.producer_headers.update(headers)
        if body == None and (self.request[0] in ('GET', 'POST')  # and
                             # response != 204 # No content
                             ):
            # Supply a response entity if one is required for the
            # request's method and that none has been assigned.
            self.producer_body = producer.Simple(
                _response(self, response, head))
        else:
            self.producer_body = body  # Use the given body.
        if self.request[2] == 'HTTP/1.1':
            # Allways use Chunked Transfer-Encoding for HTTP/1.1!
            if self.producer_body != None:
                self.producer_headers['Transfer-Encoding'] = 'chunked'
                self.producer_body = \
                        http_reactor.Chunk_producer (
                                self.producer_body
                                )
        else:
            # Do not keep-alive without chunk-encoding!
            self.producer_headers['Connection'] = 'close'
        # Build the response head with the reactor's MIME headers,
        # the channel's HTTP version the reactor's HTTP response.
        self.producer_lines = mime_headers.lines(
            self.producer_headers
        )  # TODO: Revise the MIME_producer API as generator ?
        self.producer_lines.insert(0, head)
        if self.producer_headers.get('Connection') == 'close':
            # close socket when done ...
            self.dispatcher.output_fifo.append(None)
            return True  # ... stall input now!

        return False  # let the http_server.Dispatcher continue ...
예제 #4
0
def mime_cache(filename, cache):
    subject = unicode(os.path.basename(filename), 'UTF-8')
    try:
        metadata = os.stat(filename)
    except:
        metadata = None
    if metadata == None or not stat.S_ISREG(metadata[0]):
        return subject, None

    headers = [
        ('Last-Modified',
         (time.asctime(time.gmtime(metadata[7])) + (' %d' % time.timezone))),
        ('Content-length', '%d' % metadata[1])
    ]
    ct, ce = mimetypes.guess_type(filename)
    if ce:
        headers.append(
            ('Content-Type', '%s; charset=%s' % (ct or 'text/plain', ce)))
    else:
        headers.append(('Content-Type', ct or 'application/octet-stream'))
    teed = producer.Simple(open(filename, 'rb').read())
    teed.mime_headers = headers
    cache[subject] = teed
    return subject, teed
예제 #5
0
# URL and push an HTTP reactor with the given command and URL path,
# close when done ...
#
if C * R > 1:
    collect = collector.DEVNULL
else:
    collect = collector.LOGINFO
t_instanciated = time.clock()
http = http_client.Connections()
for i in range(C):
    pipeline = http(host, port)
    for j in range(R):
        if method == 'GET':
            http_client.GET(pipeline, urlpath)(collect)
        elif method == 'POST':
            http_client.POST(pipeline, urlpath, producer.Simple(body))(collect)
    del pipeline
t_instanciated = time.clock() - t_instanciated
t_completed = time.clock()
async_loop.dispatch()
t_completed = time.clock() - t_completed
loginfo.log(
    'Completed in %f seconds, '
    'at the average rate of %f seconds per requests, '
    'or %f requests per seconds. '
    'Note that it took %f seconds to instanciate '
    'the %d client channels and %d requests.' %
    (t_completed, t_completed / (C * R),
     (C * R) / t_completed, t_instanciated, C, R), 'info')
#
# Note: