updates.
[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><img src=\"pr_1995.jpg\">",
23                             "<p>OUR DEFAULT PAGE IS THIS: ",
24                             silc_time_string(silc_time()),
25                             "<P><FORM action=\"/posttest\" method=\"post\"><P>"
26                             "<LABEL>First name: </LABEL>"
27                             "<INPUT type=\"text\" name=\"firstname\"><BR>"
28                             "<INPUT type=\"radio\" name=\"sex\" value=\"Male\"> Male<BR>"
29                             "<INPUT type=\"radio\" name=\"sex\" value=\"Female\"> Female<BR>"
30                             "<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"
31                             "</P></FORM>"
32                             "</body></html>",
33                             SILC_STRFMT_END);
34       silc_http_server_add_header(httpd, conn, "X-Date",
35                                   silc_time_string(silc_time()));
36       silc_http_server_send(httpd, conn, &page);
37       silc_buffer_purge(&page);
38       return;
39     }
40
41     if (!strcmp(uri, "/pr_1995.jpg")) {
42       SilcUInt32 data_len;
43       unsigned char *data = silc_file_readfile("pr_1995.jpg", &data_len);
44       if (!data) {
45         silc_http_server_send_error(httpd, conn, "404 Not Found", NULL);
46         return;
47       }
48       silc_buffer_set(&page, data, data_len),
49       silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg");
50       silc_http_server_send(httpd, conn, &page);
51       silc_buffer_purge(&page);
52       return;
53     }
54   }
55
56   if (!strcasecmp(method, "POST")) {
57     if (strcmp(uri, "/posttest"))
58       return;
59     memset(&page, 0, sizeof(page));
60     silc_buffer_strformat(&page,
61                           "<html><head></head><body>",
62                           "POST PROCESSED:",
63                           silc_buffer_data(data),
64                           "</body></html>",
65                           SILC_STRFMT_END);
66     silc_http_server_add_header(httpd, conn, "X-Date",
67                                 silc_time_string(silc_time()));
68     silc_http_server_send(httpd, conn, &page);
69     silc_buffer_purge(&page);
70     return;
71   }
72
73   silc_http_server_send_error(httpd, conn, "404 Not Found",
74                               "<body><h1>404 Not Found: The page you are looking for cannot be located</h1></body>");
75 }
76
77 int main(int argc, char **argv)
78 {
79   SilcBool success = FALSE;
80   SilcSchedule schedule;
81   SilcHttpServer httpd;
82
83   if (argc > 1 && !strcmp(argv[1], "-d")) {
84     silc_log_debug(TRUE);
85     silc_log_debug_hexdump(TRUE);
86     silc_log_set_debug_string("*http*,*mime*");
87   }
88
89   signal(SIGPIPE, SIG_IGN);
90
91   SILC_LOG_DEBUG(("Allocating scheduler"));
92   schedule = silc_schedule_init(0, NULL);
93   if (!schedule)
94     goto err;
95
96   SILC_LOG_DEBUG(("Allocating HTTP server at 127.0.0.1:5000"));
97   httpd = silc_http_server_alloc("127.0.0.1", 5000, schedule,
98                                  http_callback, NULL);
99   if (!httpd)
100     goto err;
101
102   silc_schedule(schedule);
103
104   silc_schedule_uninit(schedule);
105
106   success = TRUE;
107
108  err:
109   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
110   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
111
112   return success;
113 }