From 83388bbe25d3d560fcc2e71db4904a09e69a36df Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Mon, 18 Sep 2006 15:46:05 +0000 Subject: [PATCH] Added SILC HTTP PHP translator. --- lib/silchttp/DIRECTORY | 3 +- lib/silchttp/Makefile.ad | 8 +- lib/silchttp/silchttpphp.c | 107 +++++++++++++++++++++++ lib/silchttp/silchttpphp.h | 64 ++++++++++++++ lib/silchttp/tests/Makefile.am | 2 +- lib/silchttp/tests/test_silchttpserver.c | 15 +++- 6 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 lib/silchttp/silchttpphp.c create mode 100644 lib/silchttp/silchttpphp.h diff --git a/lib/silchttp/DIRECTORY b/lib/silchttp/DIRECTORY index 60015641..ff8017ea 100644 --- a/lib/silchttp/DIRECTORY +++ b/lib/silchttp/DIRECTORY @@ -2,6 +2,7 @@ @LIBRARY=SILC HTTP Library @FILENAME=silchttplib.html @LINK=silchttpserver.html:SILC HTTP Server Interface +@LINK=silchttpphp.html:SILC HTTP PHP Translator --> SILC HTTP Library @@ -14,7 +15,7 @@

SILC HTTP Library provides currently interface for very simple HTTP -Server. +Server. It also provides support to serve PHP pages through the server.

@LINKS@ diff --git a/lib/silchttp/Makefile.ad b/lib/silchttp/Makefile.ad index 0a8d9578..030e4782 100644 --- a/lib/silchttp/Makefile.ad +++ b/lib/silchttp/Makefile.ad @@ -20,11 +20,13 @@ AUTOMAKE_OPTIONS = 1.0 no-dependencies foreign noinst_LTLIBRARIES = libsilchttp.la libsilchttp_la_SOURCES = \ - silchttpserver.c + silchttpserver.c \ + silchttpphp.c #ifdef SILC_DIST_TOOLKIT -include_HEADERS = \ - silchttpserver.h +include_HEADERS = \ + silchttpserver.h \ + silchttpphp.h SILC_EXTRA_DIST = tests #endif SILC_DIST_TOOLKIT diff --git a/lib/silchttp/silchttpphp.c b/lib/silchttp/silchttpphp.c new file mode 100644 index 00000000..f68cc8c3 --- /dev/null +++ b/lib/silchttp/silchttpphp.c @@ -0,0 +1,107 @@ +/* + + silchttpphp.c + + Author: Pekka Riikonen + + Copyright (C) 2006 Pekka Riikonen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ + +#include "silc.h" +#include "silchttpphp.h" + +/* Executes PHP code and returns result */ + +SilcBuffer silc_http_php(char *php_data) +{ + SilcBuffer ret; + char *name, tmp[32]; + + /* Write the PHP data to temporary file */ +#ifdef SILC_WIN32 + name = _mktemp("silchttpphpXXXXXX"); + if (!name) + return NULL; +#else + memset(tmp, 0, sizeof(tmp)); + snprintf(tmp, sizeof(tmp) - 1, "/tmp/silchttpphpXXXXXX"); + if (mkstemp(tmp) == -1) + return NULL; + name = tmp; +#endif /* SILC_WIN32 */ + + silc_file_writefile_mode(name, php_data, strlen(php_data), 0600); + + /* Execute PHP */ + ret = silc_http_php_file(name); + +#ifdef SILC_WIN32 + _unlink(name); +#else + unlink(name); +#endif /* SILC_WIN32 */ + + return ret; +} + +/* Loads PHP file and executes the PHP code and returns the result */ + +SilcBuffer silc_http_php_file(const char *filename) +{ + SilcBuffer ret = NULL; + unsigned char tmp[8192]; + FILE *fd; + int len, off = 0; + + SILC_LOG_DEBUG(("Executing PHP")); + + memset(tmp, 0, sizeof(tmp)); + snprintf(tmp, sizeof(tmp) - 1, "php -f %s", filename); + +#ifdef SILC_WIN32 + fd = _popen(tmp, "r"); +#else + fd = popen(tmp, "r"); +#endif /* SILC_WIN32 */ + if (!fd) + return NULL; + + /* Read the result */ + do { + len = fread(tmp, 1, sizeof(tmp), fd); + if (len < 0) { + silc_buffer_free(ret); + pclose(fd); + return NULL; + } + + if (len) { + ret = silc_buffer_alloc(0); + if (!ret) { + pclose(fd); + return NULL; + } + + silc_buffer_format(ret, + SILC_STR_OFFSET(off), + SILC_STR_UI_XNSTRING(tmp, len), + SILC_STR_END); + off += len; + } + } while (len); + + if (ret) + silc_buffer_strformat(ret, "\0", SILC_STRFMT_END); + + return ret; +} diff --git a/lib/silchttp/silchttpphp.h b/lib/silchttp/silchttpphp.h new file mode 100644 index 00000000..f0c51122 --- /dev/null +++ b/lib/silchttp/silchttpphp.h @@ -0,0 +1,64 @@ +/* + + silchttpphp.h + + Author: Pekka Riikonen + + Copyright (C) 2006 Pekka Riikonen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ + +/****h* silchttp/SILC HTTP PHP Translator + * + * DESCRIPTION + * + * PHP translator for SILC HTTP Server, enabling PHP support for the pages + * served through the SILC HTTP Server interface (silchttpserver.h). + * The PHP must be installed in the system and must be in the execution + * path for the interface to work. + * + ***/ + +#ifndef SILCHTTPPHP_H +#define SILCHTTPPHP_H + +/****f* silchttp/SilcHTTPServer/silc_http_php + * + * SYNOPSIS + * + * SilcBuffer silc_http_php(char *php_data); + * + * DESCRIPTION + * + * Executes the PHP code contained in the buffer `php_data' and returns + * the result in the allocated SilcBuffer or NULL on error. The caller + * must free the returned buffer. + * + ***/ +SilcBuffer silc_http_php(char *php_data); + +/****f* silchttp/SilcHTTPServer/silc_http_php + * + * SYNOPSIS + * + * SilcBuffer silc_http_php_file(const char *filepath); + * + * DESCRIPTION + * + * Reads the PHP contents from the file indicated by the `filepath' and + * executes the PHP code and returns the result in the allocated + * SilcBuffer or NULL on error. The caller must free the returned buffer. + * + ***/ +SilcBuffer silc_http_php_file(const char *filename); + +#endif /* SILCHTTPPHP_H */ diff --git a/lib/silchttp/tests/Makefile.am b/lib/silchttp/tests/Makefile.am index e3ce7c23..00067b41 100644 --- a/lib/silchttp/tests/Makefile.am +++ b/lib/silchttp/tests/Makefile.am @@ -22,6 +22,6 @@ bin_PROGRAMS = test_silchttpserver test_silchttpserver_SOURCES = test_silchttpserver.c LIBS = $(SILC_COMMON_LIBS) -LDADD = -L.. -L../.. -lsilc +LDADD = -L.. -L../.. -lsilc -lsilchttp include $(top_srcdir)/Makefile.defines.in diff --git a/lib/silchttp/tests/test_silchttpserver.c b/lib/silchttp/tests/test_silchttpserver.c index a6b1b049..9c00851a 100644 --- a/lib/silchttp/tests/test_silchttpserver.c +++ b/lib/silchttp/tests/test_silchttpserver.c @@ -2,6 +2,7 @@ #include "silc.h" #include "../silchttpserver.h" +#include "../silchttpphp.h" static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn, const char *uri, const char *method, @@ -14,6 +15,16 @@ static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn, if (!strcasecmp(method, "GET")) { /* Send our default page */ if (!strcmp(uri, "/") || !strcmp(uri, "/index.html")) { + SilcBuffer php; + const char *php_data = NULL; + + /* Execute PHP data */ + php = silc_http_php("" + "UPDATED " + "| VERSION 4.0 | A HANDMADE WEB-SITE | (C) 1995 - 2006 PEKKA RIIKONEN"); + if (php) + php_data = silc_buffer_data(php); + memset(&page, 0, sizeof(page)); silc_buffer_strformat(&page, "", @@ -28,13 +39,15 @@ static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn, " Male
" " Female
" " " - "

" + "

", + php_data, "", SILC_STRFMT_END); silc_http_server_add_header(httpd, conn, "X-Date", silc_time_string(silc_time())); silc_http_server_send(httpd, conn, &page); silc_buffer_purge(&page); + silc_buffer_free(php); return; } -- 2.24.0