Skip to content

Commit 6d081c1

Browse files
[k2] fix headers_list (#1574)
Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru> Co-authored-by: Alexander Polyakov <al.polyakov@vk.team>
1 parent 7004f18 commit 6d081c1

4 files changed

Lines changed: 26 additions & 14 deletions

File tree

runtime-light/server/http/http-server-state.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace headers {
3838

3939
inline constexpr std::string_view HOST = "host";
4040
inline constexpr std::string_view COOKIE = "cookie";
41+
inline constexpr std::string_view SERVER = "server";
4142
inline constexpr std::string_view LOCATION = "location";
4243
inline constexpr std::string_view SET_COOKIE = "set-cookie";
4344
inline constexpr std::string_view CONNECTION = "connection";

runtime-light/server/http/init-functions.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ constexpr std::string_view CONNECTION_CLOSE = "close";
5858
constexpr std::string_view CONNECTION_KEEP_ALIVE = "keep-alive";
5959
constexpr std::string_view ENCODING_GZIP = "gzip";
6060
constexpr std::string_view ENCODING_DEFLATE = "deflate";
61-
constexpr std::string_view CONTENT_TYPE_TEXT_WIN1251 = "text/html; charset=windows-1251";
6261
constexpr std::string_view CONTENT_TYPE_APP_FORM_URLENCODED = "application/x-www-form-urlencoded";
6362
constexpr std::string_view CONTENT_TYPE_MULTIPART_FORM_DATA = "multipart/form-data";
6463

@@ -350,15 +349,12 @@ void init_server(kphp::component::stream&& request_stream, kphp::stl::vector<std
350349

351350
// ==================================
352351
// prepare some response headers
353-
354-
// add content-type header
355352
auto& static_SB{RuntimeContext::get().static_SB};
356-
static_SB.clean() << headers::CONTENT_TYPE.data() << ": " << CONTENT_TYPE_TEXT_WIN1251.data();
357-
kphp::http::header({static_SB.c_str(), static_SB.size()}, true, status::NO_STATUS);
358353
// add connection kind header
359354
const auto connection_kind{http_server_instance_st.connection_kind == connection_kind::keep_alive ? CONNECTION_KEEP_ALIVE : CONNECTION_CLOSE};
360355
static_SB.clean() << headers::CONNECTION.data() << ": " << connection_kind.data();
361356
kphp::http::header({static_SB.c_str(), static_SB.size()}, true, status::NO_STATUS);
357+
362358
kphp::log::info("http server initialized with: "
363359
"server addr -> {}, "
364360
"server port -> {}, "

runtime-light/state/instance-state.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "runtime-light/coroutine/task.h"
2222
#include "runtime-light/k2-platform/k2-api.h"
2323
#include "runtime-light/server/cli/init-functions.h"
24+
#include "runtime-light/server/http/http-server-state.h"
2425
#include "runtime-light/server/http/init-functions.h"
2526
#include "runtime-light/server/rpc/init-functions.h"
2627
#include "runtime-light/state/component-state.h"
@@ -150,6 +151,15 @@ kphp::coro::task<> InstanceState::run_instance_prologue() noexcept {
150151
superglobals.v$d$PHP_SAPI = string{sapi_name.data(), sapi_name.size()};
151152
}
152153

154+
if constexpr (kind == image_kind::cli || kind == image_kind::server) {
155+
// TODO set these headers in CLI and HTTP modes only
156+
static constexpr std::string_view DEFAULT_SERVER_NAME{"nginx/0.3.33"};
157+
static constexpr std::string_view DEFAULT_CONTENT_TYPE{"text/html; charset=windows-1251"};
158+
159+
http_server_instance_state.add_header(kphp::http::headers::SERVER, DEFAULT_SERVER_NAME, false);
160+
http_server_instance_state.add_header(kphp::http::headers::CONTENT_TYPE, DEFAULT_CONTENT_TYPE, false);
161+
}
162+
153163
// specific initialization
154164
if constexpr (kind == image_kind::cli) {
155165
co_await init_cli_instance();

tests/phpt/pk/015_header.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@ok k2_skip
1+
@ok
22
<?php
33

44
header('X-TEST-1: value1');
@@ -11,18 +11,23 @@
1111
header('Date: haha, date in tests');
1212
setcookie('test-cookie', 'blabla');
1313
$x = headers_list();
14+
foreach ($x as $_ => &$v) {
15+
$v = strtolower($v);
16+
}
17+
sort($x);
18+
1419

1520
#ifndef KPHP
1621
// not working in console php
1722
$x = [
18-
'Server: nginx/0.3.33',
19-
'Date: haha, date in tests',
20-
'Content-Type: text/html; charset=windows-1251',
21-
'X-TEST-1: value2',
22-
'X-TEST-2: value1',
23-
'X-TEST-2: value2',
24-
'X-TEST-3: value3',
25-
'Set-Cookie: test-cookie=blabla'
23+
'content-type: text/html; charset=windows-1251',
24+
'date: haha, date in tests',
25+
'server: nginx/0.3.33',
26+
'set-cookie: test-cookie=blabla',
27+
'x-test-1: value2',
28+
'x-test-2: value1',
29+
'x-test-2: value2',
30+
'x-test-3: value3'
2631
];
2732
#endif
2833

0 commit comments

Comments
 (0)