From a515cbbd95d598309011ba1de134017395b163d7 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Sat, 19 May 2007 18:04:53 +0000 Subject: [PATCH] Moved in request/response interfaces from opensaml. --- xmltooling/Makefile.am | 6 +- xmltooling/XMLToolingConfig.cpp | 2 + xmltooling/io/GenericRequest.h | 150 ++++++++++++++++++++++++++++++++++++++++ xmltooling/io/GenericResponse.h | 79 +++++++++++++++++++++ xmltooling/io/HTTPRequest.h | 89 ++++++++++++++++++++++++ xmltooling/io/HTTPResponse.h | 99 ++++++++++++++++++++++++++ xmltooling/xmltooling.vcproj | 16 +++++ 7 files changed, 440 insertions(+), 1 deletion(-) create mode 100644 xmltooling/io/GenericRequest.h create mode 100644 xmltooling/io/GenericResponse.h create mode 100644 xmltooling/io/HTTPRequest.h create mode 100644 xmltooling/io/HTTPResponse.h diff --git a/xmltooling/Makefile.am b/xmltooling/Makefile.am index 0703030..a2ae0dc 100644 --- a/xmltooling/Makefile.am +++ b/xmltooling/Makefile.am @@ -58,7 +58,11 @@ implinclude_HEADERS = \ ioinclude_HEADERS = \ io/AbstractXMLObjectMarshaller.h \ - io/AbstractXMLObjectUnmarshaller.h + io/AbstractXMLObjectUnmarshaller.h \ + io/GenericRequest.h \ + io/GenericResponse.h \ + io/HTTPRequest.h \ + io/HTTPResponse.h secinclude_HEADERS = \ security/AbstractPKIXTrustEngine.h \ diff --git a/xmltooling/XMLToolingConfig.cpp b/xmltooling/XMLToolingConfig.cpp index 80a89fd..4e06f3e 100644 --- a/xmltooling/XMLToolingConfig.cpp +++ b/xmltooling/XMLToolingConfig.cpp @@ -25,6 +25,8 @@ #include "XMLToolingConfig.h" #include "encryption/Encryption.h" #include "encryption/Encrypter.h" +#include "io/HTTPRequest.h" +#include "io/HTTPResponse.h" #include "impl/UnknownElement.h" #include "security/TrustEngine.h" #include "security/OpenSSLCryptoX509CRL.h" diff --git a/xmltooling/io/GenericRequest.h b/xmltooling/io/GenericRequest.h new file mode 100644 index 0000000..af983d5 --- /dev/null +++ b/xmltooling/io/GenericRequest.h @@ -0,0 +1,150 @@ +/* + * Copyright 2001-2007 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file xmltooling/io/GenericRequest.h + * + * Interface to generic protocol requests that transport XML messages. + */ + +#ifndef __xmltooling_genreq_h__ +#define __xmltooling_genreq_h__ + +#include + +#include +#include + +#ifndef XMLTOOLING_NO_XMLSEC +# include +#endif + +namespace xmltooling { + + /** + * Interface to generic protocol requests that transport XML messages. + * + *

This interface need not be threadsafe. + */ + class XMLTOOL_API GenericRequest { + MAKE_NONCOPYABLE(GenericRequest); + protected: + GenericRequest() {} + public: + virtual ~GenericRequest() {} + + /** + * Returns the URL scheme of the request (http, https, ftp, ldap, etc.) + * + * @return the URL scheme + */ + virtual const char* getScheme() const=0; + + /** + * Returns true iff the request is over a confidential channel. + * + * @return confidential channel indicator + */ + virtual bool isSecure() const=0; + + /** + * Returns hostname of service that received request. + * + * @return hostname of service + */ + virtual const char* getHostname() const=0; + + /** + * Returns incoming port. + * + * @return incoming port + */ + virtual int getPort() const=0; + + /** + * Returns the MIME type of the request, if known. + * + * @return the MIME type, or an empty string + */ + virtual std::string getContentType() const=0; + + /** + * Returns the length of the request body, if known. + * + * @return the content length, or -1 if unknown + */ + virtual long getContentLength() const=0; + + /** + * Returns the raw request body. + * + * @return the request body, or NULL + */ + virtual const char* getRequestBody() const=0; + + /** + * Returns a decoded named parameter value from the request. + * If a parameter has multiple values, only one will be returned. + * + * @param name the name of the parameter to return + * @return a single parameter value or NULL + */ + virtual const char* getParameter(const char* name) const=0; + + /** + * Returns all of the decoded values of a named parameter from the request. + * All values found will be returned. + * + * @param name the name of the parameter to return + * @param values a vector in which to return pointers to the decoded values + * @return the number of values returned + */ + virtual std::vector::size_type getParameters( + const char* name, std::vector& values + ) const=0; + + /** + * Returns the transport-authenticated identity associated with the request, + * if authentication is solely handled by the transport. + * + * @return the authenticated username or an empty string + */ + virtual std::string getRemoteUser() const=0; + + /** + * Returns the IP address of the client. + * + * @return the client's IP address + */ + virtual std::string getRemoteAddr() const=0; + + /** + * Returns the chain of certificates sent by the client. + * They are not guaranteed to be valid according to any particular definition. + * + * @return the client's certificate chain + */ + virtual const +#ifndef XMLTOOLING_NO_XMLSEC + std::vector& +#else + std::vector& +#endif + getClientCertificates() const=0; + }; +}; + +#endif /* __xmltooling_genreq_h__ */ diff --git a/xmltooling/io/GenericResponse.h b/xmltooling/io/GenericResponse.h new file mode 100644 index 0000000..57a459d --- /dev/null +++ b/xmltooling/io/GenericResponse.h @@ -0,0 +1,79 @@ +/* + * Copyright 2001-2007 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file xmltooling/io/GenericResponse.h + * + * Interface to generic protocol responses that transport XML messages. + */ + +#ifndef __xmltooling_genres_h__ +#define __xmltooling_genres_h__ + +#include +#include + +namespace xmltooling { + + /** + * Interface to generic protocol responses that transport XML messages. + * + *

This interface need not be threadsafe. + */ + class XMLTOOL_API GenericResponse { + MAKE_NONCOPYABLE(GenericResponse); + protected: + GenericResponse() {} + public: + virtual ~GenericResponse() {} + + /** + * Sets or clears the MIME type of the response. + * + * @param type the MIME type, or NULL to clear + */ + virtual void setContentType(const char* type=NULL)=0; + + /** + * Sends a completed response to the client along with a + * transport-specific "OK" indication. Used for "normal" responses. + * + * @param inputStream reference to source of response data + * @return a result code to return from the calling MessageEncoder + */ + virtual long sendResponse(std::istream& inputStream)=0; + + /** + * Sends an "error" response to the client along with a + * transport-specific error indication. + * + * @param inputStream reference to source of response data + * @return a result code to return from the calling MessageEncoder + */ + virtual long sendError(std::istream& inputStream)=0; + + /** + * Sends a completed response to the client. + * + * @param inputStream reference to source of response data + * @param status transport-specific status to return + * @return a result code to return from the calling MessageEncoder + */ + virtual long sendResponse(std::istream& inputStream, long status)=0; + }; +}; + +#endif /* __xmltooling_genres_h__ */ diff --git a/xmltooling/io/HTTPRequest.h b/xmltooling/io/HTTPRequest.h new file mode 100644 index 0000000..caee6d5 --- /dev/null +++ b/xmltooling/io/HTTPRequest.h @@ -0,0 +1,89 @@ +/* + * Copyright 2001-2007 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file xmltooling/io/HTTPRequest.h + * + * Interface to HTTP requests + */ + +#ifndef __xmltooling_httpreq_h__ +#define __xmltooling_httpreq_h__ + +#include + +namespace xmltooling { + + /** + * Interface to HTTP requests. + * + *

To supply information from the surrounding web server environment, + * a shim must be supplied in the form of this interface to adapt the + * library to different proprietary server APIs. + * + *

This interface need not be threadsafe. + */ + class XMLTOOL_API HTTPRequest : public GenericRequest { + protected: + HTTPRequest() {} + public: + virtual ~HTTPRequest() {} + + bool isSecure() const { + return strcmp(getScheme(),"https")==0; + } + + /** + * Returns the HTTP method of the request (GET, POST, etc.) + * + * @return the HTTP method + */ + virtual const char* getMethod() const=0; + + /** + * Returns the request URI. + * + * @return the request URI + */ + virtual const char* getRequestURI() const=0; + + /** + * Returns the complete request URL, including scheme, host, port, and URI. + * + * @return the request URL + */ + virtual const char* getRequestURL() const=0; + + /** + * Returns the HTTP query string appened to the request. The query + * string is returned without any decoding applied, everything found + * after the ? delimiter. + * + * @return the query string + */ + virtual const char* getQueryString() const=0; + + /** + * Returns a request header value. + * + * @param name the name of the header to return + * @return the header's value, or an empty string + */ + virtual std::string getHeader(const char* name) const=0; + }; +}; + +#endif /* __xmltooling_httpreq_h__ */ diff --git a/xmltooling/io/HTTPResponse.h b/xmltooling/io/HTTPResponse.h new file mode 100644 index 0000000..63e1f4b --- /dev/null +++ b/xmltooling/io/HTTPResponse.h @@ -0,0 +1,99 @@ +/* + * Copyright 2001-2007 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file xmltooling/io/HTTPResponse.h + * + * Interface to HTTP response. + */ + +#ifndef __xmltooling_httpres_h__ +#define __xmltooling_httpres_h__ + +#include + +namespace xmltooling { + + /** + * Interface to HTTP response. + * + *

To supply information to the surrounding web server environment, + * a shim must be supplied in the form of this interface to adapt the + * library to different proprietary server APIs. + * + *

This interface need not be threadsafe. + */ + class XMLTOOL_API HTTPResponse : public GenericResponse { + protected: + HTTPResponse() {} + public: + virtual ~HTTPResponse() {} + + void setContentType(const char* type) { + setResponseHeader("Content-Type", type); + } + + /** + * Sets or clears a response header. + * + * @param name header name + * @param value value to set, or NULL to clear + */ + virtual void setResponseHeader(const char* name, const char* value)=0; + + /** + * Sets a client cookie. + * + * @param name cookie name + * @param value value to set, or NULL to clear + */ + virtual void setCookie(const char* name, const char* value) { + std::string cookie(name); + cookie = cookie + '=' + value; + setResponseHeader("Set-Cookie", cookie.c_str()); + } + + /** + * Redirect the client to the specified URL and complete the response. + * Any headers previously set will be sent ahead of the redirect. + * + * @param url location to redirect client + * @return a result code to return from the calling MessageEncoder + */ + virtual long sendRedirect(const char* url)=0; + + /** Some common HTTP status codes. */ + enum status_t { + XMLTOOLING_HTTP_STATUS_OK = 200, + XMLTOOLING_HTTP_STATUS_MOVED = 302, + XMLTOOLING_HTTP_STATUS_FORBIDDEN = 403, + XMLTOOLING_HTTP_STATUS_NOTFOUND = 404, + XMLTOOLING_HTTP_STATUS_ERROR = 500 + }; + + using GenericResponse::sendResponse; + + long sendError(std::istream& inputStream) { + return sendResponse(inputStream, XMLTOOLING_HTTP_STATUS_ERROR); + } + + long sendResponse(std::istream& inputStream) { + return sendResponse(inputStream, XMLTOOLING_HTTP_STATUS_OK); + } + }; +}; + +#endif /* __xmltooling_httpres_h__ */ diff --git a/xmltooling/xmltooling.vcproj b/xmltooling/xmltooling.vcproj index 4e94427..a94c737 100644 --- a/xmltooling/xmltooling.vcproj +++ b/xmltooling/xmltooling.vcproj @@ -596,6 +596,22 @@ RelativePath=".\io\AbstractXMLObjectUnmarshaller.h" > + + + + + + + +