nxt_php_sapi.c (206:86a529b2ea9b) nxt_php_sapi.c (216:07257705cd64)
1
2/*
3 * Copyright (C) Max Romanov
4 * Copyright (C) Valentin V. Bartenev
5 * Copyright (C) NGINX, Inc.
6 */
7
8#include "php.h"
9#include "SAPI.h"
10#include "php_main.h"
11#include "php_variables.h"
12
13#include <nxt_main.h>
14#include <nxt_application.h>
15
16
17static nxt_int_t nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf);
18
1
2/*
3 * Copyright (C) Max Romanov
4 * Copyright (C) Valentin V. Bartenev
5 * Copyright (C) NGINX, Inc.
6 */
7
8#include "php.h"
9#include "SAPI.h"
10#include "php_main.h"
11#include "php_variables.h"
12
13#include <nxt_main.h>
14#include <nxt_application.h>
15
16
17static nxt_int_t nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf);
18
19static nxt_int_t nxt_php_prepare_msg(nxt_task_t *task,
20 nxt_app_request_t *r, nxt_app_wmsg_t *wmsg);
21
22static nxt_int_t nxt_php_run(nxt_task_t *task,
23 nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *wmsg);
24
25#if PHP_MAJOR_VERSION >= 7
26# define NXT_PHP7 1
27# if PHP_MINOR_VERSION >= 1
28# define NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE 1
29# else

--- 23 unchanged lines hidden (view full) ---

53static size_t nxt_php_read_post(char *buffer, size_t count_bytes TSRMLS_DC);
54#else
55static int nxt_php_unbuffered_write(const char *str, uint str_length TSRMLS_DC);
56static int nxt_php_read_post(char *buffer, uint count_bytes TSRMLS_DC);
57#endif
58
59static void nxt_php_flush(void *server_context);
60
19static nxt_int_t nxt_php_run(nxt_task_t *task,
20 nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *wmsg);
21
22#if PHP_MAJOR_VERSION >= 7
23# define NXT_PHP7 1
24# if PHP_MINOR_VERSION >= 1
25# define NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE 1
26# else

--- 23 unchanged lines hidden (view full) ---

50static size_t nxt_php_read_post(char *buffer, size_t count_bytes TSRMLS_DC);
51#else
52static int nxt_php_unbuffered_write(const char *str, uint str_length TSRMLS_DC);
53static int nxt_php_read_post(char *buffer, uint count_bytes TSRMLS_DC);
54#endif
55
56static void nxt_php_flush(void *server_context);
57
61extern nxt_int_t nxt_php_sapi_init(nxt_thread_t *thr, nxt_runtime_t *rt);
62
58
63
64static sapi_module_struct nxt_php_sapi_module =
65{
66 (char *) "cli-server",
67 (char *) "nginext",
68
69 nxt_php_startup, /* startup */
70 php_module_shutdown_wrapper, /* shutdown */
71

--- 96 unchanged lines hidden (view full) ---

168{
169 while (str->length > 0 && str->start[0] == t) {
170 str->length--;
171 str->start++;
172 }
173}
174
175
59static sapi_module_struct nxt_php_sapi_module =
60{
61 (char *) "cli-server",
62 (char *) "nginext",
63
64 nxt_php_startup, /* startup */
65 php_module_shutdown_wrapper, /* shutdown */
66

--- 96 unchanged lines hidden (view full) ---

163{
164 while (str->length > 0 && str->start[0] == t) {
165 str->length--;
166 str->start++;
167 }
168}
169
170
176nxt_application_module_t nxt_php_module = {
171NXT_EXPORT nxt_application_module_t nxt_app_module = {
172 nxt_string("php"),
173 nxt_string(PHP_VERSION),
177 nxt_php_init,
174 nxt_php_init,
178 nxt_php_prepare_msg,
179 nxt_php_run
175 nxt_php_run,
180};
181
182
176};
177
178
183nxt_int_t
184nxt_php_sapi_init(nxt_thread_t *thr, nxt_runtime_t *rt)
185{
186 nxt_app_modules[NXT_APP_PHP] = &nxt_php_module;
187
188#if PHP_MAJOR_VERSION == 5
189 nxt_app_modules[NXT_APP_PHP5] = &nxt_php_module;
190#endif
191
192#if PHP_MAJOR_VERSION == 7
193 nxt_app_modules[NXT_APP_PHP7] = &nxt_php_module;
194#endif
195
196 return NXT_OK;
197}
198
199
200static nxt_int_t
201nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
202{
203 nxt_str_t *root, *path, *script, *index;
204 nxt_php_app_conf_t *c;
205
206 c = &conf->u.php;
207

--- 144 unchanged lines hidden (view full) ---

352
353fail:
354
355 return rc;
356}
357
358
359static nxt_int_t
179static nxt_int_t
180nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
181{
182 nxt_str_t *root, *path, *script, *index;
183 nxt_php_app_conf_t *c;
184
185 c = &conf->u.php;
186

--- 144 unchanged lines hidden (view full) ---

331
332fail:
333
334 return rc;
335}
336
337
338static nxt_int_t
360nxt_php_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
361 nxt_app_wmsg_t *wmsg)
362{
363 nxt_int_t rc;
364 nxt_buf_t *b;
365 nxt_http_field_t *field;
366 nxt_app_request_header_t *h;
367
368 static const nxt_str_t prefix = nxt_string("HTTP_");
369 static const nxt_str_t eof = nxt_null_string;
370
371 h = &r->header;
372
373#define RC(S) \
374 do { \
375 rc = (S); \
376 if (nxt_slow_path(rc != NXT_OK)) { \
377 goto fail; \
378 } \
379 } while(0)
380
381#define NXT_WRITE(N) \
382 RC(nxt_app_msg_write_str(task, wmsg, N))
383
384 /* TODO error handle, async mmap buffer assignment */
385
386 NXT_WRITE(&h->method);
387 NXT_WRITE(&h->target);
388 if (h->path.start == h->target.start) {
389 NXT_WRITE(&eof);
390 } else {
391 NXT_WRITE(&h->path);
392 }
393
394 if (h->query.start != NULL) {
395 RC(nxt_app_msg_write_size(task, wmsg,
396 h->query.start - h->target.start + 1));
397 } else {
398 RC(nxt_app_msg_write_size(task, wmsg, 0));
399 }
400
401 NXT_WRITE(&h->version);
402
403 // PHP_SELF
404 // SCRIPT_NAME
405 // SCRIPT_FILENAME
406 // DOCUMENT_ROOT
407
408 NXT_WRITE(&r->remote);
409
410 NXT_WRITE(&h->host);
411 NXT_WRITE(&h->cookie);
412 NXT_WRITE(&h->content_type);
413 NXT_WRITE(&h->content_length);
414
415 RC(nxt_app_msg_write_size(task, wmsg, h->parsed_content_length));
416
417 nxt_list_each(field, h->fields) {
418 RC(nxt_app_msg_write_prefixed_upcase(task, wmsg,
419 &prefix, &field->name));
420 NXT_WRITE(&field->value);
421
422 } nxt_list_loop;
423
424 /* end-of-headers mark */
425 NXT_WRITE(&eof);
426
427 RC(nxt_app_msg_write_size(task, wmsg, r->body.preread_size));
428
429 for(b = r->body.buf; b != NULL; b = b->next) {
430 RC(nxt_app_msg_write_raw(task, wmsg, b->mem.pos,
431 nxt_buf_mem_used_size(&b->mem)));
432 }
433
434#undef NXT_WRITE
435#undef RC
436
437 return NXT_OK;
438
439fail:
440
441 return NXT_ERROR;
442}
443
444
445static nxt_int_t
446nxt_php_run(nxt_task_t *task,
447 nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *wmsg)
448{
449 nxt_int_t rc;
450 zend_file_handle file_handle;
451 nxt_php_run_ctx_t run_ctx;
452 nxt_app_request_header_t *h;
453

--- 397 unchanged lines hidden ---
339nxt_php_run(nxt_task_t *task,
340 nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *wmsg)
341{
342 nxt_int_t rc;
343 zend_file_handle file_handle;
344 nxt_php_run_ctx_t run_ctx;
345 nxt_app_request_header_t *h;
346

--- 397 unchanged lines hidden ---