Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit c31544e

Browse files
Fix population of $_POST from content-type=multipart/form-data in CGI mode
1 parent f47e310 commit c31544e

1 file changed

Lines changed: 56 additions & 28 deletions

File tree

engine/src/srvcgi.cpp

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,26 +1153,41 @@ typedef struct
11531153

11541154
MCStringRef boundary;
11551155

1156-
MCStringRef post_variable;
1157-
MCDataRef post_binary_variable;
1156+
// Storage for the data as it is being read
1157+
MCDataRef data;
1158+
1159+
// Arrays used to implement the $_POST and $_POST_BINARY variables
1160+
MCArrayRef post_variable;
1161+
MCArrayRef post_binary_variable;
11581162

11591163
} cgi_multipart_context_t;
11601164

11611165
static void cgi_dispose_multipart_context(cgi_multipart_context_t *p_context)
11621166
{
11631167
MCValueRelease(p_context->name);
1168+
p_context->name = nil;
11641169
MCValueRelease(p_context->file_name);
1170+
p_context->file_name = nil;
11651171
MCValueRelease(p_context->type);
1172+
p_context->type = nil;
11661173
MCValueRelease(p_context->boundary);
1174+
p_context->boundary = nil;
11671175
MCValueRelease(p_context->temp_name);
1176+
p_context->temp_name = nil;
11681177

11691178
if (p_context->file_handle != NULL)
11701179
MCS_close(p_context->file_handle);
1180+
p_context->file_handle = nil;
11711181

1172-
MCValueRelease(p_context->post_binary_variable);
1173-
MCValueRelease(p_context->post_variable);
1182+
MCValueRelease(p_context->data);
1183+
p_context->data = nil;
1184+
1185+
p_context->disposition = kMCDispositionUnknown;
1186+
p_context->file_size = 0;
1187+
p_context->file_status = kMCFileStatusOK;
11741188

1175-
MCMemoryClear(p_context, sizeof(cgi_multipart_context_t));
1189+
// Note that the post_variable and post_binary_variable members are
1190+
// maintained - these don't change between parts and shouldn't be reset.
11761191
}
11771192

11781193
static bool cgi_context_is_form_data(cgi_multipart_context_t *p_context)
@@ -1248,25 +1263,10 @@ static bool cgi_multipart_header_callback(void *p_context, MCMultiPartHeader *p_
12481263
t_success = t_context->name != NULL;
12491264
if (t_success)
12501265
{
1251-
// We need to reset the binary data fetched from the global variable
1252-
// and create a mutable DataRef
1253-
// SN-2015-02-06: [[ Bug 14477 ]] We want to copy the valueRef in
1254-
// t_context->post_variable, as it will be released in the end.
1255-
MCValueRef t_value;
1256-
cgi_fetch_valueref_for_key(s_cgi_post, t_context->name, t_value);
1257-
1258-
if (MCValueGetTypeCode(t_value) == kMCValueTypeCodeString)
1259-
t_context->post_variable = (MCStringRef)MCValueRetain(t_value);
1260-
else
1261-
t_success = false;
1262-
}
1263-
1264-
if (t_success)
1265-
{
1266-
// SN-2015-02-06: [[ Bug 14477 ]] We want to replace the data with a new,
1267-
// empty one
1268-
t_success = MCDataCreateMutable(0, t_context->post_binary_variable)
1269-
&& cgi_store_control_value(s_cgi_post_binary, t_context -> name, t_context -> post_binary_variable);
1266+
// Allocate storage for the data
1267+
MCAutoDataRef t_data;
1268+
t_success = MCDataCreateMutable(0, &t_data);
1269+
MCValueAssign(t_context->data, *t_data);
12701270
}
12711271
}
12721272
else if (cgi_context_is_file(t_context))
@@ -1302,13 +1302,20 @@ static bool cgi_multipart_body_callback(void *p_context, const char *p_data, uin
13021302
{
13031303
if (t_context->post_binary_variable != NULL)
13041304
{
1305-
t_success = MCDataAppendBytes(t_context->post_binary_variable, (const byte_t*)p_data, p_data_length);
1305+
t_success = MCDataAppendBytes(t_context->data, (const byte_t*)p_data, p_data_length);
13061306

13071307
if (t_success && p_finished)
13081308
{
1309-
MCAutoStringRef t_native_string;
1310-
if (cgi_native_from_encoding(MCserveroutputtextencoding, t_context->post_binary_variable, &t_native_string))
1311-
MCValueAssign(t_context->post_variable, *t_native_string);
1309+
// Store the binary data into its output variable
1310+
t_success = MCArrayStoreValue(t_context->post_binary_variable, false, t_context->name, t_context->data);
1311+
1312+
// Convert the binary data to a string
1313+
MCAutoStringRef t_native_string;
1314+
t_success = cgi_native_from_encoding(MCserveroutputtextencoding, t_context->data, &t_native_string);
1315+
1316+
// Store the string into its output variable
1317+
if (t_success)
1318+
t_success = MCArrayStoreValue(t_context->post_variable, false, t_context->name, *t_native_string);
13121319
}
13131320
}
13141321
}
@@ -1383,6 +1390,20 @@ static bool cgi_store_form_multipart(IO_handle p_stream)
13831390
// only reassigned, never directly set).
13841391
t_context . temp_name = MCValueRetain(kMCEmptyString);
13851392

1393+
// Create the arrays for storing the $_POST and $_POST_BINARY variables
1394+
MCAutoArrayRef t_post_array, t_post_binary_array;
1395+
if (t_success)
1396+
{
1397+
t_success = MCArrayCreateMutable(&t_post_array) &&
1398+
MCArrayCreateMutable(&t_post_binary_array);
1399+
1400+
if (t_success)
1401+
{
1402+
t_context.post_variable = *t_post_array;
1403+
t_context.post_binary_variable = *t_post_binary_array;
1404+
}
1405+
}
1406+
13861407
uint32_t t_bytes_read = 0;
13871408

13881409
if (t_success)
@@ -1395,6 +1416,13 @@ static bool cgi_store_form_multipart(IO_handle p_stream)
13951416
cgi_multipart_header_callback, cgi_multipart_body_callback, &t_context);
13961417
}
13971418

1419+
// Assign the values for the $_POST and $_POST_BINARY variables
1420+
if (t_success)
1421+
{
1422+
s_cgi_post->setvalueref(*t_post_array);
1423+
s_cgi_post_binary->setvalueref(*t_post_binary_array);
1424+
}
1425+
13981426
// clean up in case of errors;
13991427
if (!t_success)
14001428
cgi_dispose_multipart_context(&t_context);

0 commit comments

Comments
 (0)