feature: HTTP in recorder!

This commit is contained in:
Jan-Piet Mens
2015-08-27 23:42:18 +02:00
parent e3c29f9447
commit da7ce120b4
8 changed files with 5880 additions and 4 deletions

21
LICENSE
View File

@@ -181,3 +181,24 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
---------------------------------------------------------------------------
## mongoose
Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
Copyright (c) 2013-2015 Cesanta Software Limited
All rights reserved
This code is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation. For the terms of this
license, see <http://www.gnu.org/licenses>.
You are free to use this code under the terms of the GNU General
Public License, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
Alternatively, you can license this code under a commercial
license, as set out in <http://cesanta.com/>.
---------------------------------------------------------------------------

View File

@@ -4,17 +4,36 @@ LIBS = -L/Users/jpm/Auto/pubgit/MQTT/mosquitto/org.eclipse.mosquitto.git/lib
LIBS += -lcurl -lmosquitto
CFLAGS=-Wall -Werror -g
OTR_OBJS = json.o \
geo.o \
geohash.o \
mkpath.o \
file.o \
safewrite.o \
base64.o \
ghash.o \
misc.o \
util.o \
storage.o
ifneq ($(HAVE_REDIS),no)
CFLAGS += -DHAVE_REDIS=1
LIBS += -lhiredis
endif
ifeq ($(HAVE_HTTP),yes)
CFLAGS += -DHAVE_HTTP=1
OTR_OBJS += mongoose.o
LIBS += -lssl
endif
all: ot-recorder ocat ghashfind
ot-recorder: ot-recorder.c json.o utarray.h utstring.h geo.o geohash.o mkpath.o file.o safewrite.o base64.o ghash.o config.h udata.h misc.o util.o storage.o
$(CC) $(CFLAGS) ot-recorder.c -o ot-recorder json.o geo.o geohash.o mkpath.o file.o safewrite.o base64.o ghash.o misc.o util.o storage.o $(LIBS)
ot-recorder: ot-recorder.c $(OTR_OBJS)
$(CC) $(CFLAGS) ot-recorder.c -o ot-recorder $(OTR_OBJS) $(LIBS)
ot-recorder.o: ot-recorder.c storage.h
geo.o: geo.h geo.c udata.h Makefile config.mk config.h
geohash.o: geohash.h geohash.c udata.h Makefile config.mk
file.o: file.h file.c config.h misc.h Makefile config.mk
@@ -34,6 +53,7 @@ storage.o: storage.c storage.h config.h util.h
ghashfind: ghashfind.o util.o json.o
$(CC) $(CFLAGS) -o ghashfind ghashfind.o util.o json.o
ghashfind.o: ghashfind.c util.h
mongoose.o: mongoose.c mongoose.h
clean:
rm -f *.o

View File

@@ -1,2 +1,3 @@
# Select features
HAVE_REDIS ?= no
HAVE_HTTP ?= yes

5485
mongoose.c Normal file

File diff suppressed because it is too large Load Diff

157
mongoose.h Normal file
View File

@@ -0,0 +1,157 @@
// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
// Copyright (c) 2013-2014 Cesanta Software Limited
// All rights reserved
//
// This software is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation. For the terms of this
// license, see <http://www.gnu.org/licenses/>.
//
// You are free to use this software under the terms of the GNU General
// Public License, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// Alternatively, you can license this software under a commercial
// license, as set out in <http://cesanta.com/>.
#ifndef MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_VERSION "5.6"
#include <stdio.h> // required for FILE
#include <stddef.h> // required for size_t
#include <sys/types.h> // required for time_t
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
// This structure contains information about HTTP request.
struct mg_connection {
const char *request_method; // "GET", "POST", etc
const char *uri; // URL-decoded URI
const char *http_version; // E.g. "1.0", "1.1"
const char *query_string; // URL part after '?', not including '?', or NULL
char remote_ip[48]; // Max IPv6 string length is 45 characters
char local_ip[48]; // Local IP address
unsigned short remote_port; // Client's port
unsigned short local_port; // Local port number
int num_headers; // Number of HTTP headers
struct mg_header {
const char *name; // HTTP header name
const char *value; // HTTP header value
} http_headers[30];
char *content; // POST (or websocket message) data, or NULL
size_t content_len; // Data length
int is_websocket; // Connection is a websocket connection
int status_code; // HTTP status code for HTTP error handler
int wsbits; // First byte of the websocket frame
void *server_param; // Parameter passed to mg_create_server()
void *connection_param; // Placeholder for connection-specific data
void *callback_param;
};
struct mg_server; // Opaque structure describing server instance
enum mg_result { MG_FALSE, MG_TRUE, MG_MORE };
enum mg_event {
MG_POLL = 100, // If callback returns MG_TRUE connection closes
// after all of data is sent
MG_CONNECT, // If callback returns MG_FALSE, connect fails
MG_AUTH, // If callback returns MG_FALSE, authentication fails
MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req
MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection
MG_RECV, // Mongoose has received POST data chunk.
// Callback should return a number of bytes to discard from
// the receive buffer, or -1 to close the connection.
MG_CLOSE, // Connection is closed, callback return value is ignored
MG_WS_HANDSHAKE, // New websocket connection, handshake request
MG_WS_CONNECT, // New websocket connection established
MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
};
typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
// Websocket opcodes, from http://tools.ietf.org/html/rfc6455
enum {
WEBSOCKET_OPCODE_CONTINUATION = 0x0,
WEBSOCKET_OPCODE_TEXT = 0x1,
WEBSOCKET_OPCODE_BINARY = 0x2,
WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
WEBSOCKET_OPCODE_PING = 0x9,
WEBSOCKET_OPCODE_PONG = 0xa
};
// Server management functions
struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
void mg_destroy_server(struct mg_server **);
const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
time_t mg_poll_server(struct mg_server *, int milliseconds);
const char **mg_get_valid_option_names(void);
const char *mg_get_option(const struct mg_server *server, const char *name);
void mg_copy_listeners(struct mg_server *from, struct mg_server *to);
struct mg_connection *mg_next(struct mg_server *, struct mg_connection *);
void mg_wakeup_server(struct mg_server *);
void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...);
struct mg_connection *mg_connect(struct mg_server *, const char *);
// Connection management functions
void mg_send_status(struct mg_connection *, int status_code);
void mg_send_header(struct mg_connection *, const char *name, const char *val);
size_t mg_send_data(struct mg_connection *, const void *data, int data_len);
size_t mg_printf_data(struct mg_connection *, const char *format, ...);
size_t mg_vprintf_data(struct mg_connection *, const char *format, va_list ap);
size_t mg_write(struct mg_connection *, const void *buf, size_t len);
size_t mg_printf(struct mg_connection *conn, const char *fmt, ...);
size_t mg_vprintf(struct mg_connection *conn, const char *fmt, va_list ap);
size_t mg_websocket_write(struct mg_connection *, int opcode,
const char *data, size_t data_len);
size_t mg_websocket_printf(struct mg_connection* conn, int opcode,
const char *fmt, ...);
void mg_send_file(struct mg_connection *, const char *path, const char *);
void mg_send_file_data(struct mg_connection *, int fd);
const char *mg_get_header(const struct mg_connection *, const char *name);
const char *mg_get_mime_type(const char *name, const char *default_mime_type);
int mg_get_var(const struct mg_connection *conn, const char *var_name,
char *buf, size_t buf_len);
int mg_get_var_n(const struct mg_connection *conn, const char *var_name,
char *buf, size_t buf_len, int n);
int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
int mg_parse_multipart(const char *buf, int buf_len,
char *var_name, int var_name_len,
char *file_name, int file_name_len,
const char **data, int *data_len);
// Utility functions
void *mg_start_thread(void *(*func)(void *), void *param);
char *mg_md5(char buf[33], ...);
int mg_authorize_digest(struct mg_connection *c, FILE *fp);
size_t mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len);
int mg_url_decode(const char *src, size_t src_len, char *dst, size_t dst_len, int);
int mg_terminate_ssl(struct mg_connection *c, const char *cert);
int mg_forward(struct mg_connection *c, const char *addr);
void *mg_mmap(FILE *fp, size_t size);
void mg_munmap(void *p, size_t size);
// Templates support
struct mg_expansion {
const char *keyword;
void (*handler)(struct mg_connection *);
};
void mg_template(struct mg_connection *, const char *text,
struct mg_expansion *expansions);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // MONGOOSE_HEADER_INCLUDED

View File

@@ -20,6 +20,10 @@
#include "base64.h"
#include "misc.h"
#include "util.h"
#include "storage.h"
#ifdef HAVE_HTTP
# include "mongoose.h"
#endif
#define SSL_VERIFY_PEER (1)
@@ -31,6 +35,9 @@
#define CLEAN_SESSION false
static int run = 1;
#ifdef HAVE_HTTP
static struct mg_server *mgserver;
#endif
double number(JsonNode *j, char *element)
{
@@ -629,6 +636,141 @@ static void catcher(int sig)
exit(1);
}
#ifdef HAVE_HTTP
#if 0
static void push_message(struct mg_server *server, time_t current_time)
{
struct mg_connection *c;
char buf[90];
int len = sprintf(buf, "pussi %lu you", (unsigned long) current_time);
// Iterate over all connections, and push current time message to websocket ones.
for (c = mg_next(server, NULL); c != NULL; c = mg_next(server, c)) {
if (c->is_websocket) {
mg_websocket_write(c, 1, buf, len);
}
}
}
static void ws_push(struct mg_server *server, char *text)
{
struct mg_connection *c;
char buf[4096];
int len = snprintf(buf, sizeof(buf), "MQTT %s", text);
// Iterate over all connections, and push current time message to websocket ones.
for (c = mg_next(server, NULL); c != NULL; c = mg_next(server, c)) {
if (c->is_websocket) {
mg_websocket_write(c, 1, buf, len);
}
}
}
#endif
static int send_reply(struct mg_connection *conn)
{
if (conn->is_websocket) {
// This handler is called for each incoming websocket frame, one or more
// times for connection lifetime.
// Echo websocket data back to the client.
mg_websocket_write(conn, 1, conn->content, conn->content_len);
return conn->content_len == 4 && !memcmp(conn->content, "exit", 4) ? MG_FALSE : MG_TRUE;
} else {
mg_send_file(conn, "jp.html", NULL);
return MG_MORE;
}
}
static int ev_handler(struct mg_connection *conn, enum mg_event ev)
{
int n;
const char *ctype;
switch (ev) {
case MG_AUTH:
return MG_TRUE;
case MG_REQUEST:
ctype = mg_get_header(conn, "accept");
if (ctype != NULL)
fprintf(stderr, "ACCEPT: %s\n", ctype);
/* GET vars */
char buffer[1024];
int i, ret;
if ( mg_get_var(conn, "date", buffer, 1024) > 0) {
printf("XXXX = %s\n", buffer);
}
for(i=0; (ret = mg_get_var_n(conn, "date", buffer, 1024, i)) > 0; i++)
fprintf(stderr, "VAR: date[%d] = %s\n", i, buffer);
/* HEADERS */
for (n = 0; n < conn->num_headers; n++) {
struct mg_header *hh;
hh = &conn->http_headers[n];
fprintf(stderr, " %s=%s\n", hh->name, hh->value);
}
fprintf(stderr, "Conn from %s: %s %s\n",
conn->remote_ip,
conn->request_method,
conn->uri);
fprintf(stderr, "content-len = (%ld) %.*s\n",
conn->content_len,
(int)conn->content_len,
conn->content);
if (strncmp(conn->uri, "/api/", 5) != 0) {
return MG_FALSE; /* serve from document root */
}
if (!strcmp(conn->request_method, "POST")) {
return (MG_FALSE); /* Fail it */
}
/* GET */
if (!strcmp(conn->uri, "/api/users")) {
JsonNode *json;
if ((json = lister(NULL, NULL, 0, 0)) != NULL) {
char *js;
js = json_stringify(json, " ");
mg_printf_data(conn, js);
free(js);
}
#if 0
UT_string *text;
utstring_new(text);
utstring_bincpy(text, conn->content, conn->content_len);
printf("PP (%ld) %s\n", conn->content_len, utstring_body(text));
ws_push(mgserver, utstring_body(text));
mg_printf_data(conn, "Ta.\n");
utstring_free(text);
#endif
return MG_TRUE;
}
return send_reply(conn);
default:
return MG_FALSE;
}
}
#endif /* HAVE_HTTP */
void usage(char *prog)
{
printf("Usage: %s [options..] topic [topic ...]\n", prog);
@@ -644,6 +786,10 @@ void usage(char *prog)
printf(" --pubprefix -P republish prefix (dflt: no republish)\n");
printf(" --host -H MQTT host (localhost)\n");
printf(" --port -p MQTT port (1883)\n");
#ifdef HAVE_HTTP
printf(" --http-port <port> -A HTTP port (8083)\n");
printf(" --doc-root <directory> document root (./wdocs)\n");
#endif
exit(1);
}
@@ -659,6 +805,10 @@ int main(int argc, char **argv)
static struct udata udata, *ud = &udata;
struct utsname uts;
UT_string *clientid;
#ifdef HAVE_HTTP
int http_port = 8083;
char *doc_root = "./wdocs";
#endif
#ifdef HAVE_REDIS
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
#endif
@@ -671,6 +821,9 @@ int main(int argc, char **argv)
udata.skipdemo = TRUE;
udata.useredis = TRUE;
udata.revgeo = TRUE;
#ifdef HAVE_HTTP
mgserver = udata.server = mg_create_server(NULL, ev_handler);
#endif
if ((p = getenv("OTR_HOST")) != NULL) {
hostname = strdup(p);
@@ -705,15 +858,28 @@ int main(int argc, char **argv)
{ "host", required_argument, 0, 'H'},
{ "port", required_argument, 0, 'p'},
{ "storage", required_argument, 0, 'S'},
#ifdef HAVE_HTTP
{ "http-port", required_argument, 0, 'A'},
{ "doc-root", required_argument, 0, 2},
#endif
{0, 0, 0, 0}
};
int optindex = 0;
ch = getopt_long(argc, argv, "hDFGNRi:P:q:S:H:p:", long_options, &optindex);
ch = getopt_long(argc, argv, "hDFGNRi:P:q:S:H:p:A:", long_options, &optindex);
if (ch == -1)
break;
switch (ch) {
#ifdef HAVE_HTTP
case 'A': /* API */
http_port = atoi(optarg);
break;
case 2: /* no short char */
doc_root = strdup(optarg);
/* FIXME: check if isdir() */
break;
#endif
case 'D':
ud->skipdemo = FALSE;
break;
@@ -852,13 +1018,31 @@ int main(int argc, char **argv)
return rc;
}
#ifdef HAVE_HTTP
mg_set_option(udata.server, "listening_port", "127.0.0.1:8083");
// mg_set_option(udata.server, "listening_port", "8090,ssl://8091:cert.pem");
// mg_set_option(udata.server, "ssl_certificate", "cert.pem");
// mg_set_option(udata.server, "listening_port", "8091");
mg_set_option(udata.server, "document_root", doc_root);
mg_set_option(udata.server, "enable_directory_listing", "yes");
// mg_set_option(udata.server, "access_log_file", "access.log");
// mg_set_option(udata.server, "cgi_pattern", "**.cgi");
printf("Started on port %s\n", mg_get_option(udata.server, "listening_port"));
#endif
while (run) {
rc = mosquitto_loop(mosq, /* timeout */ 500, /* max-packets */ 1);
rc = mosquitto_loop(mosq, /* timeout */ 200, /* max-packets */ 1);
if (run && rc) {
fprintf(stderr, "loop sleep: rc=%d [%s]\n", rc, mosquitto_strerror(rc));
sleep(10);
mosquitto_reconnect(mosq);
}
#ifdef HAVE_HTTP
mg_poll_server(udata.server, 10);
#endif
}
mosquitto_disconnect(mosq);

View File

@@ -7,6 +7,10 @@
# include <hiredis/hiredis.h>
#endif
#ifdef HAVE_HTTP
# include "mongoose.h"
#endif
struct udata {
UT_array *topics; /* Array of topics to subscribe to */
@@ -20,6 +24,9 @@ struct udata {
int useredis; /* True if we should do Redis (if we have it) */
int revgeo; /* True (default) if we should do reverse Geo lookups */
int qos; /* Subscribe QoS */
#ifdef HAVE_HTTP
struct mg_server *server;
#endif
};
#endif

1
wdocs/test.txt Normal file
View File

@@ -0,0 +1 @@
Hello OwnTracks!