blob: ddc0f7e3a5be39116a4701750ff73f5c04e5c5b9 [file] [log] [blame]
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001/*
2 * Copyright (c) 2002, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
adamdunkels06f897e2004-06-06 05:59:20 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkelsca9ddcb2003-03-19 14:13:31 +000015 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * This file is part of the Contiki desktop environment for the C64.
31 *
adamdunkels823e8552005-03-01 06:03:25 +000032 * $Id: webserver.c,v 1.14 2005/03/01 06:03:25 adamdunkels Exp $
adamdunkelsca9ddcb2003-03-19 14:13:31 +000033 *
34 */
35
36
37#include "ctk.h"
adamdunkels46cd1432004-07-04 17:50:39 +000038#include "ek.h"
adamdunkelsca9ddcb2003-03-19 14:13:31 +000039#include "http-strings.h"
adamdunkelsca9ddcb2003-03-19 14:13:31 +000040
adamdunkels1cd53bd2003-04-08 19:24:55 +000041#include "loader.h"
42
adamdunkelsc7018072003-07-31 23:26:07 +000043#include "webserver.h"
adamdunkelsc7cc92a2003-05-28 05:21:49 +000044#include "httpd.h"
adamdunkelsca9ddcb2003-03-19 14:13:31 +000045
adamdunkels823e8552005-03-01 06:03:25 +000046#include <string.h>
adamdunkels8bb5cca2003-08-24 22:41:31 +000047#include <stdio.h>
48
adamdunkelsca9ddcb2003-03-19 14:13:31 +000049/* The main window. */
50static struct ctk_window mainwindow;
51
52static struct ctk_label message =
adamdunkelsc7018072003-07-31 23:26:07 +000053 {CTK_LABEL(0, 0, 15, 1, "Latest requests")};
adamdunkelsca9ddcb2003-03-19 14:13:31 +000054
adamdunkels46cd1432004-07-04 17:50:39 +000055/*static DISPATCHER_SIGHANDLER(webserver_sighandler, s, data);
adamdunkelsc7cc92a2003-05-28 05:21:49 +000056
adamdunkelsca9ddcb2003-03-19 14:13:31 +000057static struct dispatcher_proc p =
adamdunkelsc7018072003-07-31 23:26:07 +000058{DISPATCHER_PROC("Web server", NULL, webserver_sighandler,
59 httpd_appcall)};
adamdunkels46cd1432004-07-04 17:50:39 +000060static ek_id_t id;*/
61
62EK_EVENTHANDLER(webserver_eventhandler, ev, data);
63EK_PROCESS(p, "Web server", EK_PRIO_NORMAL,
64 webserver_eventhandler, NULL, NULL);
65static ek_id_t id = EK_ID_NONE;
adamdunkelsca9ddcb2003-03-19 14:13:31 +000066
adamdunkelsc7018072003-07-31 23:26:07 +000067
adamdunkelsadea4622004-09-12 07:20:04 +000068#define LOG_WIDTH 38
adamdunkels823e8552005-03-01 06:03:25 +000069#define LOG_HEIGHT 16
adamdunkelsc7018072003-07-31 23:26:07 +000070static char log[LOG_WIDTH*LOG_HEIGHT];
71
72static struct ctk_label loglabel =
73{CTK_LABEL(0, 1, LOG_WIDTH, LOG_HEIGHT, log)};
adamdunkelsca9ddcb2003-03-19 14:13:31 +000074/*-----------------------------------------------------------------------------------*/
adamdunkels8bb5cca2003-08-24 22:41:31 +000075LOADER_INIT_FUNC(webserver_init, arg)
adamdunkelsca9ddcb2003-03-19 14:13:31 +000076{
adamdunkels8bb5cca2003-08-24 22:41:31 +000077 arg_free(arg);
78
adamdunkelsca9ddcb2003-03-19 14:13:31 +000079 if(id == EK_ID_NONE) {
adamdunkels46cd1432004-07-04 17:50:39 +000080 id = ek_start(&p);
81 }
82}
83/*-----------------------------------------------------------------------------------*/
84EK_EVENTHANDLER(webserver_eventhandler, ev, data)
85{
86 EK_EVENTHANDLER_ARGS(ev, data);
adamdunkelsca9ddcb2003-03-19 14:13:31 +000087
adamdunkels46cd1432004-07-04 17:50:39 +000088 if(ev == EK_EVENT_INIT) {
adamdunkelsc7018072003-07-31 23:26:07 +000089 ctk_window_new(&mainwindow, LOG_WIDTH, LOG_HEIGHT+1, "Web server");
adamdunkelsca9ddcb2003-03-19 14:13:31 +000090
adamdunkelsc7018072003-07-31 23:26:07 +000091 CTK_WIDGET_ADD(&mainwindow, &message);
92 CTK_WIDGET_ADD(&mainwindow, &loglabel);
93
adamdunkelsc7cc92a2003-05-28 05:21:49 +000094 httpd_init();
adamdunkelsca9ddcb2003-03-19 14:13:31 +000095
adamdunkels46cd1432004-07-04 17:50:39 +000096 ctk_window_open(&mainwindow);
97 } else if(ev == ctk_signal_window_close ||
98 ev == EK_EVENT_REQUEST_EXIT) {
adamdunkelsc7018072003-07-31 23:26:07 +000099 ctk_window_close(&mainwindow);
adamdunkels46cd1432004-07-04 17:50:39 +0000100 ek_exit();
adamdunkelsc7018072003-07-31 23:26:07 +0000101 id = EK_ID_NONE;
102 LOADER_UNLOAD();
adamdunkelsadea4622004-09-12 07:20:04 +0000103 } else if(ev == tcpip_event) {
104 httpd_appcall(data);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000105 }
106}
107/*-----------------------------------------------------------------------------------*/
adamdunkelsc7018072003-07-31 23:26:07 +0000108void
adamdunkels823e8552005-03-01 06:03:25 +0000109httpd_log_file(u16_t *requester, char *file)
adamdunkelsc7018072003-07-31 23:26:07 +0000110{
111 int size;
112
113 /* Scroll previous entries upwards */
114 memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
115
116 /* Print out IP address of requesting host. */
117 size = sprintf(&log[LOG_WIDTH * (LOG_HEIGHT - 1)],
118 "%d.%d.%d.%d: ",
119 htons(requester[0]) >> 8,
120 htons(requester[0]) & 0xff,
121 htons(requester[1]) >> 8,
122 htons(requester[1]) & 0xff);
123
124 /* Copy filename into last line. */
125 strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1) + size], file, LOG_WIDTH - size);
126
127 /* Update log display. */
128 CTK_WIDGET_REDRAW(&loglabel);
129}
130/*-----------------------------------------------------------------------------------*/
adamdunkelsadea4622004-09-12 07:20:04 +0000131void
adamdunkels823e8552005-03-01 06:03:25 +0000132httpd_log(char *msg)
adamdunkelsadea4622004-09-12 07:20:04 +0000133{
134 /* Scroll previous entries upwards */
135 memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
136
137 /* Copy filename into last line. */
138 strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1)], msg, LOG_WIDTH);
139
140 /* Update log display. */
141 CTK_WIDGET_REDRAW(&loglabel);
142}
143/*-----------------------------------------------------------------------------------*/