Commit 29454e22 authored by Sergey Lyubka's avatar Sergey Lyubka

Merge pull request #524 from NickCis/array_variables

Implementing array varibles
parents b9fead4d d6cbf9f7
# Copyright (c) 2014 Cesanta Software
# All rights reserved
PROG = array_vars
CFLAGS = -W -Wall -I../.. -pthread -g -O0 $(CFLAGS_EXTRA)
SOURCES = $(PROG).c ../../mongoose.c
all: $(PROG)
run: $(PROG)
./$(PROG)
$(PROG): $(SOURCES) Makefile
$(CC) -o $(PROG) $(SOURCES) $(CFLAGS)
win:
wine cl $(SOURCES) /MD /nologo /DNDEBUG /O1 /I../.. /Fe$(PROG).exe
wine $(PROG).exe
clean:
rm -rf $(PROG) *.exe *.dSYM *.obj *.exp .*o *.lib *.gc*
// Copyright (c) 2014 Cesanta Software
// All rights reserved
//
// This example demostrates how to use array get variables using mg_get_n_var
// $Date: 2014-09-09 22:20:23 UTC $
#include <stdio.h>
#include <string.h>
#include "mongoose.h"
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
switch (ev) {
case MG_AUTH: return MG_TRUE;
case MG_REQUEST:
{
mg_printf_data(conn, "Hello! Requested URI is [%s] ", conn->uri);
char buffer[1024];
int i, ret;
for(i=0; (ret = mg_get_n_var(conn, "foo[]", buffer, 1024, i)) > 0; i++)
mg_printf_data(conn, "\nfoo[%d] = %s", i, buffer);
return MG_TRUE;
}
default: return MG_FALSE;
}
}
int main(void) {
struct mg_server *server;
// Create and configure the server
server = mg_create_server(NULL, ev_handler);
mg_set_option(server, "listening_port", "8080");
// Serve request. Hit Ctrl-C to terminate the program
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) {
mg_poll_server(server, 1000);
}
// Cleanup, and free server instance
mg_destroy_server(&server);
return 0;
}
......@@ -5015,10 +5015,10 @@ struct mg_connection *mg_next(struct mg_server *s, struct mg_connection *c) {
}
static int get_var(const char *data, size_t data_len, const char *name,
char *dst, size_t dst_len) {
char *dst, size_t dst_len, int n) {
const char *p, *e, *s;
size_t name_len;
int len;
int i, len;
if (dst == NULL || dst_len == 0) {
len = -2;
......@@ -5026,6 +5026,7 @@ static int get_var(const char *data, size_t data_len, const char *name,
len = -1;
dst[0] = '\0';
} else {
i = 0;
name_len = strlen(name);
e = data + data_len;
len = -1;
......@@ -5036,6 +5037,9 @@ static int get_var(const char *data, size_t data_len, const char *name,
if ((p == data || p[-1] == '&') && p[name_len] == '=' &&
!mg_strncasecmp(name, p, name_len)) {
if(n != i++)
continue;
// Point p to variable value
p += name_len + 1;
......@@ -5063,10 +5067,15 @@ static int get_var(const char *data, size_t data_len, const char *name,
int mg_get_var(const struct mg_connection *conn, const char *name,
char *dst, size_t dst_len) {
return mg_get_n_var(conn, name, dst, dst_len, 0);
}
int mg_get_n_var(const struct mg_connection *conn, const char *name,
char *dst, size_t dst_len, int n) {
int len = get_var(conn->query_string, conn->query_string == NULL ? 0 :
strlen(conn->query_string), name, dst, dst_len);
strlen(conn->query_string), name, dst, dst_len, n);
if (len == -1) {
len = get_var(conn->content, conn->content_len, name, dst, dst_len);
len = get_var(conn->content, conn->content_len, name, dst, dst_len, n);;
}
return len;
}
......
......@@ -121,6 +121,8 @@ const char *mg_get_header(const struct mg_connection *, const char *name);
const char *mg_get_mime_type(const char *name, const char *default_mime_type);
int mg_get_var(const struct mg_connection *conn, const char *var_name,
char *buf, size_t buf_len);
int mg_get_n_var(const struct mg_connection *conn, const char *var_name,
char *buf, size_t buf_len, int n);
int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
int mg_parse_multipart(const char *buf, int buf_len,
char *var_name, int var_name_len,
......
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