Open Lighting Architecture: ola::http::HTTPServer Class Reference

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
ola::http::HTTPServer Class Reference
HTTP » HTTP Server

## Detailed Description

The base HTTP Server.

This is a simple HTTP Server built around libmicrohttpd. It runs in a separate thread.

Example
  HTTPServer::HTTPServerOptions options;
  options.port = ...;
  HTTPServer server(options);
  server.Init();
  server.Run();
  // get on with life and later...
  server.Stop();
  ola::http::HTTPServer::HTTPServer
  HTTPServer(const HTTPServerOptions &options)
  Setup the HTTP server.
  Definition HTTPServer.cpp:435
  ola::http::HTTPServer::HTTPServerOptions
  Definition HTTPServer.h:162

Inheritance diagram for ola::http::HTTPServer:

## Classes
struct | HTTPServerOptions

## Public Types
typedef ola::Callback2< int, const HTTPRequest *, HTTPResponse * > | BaseHTTPCallback

## Public Member Functions
| HTTPServer (const HTTPServerOptions &options)
| Setup the HTTP server.
virtual | ~HTTPServer ()
| Destroy this object.
bool | Init ()
| Setup the HTTP server.
void * | Run ()
| The entry point into the new thread.
void | Stop ()
| Stop the HTTP server.
void | UpdateSockets ()
| This is run every loop iteration to update the list of sockets in the SelectServer from MHD.
void | HandleHTTPIO ()
int | DispatchRequest (const HTTPRequest *request, HTTPResponse *response)
| Call the appropriate handler.
bool | RegisterHandler (const std::string &path, BaseHTTPCallback *handler)
| Register a handler.
bool | RegisterFile (const std::string &path, const std::string &content_type)
| Register a static file. The root of the URL corresponds to the data dir.
bool | RegisterFile (const std::string &path, const std::string &file, const std::string &content_type)
| Register a static file.
void | RegisterDefaultHandler (BaseHTTPCallback *handler)
| Set the default handler.
void | Handlers (std::vector< std::string > *handlers) const
| Return a list of all handlers registered.
const std::string | DataDir () const
int | ServeError (HTTPResponse *response, const std::string &details="")
| Serve an error.
int | ServeNotFound (HTTPResponse *response)
| Serve a 404.
int | ServeStaticContent (const std::string &path, const std::string &content_type, HTTPResponse *response)
| Return the contents of a file.
ola::io::SelectServer * | SelectServer ()
Public Member Functions inherited from ola::thread::Thread
| Thread (const Options &options=Options())
| Create a new thread with the specified thread options.
virtual | ~Thread ()
| Destructor.
virtual bool | Start ()
| Start the thread and wait for the thread to be running.
virtual bool | FastStart ()
| Start the thread and return immediately.
virtual bool | Join (void *ptr=NULL)
| Join this thread.
bool | IsRunning ()
| Check if the thread is running.
ThreadId | Id () const
| Return the thread id.
std::string | Name () const
| Return the thread name.

## Static Public Member Functions
static int | ServeRedirect (HTTPResponse *response, const std::string &location)
| Serve a redirect.
static struct MHD_Response * | BuildResponse (void *data, size_t size)
Static Public Member Functions inherited from ola::thread::Thread
static ThreadId | Self ()
| Returns the current thread's id.

## Static Public Attributes
static const char | CONTENT_TYPE_PLAIN [] = "text/plain"
static const char | CONTENT_TYPE_HTML [] = "text/html"
static const char | CONTENT_TYPE_GIF [] = "image/gif"
static const char | CONTENT_TYPE_PNG [] = "image/png"
static const char | CONTENT_TYPE_CSS [] = "text/css"
static const char | CONTENT_TYPE_JS [] = "text/javascript"
static const char | CONTENT_TYPE_OCT [] = "application/octet-stream"

## Constructor & Destructor Documentation

## ◆ HTTPServer()

ola::http::HTTPServer::HTTPServer | ( | const HTTPServerOptions & | options | ) |
| explicit

Setup the HTTP server.

Parameters

  options | the configuration options for the server

## Member Function Documentation

## ◆ HandleHTTPIO()

void ola::http::HTTPServer::HandleHTTPIO | ( |  | ) |
| inline

Called when there is HTTP IO activity to deal with. This is a noop as MHD_run is called in UpdateSockets above.

## ◆ Init()

bool ola::http::HTTPServer::Init | ( |  | ) |

Setup the HTTP server.

Returns
  true on success, false on failure

## ◆ RegisterDefaultHandler()

void ola::http::HTTPServer::RegisterDefaultHandler | ( | BaseHTTPCallback * | handler | ) |

Set the default handler.

Parameters

  handler | the default handler to call. This will be freed when the HTTPServer is destroyed.

## ◆ RegisterFile() [1/2]

bool ola::http::HTTPServer::RegisterFile | ( | const std::string & | path,
|  | const std::string & | content_type )

Register a static file. The root of the URL corresponds to the data dir.

Parameters

  path | the URL path for the file e.g. '/foo.png'
  content_type | the content type.

## ◆ RegisterFile() [2/2]

bool ola::http::HTTPServer::RegisterFile | ( | const std::string & | path,
|  | const std::string & | file,
|  | const std::string & | content_type )

Register a static file.

Parameters

  path | the path to serve on e.g. /foo.png
  file | the path to the file to serve relative to the data dir e.g. images/foo.png
  content_type | the content type.

## ◆ RegisterHandler()

bool ola::http::HTTPServer::RegisterHandler | ( | const std::string & | path,
|  | BaseHTTPCallback * | handler )

Register a handler.

Parameters

  path | the url to respond on
  handler | the Closure to call for this request. These will be freed once the HTTPServer is destroyed.

## ◆ Run()

void * ola::http::HTTPServer::Run | ( |  | ) |
| virtual

The entry point into the new thread.

Implements ola::thread::Thread.

## ◆ ServeError()

int ola::http::HTTPServer::ServeError | ( | HTTPResponse * | response,
|  | const std::string & | details = "" )

Serve an error.

Parameters

  response | the response to use.
  details | the error description

## ◆ ServeNotFound()

int ola::http::HTTPServer::ServeNotFound | ( | HTTPResponse * | response | ) |

Serve a 404.

Parameters

  response | the response to use

## ◆ ServeRedirect()

int ola::http::HTTPServer::ServeRedirect | ( | HTTPResponse * | response,
|  | const std::string & | location )
| static

Serve a redirect.

Parameters

  response | the response to use
  location | the location to redirect to

The documentation for this class was generated from the following files:

- include/ola/http/HTTPServer.h
- common/http/HTTPServer.cpp