Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime-light/server/http/http-server-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ inline constexpr std::string_view CONTENT_LENGTH = "content-length";
inline constexpr std::string_view AUTHORIZATION = "authorization";
inline constexpr std::string_view ACCEPT_ENCODING = "accept-encoding";
inline constexpr std::string_view CONTENT_ENCODING = "content-encoding";
inline constexpr std::string_view CONTENT_DISPOSITION = "content-disposition";

} // namespace headers

Expand Down
23 changes: 20 additions & 3 deletions runtime-light/server/http/init-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
#include "runtime-light/core/globals/php-script-globals.h"
#include "runtime-light/k2-platform/k2-api.h"
#include "runtime-light/server/http/http-server-state.h"
#include "runtime-light/server/http/multipart.h"
#include "runtime-light/state/instance-state.h"
#include "runtime-light/stdlib/component/component-api.h"
#include "runtime-light/stdlib/diagnostics/logs.h"
#include "runtime-light/stdlib/output/output-state.h"
#include "runtime-light/stdlib/server/http-functions.h"
#include "runtime-light/stdlib/zlib/zlib-functions.h"
#include "runtime-light/stdlib/file/file-system-functions.h"
#include "runtime-light/streams/stream.h"
#include "runtime-light/tl/tl-core.h"
#include "runtime-light/tl/tl-functions.h"
Expand Down Expand Up @@ -319,14 +321,16 @@ void init_server(kphp::component::stream&& request_stream, kphp::stl::vector<std
break;
}
case kphp::http::method::post: {
string body{reinterpret_cast<const char*>(invoke_http.body.data()), static_cast<string::size_type>(invoke_http.body.size())};
if (!std::ranges::search(content_type, CONTENT_TYPE_APP_FORM_URLENCODED).empty()) {
string body{reinterpret_cast<const char*>(invoke_http.body.data()), static_cast<string::size_type>(invoke_http.body.size())};
f$parse_str(body, superglobals.v$_POST);
http_server_instance_st.opt_raw_post_data.emplace(std::move(body));
} else if (!std::ranges::search(content_type, CONTENT_TYPE_MULTIPART_FORM_DATA).empty()) {
kphp::log::error("unsupported content-type: {}", CONTENT_TYPE_MULTIPART_FORM_DATA);
std::string_view boundary{parse_boundary(content_type)};
if (!boundary.empty()) {
kphp::http::parse_multipart({body.c_str(), body.size()}, boundary, superglobals.v$_POST, superglobals.v$_FILES);
}
} else {
string body{reinterpret_cast<const char*>(invoke_http.body.data()), static_cast<string::size_type>(invoke_http.body.size())};
http_server_instance_st.opt_raw_post_data.emplace(std::move(body));
}

Expand Down Expand Up @@ -378,6 +382,7 @@ void init_server(kphp::component::stream&& request_stream, kphp::stl::vector<std

kphp::coro::task<> finalize_server() noexcept {
auto& http_server_instance_st{HttpServerInstanceState::get()};
auto& superglobals{InstanceState::get().php_script_mutable_globals_singleton.get_superglobals()};

string response_body{};
tl::HttpResponse http_response{};
Expand Down Expand Up @@ -431,6 +436,18 @@ kphp::coro::task<> finalize_server() noexcept {
[[fallthrough]];
}
case kphp::http::response_state::completed:
const array<mixed> files = superglobals.v$_FILES.to_array();
for (array<mixed>::const_iterator it = files.begin(); it != files.end(); ++it) {
const mixed& file = it.get_value();

if (!file.is_array()) {
kphp::log::error("$_FILES contains a value that is not an array");
continue;
}

const mixed tmp_filename = file.get_value(string("tmp_name"));
f$unlink(tmp_filename.to_string());
}
co_return;
}
}
Expand Down
Loading