Skip to content

Commit

Permalink
Merge dev branch code named Fossa as next stable Mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Mikulicic committed Sep 8, 2015
1 parent 28eb251 commit 8927c9d
Show file tree
Hide file tree
Showing 126 changed files with 161,499 additions and 7,952 deletions.
10 changes: 5 additions & 5 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ Copyright (c) 2004-2013 Sergey Lyubka <[email protected]>
Copyright (c) 2013-2015 Cesanta Software Limited
All rights reserved

This code is dual-licensed: you can redistribute it and/or modify
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>.
license, see <http://www.gnu.org/licenses/>.

You are free to use this code under the terms of the GNU General
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 code under a commercial
license, as set out in <http://cesanta.com/>.
Alternatively, you can license this software under a commercial
license, as set out in <https://www.cesanta.com/license>.
115 changes: 51 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,55 @@

[![Join the chat at https://gitter.im/cesanta/mongoose](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cesanta/mongoose?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Mongoose is an embedded HTTP and WebSocket library that can turn anything
into a web server in 5 minutes by adding a few lines of C/C++ code.
On the market since 2004 with over 1 million cumulative downloads,
it's simplicity and flexibility has made it the top choice for
embedded software engineers.
![](https://img.shields.io/badge/license-GPL_2-green.svg "License")

Mongoose Binary is built on top of Mongoose Library which is used to serve Web
GUI on embedded devices, implement RESTful services, RPC frameworks (e.g.
JSON-RPC), handle telemetry data exchange and perform many other tasks. You'll find
it used across various industries including aerospace, manufacturing, finance,
research, automotive, gaming, IT and many more.
[Mongoose](https://www.cesanta.com/mongoose) is a
multi-protocol networking library written in C.
It provides easy to use event-driven interface that allows to implement
network protocols or scalable network applications with little effort.
Mongoose helps developers to manage the complexity of network programming
and let them concentrate on the logic, saving time and money.

> "Nothing overdone. Nothing less. So unbelievably easy to use. Just how good
> software should be!" - Pritin Tyagaraj, SAP
Mongoose has built-in support for several protocols, like
HTTP, Websocket, MQTT, mDNS. Example applications include
Websocket-based chat server, JSON-RPC server,
database server with RESTful API, MQTT broker, netcat with SSL and hexdump,
Raspberry PI camera video feed + led control, and more.

Mongoose is ideal for the embedded environments, it has been designed as
an open source platform for connecting devices and bringing them online.

[Download Mongoose Source Code here](http://hubs.ly/H0150FK0)

Are you an embedded developer? Working on an embedded task?
Check out our [embedded development products](http://hubs.ly/H0150sY0)
to make the right choice for your project.

# Technical Specification

- Works on Windows, Mac, UNIX/Linux, iPhone, Android eCos, QNX
and many other platforms
- CGI, SSI, SSL, Digest auth, Websocket, WEbDAV, Resumed download,
URL rewrite, file blacklist
- Custom error pages, Virtual hosts, IP-based ACL, Windows service,
HTTP/HTTPS client
- Simple and clean
[embedding API](https://github.com/cesanta/mongoose/blob/master/mongoose.h).
The source is in single
[mongoose.c](https://github.com/cesanta/mongoose/blob/master/mongoose.c) file
to make embedding easy
- Extremely lightweight, has a core of under 40kB and tiny runtime footprint
- Asynchronous, non-blocking core supporting single- or multi-threaded usage
# Features

* Cross-platform: works on Linux/UNIX, QNX, eCos, Windows, Android, iPhone, etc
* Single-threaded, asynchronous, non-blocking core with simple event-based API
* Builtin protocols:
- plain TCP, plain UDP, SSL/TLS (over TCP, one-way or two-way)
- HTTP client, HTTP server
- Websocket client, Websocket server
- JSON-RPC client, JSON-RPC server
- MQTT client, MQTT broker
- CoAP client, CoAP server
- DNS client, DNS server, async DNS resolver
* Tiny static and run-time footprint
* Source code is both ISO C and ISO C++ compliant
* Very easy to integrate: just copy
[mongoose.c](https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c) and
[mongoose.h](https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.h)
files to your build tree
* Extensively tested and production-ready, trusted by many blue chip businesses

# Examples & Documentation

- [User Guide](https://docs.cesanta.com/mongoose) - Detailed User Guide and API reference
- [examples](examples) - Collection of well-commented examples. To build any example,
go into respective directory and type `make`

# Dashboard Example

Expand All @@ -47,43 +60,17 @@ and many other platforms
](https://www.cesanta.com/contact)


# Contributions

# Licensing

Cesanta made Mongoose open source under GPLv2 for a reason. We are all
developers here and appreciate easy access to code and therefore seamless
integration. It's great to be able to play around with the software before
committing to it.

However, the GPLv2 open source license does not permit incorporating the
software into non-open source programs. In order to comply with GPLv2 licensing
you need to open the source code of your end product fully or alternatively
purchase a commercial license.

[Enquire about commercial licensing here](https://www.cesanta.com/contact)

# Documentation

- [Embedding Guide](https://github.com/cesanta/mongoose/blob/master/docs/Embed.md)
- [Config Options Reference](https://github.com/cesanta/mongoose/blob/master/docs/Options.md)
- [API Reference](https://github.com/cesanta/mongoose/blob/master/docs/API.md)
- [Android Build Tutorial](https://docs.cesanta.com/AndroidBuild.shtml)
- [Release Notes](https://github.com/cesanta/mongoose/blob/master/docs/ReleaseNotes.md)

# Mongoose Binary

This is our easy to use web server for web developers (PHP, Ruby, Python, etc)
and web designers. Available in three editions to suit your needs: free, pro
(USD 5) and dev edition (from USD 8). To install, simply download, double-click
to start and run browser - that's all!

[Download Mongoose Binary here](https://www.cesanta.com/mongoose)
People who have agreed to the
[Cesanta CLA](https://docs.cesanta.com/contributors_la.shtml)
can make contributions. Note that the CLA isn't a copyright
_assigment_ but rather a copyright _license_.
You retain the copyright on your contributions.

# Other products by Cesanta
# License

- [Smart.js](https://github.com/cesanta/smart.js) - Generic, hardware independent, full-stack IoT software platform
- [Fossa](http://github.com/cesanta/fossa) - Multi-protocol networking library
- [V7](https://github.com/cesanta/v7) - Embedded JavaScript engine
- [Frozen](https://github.com/cesanta/frozen) - JSON parser and generator
- [SLRE](https://github.com/cesanta/slre) - Super Light Regular Expression
library
Mongoose is released under
[GNU GPL v.2](http://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
Businesses have an option to get non-restrictive, royalty-free commercial
license and professional support from [Cesanta](https://www.cesanta.com).
9 changes: 2 additions & 7 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# Copyright (c) 2014 Cesanta Software
# All rights reserved

SUBDIRS = $(sort $(filter-out csharp/, $(dir $(wildcard */))))
SUBDIRS = $(sort $(dir $(wildcard */)))
X = $(SUBDIRS)
ifdef WINDIR
# appending the Winsock2 library at the end of the compiler
# invocation
CFLAGS_EXTRA += -lws2_32
endif

.PHONY: $(SUBDIRS)

all: $(SUBDIRS)

$(SUBDIRS):
@$(MAKE) CFLAGS_EXTRA="$(CFLAGS_EXTRA)" -C $@
@$(MAKE) -C $@

clean:
for d in $(SUBDIRS) ; do $(MAKE) -C $$d clean ; done
25 changes: 25 additions & 0 deletions examples/api_server/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
PROG = api_server
SOURCES = $(PROG).c sqlite3.c db_plugin_sqlite.c ../../mongoose.c
CFLAGS = -W -Wall -pthread $(CFLAGS_EXTRA)

ifeq ($(OS), Windows_NT)
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S), Linux)
CFLAGS += -ldl -lm
endif
endif

all: $(PROG)

$(PROG): $(SOURCES)
$(CC) $(SOURCES) -o $@ $(CFLAGS)

$(PROG).exe: $(SOURCES)
cl $(SOURCES) /I.. /MD /Fe$@

test: $(PROG)
sh unit_test.sh $$(pwd)/$(PROG)

clean:
rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
104 changes: 104 additions & 0 deletions examples/api_server/api_server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/

#include "db_plugin.h"

static const char *s_http_port = "8000";
static struct mg_serve_http_opts s_http_server_opts;
static int s_sig_num = 0;
static void *s_db_handle = NULL;
static const char *s_db_path = "api_server.db";
static const struct mg_str s_get_method = NS_STR("GET");
static const struct mg_str s_put_method = NS_STR("PUT");
static const struct mg_str s_delele_method = NS_STR("DELETE");

static void signal_handler(int sig_num) {
signal(sig_num, signal_handler);
s_sig_num = sig_num;
}

static int has_prefix(const struct mg_str *uri, const struct mg_str *prefix) {
return uri->len > prefix->len && memcmp(uri->p, prefix->p, prefix->len) == 0;
}

static int is_equal(const struct mg_str *s1, const struct mg_str *s2) {
return s1->len == s2->len && memcmp(s1->p, s2->p, s2->len) == 0;
}

static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
static const struct mg_str api_prefix = NS_STR("/api/v1");
struct http_message *hm = (struct http_message *) ev_data;
struct mg_str key;

switch (ev) {
case NS_HTTP_REQUEST:
if (has_prefix(&hm->uri, &api_prefix)) {
key.p = hm->uri.p + api_prefix.len;
key.len = hm->uri.len - api_prefix.len;
if (is_equal(&hm->method, &s_get_method)) {
db_op(nc, hm, &key, s_db_handle, API_OP_GET);
} else if (is_equal(&hm->method, &s_put_method)) {
db_op(nc, hm, &key, s_db_handle, API_OP_SET);
} else if (is_equal(&hm->method, &s_delele_method)) {
db_op(nc, hm, &key, s_db_handle, API_OP_DEL);
} else {
mg_printf(nc, "%s",
"HTTP/1.0 501 Not Implemented\r\n"
"Content-Length: 0\r\n\r\n");
}
} else {
mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */
}
break;
default:
break;
}
}

int main(int argc, char *argv[]) {
struct mg_mgr mgr;
struct mg_connection *nc;
int i;

/* Open listening socket */
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, s_http_port, ev_handler);
mg_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = "web_root";

/* Parse command line arguments */
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-D") == 0) {
mgr.hexdump_file = argv[++i];
} else if (strcmp(argv[i], "-f") == 0) {
s_db_path = argv[++i];
} else if (strcmp(argv[i], "-r") == 0) {
s_http_server_opts.document_root = argv[++i];
}
}

signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);

/* Open database */
if ((s_db_handle = db_open(s_db_path)) == NULL) {
fprintf(stderr, "Cannot open DB [%s]\n", s_db_path);
exit(EXIT_FAILURE);
}

/* Run event loop until signal is received */
printf("Starting RESTful server on port %s\n", s_http_port);
while (s_sig_num == 0) {
mg_mgr_poll(&mgr, 1000);
}

/* Cleanup */
mg_mgr_free(&mgr);
db_close(&s_db_handle);

printf("Exiting on signal %d\n", s_sig_num);

return 0;
}
19 changes: 19 additions & 0 deletions examples/api_server/db_plugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/

#ifndef DB_PLUGIN_HEADER_DEFINED
#define DB_PLUGIN_HEADER_DEFINED

#include "../../mongoose.h"

void *db_open(const char *db_path);
void db_close(void **db_handle);

enum { API_OP_GET, API_OP_SET, API_OP_DEL };

void db_op(struct mg_connection *nc, const struct http_message *hm,
const struct mg_str *key, void *db, int op);

#endif /* DB_PLUGIN_HEADER_DEFINED */
Loading

0 comments on commit 8927c9d

Please sign in to comment.