Application Layer – WEB Server and CGI

en
tcpip_layer4

Internet Protocol Suite

uIP contains WEB Server sample application in apps/webserver. The Application Layer for this implementation uses that one.

WEB Server Processing Flow

WEB Server application handles HTTP(HyperText Transfer Protocol). The server accepts HTTP request message and returns response message. When the server finds GET method in a request message, HTML file name is set and the server seeks internal file system. If the file exists, the server returns HTML as a response.

HTTP processing flow is handled in UIP_APP_CALL().(httpd_appcall is defined as UIP_APP_CALL in webserver.h)

ether_pic6

HTTP Processing Flow Chart

HTML Preparation

Web Server example application also contains “makefsdata” perl script. This script converts HTML files in http-fs sub directory into unsigned char arrays as a file named “httpd-fsdata.c”. httpd-fsdata.c is compiled with other source codes, and HTML files are stored as a part of the final executable object.

CGI Modification and Addition

When the file extension requested by GET method is .shtml, handle_script() is called and starts CGI processing. If “%!” is in the .shtml file, the CGI function based on the string followed by “%!” is executed. After the execution, HTML strings are generated and returned as the response message.

A few CGI samples and predefined macros are available in apps/webserver/httpd-cgi.c. These are good examples to add new CGIs.The following shows an example to add new CGI.
Example: Add CGI “cpu-stats” for displaying CPU clock configuration
1.CGI name definition to be written in shtml
New CGI is added by predefined macro “HTTPD_CGI_CALL”. In this case, new CGI named cpu-stats is added.

HTTPD_CGI_CALL(cpu, "cpu-stats", cpu_stats);

2. Modify *calls[] array
Add cpu in *calls[] array.

static const struct httpd_cgi_call *calls[] = { &cpu, &file, &tcp, &net, NULL };

3. Add protothread for new CGI
Add new protothread named cpu_stats by using protosocket library. protothread is light-weight, stackless thread, and protosocket library is a socket interface provided by uIP.

static
 PT_THREAD(cpu_stats(struct httpd_state *s, char *ptr))
 {
     PSOCK_BEGIN(&s->sout);
     PSOCK_GENERATOR_SEND(&s->sout, generate_cpu_stats, s); 
     PSOCK_END(&s->sout);
 }

4.Add string generation function
Add new function named generate_cpu_stats. The function generates HTML strings and stores them into uip_appdata global variable. The function returns the length of strings stored in uip_appdata.

#define ADD_A_ROW(buf,str) do { \
     l = strlen(SPACE2); \
     strncpy(buf+len,SPACE2,l); \
     len += l; \
     l = strlen(str); \
     strncpy(buf+len,str,l); \
     len += l; \
     l = strlen(BR); \
     strncpy(buf+len,BR,l); \
     len += l; \
     } while (0)

 static unsigned short

 generate_cpu_stats(void *arg)
 {
     int l;
     struct st_cpg x;
     unsigned short len = 0;
     x.FRQCR.LONG = CPG.FRQCR.LONG; // SH4 register read

     if (x.FRQCR.LONG == 0x10244336) {
         ADD_A_ROW(buf,"x12"); 
         ADD_A_ROW(buf,"x6"); 
         ADD_A_ROW(buf,"x1"); 
         ADD_A_ROW(buf,"x24/5"); 
         ADD_A_ROW(buf,"x2"); 
     } else {
         ADD_A_ROW(buf,"Wrong Configuration"); 
     }
     strncpy((char *)uip_appdata,buf,len);

     return len;
 }

タイトルとURLをコピーしました