mongoose/examples/huge-response/main.c

92 lines
3.0 KiB
C
Raw Normal View History

2021-12-27 11:25:28 +08:00
// Copyright (c) 2021 Cesanta Software Limited
// All rights reserved
//
// Example that demonstrates how to send a large responses with limited memory.
// We're going to send a JSON array of many integer values, s_data.
// The idea is to send a response in small chunks, and let the client request
// the next chunk.
// Periodically, s_data changes, which is tracked by s_version.
// Client requests a range and a version, to ensure data integrity.
//
// 1. Start this server, type `make`
// 2. Open http://localhost:8000 in your browser
#include "mongoose.h"
static const char *s_listen_on = "http://localhost:8000";
static const char *s_root_dir = "web_root";
2022-06-10 19:14:42 +08:00
#define DATA_SIZE 10000 // Total number of elements
#define CHUNK_SIZE 100 // Max number returned in one API call
static int s_data[DATA_SIZE]; // Simulate some complex big data
static long s_version = 0; // Data "version"
2021-12-27 11:25:28 +08:00
2022-06-10 19:14:42 +08:00
static long getparam(struct mg_http_message *hm, const char *json_path) {
2021-12-27 11:25:28 +08:00
double dv = 0;
2022-06-10 19:14:42 +08:00
mg_json_get_num(hm->body, json_path, &dv);
2021-12-27 11:25:28 +08:00
return dv;
}
2022-08-14 03:09:51 +08:00
static size_t printdata(mg_pfn_t out, void *ptr, va_list *ap) {
2022-06-10 19:14:42 +08:00
unsigned start = va_arg(*ap, unsigned);
unsigned max = start + CHUNK_SIZE;
const char *comma = "";
size_t n = 0;
if (max > DATA_SIZE) max = DATA_SIZE;
while (start < max) {
2022-08-14 03:34:20 +08:00
n += mg_xprintf(out, ptr, "%s%d", comma, s_data[start]);
2022-06-10 19:14:42 +08:00
comma = ",";
start++;
}
return n;
}
2021-12-27 11:25:28 +08:00
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = ev_data;
if (mg_http_match_uri(hm, "/api/data")) {
2022-08-14 03:09:51 +08:00
const char *headers = "content-type: text/json\r\n";
2022-06-10 19:14:42 +08:00
long start = getparam(hm, "$.start");
long version = getparam(hm, "$.version");
2022-08-14 03:09:51 +08:00
MG_DEBUG(("%.*s", (int) hm->body.len, hm->body.ptr));
2021-12-27 11:25:28 +08:00
if (version > 0 && version != s_version) {
// Version mismatch: s_data has changed while client fetches it
// Tell client to restart
2023-04-14 21:11:12 +08:00
mg_http_reply(c, 200, headers, "{%m:%m, %m:%ld}", mg_print_esc, 0,
"error", mg_print_esc, 0, "wrong version", mg_print_esc,
0, "version", version);
2021-12-27 11:25:28 +08:00
} else {
// Return data, up to CHUNK_SIZE elements
2023-04-14 21:11:12 +08:00
mg_http_reply(c, 200, headers, "{%m:%ld,%m:%ld,%m:[%M]}", mg_print_esc,
0, "version", s_version, mg_print_esc, 0, "start", start,
mg_print_esc, 0, "data", printdata, start);
2021-12-27 11:25:28 +08:00
}
} else {
struct mg_http_serve_opts opts = {0};
opts.root_dir = s_root_dir;
mg_http_serve_dir(c, hm, &opts);
}
}
(void) fn_data;
}
static void timer_fn(void *arg) {
for (int i = 0; i < DATA_SIZE; i++) {
s_data[i] = rand();
}
s_version++;
(void) arg;
}
int main(void) {
struct mg_mgr mgr;
mg_mgr_init(&mgr);
srand(time(NULL));
2022-06-10 19:14:42 +08:00
mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, NULL);
2021-12-27 11:25:28 +08:00
mg_http_listen(&mgr, s_listen_on, fn, NULL);
MG_INFO(("Listening on %s", s_listen_on));
2021-12-27 11:25:28 +08:00
for (;;) mg_mgr_poll(&mgr, 1000);
mg_mgr_free(&mgr);
return 0;
}