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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * Interface to HTTP responses.
28 #include "HTTPResponse.h"
30 #include <boost/algorithm/string/predicate.hpp>
31 #include <boost/bind.hpp>
33 using namespace xmltooling;
34 using namespace boost;
37 GenericResponse::GenericResponse()
41 GenericResponse::~GenericResponse()
45 vector<string> HTTPResponse::m_allowedSchemes;
47 vector<string>& HTTPResponse::getAllowedSchemes()
49 return m_allowedSchemes;
52 void HTTPResponse::sanitizeURL(const char* url)
54 // predicate for checking scheme below
55 static bool (*fn)(const string&, const string&, const std::locale&) = iequals;
58 for (ch=url; *ch; ++ch) {
59 if (iscntrl((unsigned char)(*ch))) // convert to unsigned to allow full range from 00-FF
60 throw IOException("URL contained a control character.");
63 ch = strchr(url, ':');
65 throw IOException("URL is malformed.");
66 string s(url, ch - url);
67 vector<string>::const_iterator i =
68 find_if(m_allowedSchemes.begin(), m_allowedSchemes.end(), boost::bind(fn, boost::cref(s), _1, boost::cref(std::locale())));
69 if (i != m_allowedSchemes.end())
72 throw IOException("URL contains invalid scheme ($1).", params(1, s.c_str()));
75 HTTPResponse::HTTPResponse()
79 HTTPResponse::~HTTPResponse()
83 void HTTPResponse::setContentType(const char* type)
85 setResponseHeader("Content-Type", type);
88 void HTTPResponse::setCookie(const char* name, const char* value)
91 cookie = cookie + '=' + value;
92 setResponseHeader("Set-Cookie", cookie.c_str());
95 void HTTPResponse::setResponseHeader(const char* name, const char* value)
97 for (const char* ch=name; *ch; ++ch) {
99 throw IOException("Response header name contained a control character.");
102 for (const char* ch=value; *ch; ++ch) {
104 throw IOException("Value for response header ($1) contained a control character.", params(1,name));
108 long HTTPResponse::sendRedirect(const char* url)
111 return XMLTOOLING_HTTP_STATUS_MOVED;
114 long HTTPResponse::sendError(istream& inputStream)
116 return sendResponse(inputStream, XMLTOOLING_HTTP_STATUS_ERROR);
119 long HTTPResponse::sendResponse(istream& inputStream)
121 return sendResponse(inputStream, XMLTOOLING_HTTP_STATUS_OK);