#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include "cgisetup.h"
int setup(struct cgi *real)
{
char *method;
int len;
char *s;
int i, j;
int values;
int num;
char *temp;
real->num = 0;
method = (char*)getenv("REQUEST_METHOD");
if (!method)
return -1;
if (strcmp(method, "POST") == 0)
{
len = atoi(getenv("CONTENT_LENGTH"));
}
else if (strcmp(method, "GET") == 0)
{
s = getenv("QUERY_STRING");
if (s == NULL)
return -1;
len = strlen(s);
}
else
return -1;
temp = (char*)malloc(len + 1);
temp[len] = 0;
if (strcmp(method, "POST") == 0)
{
s = (char*)malloc(len + 1);
s[len] = 0;
for (i = 0; i < len; i++)
{
s[i] = getchar();
}
s[i] = 0;
}
values = countchars('&', s) + 1 - countstrings("=&", s);
real->num = values;
real->name = (char**)malloc(values * sizeof(char*));
real->value = (char**)malloc(values * sizeof(char*));
i = 0;
for (num = 0; num < values; num++)
{
for (j = 0; s[i] != '='; i++, j++)
{
temp[j] = s[i];
}
temp[j] = 0;
i++;
if (s[i] == '&')
{
num--;
i++;
continue;
}
fix(temp);
real->name[num] = (char*)malloc(strlen(temp)+1);
strcpy(real->name[num], temp);
for (j = 0; s[i] != '&' && i < len; i++, j++)
{
temp[j] = s[i];
}
temp[j] = 0;
fix(temp);
real->value[num] = (char*)malloc(strlen(temp)+1);
strcpy(real->value[num], temp);
i++;
}
free(s);
free(temp);
return 0;
}
char *findval(struct cgi *real, const char *name)
{
int i;
for (i = 0; i < real->num; i++)
{
if (strcmp(real->name[i], name) == 0)
return real->value[i];
}
return "";
}
void movefile(const char *from, const char *to)
{
FILE *f1, *f2;
char ch;
f1 = fopen(from, "r");
f2 = fopen(to, "w");
while (!feof(f1))
{
ch = getc(f1);
if (!feof(f1))
fputc(ch, f2);
}
fclose(f1);
fclose(f2);
unlink(from);
return;
}
void viewfile(const char *fname)
{
FILE *f1;
char ch;
f1 = fopen(fname, "r");
while (!feof(f1))
{
ch = getc(f1);
if (!feof(f1))
putchar(ch);
}
fclose(f1);
return;
}
void freecgi(struct cgi *real)
{
int i;
for (i = 0; i < real->num; i++)
{
free(real->name[i]);
free(real->value[i]);
}
free(real->name);
free(real->value);
}
int countchars(char ch, const char *string)
{
int count = 0;
int len;
int i;
len = strlen(string);
for (i = 0; i < len; i++)
{
if (string[i] == ch)
count++;
}
return count;
}
int countstrings(const char *lookfor, const char *lookin)
{
int i;
int len, len2;
int count = 0;
len = strlen(lookin);
len2 = strlen(lookfor);
for (i = 0; i < len && len2 + i <= len; i++)
{
if (strncmp(&lookin[i], lookfor, len2)==0)
count++;
}
return count;
}
void fix(char *s)
{
char *temp;
int i, j;
replace(s, '+', ' ');
temp = malloc(strlen(s) * sizeof(char) + 1);
j = 0;
for (i = 0; i < strlen(s); i++)
{
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);
free(temp);
return;
}
int convhex(char ch)
{
int ans;
if (isdigit(ch))
return ch-'0';
ans = toupper(ch);
return 10 + ans - 'A';
}
void replace(char *s, char from, char to)
{
int i;
int len;
len = strlen(s);
for (i = 0; i < len; i++)
if (s[i] == from)
s[i] = to;
return;
}
void strlwr(char *s)
{
int i;
for (i = 0; i < strlen(s); i++)
s[i] = tolower(s[i]);
return;
}