Long PHP page fix.
[silc.git] / lib / silchttp / tests / test_silchttpserver.c
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>] */
8
9 #include "silc.h"
10 #include "../silchttpserver.h"
11 #include "../silchttpphp.h"
12
13 char *htdocs = ".";
14
15 /* Serve pages */
16
17 static void http_callback_file(SilcHttpServer httpd, SilcHttpConnection conn,
18                                const char *uri, const char *method,
19                                SilcBuffer data, void *context)
20 {
21   SilcBufferStruct page;
22   SilcBuffer php;
23   char *filedata, filename[256];
24   SilcUInt32 data_len;
25   const char *type;
26   SilcBool usephp = FALSE;
27
28   if (!strcasecmp(method, "GET")) {
29     if (strstr(uri, ".php"))
30       usephp = TRUE;
31
32     if (!strcmp(uri, "/"))
33       snprintf(filename, sizeof(filename), "%s/index.html", htdocs);
34     else
35       snprintf(filename, sizeof(filename), "%s%s", htdocs, uri);
36
37     if (strchr(filename, '?'))
38       *strchr(filename, '?') = ' ';
39     while (strchr(filename, '&'))
40       *strchr(filename, '&') = ' ';
41
42     SILC_LOG_DEBUG(("Filename: '%s'", filename));
43
44     if (!usephp) {
45       filedata = silc_file_readfile(filename, &data_len);
46       if (!filedata) {
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>");
49         return;
50       }
51
52       type = silc_http_server_get_header(httpd, conn, "Content-Type");
53       if (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");
61
62       /* Send page */
63       silc_buffer_set(&page, filedata, data_len);
64       silc_http_server_send(httpd, conn, &page);
65       silc_buffer_purge(&page);
66     } else {
67       php = silc_http_php_file(filename);
68       if (!php) {
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>");
71         return;
72       }
73
74       /* Send page */
75       silc_http_server_send(httpd, conn, php);
76       silc_buffer_free(php);
77     }
78
79     return;
80   }
81
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>");
84 }
85
86 int main(int argc, char **argv)
87 {
88   SilcBool success = FALSE;
89   SilcSchedule schedule;
90   SilcHttpServer httpd;
91
92   if (argc > 1) {
93     if (!strcmp(argv[1], "-d")) {
94       silc_log_debug(TRUE);
95       silc_log_debug_hexdump(TRUE);
96       silc_log_set_debug_string("*http*,*mime*");
97       if (argc > 2)
98         htdocs = argv[2];
99     } else {
100       htdocs = argv[1];
101     }
102   }
103
104   signal(SIGPIPE, SIG_IGN);
105
106   SILC_LOG_DEBUG(("Allocating scheduler"));
107   schedule = silc_schedule_init(0, NULL);
108   if (!schedule)
109     goto err;
110
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);
114   if (!httpd)
115     goto err;
116
117   silc_schedule(schedule);
118
119   silc_schedule_uninit(schedule);
120
121   success = TRUE;
122
123  err:
124   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
125   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
126
127   return success;
128 }