Commit c06abbc5 authored by NickCis's avatar NickCis

Implementing array varibles

parent c8e88e17
# 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 basic use of Mongoose embedded web server.
// $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 ret,
i;
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;
}
......@@ -4998,7 +4998,7 @@ 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;
......@@ -5014,11 +5014,15 @@ static int get_var(const char *data, size_t data_len, const char *name,
len = -1;
dst[0] = '\0';
int i = 0;
// data is "var1=val1&var2=val2...". Find variable first
for (p = data; p + name_len < e; p++) {
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;
......@@ -5046,10 +5050,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;
}
......
......@@ -119,6 +119,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