mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-03 13:28:44 +00:00
Update huge response. More tests
This commit is contained in:
1 parent
8fc6f3f744
commit
dfca896a64
8 files changed
+56
-1300
No files matched your search
@@ -11,51 +11,57 @@
|
||||
// 1. Start this server, type `make`
|
||||
// 2. Open http://localhost:8000 in your browser
|
||||
|
||||
#include "mjson.h"
|
||||
#include "mongoose.h"
|
||||
|
||||
static const char *s_listen_on = "http://localhost:8000";
|
||||
static const char *s_root_dir = "web_root";
|
||||
|
||||
#define DATA_SIZE 10000 // Total number of elements
|
||||
#define CHUNK_SIZE 1000 // Max number returned in one API call
|
||||
static int s_data[DATA_SIZE]; // Simulate some complex big data
|
||||
static unsigned s_version = 0; // Data "version"
|
||||
#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"
|
||||
|
||||
static double getparam(struct mg_http_message *hm, const char *json_path) {
|
||||
static long getparam(struct mg_http_message *hm, const char *json_path) {
|
||||
double dv = 0;
|
||||
mjson_get_number(hm->body.ptr, hm->body.len, json_path, &dv);
|
||||
mg_json_get_num(hm->body, json_path, &dv);
|
||||
return dv;
|
||||
}
|
||||
|
||||
static size_t printdata(char *buf, size_t len, va_list *ap) {
|
||||
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) {
|
||||
n += mg_snprintf(buf ? buf + n : 0, len < n ? 0 : len - n, "%s%d", comma,
|
||||
s_data[start]);
|
||||
comma = ",";
|
||||
start++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
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")) {
|
||||
unsigned start = getparam(hm, "$.start");
|
||||
unsigned version = getparam(hm, "$.version");
|
||||
long start = getparam(hm, "$.start");
|
||||
long version = getparam(hm, "$.version");
|
||||
char *response = NULL;
|
||||
MG_INFO(("%.*s", (int) hm->body.len, hm->body.ptr));
|
||||
if (version > 0 && version != s_version) {
|
||||
// Version mismatch: s_data has changed while client fetches it
|
||||
// Tell client to restart
|
||||
response = mjson_aprintf("{%Q:%Q, %Q:%u}", "error", "wrong version",
|
||||
"version", version);
|
||||
response = mg_mprintf("{%Q:%Q, %Q:%ld}", "error", "wrong version",
|
||||
"version", version);
|
||||
} else {
|
||||
// Return data, up to CHUNK_SIZE elements
|
||||
unsigned max = start + CHUNK_SIZE;
|
||||
const char *comma = "";
|
||||
if (max > DATA_SIZE) max = DATA_SIZE;
|
||||
mjson_printf(mjson_print_dynamic_buf, &response, "{%Q:%u,%Q:%u,%Q:[",
|
||||
"version", s_version, "start", start, "data");
|
||||
while (start < max) {
|
||||
mjson_printf(mjson_print_dynamic_buf, &response, "%s%d", comma,
|
||||
s_data[start]);
|
||||
comma = ",";
|
||||
start++;
|
||||
}
|
||||
mjson_printf(mjson_print_dynamic_buf, &response, "]}");
|
||||
response = mg_mprintf("{%Q:%ld,%Q:%ld,%Q:[%M]}", "version", s_version,
|
||||
"start", start, "data", printdata, start);
|
||||
}
|
||||
mg_http_reply(c, 200, "Content-Type: text/json\r\n", "%s", response);
|
||||
MG_INFO(("%s", response));
|
||||
free(response);
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {0};
|
||||
@@ -78,7 +84,7 @@ int main(void) {
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
srand(time(NULL));
|
||||
mg_timer_add(&mgr, 1000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, NULL);
|
||||
mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, NULL);
|
||||
mg_http_listen(&mgr, s_listen_on, fn, NULL);
|
||||
MG_INFO(("Listening on %s", s_listen_on));
|
||||
for (;;) mg_mgr_poll(&mgr, 1000);
|
||||
|
||||
Reference in new issue
Block a user