Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for K2 http queries #1124

Merged
merged 2 commits into from
Oct 23, 2024
Merged
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
31 changes: 28 additions & 3 deletions runtime-light/component/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
#include <chrono>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>

#include "runtime-core/runtime-core.h"
#include "runtime-core/utils/kphp-assert-core.h"
#include "runtime-light/component/init-functions.h"
#include "runtime-light/core/globals/php-init-scripts.h"
#include "runtime-light/core/globals/php-script-globals.h"
#include "runtime-light/coroutine/task.h"
#include "runtime-light/header.h"
#include "runtime-light/scheduler/scheduler.h"
#include "runtime-light/stdlib/job-worker/job-worker-context.h"
#include "runtime-light/server/job-worker/job-worker-server-context.h"
#include "runtime-light/streams/streams.h"
#include "runtime-light/utils/context.h"

Expand Down Expand Up @@ -48,8 +51,30 @@ void ComponentState::init_script_execution() noexcept {
template<ComponentKind kind>
task_t<void> ComponentState::run_component_prologue() noexcept {
static_assert(kind != ComponentKind::Invalid);

component_kind_ = kind;

// common initialization
auto &superglobals{php_script_mutable_globals_singleton.get_superglobals()};
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
superglobals.v$argc = static_cast<int64_t>(0); // TODO
superglobals.v$argv = array<mixed>{}; // TODO
{
const auto &platform_ctx{*get_platform_context()};

SystemTime sys_time{};
platform_ctx.get_system_time(std::addressof(sys_time));
const auto time_mcs{std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::nanoseconds{sys_time.since_epoch_ns}).count()};

using namespace PhpServerSuperGlobalIndices;
superglobals.v$_SERVER.set_value(string{ARGC, std::char_traits<char>::length(ARGC)}, superglobals.v$argc);
superglobals.v$_SERVER.set_value(string{ARGV, std::char_traits<char>::length(ARGV)}, superglobals.v$argv);
superglobals.v$_SERVER.set_value(string{PHP_SELF, std::char_traits<char>::length(PHP_SELF)}, string{}); // TODO: script name
superglobals.v$_SERVER.set_value(string{SCRIPT_NAME, std::char_traits<char>::length(SCRIPT_NAME)}, string{});
superglobals.v$_SERVER.set_value(string{REQUEST_TIME, std::char_traits<char>::length(REQUEST_TIME)}, static_cast<int64_t>(sys_time.since_epoch_ns));
superglobals.v$_SERVER.set_value(string{REQUEST_TIME_FLOAT, std::char_traits<char>::length(REQUEST_TIME_FLOAT)}, static_cast<double>(time_mcs));
}
// TODO sapi, env

// specific initialization
if constexpr (kind == ComponentKind::CLI) {
standard_stream_ = co_await init_kphp_cli_component();
} else if constexpr (kind == ComponentKind::Server) {
Expand All @@ -67,7 +92,7 @@ task_t<void> ComponentState::run_component_epilogue() noexcept {
co_return;
}
// do not flush output buffers if we are in job worker
if (JobWorkerServerComponentContext::get().kind != JobWorkerServerComponentContext::Kind::Invalid) {
if (job_worker_server_component_context.kind != JobWorkerServerComponentContext::Kind::Invalid) {
co_return;
}
if (standard_stream() == INVALID_PLATFORM_DESCRIPTOR) {
Expand Down
5 changes: 4 additions & 1 deletion runtime-light/component/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#include "runtime-light/coroutine/task.h"
#include "runtime-light/header.h"
#include "runtime-light/scheduler/scheduler.h"
#include "runtime-light/server/http/http-server-context.h"
#include "runtime-light/server/job-worker/job-worker-server-context.h"
#include "runtime-light/stdlib/curl/curl-context.h"
#include "runtime-light/stdlib/file/file-stream-context.h"
#include "runtime-light/stdlib/fork/fork-context.h"
#include "runtime-light/stdlib/job-worker/job-worker-context.h"
#include "runtime-light/stdlib/job-worker/job-worker-client-context.h"
#include "runtime-light/stdlib/output/output-buffer.h"
#include "runtime-light/stdlib/regex/regex-context.h"
#include "runtime-light/stdlib/rpc/rpc-context.h"
Expand Down Expand Up @@ -107,6 +109,7 @@ struct ComponentState {

KphpCoreContext kphp_core_context;
RpcComponentContext rpc_component_context;
HttpServerComponentContext http_server_component_context{};
JobWorkerClientComponentContext job_worker_client_component_context{};
JobWorkerServerComponentContext job_worker_server_component_context{};

Expand Down
27 changes: 11 additions & 16 deletions runtime-light/component/init-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,35 @@

#include <cstdint>

#include "runtime-core/runtime-core.h"
#include "runtime-core/utils/kphp-assert-core.h"
#include "runtime-light/component/component.h"
#include "runtime-light/coroutine/awaitable.h"
#include "runtime-light/coroutine/task.h"
#include "runtime-light/header.h"
#include "runtime-light/stdlib/job-worker/job-worker-context.h"
#include "runtime-light/server/init-functions.h"
#include "runtime-light/server/job-worker/job-worker-server-context.h"
#include "runtime-light/streams/streams.h"
#include "runtime-light/tl/tl-core.h"
#include "runtime-light/tl/tl-functions.h"
#include "runtime-light/utils/context.h"

namespace {

void process_k2_invoke_http(tl::TLBuffer &tlb) noexcept {
tl::K2InvokeHttp invoke_http{};
if (!invoke_http.fetch(tlb)) {
php_error("erroneous http request");
}
init_server(ServerQuery{std::move(invoke_http)});
}

void process_k2_invoke_job_worker(tl::TLBuffer &tlb) noexcept {
tl::K2InvokeJobWorker invoke_jw{};
if (!invoke_jw.fetch(tlb)) {
php_error("erroneous job worker request");
}
php_assert(invoke_jw.image_id == vk_k2_describe()->build_timestamp); // ensure that we got the request from ourselves

auto &jw_server_ctx{JobWorkerServerComponentContext::get()};
jw_server_ctx.kind = invoke_jw.ignore_answer ? JobWorkerServerComponentContext::Kind::NoReply : JobWorkerServerComponentContext::Kind::Regular;
jw_server_ctx.state = JobWorkerServerComponentContext::State::Working;
jw_server_ctx.job_id = invoke_jw.job_id;
jw_server_ctx.body = std::move(invoke_jw.body);
get_component_context()->php_script_mutable_globals_singleton.get_superglobals().v$_SERVER.set_value(string{"JOB_ID"}, invoke_jw.job_id);
}

void process_k2_invoke_http(tl::TLBuffer &tlb) noexcept {
tl::K2InvokeHttp invoke_http{};
if (!invoke_http.fetch(tlb)) {
php_error("erroneous http request");
}
init_server(ServerQuery{std::move(invoke_jw)});
}

} // namespace
Expand Down
33 changes: 33 additions & 0 deletions runtime-light/core/globals/php-script-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,40 @@
#include "runtime-core/memory-resource/unsynchronized_pool_resource.h"
#include "runtime-core/runtime-core.h"

namespace PhpServerSuperGlobalIndices {

inline constexpr auto *ARGC = "argc";
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
inline constexpr auto *ARGV = "argv";

inline constexpr auto *PHP_SELF = "PHP_SELF";
inline constexpr auto *SCRIPT_URL = "SCRIPT_URL";
inline constexpr auto *SCRIPT_URI = "SCRIPT_URI";
inline constexpr auto *SCRIPT_NAME = "SCRIPT_NAME";

inline constexpr auto *REQUEST_URI = "REQUEST_URI";
inline constexpr auto *REQUEST_TIME = "REQUEST_TIME";
inline constexpr auto *REQUEST_METHOD = "REQUEST_METHOD";
inline constexpr auto *REQUEST_TIME_FLOAT = "REQUEST_TIME_FLOAT";

inline constexpr auto *JOB_ID = "JOB_ID";

inline constexpr auto *SERVER_NAME = "SERVER_NAME";
inline constexpr auto *SERVER_ADDR = "SERVER_ADDR";
inline constexpr auto *SERVER_PORT = "SERVER_PORT";
inline constexpr auto *SERVER_PROTOCOL = "SERVER_PROTOCOL";
inline constexpr auto *SERVER_SOFTWARE = "SERVER_SOFTWARE";
inline constexpr auto *SERVER_SIGNATURE = "SERVER_SIGNATURE";

inline constexpr auto *REMOTE_ADDR = "REMOTE_ADDR";
inline constexpr auto *REMOTE_PORT = "REMOTE_PORT";

inline constexpr auto *QUERY_STRING = "QUERY_STRING";
inline constexpr auto *GATEWAY_INTERFACE = "GATEWAY_INTERFACE";

}; // namespace PhpServerSuperGlobalIndices

struct PhpScriptBuiltInSuperGlobals {

astrophysik marked this conversation as resolved.
Show resolved Hide resolved
// variables below are PHP language superglobals
mixed v$_SERVER;
mixed v$_GET;
Expand Down
2 changes: 2 additions & 0 deletions runtime-light/runtime-light.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include(${RUNTIME_LIGHT_DIR}/allocator/allocator.cmake)
include(${RUNTIME_LIGHT_DIR}/core/core.cmake)
include(${RUNTIME_LIGHT_DIR}/scheduler/scheduler.cmake)
include(${RUNTIME_LIGHT_DIR}/server/server.cmake)
include(${RUNTIME_LIGHT_DIR}/stdlib/stdlib.cmake)
include(${RUNTIME_LIGHT_DIR}/streams/streams.cmake)
include(${RUNTIME_LIGHT_DIR}/tl/tl.cmake)
Expand All @@ -12,6 +13,7 @@ set(RUNTIME_LIGHT_SRC
${RUNTIME_CORE_SRC}
${RUNTIME_STDLIB_SRC}
${RUNTIME_SCHEDULER_SRC}
${RUNTIME_SERVER_SRC}
${RUNTIME_ALLOCATOR_SRC}
${RUNTIME_COROUTINE_SRC}
${RUNTIME_COMPONENT_SRC}
Expand Down
12 changes: 12 additions & 0 deletions runtime-light/server/http/http-server-context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2024 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include "runtime-light/server/http/http-server-context.h"

#include "runtime-light/component/component.h"
#include "runtime-light/utils/context.h"

HttpServerComponentContext &HttpServerComponentContext::get() noexcept {
return get_component_context()->http_server_component_context;
}
18 changes: 18 additions & 0 deletions runtime-light/server/http/http-server-context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2024 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <cstdint>

#include "common/mixin/not_copyable.h"

struct HttpServerComponentContext final : private vk::not_copyable {
static constexpr auto ENCODING_GZIP = static_cast<uint32_t>(1U << 0U);
static constexpr auto ENCODING_DEFLATE = static_cast<uint32_t>(1U << 1U);

uint32_t encoding{};

static HttpServerComponentContext &get() noexcept;
};
Loading
Loading