File size: 6,404 Bytes
c7436b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef ATTACHMENT_WRITER
#define ATTACHMENT_WRITER

#include <string.h>
#include <stdlib.h>
#include "indexed_list.h"
#include "pattern.h"
#include "filemanager.h"

extern "C" {
    #include "multipart.h"
}

class TempfileWriter;

typedef void (*WriterCallback_t)(TempfileWriter *, const void *context1, void *context2);

class TempfileWriter
{
    IndexedList<const char *> filenames;
    IndexedList<size_t> filesizes;
    IndexedList<uint8_t *>filebuffers;
    File *fo;
    size_t file_size;    
    HTTPReqMessage *req;
    HTTPRespMessage *resp;
    WriterCallback_t callback;
    const void *context1;
    void *context2;
public:
    TempfileWriter(HTTPReqMessage *req, HTTPRespMessage *resp, WriterCallback_t cb, const void *c1, void *c2)
        : filenames(4, NULL), filesizes(4, 0), filebuffers(4, NULL) {
        fo = NULL;
        this->req = req;
        this->resp = resp;
        callback = cb;
        context1 = c1;
        context2 = c2;
    }

    ~TempfileWriter()
    {
        // filenames were created with strdup, so we need to free them here
        for (int i=0; i<filenames.get_elements(); i++) {
            free((char *)filenames[i]);
        }
        for (int i=0; i<filebuffers.get_elements(); i++) {
            free(filebuffers[i]);
        }
    }

    HTTPReqMessage *get_request()
    {
        return req;
    }

    HTTPRespMessage *get_response()
    {
        return resp;
    }

    static void collect_wrapper(BodyDataBlock_t *block)
    {
        TempfileWriter *writer = (TempfileWriter *)block->context;
        writer->collect(block);
    }

    void collect(BodyDataBlock_t *block)
    {
        char filename[128];
        char tempstring[128];
        uint32_t dummy;

        HTTPHeaderField *f;

        switch(block->type) {
            case eStart:
                fo = NULL;
                break;
            case eDataStart:
            {
                mstring canonical_path;
                FRESULT fres = FileManager::getFileManager()->create_temp_file("upload", NULL,
                        FA_WRITE | FA_CREATE_ALWAYS, &fo, &canonical_path);
                if (fres == FR_OK) {
                    filenames.append(strdup(canonical_path.c_str()));
                } else {
                    filenames.append(strdup(""));
                    fo = NULL;
                }
            }
                file_size = 0;
                break;
            case eSubHeader:
                filename[0] = 0;
                f = (HTTPHeaderField *)block->data;
                for(int i=0; i < block->length; i++) {
                    if (strcasecmp(f[i].key, "Content-Disposition") == 0) {
                        // extract filename from value string, e.g. 'form-data; name="bestand"; filename="sample.html"'
                        strncpy(tempstring, f[i].value, 127); tempstring[127] = 0;
                        char *sub = strstr(tempstring, "filename=\"");
                        if (sub) {
                            sub += 10; // remove the 'filename="' part
                            char *quote = strstr(sub, "\"");
                            if (quote) {
                                *quote = 0;
                            }
                            while((*sub == '.') || (*sub == '\\') || (*sub == '/')) {
                                sub++;
                            }
                            fix_filename(sub);
                            strncpy(filename, sub, sizeof(filename) - 1);
                            filename[127] = 0;
                        }
                    }
                }
            {
                mstring canonical_path;
                FRESULT fres = FileManager::getFileManager()->create_temp_file("upload",
                        filename[0] ? filename : NULL, FA_WRITE | FA_CREATE_ALWAYS, &fo, &canonical_path);
                if (fres == FR_OK) {
                    filenames.append(strdup(canonical_path.c_str()));
                } else {
                    filenames.append(strdup(""));
                    fo = NULL;
                }
            }
                file_size = 0;
                break;
            case eDataBlock:
                if (fo) {
                    fo->write(block->data, block->length, &dummy);
                    file_size += block->length;
                }
                break;
            case eDataEnd:
                filesizes.append(fo ? file_size : 0);
                if (fo) {
                    FileManager::getFileManager()->fclose(fo);
                    fo = NULL;
                }
                break;
            case eTerminate:
                if (fo) {
                    FileManager::getFileManager()->fclose(fo);
                    fo = NULL;
                }
                printf("Uploaded files:\n");
                for (int i=0;i<filenames.get_elements();i++) {
                    printf("  '%s' (%d)\n", filenames[i], (int)filesizes[i]);
                }
                // The actual function should now be called to do something with these files
                // but since we don't know the context in which this writer is used,
                // we simply call the callback with the context.
                if (callback) {
                    callback(this, context1, context2);
                }
                break;
        }
    }

    const char *get_filename(int index)
    {
        const char *filename = filenames[index];
        return filename ? filename : "";
    }
    size_t get_filesize(int index)
    {
        return filesizes[index];
    }
    uint8_t *get_buffer(int index)
    {
        return filebuffers[index];
    }

    int buffer_file(int index, size_t max_size)
    {
        size_t size = filesizes[index];
        const char *filename = get_filename(index);
        if ((size == 0) || !filename[0]) {
            return -1; // no data
        }
        if (size > max_size) {
            return -2; // too large
        }

        uint8_t *buffer = (uint8_t *)malloc(size);
        if (buffer) {
            FILE *f = fopen(filename, "rb");
            if (f) {
                fread(buffer, size, 1, f);
                fclose(f);
                filebuffers.append(buffer);
                return 0; // success
            }
            free(buffer);
            return -4; // can't open file
        }
        return -3; // no mem
    }
};

#endif