#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <iostream.h>
#include "cgisetup.hxx"
const int MAX_LEN = 1000;
const char CGI_METHOD[] = "REQUEST_METHOD";
const char CGI_QUERY[] = "QUERY_STRING";
const char CGI_LENGTH[] = "CONTENT_LENGTH";
CGISetup::CGISetup()
{
char *method;
char string[MAX_LEN];
method = getenv(CGI_METHOD);
if (strcmp(method, "GET") == 0)
MethodGet(string);
else if (strcmp(method, "POST") == 0)
MethodPost(string);
else
throw "Error: No CGI input data detected!";
num = CountValues(string);
DecodeURL(string);
names = new char*[num];
values = new char*[num];
ParseString(string);
}
//
CGISetup::~CGISetup()
{
for (int i = 0; i < num; i++)
{
delete[] names[i];
delete[] values[i];
}
delete[] names;
delete[] values;
}
//
int CGISetup::GetNum() const
{
return num;
}
//
char* CGISetup::GetName(int n) const
{
if (n < num && n >= 0)
return names[n];
else
{
throw "Error: Index out of range.";
return "";
}
}
//
char* CGISetup::GetValue(int n) const
{
if (n < num && n >= 0)
return values[n];
else
{
throw "Error: Index out of range.";
return "";
}
}
//
char* CGISetup::GetValue(const char *name) const
{
for (int i = 0; i < num; i++)
if (strcmp(name, names[i]) == 0)
return values[i];
throw "Error: Name/value pair not found.";
return NULL;
}
//
void CGISetup::CopyFile(const char *from, const char *to)
{
FILE *f1;
FILE *f2;
char ch;
f1 = fopen(from, "r");
f2 = fopen(to, "w");
ch = getc(f1);
while (!feof(f1))
{
fputc(ch, f2);
ch = getc(f1);
}
fclose(f1);
fclose(f2);
return;
}
//
void CGISetup::DecodeURL(char *s)
{
char *temp;
int i, j = 0;
ReplaceChar(s, '+', ' ');
temp = new char[strlen(s)];
for (i = 0; i < strlen(s); i++, j++)
{
if (s[i] == '%')
{
temp[j] = ConvHex(s[i+1]) * 16 + ConvHex(s[i+2]);
i += 2;
}
else
temp[j] = s[i];
}
temp[j] = 0;
strcpy(s, temp);
delete[] temp;
return;
}
//
void CGISetup::MoveFile(const char *from, const char *to)
{
CopyFile(from, to);
unlink(from);
return;
}
//
void CGISetup::StrLwr(char *s)
{
for (int i = 0; i < strlen(s); i++)
s[i] = tolower(s[i]);
return;
}
//
void CGISetup::ViewFile(const char *filename)
{
FILE *f1;
char ch;
f1 = fopen(filename, "r");
ch = getc(f1);
while (!feof(f1))
{
putchar(ch);
ch = getc(f1);
}
fclose(f1);
return;
}
//
int CGISetup::CountChars(char ch, const char *s)
{
int count = 0;
for (int i = 0; i < strlen(s); i++)
if (s[i] == ch)
count++;
return count;
}
int CGISetup::CountStrings(const char *needle, const char *haystack)
{
int count = 0;
for (int i = 0; i < strlen(haystack) - strlen(needle); i++)
{
if (strncmp(&haystack[i], needle, strlen(needle)) == 0)
count++;
}
return count;
}
//
int CGISetup::CountValues(const char *s)
{
return CountChars('&', s) + 1 - CountStrings("=&", s);
}
//
int CGISetup::ConvHex(char ch)
{
ch = toupper(ch);
if (isalpha(ch))
return ch - 'A' + 10;
else
return ch - '0';
}
//
void CGISetup::MethodGet(char *s) const
{
strcpy(s, getenv(CGI_QUERY));
}
//
void CGISetup::MethodPost(char *s) const
{
int i;
int len;
len = atoi(getenv(CGI_LENGTH));
for (i = 0; i < len; i++)
s[i] = getchar();
s[i] = 0;
return;
}
//
void CGISetup::ParseString(const char *s)
{
char *temp;
int currNum, i = 0, j = 0;
temp = new char[strlen(s)];
for (currNum = 0; currNum < num; currNum++)
{
for (j = 0; s[i] != '='; i++, j++)
temp[j] = s[i];
temp[j] = 0;
i++;
if (s[i] == '&')
{
num--;
i++;
continue;
}
names[currNum] = new char[strlen(temp)];
strcpy(names[currNum], temp);
for (j = 0; s[i] != '&' && i < strlen(s); i++, j++)
temp[j] = s[i];
temp[j] = 0;
values[currNum] = new char[strlen(temp)];
strcpy(values[currNum], temp);
i++;
}
delete [] temp;
return;
}
//
void CGISetup::ReplaceChar(char *s, char from, char to)
{
for (int i = 0; i < strlen(s); i++)
if (s[i] == from)
s[i] = to;
return;
}