aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-11-19 19:52:23 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-11-19 19:52:23 +0300
commit03c27c8542d23a5e4072f0c080c396ce608c1d50 (patch)
treea80e7de3bffbf1e17d2f19dc8a50f9bdd3ececc5
parent42bb3d37c156df14560347d4c7df3f0dd0551389 (diff)
downloadmagi-03c27c8542d23a5e4072f0c080c396ce608c1d50.tar
magi-03c27c8542d23a5e4072f0c080c396ce608c1d50.tar.xz
magi-03c27c8542d23a5e4072f0c080c396ce608c1d50.zip
.
-rw-r--r--examples/append.c2
-rw-r--r--examples/cookie.c2
-rw-r--r--examples/echo.c2
-rw-r--r--examples/fcgi.c11
-rw-r--r--examples/upload.c7
-rw-r--r--src/cgi.h6
-rw-r--r--src/fastcgi.h12
-rw-r--r--src/field.c53
-rw-r--r--src/field.h41
-rw-r--r--src/file.c0
-rw-r--r--src/file.h0
-rw-r--r--src/multipart.h6
-rw-r--r--src/param.h2
-rw-r--r--src/request.h7
14 files changed, 39 insertions, 112 deletions
diff --git a/examples/append.c b/examples/append.c
index e51745a..30060fa 100644
--- a/examples/append.c
+++ b/examples/append.c
@@ -32,7 +32,7 @@ void response_request(struct magi_request * req, struct magi_response * res)
int main(int argc, char const * argv[])
{
struct magi_request request;
- if (magi_cgi(&request, 0, 0)) {
+ if (magi_cgi(&request, 0, 0, 0)) {
struct magi_response response;
response_request(&request, &response);
magi_cgi_response(&response);
diff --git a/examples/cookie.c b/examples/cookie.c
index 47929cb..f99f07f 100644
--- a/examples/cookie.c
+++ b/examples/cookie.c
@@ -30,7 +30,7 @@ void response_request(struct magi_request * req, struct magi_response * res)
int main(int argc, char const * argv[])
{
struct magi_request request;
- if (magi_cgi(&request, 0, 0)) {
+ if (magi_cgi(&request, 0, 0, 0)) {
struct magi_response response;
response_request(&request, &response);
magi_cgi_response(&response);
diff --git a/examples/echo.c b/examples/echo.c
index 6901b71..16e9d56 100644
--- a/examples/echo.c
+++ b/examples/echo.c
@@ -112,7 +112,7 @@ void response_request(struct magi_request * req, struct magi_response * res)
int main(int argc, char const * argv[])
{
struct magi_request request;
- if (magi_cgi(&request, 0, 0)) {
+ if (magi_cgi(&request, 0, 0, 0)) {
struct magi_response response;
response_request(&request, &response);
magi_cgi_response(&response);
diff --git a/examples/fcgi.c b/examples/fcgi.c
index 9cdbe90..d6cc478 100644
--- a/examples/fcgi.c
+++ b/examples/fcgi.c
@@ -21,21 +21,24 @@ void response_request(struct magi_request * req, struct magi_resopnse * res)
int main(int argc, char const * argv[])
{
struct magi_session session;
- if (magi_fcgi(&session)) {
+ int sock = magi_socket_inet("localhost", 9973);
+ /* E.g. also magi_socket_file("fcgi.sock") can be used. */
+ if (magi_fcgi(&session, sock)) {
struct magi_request request;
while (magi_fcgi_accept(&request, &session)) {
if (!request.error) {
struct magi_response response;
response_request(&request, &response);
- magi_fcgi_response(response);
- magi_reponse_destroy(response);
+ magi_fcgi_response(&response, &session);
+ magi_response_destroy(&response);
} else {
magi_fcgi_error(request.error, &session);
}
magi_request_destroy(&request);
}
}
- puts(session.error->message);
+ puts(magi_error_message(session.error));
magi_session_destroy(&session);
+ magi_socket_close(sock);
return 0;
}
diff --git a/examples/upload.c b/examples/upload.c
index 5fe3338..95c7f87 100644
--- a/examples/upload.c
+++ b/examples/upload.c
@@ -6,7 +6,10 @@
#include <string.h>
-void tempfile_callback(struct magi_field * field, char * buffer, int len)
+void tempfile_callback(struct magi_file * file,
+ char * buffer,
+ int len,
+ void * _)
{
static FILE * file = 0;
if (!strcmp(field->name, "data")) {
@@ -57,7 +60,7 @@ void response_request(struct magi_request * req, struct magi_response * res)
int main(int argc, char const * argv[])
{
struct magi_request request;
- if (magi_cgi(&request, tempfile_callback, 0)) {
+ if (magi_cgi(&request, tempfile_callback, 0, 0)) {
struct magi_response response;
response_request(&request, &response);
magi_cgi_response(&response);
diff --git a/src/cgi.h b/src/cgi.h
index 331e306..ee818ac 100644
--- a/src/cgi.h
+++ b/src/cgi.h
@@ -14,8 +14,10 @@ int magi_cgi(struct magi_request * request,
/* Null callback disables callback system. */
void (*callback)(struct magi_field * field,
char * buffer,
- int len),
- int max_post);
+ int len,
+ void * thing),
+ void * thing,
+ int max_post);
#endif
diff --git a/src/fastcgi.h b/src/fastcgi.h
index 1c2cda7..d2199b4 100644
--- a/src/fastcgi.h
+++ b/src/fastcgi.h
@@ -11,12 +11,20 @@ struct magi_session {
struct magi_socket_list * sockets;
};
-int magi_fcgi(struct magi_session * session);
+int magi_fcgi(struct magi_session * session, int socket);
/*
* Returns null if succeed, otherwise error code.
*/
-int magi_accept(struct magi_request * request, struct magi_session * session);
+int magi_fcgi_accept(
+ struct magi_request * request,
+ struct magi_session * session,
+ /* Callback will be used only for files loaded via multipart. */
+ /* Null callback disables callback system. */
+ void (*callback)(
+ struct magi_field * field, char * buffer, int len, void * thing),
+ void * thing,
+ int max_post);
#endif
diff --git a/src/field.c b/src/field.c
deleted file mode 100644
index 6358151..0000000
--- a/src/field.c
+++ /dev/null
@@ -1,53 +0,0 @@
-#include "field.h"
-
-#include "error.h"
-#include "param.h"
-#include <stdlib.h>
-#include <string.h>
-
-
-/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * Form Field
- */
-int magi_field_list_add(struct magi_field_list ** list,
- struct magi_field * item)
-{
- struct magi_field_list * old = *list;
- int ok = 1;
- *list = malloc(sizeof(**list));
- if (*list) {
- (*list)->next = old;
- (*list)->item = *item;
- } else {
- ok = 0;
- magi_error_set("[field:list] Cannot allocate new list node.");
- *list = old;
- }
- return ok;
-}
-
-struct magi_field * magi_field_list_get(struct magi_field_list * list,
- const char * name)
-{
- struct magi_field * item = 0;
- if (list && name) {
- if (!strcmp(list->item.name, name)) {
- item = &list->item;
- } else {
- item = magi_field_list_get(list->next, name);
- }
- }
- return item;
-}
-
-void magi_field_list_destroy(struct magi_field_list * list)
-{
- if (list) {
- magi_field_list_destroy(list->next);
- magi_param_list_destroy(list->item.params);
- free(list->next);
- free(list->item.name);
- free(list->item.data);
- free(list->item.params);
- }
-}
diff --git a/src/field.h b/src/field.h
deleted file mode 100644
index 04c8411..0000000
--- a/src/field.h
+++ /dev/null
@@ -1,41 +0,0 @@
-#ifndef MAGI_INCLUDED_FIELD
-#define MAGI_INCLUDED_FIELD
-
-
-/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * Form Field
- */
-struct magi_field {
- 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. */
-};
-
-/* Null is valid "struct magi_field_list *" object. */
-struct magi_field_list {
- struct magi_field_list * next;
- struct magi_field item;
-};
-
-/*
- * Adds *item to the begining of *list, item and list are dereferencable;
- * Returns null in case of error.
- */
-int magi_field_list_add(struct magi_field_list ** list,
- struct magi_field * item);
-
-/*
- * Searchs for first node in list: node.name == name, name is C-string;
- * Returns node itself if succeed, otherwise result is null.
- */
-struct magi_field * magi_field_list_get(struct magi_field_list * list,
- const char * name);
-
-/*
- * Destroys list; list is not valid after destruction.
- */
-void magi_field_list_destroy(struct magi_field_list * list);
-
-
-#endif
diff --git a/src/file.c b/src/file.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/file.c
diff --git a/src/file.h b/src/file.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/file.h
diff --git a/src/multipart.h b/src/multipart.h
index 43b6212..bb51d5c 100644
--- a/src/multipart.h
+++ b/src/multipart.h
@@ -13,8 +13,10 @@ int magi_parse_multipart(
void * get_next_arg,
char * boundary,
/* End if size < magi_parse_multipart_callback_size. */
- /* Null callback means filling list. */
- void (*callback)(struct magi_field * field, char * buffer, int size));
+ /* Null callback means skipping. */
+ void (*callback)(
+ struct magi_file * file, char * buffer, int size, void * thing),
+ void * thing);
#endif
diff --git a/src/param.h b/src/param.h
index b06c59f..21e9538 100644
--- a/src/param.h
+++ b/src/param.h
@@ -5,6 +5,8 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Parameter
*/
+enum { magi_param_max = 1024 };
+
struct magi_param {
char * name; /* name: free(name) is valid. */
char * data; /* data: free(data) is valid. */
diff --git a/src/request.h b/src/request.h
index 89181d1..d74dced 100644
--- a/src/request.h
+++ b/src/request.h
@@ -30,8 +30,9 @@
* path_info: /foo/bar
*/
struct magi_request {
- /* TODO: struct magi_param_list * url_params; */
- struct magi_field_list * fields;
+ struct magi_param_list * url_params;
+ struct magi_param_list * params;
+ struct magi_file_list * files;
struct magi_cookie_list * cookies;
char * method;
char * uri;
@@ -48,7 +49,7 @@ struct magi_request {
char * server_software;
char * path_info;
struct magi_param_list * http_params;
- struct magi_error * error;
+ enum magi_error error;
};