-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgiu.c~
executable file
·222 lines (198 loc) · 7.91 KB
/
cgiu.c~
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "cgiu.h"
/*------------------------------------------------------------------------------------*/
int countword(char *line, char stop){
int x,count;
if (line[0] == 0) return(0);
count = 1;
for(x = 0; line[x] != 0; x++) count += (line[x] == stop);
return(count);
}
char *getword(char *line, char stop){
int x,y;
char *word;
for(x = 0; (line[x] != 0) && (line[x] != stop); x++) ; /* get word length */
word = (char *) malloc((x+1)*sizeof(char)); /* allocate word buffer */
for (y = 0; y < x; y++) word[y] = line[y]; /* copy word in buffer */
word[x] = 0; /* terminate string */
if (line[x]) x++; /* skip stop character */
for (y = 0; line[x+y] != 0; y++) line[y] = line[x+y]; /* shift line left */
line[y] = 0;
return(word);
}
char x2c(char *what){ /* convert special characters hexa-coded */
char digit;
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}
void unescape_url(char *url){
int x,y;
for(x = 0, y = 0; url[y] != 0; ++x, ++y) {
url[x] = url[y];
if (url[x] == '%') {
url[x] = x2c(&(url[y+1]));
y += 2;
}
}
url[x] = 0;
}
void plustospace(char *str){
int x;
for(x = 0; str[x] != 0; x++) if (str[x] == '+') str[x] = ' ';
}
/*------------------------------------------------------------------------------------*/
void afficherStyle(){
printf("<link href=\"http://localhost/RIM/projet/style.css\" rel=stylesheet media=all type=\"text/css\"> ");
}
void print_html_head(char *title){
printf("Content-type: text/html%c%c",10,10);
printf("<HTML>\n");
printf("<HEAD>\n");
afficherStyle();
printf("<TITLE>%s</TITLE>\n",title);
printf("</HEAD>\n");
printf("<BODY>\n");
}
void printDebutBody(){
//section texte
printf(" <span class=menu1><div style=\"text-align:right;\"><INPUT TYPE=BUTTON onclick=\"self.location.href='http://localhost/RIM/projet/evaluation.html'\""
" NAME=INFO VALUE=EVALUATION class = btop> <br/></div></span>"
" <FORM ACTION=\"http://localhost/cgi-bin/RIM/post4.cgi\" METHOD=POST>");
printf("<a href=\" http://localhost/RIM/projet/\" >"
"</br><img src=\"http://localhost/RIM/projet/blackperl.jpeg\" border=0 width=70 height=70 alt=\"oups\" class=image></a> ");
printf(" <b>BLACK PERL</b> ");
printf(" <INPUT TYPE=TEXT NAME=NOM class = text>");
printf(" <input type=NUMBER name=NB min=1 max = 3204 VALUE=20 class = nombre >");
printf(" <INPUT TYPE=SUBMIT VALUE=Go NAME=Rechercher class = myButton> ");
printf("</FORM>");
}
void print_debug_query(entry *entries, int m, char *qs, int argc, char *argv[]){
int x;
printf("<H3>Éléments de votre requête :</H3>\n");
if (qs == NULL) {
printf("Aucune <code>QUERY_STRING</code> n'a été trouvée<p>\n");
} else {
printf("<code>QUERY_STRING = \"%s\"</code><br>\n",qs);
printf("<code>CONTENT_LENGTH = %zd</code><p>\n",strlen(qs));
}
for (x = 1; x < argc; x++)
printf("<code>argv[%d] = \"%s\"</code><br>\n",x,argv[x]);
if (argc > 1) printf("<br>\n");
if (m)
printf("Vous avez soumis %d paire%s nom/valeur :<p>\n",m,(m == 1) ? "" : "s");
else
printf("Vous n'avez soumis aucune paire nom/valeur.<p>\n");
printf("<UL>\n");
for (x = 0; x < m; x++)
printf("<LI> <code>%s = %s</code>\n",entries[x].name,entries[x].val);
printf("</UL>\n");
for (x = 1; x < argc; x++)
printf("argv[%d]=\"%s\"<br>\n",x,argv[x]);
if (argc == 1) printf ("Pas d'arguments sur la ligne de commande.",x,argv[x]);
printf("<p>\n");
}
void recherche(entry *entries, int m, char *qs, int argc, char *argv[]){
printf("%s\"<br>",qs);
}
void print_html_tail(){
printf("</BODY>\n");
printf("</HTML>\n");
}
void afficherBarreImage(){
//selection image
printf(" <FORM ACTION=\"http://localhost/cgi-bin/RIM/image.cgi\" METHOD=POST>");
printf("<br/><br/><a href=\" http://localhost/RIM/projet/\" >"
"<img src=\"http://localhost/RIM/projet/blackperl.jpeg\" border=0 width=70 height=70 alt=\"oups\"></a> ");
printf(" <b>BLACK PERL</b> ");
printf("<span class=marge> Indice: <input type=NUMBER name=indice min=1 max = 4998 VALUE=1 class = nombrelong > ");
printf(" k: <input type=NUMBER name=nbAff min=1 max = 500 VALUE=10 class = nombre> ");
printf(" <INPUT TYPE=SUBMIT NAME=INFO VALUE=go class = myButton> </span> ");
printf("</FORM> ");
//coin eval:
printf("<span class=menu1><div style=\"text-align:right;\"> <FORM ACTION=\"http://localhost/cgi-bin/RIM/eval.cgi\" METHOD=POST>");
printf(" k:<input type=NUMBER name=KVAL min=1 max = 20 VALUE=10 class=bnum >");
printf(" nbDocs:<input type=NUMBER name=NB min=1 max = 4980 VALUE=50 class=bnum >");
printf(" <INPUT TYPE=SUBMIT NAME=INFO VALUE=EVALUATION class = btop></FORM></div></span>");
printf(" <hr color=#000444 size=3px> <br/> ");
}
/*------------------------------------------------------------------------------------*/
char *get_query_string(int argc, char *argv[]){
int l,cl;
char *qs,*rqm,*tmp;
if ((rqm = getenv("REQUEST_METHOD")) == NULL) {
qs = NULL;
cl = 0;
for (l = 1; l < argc; l++) {
tmp = strdup(argv[l]);
unescape_url(tmp);
cl += strlen(tmp)+1;
qs = (char *) realloc((void *) qs,cl*sizeof(char));
if (l == 1) *qs = 0; else strcat(qs,"&");
strcat(qs,tmp);
free(tmp);
}
} else if (!strcmp(rqm,"GET")) {
qs = getenv("QUERY_STRING");
} else if (!strcmp(rqm,"POST")) {
cl = atoi(getenv("CONTENT_LENGTH"));
qs = (char *) malloc(sizeof(char) * (cl+1));
for (l = 0; (l < cl) && (!feof(stdin)); l++) qs[l] = (char) fgetc(stdin);
qs[l] = 0;
} else {
print_html_head("Request method error");
printf("This script should be referenced with a METHOD of POST or GET.\n");
printf("If you don't understand this, see this ");
printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/");
printf("fill-out-forms/overview.html\">forms overview</A>.%c",10);
print_html_tail();
exit(1);
}
return(qs);
}
entry *get_entries(char *query_string, int *nb_entries)
{
int x,m;
char *qs;
entry *entries;
if ((query_string == NULL) || (query_string[0] == 0)) {
*nb_entries = 0;
return(NULL);
}
qs = strdup(query_string);
*nb_entries = m = countword(qs,'&');
entries = (entry *) malloc(m*sizeof(entry));
for(x = 0; x < m; x++) {
entries[x].val = getword(qs,'&');
plustospace(entries[x].val);
unescape_url(entries[x].val);
entries[x].name = getword(entries[x].val,'=');
}
return(entries);
}
/*------------------------------------------------------------------------------------*/
char *getparam(entry *entries, int m, char *name){
int i;
for (i = 0; i < m; i++) if (!strcmp(entries[i].name,name)) return(entries[i].val);
return(NULL);
}
char **getparams(entry *entries, int m, char *name, int *nb_params){
int i,n;
char **params;
for (i = n = 0; i < m; i++) n += !strcmp(entries[i].name,name);
if (n) {
params = (char **) malloc(n*sizeof(char *));
for (i = n = 0; i < m; i++)
if (!strcmp(entries[i].name,name)) params[n++] = entries[i].val;
return(params);
} else {
return(NULL);
}
}
int testparamvalue(entry *entries, int m, char *name, char *val){
int i;
for (i = 0; i < m; i++)
if (!strcmp(entries[i].name,name) && !strcmp(entries[i].val,val)) return(1);
return(0);
}
/*------------------------------------------------------------------------------------*/