Added SILC HTTP PHP translator.
authorPekka Riikonen <priikone@silcnet.org>
Mon, 18 Sep 2006 15:46:05 +0000 (15:46 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Mon, 18 Sep 2006 15:46:05 +0000 (15:46 +0000)
lib/silchttp/DIRECTORY
lib/silchttp/Makefile.ad
lib/silchttp/silchttpphp.c [new file with mode: 0644]
lib/silchttp/silchttpphp.h [new file with mode: 0644]
lib/silchttp/tests/Makefile.am
lib/silchttp/tests/test_silchttpserver.c

index 60015641070e60aaebbcee97e58e7fabee261ee5..ff8017eac6dc9fe291c9140ff1bb42e83afa231f 100644 (file)
@@ -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
 -->
 
 <big><b>SILC HTTP Library</b></big>
@@ -14,7 +15,7 @@
 
 <br /><br />
 SILC HTTP Library provides currently interface for very simple HTTP 
-Server.
+Server.  It also provides support to serve PHP pages through the server.
 
 <br /><br />
 @LINKS@
index 0a8d9578c716cdc05a78fe6d79f435d6063b702a..030e47820a7eb590ab95054a70cd332bd34b1756 100644 (file)
@@ -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 (file)
index 0000000..f68cc8c
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+
+  silchttpphp.c
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  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 (file)
index 0000000..f0c5112
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+
+  silchttpphp.h
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  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 */
index e3ce7c23602ec352c5bdce301c0d95acaaa6a574..00067b41b3d3b6d9496a871f3e2c56ffb1236db1 100644 (file)
@@ -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
index a6b1b049f9dec87e7d50e9ef5ad27956261bd3a5..9c00851a06c88a9e7ad1c4f94a7587f000f33ee9 100644 (file)
@@ -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("<small>"
+                         "UPDATED <?php echo getcwd(); echo date(\"Y/m/d\", filemtime(\"test_silchttpserver.c\")); ?>"
+                         "| 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,
                            "<html><head></head><body>",
@@ -28,13 +39,15 @@ static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn,
                            "<INPUT type=\"radio\" name=\"sex\" value=\"Male\"> Male<BR>"
                            "<INPUT type=\"radio\" name=\"sex\" value=\"Female\"> Female<BR>"
                            "<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"
-                           "</P></FORM>"
+                           "</P></FORM>",
+                           php_data,
                            "</body></html>",
                            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;
     }