1 /* SilcHttpServer tests */
4 #include "../silchttpserver.h"
6 static void http_callback(SilcHttpServer httpd, SilcHttpConnection conn,
7 const char *uri, const char *method,
8 SilcBuffer data, void *context)
10 SilcBufferStruct page;
12 SILC_LOG_DEBUG(("HTTP data received, URI:%s, method:%s", uri, method));
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,
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\">"
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);
41 if (!strcmp(uri, "/pr_1995.jpg")) {
43 unsigned char *data = silc_file_readfile("pr_1995.jpg", &data_len);
45 silc_http_server_send_error(httpd, conn, "404 Not Found", NULL);
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);
56 if (!strcasecmp(method, "POST")) {
57 if (strcmp(uri, "/posttest"))
59 memset(&page, 0, sizeof(page));
60 silc_buffer_strformat(&page,
61 "<html><head></head><body>",
63 silc_buffer_data(data),
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);
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>");
77 int main(int argc, char **argv)
79 SilcBool success = FALSE;
80 SilcSchedule schedule;
83 if (argc > 1 && !strcmp(argv[1], "-d")) {
85 silc_log_debug_hexdump(TRUE);
86 silc_log_set_debug_string("*http*,*mime*");
89 signal(SIGPIPE, SIG_IGN);
91 SILC_LOG_DEBUG(("Allocating scheduler"));
92 schedule = silc_schedule_init(0, NULL);
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,
102 silc_schedule(schedule);
104 silc_schedule_uninit(schedule);
109 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
110 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");