]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/var.c
2 * "$Id: var.c 4869 2005-12-06 02:43:40Z mike $"
4 * CGI form variable and array functions.
6 * Copyright 1997-2005 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cgiCheckVariables() - Check for the presence of "required" variables.
27 * cgiGetArray() - Get an element from a form array...
28 * cgiGetFile() - Get the file (if any) that was submitted in the form.
29 * cgiGetSize() - Get the size of a form array value.
30 * cgiGetVariable() - Get a CGI variable from the database...
31 * cgiInitialize() - Initialize the CGI variable "database"...
32 * cgiIsPOST() - Determine whether this page was POSTed.
33 * cgiSetArray() - Set array element N to the specified string.
34 * cgiSetSize() - Set the array size.
35 * cgiSetVariable() - Set a CGI variable in the database...
36 * cgi_add_variable() - Add a form variable.
37 * cgi_compare_variables() - Compare two variables.
38 * cgi_find_variable() - Find a variable...
39 * cgi_initialize_get() - Initialize form variables using the GET method.
40 * cgi_initialize_multipart() - Initialize variables and file using the POST method.
41 * cgi_initialize_post() - Initialize variables using the POST method.
42 * cgi_initialize_string() - Initialize form variables from a string.
43 * cgi_passwd() - Catch authentication requests and notify the server.
44 * cgi_sort_variables() - Sort all form variables for faster lookup.
45 * cgi_unlink_file() - Remove the uploaded form.
49 #include "cgi-private.h"
54 * Data structure to hold all the CGI form variables and arrays...
57 typedef struct /**** Form variable structure ****/
59 const char *name
; /* Name of variable */
60 int nvalues
, /* Number of values */
61 avalues
; /* Number of values allocated */
62 const char **values
; /* Value(s) of variable */
70 static int form_count
= 0, /* Form variable count */
71 form_alloc
= 0; /* Number of variables allocated */
72 static _cgi_var_t
*form_vars
= NULL
;
74 static cgi_file_t
*form_file
= NULL
;
82 static void cgi_add_variable(const char *name
, int element
,
84 static int cgi_compare_variables(const _cgi_var_t
*v1
,
85 const _cgi_var_t
*v2
);
86 static _cgi_var_t
*cgi_find_variable(const char *name
);
87 static int cgi_initialize_get(void);
88 static int cgi_initialize_multipart(const char *boundary
);
89 static int cgi_initialize_post(void);
90 static int cgi_initialize_string(const char *data
);
91 static const char *cgi_passwd(const char *prompt
);
92 static void cgi_sort_variables(void);
93 static void cgi_unlink_file(void);
97 * 'cgiCheckVariables()' - Check for the presence of "required" variables.
99 * Names may be separated by spaces and/or commas.
102 int /* O - 1 if all variables present, 0 otherwise */
103 cgiCheckVariables(const char *names
) /* I - Variables to look for */
105 char name
[255], /* Current variable name */
106 *s
; /* Pointer in string */
107 const char *val
; /* Value of variable */
108 int element
; /* Array element number */
114 while (*names
!= '\0')
116 while (*names
== ' ' || *names
== ',')
119 for (s
= name
; *names
!= '\0' && *names
!= ' ' && *names
!= ','; s
++, names
++)
126 if ((s
= strrchr(name
, '-')) != NULL
)
129 element
= atoi(s
+ 1) - 1;
130 val
= cgiGetArray(name
, element
);
133 val
= cgiGetVariable(name
);
139 return (0); /* Can't be blank, either! */
147 * 'cgiGetArray()' - Get an element from a form array...
150 const char * /* O - Element value or NULL */
151 cgiGetArray(const char *name
, /* I - Name of array variable */
152 int element
) /* I - Element number (0 to N) */
154 _cgi_var_t
*var
; /* Pointer to variable */
157 if ((var
= cgi_find_variable(name
)) == NULL
)
160 if (var
->nvalues
== 1)
161 return (var
->values
[0]);
163 if (element
< 0 || element
>= var
->nvalues
)
166 return (var
->values
[element
]);
171 * 'cgiGetFile()' - Get the file (if any) that was submitted in the form.
174 const cgi_file_t
* /* O - Attached file or NULL */
182 * 'cgiGetSize()' - Get the size of a form array value.
185 int /* O - Number of elements */
186 cgiGetSize(const char *name
) /* I - Name of variable */
188 _cgi_var_t
*var
; /* Pointer to variable */
191 if ((var
= cgi_find_variable(name
)) == NULL
)
194 return (var
->nvalues
);
199 * 'cgiGetVariable()' - Get a CGI variable from the database...
201 * Returns NULL if the variable doesn't exist. If the variable is an
202 * array of values, returns the last element...
205 const char * /* O - Value of variable */
206 cgiGetVariable(const char *name
) /* I - Name of variable */
208 const _cgi_var_t
*var
; /* Returned variable */
211 var
= cgi_find_variable(name
);
215 printf("cgiGetVariable(\"%s\") is returning NULL...\n", name
);
217 printf("cgiGetVariable(\"%s\") is returning \"%s\"...\n", name
,
218 var
->values
[var
->nvalues
- 1]);
221 return ((var
== NULL
) ? NULL
: var
->values
[var
->nvalues
- 1]);
226 * 'cgiInitialize()' - Initialize the CGI variable "database"...
229 int /* O - Non-zero if there was form data */
232 const char *method
; /* Form posting method */
233 const char *content_type
; /* Content-Type of post data */
237 * Setup a password callback for authentication...
240 cupsSetPasswordCB(cgi_passwd
);
244 * Disable output buffering to find bugs...
247 setbuf(stdout
, NULL
);
248 puts("Content-type: text/plain\n");
252 * Get the request method (GET or POST)...
255 method
= getenv("REQUEST_METHOD");
256 content_type
= getenv("CONTENT_TYPE");
261 * Grab form data from the corresponding location...
264 if (!strcasecmp(method
, "GET"))
265 return (cgi_initialize_get());
266 else if (!strcasecmp(method
, "POST") && content_type
)
268 const char *boundary
= strstr(content_type
, "boundary=");
273 if (content_type
&& !strncmp(content_type
, "multipart/form-data; ", 21))
274 return (cgi_initialize_multipart(boundary
));
276 return (cgi_initialize_post());
284 * 'cgiIsPOST()' - Determine whether this page was POSTed.
287 int /* O - 1 if POST, 0 if GET */
290 const char *method
; /* REQUEST_METHOD environment variable */
293 if ((method
= getenv("REQUEST_METHOD")) == NULL
)
296 return (!strcmp(method
, "POST"));
301 * 'cgiSetArray()' - Set array element N to the specified string.
303 * If the variable array is smaller than (element + 1), the intervening
304 * elements are set to NULL.
308 cgiSetArray(const char *name
, /* I - Name of variable */
309 int element
, /* I - Element number (0 to N) */
310 const char *value
) /* I - Value of variable */
312 int i
; /* Looping var */
313 _cgi_var_t
*var
; /* Returned variable */
316 if (name
== NULL
|| value
== NULL
|| element
< 0 || element
> 100000)
319 if ((var
= cgi_find_variable(name
)) == NULL
)
321 cgi_add_variable(name
, element
, value
);
322 cgi_sort_variables();
326 if (element
>= var
->avalues
)
328 var
->avalues
= element
+ 16;
329 var
->values
= (const char **)realloc((void *)(var
->values
),
330 sizeof(char *) * var
->avalues
);
333 if (element
>= var
->nvalues
)
335 for (i
= var
->nvalues
; i
< element
; i
++)
336 var
->values
[i
] = NULL
;
338 var
->nvalues
= element
+ 1;
340 else if (var
->values
[element
])
341 free((char *)var
->values
[element
]);
343 var
->values
[element
] = strdup(value
);
349 * 'cgiSetSize()' - Set the array size.
353 cgiSetSize(const char *name
, /* I - Name of variable */
354 int size
) /* I - Number of elements (0 to N) */
356 int i
; /* Looping var */
357 _cgi_var_t
*var
; /* Returned variable */
360 if (name
== NULL
|| size
< 0 || size
> 100000)
363 if ((var
= cgi_find_variable(name
)) == NULL
)
366 if (size
>= var
->avalues
)
368 var
->avalues
= size
+ 16;
369 var
->values
= (const char **)realloc((void *)(var
->values
),
370 sizeof(char *) * var
->avalues
);
373 if (size
> var
->nvalues
)
375 for (i
= var
->nvalues
; i
< size
; i
++)
376 var
->values
[i
] = NULL
;
378 else if (size
< var
->nvalues
)
380 for (i
= size
; i
< var
->nvalues
; i
++)
382 free((void *)(var
->values
[i
]));
390 * 'cgiSetVariable()' - Set a CGI variable in the database...
392 * If the variable is an array, this truncates the array to a single element.
396 cgiSetVariable(const char *name
, /* I - Name of variable */
397 const char *value
) /* I - Value of variable */
399 int i
; /* Looping var */
400 _cgi_var_t
*var
; /* Returned variable */
403 if (name
== NULL
|| value
== NULL
)
406 if ((var
= cgi_find_variable(name
)) == NULL
)
408 cgi_add_variable(name
, 0, value
);
409 cgi_sort_variables();
413 for (i
= 0; i
< var
->nvalues
; i
++)
415 free((char *)var
->values
[i
]);
417 var
->values
[0] = strdup(value
);
424 * 'cgi_add_variable()' - Add a form variable.
428 cgi_add_variable(const char *name
, /* I - Variable name */
429 int element
, /* I - Array element number */
430 const char *value
) /* I - Variable value */
432 _cgi_var_t
*var
; /* New variable */
435 if (name
== NULL
|| value
== NULL
|| element
< 0 || element
> 100000)
439 printf("Adding variable \'%s\' with value \'%s\'...\n", name
, value
);
442 if (form_count
>= form_alloc
)
445 form_vars
= malloc(sizeof(_cgi_var_t
) * 16);
447 form_vars
= realloc(form_vars
, (form_alloc
+ 16) * sizeof(_cgi_var_t
));
452 var
= form_vars
+ form_count
;
453 var
->name
= strdup(name
);
454 var
->nvalues
= element
+ 1;
455 var
->avalues
= element
+ 1;
456 var
->values
= calloc(element
+ 1, sizeof(char *));
457 var
->values
[element
] = strdup(value
);
464 * 'cgi_compare_variables()' - Compare two variables.
467 static int /* O - Result of comparison */
468 cgi_compare_variables(
469 const _cgi_var_t
*v1
, /* I - First variable */
470 const _cgi_var_t
*v2
) /* I - Second variable */
472 return (strcasecmp(v1
->name
, v2
->name
));
477 * 'cgi_find_variable()' - Find a variable...
480 static _cgi_var_t
* /* O - Variable pointer or NULL */
481 cgi_find_variable(const char *name
) /* I - Name of variable */
483 _cgi_var_t key
; /* Search key */
486 if (form_count
< 1 || name
== NULL
)
491 return ((_cgi_var_t
*)bsearch(&key
, form_vars
, form_count
, sizeof(_cgi_var_t
),
492 (int (*)(const void *, const void *))cgi_compare_variables
));
497 * 'cgi_initialize_get()' - Initialize form variables using the GET method.
500 static int /* O - 1 if form data read */
501 cgi_initialize_get(void)
503 char *data
; /* Pointer to form data string */
507 puts("Initializing variables using GET method...");
511 * Check to see if there is anything for us to read...
514 data
= getenv("QUERY_STRING");
515 if (data
== NULL
|| strlen(data
) == 0)
519 * Parse it out and return...
522 return (cgi_initialize_string(data
));
527 * 'cgi_initialize_multipart()' - Initialize variables and file using the POST method.
529 * TODO: Update to support files > 2GB.
532 static int /* O - 1 if form data was read */
533 cgi_initialize_multipart(
534 const char *boundary
) /* I - Boundary string */
536 char line
[10240], /* MIME header line */
537 name
[1024], /* Form variable name */
538 filename
[1024], /* Form filename */
539 mimetype
[1024], /* MIME media type */
540 bstring
[256], /* Boundary string to look for */
541 *ptr
, /* Pointer into name/filename */
542 *end
; /* End of buffer */
543 int ch
, /* Character from file */
544 fd
, /* Temporary file descriptor */
545 blen
; /* Length of boundary string */
548 DEBUG_printf(("cgi_initialize_multipart(boundary=\"%s\")\n", boundary
));
551 * Read multipart form data until we run out...
558 snprintf(bstring
, sizeof(bstring
), "\r\n--%s", boundary
);
559 blen
= strlen(bstring
);
561 while (fgets(line
, sizeof(line
), stdin
))
563 if (!strcmp(line
, "\r\n"))
566 * End of headers, grab value...
572 * Read an embedded file...
578 * Remove previous file...
585 * Allocate memory for the new file...
588 if ((form_file
= calloc(1, sizeof(cgi_file_t
))) == NULL
)
591 form_file
->name
= strdup(name
);
592 form_file
->filename
= strdup(filename
);
593 form_file
->mimetype
= strdup(mimetype
);
595 fd
= cupsTempFd(form_file
->tempfile
, sizeof(form_file
->tempfile
));
600 atexit(cgi_unlink_file
);
603 * Copy file data to the temp file...
608 while ((ch
= getchar()) != EOF
)
612 if ((ptr
- line
) >= blen
&& !memcmp(ptr
- blen
, bstring
, blen
))
618 if ((ptr
- line
- blen
) >= 8192)
621 * Write out the first 8k of the buffer...
624 write(fd
, line
, 8192);
625 memmove(line
, line
+ 8192, ptr
- line
- 8192);
631 * Write the rest of the data and close the temp file...
635 write(fd
, line
, ptr
- line
);
642 * Just get a form variable; the current code only handles
643 * form values up to 10k in size...
647 end
= line
+ sizeof(line
) - 1;
649 while ((ch
= getchar()) != EOF
)
654 if ((ptr
- line
) >= blen
&& !memcmp(ptr
- blen
, bstring
, blen
))
664 * Set the form variable...
667 if ((ptr
= strrchr(name
, '-')) != NULL
&& isdigit(ptr
[1] & 255))
670 * Set a specific index in the array...
675 cgiSetArray(name
, atoi(ptr
) - 1, line
);
677 else if (cgiGetVariable(name
))
680 * Add another element in the array...
683 cgiSetArray(name
, cgiGetSize(name
), line
);
688 * Just set the line...
691 cgiSetVariable(name
, line
);
696 * Read the rest of the current line...
699 fgets(line
, sizeof(line
), stdin
);
702 * Clear the state vars...
709 else if (!strncasecmp(line
, "Content-Disposition:", 20))
711 if ((ptr
= strstr(line
+ 20, " name=\"")) != NULL
)
713 strlcpy(name
, ptr
+ 7, sizeof(name
));
715 if ((ptr
= strchr(name
, '\"')) != NULL
)
719 if ((ptr
= strstr(line
+ 20, " filename=\"")) != NULL
)
721 strlcpy(filename
, ptr
+ 11, sizeof(filename
));
723 if ((ptr
= strchr(filename
, '\"')) != NULL
)
727 else if (!strncasecmp(line
, "Content-Type:", 13))
729 for (ptr
= line
+ 13; isspace(*ptr
& 255); ptr
++);
731 strlcpy(mimetype
, ptr
, sizeof(mimetype
));
733 for (ptr
= mimetype
+ strlen(mimetype
) - 1;
734 ptr
> mimetype
&& isspace(*ptr
& 255);
740 * Return 1 for "form data found"...
748 * 'cgi_initialize_post()' - Initialize variables using the POST method.
751 static int /* O - 1 if form data was read */
752 cgi_initialize_post(void)
754 char *content_length
, /* Length of input data (string) */
755 *data
; /* Pointer to form data string */
756 int length
, /* Length of input data */
757 nbytes
, /* Number of bytes read this read() */
758 tbytes
, /* Total number of bytes read */
759 status
; /* Return status */
763 puts("Initializing variables using POST method...");
767 * Check to see if there is anything for us to read...
770 content_length
= getenv("CONTENT_LENGTH");
771 if (content_length
== NULL
|| atoi(content_length
) <= 0)
775 * Get the length of the input stream and allocate a buffer for it...
778 length
= atoi(content_length
);
779 data
= malloc(length
+ 1);
785 * Read the data into the buffer...
788 for (tbytes
= 0; tbytes
< length
; tbytes
+= nbytes
)
789 if ((nbytes
= read(0, data
+ tbytes
, length
- tbytes
)) < 0)
802 status
= cgi_initialize_string(data
);
805 * Free the data and return...
815 * 'cgi_initialize_string()' - Initialize form variables from a string.
818 static int /* O - 1 if form data was processed */
819 cgi_initialize_string(const char *data
) /* I - Form data string */
821 int done
; /* True if we're done reading a form variable */
822 char *s
, /* Pointer to current form string */
823 ch
, /* Temporary character */
824 name
[255], /* Name of form variable */
825 value
[65536]; /* Variable value... */
836 * Loop until we've read all the form data...
839 while (*data
!= '\0')
842 * Get the variable name...
845 for (s
= name
; *data
!= '\0'; data
++)
848 else if (*data
>= ' ' && s
< (name
+ sizeof(name
) - 1))
858 * Read the variable value...
861 for (s
= value
, done
= 0; !done
&& *data
!= '\0'; data
++)
864 case '&' : /* End of data... */
868 case '+' : /* Escaped space character */
869 if (s
< (value
+ sizeof(value
) - 1))
873 case '%' : /* Escaped control character */
875 * Read the hex code...
878 if (s
< (value
+ sizeof(value
) - 1))
896 default : /* Other characters come straight through */
897 if (*data
>= ' ' && s
< (value
+ sizeof(value
) - 1))
902 *s
= '\0'; /* nul terminate the string */
905 * Remove trailing whitespace...
911 while (s
>= value
&& *s
== ' ')
915 * Add the string to the variable "database"...
918 if ((s
= strrchr(name
, '-')) != NULL
&& isdigit(s
[1] & 255))
922 cgiSetArray(name
, atoi(s
) - 1, value
);
924 else if (cgiGetVariable(name
) != NULL
)
925 cgiSetArray(name
, cgiGetSize(name
), value
);
927 cgiSetVariable(name
, value
);
935 * 'cgi_passwd()' - Catch authentication requests and notify the server.
937 * This function sends a Status header and exits, forcing authentication
941 static const char * /* O - NULL (no return) */
942 cgi_passwd(const char *prompt
) /* I - Prompt (not used) */
946 fprintf(stderr
, "DEBUG: cgi_passwd(prompt=\"%s\") called!\n", prompt
);
949 * Send a 401 (unauthorized) status to the server, so it can notify
950 * the client that authentication is required.
953 puts("Status: 401\n");
957 * This code is never executed, but is present to satisfy the compiler.
965 * 'cgi_sort_variables()' - Sort all form variables for faster lookup.
969 cgi_sort_variables(void)
975 puts("Sorting variables...");
981 qsort(form_vars
, form_count
, sizeof(_cgi_var_t
),
982 (int (*)(const void *, const void *))cgi_compare_variables
);
985 puts("Sorted variable list is:");
986 for (i
= 0; i
< form_count
; i
++)
987 printf("%d: %s (%d) = \"%s\" ...\n", i
, form_vars
[i
].name
,
988 form_vars
[i
].nvalues
, form_vars
[i
].values
[0]);
994 * 'cgi_unlink_file()' - Remove the uploaded form.
998 cgi_unlink_file(void)
1003 * Remove the temporary file...
1006 unlink(form_file
->tempfile
);
1009 * Free memory used...
1012 free(form_file
->name
);
1013 free(form_file
->filename
);
1014 free(form_file
->mimetype
);
1023 * End of "$Id: var.c 4869 2005-12-06 02:43:40Z mike $".