aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2020-03-13 19:22:39 +0300
committerAleksey Veresov <aleksey@veresov.pro>2020-03-13 19:22:39 +0300
commitac5114fdf74099a8ce8b9c154231e226e6883b94 (patch)
treee4224c1c37507366896470ce4e8e3467e8e407af
parent8f1e6faa7f548c9a8bcbcac71c8f239a6540b926 (diff)
downloadmagi-ac5114fdf74099a8ce8b9c154231e226e6883b94.tar
magi-ac5114fdf74099a8ce8b9c154231e226e6883b94.tar.xz
magi-ac5114fdf74099a8ce8b9c154231e226e6883b94.zip
[magi]
-rw-r--r--Makefile2
-rw-r--r--examples/Makefile2
-rw-r--r--examples/echo.c13
-rw-r--r--src/cgi.c22
-rw-r--r--src/param.c3
-rw-r--r--src/request.c9
-rw-r--r--src/response.c2
-rw-r--r--src/tools.c12
-rw-r--r--src/tools.h2
-rw-r--r--src/urlencoded.c2
10 files changed, 37 insertions, 32 deletions
diff --git a/Makefile b/Makefile
index c3117d0..ef0a20c 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,7 @@ CFLAGS = -xc -ansi -pedantic -Wall -Wextra
ifeq '$(DEBUG)' 'yes'
CFLAGS += -g -O0
else
-CFLAGS += -O3
+CFLAGS += -O3 -static
endif
# Interfacial files to compile:
diff --git a/examples/Makefile b/examples/Makefile
index 88af46d..4bf9eac 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -16,7 +16,7 @@ CFLAGS = -xc -ansi -pedantic -Wall -Wextra
ifeq '$(DEBUG)' 'yes'
CFLAGS += -g -O0
else
-CFLAGS += -O3
+CFLAGS += -O3 -static
endif
# Including magi library headers and setting linker to use it:
diff --git a/examples/echo.c b/examples/echo.c
index 7c840e2..52089d9 100644
--- a/examples/echo.c
+++ b/examples/echo.c
@@ -28,7 +28,11 @@ void list_params(magi_request *r, magi_params *current)
{
for (; current; current = current->next) {
magi_param *p = &current->item;
- magi_response_format(r, "[%s] is [%s]<br/>", p->name, p->data);
+ magi_response(r, "[");
+ magi_response(r, p->name);
+ magi_response(r, "] is [");
+ magi_response(r, p->data);
+ magi_response(r, "]<br/>");
}
}
@@ -37,8 +41,11 @@ void list_files(magi_request *r)
magi_files *current;
for (current = r->files; current; current = current->next) {
magi_file *f = &current->item;
- magi_response_format(r, "[%s] was [%s] on clientside<br/>",
- f->field, f->filename);
+ magi_response(r, "[");
+ magi_response(r, f->field);
+ magi_response(r, "] was [");
+ magi_response(r, f->filename);
+ magi_response(r, "] on clientside<br/>");
}
}
diff --git a/src/cgi.c b/src/cgi.c
index 291a57f..810d85e 100644
--- a/src/cgi.c
+++ b/src/cgi.c
@@ -15,7 +15,7 @@
#include <stdlib.h>
#include <string.h>
-extern char **environ;
+extern char **const environ;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@@ -32,7 +32,9 @@ static char *plain_env(char *env_name)
static char *lower_env(char *env_name)
{
- return magi_str_lowercase(plain_env(env_name));
+ char *env = plain_env(env_name);
+ magi_str_lowercase(env);
+ return env;
}
static void cgi_http_env(magi_request *r)
@@ -42,21 +44,15 @@ static void cgi_http_env(magi_request *r)
r->meta = 0;
for (env = environ; *env; ++env) {
magi_param meta;
- char *name_end;
- int dlen;
- if (!strncmp(*env, "HTTP_COOKIE=", 12) ||
- !strncmp(*env, "QUERY_STRING=", 13)) {
- continue;
- }
- /* At least one '=' must be in *env, according to format. */
- name_end = strchr(*env, '=');
- dlen = strlen(name_end + 1);
- len += name_end - *env + dlen;
+ char *name_end = strchr(*env, '=');
+ int nlen = name_end - *env;
+ int dlen = strlen(name_end + 1);
+ len += nlen + dlen;
if (len > r->limits.params_meta && r->limits.params_meta) {
r->error = magi_error_limit;
return;
}
- meta.name = magi_str_create_copy(*env, name_end - *env);
+ meta.name = magi_str_create_copy(*env, nlen);
meta.data = magi_str_create_copy(name_end + 1, dlen);
magi_params_add(&r->meta, &meta);
}
diff --git a/src/param.c b/src/param.c
index dd89a48..9c94555 100644
--- a/src/param.c
+++ b/src/param.c
@@ -19,6 +19,9 @@ void magi_params_set(magi_params **params, magi_param *newitem)
if (!*params) {
magi_params_add(params, newitem);
} else if (!strcmp((*params)->item.name, newitem->name)) {
+ free((*params)->item.name);
+ free((*params)->item.data);
+ (*params)->item.name = newitem->name;
(*params)->item.data = newitem->data;
} else {
magi_params_set(&(*params)->next, newitem);
diff --git a/src/request.c b/src/request.c
index 41b77fe..ce0f84f 100644
--- a/src/request.c
+++ b/src/request.c
@@ -10,10 +10,10 @@ void magi_request_init(magi_request *request)
request->callback.act = 0;
request->callback.userdata = 0;
request->callback.addon_max = 1024;
- request->limits.cookies = 1024 * 16;
- request->limits.params_meta = 1024 * 16;
- request->limits.params_head = 1024 * 16;
- request->limits.params_body = 1024 * 16;
+ request->limits.cookies = 0;
+ request->limits.params_meta = 0;
+ request->limits.params_head = 0;
+ request->limits.params_body = 0;
}
}
@@ -25,6 +25,7 @@ static void request_free(magi_request *request)
free(request->head);
free(request->body);
free(request->files);
+ free(request->document_root);
free(request->method);
free(request->host);
free(request->script);
diff --git a/src/response.c b/src/response.c
index d7d2c86..f7a6f67 100644
--- a/src/response.c
+++ b/src/response.c
@@ -91,7 +91,7 @@ void magi_response_content_length(magi_request *r, int length)
void magi_response_content_type(magi_request *r, const char *type)
{
magi_param addon;
- if (r->response->head_done || !type) {
+ if (r->response->head_done) {
return;
}
addon.name = magi_str_create_copy("Content-Type", 12);
diff --git a/src/tools.c b/src/tools.c
index d6170bc..3d3a51e 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -5,15 +5,11 @@
#include <string.h>
-char *magi_str_lowercase(char *str)
+void magi_str_lowercase(char *str)
{
- if (!str) {
- return str;
- }
- for (; *str; ++str) {
+ for (; str && *str; ++str) {
*str = tolower(*str);
}
- return str;
}
char *magi_str_create_copy(const char *first, int len)
@@ -40,6 +36,6 @@ void magi_str_add(char **str, int *len, int *size, char c)
*size *= 2;
*str = realloc(*str, *size);
}
- *str[*len] = c;
- *str[++*len] = 0;
+ (*str)[*len] = c;
+ (*str)[++*len] = 0;
}
diff --git a/src/tools.h b/src/tools.h
index 0152f35..c84b7fd 100644
--- a/src/tools.h
+++ b/src/tools.h
@@ -2,7 +2,7 @@
#define MAGI_INCLUDED_TOOLS
-char *magi_str_lowercase(char *str);
+void magi_str_lowercase(char *str);
/* Results of both create functions are malloced, so need to be freed. */
char *magi_str_create_copy(const char *first, int len);
diff --git a/src/urlencoded.c b/src/urlencoded.c
index 4f9efe8..b0891dc 100644
--- a/src/urlencoded.c
+++ b/src/urlencoded.c
@@ -87,6 +87,7 @@ static void state_parse_name(automata *a, char c)
return;
}
a->s = state_parse_data;
+ return;
}
magi_str_add(&a->name, &a->nlen, &a->nsize, c);
}
@@ -114,6 +115,7 @@ static void state_parse_data(automata *a, char c)
}
add_to_list(a);
a->s = state_parse_name;
+ return;
}
magi_str_add(&a->data, &a->dlen, &a->dsize, c);
}