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"
17 static void http_callback_file(SilcHttpServer httpd, SilcHttpConnection conn,
18 const char *uri, const char *method,
19 SilcBuffer data, void *context)
21 SilcBufferStruct page;
23 char *filedata, filename[256];
26 SilcBool usephp = FALSE;
28 if (!strcasecmp(method, "GET")) {
29 if (strstr(uri, ".php"))
32 if (!strcmp(uri, "/"))
33 snprintf(filename, sizeof(filename), "%s/index.html", htdocs);
35 snprintf(filename, sizeof(filename), "%s%s", htdocs, uri);
37 if (strchr(filename, '?'))
38 *strchr(filename, '?') = ' ';
39 while (strchr(filename, '&'))
40 *strchr(filename, '&') = ' ';
42 SILC_LOG_DEBUG(("Filename: '%s'", filename));
45 filedata = silc_file_readfile(filename, &data_len);
47 silc_http_server_send_error(httpd, conn, "404 Not Found",
48 "<body><h1>404 Not Found</h1><p>The page you are looking for cannot be located</body>");
52 type = silc_http_server_get_header(httpd, conn, "Content-Type");
54 silc_http_server_add_header(httpd, conn, "Content-Type", type);
55 else if (strstr(uri, ".jpg"))
56 silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg");
57 else if (strstr(uri, ".gif"))
58 silc_http_server_add_header(httpd, conn, "Content-Type", "image/gif");
59 else if (strstr(uri, ".png"))
60 silc_http_server_add_header(httpd, conn, "Content-Type", "image/png");
63 silc_buffer_set(&page, filedata, data_len);
64 silc_http_server_send(httpd, conn, &page);
65 silc_buffer_purge(&page);
67 php = silc_http_php_file(filename);
69 silc_http_server_send_error(httpd, conn, "404 Not Found",
70 "<body><h1>404 Not Found</h1><p>The page you are looking for cannot be located</body>");
75 silc_http_server_send(httpd, conn, php);
76 silc_buffer_free(php);
82 silc_http_server_send_error(httpd, conn, "404 Not Found",
83 "<body><h1>404 Not Found</h1><p>The page you are looking for cannot be located</body>");
86 int main(int argc, char **argv)
88 SilcBool success = FALSE;
89 SilcSchedule schedule;
93 if (!strcmp(argv[1], "-d")) {
95 silc_log_debug_hexdump(TRUE);
96 silc_log_set_debug_string("*http*,*mime*");
104 signal(SIGPIPE, SIG_IGN);
106 SILC_LOG_DEBUG(("Allocating scheduler"));
107 schedule = silc_schedule_init(0, NULL);
111 SILC_LOG_DEBUG(("Allocating HTTP server at 127.0.0.1:5000"));
112 httpd = silc_http_server_alloc("127.0.0.1", 5000, schedule,
113 http_callback_file, NULL);
117 silc_schedule(schedule);
119 silc_schedule_uninit(schedule);
124 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
125 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");