Added SILC HTTP, very simple HTTP server.
[silc.git] / lib / silchttp / tests / test_silchttpserver.c
1 /* SilcHttpServer tests */
2
3 #include "silc.h"
4 #include "../silchttpserver.h"
5
6 static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn,
7                           const char *uri, const char *method,
8                           SilcBuffer data, void *context)
9 {
10   SilcBufferStruct page;
11
12   SILC_LOG_DEBUG(("HTTP data received, URI:%s, method:%s", uri, method));
13
14   if (!strcasecmp(method, "GET")) {
15     /* Send our default page */
16     if (!strcmp(uri, "/") || !strcmp(uri, "/index.html")) {
17       memset(&page, 0, sizeof(page));
18       silc_buffer_strformat(&page,
19                             "<html><head></head><body>",
20                             silc_http_server_get_header(httpd, conn,
21                                                         "User-Agent"),
22                             "<p>OUR DEFAULT PAGE IS THIS: ",
23                             silc_time_string(silc_time()),
24                             "<P><FORM action=\"/posttest\" method=\"post\"><P>"
25                             "<LABEL>First name: </LABEL>"
26                             "<INPUT type=\"text\" name=\"firstname\"><BR>"
27                             "<INPUT type=\"radio\" name=\"sex\" value=\"Male\"> Male<BR>"
28                             "<INPUT type=\"radio\" name=\"sex\" value=\"Female\"> Female<BR>"
29                             "<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"
30                             "</P></FORM>"
31                             "</body></html>",
32                             SILC_STRFMT_END);
33       silc_http_server_add_header(httpd, conn, "X-Date",
34                                   silc_time_string(silc_time()));
35       silc_http_server_send(httpd, conn, &page);
36       silc_buffer_purge(&page);
37       return;
38     }
39   }
40
41   if (!strcasecmp(method, "POST")) {
42     if (strcmp(uri, "/posttest"))
43       return;
44     memset(&page, 0, sizeof(page));
45     silc_buffer_strformat(&page,
46                           "<html><head></head><body>",
47                           "POST PROCESSED:",
48                           silc_buffer_data(data),
49                           "</body></html>",
50                           SILC_STRFMT_END);
51     silc_http_server_add_header(httpd, conn, "X-Date",
52                                 silc_time_string(silc_time()));
53     silc_http_server_send(httpd, conn, &page);
54     silc_buffer_purge(&page);
55     return;
56   }
57
58   silc_http_server_send_error(httpd, conn, "404 Not Found",
59                               "<body><h1>404 Not Found: The page you are looking for cannot be located</h1></body>");
60 }
61
62 int main(int argc, char **argv)
63 {
64   SilcBool success = FALSE;
65   SilcSchedule schedule;
66   SilcHttpServer httpd;
67
68   if (argc > 1 && !strcmp(argv[1], "-d")) {
69     silc_log_debug(TRUE);
70     silc_log_debug_hexdump(TRUE);
71     silc_log_set_debug_string("*http*,*mime*");
72   }
73
74   signal(SIGPIPE, SIG_IGN);
75
76   SILC_LOG_DEBUG(("Allocating scheduler"));
77   schedule = silc_schedule_init(0, NULL);
78   if (!schedule)
79     goto err;
80
81   SILC_LOG_DEBUG(("Allocating HTTP server at 127.0.0.1:5000"));
82   httpd = silc_http_server_alloc("127.0.0.1", 5000, 0, schedule,
83                                  http_callback, NULL);
84   if (!httpd)
85     goto err;
86
87   silc_schedule(schedule);
88
89   silc_schedule_uninit(schedule);
90
91   success = TRUE;
92
93  err:
94   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
95   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
96
97   return success;
98 }