summaryrefslogtreecommitdiff
path: root/source/cgi.c
blob: 56c293985d60e1ec3b362a4a33cb9067b14b6911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   some simple CGI helper routines
   Copyright (C) Andrew Tridgell 1997
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include "includes.h"

#define MAX_VARIABLES 512

struct var {
	char *name;
	char *value;
};

static struct var variables[MAX_VARIABLES];
static int num_variables;


static int grab_line(int *cl, char *line, int maxsize)
{
	int i = 0;

	while ((*cl)) {
		int c = fgetc(stdin);
		(*cl)--;

		if (c == EOF) {
			(*cl) = 0;
			break;
		}
		
		if (c == '+') {
			c = ' ';
		}

		if (c == '\r') continue;

		if (strchr("\n&", c)) break;

		if (c == '%' && (*cl) >= 2) {
			int c1, c2;
			c1 = fgetc(stdin);
			c2 = fgetc(stdin);
			(*cl) -= 2;
			if (c1 == EOF || c2 == EOF) break;
			if (c1 >= '0' && c1 <= '9')
				c1 = c1 - '0';
			else if (c1 >= 'A' && c1 <= 'F')
				c1 = 10 + c1 - 'A';
			else if (c1 >= 'a' && c1 <= 'f')
				c1 = 10 + c1 - 'a';
			else break;

			if (c2 >= '0' && c2 <= '9')
				c2 = c2 - '0';
			else if (c2 >= 'A' && c2 <= 'F')
				c2 = 10 + c2 - 'A';
			else if (c2 >= 'a' && c2 <= 'f')
				c2 = 10 + c2 - 'a';
			else break;
			
			c = (c1<<4) | c2;
		}

		line[i++] = c;

		if (i == maxsize) break;
	}

	/* now unescape the line */
	

	line[i] = 0;
	return 1;
}


/***************************************************************************
  load all the variables passed to the CGI program
  ***************************************************************************/
void cgi_load_variables(void)
{
	static pstring line;
	char *p;	
	int len;

	if (!(p=getenv("CONTENT_LENGTH"))) return;

	len = atoi(p);

	if (len <= 0) return;

	

	while (len && grab_line(&len, line, sizeof(line)-1)) {		
		p = strchr(line,'=');
		if (!p) continue;

		*p = 0;

		variables[num_variables].name = strdup(line);
		variables[num_variables].value = strdup(p+1);
		
		if (!variables[num_variables].name || 
		    !variables[num_variables].value)
			continue;

#if 0
		printf("%s=%s<br>\n", 
		       variables[num_variables].name,
		       variables[num_variables].value);
#endif

		num_variables++;
		if (num_variables == MAX_VARIABLES) break;
	}

	fclose(stdin);
}


/***************************************************************************
  find a variable passed via CGI
  ***************************************************************************/
char *cgi_variable(char *name)
{
	int i;

	for (i=0;i<num_variables;i++)
		if (strcmp(variables[i].name, name) == 0)
			return variables[i].value;
	return NULL;
}


/***************************************************************************
  return the value of a CGI boolean variable.
  ***************************************************************************/
int cgi_boolean(char *name, int def)
{
	char *p = cgi_variable(name);

	if (!p) return def;

	return strcmp(p, "1") == 0;
}