Debian Codegraph @

testing/web-platform/tests/tools/wptserve/docs/handlers.rst

raw
1 Handlers
2 ========
3
4 Handlers are functions that have the general signature::
5
6 handler(request, response)
7
8 It is expected that the handler will use information from
9 the request (e.g. the path) either to populate the response
10 object with the data to send, or to directly write to the
11 output stream via the ResponseWriter instance associated with
12 the request. If a handler writes to the output stream then the
13 server will not attempt additional writes, i.e. the choice to write
14 directly in the handler or not is all-or-nothing.
15
16 A number of general-purpose handler functions are provided by default:
17
18 .. _handlers.Python:
19
20 Python Handlers
21 ---------------
22
23 Python handlers are functions which provide a higher-level API over
24 manually updating the response object, by causing the return value of
25 the function to provide (part of) the response. There are four
26 possible sets of values that may be returned::
27
28
29 ((status_code, reason), headers, content)
30 (status_code, headers, content)
31 (headers, content)
32 content
33
34 Here `status_code` is an integer status code, `headers` is a list of (field
35 name, value) pairs, and `content` is a string or an iterable returning strings.
36 Such a function may also update the response manually. For example one may use
37 `response.headers.set` to set a response header, and only return the content.
38 One may even use this kind of handler, but manipulate the output socket
39 directly, in which case the return value of the function, and the properties of
40 the response object, will be ignored.
41
42 The most common way to make a user function into a python handler is
43 to use the provided `wptserve.handlers.handler` decorator::
44
45 from wptserve.handlers import handler
46
47 @handler
48 def test(request, response):
49 return [("X-Test": "PASS"), ("Content-Type", "text/plain")], "test"
50
51 #Later, assuming we have a Router object called 'router'
52
53 router.register("GET", "/test", test)
54
55 JSON Handlers
56 -------------
57
58 This is a specialisation of the python handler type specifically
59 designed to facilitate providing JSON responses. The API is largely
60 the same as for a normal python handler, but the `content` part of the
61 return value is JSON encoded, and a default Content-Type header of
62 `application/json` is added. Again this handler is usually used as a
63 decorator::
64
65 from wptserve.handlers import json_handler
66
67 @json_handler
68 def test(request, response):
69 return {"test": "PASS"}
70
71 Python File Handlers
72 --------------------
73
74 Python file handlers are Python files which the server executes in response to
75 requests made to the corresponding URL. This is hooked up to a route like
76 ``("*", "*.py", python_file_handler)``, meaning that any .py file will be
77 treated as a handler file (note that this makes it easy to write unsafe
78 handlers, particularly when running the server in a web-exposed setting).
79
80 The Python files must define a single function `main` with the signature::
81
82 main(request, response)
83
84 This function then behaves just like those described in
85 :ref:`handlers.Python` above.
86
87 asis Handlers
88 -------------
89
90 These are used to serve files as literal byte streams including the
91 HTTP status line, headers and body. In the default configuration this
92 handler is invoked for all files with a .asis extension.
93
94 File Handlers
95 -------------
96
97 File handlers are used to serve static files. By default the content
98 type of these files is set by examining the file extension. However
99 this can be overridden, or additional headers supplied, by providing a
100 file with the same name as the file being served but an additional
101 .headers suffix, i.e. test.html has its headers set from
102 test.html.headers. The format of the .headers file is plaintext, with
103 each line containing::
104
105 Header-Name: header_value
106
107 In addition headers can be set for a whole directory of files (but not
108 subdirectories), using a file called `__dir__.headers`.