From 17cfecc5c3a02a25e847afbdf8cac16408626e9f Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <lsm@cesanta.com>
Date: Mon, 20 Feb 2017 11:57:58 +0200
Subject: [PATCH] Mongoose examples fixes

PUBLISHED_FROM=b47a5b6987ee8d9e7c9bae2bd4d2943d2e7f76a9
---
 examples/captive_dns_server/Makefile           |  1 -
 examples/publish_subscribe/publish_subscribe.c |  7 +++++++
 examples/simple_crawler/simple_crawler.c       | 12 +++++++++---
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/examples/captive_dns_server/Makefile b/examples/captive_dns_server/Makefile
index ff6b2053f..60a81f1ab 100644
--- a/examples/captive_dns_server/Makefile
+++ b/examples/captive_dns_server/Makefile
@@ -1,4 +1,3 @@
 PROG = captive_dns_server
 MODULE_CFLAGS=-DMG_ENABLE_DNS_SERVER -DMG_ENABLE_IPV6
-SSL_LIB=openssl
 include ../examples.mk
diff --git a/examples/publish_subscribe/publish_subscribe.c b/examples/publish_subscribe/publish_subscribe.c
index 57d987ece..7de232552 100644
--- a/examples/publish_subscribe/publish_subscribe.c
+++ b/examples/publish_subscribe/publish_subscribe.c
@@ -36,9 +36,15 @@ static void server_handler(struct mg_connection *nc, int ev, void *p) {
     struct mg_connection *c;
 
     for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
+      if (!(c->flags |= MG_F_USER_2)) continue;  // Skip non-client connections
       mg_send(c, io->buf, io->len);
     }
     mbuf_remove(io, io->len);
+  } else if (ev == MG_EV_ACCEPT) {
+    char addr[32];
+    mg_sock_addr_to_str(p, addr, sizeof(addr),
+                        MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
+    printf("New client connected from %s\n", addr);
   }
 }
 
@@ -87,6 +93,7 @@ int main(int argc, char *argv[]) {
       fprintf(stderr, "Cannot connect to port %s\n", argv[1]);
       exit(EXIT_FAILURE);
     }
+    server_conn->flags |= MG_F_USER_2;  // Mark this as a client connection
 
     // Create a socketpair and give one end to the thread that reads stdin
     mg_socketpair(fds, SOCK_STREAM);
diff --git a/examples/simple_crawler/simple_crawler.c b/examples/simple_crawler/simple_crawler.c
index 1ae83f34e..88065d48c 100644
--- a/examples/simple_crawler/simple_crawler.c
+++ b/examples/simple_crawler/simple_crawler.c
@@ -1,8 +1,9 @@
 #include <stdio.h>
 #include <string.h>
-#include "mongoose.h"
 #include "../../../slre/slre.h"
+#include "mongoose.h"
 
+static const char *initial_url = "http://test.mosquitto.org";
 static const char *regex = "href=\"((https?://)[^\\s/'\"<>]+/?[^\\s'\"<>]*)";
 const int max_depth = 2;
 
@@ -44,7 +45,7 @@ int main(void) {
   struct mg_mgr mgr;
 
   mg_mgr_init(&mgr, NULL);
-  crawl_page(&mgr, "http://www.simpleweb.org/", ~0, 0);
+  crawl_page(&mgr, initial_url, ~0, 0);
 
   for (;;) {
     mg_mgr_poll(&mgr, 1000);
@@ -69,7 +70,12 @@ void crawl_page(struct mg_mgr *mgr, const char *url, size_t url_len,
   data->depth = depth;
 
   nc = mg_connect_http(mgr, event_handler, url, NULL, NULL);
-  nc->user_data = data;
+  if (nc != NULL) {
+    nc->user_data = data;
+  } else {
+    printf("Error connecting to [%s]\n", url);
+    free(data);
+  }
 }
 
 void handle_reply(struct mg_connection *nc, struct http_message *hm) {
-- 
GitLab