Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[shibboleth/cpp-xmltooling.git] / xmltooling / io / HTTPResponse.h
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * @file xmltooling/io/HTTPResponse.h
23  * 
24  * Interface to HTTP responses.
25  */
26
27 #ifndef __xmltooling_httpres_h__
28 #define __xmltooling_httpres_h__
29
30 #include <xmltooling/io/GenericResponse.h>
31
32 #include <string>
33 #include <vector>
34
35 namespace xmltooling {
36
37 #if defined (_MSC_VER)
38     #pragma warning( push )
39     #pragma warning( disable : 4251 )
40 #endif
41
42     /**
43      * Interface to HTTP response.
44      * 
45      * <p>To supply information to the surrounding web server environment,
46      * a shim must be supplied in the form of this interface to adapt the
47      * library to different proprietary server APIs.
48      * 
49      * <p>This interface need not be threadsafe.
50      */
51     class XMLTOOL_API HTTPResponse : public GenericResponse {
52     protected:
53         HTTPResponse();
54     public:
55         virtual ~HTTPResponse();
56         
57         void setContentType(const char* type);
58         
59         /**
60          * Sets or clears a response header.
61          * 
62          * @param name  header name
63          * @param value value to set, or nullptr to clear
64          */
65         virtual void setResponseHeader(const char* name, const char* value);
66
67         /**
68          * Sets a client cookie.
69          * 
70          * @param name  cookie name
71          * @param value value to set, or nullptr to clear
72          */
73         virtual void setCookie(const char* name, const char* value);
74         
75         /**
76          * Redirect the client to the specified URL and complete the response.
77          * 
78          * <p>Any headers previously set will be sent ahead of the redirect.
79          *
80          * <p>The URL will be validated with the sanitizeURL method below.
81          *
82          * @param url   location to redirect client
83          * @return a result code to return from the calling MessageEncoder
84          */
85         virtual long sendRedirect(const char* url);
86         
87         /** Some common HTTP status codes. */
88         enum status_t {
89             XMLTOOLING_HTTP_STATUS_OK = 200,
90             XMLTOOLING_HTTP_STATUS_MOVED = 302,
91             XMLTOOLING_HTTP_STATUS_NOTMODIFIED = 304,
92             XMLTOOLING_HTTP_STATUS_UNAUTHORIZED = 401,
93             XMLTOOLING_HTTP_STATUS_FORBIDDEN = 403,
94             XMLTOOLING_HTTP_STATUS_NOTFOUND = 404,
95             XMLTOOLING_HTTP_STATUS_ERROR = 500
96         };
97         
98         long sendError(std::istream& inputStream);
99
100         using GenericResponse::sendResponse;
101         long sendResponse(std::istream& inputStream);
102
103         /**
104          * Returns a modifiable array of schemes to permit in sanitized URLs.
105          *
106          * <p>Updates to this array must be externally synchronized with any use
107          * of this class or its subclasses.
108          *
109          * @return  a mutable array of strings containing the schemes to permit
110          */
111         static std::vector<std::string>& getAllowedSchemes();
112
113         /**
114          * Manually check for unsafe URLs vulnerable to injection attacks.
115          *
116          * @param url   location to check
117          */
118         static void sanitizeURL(const char* url);
119
120     private:
121         static std::vector<std::string> m_allowedSchemes;
122     };
123
124 #if defined (_MSC_VER)
125     #pragma warning( pop )
126 #endif
127 };
128
129 #endif /* __xmltooling_httpres_h__ */