Commit 17cfecc5 authored by Sergey Lyubka's avatar Sergey Lyubka Committed by Cesanta Bot

Mongoose examples fixes

PUBLISHED_FROM=b47a5b6987ee8d9e7c9bae2bd4d2943d2e7f76a9
parent ebdd1876
PROG = captive_dns_server
MODULE_CFLAGS=-DMG_ENABLE_DNS_SERVER -DMG_ENABLE_IPV6
SSL_LIB=openssl
include ../examples.mk
......@@ -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);
......
#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);
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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment