adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1 | /* |
| 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. |
adamdunkels | 06f897e | 2004-06-06 05:59:20 +0000 | [diff] [blame] | 14 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 15 | * 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 | * |
adamdunkels | 823e855 | 2005-03-01 06:03:25 +0000 | [diff] [blame] | 32 | * $Id: webserver.c,v 1.14 2005/03/01 06:03:25 adamdunkels Exp $ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 33 | * |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | #include "ctk.h" |
adamdunkels | 46cd143 | 2004-07-04 17:50:39 +0000 | [diff] [blame] | 38 | #include "ek.h" |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 39 | #include "http-strings.h" |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 40 | |
adamdunkels | 1cd53bd | 2003-04-08 19:24:55 +0000 | [diff] [blame] | 41 | #include "loader.h" |
| 42 | |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 43 | #include "webserver.h" |
adamdunkels | c7cc92a | 2003-05-28 05:21:49 +0000 | [diff] [blame] | 44 | #include "httpd.h" |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 45 | |
adamdunkels | 823e855 | 2005-03-01 06:03:25 +0000 | [diff] [blame] | 46 | #include <string.h> |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 47 | #include <stdio.h> |
| 48 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 49 | /* The main window. */ |
| 50 | static struct ctk_window mainwindow; |
| 51 | |
| 52 | static struct ctk_label message = |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 53 | {CTK_LABEL(0, 0, 15, 1, "Latest requests")}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 54 | |
adamdunkels | 46cd143 | 2004-07-04 17:50:39 +0000 | [diff] [blame] | 55 | /*static DISPATCHER_SIGHANDLER(webserver_sighandler, s, data); |
adamdunkels | c7cc92a | 2003-05-28 05:21:49 +0000 | [diff] [blame] | 56 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 57 | static struct dispatcher_proc p = |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 58 | {DISPATCHER_PROC("Web server", NULL, webserver_sighandler, |
| 59 | httpd_appcall)}; |
adamdunkels | 46cd143 | 2004-07-04 17:50:39 +0000 | [diff] [blame] | 60 | static ek_id_t id;*/ |
| 61 | |
| 62 | EK_EVENTHANDLER(webserver_eventhandler, ev, data); |
| 63 | EK_PROCESS(p, "Web server", EK_PRIO_NORMAL, |
| 64 | webserver_eventhandler, NULL, NULL); |
| 65 | static ek_id_t id = EK_ID_NONE; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 66 | |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 67 | |
adamdunkels | adea462 | 2004-09-12 07:20:04 +0000 | [diff] [blame] | 68 | #define LOG_WIDTH 38 |
adamdunkels | 823e855 | 2005-03-01 06:03:25 +0000 | [diff] [blame] | 69 | #define LOG_HEIGHT 16 |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 70 | static char log[LOG_WIDTH*LOG_HEIGHT]; |
| 71 | |
| 72 | static struct ctk_label loglabel = |
| 73 | {CTK_LABEL(0, 1, LOG_WIDTH, LOG_HEIGHT, log)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 74 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 75 | LOADER_INIT_FUNC(webserver_init, arg) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 76 | { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 77 | arg_free(arg); |
| 78 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 79 | if(id == EK_ID_NONE) { |
adamdunkels | 46cd143 | 2004-07-04 17:50:39 +0000 | [diff] [blame] | 80 | id = ek_start(&p); |
| 81 | } |
| 82 | } |
| 83 | /*-----------------------------------------------------------------------------------*/ |
| 84 | EK_EVENTHANDLER(webserver_eventhandler, ev, data) |
| 85 | { |
| 86 | EK_EVENTHANDLER_ARGS(ev, data); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 87 | |
adamdunkels | 46cd143 | 2004-07-04 17:50:39 +0000 | [diff] [blame] | 88 | if(ev == EK_EVENT_INIT) { |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 89 | ctk_window_new(&mainwindow, LOG_WIDTH, LOG_HEIGHT+1, "Web server"); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 90 | |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 91 | CTK_WIDGET_ADD(&mainwindow, &message); |
| 92 | CTK_WIDGET_ADD(&mainwindow, &loglabel); |
| 93 | |
adamdunkels | c7cc92a | 2003-05-28 05:21:49 +0000 | [diff] [blame] | 94 | httpd_init(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 95 | |
adamdunkels | 46cd143 | 2004-07-04 17:50:39 +0000 | [diff] [blame] | 96 | ctk_window_open(&mainwindow); |
| 97 | } else if(ev == ctk_signal_window_close || |
| 98 | ev == EK_EVENT_REQUEST_EXIT) { |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 99 | ctk_window_close(&mainwindow); |
adamdunkels | 46cd143 | 2004-07-04 17:50:39 +0000 | [diff] [blame] | 100 | ek_exit(); |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 101 | id = EK_ID_NONE; |
| 102 | LOADER_UNLOAD(); |
adamdunkels | adea462 | 2004-09-12 07:20:04 +0000 | [diff] [blame] | 103 | } else if(ev == tcpip_event) { |
| 104 | httpd_appcall(data); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 108 | void |
adamdunkels | 823e855 | 2005-03-01 06:03:25 +0000 | [diff] [blame] | 109 | httpd_log_file(u16_t *requester, char *file) |
adamdunkels | c701807 | 2003-07-31 23:26:07 +0000 | [diff] [blame] | 110 | { |
| 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 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | adea462 | 2004-09-12 07:20:04 +0000 | [diff] [blame] | 131 | void |
adamdunkels | 823e855 | 2005-03-01 06:03:25 +0000 | [diff] [blame] | 132 | httpd_log(char *msg) |
adamdunkels | adea462 | 2004-09-12 07:20:04 +0000 | [diff] [blame] | 133 | { |
| 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 | /*-----------------------------------------------------------------------------------*/ |