forked from zorxx/microhttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.c
More file actions
184 lines (157 loc) · 6.44 KB
/
post.c
File metadata and controls
184 lines (157 loc) · 6.44 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*! \copyright 2018 Zorxx Software. All rights reserved.
* \license This file is released under the MIT License. See the LICENSE file for details.
* \file post.c
* \brief microhttpd POST Implementation
*/
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "helpers.h"
#include "post.h"
static bool state_HandlePostHeader(struct md_client *client, uint32_t *consumed, bool *error);
static bool state_HandlePostHeaderComplete(struct md_client *client, uint32_t *consumed, bool *error);
static bool state_HandlePostData(struct md_client *client, uint32_t *consumed, bool *error);
/* ------------------------------------------------------------------------------------------
* Exported Functions
*/
bool state_HandleOperationPost(struct md_client *client, uint32_t *consumed, bool *error)
{
uint32_t idx, content_length = 0;
bool found;
for(idx = 0, found = false; idx < client->header_entry_count && !found; ++idx)
{
char *option = client->header_entries[idx];
if(memcmp(option, "content-length: ", 16) == 0)
{
content_length = strtoul(&option[16], NULL, 10);
found = true;
}
}
client->content_length = content_length;
client->content_remaining = content_length;
client->state = state_HandlePostHeader;
return true;
}
/* ------------------------------------------------------------------------------------------
* Private Functions
*/
static bool state_HandlePostHeader(struct md_client *client, uint32_t *consumed, bool *error)
{
uint32_t length;
char *offset;
offset = string_find(client->rx_buffer, client->rx_size, "\r\n", 2);
if(offset == NULL)
return false; /* Header entry delimiter not found; need more rx data */
length = offset - client->rx_buffer;
if(0 == length)
{
DBG("%s: Header parsing complete (%u entries)\n", __func__, client->header_entry_count);
client->state = state_HandlePostHeaderComplete; /* Empty header entry found; header complete */
client->content_remaining -= 2;
*consumed = 2;
return true;
}
DBG("%s: Found header option (length %u)\n", __func__, length);
if(!string_list_add(client->rx_buffer, length, &client->post_header_entries,
&client->post_header_entry_count))
{
DBG("%s: Failed to add entry to post header list\n", __func__);
*error = true;
return false;
}
DBG("%s: Header option %u: '%s'\n", __func__, client->post_header_entry_count,
client->post_header_entries[client->post_header_entry_count - 1]);
client->content_remaining -= length + 2;
*consumed = length + 2;
return true;
}
static bool state_HandlePostHeaderComplete(struct md_client *client, uint32_t *consumed, bool *error)
{
struct md_context *ctx = client->ctx;
uint32_t idx;
bool found;
client->post_boundary = NULL;
for(idx = 0, found = false; idx < client->header_entry_count && !found; ++idx)
{
client->post_boundary = strstr(client->header_entries[idx], "boundary=");
if(NULL != client->post_boundary)
{
client->post_boundary += 9;
DBG("%s: boundary is '%s'\n", __func__, client->post_boundary);
found = true;
}
}
client->filename = NULL;
for(idx = 0, found = false; idx < client->post_header_entry_count && !found; ++idx)
{
client->filename = strstr(client->post_header_entries[idx], "filename=\"");
if(client->filename != NULL)
{
char *end;
client->filename += 10;
end = strchr(client->filename, '\"');
if(end != NULL)
*end = '\0';
DBG("%s: POST filename is %s\n", __func__, client->filename);
found = true;
}
}
client->post_header_length = client->content_length - client->content_remaining;
client->post_trailer_length = strlen(client->post_boundary);
if(client->content_length < (client->post_header_length + client->post_trailer_length))
{
DBG("%s: Invalid post data length (total %u, header %u, footer %u\n", __func__,
client->content_length, client->post_header_length, client->post_trailer_length);
}
else
{
client->content_length -= (client->post_header_length + client->post_trailer_length);
DBG("%s: POST data lengths (total %u, header %u, footer %u\n", __func__,
client->content_length, client->post_header_length, client->post_trailer_length);
}
if(ctx->params.post_handler != NULL) /* POST start handler */
{
ctx->params.post_handler((tMicroHttpdClient) client, client->uri, client->filename,
(const char **) client->uri_params, client->uri_param_count, client->source_address,
ctx->params.post_handler_cookie, true, false, NULL, 0, client->content_length);
}
client->state = state_HandlePostData;
return true;
}
static bool state_HandlePostData(struct md_client *client, uint32_t *consumed, bool *error)
{
struct md_context *ctx = client->ctx;
uint32_t handled_length, data_length;
handled_length = client->content_remaining;
if(handled_length > client->rx_size)
handled_length = client->rx_size;
client->content_remaining -= handled_length;
DBG("%s: POST total length %u, current length %u, remaining length %u\n",
__func__, client->content_length, handled_length, client->content_remaining);
data_length = handled_length;
if(client->content_remaining < client->post_trailer_length)
data_length -= client->post_trailer_length - client->content_remaining;
if(data_length > 0 && ctx->params.post_handler != NULL)
{
DBG("%s: Sending %u bytes of data to application\n", __func__, data_length);
ctx->params.post_handler((tMicroHttpdClient) client, client->uri, client->filename,
(const char **) client->uri_params, client->uri_param_count,
client->source_address, ctx->params.post_handler_cookie,
false, false, client->rx_buffer, data_length, client->content_length);
}
*consumed = handled_length;
if(0 == client->content_remaining)
{
DBG("%s: POST finished\n", __func__);
/* Post complete handler */
if(ctx->params.post_handler != NULL)
{
ctx->params.post_handler((tMicroHttpdClient) client, client->uri, client->filename,
(const char **) client->uri_params, client->uri_param_count, client->source_address,
ctx->params.post_handler_cookie, false, true, NULL, 0, client->content_length);
}
microhttpd_ResetState(client);
return true;
}
return false; /* need more rx data */
}