1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-12-22 04:02:04 +03:00

clang_format src and include

This commit is contained in:
bel
2015-06-30 20:49:32 +02:00
parent c762a77c0e
commit 36ce480cc3
7 changed files with 218 additions and 133 deletions

View File

@@ -20,7 +20,8 @@ class CivetServer;
/** /**
* Exception class for thrown exceptions within the CivetHandler object. * Exception class for thrown exceptions within the CivetHandler object.
*/ */
class CIVETWEB_API CivetException : public std::runtime_error { class CIVETWEB_API CivetException : public std::runtime_error
{
public: public:
CivetException(const std::string &msg) : std::runtime_error(msg) {} CivetException(const std::string &msg) : std::runtime_error(msg) {}
}; };
@@ -29,7 +30,8 @@ class CIVETWEB_API CivetException : public std::runtime_error {
* Basic interface for a URI request handler. Handlers implementations * Basic interface for a URI request handler. Handlers implementations
* must be reentrant. * must be reentrant.
*/ */
class CIVETWEB_API CivetHandler { class CIVETWEB_API CivetHandler
{
public: public:
/** /**
* Destructor * Destructor
@@ -87,7 +89,8 @@ class CIVETWEB_API CivetHandler {
* *
* Basic class for embedded web server. This has an URL mapping built-in. * Basic class for embedded web server. This has an URL mapping built-in.
*/ */
class CIVETWEB_API CivetServer { class CIVETWEB_API CivetServer
{
public: public:
/** /**
* Constructor * Constructor
@@ -135,7 +138,8 @@ class CIVETWEB_API CivetServer {
*/ */
void addHandler(const std::string &uri, CivetHandler *handler); void addHandler(const std::string &uri, CivetHandler *handler);
void addHandler(const std::string &uri, CivetHandler &handler) { void addHandler(const std::string &uri, CivetHandler &handler)
{
addHandler(uri, &handler); addHandler(uri, &handler);
} }
@@ -206,8 +210,10 @@ class CIVETWEB_API CivetServer {
*based). *based).
* @return true if key was found * @return true if key was found
*/ */
static bool getParam(struct mg_connection *conn, const char *name, static bool getParam(struct mg_connection *conn,
std::string &dst, size_t occurrence = 0); const char *name,
std::string &dst,
size_t occurrence = 0);
/** /**
* getParam(const std::string &, const char *, std::string &, size_t) * getParam(const std::string &, const char *, std::string &, size_t)
@@ -223,8 +229,11 @@ class CIVETWEB_API CivetServer {
*based). *based).
* @return true if key was found * @return true if key was found
*/ */
static bool getParam(const std::string &data, const char *name, static bool getParam(const std::string &data,
std::string &dst, size_t occurrence = 0) { const char *name,
std::string &dst,
size_t occurrence = 0)
{
return getParam(data.c_str(), data.length(), name, dst, occurrence); return getParam(data.c_str(), data.length(), name, dst, occurrence);
} }
@@ -243,8 +252,11 @@ class CIVETWEB_API CivetServer {
*based). *based).
* @return true if key was found * @return true if key was found
*/ */
static bool getParam(const char *data, size_t data_len, const char *name, static bool getParam(const char *data,
std::string &dst, size_t occurrence = 0); size_t data_len,
const char *name,
std::string &dst,
size_t occurrence = 0);
/** /**
* urlDecode(const std::string &, std::string &, bool) * urlDecode(const std::string &, std::string &, bool)
@@ -256,8 +268,10 @@ class CIVETWEB_API CivetServer {
* uses '+' as character for space, see RFC 1866 section 8.2.1 * uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/ */
static void urlDecode(const std::string &src, std::string &dst, static void urlDecode(const std::string &src,
bool is_form_url_encoded = true) { std::string &dst,
bool is_form_url_encoded = true)
{
urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded); urlDecode(src.c_str(), src.length(), dst, is_form_url_encoded);
} }
@@ -272,7 +286,9 @@ class CIVETWEB_API CivetServer {
* uses '+' as character for space, see RFC 1866 section 8.2.1 * uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/ */
static void urlDecode(const char *src, size_t src_len, std::string &dst, static void urlDecode(const char *src,
size_t src_len,
std::string &dst,
bool is_form_url_encoded = true); bool is_form_url_encoded = true);
/** /**
@@ -285,7 +301,8 @@ class CIVETWEB_API CivetServer {
* uses '+' as character for space, see RFC 1866 section 8.2.1 * uses '+' as character for space, see RFC 1866 section 8.2.1
* http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
*/ */
static void urlDecode(const char *src, std::string &dst, static void urlDecode(const char *src,
std::string &dst,
bool is_form_url_encoded = true); bool is_form_url_encoded = true);
/** /**
@@ -295,8 +312,9 @@ class CIVETWEB_API CivetServer {
* @param dst - destination string * @param dst - destination string
* @param append - true if string should not be cleared before encoding. * @param append - true if string should not be cleared before encoding.
*/ */
static void urlEncode(const std::string &src, std::string &dst, static void
bool append = false) { urlEncode(const std::string &src, std::string &dst, bool append = false)
{
urlEncode(src.c_str(), src.length(), dst, append); urlEncode(src.c_str(), src.length(), dst, append);
} }
@@ -307,8 +325,8 @@ class CIVETWEB_API CivetServer {
* @param dst - destination string * @param dst - destination string
* @param append - true if string should not be cleared before encoding. * @param append - true if string should not be cleared before encoding.
*/ */
static void urlEncode(const char *src, std::string &dst, static void
bool append = false); urlEncode(const char *src, std::string &dst, bool append = false);
/** /**
* urlEncode(const char *, size_t, std::string &, bool) * urlEncode(const char *, size_t, std::string &, bool)
@@ -318,11 +336,14 @@ class CIVETWEB_API CivetServer {
* @param dst - destination string * @param dst - destination string
* @param append - true if string should not be cleared before encoding. * @param append - true if string should not be cleared before encoding.
*/ */
static void urlEncode(const char *src, size_t src_len, std::string &dst, static void urlEncode(const char *src,
size_t src_len,
std::string &dst,
bool append = false); bool append = false);
protected: protected:
class CivetConnection { class CivetConnection
{
public: public:
char *postData; char *postData;
unsigned long postDataLen; unsigned long postDataLen;

View File

@@ -35,7 +35,7 @@
#define CIVETWEB_API #define CIVETWEB_API
#endif #endif
#elif __GNUC__ >= 4 #elif __GNUC__ >= 4
#define CIVETWEB_API __attribute__((visibility ("default"))) #define CIVETWEB_API __attribute__((visibility("default")))
#else #else
#define CIVETWEB_API #define CIVETWEB_API
#endif #endif
@@ -137,7 +137,9 @@ struct mg_callbacks {
1: keep this websocket connection open. 1: keep this websocket connection open.
0: close this websocket connection. 0: close this websocket connection.
This callback is deprecated, use mg_set_websocket_handler instead. */ This callback is deprecated, use mg_set_websocket_handler instead. */
int (*websocket_data)(struct mg_connection *, int bits, char *data, int (*websocket_data)(struct mg_connection *,
int bits,
char *data,
size_t data_len); size_t data_len);
/* Called when civetweb is closing a connection. The per-context mutex is /* Called when civetweb is closing a connection. The per-context mutex is
@@ -158,7 +160,8 @@ struct mg_callbacks {
NULL: do not serve file from memory, proceed with normal file open. NULL: do not serve file from memory, proceed with normal file open.
non-NULL: pointer to the file contents in memory. data_len must be non-NULL: pointer to the file contents in memory. data_len must be
initilized with the size of the memory block. */ initilized with the size of the memory block. */
const char *(*open_file)(const struct mg_connection *, const char *path, const char *(*open_file)(const struct mg_connection *,
const char *path,
size_t *data_len); size_t *data_len);
/* Called when civetweb is about to serve Lua server page, if /* Called when civetweb is about to serve Lua server page, if
@@ -297,8 +300,8 @@ CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx,
typedef int (*mg_websocket_connect_handler)(const struct mg_connection *, typedef int (*mg_websocket_connect_handler)(const struct mg_connection *,
void *); void *);
typedef void (*mg_websocket_ready_handler)(struct mg_connection *, void *); typedef void (*mg_websocket_ready_handler)(struct mg_connection *, void *);
typedef int (*mg_websocket_data_handler)(struct mg_connection *, int, char *, typedef int (*mg_websocket_data_handler)(
size_t, void *); struct mg_connection *, int, char *, size_t, void *);
typedef void (*mg_websocket_close_handler)(const struct mg_connection *, typedef void (*mg_websocket_close_handler)(const struct mg_connection *,
void *); void *);
@@ -307,7 +310,8 @@ typedef void (*mg_websocket_close_handler)(const struct mg_connection *,
Set or remove handler functions for websocket connections. Set or remove handler functions for websocket connections.
This function works similar to mg_set_request_handler - see there. */ This function works similar to mg_set_request_handler - see there. */
CIVETWEB_API void CIVETWEB_API void
mg_set_websocket_handler(struct mg_context *ctx, const char *uri, mg_set_websocket_handler(struct mg_context *ctx,
const char *uri,
mg_websocket_connect_handler connect_handler, mg_websocket_connect_handler connect_handler,
mg_websocket_ready_handler ready_handler, mg_websocket_ready_handler ready_handler,
mg_websocket_data_handler data_handler, mg_websocket_data_handler data_handler,
@@ -391,7 +395,8 @@ mg_get_ports(const struct mg_context *ctx, size_t size, int *ports, int *ssl);
Return: Return:
1 on success, 0 on error. */ 1 on success, 0 on error. */
CIVETWEB_API int mg_modify_passwords_file(const char *passwords_file_name, CIVETWEB_API int mg_modify_passwords_file(const char *passwords_file_name,
const char *domain, const char *user, const char *domain,
const char *user,
const char *password); const char *password);
/* Return information associated with the request. */ /* Return information associated with the request. */
@@ -417,8 +422,10 @@ CIVETWEB_API int mg_write(struct mg_connection *, const void *buf, size_t len);
0 when the connection has been closed 0 when the connection has been closed
-1 on error -1 on error
>0 number of bytes written on success */ >0 number of bytes written on success */
CIVETWEB_API int mg_websocket_write(struct mg_connection *conn, int opcode, CIVETWEB_API int mg_websocket_write(struct mg_connection *conn,
const char *data, size_t data_len); int opcode,
const char *data,
size_t data_len);
/* Blocks until unique access is obtained to this connection. Intended for use /* Blocks until unique access is obtained to this connection. Intended for use
with websockets only. with websockets only.
@@ -470,8 +477,8 @@ enum {
/* Send data to the client using printf() semantics. /* Send data to the client using printf() semantics.
Works exactly like mg_write(), but allows to do message formatting. */ Works exactly like mg_write(), but allows to do message formatting. */
CIVETWEB_API int mg_printf(struct mg_connection *, CIVETWEB_API int mg_printf(struct mg_connection *,
PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_FORMAT_STRING(const char *fmt),
PRINTF_ARGS(2, 3); ...) PRINTF_ARGS(2, 3);
/* Send contents of the entire file together with HTTP headers. */ /* Send contents of the entire file together with HTTP headers. */
CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path); CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path);
@@ -510,8 +517,11 @@ CIVETWEB_API const char *mg_get_header(const struct mg_connection *,
Destination buffer is guaranteed to be '\0' - terminated if it is not Destination buffer is guaranteed to be '\0' - terminated if it is not
NULL or zero length. */ NULL or zero length. */
CIVETWEB_API int mg_get_var(const char *data, size_t data_len, CIVETWEB_API int mg_get_var(const char *data,
const char *var_name, char *dst, size_t dst_len); size_t data_len,
const char *var_name,
char *dst,
size_t dst_len);
/* Get a value of particular form variable. /* Get a value of particular form variable.
@@ -536,8 +546,11 @@ CIVETWEB_API int mg_get_var(const char *data, size_t data_len,
Destination buffer is guaranteed to be '\0' - terminated if it is not Destination buffer is guaranteed to be '\0' - terminated if it is not
NULL or zero length. */ NULL or zero length. */
CIVETWEB_API int mg_get_var2(const char *data, size_t data_len, CIVETWEB_API int mg_get_var2(const char *data,
const char *var_name, char *dst, size_t dst_len, size_t data_len,
const char *var_name,
char *dst,
size_t dst_len,
size_t occurrence); size_t occurrence);
/* Fetch value of certain cookie variable into the destination buffer. /* Fetch value of certain cookie variable into the destination buffer.
@@ -553,8 +566,10 @@ CIVETWEB_API int mg_get_var2(const char *data, size_t data_len,
parameter is not found). parameter is not found).
-2 (destination buffer is NULL, zero length or too small to hold the -2 (destination buffer is NULL, zero length or too small to hold the
value). */ value). */
CIVETWEB_API int mg_get_cookie(const char *cookie, const char *var_name, CIVETWEB_API int mg_get_cookie(const char *cookie,
char *buf, size_t buf_len); const char *var_name,
char *buf,
size_t buf_len);
/* Download data from the remote web server. /* Download data from the remote web server.
host: host name to connect to, e.g. "foo.com", or "10.12.40.1". host: host name to connect to, e.g. "foo.com", or "10.12.40.1".
@@ -572,10 +587,13 @@ CIVETWEB_API int mg_get_cookie(const char *cookie, const char *var_name,
"%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n"); "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n");
*/ */
CIVETWEB_API struct mg_connection * CIVETWEB_API struct mg_connection *
mg_download(const char *host, int port, int use_ssl, char *error_buffer, mg_download(const char *host,
int port,
int use_ssl,
char *error_buffer,
size_t error_buffer_size, size_t error_buffer_size,
PRINTF_FORMAT_STRING(const char *request_fmt), ...) PRINTF_FORMAT_STRING(const char *request_fmt),
PRINTF_ARGS(6, 7); ...) PRINTF_ARGS(6, 7);
/* Close the connection opened by mg_download(). */ /* Close the connection opened by mg_download(). */
CIVETWEB_API void mg_close_connection(struct mg_connection *conn); CIVETWEB_API void mg_close_connection(struct mg_connection *conn);
@@ -604,8 +622,11 @@ CIVETWEB_API const char *mg_version(void);
uses '+' as character for space, see RFC 1866 section 8.2.1 uses '+' as character for space, see RFC 1866 section 8.2.1
http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
Return: length of the decoded data, or -1 if dst buffer is too small. */ Return: length of the decoded data, or -1 if dst buffer is too small. */
CIVETWEB_API int mg_url_decode(const char *src, int src_len, char *dst, CIVETWEB_API int mg_url_decode(const char *src,
int dst_len, int is_form_url_encoded); int src_len,
char *dst,
int dst_len,
int is_form_url_encoded);
/* URL-encode input buffer into destination buffer. /* URL-encode input buffer into destination buffer.
returns the length of the resulting buffer or -1 returns the length of the resulting buffer or -1
@@ -628,8 +649,8 @@ CIVETWEB_API char *mg_md5(char buf[33], ...);
Example: Example:
mg_cry(conn,"i like %s", "logging"); */ mg_cry(conn,"i like %s", "logging"); */
CIVETWEB_API void mg_cry(const struct mg_connection *conn, CIVETWEB_API void mg_cry(const struct mg_connection *conn,
PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_FORMAT_STRING(const char *fmt),
PRINTF_ARGS(2, 3); ...) PRINTF_ARGS(2, 3);
/* utility method to compare two buffers, case incensitive. */ /* utility method to compare two buffers, case incensitive. */
CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len); CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len);
@@ -653,11 +674,17 @@ CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len);
On error, NULL. Se error_buffer for details. On error, NULL. Se error_buffer for details.
*/ */
CIVETWEB_API struct mg_connection *mg_connect_websocket_client( CIVETWEB_API struct mg_connection *
const char *host, int port, int use_ssl, char *error_buffer, mg_connect_websocket_client(const char *host,
size_t error_buffer_size, const char *path, const char *origin, int port,
mg_websocket_data_handler data_func, mg_websocket_close_handler close_func, int use_ssl,
void *user_data); char *error_buffer,
size_t error_buffer_size,
const char *path,
const char *origin,
mg_websocket_data_handler data_func,
mg_websocket_close_handler close_func,
void *user_data);
/* Connect to a TCP server as a client (can be used to connect to a HTTP server) /* Connect to a TCP server as a client (can be used to connect to a HTTP server)
Parameters: Parameters:
@@ -671,7 +698,8 @@ CIVETWEB_API struct mg_connection *mg_connect_websocket_client(
On success, valid mg_connection object. On success, valid mg_connection object.
On error, NULL. Se error_buffer for details. On error, NULL. Se error_buffer for details.
*/ */
CIVETWEB_API struct mg_connection *mg_connect_client(const char *host, int port, CIVETWEB_API struct mg_connection *mg_connect_client(const char *host,
int port,
int use_ssl, int use_ssl,
char *error_buffer, char *error_buffer,
size_t error_buffer_size); size_t error_buffer_size);
@@ -689,8 +717,10 @@ enum { TIMEOUT_INFINITE = -1 };
On success, >= 0 On success, >= 0
On error/timeout, < 0 On error/timeout, < 0
*/ */
CIVETWEB_API int mg_get_response(struct mg_connection *conn, char *ebuf, CIVETWEB_API int mg_get_response(struct mg_connection *conn,
size_t ebuf_len, int timeout); char *ebuf,
size_t ebuf_len,
int timeout);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -15,39 +15,44 @@
#define UNUSED_PARAMETER(x) (void)(x) #define UNUSED_PARAMETER(x) (void)(x)
#endif #endif
bool CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn) { bool CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn)
{
UNUSED_PARAMETER(server); UNUSED_PARAMETER(server);
UNUSED_PARAMETER(conn); UNUSED_PARAMETER(conn);
return false; return false;
} }
bool CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn) { bool CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn)
{
UNUSED_PARAMETER(server); UNUSED_PARAMETER(server);
UNUSED_PARAMETER(conn); UNUSED_PARAMETER(conn);
return false; return false;
} }
bool CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn) { bool CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn)
{
UNUSED_PARAMETER(server); UNUSED_PARAMETER(server);
UNUSED_PARAMETER(conn); UNUSED_PARAMETER(conn);
return false; return false;
} }
bool CivetHandler::handleDelete(CivetServer *server, bool CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn)
struct mg_connection *conn) { {
UNUSED_PARAMETER(server); UNUSED_PARAMETER(server);
UNUSED_PARAMETER(conn); UNUSED_PARAMETER(conn);
return false; return false;
} }
bool CivetHandler::handleOptions(CivetServer *server, bool CivetHandler::handleOptions(CivetServer *server,
struct mg_connection *conn) { struct mg_connection *conn)
{
UNUSED_PARAMETER(server); UNUSED_PARAMETER(server);
UNUSED_PARAMETER(conn); UNUSED_PARAMETER(conn);
return false; return false;
} }
int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata) { int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
{
const struct mg_request_info *request_info = mg_get_request_info(conn); const struct mg_request_info *request_info = mg_get_request_info(conn);
assert(request_info != NULL); assert(request_info != NULL);
CivetServer *me = (CivetServer *)(request_info->user_data); CivetServer *me = (CivetServer *)(request_info->user_data);
@@ -82,7 +87,8 @@ int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata) {
CivetServer::CivetServer(const char **options, CivetServer::CivetServer(const char **options,
const struct mg_callbacks *_callbacks) const struct mg_callbacks *_callbacks)
: context(0) { : context(0)
{
struct mg_callbacks callbacks; struct mg_callbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks)); memset(&callbacks, 0, sizeof(callbacks));
@@ -101,7 +107,8 @@ CivetServer::CivetServer(const char **options,
CivetServer::~CivetServer() { close(); } CivetServer::~CivetServer() { close(); }
void CivetServer::closeHandler(const struct mg_connection *conn) { void CivetServer::closeHandler(const struct mg_connection *conn)
{
const struct mg_request_info *request_info = mg_get_request_info(conn); const struct mg_request_info *request_info = mg_get_request_info(conn);
assert(request_info != NULL); assert(request_info != NULL);
CivetServer *me = (CivetServer *)(request_info->user_data); CivetServer *me = (CivetServer *)(request_info->user_data);
@@ -118,15 +125,18 @@ void CivetServer::closeHandler(const struct mg_connection *conn) {
mg_unlock_context(me->context); mg_unlock_context(me->context);
} }
void CivetServer::addHandler(const std::string &uri, CivetHandler *handler) { void CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
{
mg_set_request_handler(context, uri.c_str(), requestHandler, handler); mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
} }
void CivetServer::removeHandler(const std::string &uri) { void CivetServer::removeHandler(const std::string &uri)
{
mg_set_request_handler(context, uri.c_str(), NULL, NULL); mg_set_request_handler(context, uri.c_str(), NULL, NULL);
} }
void CivetServer::close() { void CivetServer::close()
{
if (context) { if (context) {
mg_stop(context); mg_stop(context);
context = 0; context = 0;
@@ -135,30 +145,37 @@ void CivetServer::close() {
int CivetServer::getCookie(struct mg_connection *conn, int CivetServer::getCookie(struct mg_connection *conn,
const std::string &cookieName, const std::string &cookieName,
std::string &cookieValue) { std::string &cookieValue)
{
// Maximum cookie length as per microsoft is 4096. // Maximum cookie length as per microsoft is 4096.
// http://msdn.microsoft.com/en-us/library/ms178194.aspx // http://msdn.microsoft.com/en-us/library/ms178194.aspx
char _cookieValue[4096]; char _cookieValue[4096];
const char *cookie = mg_get_header(conn, "Cookie"); const char *cookie = mg_get_header(conn, "Cookie");
int lRead = mg_get_cookie(cookie, cookieName.c_str(), _cookieValue, int lRead = mg_get_cookie(
sizeof(_cookieValue)); cookie, cookieName.c_str(), _cookieValue, sizeof(_cookieValue));
cookieValue.clear(); cookieValue.clear();
cookieValue.append(_cookieValue); cookieValue.append(_cookieValue);
return lRead; return lRead;
} }
const char *CivetServer::getHeader(struct mg_connection *conn, const char *CivetServer::getHeader(struct mg_connection *conn,
const std::string &headerName) { const std::string &headerName)
{
return mg_get_header(conn, headerName.c_str()); return mg_get_header(conn, headerName.c_str());
} }
void CivetServer::urlDecode(const char *src, std::string &dst, void CivetServer::urlDecode(const char *src,
bool is_form_url_encoded) { std::string &dst,
bool is_form_url_encoded)
{
urlDecode(src, strlen(src), dst, is_form_url_encoded); urlDecode(src, strlen(src), dst, is_form_url_encoded);
} }
void CivetServer::urlDecode(const char *src, size_t src_len, std::string &dst, void CivetServer::urlDecode(const char *src,
bool is_form_url_encoded) { size_t src_len,
std::string &dst,
bool is_form_url_encoded)
{
int i, j, a, b; int i, j, a, b;
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W') #define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
@@ -179,8 +196,11 @@ void CivetServer::urlDecode(const char *src, size_t src_len, std::string &dst,
} }
} }
bool CivetServer::getParam(struct mg_connection *conn, const char *name, bool CivetServer::getParam(struct mg_connection *conn,
std::string &dst, size_t occurrence) { const char *name,
std::string &dst,
size_t occurrence)
{
const char *formParams = NULL; const char *formParams = NULL;
const struct mg_request_info *ri = mg_get_request_info(conn); const struct mg_request_info *ri = mg_get_request_info(conn);
assert(ri != NULL); assert(ri != NULL);
@@ -227,8 +247,12 @@ bool CivetServer::getParam(struct mg_connection *conn, const char *name,
return false; return false;
} }
bool CivetServer::getParam(const char *data, size_t data_len, const char *name, bool CivetServer::getParam(const char *data,
std::string &dst, size_t occurrence) { size_t data_len,
const char *name,
std::string &dst,
size_t occurrence)
{
const char *p, *e, *s; const char *p, *e, *s;
size_t name_len; size_t name_len;
@@ -262,12 +286,16 @@ bool CivetServer::getParam(const char *data, size_t data_len, const char *name,
return false; return false;
} }
void CivetServer::urlEncode(const char *src, std::string &dst, bool append) { void CivetServer::urlEncode(const char *src, std::string &dst, bool append)
{
urlEncode(src, strlen(src), dst, append); urlEncode(src, strlen(src), dst, append);
} }
void CivetServer::urlEncode(const char *src, size_t src_len, std::string &dst, void CivetServer::urlEncode(const char *src,
bool append) { size_t src_len,
std::string &dst,
bool append)
{
static const char *dont_escape = "._-$,;~()"; static const char *dont_escape = "._-$,;~()";
static const char *hex = "0123456789abcdef"; static const char *hex = "0123456789abcdef";
@@ -286,7 +314,8 @@ void CivetServer::urlEncode(const char *src, size_t src_len, std::string &dst,
} }
} }
std::vector<int> CivetServer::getListeningPorts() { std::vector<int> CivetServer::getListeningPorts()
{
std::vector<int> ports(10); std::vector<int> ports(10);
std::vector<int> ssl(10); std::vector<int> ssl(10);
size_t size = mg_get_ports(context, ports.size(), &ports[0], &ssl[0]); size_t size = mg_get_ports(context, ports.size(), &ports[0], &ssl[0]);
@@ -295,7 +324,8 @@ std::vector<int> CivetServer::getListeningPorts() {
return ports; return ports;
} }
CivetServer::CivetConnection::CivetConnection() { CivetServer::CivetConnection::CivetConnection()
{
postData = NULL; postData = NULL;
postDataLen = 0; postDataLen = 0;
} }

View File

@@ -1169,7 +1169,7 @@ static void mg_set_thread_name(const char *name)
threadName[sizeof(threadName) - 1] = 0; threadName[sizeof(threadName) - 1] = 0;
#if defined(_WIN32) #if defined(_WIN32)
#if defined(_MSC_VER) #if defined(_MSC_VER)
/* Windows and Visual Studio Compiler */ /* Windows and Visual Studio Compiler */
__try __try
{ {
@@ -1185,34 +1185,34 @@ static void mg_set_thread_name(const char *name)
(ULONG_PTR *)&info); (ULONG_PTR *)&info);
} }
__except(EXCEPTION_EXECUTE_HANDLER) {} __except(EXCEPTION_EXECUTE_HANDLER) {}
#elif defined(__MINGW32__) #elif defined(__MINGW32__)
/* No option known to set thread name for MinGW */ /* No option known to set thread name for MinGW */
; ;
#endif #endif
#elif defined(__linux__) #elif defined(__linux__)
/* Linux */ /* Linux */
#if defined(GLIBC_CHK) #if defined(GLIBC_CHK)
(void)pthread_setname_np(pthread_self(), threadName); (void)pthread_setname_np(pthread_self(), threadName);
#else
(void)prctl(PR_SET_NAME, threadName, 0, 0, 0);
#endif
#elif defined(__APPLE__) || defined(__MACH__)
/* OS X */
#if defined(GLIBC_CHK)
(void)pthread_setname_np(threadName);
#endif
#elif defined(BSD) || defined(__FreeBSD__) || defined(__OpenBSD__)
/* BSD (TODO: test) */
#if defined(GLIBC_CHK)
pthread_set_name_np(pthread_self(), threadName);
#endif
#elif defined(__AIX__) || defined(_AIX) || defined(__hpux) || defined(__sun)
/* pthread_set_name_np seems to be missing on AIX, hpux, sun, ... */
#else #else
/* POSIX */ (void)prctl(PR_SET_NAME, threadName, 0, 0, 0);
#if defined(GLIBC_CHK) #endif
#elif defined(__APPLE__) || defined(__MACH__)
/* OS X */
#if defined(GLIBC_CHK)
(void)pthread_setname_np(threadName);
#endif
#elif defined(BSD) || defined(__FreeBSD__) || defined(__OpenBSD__)
/* BSD (TODO: test) */
#if defined(GLIBC_CHK)
pthread_set_name_np(pthread_self(), threadName);
#endif
#elif defined(__AIX__) || defined(_AIX) || defined(__hpux) || defined(__sun)
/* pthread_set_name_np seems to be missing on AIX, hpux, sun, ... */
#else
/* POSIX */
#if defined(GLIBC_CHK)
(void)pthread_setname_np(pthread_self(), threadName); (void)pthread_setname_np(pthread_self(), threadName);
#endif #endif
#endif #endif
} }
#else /* !defined(NO_THREAD_NAME) */ #else /* !defined(NO_THREAD_NAME) */
@@ -5244,20 +5244,24 @@ static void send_file_data(struct mg_connection *conn,
} }
mg_write(conn, filep->membuf + offset, (size_t)len); mg_write(conn, filep->membuf + offset, (size_t)len);
} else if (len > 0 && filep->fp != NULL) { } else if (len > 0 && filep->fp != NULL) {
/* file stored on disk */ /* file stored on disk */
#if defined(LINUX_SENDFILE_TEST) #if defined(LINUX)
/* TODO: Test sendfile for Linux */ /* TODO: Test sendfile for Linux */
if (conn->throttle==0 && conn->ssl==0) { if (conn->throttle == 0 && conn->ssl == 0) {
off_t offs = (off_t)offset; off_t offs = (off_t)offset;
ssize_t sent = sendfile(conn->client.sock, fileno(filep->fp), &offs, (size_t)len); ssize_t sent = sendfile(
if (sent>0) { conn->client.sock, fileno(filep->fp), &offs, (size_t)len);
if (sent > 0) {
conn->num_bytes_sent += sent; conn->num_bytes_sent += sent;
return; return; /* OK */
} }
/* sent<0 means error --> try classic way */ /* sent<0 means error --> try classic way */
mg_cry(conn, "%s: sendfile() failed: %s (trying read/write)", __func__, strerror(ERRNO)); mg_cry(conn,
"%s: sendfile() failed: %s (trying read/write)",
__func__,
strerror(ERRNO));
} }
#else #endif
if (offset > 0 && fseeko(filep->fp, offset, SEEK_SET) != 0) { if (offset > 0 && fseeko(filep->fp, offset, SEEK_SET) != 0) {
mg_cry(conn, "%s: fseeko() failed: %s", __func__, strerror(ERRNO)); mg_cry(conn, "%s: fseeko() failed: %s", __func__, strerror(ERRNO));
} else { } else {
@@ -5269,8 +5273,8 @@ static void send_file_data(struct mg_connection *conn,
} }
/* Read from file, exit the loop on error */ /* Read from file, exit the loop on error */
if ((num_read = (int)fread(buf, 1, (size_t)to_read, filep->fp)) <= if ((num_read =
0) { (int)fread(buf, 1, (size_t)to_read, filep->fp)) <= 0) {
break; break;
} }
@@ -5285,7 +5289,6 @@ static void send_file_data(struct mg_connection *conn,
len -= num_written; len -= num_written;
} }
} }
#endif
} }
} }

View File

@@ -1,4 +1,5 @@
/* "lua_civet.h" */ /* "lua_civet.h" */
/* Project internal header to allow main.c to call a non-public function in mod_lua.inl */ /* Project internal header to allow main.c to call a non-public function in
* mod_lua.inl */
void lua_civet_open_all_libs(lua_State *L); void lua_civet_open_all_libs(lua_State *L);

View File

@@ -299,11 +299,11 @@ static const char *get_option(char **options, const char *option_name)
const char *opt_value = NULL; const char *opt_value = NULL;
/* TODO (low, api makeover): options should be an array of key-value-pairs, /* TODO (low, api makeover): options should be an array of key-value-pairs,
* like * like
* struct {const char * key, const char * value} options[] * struct {const char * key, const char * value} options[]
* but it currently is an array with * but it currently is an array with
* options[2*i] = key, options[2*i + 1] = value * options[2*i] = key, options[2*i + 1] = value
* (probably with a MG_LEGACY_INTERFACE definition) * (probably with a MG_LEGACY_INTERFACE definition)
*/ */
while (options[2 * i] != NULL) { while (options[2 * i] != NULL) {
if (strcmp(options[2 * i], option_name) == 0) { if (strcmp(options[2 * i], option_name) == 0) {
@@ -1813,8 +1813,8 @@ static void change_password_file()
static int manage_service(int action) static int manage_service(int action)
{ {
static const char *service_name = static const char *service_name =
"Civetweb"; /* TODO (mid): check using server_name instead of "Civetweb"; /* TODO (mid): check using server_name instead of
* service_name */ * service_name */
SC_HANDLE hSCM = NULL, hService = NULL; SC_HANDLE hSCM = NULL, hService = NULL;
SERVICE_DESCRIPTION descr; SERVICE_DESCRIPTION descr;
char path[PATH_MAX + 20] = ""; /* Path to executable plus magic argument */ char path[PATH_MAX + 20] = ""; /* Path to executable plus magic argument */

View File

@@ -8,9 +8,9 @@ mmap(void *addr, int64_t len, int prot, int flags, int fd, int offset)
{ {
/* TODO (low): This is an incomplete implementation of mmap for windows. /* TODO (low): This is an incomplete implementation of mmap for windows.
* Currently it is sufficient, but there are a lot of unused parameters. * Currently it is sufficient, but there are a lot of unused parameters.
* Better use a function "mg_map" which only has the required parameters, * Better use a function "mg_map" which only has the required parameters,
* and implement it using mmap in Linux and CreateFileMapping in Windows. * and implement it using mmap in Linux and CreateFileMapping in Windows.
* Noone should expect a full mmap for Windows here. * Noone should expect a full mmap for Windows here.
*/ */
HANDLE fh = (HANDLE)_get_osfhandle(fd); HANDLE fh = (HANDLE)_get_osfhandle(fd);
HANDLE mh = CreateFileMapping(fh, 0, PAGE_READONLY, 0, 0, 0); HANDLE mh = CreateFileMapping(fh, 0, PAGE_READONLY, 0, 0, 0);
@@ -1109,14 +1109,14 @@ void lua_civet_open_all_libs(lua_State *L)
} }
#endif #endif
#ifdef USE_LUA_BINARY #ifdef USE_LUA_BINARY
{ {
/* TODO (low): Test if this could be used as a replacement for bit32. /* TODO (low): Test if this could be used as a replacement for bit32.
* Check again with Lua 5.3 later. */ * Check again with Lua 5.3 later. */
extern int luaopen_binary(lua_State *); extern int luaopen_binary(lua_State *);
luaL_requiref(L, "binary", luaopen_binary, 1); luaL_requiref(L, "binary", luaopen_binary, 1);
lua_pop(L, 1); lua_pop(L, 1);
} }
#endif #endif
} }