Forum Discussion
Altera_Forum
Honored Contributor
21 years agoCGI?
Hello everyone:
Does the boa webserver ("pre-buit version" provided by Microtronix) support CGI ? In the directory:"/c/altera/kits/nios2/examples/software/linux/apps/boa/util", there are some files such as *.pl or cgi-test.cgi .....etc, Is that means those files are examples for writing perl CGI? http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/blink.gif24 Replies
- Altera_Forum
Honored Contributor
Yes it does support cgi. I got it to work.
You have to create /home/httpd/cgi-bin directory in your fs project and place your programs there. -Thomas - Altera_Forum
Honored Contributor
hello Thomas:
thanks for your reply, so,what CGI language does the Microtronix linux distribution support? perl? and ,is that means we don't have to install any additional application to support CGI? then finally,could you show us a simple CGI program as an exmple? your information helps a lot, thanks. - Altera_Forum
Honored Contributor
Ok,
I was going to post this in the web server thread, but i will post it here instead. CGI program can be written in any language because CGI is a method of passing data not a specific implementation. The easiest way is to create a C program using the Microtronix linux application wizard. For example this is a program that controls the LEDs on the cyclone dev board: # include <stdio.h># include <string.h># include <stdlib.h> // replace the path with proper path to nios2_system.h# include <C:\altera\kits\nios2\bin\eclipse\workspace\linux1\build\include\nios2_system.h> void main(void) { np_pio *pio = na_led_pio; pio->np_piodirection = 3; pio->np_piodata = 0; int content_length=0; char *cgiinput ; unsigned char mydata=0; pio->np_piodata = 0x00; printf("Content-type: text/html\n"); printf("\n"); if ( !(content_length = atoi(getenv("CONTENT_LENGTH"))) ) { printf("No Content-Length was sent with the POST request.\n") ; exit(1) ; } if ( !(cgiinput=(char *)malloc(content_length+1)) ) { printf("Could not allocate memory for cgiinput.\n") ; exit(1) ; } if (!fread(cgiinput, content_length, 1, stdin)) { printf("Could not read CGI input from STDIN.\n") ; exit(1) ; } cgiinput[content_length]='\0' ; if (strstr(cgiinput, "LED1")) mydata |= 1; if (strstr(cgiinput, "LED2")) mydata |= 2; if (strstr(cgiinput, "LED3")) mydata |= 4; if (strstr(cgiinput, "LED4")) mydata |= 8; if (strstr(cgiinput, "LED5")) mydata |= 16; if (strstr(cgiinput, "LED6")) mydata |= 32; if (strstr(cgiinput, "LED7")) mydata |= 64; if (strstr(cgiinput, "LED8")) mydata |= 128; pio->np_piodata = mydata; // printf("cgi input was: \n%s\n", cgiinput); printf("<html><head><title>CGI\n"); printf("Test</title><body>\n"); printf("<h1>LEDs set</h1>"); printf("\n");printf("</body></html>\n"); exit(0); } ----------- Please note that this program does not do proper parsing of CGI input parameters, it only checks if certain parameters were included. That works fine in this simple case. To place this program in the cgi-bin directory, compile it first, then copy/paste to /home/httpd/cgi-bin and rename to "<filename>.cgi.exe" so that when the filesystem is built, it ends up with the proper ".cgi" extension. Then you need a html file in order to make this CGI program useful. For example (Note: replace the IP with the dev boards IP, and cgitest2.cgi with the filename of your CGI program): <FORM METHOD="POST" ACTION="http://192.168.1.122/cgi-bin/cgitest2.cgi"> 1<INPUT TYPE="checkbox" NAME="LED1" VALUE="led1" CHECKED> 2<INPUT TYPE="checkbox" NAME="LED2" VALUE="led2"> 3<INPUT TYPE="checkbox" NAME="LED3" VALUE="led3"> 4<INPUT TYPE="checkbox" NAME="LED4" VALUE="led4"> 5<INPUT TYPE="checkbox" NAME="LED5" VALUE="led5"> 6<INPUT TYPE="checkbox" NAME="LED6" VALUE="led6"> 7<INPUT TYPE="checkbox" NAME="LED7" VALUE="led7"> 8<INPUT TYPE="checkbox" NAME="LED8" VALUE="led8"> <INPUT TYPE="submit" value="OK"> </FORM> ------------------- You can execute this html file from your PC's desktop or host it on the dev board under /home/httpd. I hope this helps, let me know if more explanation is needed. -Thomas
- Altera_Forum
Honored Contributor
http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/laugh.gif it works!
Thank you very much,Thomas. - Altera_Forum
Honored Contributor
i do it according to guide
but HTTP 400 appear why? - Altera_Forum
Honored Contributor
--- Quote Start --- originally posted by zhangyan_swpu@Oct 17 2006, 04:09 AM i do it according to guidebut http 400 appear
why?
<div align='right'><{post_snapback}> (index.php?act=findpost&pid=18726)
--- quote end ---
--- Quote End --- hi
don't give up, try this simple example : ************* html code : index.html****************** <FORM method="get" ACTION="mult.cgi" > Please specify the multiplicands: <INPUT NAME="m" SIZE="5"> <INPUT NAME="n" SIZE="5"> <INPUT TYPE="SUBMIT" VALUE="Multiply!"> </FORM> **************************************** ************** cgi code : mult.c****************** # include <stdio.h># include <stdlib.h> int main(void) { char *data; long m,n; printf("%s%c%c\n", "Content-Type:text/html;charset=iso-8859-1",13,10); printf("<TITLE>Multiplication results</TITLE>\n"); printf("<H3>Multiplication results</H3>\n"); data = getenv("QUERY_STRING"); if(data == NULL) printf(" Error! Error in passing data from form to script."); else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2) printf(" Error! Invalid data. Data must be numeric."); else printf(" The product of %ld and %ld is %ld.",m,n,m*n); return 0; } ****************************************************** ******************** Makefile : Makefile***********************# # configurable options# - set DEBUG = 1 to turn on debugging support# DEBUG = 1 PROJ_NAME = mult INSTALL_DIR = PROGS := $(PROJ_NAME).exe CFLAGS += # # You should not need to modify anything beyond this point# TOPDIR = . include ./Rules.mak ifeq '$(DEBUG)' '1' PROGS += $(PROGS:.exe=.gdb) endif all: $(PROGS) .PHONY: clean clean: -rm -f *. *.elf *.gdb *.bin *.exe .PHONY: install install: all ifeq "$(INSTALL_DIR)" "" $(error No installation directory specified) endif mkdir -p $(INSTALL_DIR)/bin install -v $(filter %.exe, $(PROGS)) $(INSTALL_DIR)/bin *************************************************************** steps to follow : --> create a new uclinux application --> add "mult.c" and "Makefile" to project --> right click on project : -- creat make target --> yes -- build make target --> if compilation success it will create a multi.exe --> add "index.html" and "mult.exe" to httpd derictory on file system --> rename "mult.exe" by "mult.cgi" and go ! :D - Altera_Forum
Honored Contributor
thank you very much!
sdhnain! i will try again! - Altera_Forum
Honored Contributor
--- Quote Start --- originally posted by zhangyan_swpu@Oct 17 2006, 11:09 AM i do it according to guidebut http 400 appear
why?
<div align='right'><{post_snapback}> (index.php?act=findpost&pid=18726)
--- quote end ---
--- Quote End --- Or, you can move to Linux PC, try out uClinux-dist as in Nios wiki. There is a generic cgi example in uClinux-dist. It is totally wrong to develop Linux on a Windows machine. I believe you will need to move to Linux PC, sooner or later.
- Altera_Forum
Honored Contributor
Hello,
I'm a newbie. http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/rolleyes.gif I just joined. I'll try this example and post another reply later. Basically: I'm trying to build a cgi app in C that will pipe SCPI ascii strings to and from a web page(s) and rack of (<=96) modules (CycloneIII's) via one App on one master controller with Altera Cyclone I/NIOS II/Microtronix Linux/etc. All in a few weeks. LOL Obviously I dont even know what the right questions to ask are yet. LOL. http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/biggrin.gif I hope this is the right forum? - Altera_Forum
Honored Contributor
I'm trying to build/run the simpler sdhnain version.
I get these error messages when I try to "-- build make target"? make -k all no emulation specific options. Makefile:25: *** missing separator. Stop. What are emulation options? from SOPC builder ? I'll try some more. These are lines 23, 24, 25 in the makefile? .PHONY: clean clean: -rm -f *.[oad] *.elf *.gdb *.bin *.exe What seperator is needed and where?