Skip to content

Commit 16d7020

Browse files
committed
Ensure cookies are passed to PHP request.
1 parent 57fc87f commit 16d7020

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/node_php_embed.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ static void node_php_embed_send_header(sapi_header_struct *sapi_header,
139139
TRACE("<");
140140
}
141141

142+
static char * node_php_embed_read_cookies(TSRMLS_D) {
143+
// This is a hack to prevent the SAPI from overwriting the
144+
// cookie data we set up in the PhpRequestWorker constructor.
145+
return SG(request_info).cookie_data;
146+
}
147+
142148
static void node_php_embed_register_server_variables(
143149
zval *track_vars_array TSRMLS_DC) {
144150
TRACE(">");
@@ -275,15 +281,7 @@ static void node_php_embed_ensure_init(void) {
275281
// Shutdown the initially-created request; we'll create our own request
276282
// objects inside PhpRequestWorker.
277283
php_request_shutdown(nullptr);
278-
#define CHECK_NULL(requestvar) \
279-
if (SG(request_info).requestvar) { \
280-
NPE_ERROR("OOPS! " #requestvar " is set!"); \
281-
SG(request_info).requestvar = nullptr; \
282-
}
283-
CHECK_NULL(request_method);
284-
CHECK_NULL(query_string);
285-
CHECK_NULL(path_translated);
286-
CHECK_NULL(request_uri);
284+
PhpRequestWorker::CheckRequestInfo(TSRMLS_C);
287285
node::AtExit(ModuleShutdown, nullptr);
288286
TRACE("<");
289287
}
@@ -294,10 +292,13 @@ NAN_MODULE_INIT(ModuleInit) {
294292
php_embed_module.php_ini_ignore = true;
295293
php_embed_module.php_ini_ignore_cwd = true;
296294
php_embed_module.ini_defaults = nullptr;
295+
// The following initialization statements are kept in the same
296+
// order as the fields in `struct _sapi_module_struct` (SAPI.h)
297297
php_embed_module.startup = node_php_embed_startup;
298-
php_embed_module.send_header = node_php_embed_send_header;
299298
php_embed_module.ub_write = node_php_embed_ub_write;
300299
php_embed_module.flush = node_php_embed_flush;
300+
php_embed_module.send_header = node_php_embed_send_header;
301+
php_embed_module.read_cookies = node_php_embed_read_cookies;
301302
php_embed_module.register_server_variables =
302303
node_php_embed_register_server_variables;
303304
// Most of init will be done lazily in node_php_embed_ensure_init()

src/phprequestworker.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,16 @@ void PhpRequestWorker::Execute(MapperChannel *channel TSRMLS_DC) {
8383
efree(const_cast<char*>(SG(request_info).requestvar)); \
8484
SG(request_info).requestvar = nullptr; \
8585
}
86+
#define CHECK_REQUEST_INFO(requestvar) \
87+
if (SG(request_info).requestvar) { \
88+
NPE_ERROR("OOPS! " #requestvar " is set!"); \
89+
SG(request_info).requestvar = nullptr; \
90+
}
8691
SET_REQUEST_INFO("REQUEST_METHOD", request_method);
8792
SET_REQUEST_INFO("QUERY_STRING", query_string);
8893
SET_REQUEST_INFO("PATH_TRANSLATED", path_translated);
8994
SET_REQUEST_INFO("REQUEST_URI", request_uri);
95+
SET_REQUEST_INFO("HTTP_COOKIE", cookie_data);
9096
// xxx set proto_num ?
9197
// xxx set cookie_data ?
9298
server_vars_.clear(); // We don't need to keep this around any more.
@@ -148,9 +154,17 @@ void PhpRequestWorker::AfterExecute(TSRMLS_D) {
148154
FREE_REQUEST_INFO(query_string);
149155
FREE_REQUEST_INFO(path_translated);
150156
FREE_REQUEST_INFO(request_uri);
157+
FREE_REQUEST_INFO(cookie_data);
151158
php_request_shutdown(nullptr);
152159
TRACE("< PhpRequestWorker");
153160
}
161+
void PhpRequestWorker::CheckRequestInfo(TSRMLS_D) {
162+
CHECK_REQUEST_INFO(request_method);
163+
CHECK_REQUEST_INFO(query_string);
164+
CHECK_REQUEST_INFO(path_translated);
165+
CHECK_REQUEST_INFO(request_uri);
166+
CHECK_REQUEST_INFO(cookie_data);
167+
}
154168

155169
// Executed when the async work is complete.
156170
// This function will be run inside the main event loop

src/phprequestworker.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ class PhpRequestWorker : public AsyncMessageWorker {
3535
void Execute(MapperChannel *channel TSRMLS_DC) override;
3636
void AfterAsyncLoop(TSRMLS_D) override;
3737
void AfterExecute(TSRMLS_D) override;
38+
39+
// Executed in the JS thread.
3840
void HandleOKCallback(JsObjectMapper *m) override;
3941

42+
// Used during module startup to check SG(request_info)
43+
static void CheckRequestInfo(TSRMLS_D);
44+
4045
private:
4146
Value source_;
4247
Value result_;

0 commit comments

Comments
 (0)