Added socket stream and socket into SilcClientConnection context.
[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 /* Add proper content type to reply per URI */
16
17 static void http_content_type(SilcHttpServer httpd, SilcHttpConnection conn,
18                               const char *uri)
19 {
20   const char *type;
21
22   type = silc_http_server_get_header(httpd, conn, "Content-Type");
23   if (type)
24     silc_http_server_add_header(httpd, conn, "Content-Type", type);
25   else if (strstr(uri, ".jpg"))
26     silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg");
27   else if (strstr(uri, ".gif"))
28     silc_http_server_add_header(httpd, conn, "Content-Type", "image/gif");
29   else if (strstr(uri, ".png"))
30     silc_http_server_add_header(httpd, conn, "Content-Type", "image/png");
31   else if (strstr(uri, ".css"))
32     silc_http_server_add_header(httpd, conn, "Content-Type", "text/css");
33   else if (strstr(uri, ".htm"))
34     silc_http_server_add_header(httpd, conn, "Content-Type", "text/html");
35   else if (strstr(uri, ".php"))
36     silc_http_server_add_header(httpd, conn, "Content-Type", "text/html");
37 }
38
39 /* Serve pages */
40
41 static void http_callback_file(SilcHttpServer httpd, SilcHttpConnection conn,
42                                const char *uri, const char *method,
43                                SilcBuffer data, void *context)
44 {
45   SilcBufferStruct page;
46   SilcBuffer php;
47   char *filedata, filename[256];
48   SilcUInt32 data_len;
49   SilcBool usephp = FALSE;
50
51   if (!strcasecmp(method, "GET")) {
52     if (strstr(uri, ".php"))
53       usephp = TRUE;
54
55     if (!strcmp(uri, "/"))
56       snprintf(filename, sizeof(filename), "%s/index.html", htdocs);
57     else
58       snprintf(filename, sizeof(filename), "%s%s", htdocs, uri);
59
60     if (strchr(filename, '?'))
61       *strchr(filename, '?') = ' ';
62     while (strchr(filename, '&'))
63       *strchr(filename, '&') = ' ';
64
65     SILC_LOG_DEBUG(("Filename: '%s'", filename));
66
67     if (!usephp) {
68       filedata = silc_file_readfile(filename, &data_len, NULL);
69       if (!filedata) {
70         silc_http_server_send_error(httpd, conn, "404 Not Found",
71                                     "<body><h1>404 Not Found</h1><p>The page you are looking for cannot be located</body>");
72         return;
73       }
74
75       http_content_type(httpd, conn, uri);
76
77       /* Send page */
78       silc_buffer_set(&page, filedata, data_len);
79       silc_http_server_send(httpd, conn, &page);
80       silc_buffer_purge(&page);
81     } else {
82       php = silc_http_php_file(filename);
83       if (!php) {
84         silc_http_server_send_error(httpd, conn, "404 Not Found",
85                                     "<body><h1>404 Not Found</h1><p>The page you are looking for cannot be located</body>");
86         return;
87       }
88
89       http_content_type(httpd, conn, uri);
90
91       /* Send page */
92       silc_http_server_send(httpd, conn, php);
93       silc_buffer_free(php);
94     }
95
96     return;
97   }
98
99   silc_http_server_send_error(httpd, conn, "404 Not Found",
100                               "<body><h1>404 Not Found</h1><p>The page you are looking for cannot be located</body>");
101 }
102
103 int main(int argc, char **argv)
104 {
105   SilcBool success = FALSE;
106   SilcSchedule schedule;
107   SilcHttpServer httpd;
108
109   if (argc > 1) {
110     if (!strcmp(argv[1], "-d")) {
111       silc_log_debug(TRUE);
112       silc_log_debug_hexdump(TRUE);
113       silc_log_set_debug_string("*http*,*mime*");
114       if (argc > 2)
115         htdocs = argv[2];
116     } else {
117       htdocs = argv[1];
118     }
119   }
120
121   signal(SIGPIPE, SIG_IGN);
122
123   SILC_LOG_DEBUG(("Allocating scheduler"));
124   schedule = silc_schedule_init(0, NULL, NULL);
125   if (!schedule)
126     goto err;
127
128   SILC_LOG_DEBUG(("Allocating HTTP server at 127.0.0.1:5000"));
129   httpd = silc_http_server_alloc("127.0.0.1", 5000, schedule,
130                                  http_callback_file, NULL);
131   if (!httpd)
132     goto err;
133
134   silc_schedule(schedule);
135
136   silc_schedule_uninit(schedule);
137
138   success = TRUE;
139
140  err:
141   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
142   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
143
144   return success;
145 }