1 /* SilcHttpServer tests */
4 #include "../silchttpserver.h"
5 #include "../silchttpphp.h"
7 static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn,
8 const char *uri, const char *method,
9 SilcBuffer data, void *context)
11 SilcBufferStruct page;
13 SILC_LOG_DEBUG(("HTTP data received, URI:%s, method:%s", uri, method));
15 if (!strcasecmp(method, "GET")) {
16 /* Send our default page */
17 if (!strcmp(uri, "/") || !strcmp(uri, "/index.html")) {
19 const char *php_data = NULL;
21 /* Execute PHP data */
22 php = silc_http_php("<small>"
23 "UPDATED <?php echo getcwd(); echo date(\"Y/m/d\", filemtime(\"test_silchttpserver.c\")); ?>"
24 "| VERSION 4.0 | A HANDMADE WEB-SITE | (C) 1995 - 2006 PEKKA RIIKONEN");
26 php_data = silc_buffer_data(php);
28 memset(&page, 0, sizeof(page));
29 silc_buffer_strformat(&page,
30 "<html><head></head><body>",
31 silc_http_server_get_header(httpd, conn,
33 "<p><img src=\"pr_1995.jpg\">",
34 "<p>OUR DEFAULT PAGE IS THIS: ",
35 silc_time_string(silc_time()),
36 "<P><FORM action=\"/posttest\" method=\"post\"><P>"
37 "<LABEL>First name: </LABEL>"
38 "<INPUT type=\"text\" name=\"firstname\"><BR>"
39 "<INPUT type=\"radio\" name=\"sex\" value=\"Male\"> Male<BR>"
40 "<INPUT type=\"radio\" name=\"sex\" value=\"Female\"> Female<BR>"
41 "<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"
46 silc_http_server_add_header(httpd, conn, "X-Date",
47 silc_time_string(silc_time()));
48 silc_http_server_send(httpd, conn, &page);
49 silc_buffer_purge(&page);
50 silc_buffer_free(php);
54 if (!strcmp(uri, "/pr_1995.jpg")) {
56 unsigned char *data = silc_file_readfile("pr_1995.jpg", &data_len);
58 silc_http_server_send_error(httpd, conn, "404 Not Found", NULL);
61 silc_buffer_set(&page, data, data_len),
62 silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg");
63 silc_http_server_send(httpd, conn, &page);
64 silc_buffer_purge(&page);
69 if (!strcasecmp(method, "POST")) {
70 if (strcmp(uri, "/posttest"))
72 memset(&page, 0, sizeof(page));
73 silc_buffer_strformat(&page,
74 "<html><head></head><body>",
76 silc_buffer_data(data),
79 silc_http_server_add_header(httpd, conn, "X-Date",
80 silc_time_string(silc_time()));
81 silc_http_server_send(httpd, conn, &page);
82 silc_buffer_purge(&page);
86 silc_http_server_send_error(httpd, conn, "404 Not Found",
87 "<body><h1>404 Not Found: The page you are looking for cannot be located</h1></body>");
90 int main(int argc, char **argv)
92 SilcBool success = FALSE;
93 SilcSchedule schedule;
96 if (argc > 1 && !strcmp(argv[1], "-d")) {
98 silc_log_debug_hexdump(TRUE);
99 silc_log_set_debug_string("*http*,*mime*");
102 signal(SIGPIPE, SIG_IGN);
104 SILC_LOG_DEBUG(("Allocating scheduler"));
105 schedule = silc_schedule_init(0, NULL);
109 SILC_LOG_DEBUG(("Allocating HTTP server at 127.0.0.1:5000"));
110 httpd = silc_http_server_alloc("127.0.0.1", 5000, schedule,
111 http_callback, NULL);
115 silc_schedule(schedule);
117 silc_schedule_uninit(schedule);
122 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
123 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");