From f7812c9b0a1780db96d5f93ae4b2f33e932dafe0 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Fri, 1 Jul 2022 15:07:33 -0300 Subject: [PATCH] Added parsing of remote responses --- examples/udp-ssdp-search/main.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/examples/udp-ssdp-search/main.c b/examples/udp-ssdp-search/main.c index 35bb2b09..dc8f8cae 100644 --- a/examples/udp-ssdp-search/main.c +++ b/examples/udp-ssdp-search/main.c @@ -8,11 +8,26 @@ static const char *s_ssdp_url = "udp://239.255.255.250:1900"; static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { MG_DEBUG(("%p got event: %d %p %p", c, ev, ev_data, fn_data)); if (ev == MG_EV_OPEN) { - c->is_hexdumping = 1; + // c->is_hexdumping = 1; } else if (ev == MG_EV_RESOLVE) { // c->rem gets populated with multicast address. Store it in c->label memcpy(c->label, &c->rem, sizeof(c->rem)); } else if (ev == MG_EV_READ) { + MG_INFO(("Got a response")); + struct mg_http_message hm; + if (mg_http_parse((const char *) c->recv.buf, c->recv.len, &hm) > 0) { + size_t i, max = sizeof(hm.headers) / sizeof(hm.headers[0]); + // Iterate over request headers + for (i = 0; i < max && hm.headers[i].name.len > 0; i++) { + struct mg_str *k = &hm.headers[i].name, *v = &hm.headers[i].value; + if ((mg_vcasecmp(k, "SERVER") == 0) || + (mg_vcasecmp(k, "LOCATION") == 0)) { + printf("\t%.*s -> %.*s\n", (int) k->len, k->ptr, (int) v->len, + v->ptr); + } + } + printf("\n"); + } // Each response to the SSDP socket will change c->rem. // We can now do mg_printf(c, "haha"); to respond back to the remote side. // But in our case, we should restore the multicast address in order