html version for mbed os V5.2
Dependents: scooter_mbed_correction_mbed_os
Fork of html by
html.cpp@2:468edcd380ae, 2016-11-26 (annotated)
- Committer:
- jlsalvat
- Date:
- Sat Nov 26 16:05:42 2016 +0000
- Branch:
- html_mbed_os
- Revision:
- 2:468edcd380ae
- Parent:
- 1:ba608856b208
change html for mbed_os 5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
superphil06 | 0:539b0fc9d536 | 1 | /**************************************************************************** |
superphil06 | 0:539b0fc9d536 | 2 | * |
superphil06 | 0:539b0fc9d536 | 3 | * (C) 2000 by BECK IPC GmbH |
superphil06 | 0:539b0fc9d536 | 4 | * |
superphil06 | 0:539b0fc9d536 | 5 | * BECK IPC GmbH |
superphil06 | 0:539b0fc9d536 | 6 | * Garbenheimerstr. 38 |
superphil06 | 0:539b0fc9d536 | 7 | * D-35578 Wetzlar |
superphil06 | 0:539b0fc9d536 | 8 | * |
superphil06 | 0:539b0fc9d536 | 9 | * Phone : (49)-6441-905-240 |
superphil06 | 0:539b0fc9d536 | 10 | * Fax : (49)-6441-905-245 |
superphil06 | 0:539b0fc9d536 | 11 | * |
superphil06 | 0:539b0fc9d536 | 12 | * --------------------------------------------------------------------------- |
superphil06 | 0:539b0fc9d536 | 13 | * Module : HTML.C |
superphil06 | 0:539b0fc9d536 | 14 | * Function : contains functions to operate between HTML files and SC12 |
superphil06 | 0:539b0fc9d536 | 15 | * Cgi Interface. It read Html files, copy it into memory |
superphil06 | 0:539b0fc9d536 | 16 | * block and save positions of '^VARDEF[x]' entry in var_field |
superphil06 | 0:539b0fc9d536 | 17 | * struct to insert dynamtic code int the static file. |
superphil06 | 0:539b0fc9d536 | 18 | |
superphil06 | 0:539b0fc9d536 | 19 | By Example: |
superphil06 | 0:539b0fc9d536 | 20 | On the flash exists the following htm file (a:\exp.htm): |
superphil06 | 0:539b0fc9d536 | 21 | _______________________________________________________ |
superphil06 | 0:539b0fc9d536 | 22 | |
superphil06 | 0:539b0fc9d536 | 23 | <HTML><HEAD><TITLE>Example Page</TITLE></HEAD> |
superphil06 | 0:539b0fc9d536 | 24 | <BODY> |
superphil06 | 0:539b0fc9d536 | 25 | The first Dynamic code/text ^VARDEF[0] = 32 <BR> |
superphil06 | 0:539b0fc9d536 | 26 | And now the Dynamic code/text ^VARDEF[1] = 3 <BR> |
superphil06 | 0:539b0fc9d536 | 27 | </BODY> |
superphil06 | 0:539b0fc9d536 | 28 | </HTML> |
superphil06 | 0:539b0fc9d536 | 29 | _______________________________________________________ |
superphil06 | 0:539b0fc9d536 | 30 | |
superphil06 | 0:539b0fc9d536 | 31 | now you have to call the function in the following way: |
superphil06 | 0:539b0fc9d536 | 32 | |
superphil06 | 0:539b0fc9d536 | 33 | |
superphil06 | 0:539b0fc9d536 | 34 | |
superphil06 | 0:539b0fc9d536 | 35 | |
superphil06 | 0:539b0fc9d536 | 36 | |
superphil06 | 0:539b0fc9d536 | 37 | |
superphil06 | 0:539b0fc9d536 | 38 | #include "html.h" |
superphil06 | 0:539b0fc9d536 | 39 | |
superphil06 | 0:539b0fc9d536 | 40 | vard_field_t exp_vardef[2]; // size "2": 2 var defs in htm file |
superphil06 | 0:539b0fc9d536 | 41 | char exp_page[1024]; // html code buffer (1024 bytes) |
superphil06 | 0:539b0fc9d536 | 42 | |
superphil06 | 0:539b0fc9d536 | 43 | .... |
superphil06 | 0:539b0fc9d536 | 44 | .... |
superphil06 | 0:539b0fc9d536 | 45 | .. |
superphil06 | 0:539b0fc9d536 | 46 | |
superphil06 | 0:539b0fc9d536 | 47 | int main(void) |
superphil06 | 0:539b0fc9d536 | 48 | { |
superphil06 | 0:539b0fc9d536 | 49 | char tmpBuffer[1024]; |
superphil06 | 0:539b0fc9d536 | 50 | ... |
superphil06 | 0:539b0fc9d536 | 51 | |
superphil06 | 0:539b0fc9d536 | 52 | |
superphil06 | 0:539b0fc9d536 | 53 | |
superphil06 | 0:539b0fc9d536 | 54 | |
superphil06 | 0:539b0fc9d536 | 55 | exp_page = Gen_HtmlCode_From_File( "a:\exp.htm", &exp_vardef, 2 ); |
superphil06 | 0:539b0fc9d536 | 56 | |
superphil06 | 0:539b0fc9d536 | 57 | // know, if exp != Null the page is in the memory block. |
superphil06 | 0:539b0fc9d536 | 58 | // the Vardef field are filled with blank spaces (ASCII 32). |
superphil06 | 0:539b0fc9d536 | 59 | // The exp_vardef struct points to this vardefs fields. |
superphil06 | 0:539b0fc9d536 | 60 | // So you can access to this empty fields in the |
superphil06 | 0:539b0fc9d536 | 61 | // following way (normaly the filling of the Vardefs will |
superphil06 | 0:539b0fc9d536 | 62 | // be done in the CGI function): |
superphil06 | 0:539b0fc9d536 | 63 | |
superphil06 | 0:539b0fc9d536 | 64 | // 1. text: |
superphil06 | 0:539b0fc9d536 | 65 | strcpy( tmpBuffer, "I am the First text" ); |
superphil06 | 0:539b0fc9d536 | 66 | FILL_STR( tmpBuffer, exp_vardef[0].length ); // this will |
superphil06 | 0:539b0fc9d536 | 67 | // to fill the not used characters of |
superphil06 | 0:539b0fc9d536 | 68 | // the string with blank spaces |
superphil06 | 0:539b0fc9d536 | 69 | memcpy( exp_vardef[0].ptr, tmpBufferm, exp_vardef[0].length ); |
superphil06 | 0:539b0fc9d536 | 70 | |
superphil06 | 0:539b0fc9d536 | 71 | |
superphil06 | 0:539b0fc9d536 | 72 | // 2. text: |
superphil06 | 0:539b0fc9d536 | 73 | strcpy( tmpBuffer, "I am the First text" ); |
superphil06 | 0:539b0fc9d536 | 74 | FILL_STR( tmpBuffer, exp_vardef[0].length ); // this will |
superphil06 | 0:539b0fc9d536 | 75 | // to fill the not used characters of |
superphil06 | 0:539b0fc9d536 | 76 | // the string with blank spaces |
superphil06 | 0:539b0fc9d536 | 77 | memcpy( exp_vardef[0].ptr, tmpBufferm, exp_vardef[0].length ); |
superphil06 | 0:539b0fc9d536 | 78 | |
superphil06 | 0:539b0fc9d536 | 79 | ..... |
superphil06 | 0:539b0fc9d536 | 80 | .. |
superphil06 | 0:539b0fc9d536 | 81 | return; |
superphil06 | 0:539b0fc9d536 | 82 | |
superphil06 | 0:539b0fc9d536 | 83 | } |
superphil06 | 0:539b0fc9d536 | 84 | |
superphil06 | 0:539b0fc9d536 | 85 | * |
superphil06 | 0:539b0fc9d536 | 86 | * Author : Stoidner |
superphil06 | 0:539b0fc9d536 | 87 | * Date : 30.08.00 |
superphil06 | 0:539b0fc9d536 | 88 | * Version : V1.00 |
superphil06 | 0:539b0fc9d536 | 89 | * --------------------------------------------------------------------------- |
superphil06 | 0:539b0fc9d536 | 90 | * History : |
superphil06 | 0:539b0fc9d536 | 91 | * |
superphil06 | 0:539b0fc9d536 | 92 | * Vx.yy Author Changes |
superphil06 | 0:539b0fc9d536 | 93 | * |
superphil06 | 0:539b0fc9d536 | 94 | * V1.00 cs Create |
superphil06 | 0:539b0fc9d536 | 95 | **************************************************************/ |
superphil06 | 0:539b0fc9d536 | 96 | |
superphil06 | 0:539b0fc9d536 | 97 | |
superphil06 | 0:539b0fc9d536 | 98 | //******************* |
superphil06 | 0:539b0fc9d536 | 99 | //* standard includes |
superphil06 | 0:539b0fc9d536 | 100 | |
superphil06 | 0:539b0fc9d536 | 101 | #include "mbed.h" |
superphil06 | 0:539b0fc9d536 | 102 | #include <stdio.h> |
superphil06 | 0:539b0fc9d536 | 103 | #include <stdlib.h> |
superphil06 | 0:539b0fc9d536 | 104 | #include <string.h> |
superphil06 | 1:ba608856b208 | 105 | #include <stdlib.h> |
superphil06 | 1:ba608856b208 | 106 | #include <string.h> |
jlsalvat | 2:468edcd380ae | 107 | #include "TCPSocket.h" |
jlsalvat | 2:468edcd380ae | 108 | #include "TCPServer.h" |
jlsalvat | 2:468edcd380ae | 109 | #include "EthernetInterface.h" |
superphil06 | 0:539b0fc9d536 | 110 | |
superphil06 | 0:539b0fc9d536 | 111 | //******************** |
superphil06 | 0:539b0fc9d536 | 112 | //* specified includes |
superphil06 | 0:539b0fc9d536 | 113 | #include "html.h" |
superphil06 | 1:ba608856b208 | 114 | |
superphil06 | 0:539b0fc9d536 | 115 | |
superphil06 | 1:ba608856b208 | 116 | #define _HTML_DEBUG |
superphil06 | 1:ba608856b208 | 117 | #define _IP_DEBUG |
superphil06 | 1:ba608856b208 | 118 | #define _TCP_DEBUG |
superphil06 | 1:ba608856b208 | 119 | #define _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 120 | # define _HTML_DEBUG |
superphil06 | 1:ba608856b208 | 121 | |
superphil06 | 1:ba608856b208 | 122 | extern var_field_t tab_balise[10]; //une balise est présente dans le squelette |
superphil06 | 1:ba608856b208 | 123 | |
superphil06 | 1:ba608856b208 | 124 | |
superphil06 | 1:ba608856b208 | 125 | EthernetInterface eth; // ethernet layer management object |
jlsalvat | 2:468edcd380ae | 126 | TCPServer svr; // |
superphil06 | 1:ba608856b208 | 127 | bool serverIsListened = false;// flag to indicate server is listening |
superphil06 | 1:ba608856b208 | 128 | #define PORT 80 // tcp port for socket listeningt |
jlsalvat | 2:468edcd380ae | 129 | TCPSocket clt_sock;// socket to identify clt_sock side |
jlsalvat | 2:468edcd380ae | 130 | SocketAddress clt_addr; //address clt_sock side |
jlsalvat | 2:468edcd380ae | 131 | bool clt_sockIsConnected = false;// flag that indicate if a web clt_sock is avalaible |
superphil06 | 1:ba608856b208 | 132 | |
superphil06 | 1:ba608856b208 | 133 | |
superphil06 | 1:ba608856b208 | 134 | char *html_page; // pointer to memory area of Html Page |
superphil06 | 1:ba608856b208 | 135 | /*************** locals prototypes ***************/ |
superphil06 | 0:539b0fc9d536 | 136 | |
superphil06 | 0:539b0fc9d536 | 137 | |
superphil06 | 0:539b0fc9d536 | 138 | |
superphil06 | 0:539b0fc9d536 | 139 | |
superphil06 | 0:539b0fc9d536 | 140 | |
superphil06 | 0:539b0fc9d536 | 141 | |
superphil06 | 1:ba608856b208 | 142 | //char * Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ) |
superphil06 | 1:ba608856b208 | 143 | char * Gen_HtmlCode_From_File( char *Path, var_field_t *var_field, int max_vardef ) |
superphil06 | 0:539b0fc9d536 | 144 | {LocalFileSystem local("local"); |
superphil06 | 0:539b0fc9d536 | 145 | FILE *in; // File Handle |
superphil06 | 0:539b0fc9d536 | 146 | //fpos_t filepos; // file position counter |
superphil06 | 0:539b0fc9d536 | 147 | long filepos; // file position counter |
superphil06 | 0:539b0fc9d536 | 148 | int data; // read data from file |
superphil06 | 0:539b0fc9d536 | 149 | char value_str[40]; // to search for '^VARDEF[x]' entrys |
superphil06 | 0:539b0fc9d536 | 150 | int i=0; // VarDef Counter ( '^VARDEF[i]' ) |
superphil06 | 0:539b0fc9d536 | 151 | long mem_pos = -1; // actually mem offset |
superphil06 | 1:ba608856b208 | 152 | //char *html_page; // pointer to memory area of Html Page |
superphil06 | 0:539b0fc9d536 | 153 | |
superphil06 | 0:539b0fc9d536 | 154 | if ((in = fopen(Path, "rt")) == NULL) |
superphil06 | 0:539b0fc9d536 | 155 | { |
superphil06 | 0:539b0fc9d536 | 156 | printf("\r\nError Open File: -%s-", Path); |
superphil06 | 0:539b0fc9d536 | 157 | return(NULL); |
superphil06 | 0:539b0fc9d536 | 158 | } |
jlsalvat | 2:468edcd380ae | 159 | else |
superphil06 | 0:539b0fc9d536 | 160 | |
superphil06 | 1:ba608856b208 | 161 | #ifdef _HTML_DEBUG |
superphil06 | 0:539b0fc9d536 | 162 | printf("\r\nOpen File.. \"%s\"", Path); |
superphil06 | 0:539b0fc9d536 | 163 | #endif |
superphil06 | 0:539b0fc9d536 | 164 | |
superphil06 | 0:539b0fc9d536 | 165 | // checking Html Page size and filling var_field struct |
superphil06 | 0:539b0fc9d536 | 166 | while (!feof(in)) |
superphil06 | 0:539b0fc9d536 | 167 | { |
superphil06 | 0:539b0fc9d536 | 168 | // serach for Commands |
superphil06 | 0:539b0fc9d536 | 169 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 170 | |
superphil06 | 0:539b0fc9d536 | 171 | // check for unexpected page size |
superphil06 | 0:539b0fc9d536 | 172 | if (mem_pos >= 0xFFFF) |
superphil06 | 0:539b0fc9d536 | 173 | { |
superphil06 | 0:539b0fc9d536 | 174 | fclose(in); |
superphil06 | 0:539b0fc9d536 | 175 | return(NULL); |
superphil06 | 0:539b0fc9d536 | 176 | } |
superphil06 | 0:539b0fc9d536 | 177 | |
superphil06 | 0:539b0fc9d536 | 178 | if ( fgetc(in) == CMD_HTML_SIGN ) |
superphil06 | 0:539b0fc9d536 | 179 | { |
superphil06 | 0:539b0fc9d536 | 180 | //fgetpos(in, &filepos); |
superphil06 | 0:539b0fc9d536 | 181 | filepos= ftell ( in );// replace fgetpos call |
superphil06 | 0:539b0fc9d536 | 182 | |
superphil06 | 0:539b0fc9d536 | 183 | |
superphil06 | 0:539b0fc9d536 | 184 | if ( fscanf(in , "VARDEF[ %d ] = %s", &i, &value_str ) ) |
superphil06 | 0:539b0fc9d536 | 185 | { |
superphil06 | 1:ba608856b208 | 186 | #ifdef _HTML_DEBUG |
superphil06 | 0:539b0fc9d536 | 187 | printf("\r\nFound and save entry.."); |
superphil06 | 0:539b0fc9d536 | 188 | #endif |
superphil06 | 0:539b0fc9d536 | 189 | mem_pos += atoi(value_str)-1; |
superphil06 | 0:539b0fc9d536 | 190 | } |
superphil06 | 0:539b0fc9d536 | 191 | else fseek( in, filepos, SEEK_SET ); |
superphil06 | 0:539b0fc9d536 | 192 | } |
superphil06 | 0:539b0fc9d536 | 193 | |
superphil06 | 0:539b0fc9d536 | 194 | } // while (!feof |
superphil06 | 0:539b0fc9d536 | 195 | |
superphil06 | 0:539b0fc9d536 | 196 | // allocate memory |
superphil06 | 0:539b0fc9d536 | 197 | html_page = (char *) malloc( (int) mem_pos+1 ); |
superphil06 | 0:539b0fc9d536 | 198 | |
superphil06 | 0:539b0fc9d536 | 199 | // check if alloc was successful |
superphil06 | 0:539b0fc9d536 | 200 | if (html_page == NULL) return(NULL); |
superphil06 | 0:539b0fc9d536 | 201 | |
superphil06 | 0:539b0fc9d536 | 202 | // jump to beginning of html file |
superphil06 | 0:539b0fc9d536 | 203 | fseek( in, 0, SEEK_SET); |
superphil06 | 0:539b0fc9d536 | 204 | |
superphil06 | 0:539b0fc9d536 | 205 | // now loading website into memory |
superphil06 | 0:539b0fc9d536 | 206 | mem_pos = 0; |
superphil06 | 1:ba608856b208 | 207 | #ifdef _HTML_DEBUG |
superphil06 | 0:539b0fc9d536 | 208 | printf("\r\nReading Page"); |
superphil06 | 0:539b0fc9d536 | 209 | #endif |
superphil06 | 0:539b0fc9d536 | 210 | while (!feof(in)) |
superphil06 | 0:539b0fc9d536 | 211 | { |
superphil06 | 0:539b0fc9d536 | 212 | data = fgetc(in); |
superphil06 | 0:539b0fc9d536 | 213 | |
superphil06 | 0:539b0fc9d536 | 214 | // serach for Commands |
superphil06 | 0:539b0fc9d536 | 215 | if ( data == CMD_HTML_SIGN ) |
superphil06 | 0:539b0fc9d536 | 216 | { |
superphil06 | 0:539b0fc9d536 | 217 | // fgetpos(in, &filepos); |
superphil06 | 0:539b0fc9d536 | 218 | filepos =ftell ( in );// replace fgetpos call |
superphil06 | 0:539b0fc9d536 | 219 | |
superphil06 | 0:539b0fc9d536 | 220 | if ( fscanf(in , "VARDEF[ %d ] = %s", &i, &value_str ) ) |
superphil06 | 0:539b0fc9d536 | 221 | { |
superphil06 | 0:539b0fc9d536 | 222 | if (var_field) |
superphil06 | 0:539b0fc9d536 | 223 | { |
superphil06 | 0:539b0fc9d536 | 224 | // save VarDef |
superphil06 | 0:539b0fc9d536 | 225 | |
superphil06 | 0:539b0fc9d536 | 226 | // check if vardef index is valid |
superphil06 | 0:539b0fc9d536 | 227 | if ( i>max_vardef ) { free(html_page); return(NULL); }; |
superphil06 | 0:539b0fc9d536 | 228 | //var_field[i].length = atoi(value_str); |
superphil06 | 0:539b0fc9d536 | 229 | var_field[i].length=strlen(value_str); |
superphil06 | 0:539b0fc9d536 | 230 | var_field[i].ptr = &html_page[(int)mem_pos]; |
superphil06 | 0:539b0fc9d536 | 231 | } |
superphil06 | 0:539b0fc9d536 | 232 | |
superphil06 | 0:539b0fc9d536 | 233 | // insert free characters |
superphil06 | 0:539b0fc9d536 | 234 | // for (i=0; i<=atoi(value_str)-1; i++) |
superphil06 | 0:539b0fc9d536 | 235 | for(i=0;i<=strlen(value_str);i++) |
superphil06 | 0:539b0fc9d536 | 236 | { |
superphil06 | 0:539b0fc9d536 | 237 | html_page[(int)mem_pos] = (char) 32; |
superphil06 | 0:539b0fc9d536 | 238 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 239 | } |
superphil06 | 0:539b0fc9d536 | 240 | |
superphil06 | 0:539b0fc9d536 | 241 | } |
superphil06 | 0:539b0fc9d536 | 242 | else |
superphil06 | 0:539b0fc9d536 | 243 | { |
superphil06 | 0:539b0fc9d536 | 244 | // set data pointer back |
superphil06 | 0:539b0fc9d536 | 245 | fseek( in, (long) filepos, SEEK_SET); |
superphil06 | 0:539b0fc9d536 | 246 | |
superphil06 | 0:539b0fc9d536 | 247 | // copy data into memory |
superphil06 | 0:539b0fc9d536 | 248 | html_page[(int)mem_pos] = (char)data; |
superphil06 | 0:539b0fc9d536 | 249 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 250 | } |
superphil06 | 0:539b0fc9d536 | 251 | |
superphil06 | 0:539b0fc9d536 | 252 | } |
superphil06 | 0:539b0fc9d536 | 253 | else |
superphil06 | 0:539b0fc9d536 | 254 | { |
superphil06 | 0:539b0fc9d536 | 255 | // copy data into memory |
superphil06 | 0:539b0fc9d536 | 256 | html_page[(int)mem_pos] = (char)data; |
superphil06 | 0:539b0fc9d536 | 257 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 258 | } |
superphil06 | 0:539b0fc9d536 | 259 | |
superphil06 | 0:539b0fc9d536 | 260 | } // while (!feof |
superphil06 | 0:539b0fc9d536 | 261 | |
superphil06 | 0:539b0fc9d536 | 262 | // terminate string |
superphil06 | 0:539b0fc9d536 | 263 | html_page[(int)mem_pos-1] = (char)'\0'; |
superphil06 | 0:539b0fc9d536 | 264 | |
superphil06 | 0:539b0fc9d536 | 265 | |
superphil06 | 0:539b0fc9d536 | 266 | |
superphil06 | 0:539b0fc9d536 | 267 | // close HTML Page |
superphil06 | 0:539b0fc9d536 | 268 | fclose(in); |
superphil06 | 0:539b0fc9d536 | 269 | |
superphil06 | 1:ba608856b208 | 270 | #ifdef _HTML_DEBUG |
superphil06 | 0:539b0fc9d536 | 271 | printf("\r\nDone."); |
superphil06 | 0:539b0fc9d536 | 272 | #endif |
superphil06 | 0:539b0fc9d536 | 273 | |
superphil06 | 0:539b0fc9d536 | 274 | |
superphil06 | 0:539b0fc9d536 | 275 | return(html_page); |
superphil06 | 0:539b0fc9d536 | 276 | } |
superphil06 | 0:539b0fc9d536 | 277 | //simple function thatr load file from disk and return pointer |
superphil06 | 0:539b0fc9d536 | 278 | |
superphil06 | 0:539b0fc9d536 | 279 | char *load_HtmlCode_From_File( char *Path,long * size) |
superphil06 | 0:539b0fc9d536 | 280 | {LocalFileSystem local("local"); |
superphil06 | 0:539b0fc9d536 | 281 | FILE *in; // File Handle |
superphil06 | 0:539b0fc9d536 | 282 | int data; // read data from file |
superphil06 | 0:539b0fc9d536 | 283 | long mem_pos = -1; // actually mem offset |
superphil06 | 0:539b0fc9d536 | 284 | char *html_page; // pointer to memory area of Html Page |
superphil06 | 0:539b0fc9d536 | 285 | if ((in = fopen(Path, "rt")) == NULL) |
superphil06 | 0:539b0fc9d536 | 286 | { printf("\r\nError Open File: -%s-", Path); //open file from disk |
superphil06 | 0:539b0fc9d536 | 287 | return(NULL); |
superphil06 | 0:539b0fc9d536 | 288 | } |
superphil06 | 0:539b0fc9d536 | 289 | |
superphil06 | 0:539b0fc9d536 | 290 | while (!feof(in)) // checking Html Page size |
superphil06 | 0:539b0fc9d536 | 291 | { |
superphil06 | 0:539b0fc9d536 | 292 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 293 | if ( (fgetc(in) == EOF)||(mem_pos==0xFFFF) ) |
superphil06 | 0:539b0fc9d536 | 294 | {break;} |
superphil06 | 0:539b0fc9d536 | 295 | }// while (!feof ) |
superphil06 | 0:539b0fc9d536 | 296 | html_page = (char *) malloc( (int) mem_pos+1 );// allocate memory |
superphil06 | 0:539b0fc9d536 | 297 | |
superphil06 | 0:539b0fc9d536 | 298 | // check if alloc was successful |
superphil06 | 0:539b0fc9d536 | 299 | if (html_page == NULL) return(NULL); |
superphil06 | 0:539b0fc9d536 | 300 | fseek( in, 0, SEEK_SET);// jump to beginning of html file |
superphil06 | 0:539b0fc9d536 | 301 | |
superphil06 | 0:539b0fc9d536 | 302 | mem_pos = 0; // now loading website into memory |
superphil06 | 0:539b0fc9d536 | 303 | while (!feof(in)) |
superphil06 | 0:539b0fc9d536 | 304 | { data = fgetc(in); |
superphil06 | 0:539b0fc9d536 | 305 | html_page[(int)mem_pos] = (char)data; |
superphil06 | 0:539b0fc9d536 | 306 | mem_pos++; |
superphil06 | 0:539b0fc9d536 | 307 | } // while (!feof |
superphil06 | 0:539b0fc9d536 | 308 | |
superphil06 | 0:539b0fc9d536 | 309 | html_page[(int)mem_pos-1] = (char)'\0'; // terminate string |
superphil06 | 0:539b0fc9d536 | 310 | *size=mem_pos-1; //return size of file |
superphil06 | 0:539b0fc9d536 | 311 | fclose(in); // close HTML Page |
superphil06 | 0:539b0fc9d536 | 312 | return(html_page); |
superphil06 | 0:539b0fc9d536 | 313 | } |
superphil06 | 0:539b0fc9d536 | 314 | |
superphil06 | 0:539b0fc9d536 | 315 | void Html_Patch ( var_field_t *pTab_Balise,int index, char * pChaine ){ |
superphil06 | 0:539b0fc9d536 | 316 | |
superphil06 | 0:539b0fc9d536 | 317 | FILL_STR( pChaine,pTab_Balise[index].length ); |
superphil06 | 0:539b0fc9d536 | 318 | memcpy( pTab_Balise[index].ptr, pChaine, pTab_Balise[index].length ); |
superphil06 | 0:539b0fc9d536 | 319 | } |
superphil06 | 1:ba608856b208 | 320 | |
superphil06 | 1:ba608856b208 | 321 | void (*pfPtr) (void); |
superphil06 | 1:ba608856b208 | 322 | |
superphil06 | 1:ba608856b208 | 323 | |
superphil06 | 1:ba608856b208 | 324 | |
superphil06 | 1:ba608856b208 | 325 | int Init_Web_Server(void (*fPtr)(void) )// fptr pointer to a void interrupt CGI function |
superphil06 | 1:ba608856b208 | 326 | {pfPtr=fPtr;// affectation du pointeur public |
superphil06 | 1:ba608856b208 | 327 | |
superphil06 | 1:ba608856b208 | 328 | //setup ethernet interface |
jlsalvat | 2:468edcd380ae | 329 | // eth.init(); //Use DHCP |
superphil06 | 1:ba608856b208 | 330 | eth.connect(); |
superphil06 | 1:ba608856b208 | 331 | #ifdef _IP_DEBUG |
jlsalvat | 2:468edcd380ae | 332 | printf("IP Address is %s\n\r", eth.get_ip_address());// display server ip address |
superphil06 | 1:ba608856b208 | 333 | #endif |
jlsalvat | 2:468edcd380ae | 334 | /* Open the server on ethernet stack */ |
jlsalvat | 2:468edcd380ae | 335 | svr.open(ð); |
jlsalvat | 2:468edcd380ae | 336 | |
jlsalvat | 2:468edcd380ae | 337 | /* Bind the HTTP port (TCP 80) to the server */ |
jlsalvat | 2:468edcd380ae | 338 | if(svr.bind(PORT)< 0) { |
superphil06 | 1:ba608856b208 | 339 | #ifdef _IP_DEBUG |
superphil06 | 1:ba608856b208 | 340 | printf("tcp server bind failed.\n\r"); |
superphil06 | 1:ba608856b208 | 341 | #endif |
superphil06 | 1:ba608856b208 | 342 | return -1; |
superphil06 | 1:ba608856b208 | 343 | } else { |
superphil06 | 1:ba608856b208 | 344 | #ifdef _IP_DEBUG |
superphil06 | 1:ba608856b208 | 345 | printf("tcp server bind successed.\n\r"); |
superphil06 | 1:ba608856b208 | 346 | #endif |
superphil06 | 1:ba608856b208 | 347 | serverIsListened = true; |
superphil06 | 1:ba608856b208 | 348 | } |
jlsalvat | 2:468edcd380ae | 349 | //accept only 1 connection |
superphil06 | 1:ba608856b208 | 350 | if(svr.listen(1) < 0) { |
superphil06 | 1:ba608856b208 | 351 | #ifdef _IP_DEBUG |
superphil06 | 1:ba608856b208 | 352 | printf("tcp server listen failed.\n\r"); |
superphil06 | 1:ba608856b208 | 353 | #endif |
superphil06 | 1:ba608856b208 | 354 | return -1; |
superphil06 | 1:ba608856b208 | 355 | } else { |
superphil06 | 1:ba608856b208 | 356 | #ifdef _IP_DEBUG |
superphil06 | 1:ba608856b208 | 357 | printf("tcp server is listening...\n\r"); |
superphil06 | 1:ba608856b208 | 358 | #endif |
superphil06 | 1:ba608856b208 | 359 | |
superphil06 | 1:ba608856b208 | 360 | return 0; |
superphil06 | 1:ba608856b208 | 361 | } |
superphil06 | 1:ba608856b208 | 362 | |
superphil06 | 1:ba608856b208 | 363 | } |
superphil06 | 1:ba608856b208 | 364 | |
superphil06 | 1:ba608856b208 | 365 | |
superphil06 | 1:ba608856b208 | 366 | //************* used to close srv socket and web server thread *********************** |
superphil06 | 1:ba608856b208 | 367 | int DeInit_Web_Server(void) |
superphil06 | 1:ba608856b208 | 368 | {int iError; |
superphil06 | 1:ba608856b208 | 369 | serverIsListened=false;// |
jlsalvat | 2:468edcd380ae | 370 | iError=svr.close ();// close main server socket |
superphil06 | 1:ba608856b208 | 371 | return iError; |
superphil06 | 1:ba608856b208 | 372 | |
superphil06 | 1:ba608856b208 | 373 | } |
superphil06 | 1:ba608856b208 | 374 | |
superphil06 | 1:ba608856b208 | 375 | |
superphil06 | 1:ba608856b208 | 376 | |
superphil06 | 1:ba608856b208 | 377 | |
superphil06 | 1:ba608856b208 | 378 | |
superphil06 | 1:ba608856b208 | 379 | |
superphil06 | 1:ba608856b208 | 380 | |
superphil06 | 1:ba608856b208 | 381 | /***************** thread section **************/ |
superphil06 | 1:ba608856b208 | 382 | |
superphil06 | 1:ba608856b208 | 383 | |
superphil06 | 1:ba608856b208 | 384 | void Web_Server_Thread(void const *args) |
superphil06 | 1:ba608856b208 | 385 | { |
superphil06 | 1:ba608856b208 | 386 | |
superphil06 | 1:ba608856b208 | 387 | printf("web server thread start.. \n\r"); |
superphil06 | 1:ba608856b208 | 388 | |
superphil06 | 1:ba608856b208 | 389 | while ( serverIsListened) { |
superphil06 | 1:ba608856b208 | 390 | //blocking mode(never timeout) |
jlsalvat | 2:468edcd380ae | 391 | if(svr.accept(&clt_sock, &clt_addr)<0) { |
superphil06 | 1:ba608856b208 | 392 | #ifdef _TCP_DEBUG |
superphil06 | 1:ba608856b208 | 393 | printf("failed to accept connection.\n\r"); |
superphil06 | 1:ba608856b208 | 394 | #endif |
superphil06 | 1:ba608856b208 | 395 | } else { |
superphil06 | 1:ba608856b208 | 396 | #ifdef _TCP_DEBUG |
jlsalvat | 2:468edcd380ae | 397 | printf("accept, connection success! IP:%s PORT:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
superphil06 | 1:ba608856b208 | 398 | #endif |
jlsalvat | 2:468edcd380ae | 399 | clt_sockIsConnected = true; |
superphil06 | 1:ba608856b208 | 400 | // Setup the new socket events |
jlsalvat | 2:468edcd380ae | 401 | // clt_sock.setOnEvent(&onConnectedTCPSocketEvent); |
superphil06 | 1:ba608856b208 | 402 | // We can find out from where the connection is coming by looking at the |
superphil06 | 1:ba608856b208 | 403 | // Host parameter of the accept() method |
superphil06 | 1:ba608856b208 | 404 | |
jlsalvat | 2:468edcd380ae | 405 | while(clt_sockIsConnected) { |
superphil06 | 1:ba608856b208 | 406 | char buffer[1024] = {}; |
superphil06 | 1:ba608856b208 | 407 | |
jlsalvat | 2:468edcd380ae | 408 | switch(clt_sock.recv(buffer, 1023)) { |
superphil06 | 1:ba608856b208 | 409 | case 0: |
superphil06 | 1:ba608856b208 | 410 | #ifdef _TCP_DEBUG |
superphil06 | 1:ba608856b208 | 411 | printf("recieved buffer is empty.\n\r"); |
superphil06 | 1:ba608856b208 | 412 | #endif |
jlsalvat | 2:468edcd380ae | 413 | clt_sockIsConnected = false; |
superphil06 | 1:ba608856b208 | 414 | break; |
superphil06 | 1:ba608856b208 | 415 | case -1: |
superphil06 | 1:ba608856b208 | 416 | #ifdef _TCP_DEBUG |
jlsalvat | 2:468edcd380ae | 417 | printf("failed to read data from clt_sock.\n\r"); |
superphil06 | 1:ba608856b208 | 418 | #endif |
jlsalvat | 2:468edcd380ae | 419 | clt_sockIsConnected = false; |
superphil06 | 1:ba608856b208 | 420 | break; |
superphil06 | 1:ba608856b208 | 421 | default: |
superphil06 | 1:ba608856b208 | 422 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 423 | printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); |
superphil06 | 1:ba608856b208 | 424 | #endif |
superphil06 | 1:ba608856b208 | 425 | if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) { |
superphil06 | 1:ba608856b208 | 426 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 427 | printf("GET request incomming.\n\r"); |
superphil06 | 1:ba608856b208 | 428 | #endif |
superphil06 | 1:ba608856b208 | 429 | //setup http response header & data |
superphil06 | 1:ba608856b208 | 430 | /************* patch all dynamic data in html response page ************/ |
superphil06 | 1:ba608856b208 | 431 | //giCounter++; |
superphil06 | 1:ba608856b208 | 432 | |
superphil06 | 1:ba608856b208 | 433 | (*pfPtr)();// call to external CGI function located in main |
superphil06 | 1:ba608856b208 | 434 | |
superphil06 | 1:ba608856b208 | 435 | /* char ma_chaine4[20]={};// needed to form html response |
superphil06 | 1:ba608856b208 | 436 | sprintf (ma_chaine4,"%d",(int)15);// convert speed as ascii string |
superphil06 | 1:ba608856b208 | 437 | Html_Patch (tab_balise,0,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 438 | |
superphil06 | 1:ba608856b208 | 439 | sprintf (ma_chaine4,"%d",(int)16);// convert count as ascii string |
superphil06 | 1:ba608856b208 | 440 | Html_Patch (tab_balise,1,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 441 | |
superphil06 | 1:ba608856b208 | 442 | sprintf (ma_chaine4,"%d",17);// convert count as ascii string |
superphil06 | 1:ba608856b208 | 443 | Html_Patch (tab_balise,2,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 444 | |
superphil06 | 1:ba608856b208 | 445 | sprintf (ma_chaine4,"%d",18);// convert count as ascii string |
superphil06 | 1:ba608856b208 | 446 | Html_Patch (tab_balise,3,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 447 | |
superphil06 | 1:ba608856b208 | 448 | sprintf (ma_chaine4,"%d",19);// convert count as ascii string |
superphil06 | 1:ba608856b208 | 449 | Html_Patch (tab_balise,4,ma_chaine4);// patch first label with dyn.string |
superphil06 | 1:ba608856b208 | 450 | |
superphil06 | 1:ba608856b208 | 451 | */ |
superphil06 | 1:ba608856b208 | 452 | |
superphil06 | 1:ba608856b208 | 453 | |
superphil06 | 1:ba608856b208 | 454 | |
superphil06 | 1:ba608856b208 | 455 | |
superphil06 | 1:ba608856b208 | 456 | char echoHeader[256] = {}; |
superphil06 | 1:ba608856b208 | 457 | sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r",strlen(html_page)); |
jlsalvat | 2:468edcd380ae | 458 | clt_sock.send(echoHeader,strlen(echoHeader)); |
superphil06 | 1:ba608856b208 | 459 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 460 | printf("%s",echoHeader);// debut http header sent |
superphil06 | 1:ba608856b208 | 461 | #endif |
jlsalvat | 2:468edcd380ae | 462 | //clt_sock.send(buffer,strlen(buffer));// realise echo request to clt_sock |
superphil06 | 1:ba608856b208 | 463 | |
superphil06 | 1:ba608856b208 | 464 | |
jlsalvat | 2:468edcd380ae | 465 | clt_sock.send(html_page,strlen(html_page));// realise echo request to clt_sock |
superphil06 | 1:ba608856b208 | 466 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 467 | printf("%s",html_page); |
superphil06 | 1:ba608856b208 | 468 | #endif |
superphil06 | 1:ba608856b208 | 469 | |
jlsalvat | 2:468edcd380ae | 470 | clt_sockIsConnected = false;// close connection with this clt_sock at end of http response |
superphil06 | 1:ba608856b208 | 471 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 472 | printf("web page sent.\n\r"); |
superphil06 | 1:ba608856b208 | 473 | #endif |
superphil06 | 1:ba608856b208 | 474 | } |
superphil06 | 1:ba608856b208 | 475 | break; |
superphil06 | 1:ba608856b208 | 476 | }// end case |
superphil06 | 1:ba608856b208 | 477 | //Thread::wait(10);// sleep for 10 ms |
jlsalvat | 2:468edcd380ae | 478 | }// end while clt_sock is Connected |
superphil06 | 1:ba608856b208 | 479 | #ifdef _HTTP_DEBUG |
superphil06 | 1:ba608856b208 | 480 | printf("close connection.\n\rtcp server is listening...\n\r"); |
superphil06 | 1:ba608856b208 | 481 | #endif |
jlsalvat | 2:468edcd380ae | 482 | clt_sock.close(); |
superphil06 | 1:ba608856b208 | 483 | |
superphil06 | 1:ba608856b208 | 484 | |
superphil06 | 1:ba608856b208 | 485 | } |
superphil06 | 1:ba608856b208 | 486 | Thread::wait(10);// sleep for 10 ms |
superphil06 | 1:ba608856b208 | 487 | } // end while ServerIs listening |
superphil06 | 1:ba608856b208 | 488 | } |
superphil06 | 1:ba608856b208 | 489 | |
superphil06 | 1:ba608856b208 | 490 | |
superphil06 | 1:ba608856b208 | 491 | |
superphil06 | 1:ba608856b208 | 492 | |
superphil06 | 1:ba608856b208 | 493 | |
superphil06 | 1:ba608856b208 | 494 | |
superphil06 | 1:ba608856b208 | 495 | |
superphil06 | 1:ba608856b208 | 496 | |
superphil06 | 1:ba608856b208 | 497 |