From f3f9c925687d262547d0e203ad3216fa6f46fd5d Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Sun, 22 Sep 2019 11:03:31 +0300 Subject: . --- example/append.c | 48 ++++++++++++++++++++++++++++++++++++ example/cookie.c | 58 +++++++++++++++++++++++++++++++++++++++++++ example/makefile | 24 ++++++++++++++++++ example/upload.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ examples/append.c | 48 ------------------------------------ examples/cookie.c | 58 ------------------------------------------- examples/makefile | 24 ------------------ examples/upload.c | 73 ------------------------------------------------------- src/field.h | 6 ++--- 9 files changed, 206 insertions(+), 206 deletions(-) create mode 100644 example/append.c create mode 100644 example/cookie.c create mode 100644 example/makefile create mode 100644 example/upload.c delete mode 100644 examples/append.c delete mode 100644 examples/cookie.c delete mode 100644 examples/makefile delete mode 100644 examples/upload.c diff --git a/example/append.c b/example/append.c new file mode 100644 index 0000000..f0dfe2a --- /dev/null +++ b/example/append.c @@ -0,0 +1,48 @@ +#include +#include +#include + + +void handle_request() +{ + struct magi_request request; + if (magi_request_build_cgi(&request, 0, 0)) { + struct magi_field *a = magi_field_list_get(request.fields, "addon"); + if (a && a->data) { + FILE *file = fopen("file_to_append", "a"); + fputs(a->data, file); + fclose(file); + } + magi_request_destroy(&request); + } +} + +void print_preamble() +{ + puts("Content-type: application/xhtml+xml\r\n\r\n"); +} + +void print_webpage() +{ + puts( + "" + "" + "Append to File" + "" + "
" + "" + "" + "
" + "" + "" + ); +} + +int main(int argc, char const *argv[]) +{ + handle_request(); + print_preamble(); + print_webpage(); + return 0; +} diff --git a/example/cookie.c b/example/cookie.c new file mode 100644 index 0000000..8f4018b --- /dev/null +++ b/example/cookie.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + + +void print_preamble() +{ + puts( + "Set-Cookie:cookie=monstre\r\n" /* Important to set cookies before: */ + "Content-Type: application/xhtml+xml\r\n\r\n" + ); +} + +void print_webpage_top() +{ + puts( + "" + "" + "Cookie Listing and Setting" + "" + ); +} + +void read_and_print_cookies() +{ + struct magi_request request; + if (magi_request_build_cgi(&request, 0, 0)) { + struct magi_cookie_list *cookie; + for (cookie = request.cookies; cookie; cookie = cookie->next) { + printf( + "[%s] = [%s]
", + cookie->item.name, + cookie->item.data + ); + } + magi_request_destroy(&request); + } +} + +void print_webpage_bottom() +{ + puts( + "" + "" + ); +} + +int main(int argc, char const *argv[]) +{ + print_preamble(); + /* Following probably will be much more pleasant with use of templates. */ + print_webpage_top(); + read_and_print_cookies(); + print_webpage_bottom(); + return 0; +} diff --git a/example/makefile b/example/makefile new file mode 100644 index 0000000..de6b310 --- /dev/null +++ b/example/makefile @@ -0,0 +1,24 @@ +# Uncomment following to enable debug mode: +# DEBUG = yes + +CC = gcc +EXAMPLES = append cookie upload + +CFLAGS = -xc -ansi -pedantic -Wall +ifeq '$(DEBUG)' 'yes' +CFLAGS += -g -O0 +else +CFLAGS += -O3 +endif + +INCLUDE = -I ../src +LFLAGS = -L.. -lmagi + + +default: $(EXAMPLES) + +%: %.c + $(CC) $(CFLAGS) $(INCLUDE) $< $(LFLAGS) -o $@ + +clean: + rm -f $(EXAMPLES) diff --git a/example/upload.c b/example/upload.c new file mode 100644 index 0000000..9dc1efd --- /dev/null +++ b/example/upload.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include + +void tempfile_callback(struct magi_field *field, char *buffer, int len) +{ + static FILE *file = 0; + if (!strcmp(field->name, "data")) { + if (!file) { + remove(field->name); + file = fopen(field->name, "wb"); + } + if (len) { + fwrite(buffer, 1, len, file); + } + if (len < magi_parse_multipart_callback_size) { + fclose(file); + file = 0; + } + } else if (len) { + field->data = realloc(field->data, field->len + len + 1); + memcpy(field->data + field->len, buffer, len); + field->len += len; + field->data[field->len] = 0; + } +} + +void handle_request() +{ + struct magi_request request; + if (magi_request_build_cgi(&request, tempfile_callback, 0)) { + struct magi_field *name = magi_field_list_get(request.fields, "name"); + struct magi_field *data = magi_field_list_get(request.fields, "data"); + if (name && name->data && data) { + rename("data", name->data); + } + magi_request_destroy(&request); + } +} + +void print_preamble() +{ + puts("Content-type: application/xhtml+xml\r\n\r\n"); +} + +void print_webpage() +{ + puts( + "" + "" + "Upload File" + "" + "
" + "" + "" + "" + "
" + "" + "" + ); +} + +int main(int argc, char const *argv[]) +{ + handle_request(); + print_preamble(); + print_webpage(); + return 0; +} diff --git a/examples/append.c b/examples/append.c deleted file mode 100644 index f0dfe2a..0000000 --- a/examples/append.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include - - -void handle_request() -{ - struct magi_request request; - if (magi_request_build_cgi(&request, 0, 0)) { - struct magi_field *a = magi_field_list_get(request.fields, "addon"); - if (a && a->data) { - FILE *file = fopen("file_to_append", "a"); - fputs(a->data, file); - fclose(file); - } - magi_request_destroy(&request); - } -} - -void print_preamble() -{ - puts("Content-type: application/xhtml+xml\r\n\r\n"); -} - -void print_webpage() -{ - puts( - "" - "" - "Append to File" - "" - "
" - "" - "" - "
" - "" - "" - ); -} - -int main(int argc, char const *argv[]) -{ - handle_request(); - print_preamble(); - print_webpage(); - return 0; -} diff --git a/examples/cookie.c b/examples/cookie.c deleted file mode 100644 index 8f4018b..0000000 --- a/examples/cookie.c +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include -#include -#include - - -void print_preamble() -{ - puts( - "Set-Cookie:cookie=monstre\r\n" /* Important to set cookies before: */ - "Content-Type: application/xhtml+xml\r\n\r\n" - ); -} - -void print_webpage_top() -{ - puts( - "" - "" - "Cookie Listing and Setting" - "" - ); -} - -void read_and_print_cookies() -{ - struct magi_request request; - if (magi_request_build_cgi(&request, 0, 0)) { - struct magi_cookie_list *cookie; - for (cookie = request.cookies; cookie; cookie = cookie->next) { - printf( - "[%s] = [%s]
", - cookie->item.name, - cookie->item.data - ); - } - magi_request_destroy(&request); - } -} - -void print_webpage_bottom() -{ - puts( - "" - "" - ); -} - -int main(int argc, char const *argv[]) -{ - print_preamble(); - /* Following probably will be much more pleasant with use of templates. */ - print_webpage_top(); - read_and_print_cookies(); - print_webpage_bottom(); - return 0; -} diff --git a/examples/makefile b/examples/makefile deleted file mode 100644 index ecd7bc9..0000000 --- a/examples/makefile +++ /dev/null @@ -1,24 +0,0 @@ -# Uncomment following to enable debug mode: -# DEBUG = yes - -CC = gcc -EXAMPLES = append cookie upload - -CFLAGS = -xc -ansi -pedantic -Wall -ifeq '$(DEBUG)' 'yes' -CFLAGS += -g -O0 -else -CFLAGS += -O3 -endif - -INCLUDE = ../src -LFLAGS = -L.. -lmagi - - -default: $(EXAMPLES) - -%: %.c - $(CC) $(CFLAGS) -I $(INCLUDE) $< $(LFLAGS) -o $@ - -clean: - rm -f $(EXAMPLES) diff --git a/examples/upload.c b/examples/upload.c deleted file mode 100644 index 9dc1efd..0000000 --- a/examples/upload.c +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include -#include -#include - -void tempfile_callback(struct magi_field *field, char *buffer, int len) -{ - static FILE *file = 0; - if (!strcmp(field->name, "data")) { - if (!file) { - remove(field->name); - file = fopen(field->name, "wb"); - } - if (len) { - fwrite(buffer, 1, len, file); - } - if (len < magi_parse_multipart_callback_size) { - fclose(file); - file = 0; - } - } else if (len) { - field->data = realloc(field->data, field->len + len + 1); - memcpy(field->data + field->len, buffer, len); - field->len += len; - field->data[field->len] = 0; - } -} - -void handle_request() -{ - struct magi_request request; - if (magi_request_build_cgi(&request, tempfile_callback, 0)) { - struct magi_field *name = magi_field_list_get(request.fields, "name"); - struct magi_field *data = magi_field_list_get(request.fields, "data"); - if (name && name->data && data) { - rename("data", name->data); - } - magi_request_destroy(&request); - } -} - -void print_preamble() -{ - puts("Content-type: application/xhtml+xml\r\n\r\n"); -} - -void print_webpage() -{ - puts( - "" - "" - "Upload File" - "" - "
" - "" - "" - "" - "
" - "" - "" - ); -} - -int main(int argc, char const *argv[]) -{ - handle_request(); - print_preamble(); - print_webpage(); - return 0; -} diff --git a/src/field.h b/src/field.h index 170dc7c..a6fcff3 100644 --- a/src/field.h +++ b/src/field.h @@ -6,9 +6,9 @@ * Form Field */ struct magi_field { - char *name; /* name: free(name) is valid. */ - char *data; /* data: free(data) is valid. */ - int len; /* Length of data. */ + char *name; /* name: free(name) is valid. */ + char *data; /* data: free(data) is valid. */ + int len; /* Length of data. */ struct magi_param_list *params; /* Only used if field is for file. */ }; -- cgit v1.2.3