2 * Copyright 2009 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * Interface to HTTP responses.
24 #include "HTTPResponse.h"
26 using namespace xmltooling;
29 GenericResponse::GenericResponse()
33 GenericResponse::~GenericResponse()
37 vector<string> HTTPResponse::m_allowedSchemes;
39 vector<string>& HTTPResponse::getAllowedSchemes()
41 return m_allowedSchemes;
44 void HTTPResponse::sanitizeURL(const char* url)
47 for (ch=url; *ch; ++ch) {
48 if (iscntrl((unsigned char)(*ch))) // convert to unsigned to allow full range from 00-FF
49 throw IOException("URL contained a control character.");
52 ch = strchr(url, ':');
54 throw IOException("URL is malformed.");
55 string s(url, ch - url);
56 for (vector<string>::const_iterator i = m_allowedSchemes.begin(); i != m_allowedSchemes.end(); ++i) {
57 #ifdef HAVE_STRCASECMP
58 if (!strcasecmp(s.c_str(), i->c_str()))
60 if (!stricmp(s.c_str(), i->c_str()))
65 throw IOException("URL contains invalid scheme ($1).", params(1, s.c_str()));
68 HTTPResponse::HTTPResponse()
72 HTTPResponse::~HTTPResponse()
76 void HTTPResponse::setContentType(const char* type)
78 setResponseHeader("Content-Type", type);
81 void HTTPResponse::setCookie(const char* name, const char* value)
84 cookie = cookie + '=' + value;
85 setResponseHeader("Set-Cookie", cookie.c_str());
88 void HTTPResponse::setResponseHeader(const char* name, const char* value)
90 for (const char* ch=name; *ch; ++ch) {
92 throw IOException("Response header name contained a control character.");
95 for (const char* ch=value; *ch; ++ch) {
97 throw IOException("Value for response header ($1) contained a control character.", params(1,name));
101 long HTTPResponse::sendRedirect(const char* url)
104 return XMLTOOLING_HTTP_STATUS_MOVED;
107 long HTTPResponse::sendError(istream& inputStream)
109 return sendResponse(inputStream, XMLTOOLING_HTTP_STATUS_ERROR);
112 long HTTPResponse::sendResponse(istream& inputStream)
114 return sendResponse(inputStream, XMLTOOLING_HTTP_STATUS_OK);