6e4514fe29e5b59b2e2205c878d6ddb15686edbd
[shibboleth/cpp-xmltooling.git] / xmltooling / io / HTTPResponse.h
1 /*
2  *  Copyright 2001-2010 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 xmltooling/io/HTTPResponse.h
19  * 
20  * Interface to HTTP responses.
21  */
22
23 #ifndef __xmltooling_httpres_h__
24 #define __xmltooling_httpres_h__
25
26 #include <xmltooling/io/GenericResponse.h>
27
28 #include <string>
29 #include <vector>
30
31 namespace xmltooling {
32
33 #if defined (_MSC_VER)
34     #pragma warning( push )
35     #pragma warning( disable : 4251 )
36 #endif
37
38     /**
39      * Interface to HTTP response.
40      * 
41      * <p>To supply information to the surrounding web server environment,
42      * a shim must be supplied in the form of this interface to adapt the
43      * library to different proprietary server APIs.
44      * 
45      * <p>This interface need not be threadsafe.
46      */
47     class XMLTOOL_API HTTPResponse : public GenericResponse {
48     protected:
49         HTTPResponse();
50     public:
51         virtual ~HTTPResponse();
52         
53         void setContentType(const char* type);
54         
55         /**
56          * Sets or clears a response header.
57          * 
58          * @param name  header name
59          * @param value value to set, or nullptr to clear
60          */
61         virtual void setResponseHeader(const char* name, const char* value);
62
63         /**
64          * Sets a client cookie.
65          * 
66          * @param name  cookie name
67          * @param value value to set, or nullptr to clear
68          */
69         virtual void setCookie(const char* name, const char* value);
70         
71         /**
72          * Redirect the client to the specified URL and complete the response.
73          * 
74          * <p>Any headers previously set will be sent ahead of the redirect.
75          *
76          * <p>The URL will be validated with the sanitizeURL method below.
77          *
78          * @param url   location to redirect client
79          * @return a result code to return from the calling MessageEncoder
80          */
81         virtual long sendRedirect(const char* url);
82         
83         /** Some common HTTP status codes. */
84         enum status_t {
85             XMLTOOLING_HTTP_STATUS_OK = 200,
86             XMLTOOLING_HTTP_STATUS_MOVED = 302,
87             XMLTOOLING_HTTP_STATUS_NOTMODIFIED = 304,
88             XMLTOOLING_HTTP_STATUS_UNAUTHORIZED = 401,
89             XMLTOOLING_HTTP_STATUS_FORBIDDEN = 403,
90             XMLTOOLING_HTTP_STATUS_NOTFOUND = 404,
91             XMLTOOLING_HTTP_STATUS_ERROR = 500
92         };
93         
94         long sendError(std::istream& inputStream);
95
96         using GenericResponse::sendResponse;
97         long sendResponse(std::istream& inputStream);
98
99         /**
100          * Returns a modifiable array of schemes to permit in sanitized URLs.
101          *
102          * <p>Updates to this array must be externally synchronized with any use
103          * of this class or its subclasses.
104          *
105          * @return  a mutable array of strings containing the schemes to permit
106          */
107         static std::vector<std::string>& getAllowedSchemes();
108
109         /**
110          * Manually check for unsafe URLs vulnerable to injection attacks.
111          *
112          * @param url   location to check
113          */
114         static void sanitizeURL(const char* url);
115
116     private:
117         static std::vector<std::string> m_allowedSchemes;
118     };
119
120 #if defined (_MSC_VER)
121     #pragma warning( pop )
122 #endif
123 };
124
125 #endif /* __xmltooling_httpres_h__ */