From 7df5e78201d7eb15ff5ecdc378af70d7d1ea1453 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Mon, 18 Sep 2006 18:00:01 +0000 Subject: [PATCH] Long PHP page fix. The test program is now almost full-fledged HTTPD able to serve HTML and PHP pages. --- lib/silchttp/silchttpphp.c | 8 +- lib/silchttp/tests/test_silchttpserver.c | 148 ++++++++++++----------- 2 files changed, 80 insertions(+), 76 deletions(-) diff --git a/lib/silchttp/silchttpphp.c b/lib/silchttp/silchttpphp.c index f68cc8c3..02f95e2c 100644 --- a/lib/silchttp/silchttpphp.c +++ b/lib/silchttp/silchttpphp.c @@ -86,10 +86,12 @@ SilcBuffer silc_http_php_file(const char *filename) } if (len) { - ret = silc_buffer_alloc(0); if (!ret) { - pclose(fd); - return NULL; + ret = silc_buffer_alloc(0); + if (!ret) { + pclose(fd); + return NULL; + } } silc_buffer_format(ret, diff --git a/lib/silchttp/tests/test_silchttpserver.c b/lib/silchttp/tests/test_silchttpserver.c index 9c00851a..76604b88 100644 --- a/lib/silchttp/tests/test_silchttpserver.c +++ b/lib/silchttp/tests/test_silchttpserver.c @@ -1,90 +1,86 @@ /* SilcHttpServer tests */ +/* Actually this is almost a full-fledged HTTP server. It can serve HTML + and PHP pages pretty well. In PHP the variables passed in URI with '?' + work in PHP script, with this HTTPD of ours, only if $_REQUEST variable + is used to fetch them (limitation in PHP command line version). In other + ways '?' in URI is not supported. */ +/* Usage: ./test_silchttpserver [-d] [] */ #include "silc.h" #include "../silchttpserver.h" #include "../silchttpphp.h" -static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn, - const char *uri, const char *method, - SilcBuffer data, void *context) +char *htdocs = "."; + +/* Serve pages */ + +static void http_callback_file(SilcHttpServer httpd, SilcHttpConnection conn, + const char *uri, const char *method, + SilcBuffer data, void *context) { SilcBufferStruct page; - - SILC_LOG_DEBUG(("HTTP data received, URI:%s, method:%s", uri, method)); + SilcBuffer php; + char *filedata, filename[256]; + SilcUInt32 data_len; + const char *type; + SilcBool usephp = FALSE; 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, - "", - silc_http_server_get_header(httpd, conn, - "User-Agent"), - "

", - "

OUR DEFAULT PAGE IS THIS: ", - silc_time_string(silc_time()), - "

" - "" - "
" - " 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; - } - - 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; + if (strstr(uri, ".php")) + usephp = TRUE; + + if (!strcmp(uri, "/")) + snprintf(filename, sizeof(filename), "%s/index.html", htdocs); + else + snprintf(filename, sizeof(filename), "%s%s", htdocs, uri); + + if (strchr(filename, '?')) + *strchr(filename, '?') = ' '; + while (strchr(filename, '&')) + *strchr(filename, '&') = ' '; + + SILC_LOG_DEBUG(("Filename: '%s'", filename)); + + if (!usephp) { + filedata = silc_file_readfile(filename, &data_len); + if (!filedata) { + silc_http_server_send_error(httpd, conn, "404 Not Found", + "

404 Not Found

The page you are looking for cannot be located"); + return; } - silc_buffer_set(&page, data, data_len), - silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg"); + + type = silc_http_server_get_header(httpd, conn, "Content-Type"); + if (type) + silc_http_server_add_header(httpd, conn, "Content-Type", type); + else if (strstr(uri, ".jpg")) + silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg"); + else if (strstr(uri, ".gif")) + silc_http_server_add_header(httpd, conn, "Content-Type", "image/gif"); + else if (strstr(uri, ".png")) + silc_http_server_add_header(httpd, conn, "Content-Type", "image/png"); + + /* Send page */ + silc_buffer_set(&page, filedata, data_len); silc_http_server_send(httpd, conn, &page); silc_buffer_purge(&page); - return; + } else { + php = silc_http_php_file(filename); + if (!php) { + silc_http_server_send_error(httpd, conn, "404 Not Found", + "

404 Not Found

The page you are looking for cannot be located"); + return; + } + + /* Send page */ + silc_http_server_send(httpd, conn, php); + silc_buffer_free(php); } - } - if (!strcasecmp(method, "POST")) { - if (strcmp(uri, "/posttest")) - return; - memset(&page, 0, sizeof(page)); - silc_buffer_strformat(&page, - "", - "POST PROCESSED:", - silc_buffer_data(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); return; } silc_http_server_send_error(httpd, conn, "404 Not Found", - "

404 Not Found: The page you are looking for cannot be located

"); + "

404 Not Found

The page you are looking for cannot be located"); } int main(int argc, char **argv) @@ -93,10 +89,16 @@ int main(int argc, char **argv) SilcSchedule schedule; SilcHttpServer httpd; - if (argc > 1 && !strcmp(argv[1], "-d")) { - silc_log_debug(TRUE); - silc_log_debug_hexdump(TRUE); - silc_log_set_debug_string("*http*,*mime*"); + if (argc > 1) { + if (!strcmp(argv[1], "-d")) { + silc_log_debug(TRUE); + silc_log_debug_hexdump(TRUE); + silc_log_set_debug_string("*http*,*mime*"); + if (argc > 2) + htdocs = argv[2]; + } else { + htdocs = argv[1]; + } } signal(SIGPIPE, SIG_IGN); @@ -108,7 +110,7 @@ int main(int argc, char **argv) SILC_LOG_DEBUG(("Allocating HTTP server at 127.0.0.1:5000")); httpd = silc_http_server_alloc("127.0.0.1", 5000, schedule, - http_callback, NULL); + http_callback_file, NULL); if (!httpd) goto err; -- 2.24.0