1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-07 16:02:55 +03:00

Modified or removed unmaintained parts of the source

This commit is contained in:
Lammert Bies
2019-05-19 19:40:42 +02:00
parent b1aaf3ce77
commit 7d57f6f065
7 changed files with 2 additions and 3120 deletions

View File

@@ -152,16 +152,10 @@ ${TSTDIR}${OBJDIR}%${OBJEXT} : ${TSTDIR}%.c
all: ${LIBDIR}libhttp${LIBEXT} testmime${EXEEXT} all: ${LIBDIR}libhttp${LIBEXT} testmime${EXEEXT}
#
# Temporarily removed from all: rule
# libhttpserver${EXEEXT}
#
clean: clean:
${RM} ${OBJDIR}*${OBJEXT} ${RM} ${OBJDIR}*${OBJEXT}
${RM} ${LIBDIR}libhttp${LIBEXT} ${RM} ${LIBDIR}libhttp${LIBEXT}
${RM} testmime${EXEEXT} ${RM} testmime${EXEEXT}
${RM} libhttpserver${EXEEXT}
testmime${EXEEXT} : \ testmime${EXEEXT} : \
${TSTDIR}${OBJDIR}testmime${OBJEXT} \ ${TSTDIR}${OBJDIR}testmime${OBJEXT} \
@@ -173,18 +167,6 @@ testmime${EXEEXT} : \
${LIBS} ${LIBS}
${STRIP} testmime${EXEEXT} ${STRIP} testmime${EXEEXT}
libhttpserver${EXEEXT} : \
${OBJDIR}main${OBJEXT} \
${LIBDIR}libhttp${LIBEXT} \
Makefile
${LINK} ${XFLAG}libhttpserver${EXEEXT} \
${OBJDIR}main${OBJEXT} \
${LIBDIR}libhttp${LIBEXT} \
${LIBS}
${STRIP} libhttpserver${EXEEXT}
OBJLIST = \ OBJLIST = \
${OBJDIR}extern_md5${OBJEXT} \ ${OBJDIR}extern_md5${OBJEXT} \
${OBJDIR}extern_sha1${OBJEXT} \ ${OBJDIR}extern_sha1${OBJEXT} \

View File

@@ -55,13 +55,12 @@ Clean up files generated during the build
Make options can be set on the command line with the make command like so. Make options can be set on the command line with the make command like so.
``` ```
make build WITH_LUA=1 make build WITH_IPV6=1
``` ```
| Make Options | Description | | Make Options | Description |
| ------------------------- | ---------------------------------------- | | ------------------------- | ---------------------------------------- |
| WITH_LUA=1 | build with Lua support |
| WITH_DEBUG=1 | build with GDB debug support | | WITH_DEBUG=1 | build with GDB debug support |
| WITH_IPV6=1 | with IPV6 support | | WITH_IPV6=1 | with IPV6 support |
| WITH_WEBSOCKET=1 | build with web socket support | | WITH_WEBSOCKET=1 | build with web socket support |
@@ -119,34 +118,3 @@ one additional *package* rule.
``` ```
make -f Makefile.osx package make -f Makefile.osx package
``` ```
Building on Android
---------
This is a small guide to help you run LibHTTP on Android. Currently it is
tested on the HTC Wildfire. If you have managed to run it on other devices
as well, please comment or drop an email in the mailing list.
Note: You do not need root access to run LibHTTP on Android.
- Download the source from the Downloads page.
- Download the Android NDK from [http://developer.android.com/tools/sdk/ndk/index.html](http://developer.android.com/tools/sdk/ndk/index.html)
- Run `/path-to-ndk/ndk-build -C /path-to-libhttp/resources`
That should generate libhttp/lib/armeabi/libhttp
- Using the adb tool (you need to have Android SDK installed for that),
push the generated libhttp binary to `/data/local` folder on device.
- From adb shell, navigate to `/data/local` and execute `./libhttp`.
- To test if the server is running fine, visit your web-browser and
navigate to `http://127.0.0.1:8080` You should see the `Index of /` page.
![screenshot](https://a248.e.akamai.net/camo.github.com/b88428bf009a2b6141000937ab684e04cc8586af/687474703a2f2f692e696d6775722e636f6d2f62676f6b702e706e67)
Notes:
- `jni` stands for Java Native Interface. Read up on Android NDK if you want
to know how to interact with the native C functions of libhttp in Android
Java applications.
- TODO: A Java application that interacts with the native binary or a
shared library.

View File

@@ -3,7 +3,7 @@ Embedding LibHTTP
LibHTTP is primarily designed so applications can easily add HTTP and HTTPS server as well as WebSocket functionality. For example, an application server could use LibHTTP to enable a web service interface for automation or remote control. LibHTTP is primarily designed so applications can easily add HTTP and HTTPS server as well as WebSocket functionality. For example, an application server could use LibHTTP to enable a web service interface for automation or remote control.
However, it can also be used as a stand-alone executable. It can deliver static files and offers built-in server side Lua, JavaScript and CGI support. Some instructions how to build the stand-alone server can be found in [Building.md](Building.md). It can deliver static files and offers built-in server side Lua, JavaScript and CGI support. Some instructions how to build the stand-alone server can be found in [Building.md](Building.md).
Files Files
------ ------
@@ -25,28 +25,6 @@ but all functions required to run a HTTP server.
- src/libhttp.c - src/libhttp.c
- src/md5.inl (MD5 calculation) - src/md5.inl (MD5 calculation)
- src/handle_form.inl (HTML form handling functions) - src/handle_form.inl (HTML form handling functions)
- Optional: C++ wrapper
- include/LibHTTPServer.h (C++ interface)
- src/LibHTTPServer.cpp (C++ wrapper implementation)
- Optional: Third party components
- src/third_party/* (third party components, mainly used for the standalone server)
- src/mod_*.inl (modules to access third party components from LibHTTP)
Note: The C++ wrapper uses the official C interface (libhttp.h) and does not add new features to the server. Some features available in the C interface might be missing in the C++ interface.
#### Additional Source Files for Executables
These files can be used to build a server executable. They contain a `main` function
starting the HTTP server.
- Stand-alone C Server
- src/main.c
- Reference embedded C Server
- examples/embedded_c/embedded_c.c
- Reference embedded C++ Server
- examples/embedded_cpp/embedded_cpp.cpp
Note: The "embedded" example is actively maintained, updated, extended and tested. Other examples in the examples/ folder might be outdated and remain there for reference.
Quick Start Quick Start
------ ------
@@ -61,72 +39,6 @@ By default, the server will automatically serve up files like a normal HTTP serv
- Use `httplib_set_request_handler()` to easily add your own request handlers. - Use `httplib_set_request_handler()` to easily add your own request handlers.
- Use `httplib_stop()` to stop the server. - Use `httplib_stop()` to stop the server.
### C++
- Note that LibHTTP is Clean C, and C++ interface ```LibHTTPServer.h``` is only a wrapper layer around the C interface.
Not all LibHTTP features available in C are also available in C++.
- Create LibHTTPHandlers for each URI.
- Register the handlers with `LibHTTPServer::addHandler()`
- `LibHTTPServer` starts on contruction and stops on destruction.
- Use contructor *options* to select the port and document root among other things.
- Use constructor *callbacks* to add your own hooks.
Lua Support
------
Lua is a server side include functionality. Files ending in .lua will be processed with Lua.
##### Add the following CFLAGS
- -DLUA_COMPAT_ALL
- -DUSE_LUA
- -DUSE_LUA_SQLITE3
- -DUSE_LUA_FILE_SYSTEM
##### Add the following sources
- src/mod_lua.inl
- src/third_party/lua-5.2.4/src
+ lapi.c
+ lauxlib.c
+ lbaselib.c
+ lbitlib.c
+ lcode.c
+ lcorolib.c
+ lctype.c
+ ldblib.c
+ ldebug.c
+ ldo.c
+ ldump.c
+ lfunc.c
+ lgc.c
+ linit.c
+ liolib.c
+ llex.c
+ lmathlib.c
+ lmem.c
+ loadlib.c
+ lobject.c
+ lopcodes.c
+ loslib.c
+ lparser.c
+ lstate.c
+ lstring.c
+ lstrlib.c
+ ltable.c
+ ltablib.c
+ ltm.c
+ lundump.c
+ lvm.c
+ lzio.c
- src/third_party/sqlite3.c
- src/third_party/sqlite3.h
- src/third_party/lsqlite3.c
- src/third_party/lfs.c
- src/third_party/lfs.h
This build is valid for Lua version Lua 5.2. It is also possible to build with Lua 5.1 (including LuaJIT) or Lua 5.3.
JavaScript Support JavaScript Support
------ ------

View File

@@ -1,36 +0,0 @@
#
# Copyright (c) 2013 No Face Press, LLC
# License http://opensource.org/licenses/mit-license.php MIT License
#
#This makefile is used to test the other Makefiles
PROG = embedded_cpp
SRC = embedded_cpp.cpp
TOP = ../..
CIVETWEB_LIB = libcivetweb.a
CFLAGS = -I$(TOP)/include $(COPT)
LIBS = -lpthread
include $(TOP)/resources/Makefile.in-os
ifeq ($(TARGET_OS),LINUX)
LIBS += -ldl
endif
all: $(PROG)
$(PROG): $(CIVETWEB_LIB) $(SRC)
$(CXX) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS)
$(CIVETWEB_LIB):
$(MAKE) -C $(TOP) clean lib WITH_CPP=1
cp $(TOP)/$(CIVETWEB_LIB) .
clean:
rm -f $(CIVETWEB_LIB) $(PROG)
.PHONY: all clean

View File

@@ -1,297 +0,0 @@
/* Copyright (c) 2013-2014 the Civetweb developers
* Copyright (c) 2013 No Face Press, LLC
* License http://opensource.org/licenses/mit-license.php MIT License
*/
// Simple example program on how to use Embedded C++ interface.
#include "CivetServer.h"
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#define DOCUMENT_ROOT "."
#define PORT "8081"
#define EXAMPLE_URI "/example"
#define EXIT_URI "/exit"
bool exitNow = false;
class ExampleHandler : public CivetHandler
{
public:
bool
handleGet(CivetServer *server, struct httplib_connection *conn)
{
httplib_printf(conn,
"HTTP/1.1 200 OK\r\nContent-Type: "
"text/html\r\nConnection: close\r\n\r\n");
httplib_printf(conn, "<html><body>\r\n");
httplib_printf(conn,
"<h2>This is an example text from a C++ handler</h2>\r\n");
httplib_printf(conn,
"<p>To see a page from the A handler <a "
"href=\"a\">click here</a></p>\r\n");
httplib_printf(conn,
"<p>To see a page from the A handler with a parameter "
"<a href=\"a?param=1\">click here</a></p>\r\n");
httplib_printf(conn,
"<p>To see a page from the A/B handler <a "
"href=\"a/b\">click here</a></p>\r\n");
httplib_printf(conn,
"<p>To see a page from the *.foo handler <a "
"href=\"xy.foo\">click here</a></p>\r\n");
httplib_printf(conn,
"<p>To exit <a href=\"%s\">click here</a></p>\r\n",
EXIT_URI);
httplib_printf(conn, "</body></html>\r\n");
return true;
}
};
class ExitHandler : public CivetHandler
{
public:
bool
handleGet(CivetServer *server, struct httplib_connection *conn)
{
httplib_printf(conn,
"HTTP/1.1 200 OK\r\nContent-Type: "
"text/plain\r\nConnection: close\r\n\r\n");
httplib_printf(conn, "Bye!\n");
exitNow = true;
return true;
}
};
class AHandler : public CivetHandler
{
private:
bool
handleAll(const char *method,
CivetServer *server,
struct httplib_connection *conn)
{
std::string s = "";
httplib_printf(conn,
"HTTP/1.1 200 OK\r\nContent-Type: "
"text/html\r\nConnection: close\r\n\r\n");
httplib_printf(conn, "<html><body>");
httplib_printf(conn, "<h2>This is the A handler for \"%s\" !</h2>", method);
if (CivetServer::getParam(conn, "param", s)) {
httplib_printf(conn, "<p>param set to %s</p>", s.c_str());
} else {
httplib_printf(conn, "<p>param not set</p>");
}
httplib_printf(conn, "</body></html>\n");
return true;
}
public:
bool
handleGet(CivetServer *server, struct httplib_connection *conn)
{
return handleAll("GET", server, conn);
}
bool
handlePost(CivetServer *server, struct httplib_connection *conn)
{
return handleAll("POST", server, conn);
}
};
class ABHandler : public CivetHandler
{
public:
bool
handleGet(CivetServer *server, struct httplib_connection *conn)
{
httplib_printf(conn,
"HTTP/1.1 200 OK\r\nContent-Type: "
"text/html\r\nConnection: close\r\n\r\n");
httplib_printf(conn, "<html><body>");
httplib_printf(conn, "<h2>This is the AB handler!!!</h2>");
httplib_printf(conn, "</body></html>\n");
return true;
}
};
class FooHandler : public CivetHandler
{
public:
bool
handleGet(CivetServer *server, struct httplib_connection *conn)
{
/* Handler may access the request info using httplib_get_request_info */
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
httplib_printf(conn,
"HTTP/1.1 200 OK\r\nContent-Type: "
"text/html\r\nConnection: close\r\n\r\n");
httplib_printf(conn, "<html><body>\n");
httplib_printf(conn, "<h2>This is the Foo GET handler!!!</h2>\n");
httplib_printf(conn,
"<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n",
req_info->request_method,
req_info->uri,
req_info->http_version);
httplib_printf(conn, "</body></html>\n");
return true;
}
bool
handlePost(CivetServer *server, struct httplib_connection *conn)
{
/* Handler may access the request info using httplib_get_request_info */
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
int64_t rlen;
int64_t wlen;
int64_t nlen = 0;
int64_t tlen = req_info->content_length;
char buf[1024];
httplib_printf(conn,
"HTTP/1.1 200 OK\r\nContent-Type: "
"text/html\r\nConnection: close\r\n\r\n");
httplib_printf(conn, "<html><body>\n");
httplib_printf(conn, "<h2>This is the Foo POST handler!!!</h2>\n");
httplib_printf(conn,
"<p>The request was:<br><pre>%s %s HTTP/%s</pre></p>\n",
req_info->request_method,
req_info->uri,
req_info->http_version);
httplib_printf(conn, "<p>Content Length: %li</p>\n", (long)tlen);
httplib_printf(conn, "<pre>\n");
while (nlen < tlen) {
rlen = tlen - nlen;
if (rlen > sizeof(buf)) {
rlen = sizeof(buf);
}
rlen = httplib_read(conn, buf, (size_t)rlen);
if (rlen <= 0) break;
wlen = httplib_write(conn, buf, (size_t)rlen);
if (rlen != rlen) break;
nlen += wlen;
}
httplib_printf(conn, "\n</pre>\n");
httplib_printf(conn, "</body></html>\n");
return true;
}
#define fopen_recursive fopen
bool
handlePut(CivetServer *server, struct httplib_connection *conn)
{
/* Handler may access the request info using httplib_get_request_info */
const struct httplib_request_info *req_info = httplib_get_request_info(conn);
int64_t rlen;
int64_t wlen;
int64_t nlen = 0;
int64_t tlen = req_info->content_length;
FILE * f;
char buf[1024];
int fail = 0;
#ifdef _WIN32
_snprintf(buf, sizeof(buf), "D:\\somewhere\\%s\\%s", req_info->remote_user, req_info->local_uri);
buf[sizeof(buf)-1] = 0; /* TODO: check overflow */
f = fopen_recursive(buf, "wb");
#else
snprintf(buf, sizeof(buf), "~/somewhere/%s/%s", req_info->remote_user, req_info->local_uri);
buf[sizeof(buf)-1] = 0; /* TODO: check overflow */
f = fopen_recursive(buf, "w");
#endif
if (!f) {
fail = 1;
} else {
while (nlen < tlen) {
rlen = tlen - nlen;
if (rlen > sizeof(buf)) {
rlen = sizeof(buf);
}
rlen = httplib_read(conn, buf, (size_t)rlen);
if (rlen <= 0) {
fail = 1;
break;
}
wlen = fwrite(buf, 1, (size_t)rlen, f);
if (rlen != rlen) {
fail = 1;
break;
}
nlen += wlen;
}
fclose(f);
}
if (fail) {
httplib_printf(conn,
"HTTP/1.1 409 Conflict\r\n"
"Content-Type: text/plain\r\n"
"Connection: close\r\n\r\n");
} else {
httplib_printf(conn,
"HTTP/1.1 201 Created\r\n"
"Content-Type: text/plain\r\n"
"Connection: close\r\n\r\n");
}
return true;
}
};
int
main(int argc, char *argv[])
{
const char *options[] = {
"document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0};
std::vector<std::string> cpp_options;
for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
cpp_options.push_back(options[i]);
}
// CivetServer server(options); // <-- C style start
CivetServer server(cpp_options); // <-- C++ style start
ExampleHandler h_ex;
server.addHandler(EXAMPLE_URI, h_ex);
ExitHandler h_exit;
server.addHandler(EXIT_URI, h_exit);
AHandler h_a;
server.addHandler("/a", h_a);
ABHandler h_ab;
server.addHandler("/a/b", h_ab);
FooHandler h_foo;
server.addHandler("", h_foo);
printf("Browse files at http://localhost:%s/\n", PORT);
printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI);
printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
while (!exitNow) {
#ifdef _WIN32
Sleep(1000);
#else
sleep(1);
#endif
}
printf("Bye!\n");
return 0;
}

View File

@@ -1,588 +0,0 @@
/*
* Copyright (c) 2016 Lammert Bies
* Copyright (c) 2013-2014 the Civetweb developers
* Copyright (c) 2013 No Face Press, LLC
*
* License http://opensource.org/licenses/mit-license.php MIT License
*/
#ifndef _CIVETWEB_SERVER_H_
#define _CIVETWEB_SERVER_H_
#ifdef __cplusplus
#include "libhttp.h"
#include <map>
#include <string>
#include <vector>
#include <stdexcept>
// forward declaration
class LibHTTPServer;
/**
* Exception class for thrown exceptions within the LibHTTPHandler object.
*/
class CIVETWEB_API LibHTTPException : public std::runtime_error
{
public:
LibHTTPException(const std::string &msg) : std::runtime_error(msg)
{
}
};
/**
* Basic interface for a URI request handler. Handlers implementations
* must be reentrant.
*/
class CIVETWEB_API LibHTTPHandler
{
public:
/**
* Destructor
*/
virtual ~LibHTTPHandler()
{
}
/**
* Callback method for GET request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handleGet(LibHTTPServer *server, struct httplib_connection *conn);
/**
* Callback method for POST request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handlePost(LibHTTPServer *server, struct httplib_connection *conn);
/**
* Callback method for HEAD request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handleHead(LibHTTPServer *server, struct httplib_connection *conn);
/**
* Callback method for PUT request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handlePut(LibHTTPServer *server, struct httplib_connection *conn);
/**
* Callback method for DELETE request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handleDelete(LibHTTPServer *server, struct httplib_connection *conn);
/**
* Callback method for OPTIONS request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handleOptions(LibHTTPServer *server, struct httplib_connection *conn);
/**
* Callback method for PATCH request.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if implemented, false otherwise
*/
virtual bool handlePatch(LibHTTPServer *server, struct httplib_connection *conn);
};
/**
* Basic interface for a URI authorization handler. Handler implementations
* must be reentrant.
*/
class CIVETWEB_API LibHTTPAuthHandler
{
public:
/**
* Destructor
*/
virtual ~LibHTTPAuthHandler()
{
}
/**
* Callback method for authorization requests. It is up the this handler
* to generate 401 responses if authorization fails.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true if authorization succeeded, false otherwise
*/
virtual bool authorize(LibHTTPServer *server, struct httplib_connection *conn) = 0;
};
/**
* Basic interface for a websocket handler. Handlers implementations
* must be reentrant.
*/
class CIVETWEB_API LibHTTPWebSocketHandler
{
public:
/**
* Destructor
*/
virtual ~LibHTTPWebSocketHandler()
{
}
/**
* Callback method for when the client intends to establish a websocket
*connection, before websocket handshake.
*
* @param server - the calling server
* @param conn - the connection information
* @returns true to keep socket open, false to close it
*/
virtual bool handleConnection(LibHTTPServer *server, const struct httplib_connection *conn);
/**
* Callback method for when websocket handshake is successfully completed,
*and connection is ready for data exchange.
*
* @param server - the calling server
* @param conn - the connection information
*/
virtual void handleReadyState(LibHTTPServer *server, struct httplib_connection *conn);
/**
* Callback method for when a data frame has been received from the client.
*
* @param server - the calling server
* @param conn - the connection information
* @bits: first byte of the websocket frame, see websocket RFC at
*http://tools.ietf.org/html/rfc6455, section 5.2
* @data, data_len: payload, with mask (if any) already applied.
* @returns true to keep socket open, false to close it
*/
virtual bool handleData(LibHTTPServer *server, struct httplib_connection *conn, int bits, char *data, size_t data_len);
/**
* Callback method for when the connection is closed.
*
* @param server - the calling server
* @param conn - the connection information
*/
virtual void handleClose(LibHTTPServer *server, const struct httplib_connection *conn);
};
/**
* LibHTTPCallbacks
*
* wrapper for httplib_callbacks
*/
struct CIVETWEB_API LibHTTPCallbacks : public httplib_callbacks {
LiBHTTPCallbacks();
};
/**
* LibHTTPServer
*
* Basic class for embedded web server. This has an URL mapping built-in.
*/
class CIVETWEB_API LibHTTPServer
{
public:
/**
* Constructor
*
* This automatically starts the sever.
* It is good practice to call getContext() after this in case there
* were errors starting the server.
*
* Note: LibHTTPServer should not be used as a static instance in a Windows
* DLL, since the constructor creates threads and the destructor joins
* them again (creating/joining threads should not be done in static
* constructors).
*
* @param options - the web server options.
* @param callbacks - optional web server callback methods.
*
* @throws LibHTTPException
*/
LibHTTPServer(const char **options,
const struct LibHTTPCallbacks *callbacks = 0);
LibHTTPServer(std::vector<std::string> options,
const struct LibHTTPCallbacks *callbacks = 0);
/**
* Destructor
*/
virtual ~LibHTTPServer();
/**
* close()
*
* Stops server and frees resources.
*/
void close();
/**
* getContext()
*
* @return the context or 0 if not running.
*/
const struct httplib_context *
getContext() const
{
return context;
}
/**
* addHandler(const std::string &, LibHTTPHandler *)
*
* Adds a URI handler. If there is existing URI handler, it will
* be replaced with this one.
*
* URI's are ordered and prefix (REST) URI's are supported.
*
* @param uri - URI to match.
* @param handler - handler instance to use.
*/
void addHandler(const std::string &uri, LibHTTPHandler *handler);
void
addHandler(const std::string &uri, LibHTTPHandler &handler)
{
addHandler(uri, &handler);
}
/**
* addWebSocketHandler
*
* Adds a WebSocket handler for a specific URI. If there is existing URI
*handler, it will
* be replaced with this one.
*
* URI's are ordered and prefix (REST) URI's are supported.
*
* @param uri - URI to match.
* @param handler - handler instance to use.
*/
void addWebSocketHandler(const std::string &uri,
LibHTTPWebSocketHandler *handler);
void
addWebSocketHandler(const std::string &uri, LibHTTPWebSocketHandler &handler)
{
addWebSocketHandler(uri, &handler);
}
/**
* removeHandler(const std::string &)
*
* Removes a handler.
*
* @param uri - the exact URL used in addHandler().
*/
void removeHandler(const std::string &uri);
/**
* removeWebSocketHandler(const std::string &)
*
* Removes a web socket handler.
*
* @param uri - the exact URL used in addWebSocketHandler().
*/
void removeWebSocketHandler(const std::string &uri);
/**
* addAuthHandler(const std::string &, LibHTTPAuthHandler *)
*
* Adds a URI authorization handler. If there is existing URI authorization
* handler, it will be replaced with this one.
*
* URI's are ordered and prefix (REST) URI's are supported.
*
* @param uri - URI to match.
* @param handler - authorization handler instance to use.
*/
void addAuthHandler(const std::string &uri, LibHTTPAuthHandler *handler);
void
addAuthHandler(const std::string &uri, LibHTTPAuthHandler &handler)
{
addAuthHandler(uri, &handler);
}
/**
* removeAuthHandler(const std::string &)
*
* Removes an authorization handler.
*
* @param uri - the exact URL used in addAuthHandler().
*/
void removeAuthHandler(const std::string &uri);
/**
* getListeningPorts()
*
* Returns a list of ports that are listening
*
* @return A vector of ports
*/
std::vector<int> getListeningPorts();
/**
* getCookie(struct httplib_connection *conn, const std::string &cookieName,
*std::string &cookieValue)
*
* Puts the cookie value string that matches the cookie name in the
*cookieValue destinaton string.
*
* @param conn - the connection information
* @param cookieName - cookie name to get the value from
* @param cookieValue - cookie value is returned using thiis reference
* @returns the size of the cookie value string read.
*/
static int getCookie(struct httplib_connection *conn,
const std::string &cookieName,
std::string &cookieValue);
/**
* getHeader(struct httplib_connection *conn, const std::string &headerName)
* @param conn - the connection information
* @param headerName - header name to get the value from
* @returns a char array whcih contains the header value as string
*/
static const char *getHeader(struct httplib_connection *conn, const std::string &headerName);
/**
* getParam(struct httplib_connection *conn, const char *, std::string &, size_t)
*
* Returns a query paramter contained in the supplied buffer. The
* occurance value is a zero-based index of a particular key name. This
* should not be confused with the index over all of the keys. Note that
*this
* function assumes that parameters are sent as text in http query string
* format, which is the default for web forms. This function will work for
* html forms with method="GET" and method="POST" attributes. In other
*cases,
* you may use a getParam version that directly takes the data instead of
*the
* connection as a first argument.
*
* @param conn - parameters are read from the data sent through this
*connection
* @param name - the key to search for
* @param dst - the destination string
* @param occurrence - the occurrence of the selected name in the query (0
*based).
* @return true if key was found
*/
static bool getParam(struct httplib_connection *conn, const char *name, std::string &dst, size_t occurrence = 0);
/**
* getParam(const std::string &, const char *, std::string &, size_t)
*
* Returns a query paramter contained in the supplied buffer. The
* occurance value is a zero-based index of a particular key name. This
* should not be confused with the index over all of the keys.
*
* @param data - the query string (text)
* @param name - the key to search for
* @param dst - the destination string
* @param occurrence - the occurrence of the selected name in the query (0
*based).
* @return true if key was found
*/
static bool
getParam(const std::string &data,
const char *name,
std::string &dst,
size_t occurrence = 0)
{
return getParam(data.c_str(), data.length(), name, dst, occurrence);
}
/**
* getParam(const char *, size_t, const char *, std::string &, size_t)
*
* Returns a query paramter contained in the supplied buffer. The
* occurance value is a zero-based index of a particular key name. This
* should not be confused with the index over all of the keys.
*
* @param data the - query string (text)
* @param data_len - length of the query string
* @param name - the key to search for
* @param dst - the destination string
* @param occurrence - the occurrence of the selected name in the query (0
*based).
* @return true if key was found
*/
static bool getParam(const char *data,
size_t data_len,
const char *name,
std::string &dst,
size_t occurrence = 0);
/**
* urlDecode(const std::string &, std::string &, bool)
*
* @param src - string to be decoded
* @param dst - destination string
* @param is_form_url_encoded - true if form url encoded
* form-url-encoded data differs from URI encoding in a way that it
* uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/
static void
urlDecode(const std::string &src,
std::string &dst,
bool is_form_url_encoded = true)
{
urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
}
/**
* urlDecode(const char *, size_t, std::string &, bool)
*
* @param src - buffer to be decoded
* @param src_len - length of buffer to be decoded
* @param dst - destination string
* @param is_form_url_encoded - true if form url encoded
* form-url-encoded data differs from URI encoding in a way that it
* uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/
static void urlDecode(const char *src,
size_t src_len,
std::string &dst,
bool is_form_url_encoded = true);
/**
* urlDecode(const char *, std::string &, bool)
*
* @param src - buffer to be decoded (0 terminated)
* @param dst - destination string
* @param is_form_url_encoded true - if form url encoded
* form-url-encoded data differs from URI encoding in a way that it
* uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/
static void urlDecode(const char *src,
std::string &dst,
bool is_form_url_encoded = true);
/**
* urlEncode(const std::string &, std::string &, bool)
*
* @param src - buffer to be encoded
* @param dst - destination string
* @param append - true if string should not be cleared before encoding.
*/
static void
urlEncode(const std::string &src, std::string &dst, bool append = false)
{
urlEncode(src.c_str(), src.length(), dst, append);
}
/**
* urlEncode(const char *, size_t, std::string &, bool)
*
* @param src - buffer to be encoded (0 terminated)
* @param dst - destination string
* @param append - true if string should not be cleared before encoding.
*/
static void
urlEncode(const char *src, std::string &dst, bool append = false);
/**
* urlEncode(const char *, size_t, std::string &, bool)
*
* @param src - buffer to be encoded
* @param src_len - length of buffer to be decoded
* @param dst - destination string
* @param append - true if string should not be cleared before encoding.
*/
static void urlEncode(const char *src,
size_t src_len,
std::string &dst,
bool append = false);
protected:
class LibHTTPConnection
{
public:
char *postData;
unsigned long postDataLen;
LibHTTPConnection();
~LibHTTPConnection();
};
struct httplib_context *context;
std::map<struct httplib_connection *, class LibHTTPConnection> connections;
private:
/**
* requestHandler(struct httplib_connection *, void *cbdata)
*
* Handles the incomming request.
*
* @param conn - the connection information
* @param cbdata - pointer to the LibHTTPHandler instance.
* @returns 0 if implemented, false otherwise
*/
static int requestHandler(struct httplib_connection *conn, void *cbdata);
static int webSocketConnectionHandler(const struct httplib_connection *conn,
void *cbdata);
static void webSocketReadyHandler(struct httplib_connection *conn, void *cbdata);
static int webSocketDataHandler(struct httplib_connection *conn,
int bits,
char *data,
size_t data_len,
void *cbdata);
static void webSocketCloseHandler(const struct httplib_connection *conn,
void *cbdata);
/**
* authHandler(struct httplib_connection *, void *cbdata)
*
* Handles the authorization requests.
*
* @param conn - the connection information
* @param cbdata - pointer to the LibHTTPAuthHandler instance.
* @returns 1 if authorized, 0 otherwise
*/
static int authHandler(struct httplib_connection *conn, void *cbdata);
/**
* closeHandler(struct httplib_connection *)
*
* Handles closing a request (internal handler)
*
* @param conn - the connection information
*/
static void closeHandler(const struct httplib_connection *conn);
/**
* Stores the user provided close handler
*/
void (*userCloseHandler)(const struct httplib_connection *conn);
};
#endif /* __cplusplus */
#endif /* _CIVETWEB_SERVER_H_ */

2059
src/main.c

File diff suppressed because it is too large Load Diff