Вы находитесь на странице: 1из 6

Com/PC1 with Embedded Gateway Linux (EGL/2) microHOWTO

How to write a CGI for the Apache Web server in C


The Com/PC Embedded Gateway Linux (EGL/2) operating system comes with a pre-installed Apache Web server. Please see also mHT-CPC1L-07.pdf: How to use the Apache Web server for more information. This server supports also CGI (Common Gateway Interface). 1. Step: Download the file cgi-demo.c from the Com/PC website at www.ssv-comm.de. This file is the C source code for a simple CGI sample. 2. Step: Run your SSV coLinux environment on your development PC. Translate cgi-demo.c to an executable. Use the following command line gcc o cgi-demo cgi.demo.c within the SSV coLinux environment. This command line runs the GNU C compiler and creates the executable cgi-demo.

3. Step: Transfer the executable cgi-demo from the coLinux directory to the Windows directory /windows/transfer. Then run a TFTP server within the Windows environment.

SSV EMBEDDED SYSTEMS 2007, mHT-CPC1L-010.doc, Rev. 1.00.

Com/PC1 with Embedded Gateway Linux (EGL/2) microHOWTO

4. Step: Run a Telnet or SSH session. Then execute the following commands: cd /rwnv/opt/lampp/cgi-bin tftp binary connect 192.168.0.2 get cgi-demo quit chmod +x cgi-demo

Please note: This sample assumes that your development PC with Windows and coLinux is using the IP address 192.168.0.2. Change this address if necessary. 5. Step: Access the executable cgi-demo with your Web browser. The URL for this CGI on the Com/PC is: http://192.168.0.126/cgi-bin/cgi-demo

SSV EMBEDDED SYSTEMS 2007, mHT-CPC1L-010.doc, Rev. 1.00.

Com/PC1 with Embedded Gateway Linux (EGL/2) microHOWTO

This URL assumes that your Com/PC is using the IP address 192.168.0.126. Change this address if necessary.

The cgi-demo.c source code:


// cgi-demo.c: Simple CGI demo program. // Vers. 2.00 - 04.April 2007 // k.d.walter@t-online.de // Includes #include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc, char *argv[]) { char *pEnvPtr; printf printf printf printf printf printf printf ("Content-type: text/html\n"); ("\n"); ("<HTML>\n"); ("<HEAD>\n"); ("<TITLE>CGI Environment Variable</TITLE>\n"); ("</HEAD>\n"); ("<BODY>\n");

// SERVER_SOFTWARE pEnvPtr= getenv ("SERVER_SOFTWARE"); printf ("SERVER_SOFTWARE= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // SERVER_NAME pEnvPtr= getenv ("SERVER_NAME"); printf ("SERVER_NAME= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // SERVER_PROTOCOL pEnvPtr= getenv ("SERVER_PROTOCOL"); printf ("SERVER_PROTOCOL= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n");

SSV EMBEDDED SYSTEMS 2007, mHT-CPC1L-010.doc, Rev. 1.00.

Com/PC1 with Embedded Gateway Linux (EGL/2) microHOWTO

// SERVER_PORT pEnvPtr= getenv ("SERVER_PORT"); printf ("SERVER_PORT= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // REQUEST_URI pEnvPtr= getenv ("REQUEST_URI"); printf ("REQUEST_URI= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // REQUEST_METHOD pEnvPtr= getenv ("REQUEST_METHOD"); printf ("REQUEST_METHOD= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // GATEWAY_INTERFACE pEnvPtr= getenv ("GATEWAY_INTERFACE"); printf ("GATEWAY_INTERFACE= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // HTTP_CONNECTION pEnvPtr= getenv ("HTTP_CONNECTION"); printf ("HTTP_CONNECTION= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // PATH_INFO pEnvPtr= getenv ("PATH_INFO"); printf ("PATH_INFO= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // PATH_TRANSLATED
SSV EMBEDDED SYSTEMS 2007, mHT-CPC1L-010.doc, Rev. 1.00.

Com/PC1 with Embedded Gateway Linux (EGL/2) microHOWTO

pEnvPtr= getenv ("PATH_TRANSLATED"); printf ("PATH_TRANSLATED= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // REMOTE_HOST pEnvPtr= getenv ("REMOTE_HOST"); printf ("REMOTE_HOST= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // REMOTE_ADDR pEnvPtr= getenv ("REMOTE_ADDR"); printf ("REMOTE_ADDR= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // REMOTE_PORT pEnvPtr= getenv ("REMOTE_PORT"); printf ("REMOTE_PORT= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // REMOTE_IDENT pEnvPtr= getenv ("REMOTE_IDENT"); printf ("REMOTE_IDENT= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // SCRIPT_FILENAME pEnvPtr= getenv ("SCRIPT_FILENAME"); printf ("SCRIPT_FILENAME= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // SCRIPT_NAME

SSV EMBEDDED SYSTEMS 2007, mHT-CPC1L-010.doc, Rev. 1.00.

Com/PC1 with Embedded Gateway Linux (EGL/2) microHOWTO

pEnvPtr= getenv ("SCRIPT_NAME"); printf ("SCRIPT_NAME= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // QUERY_STRING pEnvPtr= getenv ("QUERY_STRING"); printf ("QUERY_STRING= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // CONTENT_TYPE pEnvPtr= getenv ("CONTENT_TYPE"); printf ("CONTENT_TYPE= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); // CONTENT_LENGTH pEnvPtr= getenv ("CONTENT_LENGTH"); printf ("CONTENT_LENGTH= "); if (!pEnvPtr) printf ("<NULL-POINTER>\n"); else printf ("%s\n", pEnvPtr); printf ("<br>\n"); printf ("</BODY>\n"); printf ("</HTML>\n"); return (EXIT_SUCCESS);

That is all.

SSV EMBEDDED SYSTEMS 2007, mHT-CPC1L-010.doc, Rev. 1.00.

Вам также может понравиться