1 /* SilcHttpServer tests */
2 /* Actually this is almost a full-fledged HTTP server. It can serve HTML
3 and PHP pages pretty well. In PHP the variables passed in URI with '?'
4 work in PHP script, with this HTTPD of ours, only if $_REQUEST variable
5 is used to fetch them (limitation in PHP command line version). In other
6 ways '?' in URI is not supported. */
7 /* Usage: ./test_silchttpserver [-d] [<htdocsdir>] */
10 #include "../silchttpserver.h"
11 #include "../silchttpphp.h"
15 /* Add proper content type to reply per URI */
17 static void http_content_type(SilcHttpServer httpd, SilcHttpConnection conn,
22 type = silc_http_server_get_header(httpd, conn, "Content-Type");
24 silc_http_server_add_header(httpd, conn, "Content-Type", type);
25 else if (strstr(uri, ".jpg"))
26 silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg");
27 else if (strstr(uri, ".gif"))
28 silc_http_server_add_header(httpd, conn, "Content-Type", "image/gif");
29 else if (strstr(uri, ".png"))
30 silc_http_server_add_header(httpd, conn, "Content-Type", "image/png");
31 else if (strstr(uri, ".css"))
32 silc_http_server_add_header(httpd, conn, "Content-Type", "text/css");
33 else if (strstr(uri, ".htm"))
34 silc_http_server_add_header(httpd, conn, "Content-Type", "text/html");
35 else if (strstr(uri, ".php"))
36 silc_http_server_add_header(httpd, conn, "Content-Type", "text/html");
41 static void http_callback_file(SilcHttpServer httpd, SilcHttpConnection conn,
42 const char *uri, const char *method,
43 SilcBuffer data, void *context)
45 SilcBufferStruct page;
47 char *filedata, filename[256];
49 SilcBool usephp = FALSE;
51 if (!strcasecmp(method, "GET")) {
52 if (strstr(uri, ".php"))
55 if (!strcmp(uri, "/"))
56 snprintf(filename, sizeof(filename), "%s/index.html", htdocs);
58 snprintf(filename, sizeof(filename), "%s%s", htdocs, uri);
60 if (strchr(filename, '?'))
61 *strchr(filename, '?') = ' ';
62 while (strchr(filename, '&'))
63 *strchr(filename, '&') = ' ';
65 SILC_LOG_DEBUG(("Filename: '%s'", filename));
68 filedata = silc_file_readfile(filename, &data_len, NULL);
70 silc_http_server_send_error(httpd, conn, "404 Not Found",
71 "<body><h1>404 Not Found</h1><p>The page you are looking for cannot be located</body>");
75 http_content_type(httpd, conn, uri);
78 silc_buffer_set(&page, filedata, data_len);
79 silc_http_server_send(httpd, conn, &page);
80 silc_buffer_purge(&page);
82 php = silc_http_php_file(filename);
84 silc_http_server_send_error(httpd, conn, "404 Not Found",
85 "<body><h1>404 Not Found</h1><p>The page you are looking for cannot be located</body>");
89 http_content_type(httpd, conn, uri);
92 silc_http_server_send(httpd, conn, php);
93 silc_buffer_free(php);
99 silc_http_server_send_error(httpd, conn, "404 Not Found",
100 "<body><h1>404 Not Found</h1><p>The page you are looking for cannot be located</body>");
103 int main(int argc, char **argv)
105 SilcBool success = FALSE;
106 SilcSchedule schedule;
107 SilcHttpServer httpd;
110 if (!strcmp(argv[1], "-d")) {
111 silc_log_debug(TRUE);
112 silc_log_debug_hexdump(TRUE);
113 silc_log_set_debug_string("*http*,*mime*");
121 signal(SIGPIPE, SIG_IGN);
123 SILC_LOG_DEBUG(("Allocating scheduler"));
124 schedule = silc_schedule_init(0, NULL, NULL);
128 SILC_LOG_DEBUG(("Allocating HTTP server at 127.0.0.1:5000"));
129 httpd = silc_http_server_alloc("127.0.0.1", 5000, schedule,
130 http_callback_file, NULL);
134 silc_schedule(schedule);
136 silc_schedule_uninit(schedule);
141 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
142 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");