Added SILC HTTP PHP translator.
[silc.git] / lib / silchttp / tests / test_silchttpserver.c
1 /* SilcHttpServer tests */
2
3 #include "silc.h"
4 #include "../silchttpserver.h"
5 #include "../silchttpphp.h"
6
7 static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn,
8                           const char *uri, const char *method,
9                           SilcBuffer data, void *context)
10 {
11   SilcBufferStruct page;
12
13   SILC_LOG_DEBUG(("HTTP data received, URI:%s, method:%s", uri, method));
14
15   if (!strcasecmp(method, "GET")) {
16     /* Send our default page */
17     if (!strcmp(uri, "/") || !strcmp(uri, "/index.html")) {
18       SilcBuffer php;
19       const char *php_data = NULL;
20
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");
25       if (php)
26         php_data = silc_buffer_data(php);
27
28       memset(&page, 0, sizeof(page));
29       silc_buffer_strformat(&page,
30                             "<html><head></head><body>",
31                             silc_http_server_get_header(httpd, conn,
32                                                         "User-Agent"),
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\">"
42                             "</P></FORM>",
43                             php_data,
44                             "</body></html>",
45                             SILC_STRFMT_END);
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);
51       return;
52     }
53
54     if (!strcmp(uri, "/pr_1995.jpg")) {
55       SilcUInt32 data_len;
56       unsigned char *data = silc_file_readfile("pr_1995.jpg", &data_len);
57       if (!data) {
58         silc_http_server_send_error(httpd, conn, "404 Not Found", NULL);
59         return;
60       }
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);
65       return;
66     }
67   }
68
69   if (!strcasecmp(method, "POST")) {
70     if (strcmp(uri, "/posttest"))
71       return;
72     memset(&page, 0, sizeof(page));
73     silc_buffer_strformat(&page,
74                           "<html><head></head><body>",
75                           "POST PROCESSED:",
76                           silc_buffer_data(data),
77                           "</body></html>",
78                           SILC_STRFMT_END);
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);
83     return;
84   }
85
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>");
88 }
89
90 int main(int argc, char **argv)
91 {
92   SilcBool success = FALSE;
93   SilcSchedule schedule;
94   SilcHttpServer httpd;
95
96   if (argc > 1 && !strcmp(argv[1], "-d")) {
97     silc_log_debug(TRUE);
98     silc_log_debug_hexdump(TRUE);
99     silc_log_set_debug_string("*http*,*mime*");
100   }
101
102   signal(SIGPIPE, SIG_IGN);
103
104   SILC_LOG_DEBUG(("Allocating scheduler"));
105   schedule = silc_schedule_init(0, NULL);
106   if (!schedule)
107     goto err;
108
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);
112   if (!httpd)
113     goto err;
114
115   silc_schedule(schedule);
116
117   silc_schedule_uninit(schedule);
118
119   success = TRUE;
120
121  err:
122   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
123   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
124
125   return success;
126 }