Slon/Http/Server: Add @slon_http_json_object_from_multipart_form_data()
This commit is contained in:
parent
140284233d
commit
bcb46d47f1
1 changed files with 32 additions and 15 deletions
|
@ -392,10 +392,9 @@ JsonObject* @slon_http_json_object_from_form_urlencoded_string(SlonHttpSession*
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
U8* @slon_http_json_string_from_multipart_form_data(SlonHttpSession* session, U8* multipart_form_data)
|
JsonObject* @slon_http_json_object_from_multipart_form_data(SlonHttpSession* session, U8* multipart_form_data)
|
||||||
{
|
{
|
||||||
U8* json_string = @slon_calloc(session, StrLen(multipart_form_data) * 2);
|
JsonObject* obj = Json.CreateObject();
|
||||||
String.Append(json_string, "{");
|
|
||||||
U8* multipart_form_data_copy = @slon_strnew(session, multipart_form_data);
|
U8* multipart_form_data_copy = @slon_strnew(session, multipart_form_data);
|
||||||
|
|
||||||
U8* boundary = StrFind("boundary=", session->header("content-type")) + 9;
|
U8* boundary = StrFind("boundary=", session->header("content-type")) + 9;
|
||||||
|
@ -412,7 +411,8 @@ U8* @slon_http_json_string_from_multipart_form_data(SlonHttpSession* session, U8
|
||||||
|
|
||||||
U8* line;
|
U8* line;
|
||||||
U8* name;
|
U8* name;
|
||||||
U8* replace_line;
|
U8* value = @slon_calloc(session, 262144);
|
||||||
|
U8* sub_key = NULL;
|
||||||
|
|
||||||
I64 i = 0;
|
I64 i = 0;
|
||||||
while (i < lines_count) {
|
while (i < lines_count) {
|
||||||
|
@ -435,23 +435,41 @@ U8* @slon_http_json_string_from_multipart_form_data(SlonHttpSession* session, U8
|
||||||
name++;
|
name++;
|
||||||
while (name[StrLen(name) - 1] == '\"')
|
while (name[StrLen(name) - 1] == '\"')
|
||||||
name[StrLen(name) - 1] = NULL;
|
name[StrLen(name) - 1] = NULL;
|
||||||
String.Append(json_string, "\"%s\":\"", name);
|
StrCpy(value, "");
|
||||||
state = SLON_MULTIPART_PARSER_CONSUME_CONTENT;
|
state = SLON_MULTIPART_PARSER_CONSUME_CONTENT;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SLON_MULTIPART_PARSER_CONSUME_CONTENT:
|
case SLON_MULTIPART_PARSER_CONSUME_CONTENT:
|
||||||
if (StrFind(boundary, line)) {
|
if (StrFind(boundary, line)) {
|
||||||
String.Append(json_string, "\"");
|
if (String.EndsWith("[]", name)) {
|
||||||
|
// We have an array
|
||||||
|
StrFind("[]", name)[0] = NULL;
|
||||||
|
if (!obj->@(name)) {
|
||||||
|
obj->set(name, Json.CreateArray(), JSON_ARRAY);
|
||||||
|
}
|
||||||
|
obj->a(name)->append(Json.CreateItem(value, JSON_STRING));
|
||||||
|
} else if (StrFind("[", name) > 0) {
|
||||||
|
// We have an object
|
||||||
|
sub_key = StrFind("[", name) + 1;
|
||||||
|
while (sub_key[StrLen(sub_key) - 1] == ']') {
|
||||||
|
sub_key[StrLen(sub_key) - 1] = NULL;
|
||||||
|
}
|
||||||
|
StrFind("[", name)[0] = NULL;
|
||||||
|
if (!obj->@(name)) {
|
||||||
|
obj->set(name, Json.CreateObject(), JSON_OBJECT);
|
||||||
|
}
|
||||||
|
obj->o(name)->set(sub_key, value, JSON_STRING);
|
||||||
|
} else {
|
||||||
|
// We have a boring old parameter
|
||||||
|
obj->set(name, value, JSON_STRING);
|
||||||
|
}
|
||||||
if (!String.EndsWith("--", line)) {
|
if (!String.EndsWith("--", line)) {
|
||||||
String.Append(json_string, ",");
|
|
||||||
state = SLON_MULTIPART_PARSER_CONSUME_CONTENT_DISPOSITION;
|
state = SLON_MULTIPART_PARSER_CONSUME_CONTENT_DISPOSITION;
|
||||||
} else {
|
} else {
|
||||||
state = SLON_MULTIPART_PARSER_DONE;
|
state = SLON_MULTIPART_PARSER_DONE;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
replace_line = String.Replace(line, "\"", "\\\"");
|
String.Append(value, line);
|
||||||
String.Append(json_string, replace_line);
|
|
||||||
Free(replace_line);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -459,9 +477,10 @@ U8* @slon_http_json_string_from_multipart_form_data(SlonHttpSession* session, U8
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
String.Append(json_string, "}");
|
|
||||||
|
@slon_free(session, value);
|
||||||
@slon_free(session, multipart_form_data_copy);
|
@slon_free(session, multipart_form_data_copy);
|
||||||
return json_string;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
U0 @slon_http_parse_query_string(SlonHttpSession* session)
|
U0 @slon_http_parse_query_string(SlonHttpSession* session)
|
||||||
|
@ -487,9 +506,7 @@ U0 @slon_http_parse_request_as_multipart_form_data(SlonHttpSession* session)
|
||||||
session->request->json = Json.Parse("{}");
|
session->request->json = Json.Parse("{}");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
U8* json_string = @slon_http_json_string_from_multipart_form_data(session, session->request->data);
|
session->request->json = @slon_http_json_object_from_multipart_form_data(session, session->request->data);
|
||||||
session->request->json = Json.Parse(json_string);
|
|
||||||
@slon_free(session, json_string);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
U0 @slon_http_parse_request_as_json(SlonHttpSession* session)
|
U0 @slon_http_parse_request_as_json(SlonHttpSession* session)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue