aboutsummaryrefslogtreecommitdiff
path: root/examples/upload.c
blob: 2ef8bc50112f39bb2e641b0d66825afd97f317e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cgi.h>
#include <multipart.h>
#include <request.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void tempfile_callback(struct magi_file * file,
                       char *             buffer,
                       int                len,
                       void *             _)
{
    if (!strcmp(file->param_name, "data")) {
        static FILE * f = 0;
        if (!f) {
            remove(file->param_name);
            f = fopen(file->param_name, "wb");
        }
        if (len) {
            fwrite(buffer, 1, len, f);
        }
        if (len < magi_file_callback_portion_max) {
            fclose(f);
            f = 0;
        }
    }
}

void response_request(struct magi_request * req, struct magi_response * res)
{
    magi_response_content_type(res, magi_xhtml);
    magi_response_content(
        res, "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' "
             "'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>"
             "<html xmlns='http://www.w3.org/1999/xhtml'>"
             "<head><title>Upload File</title></head>"
             "<body>"
             "<form action='/cgi-bin/upload' method='post' "
             "enctype='multipart/form-data'><fieldset>"
             "<input type='text' name='name' value='filename'/>"
             "<input type='file' name='data'/>"
             "<input type='submit' value='Upload'/>"
             "</fieldset></form>"
             "</body>"
             "</html>");

    struct magi_field * name = magi_field_list_get(req->fields, "name");
    struct magi_field * data = magi_field_list_get(req->fields, "data");
    if (name && name->data && data) {
        rename("data", name->data);
    }
}

int main(int argc, char const * argv[])
{
    struct magi_request request;
    if (magi_cgi(&request, tempfile_callback, 0, 0)) {
        struct magi_response response;
        response_request(&request, &response);
        magi_cgi_response(&response);
        magi_response_destroy();
    } else {
        magi_cgi_error(request.error);
    }
    magi_request_destroy(&request);
    return 0;
}