Opened 10 years ago

Last modified 9 years ago

#2983 enhancement new

— at Separate the concerns in twisted.web request processing to allow easier testabilityVersion 1

Reported by: radix Owned by: radix
Priority: normal Milestone:
Component: web Keywords:
Cc: Branch:
Author: radix

Description (last modified by radix)

We want twisted.web to have better unit tests, and we also want to be able to use twisted.web request-processing code easily in our unit tests for our resources. We should improve this situation by separating the concerns in request processing to allow using fewer parts of it at once.

The change proposed here is basically separating the concepts of Request and Response, and moving the data serialization code to the HTTP Channel (i.e. the protocol implementation).

This is kind of a major change for twisted.web, but we would like to do it without breaking backwards compatibility.

Here's an initial pass at some (incomplete) interfaces for this change. I might modify the specifics while maintaining the spirit of the refactoring. The idea is that the HTTP Channel will pass requests off to its "HTTP Request Receiver" object. This will likely be the L{twisted.web.server.Site}. This will look up a resource for the request, render it, and return a Response object. The channel will then tell the response to give it the data by calling sendResponse on it; the response will write the data (optionally registering itself as a producer) to the channel in a protocol-agnostic manner (i.e., chunking and stuff will be the responsibility of the channel).

At the end of this change, Request should have no required parameters. Of course, it will still remain compatible with guys who pass arguments to it, and directly call request.write and request.finish; these will be implemented by creating a BackwardsCompatibilityResponse at request._bcResponse. request.write and request.finish will probably require having a request that had a channel passed to it.

class IHTTPResponse(Interface):

    def sendResponse(channel):
        Send data to the given C{channel} by calling its C{writeHeader},
        C{write} and C{finish} methods.

        @param channel: L{IHTTPChannel} provider.

class IHTTPChannel(Interface):

    def writeHeader(self, key, value):
        Write a header.

        Once any of the other methods in this interface are called, this method
        can no longer be called.

    def writeBody(self, data):
        Write a chunk of body data.

    def finishRequest(self):
        Finish the current request.

    def registerProducerForResponseBody(self, producer):
        Register the given C{producer} to receive events as per the
        L{twisted.internet.interfaces.IProducer} interface.

    def setHTTPRequestReceiver(self, receiver):
        @param receiver: The request receiver that will be notified when
            requests are made.
        @type receiver: L{IHTTPRequestReceiver}.

class IHTTPRequestReceiver(Interface):

    def requestReceived(self, request):
        Process a request and return an L{IHTTPResponse}.

        @param request: The request to respond to
        @type request: L{IHTTPRequest}

        @return: The HTTP response object that will be asked to send its
            response to the channel.
        @rtype: L{IHTTPResponse}

Change History (1)

comment:1 Changed 10 years ago by radix

Description: modified (diff)
Note: See TracTickets for help on using tickets.