-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform_reader.c
More file actions
executable file
·167 lines (140 loc) · 5.72 KB
/
form_reader.c
File metadata and controls
executable file
·167 lines (140 loc) · 5.72 KB
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
#include "form_reader.h"
char *trim_newline(char *str)
{
char *end = str + strlen(str) - 1;
while (end > str && (*end == '\r' || *end == '\n'))
{
*end = '\0';
end--;
}
return str;
}
static int upload_file(const char *filepath, const char *content, size_t content_length)
{
FILE *file = fopen(filepath, "wb");
if (!file)
{
return -1; // File open error
}
fwrite(content, sizeof(char), content_length, file);
fclose(file);
if (chmod(filepath, 0666) != 0)
{
return -1; // Gagal mengatur izin
}
return 0; // Success
}
keyValuePair *parse_multipart_form_data(request_rec *r, const char *path_temp, int clamav_status)
{
apr_off_t size;
const char *buffer;
keyValuePair *kvp;
if (util_read(r, &buffer, &size) == OK)
{
const char *boundary = strstr(r->content_type, "boundary=");
if (!boundary)
{
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "No boundary found in Content-Type");
return NULL;
}
boundary += 9; // Move past "boundary="
const char *boundary_str = apr_pstrcat(r->pool, "--", boundary, NULL);
kvp = apr_pcalloc(r->pool, sizeof(keyValuePair) * (size + 1));
if (kvp == NULL)
{
return NULL; // Handle memory allocation failure
}
const char *part_start = strstr(buffer, boundary_str);
int i = 0;
while ((part_start = strstr(part_start, boundary_str)))
{
part_start += strlen(boundary_str);
if (*part_start == '-' && *(part_start + 1) == '-')
{
break; // End of multipart data
}
part_start += 2; // Skip the boundary line break
const char *part_end = strstr(part_start, boundary_str);
if (!part_end)
{
part_end = buffer + size;
}
else
{
part_end -= 2; // Remove the trailing line break before boundary
}
const char *header_end = strstr(part_start, "\r\n\r\n");
if (header_end)
{
char *header_end_modifiable = (char *)header_end;
*header_end_modifiable = '\0';
const char *body_start = header_end + 4;
if (body_start >= part_end)
{
break;
}
const char *content_disposition = strstr(part_start, "Content-Disposition:");
const char *content_type_start = strstr(part_start, "Content-Type:");
if (content_disposition)
{
const char *name_start = strstr(content_disposition, "name=\"");
const char *namefile = strstr(content_disposition, "filename=\"");
if (name_start)
{
name_start += strlen("name=\"");
const char *name_end = strstr(name_start, "\"");
if (content_type_start != NULL && namefile != NULL)
{
namefile += strlen("filename=\"");
const char *file_end = strstr(namefile, "\"");
if (file_end)
{
char *file_end_modifiable = (char *)file_end;
*file_end_modifiable = '\0';
char *name_end_modifiable = (char *)name_end;
*name_end_modifiable = '\0';
char *key = apr_pstrdup(r->pool, name_start);
char *value = apr_pstrdup(r->pool, namefile);
value = trim_newline(value);
if (clamav_status == 1)
{
char file_path[512];
snprintf(file_path, sizeof(file_path), "%s/%s", path_temp, value);
if (upload_file(file_path, body_start, part_end - body_start) == 0)
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "File uploaded successfully: %s\n", file_path);
}
else
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Failed to upload successfully: %s\n", file_path);
}
}
kvp[i].key = key;
kvp[i].value = value;
kvp[i].type = strdup("file");
}
}
else
{
if (name_end)
{
char *name_end_modifiable = (char *)name_end;
*name_end_modifiable = '\0';
char *key = apr_pstrdup(r->pool, name_start);
char *value = apr_pstrndup(r->pool, body_start, part_end - body_start);
value = trim_newline(value);
kvp[i].key = key;
kvp[i].value = value;
kvp[i].type = strdup("text");
}
}
}
}
}
part_start = part_end;
i++;
}
kvp[i].key = NULL;
}
return kvp;
}