X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=blobdiff_plain;f=lib%2Fsilchttp%2Ftests%2Ftest_silchttpserver.c;h=cb9050b14f93a6c09b1595de03c7a815e3417ca6;hp=e4fc526ae568f3ff592df33804339b65faf540e0;hb=08e0dddd6750b245da53e2bc3528d0b5a6d57e8b;hpb=c33005f2f5ead342fd0417931e38013aacfb98b8 diff --git a/lib/silchttp/tests/test_silchttpserver.c b/lib/silchttp/tests/test_silchttpserver.c index e4fc526a..cb9050b1 100644 --- a/lib/silchttp/tests/test_silchttpserver.c +++ b/lib/silchttp/tests/test_silchttpserver.c @@ -1,62 +1,103 @@ /* 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 = "."; + +/* Add proper content type to reply per URI */ + +static void http_content_type(SilcHttpServer httpd, SilcHttpConnection conn, + const char *uri) { - SilcBufferStruct page; + const char *type; + + 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"); + else if (strstr(uri, ".css")) + silc_http_server_add_header(httpd, conn, "Content-Type", "text/css"); + else if (strstr(uri, ".htm")) + silc_http_server_add_header(httpd, conn, "Content-Type", "text/html"); + else if (strstr(uri, ".php")) + silc_http_server_add_header(httpd, conn, "Content-Type", "text/html"); +} - SILC_LOG_DEBUG(("HTTP data received, URI:%s, method:%s", uri, method)); +/* Serve pages */ + +static void http_callback_file(SilcHttpServer httpd, SilcHttpConnection conn, + const char *uri, const char *method, + SilcBuffer data, void *context) +{ + SilcBufferStruct page; + SilcBuffer php; + char *filedata, filename[256]; + SilcUInt32 data_len; + SilcBool usephp = FALSE; if (!strcasecmp(method, "GET")) { - /* Send our default page */ - if (!strcmp(uri, "/") || !strcmp(uri, "/index.html")) { - 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
" - " " - "

" - "", - SILC_STRFMT_END); - silc_http_server_add_header(httpd, conn, "X-Date", - silc_time_string(silc_time())); + 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, NULL); + 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; + } + + http_content_type(httpd, conn, uri); + + /* 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; + } + + http_content_type(httpd, conn, uri); + + /* 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) @@ -65,22 +106,28 @@ 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); SILC_LOG_DEBUG(("Allocating scheduler")); - schedule = silc_schedule_init(0, NULL); + schedule = silc_schedule_init(0, NULL, NULL); if (!schedule) 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, - http_callback, NULL); + httpd = silc_http_server_alloc("127.0.0.1", 5000, schedule, + http_callback_file, NULL); if (!httpd) goto err;