# HG changeset patch # User Louis Opter # Date 1269220104 -3600 # Node ID 7c8cd74023ce48cecb687ab3af833f7adaa7c480 # Parent a3ad9dfcf01172fc7acc8c1984f471a0a44b4f05 Rework file layout, fix build, add README and SUPPORT diff -r a3ad9dfcf011 -r 7c8cd74023ce CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CMakeLists.txt Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,29 @@ +PROJECT(STREAMIT_API CXX C) + +CMAKE_MINIMUM_REQUIRED(VERSION 2.4) + +IF (CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4) + # CMP0005 is about escaping stuff in ADD_DEFINITIONS() + # But, I want to be sure that will works on cmake 2.4 + CMAKE_POLICY(SET CMP0005 OLD) + CMAKE_POLICY(SET CMP0003 OLD) +ENDIF (CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4) + +# Global definitions ########################################################### + +IF (CMAKE_COMPILER_IS_GNUCXX) + SET(CMAKE_CXX_FLAGS "-pipe -Wextra -Wall") + IF (${CMAKE_BUILD_TYPE} MATCHES "DEBUG") + ADD_DEFINITIONS("-D_GLIBCXX_DEBUG") + ENDIF (${CMAKE_BUILD_TYPE} MATCHES "DEBUG") +ENDIF (CMAKE_COMPILER_IS_GNUCXX) + +IF (CMAKE_SYSTEM_NAME MATCHES "Linux") + ADD_DEFINITIONS("-D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=500") +ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux") + +# Subdirectories ############################################################### + +INCLUDE_DIRECTORIES(${STREAMIT_API_SOURCE_DIR}/include) + +ADD_SUBDIRECTORY(examples) diff -r a3ad9dfcf011 -r 7c8cd74023ce README --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,46 @@ +File Layout: +------------ + +|~examples/ +| |+Buffer/ Sample implementation of std::streambuf; +| |+rot13/ Sample module (http://en.wikipedia.org/wiki/Rot13); +|+include/ API Interfaces Headers; +|+uml/ API Diagrams. + +Build examples: +------------------- + +You will need cmake >= 2.4 and a compiler tool-chain. + +You can install cmake with your OS tools or get it from the cmake website at: +http://www.cmake.org/cmake/resources/software.html + +Launch cmake-gui and follow instructions on the screen. You can choose to +create a Makefile or a VisualStudio solution or any other choice at your +convenience. + +If you don't have the cmake-gui (older version of cmake for Unix for example) +use this command: + +cd /where/you/have/extracted/the/sources/ && cmake . && make + +Install the api headers on your system: +--------------------------------------- + +This is optional, but if you don't want to bother with include paths and +environment variables this is for you. + +Windows: Use the installer. + +If you are not on Windows do as in "Build examples" but after make do: + +make install + +Uninstall the api headers from your system: +------------------------------------------- + +Windows: Use the control panel. + +If you are not on Windows do as in "Build examples" but after make do: + +make uninstall diff -r a3ad9dfcf011 -r 7c8cd74023ce SUPPORT --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SUPPORT Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,1 @@ +Go to http://www.ziastream.it/ to see how to get support on our forum or IRC. diff -r a3ad9dfcf011 -r 7c8cd74023ce examples/Buffer/Buffer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Buffer/Buffer.cpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,56 @@ +#include + +#include "Buffer.hpp" + +StreamBufferImpl::StreamBufferImpl(void) + : _buffer(NULL), + _size(0) +{ +} + +StreamBufferImpl * StreamBufferImpl::setbuf(char *, std::streamsize) +{ + if (s != NULL) + { + /* + * We set the get pointer. + * s is the beginning of the get sequence. + * s is also the current position of the get pointer. + * s + n is a pointer to the end of the get sequence. + */ + this->setg(s, s, s + n); + + /* + * We set the put pointer. + * s is the beginning of the put sequence. + * s + n is a pointer to the end of the put sequence. + */ + this->setp(s, s + n); + } + return (*this); +} + +virtual std::streampos seekoff(std::streamoff off, + std::ios_base::seekdir way, + std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) +{ + if (off != 0) + { + } + + if ((which == std::ios_base::in | std::ios_base::out) + || (which == std::ios_base::in)) + return (this->gptr() - this->eback()); + + return (this->pptr() - this->pbase()); +} + +int StreamBufferImpl::sync(void) +{ + return (0); +} + +int StreamBufferImpl::underflow(void) +{ + return (EOF); +} diff -r a3ad9dfcf011 -r 7c8cd74023ce examples/Buffer/Buffer.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Buffer/Buffer.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,199 @@ +#ifndef _BUFFER_HPP_ +# define _BUFFER_HPP_ + +# include +# include + +/** + * @brief Example of a simple implementation of std::streambuf. + * + * It's purpose is only to show how streambuf works. + * + * The full documentation is available here: + * @link a http://cplusplus.com/reference/iostream/streambuf/ @endlink + * + * According to the official documentation, we will call @b put, all operations + * that fill the buffer (such operations are performed by std::ostream) + * and we will call @b get, all operations that consume the buffer's content + * (operations performed by std::istream) + * + * The std::streambuf abstract class works with two kind of pointers. @b get + * pointers and @b put pointers. + * + * For each kind of pointer, there are three different pointers: + * @li A pointer to the beginning of the sequence; + * @li A pointer to the current position; + * @li A pointer to the end of the sequence. + * + * These pointers are sets by two methods: + * @li setp (set @b put pointers); + * @li setg (set @b get pointers). + * + * Anyway, these pointers can be moved using @ref seekoff and @ref seekpos. + * Any operation that add or remove content from the buffer should move the + * appropriate pointer. + */ +class StreamBufferImpl : public std::streambuf +{ +public: + StreamBufferImpl(void); + virtual ~StreamBufferImpl(void) { } + +protected: + /** + * @brief Set a new buffer for the put/get operations. + * + * This method is called by pubsetbuf. + * + * @param [in] s A pointer to the array of char where operations will be + * performed. + * @param [in] n An integer giving the size of the array. + * + * In our example, we will store the pointer to the buffer and its size, + * and then, set the @b put and @b get pointers using setp and setg. + * + * @return A pointer to the class itself. + */ + virtual StreamBufferImpl * setbuf(char *s, std::streamsize n); + + /** + * @brief Set the @b get and/or @b put pointer to a relative + * position. + * + * @param [in] off Offset value. + * @param [in] way Where the offset takes effect. The possible values are: + * @li @c std::ios_base::beg from the beginning of the stream buffer; + * @li @c std::ios_base::cur from the current position in the stream + * buffer; + * @li @c std::ios_base::end from the end of the stream buffer. + * @param [in] which Allow us to choose which pointer should be moved: + * @li @c std::ios_base::in Move the @b get pointer; + * @li @c std::ios_base::out Move the @b put pointer. + * + * @return The new position. Errors (like an invalid position) should be + * signaled by a value of -1. + */ + virtual std::streampos seekoff(std::streamoff off, + std::ios_base::seekdir way, + std::ios_base::openmode which = std::ios_base::in | std::ios_base::out); + + /** + * @brief Set the @b get and/or @b put pointer to an absolute position. + * + * @param [in] sp The pointer "new position". + * @param [in] which Allow us to choose which pointer should be moved: + * @li @c std::ios_base::in Move the @b get pointer; + * @li @c std::ios_base::out Move the @b put pointer. + * + * @return The new position. Errors (like an invalid position) should be + * signaled by a value of -1. + */ + virtual std::streampos seekpos(std::streampos sp, + std::ios_base::openmode which = std::ios_base::in | std::ios_base::out); + + /** + * @brief Called when there is a need to synchronize the buffer's content + * with the ``real'' content it represents (a device or anything else). + * + * For example when std::endl is applied on a stream. + * + * In our example, this method do nothing. + * + * @return Zero in case of success otherwise -1. + */ + virtual int sync(void); + + /** + * @brief Get the number of characters available once the buffer is empty. + * + * @return An estimation on the number of characters that could be + * added to the buffer if an underflow is made. + */ + virtual std::streamsize showmanyc(void); + + /** + * @brief Get characters from the buffer. + * + * @param [in] s Array where characters will be stored. + * @param [in] n Number of characters to store. + * + * @return The number of characters stored. + */ + virtual std::streamsize xsgetn(char * s, streamsize n); + + /** + * @brief Called when there is a need to read data from the buffer but + * there no more data available in it. This method doesn't move the @b get + * pointer. + * + * In our example, do nothing since we doesn't want to refill the buffer + * when it's empty. + * + * @return The new character available at the get pointer position, if + * any. Otherwise, @c EOF. + */ + virtual int underflow(void); + + /** + * @brief Similar to @ref underflow except that it moves the @b get + * pointer. + * + * In our example, do nothing since we doesn't want to refill the buffer + * when it's empty. + * + * @return The new character available at the get pointer position, if + * any. Otherwise, @c EOF. + */ + virtual int uflow(void); + + /** + * @brief The stream class allows to putback characters in the buffer. + * + * The @ref pbackfail method is called when there is an error while putting + * back a character in the buffer. + * + * @param [in] c The character to put back. + * + * @return The method returns @c EOF on failure. + */ + virtual int pbackfail(int c = EOF); + + /** + * @brief Add content to the buffer. + * + * @param [in] s An array of char to add to the buffer. + * @param [in] n The array's length + * + * @return The number of characters written to the buffer. + */ + virtual streamsize xsputn(const char * s, streamsize n); + + /** + * @brief Called when there is no more available space on the buffer. This + * method could be used to allocate a new buffer. + * + * @param [in] c The character to be written. + * + * @return Any value different than @c EOF in case of success. If the + * function fails, it should return an @c EOF or throw an exception. + */ + virtual int overflow(int c = EOF); + +private: + /** + * @brief std::streambuf has a private copy constructor we do the same. + */ + StreamBufferImpl(const StreamBufferImpl & cpy); + + /** + * @brief This is the buffer our class will fill or consume. + */ + char * _buffer; + + /** + * @brief Size of _buffer. + */ + std::streamsize _size; +}; + +#endif // ! _BUFFER_HPP_ diff -r a3ad9dfcf011 -r 7c8cd74023ce examples/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/CMakeLists.txt Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,1 @@ +ADD_SUBDIRECTORY(rot13) \ No newline at end of file diff -r a3ad9dfcf011 -r 7c8cd74023ce examples/rot13/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/rot13/CMakeLists.txt Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,7 @@ +SET(ROT13_SRC + main.cpp + Rot13Module.cpp + Rot13Handler.cpp + ) + +ADD_EXECUTABLE(rot13 ${ROT13_SRC}) diff -r a3ad9dfcf011 -r 7c8cd74023ce examples/rot13/Rot13Handler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/rot13/Rot13Handler.cpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,42 @@ +#include "Rot13Handler.hpp" + +namespace +{ + /** + * @brief Convert a char to its rot13 value. + * + * @param [in] c The char to encode. + * + * @return The encoded char. + */ + char Rot13Char(char c) + { + if ((c >= ('a' + 13) && c <= 'z') || (c >= ('A' + 13) && c <= 'Z' )) + c -= 13; + else if ((c >= 'a' && c < 'a' + 13) || (c >= 'A' && c < 'A' + 13)) + c += 13; + return (c); + } +}; + +zia::api::handler::EHook Rot13Handler::getHook(void) const +{ + return (zia::api::handler::ResponseBodyModifier); +} + +zia::api::handler::ECode Rot13Handler::operator()(zia::api::http::ITransaction &, std::istream &, std::ostream &) +{ + return (zia::api::handler::Ok); +} + +zia::api::handler::ECode Rot13Handler::operator()(std::istream & is, std::ostream & os) +{ + while (!is.eof()) + { + char c; + is.get(c); + os << Rot13Char(c); + } + + return (zia::api::handler::Ok); +} diff -r a3ad9dfcf011 -r 7c8cd74023ce examples/rot13/Rot13Handler.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/rot13/Rot13Handler.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,50 @@ +#ifndef __ZIA_ROT13_HANDLER_HPP__ +# define __ZIA_ROT13_HANDLER_HPP__ + +# include +# include + +# include "handler/IBodyHandler.hpp" + +/** + * @brief Simple implementation of the StreamIt IHttpHandler interface. + */ +class Rot13Handler : public zia::api::handler::IBodyHandler +{ +public: + virtual ~Rot13Handler(void) { } + + /** + * @brief Getter to know were we should plug this hook. + * + * @return An identifier telling when we should call this handler. + */ + virtual zia::api::handler::EHook getHook(void) const; + + /** + * @brief This is the normal method the server should call. + * + * Since the purpose of this example is to show how std::istream, + * std::ostream and std::streambuf works, we overload the functor so we + * haven't to implement ITransaction. + */ + virtual zia::api::handler::ECode operator()(zia::api::http::ITransaction & transac, + std::istream & is, + std::ostream & os); + + /** + * @brief This is the method we will call in our example. + * + * It is the same as the functor defined in the interface, + * except there is no ITransaction parameter. + * + * @param [in] is Input stream where to read data. + * @param [in] os ouput stream where to write data. + * + * @return A zia::api::Ecode + */ + zia::api::handler::ECode operator()(std::istream & is, + std::ostream & os); +}; + +#endif /* ! __ZIA_ROT13_HANDLER_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce examples/rot13/Rot13Module.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/rot13/Rot13Module.cpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,32 @@ +#include "Rot13Module.hpp" +#include "Rot13Handler.hpp" + +ModRot13::ModRot13() + : _name("Rot13Module"), + _version("0.1"), + _handlers() +{ + zia::api::handler::IHandler * rot13Handler = new Rot13Handler; + + this->_handlers.push_back(rot13Handler); +} + +const std::string & ModRot13::getName(void) const +{ + return (this->_name); +} + +const std::string & ModRot13::getVersion(void) const +{ + return (this->_version); +} + +const std::vector & ModRot13::getHandlers(void) const +{ + return (this->_handlers); +} + +bool ModRot13::configure(zia::api::IConfig *) +{ + return (0); +} diff -r a3ad9dfcf011 -r 7c8cd74023ce examples/rot13/Rot13Module.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/rot13/Rot13Module.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,53 @@ +#ifndef _ZIA_ROT13_MODULE_HPP_ +# define _ZIA_ROT13_MODULE_HPP_ + +# include "http/ITransaction.hpp" +# include "IConfig.hpp" +# include "handler/IHandler.hpp" +# include "IModule.hpp" + +/** + * @brief Simple implementation of the StreamIt IModule interface. + */ +class ModRot13 : public zia::api::IModule +{ +public: + ModRot13(void); + virtual ~ModRot13(void) { } + + /** + * @brief Module name getter. + * + * @return The module name. + */ + const std::string & getName(void) const; + + /** + * @brief Module version getter. + * + * @return The module version. + */ + const std::string & getVersion(void) const; + + /** + * @brief Module's handlers getter. + * + * @return the module's handlers in a vector. + */ + const std::vector & getHandlers(void) const; + + /** + * @brief Allow us to configure the module. + * + * @return true if the configuration was succesful. In our simple example + * it returns true everytime, because we don't need a configuration. + */ + bool configure(zia::api::IConfig *); + +private: + std::string _name; + std::string _version; + std::vector _handlers; +}; + +#endif /* ! _ZIA_ROT13_MODULE_HPP_ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce examples/rot13/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/rot13/main.cpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,77 @@ +#include +#include +#include + +#include "Rot13Module.hpp" +#include "Rot13Handler.hpp" + +/** + * @brief Function that "emulates" the behavior of a server by instanciating a + * IModule and by calling one of its handlers. + * + * @param [in] is The stream to read from. + * @param [in] os The stream to write to. + */ +void process_stream(std::istream & is, std::ostream & os) +{ + ModRot13 module; + const std::vector & handlers = module.getHandlers(); + + std::cout << "Running: " << module.getName() + << " v" << module.getVersion() << std::endl; + + if (!handlers.empty() + && handlers[0]->getHook() == zia::api::handler::ResponseBodyModifier) + { + /* + * We should cast to zia::api::handler::IHttpHandler, but to call the + * functor we would need to implement zia::api::http::ITransaction, + * which isn't the purpose of this example. + */ + Rot13Handler * bodyHandler = dynamic_cast(handlers[0]); + + if (bodyHandler != NULL) + (*bodyHandler)(is, os); + } +} + +/** + * @brief This function print the content of a stream on the standard output + * + * @param [in] is the stream to dump + */ +void dump_stream(std::istream & is) +{ + while (!is.eof()) + { + char c; + is.get(c); + std::cout << c; + } + std::cout << "---" << std::endl; +} + +int main(void) +{ + std::stringbuf buffer1; + std::stringbuf buffer2; + std::ostream os(NULL); + std::istream is(NULL); + + os.rdbuf(&buffer1); + os << "This is an example for the StreamIt api !" << std::endl; + + is.rdbuf(&buffer1); + os.rdbuf(&buffer2); + + process_stream(is, os); + + is.rdbuf(&buffer2); + + dump_stream(is); + + std::cout << "The correct rot13 is: " << std::endl + << "Guvf vf na rknzcyr sbe gur FgernzVg ncv !" << std::endl; + + return (0); +} diff -r a3ad9dfcf011 -r 7c8cd74023ce include/IConfig.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/IConfig.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,34 @@ +#ifndef __ZIA_API_ICONFIG_HPP__ +# define __ZIA_API_ICONFIG_HPP__ + +# include +# include + +namespace zia +{ + namespace api + { + /** + * @brief Represent the interface that the configurtion handler has to + * respect to interact with module. + */ + class IConfig + { + typedef std::map ValueMap; + + virtual ~IConfig(void) { } + + /** + * @brief Get the configuration entries. + * + * At the moment we return a map of strings. We will see if more + * complex data structure will be required. + * + * @return the map of key/value found in configuration file. + */ + virtual const ValueMap & getEntries(void) const = 0; + }; + }; +}; + +#endif /* ! __ZIA_API_ICONFIG_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/IModule.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/IModule.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,75 @@ +#ifndef __ZIA_API_IMODULE_HPP__ +# define __ZIA_API_IMODULE_HPP__ + +# include +# include + +# include "http/ITransaction.hpp" +# include "network/IEndPoint.hpp" +# include "IConfig.hpp" + +namespace zia +{ + namespace api + { + /** + * @brief It provides basic method to identify the module and load a + * module. + */ + class IModule + { + public: + virtual ~IModule(void) { } + + /** + * @brief Get the module name. + * + * @return The module name. + */ + virtual const std::string & getName(void) const = 0; + + /** + * @brief Get module version. + * + * @return The module version. + */ + virtual const std::string & getVersion(void) const = 0; + + /** + * @brief Get the list of module's hook. + * + * @return A vector of IHandler. + * + * @see IHandler + */ + virtual const std::vector & getHandlers(void) const = 0; + + /** + * @brief Update module configuration. + * + * @param [in] conf The pointer on IConfig which hold the + * configuration. + * + * @return True if the configuration was successfully loaded. False + * otherwise and the module will be unloaded. + * + * @see IConfig + */ + virtual bool configure(IConfig* conf = NULL) = 0; + }; + + extern "C" + { + /** + * @brief Entry point of module. + * + * @return The module instance. + * + * @see IModule + */ + IModule * ziaGetModuleInstance(void); + } + }; +}; + +#endif /* ! __ZIA_API_IMODULE_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/handler/IBodyHandler.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/handler/IBodyHandler.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,43 @@ +#ifndef __ZIA_API_HANDLER_IBODYHANDLER_HPP__ +# define __ZIA_API_HANDLER_IBODYHANDLER_HPP__ + +# include + +# include "http/ITransaction.hpp" +# include "handler/IHandler.hpp" + +namespace zia +{ + namespace api + { + namespace handler + { + /** + * @brief Interface for handlers which will interact with the + * client by reading the client's socket or writing in client + * socket. + */ + class IBodyHandler : public IHandler + { + public: + virtual ~IBodyHandler(void) { } + + /** + * @brief Called by the server. + * + * @param [in] transac The transation associated with streams. + * @param [in] is the Input stream. + * @param [in] os the Output stream. + * + * @return An ECode. + * + * @see ITransaction + * @see ECode + */ + virtual ECode operator()(zia::api::http::ITransaction & transac, std::istream & is, std::ostream & os) = 0; + }; + }; + }; +}; + +#endif /* ! __ZIA_API_HANDLER_IBODYHANDLER_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/handler/IHandler.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/handler/IHandler.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,56 @@ +#ifndef __ZIA_API_IHANDLER_HPP_ +# define __ZIA_API_IHANDLER_HPP_ + +# include "http/ITransaction.hpp" + +namespace zia +{ + namespace api + { + namespace handler + { + /** + * @brief Identify a connection point in the server. + */ + enum EHook + { + NetwordModifier, /**< Hook to replace read/write/accept function. (INetworkHandler) */ + RequestHeaderModifier, /**< Hook to modify the request's header. (IHeaderHandler) */ + RequestBodyModifier, /**< Hook to modify the request's body. (IBodyHandler) */ + ResponseProcessor, /**< Hook for content generation. (IBodyHandler) */ + ResponseHeaderModifier, /**< Hook to modify the response's header. (IHeaderHandler) */ + ResponseBodyModifier /**< Hook to modify the response's body. (IBodyHandler) */ + }; + + /** + * @brief Identify the values that each handler can return. + */ + enum ECode + { + Ok, /**< It works !*/ + Decline, /**< No treatment performed */ + ClientError, /**< Client error, module must have set the correct error code in the response headers. */ + ServerError /**< Server error, module must have set the correct error code in the response headers. */ + }; + + /** + * @brief Interface which all handler have to respect. + */ + class IHandler + { + public: + virtual ~IHandler(void) { } + + /** + * @brief Get where the hook has to be called. + * + * @return EHook which represent the location of where the hook + * should be added. + */ + virtual EHook getHook(void) const = 0; + }; + }; + }; +}; + +#endif /* ! __ZIA_API_IHANDLER_HPP_ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/handler/IHeaderHandler.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/handler/IHeaderHandler.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,40 @@ +#ifndef __ZIA_API_HANDLER_IHEADERHANDLER_HPP__ +# define __ZIA_API_HANDLER_IHEADERHANDLER_HPP__ + +# include + +# include "http/ITransaction.hpp" +# include "handler/IHandler.hpp" + +namespace zia +{ + namespace api + { + namespace handler + { + /** + * @brief Interface for headers modifications. + */ + class IHeaderHandler : public IHandler + { + public: + + virtual ~IHeaderHandler(void) { } + + /** + * @brief Called by the server. + * + * @param [in] transac The transation associated with streams. + * + * @return an ECode. + * + * @see ITransaction + * @see ECode + */ + virtual zia::api::handler::Ecode operator()(zia::api::http::ITransaction & transac) = 0; + }; + }; + }; +}; + +#endif /* ! __ZIA_API_HANDLER_IHEADERHANDLER_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/handler/INetworkHandler.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/handler/INetworkHandler.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,102 @@ +#ifndef __ZIA_API_HANDLER_INETWORKHANDLER_HPP__ +# define __ZIA_API_HANDLER_INETWORKHANDLER_HPP__ + +# include + +# include "network/IEndPoint.hpp" +# include "http/ITransaction.hpp" + +namespace zia +{ + namespace api + { + namespace handler + { + /** + * @brief Interface for handlers which redefines some basic + * functions like @b @c accept(2), @b @c write(2), @b @c read(2), + * @b @c close(2). + * + * It allows protocol implementation like ssl. + */ + class INetworkHandler + { + public: + /** + * @brief Agregate functions pointers that can be "overloaded". + * + * If an handler doesn't want to overload one of this function + * it should set the pointer to @c NULL. + */ + struct sPtrFunc + { + /** + * @brief Accept redefinition, called when we want accept a + * new connection sock_accept return a pointer on + * IEndPoint. + * + * @param [in] server Holds the accept socket. + * + * @return Null if an error occured. Otherwise the pointer + * returned MUST BE allocated using the new operator, which + * the server can delete later. + * + * @see network::IEndPoint + */ + network::IEndPoint* (*sock_accept) (network::IEndPoint& server); + + /** + * @brief Read redefinition, called when we want to read + * something on the socket + * + * @param [in] client Holds the client socket. + * @param [out] buffer Buffer that should be filled by the function. + * @param [in] len Buffer size. + * + * @return The number of bytes read, -1 on error and 0 on EOF. + * + * @see network::IEndPoint + */ + ssize_t (*sock_read)(network::IEndPoint& client, void* buffer, size_t len); + + /** + * @brief Write redefinition, called when we want to write + * something on the socket. + * + * @param [in] client Holds the client socket. + * @param [out] buffer Buffer that have to be send. + * @param [in] len Buffer size. + * + * @return The number of bytes written, -1 on error and 0 on EOF. + * + * @see network::IEndPoint + */ + ssize_t (*sock_write)(network::IEndPoint& client, const void* buffer, size_t len); + + /** + * @brief Close redefinition, called when we want to close the socket. + * + * @param [in] client Holds the client socket. + * + * @return 0 on success, -1 on failure. + */ + int (*sock_close)(network::IEndPoint& client); + }; + + virtual ~INetworkHandler(void) { } + + /** + * @brief Return the structure wich contains all function + * pointers redefined. + * + * @return A copy of the structure. + * + * @see sPtrFunc + */ + virtual sPtrFunc getNetworkHandlers(void) = 0; + }; + }; + }; +}; + +#endif /* ! __ZIA_API_HANDLER_INETWORKHANDLER_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/http/IHeader.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/http/IHeader.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,72 @@ +#ifndef __ZIA_API_IHEADER_HPP__ +# define __ZIA_API_IHEADER_HPP__ + +# include +# include + +namespace zia +{ + namespace api + { + namespace http + { + /** + * @brief erovides a convenient way to interact with HTTP headers. + */ + class IHeader + { + typedef std::string Key; + typedef std::string Value; + + public: + virtual ~IHeader(void) { } + + /** + * @brief Get value associated with a key. + * + * @param [in] k The key. + * @param [in,out] value Where the value will be copied. + * + * @return If the key does not exists return false otherwise + * return true. + */ + virtual bool getValue(const Key & k, Value & value) const = 0; + + /** + * @brief Set a value to a given key. + * + * @param [in] k the key. + * @param [in] value The value. + */ + virtual void setValue(const Key & k, const Value & value) = 0; + + /** + * @brief Operator overloading to ease header access. + * + * If the key does not exists, it will be created. + * + * @see IHeader::getValue + */ + virtual const Value & operator[](const Key & k) const = 0; + + /** + * @brief Operator overloading to ease header access. + * + * @see IHeader::setValue + * + * @throw std::exception if the key does not exists. + */ + virtual Value & operator[](const Key & k) = 0; + + /** + * @brief Remove a key and its associated value. + * + * @param [in] k The key to delete. + */ + virtual void deleteKey(const Key & k) = 0; + }; + }; + }; +}; + +#endif /* ! __ZIA_API_IHEADER_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/http/IRequest.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/http/IRequest.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,86 @@ +#ifndef __ZIA_API_REQUEST_HPP__ +# define __ZIA_API_REQUEST_HPP__ + +# include + +# include "http/IHeader.hpp" + +namespace zia +{ + namespace api + { + namespace http + { + /** + * @brief Interface to represent the request received. + * + * @see IHeader + * @see IResponse + */ + class IRequest + { + + public: + + virtual ~IRequest(void) { } + + /** + * @brief Get method used. + * + * @return The name of method. + */ + virtual const std::string & getMethod(void) const = 0; + virtual void setMethod(const std::string &) = 0; + + /** + * @brief Get the uri requested. + * + * @return The URI. + */ + virtual const std::string & getUri(void) const = 0; + + /** + * @brief Set the uri requested. + * + * @param [in] uri The new uri. + */ + virtual void setUri(const std::string & uri) = 0; + + /** + * @brief Get the HTTP version. + * + * It has to be like: '1.1', '1.0' ... + */ + virtual const std::string & getVersion(void) const = 0; + + /** + * @brief Set the HTTP version. + * + * @param [in] v The HTTP version to set it has to be like: + * 'X.X' where X is a digit. + */ + virtual void setVersion(const std::string & v) = 0; + + /** + * @brief Get HTTP header. + * + * @return The http header. + * + * @see IHeader + */ + virtual const IHeader & getHeaders(void) const = 0; + + /** + * @brief Get HTTP header. + * + * @return the http header. + * + * @see IHeader + */ + virtual IHeader & getHeaders(void) = 0; + }; + }; + }; +}; + +#endif /* ! __ZIA_API_REQUEST_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/http/IResponse.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/http/IResponse.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,93 @@ +#ifndef __ZIA_API_IRESPONSE_HPP__ +# define __ZIA_API_IRESPONSE_HPP__ + +# include "http/IHeader.hpp" + +namespace zia +{ + namespace api + { + namespace http + { + /** + * @brief Interface to represent a HTTP response. It provides + * methods to modify the header and the response line. + * + * @see IHeader + * @see IRequest + */ + class IResponse + { + public: + typedef int StatusCode; + + virtual ~IResponse(void) { } + + /** + * @brief Get the status code of the response. + * + * @return The status code, 0 if it is unset. + * + * 'HTTP/1.1 200 OK' -> the '200' in string. + */ + virtual StatusCode getStatusCode(void) const = 0; + + /** + * @brief Get the status message. + * + * @return The status message. + * + * 'HTTP/1.1 200 OK' -> the 'OK' in string. + */ + virtual const std::string & getStatusMessage(void) const = 0; + + /** + * @brief Get the version of HTTP in response. + * + * @return The HTTP version. + * + * 'HTTP/1.1 200 OK' -> the '1.1' in string. + */ + virtual const std::string & getVersion(void) const = 0; + + /** + * @brief Set the status code. + * + * @param [in] code The status code to set. + * + * @see IResponse::getStatusCode + */ + virtual void setStatusCode(StatusCode code) = 0; + + /** + * @brief Set the status message. + * + * @param [in] msg The message to set. + * + * @see IResponse::getStatusMessage + */ + virtual void setStatusMessage(const std::string & msg) = 0; + + /** + * @brief Set the HTTP version. + * + * @param [in] v The HTTP version to set. + * + * @see IResponse::getVersion + */ + virtual void setVersion(const std::string & v) = 0; + + /** + * @brief Get HTTP header associated with the response. + * + * @return The http headers. + * + * @see IHeader + */ + virtual IHeader & getHeaders(void) const = 0; + }; + }; + }; +}; + +#endif /* ! __ZIA_API_IRESPONSE_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/http/ITransaction.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/http/ITransaction.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,101 @@ +#ifndef __ZIA_API_TRANSACTION_HPP__ +# define __ZIA_API_TRANSACTION_HPP__ + +# include "http/IRequest.hpp" +# include "http/IResponse.hpp" +# include "network/IEndPoint.hpp" + +namespace zia +{ + namespace api + { + namespace http + { + /** + * @brief Provide a way to get all informations related to the + * request and the response. + * + * Even if response is always available, it should not be modify in + * RequestModifier * handler. + * + * @note We don't provide differents interfaces for the request and + * the response, to avoid code duplication. + */ + class ITransaction + { + public: + virtual ~ITransaction(void) { } + + /** + * @brief Get the request received. + * + * @return The request. + * + * @see IRequest + */ + virtual const IRequest & getRequest(void) const = 0; + + /** + * @brief Get the response that will be sent. + * + * @return The response. + * + * @remarks This should not be used by RequestHeaderModifier + * and RequestBodyModifier. + * + * @see IResponse + * @see RequestHeaderModifier, RequestHeaderModifier, IResponse + */ + virtual const IResponse & getResponse(void) const = 0; + + /** + * @brief Get client endpoint to know from where it is connected. + * + * @return The client connection point. + * + * @see network::IEndPoint + */ + virtual const network::IEndPoint & getClientEndPoint(void) const = 0; + + /** + * @brief Get on what server the client is connected. + * + * @return The server connection point. + * + * @remarks This can be used to have a module adapted to different vhost + * + * @see network::IEndPoint + */ + virtual const network::IEndPoint & getServerEndPoint(void) const = 0; + + /** + * @brief Get the request received. + * + * @return The request. + * + * @see IRequest + */ + virtual IRequest & getRequest(void) = 0; + + /** + * @brief Get the response that will be sent. + * + * @return The response. + * + * @remarks This should not be used by RequestHeaderModifier + * and RequestBodyModifier. + * + * @see IResponse + * @see handler::EHook + */ + virtual IResponse & getResponse(void) = 0; + + virtual network::IEndPoint & getClientEndPoint(void) = 0; + + virtual network::IEndPoint & getServerEndPoint(void) = 0; + }; + }; + }; +}; + +#endif /* ! __ZIA_API_TRANSACTION_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce include/network/IEndPoint.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/network/IEndPoint.hpp Mon Mar 22 02:08:24 2010 +0100 @@ -0,0 +1,44 @@ +#ifndef __ZIA_API_IENDPOINT_HPP_ +# define __ZIA_API_IENDPOINT_HPP_ + +namespace zia +{ + namespace api + { + namespace network + { + /** + * @brief Represent a connection point. + * + */ + class IEndPoint + { + public: + typedef int Ip; + typedef unsigned short Port; + typedef int Socket; + typedef std::string IAddress; + + virtual ~IEndPoint(void){} + + /** + * @brief Get on wich port socket is connected. + * + * @return The port number. + */ + virtual Port getPort(void) const = 0; + + virtual const Ip & getIp(void) const = 0; + + /** + * @brief Get the socket associated with the endpoint. + * + * @return The socket. + */ + virtual Socket getSocket(void) const = 0; + }; + }; + }; +}; + +#endif /* ! __ZIA_API_IENDPOINT_HPP_ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce src/CMakeLists.txt --- a/src/CMakeLists.txt Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -PROJECT(STREAMIT_API CXX C) - -CMAKE_MINIMUM_REQUIRED(VERSION 2.4) - -IF (CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4) - # CMP0005 is about escaping stuff in ADD_DEFINITIONS() - # But, I want to be sure that will works on cmake 2.4 - CMAKE_POLICY(SET CMP0005 OLD) - CMAKE_POLICY(SET CMP0003 OLD) -ENDIF (CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4) - -# Global definitions ########################################################### - -IF (CMAKE_COMPILER_IS_GNUCXX) - SET(CMAKE_CXX_FLAGS "-pipe -Wextra -Wall") - IF (${CMAKE_BUILD_TYPE} MATCHES "DEBUG") - ADD_DEFINITIONS("-D_GLIBCXX_DEBUG") - ENDIF (${CMAKE_BUILD_TYPE} MATCHES "DEBUG") -ENDIF (CMAKE_COMPILER_IS_GNUCXX) - -IF (CMAKE_SYSTEM_NAME MATCHES "Linux") - ADD_DEFINITIONS("-D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=500") -ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux") - -# Subdirectories ############################################################### - -INCLUDE_DIRECTORIES(${STREAMIT_API_SOURCE_DIR}/include) - -ADD_SUBDIRECTORY(examples) diff -r a3ad9dfcf011 -r 7c8cd74023ce src/examples/Buffer/Buffer.cpp --- a/src/examples/Buffer/Buffer.cpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -#include - -#include "Buffer.hpp" - -StreamBufferImpl::StreamBufferImpl(void) - : _buffer(NULL), - _size(0) -{ -} - -StreamBufferImpl * StreamBufferImpl::setbuf(char *, std::streamsize) -{ - if (s != NULL) - { - /* - * We set the get pointer. - * s is the beginning of the get sequence. - * s is also the current position of the get pointer. - * s + n is a pointer to the end of the get sequence. - */ - this->setg(s, s, s + n); - - /* - * We set the put pointer. - * s is the beginning of the put sequence. - * s + n is a pointer to the end of the put sequence. - */ - this->setp(s, s + n); - } - return (*this); -} - -virtual std::streampos seekoff(std::streamoff off, - std::ios_base::seekdir way, - std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) -{ - if (off != 0) - { - } - - if ((which == std::ios_base::in | std::ios_base::out) - || (which == std::ios_base::in)) - return (this->gptr() - this->eback()); - else - return (this->pptr() - this->pbase()); -} - - -int StreamBufferImpl::sync(void) -{ - return (0); -} - -int StreamBufferImpl::underflow(void) -{ - return (EOF); -} diff -r a3ad9dfcf011 -r 7c8cd74023ce src/examples/Buffer/Buffer.hpp --- a/src/examples/Buffer/Buffer.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,185 +0,0 @@ -#ifndef _BUFFER_HPP_ -# define _BUFFER_HPP_ - -#include -#include - -/** - ** - ** @brief This class is an example of a simple implementation of std::streambuf. - ** It's purpose is only to show how streambuf works. - ** The full documentation is available here: - ** @link a http://cplusplus.com/reference/iostream/streambuf/ @endlink - ** - ** According to the official documentation, we will call @b put, all operations - ** that fill the buffer (such operations are performed by std::ostream) - ** and we will call @b get, all operations that consume the buffer's content - ** (operations performed by std::istream) - ** - ** The std::streambuf abstract class works with two kind of pointers. @b get pointers - ** and @b put pointers. - ** @n - ** For each kind of pointer, there are three different pointers: - ** @li A pointer to the beginning of the sequence - ** @li A pointer to the current position - ** @li A pointer to the end of the sequence - ** - ** These pointers are sets by two methods: - ** @li setp (set @b put pointers) - ** @li setg (set @b get pointers). - ** - ** Anyway, these pointers can be moved using @ref seekoff and @ref seekpos. - ** Any operation that add or remove content from the buffer should move the - ** appropriate pointer. - ** - */ -class StreamBufferImpl : public std::streambuf -{ - -public: - - StreamBufferImpl(void); - virtual ~StreamBufferImpl(void) { } - -protected: - - /** - ** @brief This method sets a new buffer for the put/get operations - ** This method is called by pubsetbuf. - ** @param [in] s A pointer to the array of char where operations will be performed - ** @param [in] n An integer giving the size of the array - ** - ** In our example, we will store the pointer to the buffer and its size, - ** and then, set the @b put and @b get pointers using setp and setg. - ** @return A pointer to the class itself - */ - virtual StreamBufferImpl * setbuf(char *s, std::streamsize n); - - - /** - ** @brief This method set the @b get and/or @b put pointer to a relative position. - ** @param [in] off offset value. - ** @param [in] way Where the offset takes effect. The possible values are - ** @li @c std::ios_base::beg from the beginning of the stream buffer - ** @li @c std::ios_base::cur from the current position in the stream buffer - ** @li @c std::ios_base::end from the end of the stream buffer - ** @param [in] which Allow us to choose which pointer should be moved. - ** @li @c std::ios_base::in Move the @b get pointer. - ** @li @c std::ios_base::out Move the @b put pointer. - ** @return The new position. Errors (like an invalid position) should be signaled by a value of -1. - */ - virtual std::streampos seekoff(std::streamoff off, - std::ios_base::seekdir way, - std::ios_base::openmode which = std::ios_base::in | std::ios_base::out); - - - /** - ** @brief This method set the @b get and/or @b put pointer to an absolute position. - ** @param [in] sp The pointer new position. - ** @param [in] which Allow us to choose which pointer should be moved. - ** @li @c std::ios_base::in Move the @b get pointer. - ** @li @c std::ios_base::out Move the @b put pointer. - ** @return The new position. Errors (like an invalid position) should be signaled by a value of -1. - */ - virtual std::streampos seekpos(std::streampos sp, - std::ios_base::openmode which = std::ios_base::in | std::ios_base::out); - - - /** - ** @brief This method is called when there is a need to synchronize - ** the buffer's content with the ``real'' content it represents - ** (a device or anything else). - ** This method is called when std::endl is applied on a stream - ** - ** In our example, this method will do nothing. - ** @return In case of success, zero is returned. - ** Errors are expected to be signaled by any other value, like -1. - ** - */ - virtual int sync(void); - - - /** - ** @brief This method get the number of characters available once the buffer is empty. - ** @return An estimation on the number of characters that could be - ** added to the buffer if an underflow is made. - */ - virtual std::streamsize showmanyc(void); - - - /** - ** @brief This method gets characters from the buffer. - ** @param [in] s Array where the gotten characters must be stored. - ** @param [in] n Number of characters to store. - ** @return The number of characters stored. - */ - virtual std::streamsize xsgetn(char * s, streamsize n); - - - /** - ** @brief This method is called when there is a need to read data from the buffer - ** but there no more data available in it. This method doesn't move the @b get pointer. - ** - ** In our example, this method will do nothing since we doesn't want to refill - ** the buffer when it's empty. - ** @return The new character available at the get pointer position, if any. Otherwise, @c EOF. - */ - virtual int underflow(void); - - - /** - ** @brief This method is similar to @ref underflow except that it moves the @b get pointer. - ** - ** In our example, this method will do nothing since we doesn't want to refill - ** the buffer when it's empty. - ** @return The new character available at the get pointer position, if any. Otherwise, @c EOF. - */ - virtual int uflow(void); - - - /** - ** @brief The strea class allows to putback characters in the buffer. The @ref pbackfail - ** method is called when there is an error while putting back a character in the buffer. - ** @param [in] c The character to put back. - ** @return The method returns @c EOF on failure or any other value if it was successful. - */ - virtual int pbackfail(int c = EOF); - - - /** - ** @brief This method add content to the buffer. - ** @param [in] s An array of char to add to the buffer. - ** @param [in] n The array's length - ** @return The number of characters written to the buffer. - */ - virtual streamsize xsputn(const char * s, streamsize n); - - - /** - ** @brief This method is called when there is no more - ** available space on the buffer. This method could be used to allocate a new buffer. - ** @param [in] c the character to be written - ** @return Any value different than @c EOF in case of success. - ** If the function fails, it should return an @c EOF or throw an exception - */ - virtual int overflow(int c = EOF); - - -private: - /** - ** @brief std::streambuf has a private copy constructor - ** we do the same - */ - StreamBufferImpl(const StreamBufferImpl & cpy); - -private: - - /** - ** @brief This is the buffer our class will fill or consume, with its size associated - */ - char * _buffer; - std::streamsize _size; - -}; - -#endif // ! _BUFFER_HPP_ diff -r a3ad9dfcf011 -r 7c8cd74023ce src/examples/CMakeLists.txt --- a/src/examples/CMakeLists.txt Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -ADD_SUBDIRECTORY(rot13) \ No newline at end of file diff -r a3ad9dfcf011 -r 7c8cd74023ce src/examples/rot13/CMakeLists.txt --- a/src/examples/rot13/CMakeLists.txt Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -SET(ROT13_SRC - main.cpp - Rot13Module.cpp - Rot13Handler.cpp - ) - -ADD_EXECUTABLE(rot13 ${ROT13_SRC}) diff -r a3ad9dfcf011 -r 7c8cd74023ce src/examples/rot13/Rot13Handler.cpp --- a/src/examples/rot13/Rot13Handler.cpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -#include "Rot13Handler.hpp" - -char Rot13Char(char c) -{ - if ((c >= ('a' + 13) && c <= 'z') || (c >= ('A' + 13) && c <= 'Z' )) - c -= 13; - else if ((c >= 'a' && c < 'a' + 13) || (c >= 'A' && c < 'A' + 13)) - c += 13; - return (c); -} - -zia::api::handler::EHook Rot13Handler::getHook(void) const -{ - return (zia::api::handler::ResponseBodyModifier); -} - -int Rot13Handler::operator()(zia::api::http::ITransaction &, std::istream &, std::ostream &) -{ - return (0); -} - -int Rot13Handler::operator()(std::istream & is, std::ostream & os) -{ - char c; - - while (!is.eof()) - { - is.get(c); - os << Rot13Char(c); - } - return (0); -} diff -r a3ad9dfcf011 -r 7c8cd74023ce src/examples/rot13/Rot13Handler.hpp --- a/src/examples/rot13/Rot13Handler.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -#ifndef __ZIA_ROT13_HANDLER_HPP__ -# define __ZIA_ROT13_HANDLER_HPP__ - -# include -# include - -# include "handler/IBodyHandler.hpp" - -/** - ** @brief Function that convert a char to its rot13 value - ** @param [in] c The char to encode - ** @return The char encoded - */ -char Rot13Char(char c); - -/** - ** @brief this class is an simple implementation of the StreamIt IHttpHandler interface - */ -class Rot13Handler : public zia::api::handler::IBodyHandler -{ -public: - virtual ~Rot13Handler(void) { } - - /** - ** @brief Getter to know were we should plug this hook. - ** @return Enumeration telling when we should call this handler. - */ - virtual zia::api::handler::EHook getHook(void) const; - - /** - ** @brief This is the normal method the server should call. - ** But since the purpose of this example is to show how - ** std::istream, std::ostream and std::streambuf works, - ** we overload the functor so we haven't to implement ITransaction. - */ - virtual int operator()(zia::api::http::ITransaction & transac, - std::istream & is, - std::ostream & os); - - /** - ** @brief This is the method we will call in our example. - ** It is the same as the functor defined in the interface, - ** except there is no ITransaction parameter. - ** @param [in] is input stream where to read data - ** @param [in] os ouput stream where to write data - ** @return an integer TODO define an enum XXX - */ - int operator()(std::istream & is, - std::ostream & os); - -}; - -#endif /* ! __ZIA_ROT13_HANDLER_HPP__ */ - diff -r a3ad9dfcf011 -r 7c8cd74023ce src/examples/rot13/Rot13Module.cpp --- a/src/examples/rot13/Rot13Module.cpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ -#include "Rot13Module.hpp" -#include "Rot13Handler.hpp" - -ModRot13::ModRot13(void) - : _name("Rot13Module"), - _version("0.0.0.0.1"), - _handlers() -{ - zia::api::handler::IHandler * rot13Handler = new Rot13Handler; - - this->_handlers.push_back(rot13Handler); -} - -const std::string & ModRot13::getName(void) const { return (this->_name); } -const std::string & ModRot13::getVersion(void) const { return (this->_version); } -const std::vector & ModRot13::getHandlers(void) const { return (this->_handlers); } - -bool ModRot13::configure(zia::api::IConfig *) -{ - return (0); -} diff -r a3ad9dfcf011 -r 7c8cd74023ce src/examples/rot13/Rot13Module.hpp --- a/src/examples/rot13/Rot13Module.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -#ifndef _ZIA_ROT13_MODULE_HPP_ -# define _ZIA_ROT13_MODULE_HPP_ - -# include "http/ITransaction.hpp" -# include "IConfig.hpp" -# include "handler/IHandler.hpp" -# include "IModule.hpp" - -/** - ** @brief this class is an simple implementation of the StreamIt IModule interface - */ -class ModRot13 : public zia::api::IModule -{ - -public: - - ModRot13(void); - virtual ~ModRot13(void) { } - - /** - ** @brief Module name getter - ** @return The module name - */ - const std::string & getName(void) const; - - /** - ** @brief Module version getter - ** @return The module version - */ - const std::string & getVersion(void) const; - - /** - ** @brief Module's handlers getter - ** @return the module's handlers in a vector - */ - const std::vector & getHandlers(void) const; - - /** - ** @brief Allow us to configure the module. - ** This example won't implement this method since we want - ** to show the IHandler's basics - ** @return true if the configuration was succesful. - ** In our example, it will always return true - */ - bool configure(zia::api::IConfig *); - -private: - std::string _name; - std::string _version; - std::vector _handlers; - -}; - -#endif /* ! _ZIA_ROT13_MODULE_HPP_ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce src/examples/rot13/main.cpp --- a/src/examples/rot13/main.cpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -#include -#include -#include - -#include "Rot13Module.hpp" -#include "Rot13Handler.hpp" - -/** - ** @brief Function that "emulates" the behavior of a server by instanciating a IModule - ** and by calling one of its handlers. - ** @param [in] is The stream to read from - ** @param [in] os The stream to write to - */ -void process_stream(std::istream & is, std::ostream & os) -{ - ModRot13 module; - const std::vector & handlers = module.getHandlers(); - - std::cout << "Running: " << module.getName() << " " << module.getVersion() << std::endl; - - if (!handlers.empty() && handlers[0]->getHook() == zia::api::handler::ResponseBodyModifier) - { - /* - ** We should cast to zia::api::handler::IHttpHandler, but to call the functor - ** we would need to implement zia::api::http::ITransaction, which isn't the purpose of this example - */ - Rot13Handler * bodyHandler = dynamic_cast(handlers[0]); - - if (bodyHandler != NULL) - (*bodyHandler)(is, os); - } -} - -/** - ** @brief This function print the content of a stream on the standard output - ** @param [in] is the stream to dump - */ -void dump_stream(std::istream & is) -{ - char c; - - while (!is.eof()) - { - is.get(c); - std::cout << c; - } - std::cout << "---" << std::endl; -} - -int main(void) -{ - std::stringbuf buffer1; - std::stringbuf buffer2; - std::ostream os(NULL); - std::istream is(NULL); - - os.rdbuf(&buffer1); - os << "This is an example for the StreamIt api !" << std::endl; - - is.rdbuf(&buffer1); - os.rdbuf(&buffer2); - process_stream(is, os); - is.rdbuf(&buffer2); - dump_stream(is); - std::cout << "The correct rot13 is: " << std::endl - << "Guvf vf na rknzcyr sbe gur FgernzVg ncv !" << std::endl; - return (0); -} diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/IConfig.hpp --- a/src/include/IConfig.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -#ifndef __ZIA_API_ICONFIG_HPP__ -# define __ZIA_API_ICONFIG_HPP__ - -# include -# include - -namespace zia -{ - -namespace api -{ - -/** - * @brief this class represent the interface that the configurtion handler - * has to respect to interact with module. - */ -class IConfig -{ - -public: - typedef std::map ValueMap; - -public: - virtual ~IConfig(void) { } - - /** - * @brief Get the configuration entries - * - * for the moment we returned an map of strings. - * We will see if more complex data structure will be required - * - * @return the map of key/value found in configuration file. - */ - virtual const ValueMap & getEntries(void) const = 0; - - -}; - -} - -} - -#endif /* ! __ZIA_API_ICONFIG_HPP__ */ - diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/IModule.hpp --- a/src/include/IModule.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -#ifndef __ZIA_API_IMODULE_HPP__ -# define __ZIA_API_IMODULE_HPP__ - -# include -# include - -# include "http/ITransaction.hpp" -# include "network/IEndPoint.hpp" -# include "IConfig.hpp" - -namespace zia -{ - -namespace api -{ - -/** - * @brief this class represent a Module - * - * it provides basic method to identify the module. - */ -class IModule -{ - -public: - - virtual ~IModule(void) { } - - /** - * @brief Get the module name - * @return The module name - */ - virtual const std::string & getName(void) const = 0; - - /** - * @brief Get module version - * @return The module version - */ - virtual const std::string & getVersion(void) const = 0; - - - /** - * @brief Get the list of module's hook - * @return a vector of IHandler - * @see IHandler - */ - virtual const std::vector & getHandlers(void) const = 0; - - - /** - * @brief Update module configuration - * @param [in] conf the pointer on IConfig which hold the configuration - * @return true if the configuration was successfully loaded - * if the configure module return false, it will be unload. - * @see IConfig - */ - virtual bool configure(IConfig* conf = NULL) = 0; - -}; - -extern "C" -{ - /** - * @brief entry point of module - * @return The module instance - * @see IModule - */ - IModule * ziaGetModuleInstance(void); -} - -} - -} - -#endif /* ! __ZIA_API_IMODULE_HPP__ */ - diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/handler/IBodyHandler.hpp --- a/src/include/handler/IBodyHandler.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -#ifndef __ZIA_API_HANDLER_IBODYHANDLER_HPP__ -# define __ZIA_API_HANDLER_IBODYHANDLER_HPP__ - -# include - -# include "http/ITransaction.hpp" -# include "handler/IHandler.hpp" - -namespace zia -{ - -namespace api -{ - -namespace handler -{ - -/** - * @brief This class represent an handler which will interact - * with the client by reading the client's socket or - * writing in client socket. - */ -class IBodyHandler : public IHandler -{ -public: - - virtual ~IBodyHandler(void) { } - - /** - * @brief method called by the server. - * @param [in] transac the transation associated with streams. - * @param [in] is the input stream - * @param [in] os the output stream - * @return an ECode - * @see ITransaction - * @see ECode - */ - virtual ECode operator()(zia::api::http::ITransaction & transac, std::istream & is, std::ostream & os) = 0; - -}; - -} - -} - -} - -#endif /* ! __ZIA_API_HANDLER_IBODYHANDLER_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/handler/IHandler.hpp --- a/src/include/handler/IHandler.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -#ifndef __ZIA_API_IHANDLER_HPP_ -# define __ZIA_API_IHANDLER_HPP_ - -# include "http/ITransaction.hpp" - -namespace zia -{ - -namespace api -{ - -namespace handler -{ - -/** - * @brief enum which represent connection point in the server - */ -enum EHook -{ - NetwordModifier, /** - -# include "http/ITransaction.hpp" -# include "handler/IHandler.hpp" - -namespace zia -{ - -namespace api -{ - -namespace handler -{ - -/** - * @brief this class allows header modification. - */ -class IHeaderHandler : public IHandler -{ -public: - - virtual ~IHeaderHandler(void) { } - - /** - ** @brief method called by the server. - ** @param [in] transac the transation associated with streams. - ** @return an ECode - ** @see ITransaction - ** @see ECode - */ - virtual int operator()(zia::api::http::ITransaction & transac) = 0; - -}; - -} - -} - -} - -#endif /* ! __ZIA_API_HANDLER_IHEADERHANDLER_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/handler/INetworkHandler.hpp --- a/src/include/handler/INetworkHandler.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,94 +0,0 @@ -#ifndef __ZIA_API_HANDLER_INETWORKHANDLER_HPP__ -# define __ZIA_API_HANDLER_INETWORKHANDLER_HPP__ - -# include - -# include "network/IEndPoint.hpp" -# include "http/ITransaction.hpp" - -namespace zia -{ - -namespace api -{ - -namespace handler -{ -/** - ** @brief This class represents an handler which redefines some basic functions - ** like @b @c accept(2), @b @c write(2), @b @c read(2), @b @c close(2). - ** - ** It allows protocol implementation like ssl. - */ -class INetworkHandler -{ - public: - - /** - ** @brief This structure represents function pointers that can be overloaded. - ** If an handler doesn't want to overload one of this function it should set - ** the pointer to @c NULL. - ** - */ - struct sPtrFunc - { - /** - ** @brief accept redefinition, called when we want accept a new connection - ** sock_accept return a pointer on IEndPoint. - ** @param [in] server Holds the accept socket. - ** @return if an error happend the pointer returned is null. - ** the pointer HAS TO BE ALLOCATED using the new operator, - ** all memory managment is made by the server. - ** @see network::IEndPoint - */ - network::IEndPoint* (*sock_accept) (network::IEndPoint& server); - - /** - ** @brief write redefinition, called when we want to write something on the socket - ** @param [in] client Holds the client socket. - ** @param [out] buffer buffer that should be filled by the function. - ** @param [in] len buffer' size. - ** @return the number of bytes read, -1 on error and 0 on EOF - ** @see network::IEndPoint - */ - int (*sock_read)(network::IEndPoint& client, void* buffer, size_t len); - - /** - ** @brief read redefinition, called when we want to read something on the socket - ** @param [in] client Holds the client socket. - ** @param [out] buffer buffer that have to be send. - ** @param [in] len buffer' size. - ** @return the number of bytes written, -1 on error and 0 on EOF - ** @see network::IEndPoint - */ - int (*sock_write)(network::IEndPoint& client, const void* buffer, size_t len); - - /** - * @brief close redefinition, called when we want to close the socket - * @param [in] client Hols the client socket - * @return 0 on sucess, -1 on failure - */ - int (*sock_close)(network::IEndPoint& client); - }; - - - - virtual ~INetworkHandler(void){} - - /** - ** @brief This method returns the structure wich contains - ** all function pointers redefined. - ** @return a copy of a structure - ** @see sPtrFunc - */ - virtual sPtrFunc getNetworkHandlers(void) = 0; - -}; - -} - -} - -} - -#endif /* ! __ZIA_API_HANDLER_INETWORKHANDLER_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/http/IHeader.hpp --- a/src/include/http/IHeader.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,77 +0,0 @@ - -#ifndef __ZIA_API_IHEADER_HPP__ -# define __ZIA_API_IHEADER_HPP__ - -# include -# include - -namespace zia -{ - -namespace api -{ - -namespace http -{ -/** - * @brief This class provides simple way to interact with HTTP header. - */ -class IHeader -{ - typedef std::string Key; - typedef std::string Value; - -public: - - virtual ~IHeader(void) { } - - /** - * @brief Get value associated with a key - * @param [in] k the key - * @param [in,out] value where the value will be copied - * @return if the key does not exists return false - * otherwise return true - */ - - virtual bool getValue(const Key & k, Value & value) const = 0; - - - /** - * @brief Set a value to a given key - * @param [in] k the key - * @param [in] value the value - */ - - virtual void setValue(const Key & k, const Value & value) = 0; - - /** - * @brief operator overloading to facilitate header access. - * If the key does not exists, it will be created. - * @see IHeader::getValue - */ - virtual const Value & operator[](const Key & k) const = 0; - - /** - * @brief operator overloading to facilitate header access. - * @see IHeader::setValue - * @throw std::exception if the key does not exists. - */ - virtual Value & operator[](const Key & k) = 0; - - /** - ** @brief Remove a key and its associated value. - ** @param [in] k The key to delete. - */ - virtual void deleteKey(const Key & k) = 0; - - -}; - -} - -} - -} - -#endif /* ! __ZIA_API_IHEADER_HPP__ */ - diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/http/IRequest.hpp --- a/src/include/http/IRequest.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,84 +0,0 @@ -#ifndef __ZIA_API_REQUEST_HPP__ -# define __ZIA_API_REQUEST_HPP__ - -#include - -#include "http/IHeader.hpp" - -namespace zia -{ - -namespace api -{ - -namespace http -{ -/** - * @brief This class represent the request received - * @see IHeader - * @see IResponse - */ -class IRequest -{ - -public: - - virtual ~IRequest(void) { } - - /** - * @brief Get method used - * @return the name of method - */ - virtual const std::string & getMethod(void) const = 0; - virtual void setMethod(const std::string &) = 0; - - /** - * @brief get the uri requested - * @return the URI - */ - virtual const std::string & getUri(void) const = 0; - - /** - * @brief set the uri requested - * @param [in] uri the new uri - */ - virtual void setUri(const std::string & uri) = 0; - - /** - * @brief get the HTTP version - * - * it has to be like: '1.1', '1.0' ... - * - */ - virtual const std::string & getVersion(void) const = 0; - - /** - * @brief Set the HTTP version - * @param v the HTTP version to set - * it has to be like: 'X.X' where X is a digit - */ - virtual void setVersion(const std::string & v) = 0; - - /** - * @brief Get HTTP header - * @return the http header - * @see IHeader - */ - virtual const IHeader & getHeaders(void) const = 0; - - /** - * @brief Get HTTP header - * @return the http header - * @see IHeader - */ - virtual IHeader & getHeaders(void) = 0; - -}; - -} - -} - -} - -#endif /* ! __ZIA_API_REQUEST_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/http/IResponse.hpp --- a/src/include/http/IResponse.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -#ifndef __ZIA_API_IRESPONSE_HPP__ -# define __ZIA_API_IRESPONSE_HPP__ - -# include "http/IHeader.hpp" - -namespace zia -{ - -namespace api -{ -namespace http -{ - -/** - * @brief this class represent a HTTP response. It provides some function - * to modify the header and the response line. - * @see IHeader - * @see IRequest - */ -class IResponse -{ - -public: - typedef int StatusCode; - -public: - virtual ~IResponse(void) { } - - /** - * @brief Get the status code of the response - * @return the status code, 0 if it is unset - * - * 'HTTP/1.1 200 OK' -> the '200' in string - */ - virtual StatusCode getStatusCode(void) const = 0; - - /** - * @brief Get the status message - * @return the status message - * - * 'HTTP/1.1 200 OK' -> the 'OK' in string - */ - virtual const std::string & getStatusMessage(void) const = 0; - - /** - * @brief get the version of HTTP in response - * @return the HTTP version - * - * 'HTTP/1.1 200 OK' -> the '1.1' in string - */ - virtual const std::string & getVersion(void) const = 0; - - /** - * @brief set the status code - * @param [in] code the status code to set - * @see IResponse::getStatusCode - */ - virtual void setStatusCode(StatusCode code) = 0; - - /** - * @brief Set the status message - * @param [in] msg the message to set - * @see IResponse::getStatusMessage - */ - virtual void setStatusMessage(const std::string & msg) = 0; - - /** - * @brief set the HTTP version - * @param [in] v the HTTP version to set - * @see IResponse::getVersion - */ - virtual void setVersion(const std::string & v) = 0; - - /** - * @brief get HTTP header associated with the - * response - * @return the http headers - * @see IHeader - */ - virtual IHeader & getHeaders(void) const = 0; - -}; - -} - -} - -} - -#endif /* ! __ZIA_API_IRESPONSE_HPP__ */ - diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/http/ITransaction.hpp --- a/src/include/http/ITransaction.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -#ifndef __ZIA_API_TRANSACTION_HPP__ -# define __ZIA_API_TRANSACTION_HPP__ - -# include "http/IRequest.hpp" -# include "http/IResponse.hpp" -# include "network/IEndPoint.hpp" - -namespace zia -{ - -namespace api -{ - -namespace http -{ - -/** - * @brief This class provide a way to get all informations related - * to the request, and the response. - * - * Even if response is always available, it should not be modify in - * RequestModifier* handler. - * - * We do not provide a different interface to avoid too much useless class - * and it can be avoided with good practice - * - */ -class ITransaction -{ - -public: - virtual ~ITransaction(void) { } - - /** - * @brief Get the request received. - * @return the request - * @see IRequest - */ - virtual const IRequest & getRequest(void) const = 0; - - /** - * @brief Get the response that will be sent - * @return the response - * @see IResponse - * @remarks This should not be used by RequestHeaderModifier and RequestBodyModifier - * @see RequestHeaderModifier, RequestHeaderModifier, IResponse - */ - virtual const IResponse & getResponse(void) const = 0; - - /** - * @brief Get client endpoint to know from where he is connected - * @return the client connection point - * @see network::IEndPoint - */ - virtual const network::IEndPoint & getClientEndPoint(void) const = 0; - - /** - * @brief Get on what server the client is connected - * @return the server connection point - * @see network::IEndPoint - * @remarks This can be used to have a module adapted to different vhost - */ - virtual const network::IEndPoint & getServerEndPoint(void) const = 0; - - /** - * @brief Get the request received. - * @return the request - * @see IRequest - */ - virtual IRequest & getRequest(void) = 0; - - /** - * @brief Get the response that will be sent - * @return the response - * @remarks This should not be used by RequestHeaderModifier and RequestBodyModifier - * @see IResponse - * @see handler::EHook - */ - virtual IResponse & getResponse(void) = 0; - - virtual network::IEndPoint & getClientEndPoint(void) = 0; - virtual network::IEndPoint & getServerEndPoint(void) = 0; - -}; - -} - -} - -} - -#endif /* ! __ZIA_API_TRANSACTION_HPP__ */ diff -r a3ad9dfcf011 -r 7c8cd74023ce src/include/network/IEndPoint.hpp --- a/src/include/network/IEndPoint.hpp Sun Mar 21 19:45:53 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -#ifndef __ZIA_API_IENDPOINT_HPP_ -# define __ZIA_API_IENDPOINT_HPP_ - -namespace zia -{ - -namespace api -{ - -namespace network -{ - -/** - * @brief This class represent a connection point. - * - */ -class IEndPoint -{ - -public: - typedef int Ip; - typedef unsigned short Port; - typedef int Socket; - typedef std::string IAddress; - -public: - - virtual ~IEndPoint(void){} - - /** - * @brief Get on wich port socket is connected - * @return the port - */ - virtual Port getPort(void) const = 0; - - - virtual const Ip & getIp(void) const = 0; - - - /** - * @brief Get the socket associated with the endpoint - * @return the socket - */ - virtual Socket getSocket(void) const = 0; - -}; - -} - -} - -} - -#endif /* ! __ZIA_API_IENDPOINT_HPP_ */