Added SILC HTTP PHP translator.
[silc.git] / lib / silchttp / tests / test_silchttpserver.c
index e4fc526ae568f3ff592df33804339b65faf540e0..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,11 +15,22 @@ 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>",
                            silc_http_server_get_header(httpd, conn,
                                                        "User-Agent"),
+                           "<p><img src=\"pr_1995.jpg\">",
                            "<p>OUR DEFAULT PAGE IS THIS: ",
                            silc_time_string(silc_time()),
                            "<P><FORM action=\"/posttest\" method=\"post\"><P>"
@@ -27,13 +39,29 @@ 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;
+    }
+
+    if (!strcmp(uri, "/pr_1995.jpg")) {
+      SilcUInt32 data_len;
+      unsigned char *data = silc_file_readfile("pr_1995.jpg", &data_len);
+      if (!data) {
+       silc_http_server_send_error(httpd, conn, "404 Not Found", NULL);
+        return;
+      }
+      silc_buffer_set(&page, data, data_len),
+      silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg");
+      silc_http_server_send(httpd, conn, &page);
+      silc_buffer_purge(&page);
       return;
     }
   }
@@ -79,7 +107,7 @@ int main(int argc, char **argv)
     goto err;
 
   SILC_LOG_DEBUG(("Allocating HTTP server at 127.0.0.1:5000"));
-  httpd = silc_http_server_alloc("127.0.0.1", 5000, 0, schedule,
+  httpd = silc_http_server_alloc("127.0.0.1", 5000, schedule,
                                 http_callback, NULL);
   if (!httpd)
     goto err;