WebStack

docs/responses.html

401:ca2a40e20036
2005-07-16 paulb [project @ 2005-07-16 20:32:56 by paulb] Enhanced the PYTHONPATH handling to respect the existing definition.
     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     2 <html xmlns="http://www.w3.org/1999/xhtml">     3 <head>     4   <title>Responses and Presentation</title>     5   <meta name="generator"     6  content="amaya 8.1a, see http://www.w3.org/Amaya/" />     7   <link href="styles.css" rel="stylesheet" type="text/css" />     8 </head>     9 <body>    10 <h1>Responses and Presentation</h1>    11 <p>After performing some kind of    12 processing on input information, an    13 application will then want to produce some kind of response to indicate    14 what    15 went on. Here are some examples of responses:</p>    16 <ul>    17   <li>Returning the contents of a    18 requested file.</li>    19   <li>Showing a message telling    20 the user that the requested operation succeeded or failed.</li>    21   <li>Presenting a view onto the    22 application with the results of the recent activity shown in a Web page.</li>    23 </ul>    24 <h2>Generating Responses</h2>    25 <p>The procedure involved in    26 generating a response usually involves the    27 following steps:</p>    28 <ol>    29   <li>Setting a response code to    30 signal whether the application performed the requested operation    31 successfully.</li>    32   <li>Setting a content type and a <a>character encoding</a>.</li>    33   <li>Producing content and    34 sending it to the user.</li>    35 </ol>    36 <p>The kind of code involved may    37 well resemble the following:</p>    38 <pre>from WebStack.Generic import ContentType<br /><br />class MyResource:<br />    def respond(self, trans):<br />        [Perform the requested operations.]<br /><br />        if [the operation was successful]:<br />            trans.set_response_code(200)<br />            trans.set_content_type(ContentType("text/html", encoding="utf-8"))<br />            out = trans.get_response_stream()<br />            out.write([some data either as a plain string suitably encoded or as Unicode])<br />        else:<br />            trans.set_response_code(500) # or some other code<br />            trans.set_content_type(ContentType("text/html", encoding="utf-8"))<br />            out = trans.get_response_stream()<br />            out.write([some other data either as a plain string suitably encoded or as Unicode])</pre>    39 <p>As discussed in <a href="encodings.html">"Character Encodings"</a>,    40 care    41 must be taken generating the response so that it meets any expectations    42 that    43 browsers and other Web clients may have.</p>    44 <div class="WebStack">    45 <h3>WebStack API -    46 Response-Related Methods</h3>    47 <p>Transaction objects have    48 various methods that can be used in generating    49 responses:</p>    50 <dl>    51   <dt><code>set_response_code</code></dt>    52   <dd>This accepts an integer    53 value denoting the response condition as described in the HTTP    54 specification. If this method is not used, WebStack sets a <code>200</code>    55 status condition on the response, meaning that the request was    56 processed successfully.</dd>    57   <dt><code>set_content_type</code></dt>    58   <dd>This accepts a content type    59 object (typically <code>WebStack.Generic.ContentType</code>)    60 which specifies both the media type and the character encoding (if    61 relevant) of the data sent to the user. The media type describes the    62 format of the data (eg. <code>text/html</code>    63 - a Web page), whereas the character encoding describes how any    64 character information on the page is encoded - see <a    65  href="encodings.html">"Character Encodings"</a>    66 for more information.</dd>    67   <dt><code>get_response_stream</code></dt>    68   <dd>This returns the output    69 stream through which data may be sent to the user.</dd>    70 </dl>    71 </div>    72 <h2>Ending the Response Explicitly</h2>    73 <p>Although it is possible to produce some output and then to let    74 the&nbsp;<code>respond</code> function complete normally, sometimes it    75 is appropriate to terminate the response and to hand control straight    76 back to the server environment; in other words, to decide that no more    77 activity will be performed within the application and to send the    78 response immediately. Whilst just using a&nbsp;<code>return</code>    79 statement might be adequate in many applications...</p>    80 <pre>        # In the respond method...<br />        if some_condition:<br />            [Produce a response.]<br />            return<br />        [Produce a different response.]</pre>    81 <p>...sometimes a resource's&nbsp;<code>respond</code> method is being    82 called from another resource, and it may be the case that this other    83 resource may produce additional output if control is returned to it.</p>    84 <p>To provide a definitive end of response signal, a special exception    85 is available:</p>    86 <pre>from WebStack.Generic import EndOfResponse<br /><br />[The usual declarations for the resource and the respond method...]<br /><br />        # In the respond method (possibly called by another resource)...<br />        if some_condition:<br />            [Produce a response.]<br />            raise EndOfResponse</pre>    87 <p>This exception, when raised, ensures that the response is sent    88 exactly as the resource intended upon raising the exception. Note that    89 although <code>WebStack.Generic.EndOfResponse</code> is an exception,    90 it will not cause an error condition or change the response code in any    91 way.</p>    92 <h2>Integrating with Content Generators</h2>    93 <p>Just as applications might need to integrate with other systems in    94 order to fetch information or to perform operations on behalf of the    95 user, the generation of response content can also be made a lot easier    96 by using external libraries. In the above example code, the process of    97 obtaining and formatting the actual data to be written out has been    98 left unspecified, but for anything more complicated than "hello world"    99 it is usually advisable to consider using templating systems which   100 combine raw data and templates to produce formatted output that can be   101 displayed as a Web page (amongst other things).</p>   102 <p>See&nbsp;<a href="integrating.html">"Integration with Other Systems"</a>   103 for more information on the principles of using such external libraries.</p>   104 </body>   105 </html>