static void sj_conf_emit_obj(struct emit_ctx *ctx, const struct sj_conf_entry *schema, int num_entries, int indent) { mbuf_append(ctx->out, "{", 1); bool first = true; for (int i = 0; i < num_entries;) { const struct sj_conf_entry *e = schema + i; if (sj_conf_value_eq(ctx->cfg, ctx->base, e)) { i += (e->type == CONF_TYPE_OBJECT ? e->num_desc : 1); continue; } if (!first) { mbuf_append(ctx->out, ",", 1); } else { first = false; } if (ctx->pretty) sj_emit_indent(ctx->out, indent); sj_json_emit_str(ctx->out, mg_mk_str(e->key), 1); mbuf_append(ctx->out, ": ", (ctx->pretty ? 2 : 1)); switch (e->type) { case CONF_TYPE_INT: { char buf[20]; int len = snprintf(buf, sizeof(buf), "%d", *((int *) (((char *) ctx->cfg) + e->offset))); mbuf_append(ctx->out, buf, len); break; } case CONF_TYPE_BOOL: { int v = *((int *) (((char *) ctx->cfg) + e->offset)); const char *s; int len; if (v != 0) { s = "true"; len = 4; } else { s = "false"; len = 5; } mbuf_append(ctx->out, s, len); break; } case CONF_TYPE_STRING: { const char *v = *((char **) (((char *) ctx->cfg) + e->offset)); sj_json_emit_str(ctx->out, mg_mk_str(v), 1); break; } case CONF_TYPE_OBJECT: { sj_conf_emit_obj(ctx, schema + i + 1, e->num_desc, indent + 2); break; } } i++; if (e->type == CONF_TYPE_OBJECT) i += e->num_desc; if (ctx->cb != NULL) ctx->cb(ctx->out, ctx->cb_param); } if (ctx->pretty) sj_emit_indent(ctx->out, indent - 2); mbuf_append(ctx->out, "}", 1); }
static void handle_save(struct mg_connection *nc, struct http_message *hm) { // Get form variables and store settings values mg_get_http_var(&hm->body, "setting1", s_settings.setting1, sizeof(s_settings.setting1)); mg_get_http_var(&hm->body, "setting2", s_settings.setting2, sizeof(s_settings.setting2)); // Send response mg_http_send_redirect(nc, 302, mg_mk_str("/"), mg_mk_str(NULL)); }
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { switch (ev) { case MG_EV_WEBSOCKET_HANDSHAKE_DONE: { /* New websocket connection. Tell everybody. */ broadcast(nc, mg_mk_str("++ joined")); break; } case MG_EV_WEBSOCKET_FRAME: { struct websocket_message *wm = (struct websocket_message *) ev_data; /* New websocket message. Tell everybody. */ struct mg_str d = {(char *) wm->data, wm->size}; broadcast(nc, d); break; } case MG_EV_CLOSE: { /* Disconnect. Tell everybody. */ if (is_websocket(nc)) { broadcast(nc, mg_mk_str("-- left")); } break; } } }
/* * Make a new MQTT client. * * Arguments: * url: url where to connect to * opts: option object * * Recognized option object properties: * * - clientId: string; mqtt client id. defaults to * Math.random().toString(16).substr(2, 10) * * Example: * * var client = MQTT.connect('mqtt://test.mosquitto.org'); * * client.on('connect', function () { * client.subscribe('presence'); * client.publish('presence', 'Hello mqtt'); * }); * * client.on('message', function (topic, message) { * console.log(message); * }); * * TLS can be enabled by choosing the `mqtts://` protocol. In that * case the default port is 8883 as defined by IANA. * * The API is modeled after https://www.npmjs.com/package/mqtt. * */ enum v7_err sj_mqtt_connect(struct v7 *v7, v7_val_t *res) { enum v7_err rcode = V7_OK; const char *url; size_t len; struct mg_str host, scheme; unsigned int port; struct mg_connection *nc; struct user_data *ud; char *url_with_port = NULL; int use_ssl = 0; v7_val_t urlv = v7_arg(v7, 0), opts = v7_arg(v7, 1); v7_val_t client_id; v7_val_t proto = v7_get(v7, v7_get(v7, v7_get_global(v7), "MQTT", ~0), "proto", ~0); if (!v7_is_string(urlv)) { rcode = v7_throwf(v7, "Error", "invalid url string"); goto clean; } url = v7_get_string(v7, &urlv, &len); if (mg_parse_uri(mg_mk_str(url), &scheme, NULL, &host, &port, NULL, NULL, NULL) < 0) { rcode = v7_throwf(v7, "Error", "invalid url string"); goto clean; } if (mg_vcmp(&scheme, "mqtt") == 0) { url += sizeof("mqtt://") - 1; } else if (mg_vcmp(&scheme, "mqtts") == 0) { url += sizeof("mqtts://") - 1; use_ssl = 1; } else { rcode = v7_throwf(v7, "Error", "unsupported protocol"); goto clean; } client_id = v7_get(v7, opts, "clientId", ~0); if (v7_is_undefined(client_id)) { rcode = v7_exec(v7, "Math.random().toString(16).substr(2,8)", &client_id); if (rcode != V7_OK) { goto clean; } } if (port == 0) { if (asprintf(&url_with_port, "%.*s%s", (int) host.len, host.p, (use_ssl ? ":8883" : ":1883")) < 0) { rcode = v7_throwf(v7, "Error", "Out of memory"); goto clean; } } nc = mg_connect(&sj_mgr, url_with_port ? url_with_port : url, mqtt_ev_handler); if (nc == NULL) { rcode = v7_throwf(v7, "Error", "cannot create connection"); goto clean; } if (use_ssl) { #ifdef MG_ENABLE_SSL mg_set_ssl(nc, NULL, NULL); #else rcode = v7_throwf(v7, "Error", "SSL not enabled"); goto clean; #endif } mg_set_protocol_mqtt(nc); *res = v7_mk_object(v7); v7_set_proto(v7, *res, proto); ud = calloc(1, sizeof(*ud)); if (ud == NULL) { rcode = v7_throwf(v7, "Error", "Out of memory"); goto clean; } ud->v7 = v7; ud->client = *res; ud->client_id = strdup(v7_get_cstring(v7, &client_id)); if (ud->client_id == NULL) { free(ud); rcode = v7_throwf(v7, "Error", "Out of memory"); goto clean; } nc->user_data = ud; v7_own(v7, &ud->client); v7_def(v7, *res, "_nc", ~0, _V7_DESC_HIDDEN(1), v7_mk_foreign(v7, nc)); clean: free(url_with_port); return rcode; }
struct mg_str mg_url_encode(const struct mg_str src) { return mg_url_encode_opt(src, mg_mk_str("._-$,;~()/"), 0); }