.

To test CGI scripts you need three things - one, a language that can use stdin and stdout; two, an OS that allows you to use stdin and stdout; three, a web server. (1)Almost all languages can use stdin and stdout. I have used PowerBasic for DOS, batch files, Bash, and C.
(2)Windows and Linux allow stdin and stdout.
(3)Web server I use APACHE.
If you decide to install LINUX, use the APATHE use the linux downloader.
During installing it will ask 3 questions: "Network Domain" I put "issw.loc" "Server Name" I put "www.issw.loc" and e-mail Address I put admin@issw.loc. As you can see, the Server Name is www. and the Network Domain. DO NOT use a .com, .net, etc.
Next, open "My Computer" and go to C:/Windows and look for either HOSTS or HOSTS.SAM. If you see HOSTS with no extension, you will edit it. But most likely you will see HOSTS.SAM. Open C:\windows\hosts.sam in Notepad. Then save it as hosts (Notepad will add a .txt) , the bottom line will read 127.0.0.1 localhost. Under it add 127.0.0.1 and whatever you put as Server Name. In my case, 127.0.0.1 www.issw.loc. Save it.
In "My Computer" you will see HOSTS.TXT and HOSTS.SAM. You may need to press the "F5" key to refresh the screen if the .TXT is not there. Go to "View" then "Folder Options" and choose "Show all files" and uncheck "Hide file extensions for known file types". Press the "F5" key and you will see HOSTS.TXT. Right click on HOSTS.TXT, choose "RENAME" from the list and remove the ".TXT" so you have HOSTS all done.
Go to "Start -> Programs -> Apache HTTP Server 2.0 -> Control Apache Server -> Start Apache in Console". A DOS Box will popup, minimize it but do not close it. Now you can open your web browser and type in your Server Name. In my case, www.issw.loc or http://127.0.0.1 or http://localhost and you will see the Apache default screen. Make and install you own index.html file in (if you choose the default location for the Apache directory) C:\Program Files\Apache Group\Apache2\htdocs. Press "F5" and you will see the new file.
If you have a problem, look at your firewall.
IN LINUX, edit the HOSTS file and enter the same as Windows. The HOST file is generally in /etc directory. If not, try "locate hosts" in a terminal.
In both Linux and Windows if you do not want to edit the HOSTS file you can just type http://127.0.0.1 or http://localhost in the URL in your web browser.
printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<html>\n") ;
printf("<head><title>CGI Output</title></head>\n");
printf("<body>\n") ;
printf("<H1><TEST OUTPUT!</CENTER></H1>");
printf("<p><h1><center>%d,%s,%s,%d</center></h1></p>\n",a,b,c,d);
printf("</body>\n") ;
printf("</html>\n") ;
//at the top This help to not define the same header more then once. #ifndef HEADER_FILE_NAME_H #define HEADER_FILE_NAME_H //and at bottom #endif
//----------------------------- TEST FUNCTIONS ------------------------------
//Prints test output to html browser.
void TM(int a,char *b, char *c, int d) {
printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<html>\n") ;
printf("<head><title>CGI Output</title></head>\n") ;
printf("<body>\n") ;
printf("<H1><TEST OUTPUT!</CENTER></H1>");
printf("<p><h1><center>%d,%s,%s,%d</center></h1></p>\n",a,b,c,d);
printf("</body>\n") ;
printf("</html>\n") ;
}
bob 123456 bob 123456
else if(*src == '&') *dest = '\n'; else if(*src == '&') *dest = ' ';
1) #define strsize 43 //max name + max password + 3 (=&=)
2) #define minsize 7 //min name + min password + 3
3) char *lenstr;
4) char RawBuffer[(3 * strsize) + 1];
5) char DecBuffer[strsize + 1];
6) long len;
7) lenstr = getenv("CONTENT_LENGTH");
8) sscanf(lenstr,"%ld",&len);
9) if(len < minsize || len > (strsize * 3)) { EMessage(1); }
10) else { inputfile(DecBuffer, len, RawBuffer); }
//create a pointer (1 bite) and a buffer (50,000 bites).
char *ptr_data, char data[50000];
//You referenced the pointer to the buffer.
//The first element of the buffer is pointed to by the pointer.
ptr_data = data;
int x;
//you have put 50,000 5's in the area data.
for (x=0; x< sizeof(data); x++) { data[x] = '5'; }
//if you send the data like this, there will be 2 buffers with 50,000 elements. 100,0000 elements.
send(data);
send(ptr_data); //you have sent 1 bit. telling the receiving function where the first char of the data buffer is.
int inputfile(char *pasw, int lensize, char *usern) {
fgets(usern, lensize+1, stdin);
unencode(usern, usern+lensize, pasw);
return 1; }
void unencode(char *src, char *last, char *dest) {
for(; src != last; src++, dest++)
if(*src == '+')
*dest = ' ';
// else if(*src == '&')
// *dest = '\n';
else if(*src == '&')
*dest = ' ';
else if(*src == '=')
*dest = ' ';
else if(*src == '%') {
int code;
if(sscanf(src+1, "%2x", &code) != 1) code = '?';
*dest = code;
src +=2; }
else
*dest = *src;
*dest = '\n'; //new line.
*++dest = '\0'; //end of file.
}
else if(*src == '%') {
int code;
if(sscanf(src+1, "%2x", &code) != 1) code = '?';
*dest = code;
src +=2; }
int readpasw(char *name) {
FILE *fp; //creates file pointer.
char string1[60], *pst1; //creates a char buffer and a char pointer.
int d=0,e=1,read; //creates 3 int.
char *line = NULL; //creates a NULL char pointer.
size_t len = 0; //creates a size_t int
pst1 = string1; //The pointer pst1 now points to the first character of the buffer string1.
strcpy(lstr,name); //copies name buffer into the global area lstr to use int writing the log files.
memset(pst1,'\0',sizeof(string1)); //clears the area string1
d = strlen(name); //gets the length of name
if(d > 63) { EMessage(1); } //If name is larger than 63 characters error. You can do without this if you want.
fp = fopen(passw, "r"); //opensfile for read only.
if(fp == NULL) { return 0; } //if unable to open file return 0.
while(read != -1){ //Loop till getline returns a -1.
//getline() reads each line and puts it into the buffer referenced by address of line because line is a NULL pointer.
// getline will dynamically create a buffer to hold the string.
read = getline(&line, &len, fp);
//If the length of the string name is the same size as the string in the file we will compare the two string.
// Because we only compare the strings if they are the same size, the function is faster.
if(read == d) { e = strncmp(name, line, read); }
if(e == 0) { if(line) { free(line); } return 1; } . //If the two string are the same clears the getline buffer and returns 1.
}//end while(). //Loops tell -1.
//getline() will resize the buffer it creates to the length of the string in the file.
// BUT it will not delete it. But if you try to delete a buffer that does not exist
// your program will do an unpredictable thing, if(line) checks to see if the line buffer
//exists before deleting it.
if(line) { free(line); }
fclose(fp); //closes the file.
return 2; } //if no match and no errors returns 2.
void EMessage(int error) {
char *ptr_str;
char str1[] = " INVALID PASSWORD! "; //Create areas to hold the error messages.
char str2[] = "UNABLE TO OPEN DATA FILE.";
char str3[] = "UNABLE TO OPEN PASSWORD FILE.";
char str4[] = "UNABLE TO OPEN HTML FILE.";
char str5[] = "UNABLE TO REMOVE FILE LOCK!";
if(error == 1) { ptr_str = str1; } //Print the right error message and only one.
else if(error == 2) { ptr_str = str2; } // (if, else if) for speed.
else if(error == 3) { ptr_str = str3; }
else if(error == 4) { ptr_str = str4; }
else if(error == 5) { ptr_str = str5; }
//Vertual web page.
// Print the CGI response header, required for all HTML output.
// Print the HTML response page to STDOUT.
printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10); //don't forget this.
printf("<html>\n") ;
printf("<head><title>CGI Output</title></head>\n") ;
printf("<body>\n") ;
printf("<H1><CENTER>ERROR!</CENTER></H1>");
printf("<p><h1><center>%s</center></h1></p>\n", ptr_str);
printf("</body>\n") ;
printf("</html>\n") ;
exit(1); } //exit program.
1) void admin(void) {
2) char filename[] = tofile;
3) char buffer[80];
4) FILE *fname;
5) loge_file(1); //log username, password, and time.
6) printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
7) printf("<html>\n") ;
8) printf("<head><title>CGI Output</title></head>\n") ;
9) printf("<body>\n") ;
10) if((fname = fopen(filename, "r")) == NULL)
11) { printf("UNABLE TO OPEN //ADMIN//INDEX.HTM"); exit(1); }
12) while(!feof(fname)) {
13) fgets(buffer, sizeof(buffer), fname);
14) printf("%s\n", buffer); }
15) printf("</body>\n") ;
16) printf("</html>\n") ;
17) fclose(fname); }
void admin(void) {
int read;
char *line = NULL;
size_t len = 0;
FILE *fname;
loge_file(1); //log username, password, and time.
printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
if((fname = fopen(tofile, "r")) == NULL)
{ EMessage(6); }
while(read != -1){
read = getline(&line, &len, fname);
printf("%s", line);
}//end while().
if(line) { free(line); } //frees the getline() memory.
fclose(fname);
}
void loge_file(int mode) {
time_t t;
FILE *l;
time(&t);
//will enter every password entry if it is correct or incorrect.
if(mode == 1) { l = fopen(logefile, "a"); }
else { l = fopen(errorfile, "a"); }
flock(fileno(l),LOCK_EX);
//enter user name and password.
fputs(lstr, l); fputs(" ",l); fputs(ctime(&t), l);
if(flock(fileno(l),LOCK_UN) != 0) { EMessage(5); }
fclose(l);
}
//home mad lock.
int file_lock(int mode) {
const int MAX_TRY = 10;
FILE *fp;
if(mode != 1) { fclose(fp); } //close file
else {
for(int tryno = 0; tryno < MAX_TRY; tryno++) {
if(fp = fopen("ttemp.tmp", "r") == NULL) {
usleep(200); } //end if()
} //enf for().
} //end else.
return 0; }
//You do not have to fill all the elements.
struct flock {
...
short l_type; // Type of lock: F_RDLCK,
// F_WRLCK, F_UNLCK
short l_whence; // How to interpret l_start:
// SEEK_SET, SEEK_CUR, SEEK_END
off_t l_start; // Starting offset for lock
off_t l_len; // Number of bytes to lock, 0 means until EOF
pid_t l_pid; // PID of process blocking our lock
...
};
struct flock lck;
#include <iostream> #define max_length 127 //max length of the form string. #define min_length 5 //min length of the form string. #define error 1 //creates an allus for error. char *data = (char *) 0;
//prints to a html page. CENTERED.
int return_message(char *str, int nexit) {
printf("Content-type: text/HTML\n\n");
printf("<html>\n") ;
printf("<head><title>CGI Output</title></head>\n") ;
printf("<body>\n") ;
printf("<p><H3><center>%s</center></H3>\n", str) ;
printf("</body>\n") ;
printf("</html>\n") ;
if(nexit == 1) { exit(0); }
return 0; }
//Determins the method.
1)int get_method(void) {
2)int id;
3)char *type;
4)char *get = "GET";
5)char *post = "POST";
6)char *error1 = "ERROR = REQUEST_METHOD EMPTY!";
7)type = getenv("REQUEST_METHOD");
8) if(strcmp(type,post) == 0) { return 1; } //post
9) else if(strcmp(type,get) == 0) { return 2; } //get
10) else { return_message(error1,error); } //error
11)return 0; }
//Determins the method.
int get_method(void) {
int id;
char *type;
char *post = "POST";
type = getenv("REQUEST_METHOD");
if(strcmp(type,post) == 0) { return 1; } //post
return 0; }
//
1)int get_post(void) {
3)char *char_size;
4)char *rtrn = "DATA RECIVED!";
5)char *error1 = "POST IS NOT THE RIGHT SIZE!";
6)char *error2 = "UNABLE TO ALLOC MEMORY FOR POST!";
7)char *error3 = "POST STRING IS ENPTY!";
8) char_size = getenv("CONTENT_LENGTH");
9) if(char_size != NULL) { len = atoi(char_size); }
10) else { return_message(error3,error); }
11) if(len max_length) {
12) return_message(error1,error); }
13) data = (char *) malloc(sizeof(char) * (len + 2));
14) if(data == NULL) { return_message(error2,error); }
15) fgets(data, len+1, stdin);
len += 2;
16) *(data + len) = '\0';
17)//////return_message(data,0); //test only.
18)return_message(rtrn,0);
19)return 0; }
1)int get_get(void) {
2)char *querystring;
3)char *rtrn = "DATA RECIVED!";
4)char *error1 = "GET IS NOT THE RIGHT SIZE!";
5)char *error2 = "UNABLE TO ALLOC MEMORY FOR GET!";
6)char *error3 = "GET STRING IS ENPTY!";
7)querystring = getenv("QUERY_STRING");
8) if(querystring != NULL) { len = strlen(querystring); }
9) else { return_message(error3,error); }
10) if(len max_length) { return_message(error1,error); }
11) data = (char *) malloc(sizeof(char) * (len));
12) if(data == NULL) { return_message(error2,error); }
13) strcpy(data,querystring);
14)transcode();
15)//return_message(data,error); //test only.
16)return_message(rtrn,0);
17)return 0; }
//Tack out the +,=,&,% and coverts the scancodes.
void transcode(void) {
1)int x=0,y=0;
2)char *error1 = "UNABLE TO ALLOC MEMORY!";
3) outp = (char *) malloc(sizeof(char) * (len + 2));
4) if(data == NULL) { return_message(error1,error); }
5) for(;x < len; x++,y++ ) {
6) if(*(data+y) == '+') { *(outp+x) = ' '; }
7) else if(*(data+y) == '=') { *(outp+x) = ' '; }
8) else if(*(data+y) == '&') { *(outp+x) = '\n'; }
9) else if(*(data+y) == '%') {
10) int scode;
11) y += 1;
12) if(sscanf(data+y,"%2x",&scode) != 1) scode = '?';
13) else { *(outp+x) = scode; }
14) y += 1; }
15) else { *(outp+x) = *(data+y); }
16) }
17) *(outp+x) = '\n';
18) x += 2;
19) *(outp+x) = '\0';
}
FNAME Gary LNAME Russell ADDR1 1127 Acane Drv. ADDR2 Spit,Mo ZIP 64355 SEND SEND
int main() {
int id;
id = get_method();
if(id == 1) { get_post(); }
else { get_get(); }
return 0;
}