1 //
2 // Copyright (c) 2006, Brian Frank and Andy Frank
3 // Licensed under the Academic Free License version 3.0
4 //
5 // History:
6 // 15 Mar 06 Andy Frank Creation
7 //
8
9 **
10 ** WebRes encapsulates a response to a web request.
11 **
12 abstract class WebRes
13 {
14
15 //////////////////////////////////////////////////////////////////////////
16 // Public
17 //////////////////////////////////////////////////////////////////////////
18
19 **
20 ** Get the WebService managing the request.
21 **
22 abstract WebService service()
23
24 **
25 ** Get or set the HTTP status code for this response. Status code
26 ** defaults to 200. If response has already been committed, throws Err.
27 ** If status code passed in is not recognized, throws Err.
28 **
29 abstract Int statusCode
30
31 **
32 ** Map of HTTP response headers. You must set all headers before
33 ** you access out() for the first time, which commits the response.
34 ** After the response is commited this map becomes read only.
35 **
abstract Str:Str headers()
37
38 **
39 ** Return true if this response has been commmited. A committed
40 ** response has written its response headers, and can no longer
41 ** modify its status code or headers. A response is committed
42 ** the first time that `out` is called.
43 **
44 abstract Bool isCommitted()
45
46 **
47 ** Return the WebOutStream for this response. The first time this
48 ** method is accessed the response is committed: all headers
49 ** currently set will be written to the stream, and can no longer
50 ** be modified.
51 **
52 abstract WebOutStream out()
53
54 **
55 ** Send a redirect response to the client using the specified status
56 ** code and url. If this response has already been committed this
57 ** method throws an Err.
58 **
59 abstract Void redirect(Int statusCode, Uri uri)
60
61 **
62 ** Send an error response to client using the specified status and
63 ** HTML formatted message. If this response has already been committed
64 ** this method throws an Err. If the server has a preconfigured page
65 ** for this error code, it will trump the message passed in.
66 **
67 abstract Void sendError(Int statusCode, Str msg := null)
68
69 //////////////////////////////////////////////////////////////////////////
70 // Static
71 //////////////////////////////////////////////////////////////////////////
72
73 **
74 ** Map of HTTP status codes to status messages.
75 **
76 static const Int:Str statusMsg :=
77 [
78 // 100
79 100: "Continue",
80 101: "Switching Protocols",
81 // 200
82 200: "OK",
83 201: "Created",
84 202: "Accepted",
85 203: "203 Non-Authoritative Information",
86 204: "No Content",
87 205: "Reset Content",
88 206: "Partial Content",
89 // 300
90 300: "Multiple Choices",
91 301: "Moved Permanently",
92 302: "Found",
93 303: "See Other",
94 304: "Not Modified",
95 305: "Use Proxy",
96 307: "Temporary Redirect",
97 // 400
98 400: "Bad Request",
99 401: "Unauthorized",
100 402: "Payment Required",
101 403: "Forbidden",
102 404: "Not Found",
103 405: "Method Not Allowed",
104 406: "Not Acceptable",
105 407: "Proxy Authentication Required",
106 408: "Request Timeout",
107 409: "Conflict",
108 410: "Gone",
109 411: "Length Required",
110 412: "Precondition Failed",
111 413: "Request Entity Too Large",
112 414: "Request-URI Too Long",
113 415: "Unsupported Media Type",
114 416: "Requested Range Not Satisfiable",
115 417: "Expectation Failed",
116 // 500
117 500: "Internal Server Error",
118 501: "Not Implemented",
119 502: "Bad Gateway",
120 503: "Service Unavailable",
121 504: "Gateway Timeout",
122 505: "HTTP Version Not Supported"
123 ].toImmutable
124
125 }