Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Power of C++ - Developing a Portable HTTP Server with Python Interpreter using Boost, TinyXML

, 1 Jun 2008 CPOL
This article describes portable networking library (ahttp) and small HTTP server - result of modern C++ programming approaches investigation
ahttpserver_demo.zip
ahttpserver_demo
ahttpserver.exe
boost_python-vc80-mt-1_34_1.dll
python_handler.dll
web
images
icon_dir.gif
icon_file.gif
icon_virtual_dir.gif
python
favicon.ico
print.pyhtml
ahttpserver_src.zip
ahttplib
aconnect
ahttp
http_header_read_check.inl
tinyxml
ahttpserver
out
server.config.ubuntu
web
images
icon_dir.gif
icon_file.gif
icon_virtual_dir.gif
python
python_handler
wrappers.inl
makefile
module.inl
server.config.win-debug
server.config.win-release
favicon.ico
print.pyhtml
#ifndef PYTHON_HANDLER_WRAPPERS_H
#define PYTHON_HANDLER_WRAPPERS_H

#include <boost/noncopyable.hpp>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp> 
#include <boost/python/suite/indexing/map_indexing_suite.hpp> 

namespace python = boost::python;

#include "aconnect/types.hpp"

//////////////////////////////////////////////////////////////////////////
class TracebackLoaderWrapper : private boost::noncopyable
{
public:
	inline void write (aconnect::string_constref data)	{ 
		content += data;	
	}
	aconnect::string content;
};



//////////////////////////////////////////////////////////////////////////
// 
//	
class RequestHeaderWrapper : private boost::noncopyable
{
public:
	RequestHeaderWrapper (ahttp::HttpRequestHeader *header) : header_(header) {
		assert (header_);
	}

	inline size_t getLength() {
		return header_->Headers.size(); 
	}
	inline std::string getHeader (aconnect::string_constref key) {
		return header_->getHeader(key); 
	}
	inline bool hasHeader (aconnect::string_constref key) {
		return header_->hasHeader(key); 
	}
	inline aconnect::str2str_map& items () {
		return header_->Headers; 
	}
	inline std::string requestMethod()		{ return header_->Method;		}
	inline int requestHttpVerHigh()			{ return header_->VersionHigh;	}
	inline int requestHttpVerLow()			{ return header_->VersionLow;	}
	inline std::string userAgent()			{ return header_->getHeader ( ahttp::detail::HeaderUserAgent);	}

protected:
	ahttp::HttpRequestHeader *header_;

};



//////////////////////////////////////////////////////////////////////////
// 
//	
class RequestWrapper : private boost::noncopyable
{
public:
	RequestWrapper (ahttp::HttpContext *context) : 
		  context_(context), 
		  requestLoaded_ (false), 
		  requestReadInRawForm_(false)
	  {
		  assert (context);
	  }

	  inline const aconnect::str2str_map& getParameters ()		{ 
		  processRequest(); return context_->GetParameters; 
	  }
	  inline const aconnect::str2str_map& postParameters()		{ 
		  processRequest(); return context_->PostParameters;
	  }
	  inline const aconnect::str2str_map& cookies ()			{ 
		  processRequest(); return context_->Cookies; 
	  }
	  inline const std::map <aconnect::string, ahttp::UploadFileInfo>& files ()			{ 
		  processRequest(); return context_->UploadedFiles; 
	  }

	  aconnect::string_constptr param (aconnect::string_constref key);
	  std::string rawRead (int buffSize);
	  void processRequest();


protected:
	inline void throwRequestReadRawError () {
		PyErr_SetString (PyExc_RuntimeError, "HTTP request has been read in raw form");
		python::throw_error_already_set();
	}
	inline void throwRequestProcessedError () {
		PyErr_SetString (PyExc_RuntimeError, "HTTP request has been loaded to collections: use 'get' or 'post'");
		python::throw_error_already_set();
	}

	ahttp::HttpContext *context_;
	bool requestLoaded_;
	bool requestReadInRawForm_;

};


//////////////////////////////////////////////////////////////////////////
// 
//	wrapper for ahttp::HttpContext - cover some HttpContext functionality
class HttpContextWrapper : private boost::noncopyable
{
public:
	HttpContextWrapper (ahttp::HttpContext *context) : 
	  context_ (context), contentWritten_ (false), 
		  requestHeader_ (context ? &context->RequestHeader : NULL),
		  request_ (context) 
	  {
		  assert (context);
	  }

	  // request info
	  inline std::string virtualPath() {
		  return context_->VirtualPath; 
	  }
	  inline std::string scriptPath() {
		  return context_->FileSystemPath.string(); 
	  }

	  // client info
	  inline std::string clientIpAddr() {
		  return aconnect::util::formatIpAddr (context_->Client->ip); 
	  }
	  inline aconnect::port_type clientPort() {
		  return context_->Client->port; 
	  }
	  inline aconnect::port_type serverPort () {
		  return context_->Client->server->port(); 
	  }
	  //////////////////////////////////////////////////////////////////////////

	  inline void throwResponseWrittenError () {
		  PyErr_SetString (PyExc_RuntimeError, "HTTP response header cannot be set - response content writing already started");
		  python::throw_error_already_set();
	  }

	  inline bool isClientConnected () {
		  return context_->isClientConnected();
	  }


	  //////////////////////////////////////////////////////////////////////////
	  //
	  //	response modification

	  void write (aconnect::string_constref data);
	  void writeEscaped (aconnect::string_constref data);
	  void flush ();
	  void setContentType (aconnect::string_constref contentType, aconnect::string_constref charset="");

	  inline void setUtf8Html () {
		  setContentType (ahttp::detail::ContentTypeTextHtml, ahttp::detail::ContentCharsetUtf8);
	  }

protected:	
	ahttp::HttpContext *context_;
	bool contentWritten_;

public:
	RequestHeaderWrapper requestHeader_;
	RequestWrapper request_;

};



#endif // PYTHON_HANDLER_WRAPPERS_H

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Artem Kustikov
Software Developer (Senior)
Belarus Belarus
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 1 Jun 2008
Article Copyright 2008 by Artem Kustikov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid