Multi-line svn commit, see body.
[shibboleth/cpp-opensaml.git] / saml / binding / HTTPResponse.h
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file saml/binding/HTTPResponse.h
19  * 
20  * Interface to HTTP requests 
21  */
22
23 #ifndef __saml_httpres_h__
24 #define __saml_httpres_h__
25
26 #include <saml/binding/GenericResponse.h>
27
28 namespace opensaml {
29     
30     /**
31      * Interface to caller-supplied shim for issuing an HTTP response.
32      * 
33      * <p>To supply information to the surrounding web server environment,
34      * a shim must be supplied in the form of this interface to adapt the
35      * library to different proprietary server APIs.
36      * 
37      * <p>This interface need not be threadsafe.
38      */
39     class SAML_API HTTPResponse : public GenericResponse {
40         MAKE_NONCOPYABLE(HTTPResponse);
41     protected:
42         HTTPResponse() {}
43     public:
44         virtual ~HTTPResponse() {}
45         
46         void setContentType(const char* type) {
47             setResponseHeader("Content-Type", type);
48         }
49         
50         /**
51          * Sets or clears a response header.
52          * 
53          * @param name  header name
54          * @param value value to set, or NULL to clear
55          */
56         virtual void setResponseHeader(const char* name, const char* value)=0;
57
58         /**
59          * Sets a client cookie.
60          * 
61          * @param name  cookie name
62          * @param value value to set, or NULL to clear
63          */
64         virtual void setCookie(const char* name, const char* value) {
65             std::string cookie(name);
66             cookie = cookie + '=' + value;
67             setResponseHeader("Set-Cookie", cookie.c_str());
68         }
69         
70         /**
71          * Redirect the client to the specified URL and complete the response.
72          * Any headers previously set will be sent ahead of the redirect.
73          * 
74          * @param url   location to redirect client
75          * @return a result code to return from the calling MessageEncoder
76          */
77         virtual long sendRedirect(const char* url)=0;
78         
79         /** Some common HTTP status codes. */
80         enum status_t {
81             SAML_HTTP_STATUS_OK = 200,
82             SAML_HTTP_STATUS_MOVED = 302,
83             SAML_HTTP_STATUS_FORBIDDEN = 403,
84             SAML_HTTP_STATUS_NOTFOUND = 404,
85             SAML_HTTP_STATUS_ERROR = 500
86         };
87         
88         using GenericResponse::sendResponse;
89
90         long sendError(std::istream& inputStream) {
91             return sendResponse(inputStream, SAML_HTTP_STATUS_ERROR);
92         }
93
94         long sendResponse(std::istream& inputStream) {
95             return sendResponse(inputStream, SAML_HTTP_STATUS_OK);
96         }
97     };
98 };
99
100 #endif /* __saml_httpres_h__ */