123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- #include "php.h"
- #include "SAPI.h"
- #include "php_main.h"
- #ifdef HAVE_PHTTPD
- #include "ext/standard/info.h"
-
- #ifndef ZTS
- #error PHTTPD module is only useable in thread-safe mode
- #endif
- #include "php_phttpd.h"
- typedef struct {
- struct connectioninfo *cip;
- struct stat sb;
- } phttpd_globals_struct;
- static int ph_globals_id;
- #define PHG(v) TSRMG(ph_globals_id, phttpd_globals_struct *, v)
- static int
- php_phttpd_startup(sapi_module_struct *sapi_module)
- {
- fprintf(stderr,"***php_phttpd_startup\n");
- if (php_module_startup(sapi_module, NULL, 0)) {
- return FAILURE;
- } else {
- return SUCCESS;
- }
- }
- static int
- php_phttpd_sapi_ub_write(const char *str, uint str_length TSRMLS_DC)
- {
- int sent_bytes;
- sent_bytes = fd_write(PHG(cip)->fd, str, str_length);
- if (sent_bytes == -1) {
- php_handle_aborted_connection();
- }
- return sent_bytes;
- }
- static int
- php_phttpd_sapi_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC)
- {
- char *header_name, *header_content;
- char *p;
-
- http_sendheaders(PHG(cip)->fd, PHG(cip), SG(sapi_headers).http_response_code, NULL);
- header_name = sapi_header->header;
- header_content = p = strchr(header_name, ':');
-
- if (p) {
- *p = '\0';
- do {
- header_content++;
- } while (*header_content == ' ');
- fd_printf(PHG(cip)->fd,"%s: %s\n", header_name, header_content);
-
- *p = ':';
- }
- sapi_free_header(sapi_header);
- return 0;
- }
- static int
- php_phttpd_sapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
- {
- if (SG(sapi_headers).send_default_content_type) {
- fd_printf(PHG(cip)->fd,"Content-Type: text/html\n");
- }
-
- fd_putc('\n', PHG(cip)->fd);
-
- return SAPI_HEADER_SENT_SUCCESSFULLY;
- }
- static char *
- php_phttpd_sapi_read_cookies(TSRMLS_D)
- {
- fprintf(stderr,"***php_phttpd_sapi_read_cookies\n");
-
- return 0;
- }
- static int
- php_phttpd_sapi_read_post(char *buf, uint count_bytes TSRMLS_DC)
- {
- fprintf(stderr,"***php_phttpd_sapi_read_post\n");
- return 0;
- }
- static sapi_module_struct phttpd_sapi_module = {
- "phttpd",
- "PHTTPD",
-
- php_phttpd_startup,
- php_module_shutdown_wrapper,
-
- NULL,
- NULL,
- php_phttpd_sapi_ub_write,
- NULL,
- NULL,
- NULL,
- php_error,
-
- php_phttpd_sapi_header_handler,
- php_phttpd_sapi_send_headers,
- NULL,
-
- php_phttpd_sapi_read_post,
- php_phttpd_sapi_read_cookies,
-
- NULL,
- NULL,
- NULL,
- NULL,
- STANDARD_SAPI_MODULE_PROPERTIES
- };
- static void
- php_phttpd_request_ctor(TSRMLS_D TSRMLS_DC)
- {
- memset(&SG(request_info), 0, sizeof(sapi_globals_struct));
- SG(request_info).query_string = PHG(cip)->hip->request;
- SG(request_info).request_method = PHG(cip)->hip->method;
- SG(request_info).path_translated = malloc(MAXPATHLEN);
- SG(sapi_headers).http_response_code = 200;
- if (url_expand(PHG(cip)->hip->url, SG(request_info).path_translated, MAXPATHLEN, &PHG(sb), NULL, NULL) == NULL) {
-
- }
- #if 0
- char *server;
- Ns_DString ds;
- char *root;
- int index;
- char *tmp;
-
- server = Ns_ConnServer(NSG(conn));
-
- Ns_DStringInit(&ds);
- Ns_UrlToFile(&ds, server, NSG(conn->request->url));
-
-
- SG(request_info).path_translated = strdup(Ns_DStringValue(&ds));
- Ns_DStringFree(&ds);
- root = Ns_PageRoot(server);
- SG(request_info).request_uri = SG(request_info).path_translated + strlen(root);
- SG(request_info).content_length = Ns_ConnContentLength(NSG(conn));
- index = Ns_SetIFind(NSG(conn)->headers, "content-type");
- SG(request_info).content_type = index == -1 ? NULL :
- Ns_SetValue(NSG(conn)->headers, index);
-
- tmp = Ns_ConnAuthUser(NSG(conn));
- if(tmp) {
- tmp = estrdup(tmp);
- }
- SG(request_info).auth_user = tmp;
-
- tmp = Ns_ConnAuthPasswd(NSG(conn));
- if(tmp) {
- tmp = estrdup(tmp);
- }
- SG(request_info).auth_password = tmp;
-
- NSG(data_avail) = SG(request_info).content_length;
- #endif
- }
- static void
- php_phttpd_request_dtor(TSRMLS_D TSRMLS_DC)
- {
- free(SG(request_info).path_translated);
- }
- int php_doit(TSRMLS_D)
- {
- struct stat sb;
- zend_file_handle file_handle;
- struct httpinfo *hip = PHG(cip)->hip;
- if (php_request_startup(TSRMLS_C) == FAILURE) {
- return -1;
- }
- file_handle.type = ZEND_HANDLE_FILENAME;
- file_handle.filename = SG(request_info).path_translated;
- file_handle.free_filename = 0;
- php_execute_script(&file_handle TSRMLS_CC);
- php_request_shutdown(NULL);
- return SG(sapi_headers).http_response_code;
- }
- int pm_init(const char **argv)
- {
- tsrm_startup(1, 1, 0, NULL);
- sapi_startup(&phttpd_sapi_module);
- phttpd_sapi_module.startup(&phttpd_sapi_module);
- ts_allocate_id(&ph_globals_id, sizeof(phttpd_globals_struct), NULL, NULL);
- return 0;
- }
- void pm_exit(void)
- {
- fprintf(stderr,"***pm_exit\n");
- }
- int pm_request(struct connectioninfo *cip)
- {
- struct httpinfo *hip = cip->hip;
- int status;
- TSRMLS_FETCH();
- if (strcasecmp(hip->method, "GET") == 0 ||
- strcasecmp(hip->method, "HEAD") == 0 ||
- strcasecmp(hip->method, "POST") == 0) {
- PHG(cip) = cip;
-
- php_phttpd_request_ctor(TSRMLS_C);
- status = php_doit(TSRMLS_C);
- php_phttpd_request_dtor(TSRMLS_C);
- return status;
- } else {
- return -2;
- }
- }
- #endif
|