cgilib-0.6/0000755000076500001440000000000010776143041011505 5ustar joeyuserscgilib-0.6/CHANGES0000644000076500001440000000321510776116441012505 0ustar joeyusersVersion 0.6, 2008/04/06 . Support for ';' as delimiter . Properly return NULL when no value was found . Support for multipart/form-data . Support for file upload . Added support for inclusion into C++ . Declare some arguments const (Neil Spring) . Decode variable names and data Version 0.5, 1999/08/20 . Added proper install target to main Makefile . Corrected multiple fields code . Reworked offline parser, thanks to Mikko . Improved test suite . Added cgiGetVariables() to return a list of all variables . Fixed problem with name being a prefix of another . Also decode HTTP strings for multiple fields . Added cgiSetType() and cgiSetHeader() with documentation . Added support for HTML Cookies, new routines cgiGetCookie() and cgiGetCookies(), see manpages . Added cgiFree() to free internal datastructures . Internal reorganization . CAUTION: s_cgi **cgi is now s_cgi *cgi in programs . Added cgiFreeList() to free zero terminated lists of arrays Version 0.4, 1998/03/20 . Initial and fixed versions. . Corrected html code in redirection . Corrected number of bytes in redirection Version 0.3, 1998/02/23 . Fixed little mistake in offline mode . Included another example, jumpto Version 0.2, 1998/02/18 . Corrected debugging . Added cgiRedirect() . Added cgiRedirect(3) manpage . Added call to cgiRedirect() to example cgitest.c . Added compile notice to cgitest.c . Added support for the GET method . Reorganized POST/GET/stdin support . Implemented support for multiple fields and updated manpage Version 0.1, 1998/02/15 . Initial version . DFSG Free CGI library . Very small package . Contains all important manpages cgilib-0.6/COPYING0000644000076500001440000004310310776115531012544 0ustar joeyusers GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. cgilib-0.6/CREDITS0000644000076500001440000000071310745074564012537 0ustar joeyusersThis is a list of people who have helped me working on this CGI library. Martin Schulze - Design, Implementation, Maintenance Mikko Torni - Cookie support (abandoned though) - Bugfixes and improvements Laszlo Zavaleta - Support for ';' as delimiter Stephen Uitti - Spelling corrections Neil Spring - Declare some arguments const cgilib-0.6/Makefile0000644000076500001440000000254710776115515013162 0ustar joeyusers# Copyright (c) 1998,9,2001,7,8 by Martin Schulze # # 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. CFLAGS = -Wall -O2 -g IFLAGS = -I. LDFLAGS = -L. #LIBS = -lcgi OBJS = cgi.o cookies.o aux.o CC += $(IFLAGS) libcgi.a: $(OBJS) ar rc $@ $^ cgitest: cgitest.o libcgi.a $(CC) $(CFLAGS) $(IFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) jumpto: jumpto.o libcgi.a $(CC) $(CFLAGS) $(IFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) all: libcgi.a cgitest jumpto install: cgitest install -m 755 cgitest /usr/lib/cgi-bin clean: rm -f cgitest cgitest.o jumpto jumpto.o libcgi.a $(OBJS) *.[35].html htmlman: for f in *.[35]; do \ man -l $$f|rman -f HTML --title $$f -r "%s.%s.html" > $$f.html; \ done cgilib-0.6/aux.c0000644000076500001440000000614610776115515012462 0ustar joeyusers/* aux.c - Auxilliary code for CGI library Copyright (C) 2007,8 by Martin Schulze 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include #include #include int cgiDebugLevel = 0; int cgiDebugType = 1; char *cgiEscape (char *string) { char *cp, *np; char *buf; size_t len; for (cp=string, len=0; *cp; cp++) { switch (*cp) { case '&': len+=5; break; case '<': case '>': len+=4; break; default: len++; break; } } if (len == strlen(string)) return strdup(string); if ((buf = (char *)malloc(len+1)) == NULL) return NULL; for (cp=string, np=buf; *cp; cp++) { switch (*cp) { case '&': *np++ = '&'; *np++ = 'a'; *np++ = 'm'; *np++ = 'p'; *np++ = ';'; break; case '<': *np++ = '&'; *np++ = 'l'; *np++ = 't'; *np++ = ';'; break; case '>': *np++ = '&'; *np++ = 'g'; *np++ = 't'; *np++ = ';'; break; default: *np++ = *cp; break; } } *np = '\0'; return buf; } void cgiDebugOutput (int level, char *format, ...) { va_list args; if (level <= cgiDebugLevel) { va_start (args, format); switch (cgiDebugType) { case 0: printf ("
\n");
	  vprintf (format, args);
	  printf ("\n
\n"); break; case 1: vfprintf (stderr, format, args); printf ("\n"); break; case 2: vsyslog (LOG_DEBUG, format, args); break; } va_end (args); } } #define BUFSIZE 128 char *cgiGetLine (FILE *stream) { static char *line = NULL; static size_t size = 0; char buf[BUFSIZE]; char *cp; if (!line) { if ((line = (char *)malloc (BUFSIZE)) == NULL) return NULL; size = BUFSIZE; } line[0] = '\0'; while (!feof (stream)) { if ((cp = fgets (buf, sizeof (buf), stream)) == NULL) return NULL; if (strlen(line)+strlen(buf)+1 > size) { if ((cp = (char *)realloc (line, size + BUFSIZE)) == NULL) return line; size += BUFSIZE; line = cp; } strcat (line, buf); if (line[strlen(line)-1] == '\n') { line[strlen(line)-1] = '\0'; if (line[strlen(line)-1] == '\r') line[strlen(line)-1] = '\0'; cgiDebugOutput (4, "Read line '%s'", line); return line; } } return NULL; } /* * Local variables: * c-indent-level: 4 * c-basic-offset: 4 * tab-width: 8 * End: */ cgilib-0.6/aux.h0000644000076500001440000000177610776115515012473 0ustar joeyusers/* aux.h - Auxilliary code for CGI library Copyright (C) 2007,8 by Martin Schulze 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ extern int cgiDebugLevel; extern int cgiDebugType; char *cgiEscape (char *string); void cgiDebugOutput (int level, char *format, ...); char *cgiGetLine (FILE *stream); cgilib-0.6/cgi.50000644000076500001440000000751710776115515012354 0ustar joeyusers.\" cgi - Common Gateway Interface .\" Copyright (c) 1998,9,2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgi 5 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgi \- Common Gateway Interface .SH DESCRIPTION The Common Gateway Interface is a way to create dynamic web pages. It defines rules for interaction between a program and the web server while the server talks to the client. There are some ways to use it. .SH "ENVIRONMENT" Normally the webserver sets several environment variables to give some information to the CGI program so it can determine various stuff. .TP .B AUTH_TYPE This reflects the authentification method used to validate a user. .TP .B CONTENT_LENGTH The length of the data in bytes passed to the CGI program through standard input. This is used by the POST method. .TP .B CONTENT_TYPE The MIME type of the query data, such as "text/html", optional. .TP .B DOCUMENT_ROOT This reflects the document root directory of the webserver. .TP .B GATEWAY_INTERFACE Reflects the version of the Common Gateway Interface that the server is using .TP .B HTTP_ACCEPT A comma separated list of MIME type that the client is willing to accept. .TP .B HTTP_FROM The email address of the user issuing the information request. This is not supported by most browsers. .TP .B HTTP_REFERER Reflects the URL from which this CGI program was accessed. .TP .B HTTP_USER_AGENT The name, version and libraries of the browser making the request. This information can be used to determine if the browser is capable of graphics and is able to display frames and tables. .TP .B PATH_INFO This shows extra information that was passed to the CGI program via command line. Normally it's empty or non-existent. .TP .B PATH_TRANSLATED The translated path on the local filesystem. .TP .B QUERY_STRING This variable refers to additional arguments that were appended to the CGI program - normally with the '?' sign. .TP .B REMOTE_ADDR This refers to the host from which the information request was issued, as IP number. .TP .B REMOTE_HOST This refers to the host from which the information request was issued. .TP .B REMOTE_USER The authenticated name of the user. .TP .B REQUEST_METHOD This refers to the method with which the information request was issued. Normally this is either GET or POST. .TP .B SCRIPT_NAME The virtual name of the script being executed. .TP .B SERVER_NAME The server's hostname or IP number. This may be used to determine the correct paths or resulting HTML code for CGI programs that are used on the same machine for several servers. .TP .B SERVER_PROTOCOL This is the name and version of the information protocol the request came in with. Normally this is "HTTP/1.0" or "HTTP/1.1". .TP .B SERVER_PORT This refers to the TCP/IP port on which the webserver is running. .TP .B SERVER_SOFTWARE This reflects the name and revision of the webserver software. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiDebug (3), .BR cgiHeader (3), .BR cgiSetType (3), .BR cgiSetHeader (3), .BR cgiGetValue (3), .BR cgiGetVariables (3). cgilib-0.6/cgi.c0000644000076500001440000004677410776115515012442 0ustar joeyusers/* cgi.c - Some simple routines for CGI programming Copyright (c) 1996-9,2007,8 Martin Schulze 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _GNU_SOURCE 1 #include #include #include #include #include #include #include #include #include "aux.h" char *cgiHeaderString = NULL; char *cgiType = NULL; extern s_cookie **cgiReadCookies(); int cgiSetHeader (const char *name, const char *value) { const char *cp, *vp; char *pivot; int len; if (!name || !strlen (name) || !value || !strlen (value)) return 0; for (cp=name;*cp && *cp!=' ' && *cp!='\r' && *cp!='\n' && *cp!=':';cp++); for (vp=value;*vp && *vp!='\r' && *vp!='\n';vp++); if (cgiHeaderString) { len = (strlen (cgiHeaderString) + cp-name + vp-value + 5) * sizeof (char); if ((pivot = (char *)realloc (cgiHeaderString,len)) == NULL) return 0; cgiHeaderString = pivot; pivot += strlen (cgiHeaderString); } else { len = (cp-name + vp-value + 5) * sizeof (char); if ((cgiHeaderString = (char *)malloc (len)) == NULL) return 0; pivot = cgiHeaderString; } strncpy (pivot, name, cp-name); strncat (pivot, ": ", 2); strncat (pivot, value, vp-value); strncat (pivot, "\r\n", 2); return 1; } int cgiSetType (const char *type) { int len; const char *cp; if (!type || !strlen (type)) return 0; if (cgiType) free (cgiType); for (cp=type;*cp && *cp!='\r' && *cp!='\n';cp++); len = (cp-type+1) * sizeof (char); if ((cgiType = (char *)malloc (len+20)) == NULL) return 0; memset (cgiType, 0, len); strncpy (cgiType, type, cp-type); return 1; } void cgiHeader () { if (cgiType) printf ("Content-type: %s\r\n", cgiType); else printf ("Content-type: text/html\r\n"); if (cgiHeaderString) printf ("%s", cgiHeaderString); printf ("\r\n"); } void cgiDebug (int level, int where) { if (level > 0) cgiDebugLevel = level; else cgiDebugLevel = 0; if (where > 0) { if (where < 3) { cgiDebugType = where; if (where == 2) openlog("cgilib", LOG_PID, LOG_USER); } else cgiDebugType = 0; } } char *cgiDecodeString (char *text) { char *cp, *xp; for (cp=text,xp=text; *cp; cp++) { if (*cp == '%') { if (strchr("0123456789ABCDEFabcdef", *(cp+1)) && strchr("0123456789ABCDEFabcdef", *(cp+2))) { if (islower(*(cp+1))) *(cp+1) = toupper(*(cp+1)); if (islower(*(cp+2))) *(cp+2) = toupper(*(cp+2)); *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0'); xp++;cp+=2; } } else { *(xp++) = *cp; } } memset(xp, 0, cp-xp); return text; } /* cgiReadFile() * * Read and save a file fro a multipart request */ #include char *cgiReadFile (FILE *stream, char *boundary) { char *crlfboundary, *buf; size_t boundarylen; int c; unsigned int pivot; char *cp; char template[]= "/tmp/cgilibXXXXXX"; FILE *tmpfile; int fd; boundarylen = strlen(boundary)+3; if ((crlfboundary = (char *)malloc (boundarylen)) == NULL) return NULL; sprintf (crlfboundary, "\r\n%s", boundary); if ((buf = (char *)malloc (boundarylen)) == NULL) { free (crlfboundary); return NULL; } memset (buf, 0, boundarylen); pivot = 0; if ((fd = mkstemp (template)) == -1) { free (crlfboundary); free (buf); return NULL; } if ((tmpfile = fdopen (fd, "w")) == NULL) { free (crlfboundary); free (buf); unlink (template); return NULL; } while (!feof (stream)) { c = fgetc (stream); if (c == 0) { if (strlen (buf)) { for (cp=buf; *cp; cp++) putc (*cp, tmpfile); memset (buf, 0, boundarylen); pivot = 0; } putc (c, tmpfile); continue; } if (strlen (buf)) { if (crlfboundary[pivot+1] == c) { buf[++pivot] = c; if (strlen (buf) == strlen (crlfboundary)) break; else continue; } else { for (cp=buf; *cp; cp++) putc (*cp, tmpfile); memset (buf, 0, boundarylen); pivot = 0; } } if (crlfboundary[0] == c) { buf[0] = c; } else { fputc (c, tmpfile); } } if (!feof (stream)) fgets (buf, boundarylen, stream); fclose (tmpfile); free (crlfboundary); free (buf); return strdup (template); } /* cgiReadMultipart() * * Decode multipart/form-data */ #define MULTIPART_DELTA 5 s_cgi *cgiReadMultipart (char *boundary) { char *line; char *cp, *xp; char *name = NULL, *type = NULL; char *fname = NULL; char *tmpfile; int header = 1; s_var **result = NULL; s_var **tmp; int numresults = 0, current = 0; s_file **files = NULL; s_file **tmpf; s_file *file; int index = 0; size_t len; s_cgi *res; while ((line = cgiGetLine (stdin)) != NULL) { if (!strncmp (line, boundary, strlen(boundary))) { header = 1; } else if (!strncasecmp (line, "Content-Disposition: form-data; ", 32)) { if (!name) { if ((cp = strstr (line, "name=\"")) == NULL) continue; cp += 6; if ((xp = strchr (cp, '\"')) == NULL) continue; name = strndup (cp, xp-cp); cgiDecodeString (name); cgiDebugOutput (2, "Found field name %s", name); if ((cp = strstr (line, "filename=\"")) == NULL) continue; cp += 10; if ((xp = strchr (cp, '\"')) == NULL) continue; fname = strndup (cp, xp-cp); cgiDecodeString (fname); cgiDebugOutput (2, "Found filename %s", fname); } } else if (!strncasecmp (line, "Content-Type: ", 14)) { if (!type) { cp = line + 14; type = strdup (cp); cgiDebugOutput (2, "Found mime type %s", type); } } else if (header) { if (!strlen(line)) { header = 0; if (fname) { header = 1; tmpfile = cgiReadFile (stdin, boundary); if (!tmpfile) { free (name); free (fname); if (type) free (type); name = fname = type = NULL; } cgiDebugOutput (2, "Wrote %s (%s) to file: %s", name, fname, tmpfile); if (!strlen (fname)) { cgiDebugOutput (3, "Found empty filename, removing"); unlink (tmpfile); free (tmpfile); free (name); free (fname); if (type) free (type); name = fname = type = NULL; } else { if ((file = (s_file *)malloc (sizeof (s_file))) == NULL) { cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname); unlink (tmpfile); free (tmpfile); free (name); free (fname); if (type) free (type); name = fname = type = NULL; continue; } file->name = name; file->type = type; file->tmpfile = tmpfile; if ((cp = rindex (fname, '/')) == NULL) file->filename = fname; else { file->filename = strdup (++cp); free (fname); } name = type = fname = NULL; if (!files) { if ((files = (s_file **)malloc(2*sizeof (s_file *))) == NULL) { cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname); unlink (tmpfile); free (tmpfile); free (name); name = NULL; if (type) { free (type); type = NULL; } free (file->filename); free (file); continue; } memset (files, 0, 2*sizeof (s_file *)); index = 0; } else { for (index=0; files[index]; index++); if ((tmpf = (s_file **)realloc(files, (index+2)*sizeof (s_file *))) == NULL) { cgiDebugOutput (3, "realloc failed, ignoring %s=%s", name, fname); unlink (tmpfile); free (tmpfile); free (name); if (type) free (type); free (file->filename); free (file); name = type = fname = NULL; continue; } files = tmpf; memset (files + index, 0, 2*sizeof (s_file *)); } files[index] = file; } } } } else { if (name) { /* try to find out if there's already such a variable */ for (index=0; indexname,name); index++); if (index == current) { if (!result) { len = MULTIPART_DELTA * sizeof (s_var *); if ((result = (s_var **)malloc (len)) == NULL) { free (name); if (type) free (type); return NULL; } numresults = MULTIPART_DELTA; memset (result, 0, len); current = 0; } else { if (current+2 > numresults) { len = (numresults + MULTIPART_DELTA) * sizeof(s_var *); if ((tmp = (s_var **)realloc (result, len)) == NULL) { for (index=0; result[index]; index++) free (result[index]); free (result); free (name); if (type) free (type); return NULL; } result = tmp; memset (result + numresults, 0, len - numresults*sizeof(s_var *)); numresults += MULTIPART_DELTA; } } if ((result[current] = (s_var *)malloc(sizeof(s_var))) == NULL) { for (index=0; result[index]; index++) free (result[index]); free (result); free (name); if (type) free (type); return NULL; } current++; cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line); result[index]->name = name; name = NULL; result[index]->value = strdup (line); cgiDecodeString (result[index]->value); if (type) { free (type); type = NULL; } } else { cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line); free (name); name = NULL; if ((cp = (char *)realloc (result[index]->value, strlen(result[index]->value)+strlen(line)+2)) != NULL) { strcat(cp, "\n"); strcat(cp, line); result[index]->value = cp; if (type) { free (type); type = NULL; } } } } else { if (index > 0) { xp = strdup (line); cgiDecodeString (xp); if ((name = (char *)malloc (strlen(result[index]->value)+strlen(xp)+3)) == NULL) { for (index=0; result[index]; index++) free (result[index]); free (result); free (xp); return NULL; } sprintf (name, "%s\r\n%s", result[index]->value, xp); free (result[index]->value); result[index]->value = name; name = NULL; free (xp); } } } } if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL) return NULL; res->vars = result; res->cookies = NULL; res->files = files; return res; } /* cgiReadVariables() * * Read from stdin if no string is provided via CGI. Variables that * doesn't have a value associated with it doesn't get stored. */ s_cgi *cgiReadVariables () { int length; char *line = NULL; int numargs; char *cp, *ip, *esp, *sptr; s_var **result; int i, k, len; char tmp[101]; s_cgi *res; cp = getenv("CONTENT_TYPE"); if (cp) cgiDebugOutput (2, "Content-Type: %s", cp); if (cp && strstr(cp, "multipart/form-data") && strstr(cp, "boundary=")) { cp = strstr(cp, "boundary=") + strlen ("boundary=") - 2; *cp = *(cp+1) = '-'; return cgiReadMultipart (cp); } if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL) return NULL; cp = getenv("REQUEST_METHOD"); if (cp) cgiDebugOutput (2, "REQUEST_METHOD: %s", cp); ip = getenv("CONTENT_LENGTH"); if (ip) cgiDebugOutput (2, "CONTENT_LENGTH: %s", ip); if (cp && !strcmp(cp, "POST")) { if (ip) { length = atoi(ip); if (length <= 0) return NULL; if ((line = (char *)malloc (length+2)) == NULL) return NULL; fgets(line, length+1, stdin); } else return NULL; } else if (cp && !strcmp(cp, "GET")) { esp = getenv("QUERY_STRING"); if (esp && strlen(esp)) { if ((line = (char *)malloc (strlen(esp)+2)) == NULL) return NULL; sprintf (line, "%s", esp); } else return NULL; } else { length = 0; printf ("(offline mode: enter name=value pairs on standard input)\n"); memset (tmp, 0, sizeof(tmp)); while((cp = fgets (tmp, 100, stdin)) != NULL) { if (strlen(tmp)) { if (tmp[strlen(tmp)-1] == '\n') tmp[strlen(tmp)-1] = '&'; if (length) { length += strlen(tmp); len = (length+1) * sizeof(char); if ((line = (char *)realloc (line, len)) == NULL) return NULL; strcat (line, tmp); } else { length = strlen(tmp); len = (length+1) * sizeof(char); if ((line = (char *)malloc (len)) == NULL) return NULL; memset (line, 0, len); strcpy (line, tmp); } } memset (tmp, 0, sizeof(tmp)); } if (!line) return NULL; if (line[strlen(line)-1] == '&') line[strlen(line)-1] = '\0'; } /* * From now on all cgi variables are stored in the variable line * and look like foo=bar&foobar=barfoo&foofoo= */ cgiDebugOutput (1, "Received CGI input: %s", line); for (cp=line; *cp; cp++) if (*cp == '+') *cp = ' '; if (strlen(line)) { for (numargs=1,cp=line; *cp; cp++) if (*cp == '&' || *cp == ';' ) numargs++; } else numargs = 0; cgiDebugOutput (1, "%d cgi variables found.", numargs); len = (numargs+1) * sizeof(s_var *); if ((result = (s_var **)malloc (len)) == NULL) return NULL; memset (result, 0, len); cp = line; i=0; while (*cp) { if ((ip = (char *)strchr(cp, '&')) != NULL) { *ip = '\0'; } else if ((ip = (char *)strchr(cp, ';')) != NULL) { *ip = '\0'; } else ip = cp + strlen(cp); if ((esp=(char *)strchr(cp, '=')) == NULL) { cp = ++ip; continue; } if (!strlen(esp)) { cp = ++ip; continue; } if (iname, name); k++); if (k == i) { /* No such variable yet */ if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL) return NULL; result[i]->name = name; result[i]->value = value; cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value); i++; } else { /* There is already such a name, suppose a mutiple field */ free (name); len = (strlen(result[k]->value)+strlen(value)+2) * sizeof (char); if ((sptr = (char *)malloc(len)) == NULL) { free (value); return NULL; } memset (sptr, 0, len); sprintf (sptr, "%s\n%s", result[k]->value, value); free (result[k]->value); free (value); result[k]->value = sptr; cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value); } } cp = ++ip; } res->vars = result; res->cookies = NULL; res->files = NULL; return res; } /* cgiInit() * * Read from stdin if no string is provided via CGI. Variables that * doesn't have a value associated with it doesn't get stored. */ s_cgi *cgiInit() { s_cgi *res; res = cgiReadVariables (); if (res) res->cookies = cgiReadCookies (); else return NULL; if (!res->vars && !res->cookies && !res->files) { free (res); return NULL; } return res; } char *cgiGetValue (s_cgi *parms, const char *name) { int i; if (!parms || !parms->vars) return NULL; for (i=0;parms->vars[i]; i++) if (!strcmp(name,parms->vars[i]->name)) { cgiDebugOutput (1, "%s found as %s", name, parms->vars[i]->value); if (strlen(parms->vars[i]->value) > 0) return parms->vars[i]->value; else return NULL; } cgiDebugOutput (1, "%s not found", name); return NULL; } char **cgiGetVariables (s_cgi *parms) { int i; char **res = NULL; int len; if (!parms || !parms->vars) return NULL; for (i=0;parms->vars[i]; i++); len = sizeof (char *) * ++i; if ((res = (char **)malloc (len)) == NULL) return NULL; memset (res, 0, len); for (i=0;parms->vars[i]; i++) { len = strlen (parms->vars[i]->name) +1; if ((res[i] = (char *)malloc (len)) == NULL) return NULL; memset (res[i], 0, len); strcpy (res[i], parms->vars[i]->name); } return res; } /* cgiGetFiles * * Returns a list of names of all files. */ char **cgiGetFiles (s_cgi *parms) { int i; char **res = NULL; int len; if (!parms || !parms->files) return NULL; for (i=0;parms->files[i]; i++); len = sizeof (char *) * ++i; if ((res = (char **)malloc (len)) == NULL) return NULL; memset (res, 0, len); for (i=0;parms->files[i]; i++) { len = strlen (parms->files[i]->name) +1; if ((res[i] = (char *)malloc (len)) == NULL) return NULL; memset (res[i], 0, len); strcpy (res[i], parms->files[i]->name); } return res; } /* cgiGetFile * * Return data structure for CGI file variable */ s_file *cgiGetFile (s_cgi *parms, const char *name) { int i; if (!parms || !parms->files) return NULL; for (i=0;parms->files[i]; i++) if (!strcmp(name,parms->files[i]->name)) { cgiDebugOutput (1, "%s found as %s", name, parms->files[i]->filename); return parms->files[i]; } cgiDebugOutput (1, "%s not found", name); return NULL; } void cgiRedirect (const char *url) { if (url && strlen(url)) { printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2)); printf ("Status: 302 Temporal Relocation\r\n"); printf ("Location: %s\r\n\r\n", url); printf ("\n\nThe page has been moved to %s\n\n\n", url, url); } } void cgiFreeList (char **list) { int i; for (i=0; list[i] != NULL; i++) free (list[i]); free (list); } void cgiFree (s_cgi *parms) { int i; if (!parms) return; if (parms->vars) { for (i=0;parms->vars[i]; i++) { if (parms->vars[i]->name) free (parms->vars[i]->name); if (parms->vars[i]->value) free (parms->vars[i]->value); free (parms->vars[i]); } free (parms->vars); } if (parms->cookies) { if (parms->cookies[0]->version) free (parms->cookies[0]->version); for (i=0;parms->cookies[i]; i++) { if (parms->cookies[i]->name) free (parms->cookies[i]->name); if (parms->cookies[i]->value) free (parms->cookies[i]->value); if (parms->cookies[i]->path) free (parms->cookies[i]->path); if (parms->cookies[i]->domain) free (parms->cookies[i]->domain); free (parms->cookies[i]); } free (parms->cookies); } if (parms->files) { for (i=0;parms->files[i]; i++) { if (parms->files[i]->name) free (parms->files[i]->name); if (parms->files[i]->type) free (parms->files[i]->type); if (parms->files[i]->filename) free (parms->files[i]->filename); if (parms->files[i]->tmpfile) { unlink (parms->files[i]->tmpfile); free (parms->files[i]->tmpfile); } free (parms->files[i]); } free (parms->files); } free (parms); if (cgiHeaderString) { free (cgiHeaderString); cgiHeaderString = NULL; } if (cgiType) { free (cgiType); cgiType = NULL; } } /* * Local variables: * c-indent-level: 4 * c-basic-offset: 4 * tab-width: 8 * End: */ cgilib-0.6/cgi.h0000644000076500001440000000564710776115516012442 0ustar joeyusers/* cgi.h - Routines for CGI programming Copyright (c) 1996-8,2007,8 Martin Schulze 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifndef _CGI_H_ #define _CGI_H_ #ifdef __cplusplus extern "C" { #endif typedef struct var_s { char *name, *value; } s_var; typedef struct cookie_s { char *version, *name, *value, *path, *domain; } s_cookie; typedef struct file_s { char *name, *type, *filename, *tmpfile; } s_file; typedef struct cgi_s { s_var **vars; s_cookie **cookies; s_file **files; } s_cgi; /* cgiSetHeader * * Sets additional HTTP header lines to be printed with cgiHeader */ int cgiSetHeader (const char *name, const char *value); /* cgiSetType * * Sets result type for HTTP */ int cgiSetType (const char *type); /* cgiHeader * * Prints a valid CGI Header (Content-type...) etc. */ void cgiHeader (); /* cgiDebug * * Set/unsets debugging */ void cgiDebug (int level, int where); /* cgiInit * * Reads in variables set via POST or stdin, reads HTTP Cookies. */ s_cgi *cgiInit (); /* cgiGetValue * * Returns the value of the specified variable or NULL if it's empty * or doesn't exist. */ char *cgiGetValue (s_cgi *parms, const char *name); /* cgiGetVariables * * Returns the names of all form variables. */ char **cgiGetVariables (s_cgi *parms); /* cgiRedirect * * Provides a valid redirect for web pages. */ void cgiRedirect (const char *url); /* cgiGetCookie * * Returns the cookie referenced by the given name or NULL if it * doesn't exist or is empty. */ s_cookie *cgiGetCookie (s_cgi *parms, const char *name); /* cgiGetCookies * * Returns a list of names of all cookies. */ char **cgiGetCookies (s_cgi *parms); /* cgiGetFiles * * Returns a list of names of all files. */ char **cgiGetFiles (s_cgi *parms); /* cgiGetFile * * Return data structure for CGI file variable */ s_file *cgiGetFile (s_cgi *parms, const char *name); /* cgiFreeList * * Frees a list as returned by cgiGetVariables() and cgiGetCookies() */ void cgiFreeList (char **list); /* cgiFree * * Frees the internal data structures */ void cgiFree (s_cgi *parms); /* cgiEscape * * Escapes <&> in a string */ char *cgiEscape (char *string); #ifdef __cplusplus extern } #endif #endif /* _CGI_H_ */ cgilib-0.6/cgiDebug.30000644000076500001440000000332210776115516013310 0ustar joeyusers.\" cgiDebug - Set the debug level for cgi programming .\" Copyright (c) 1998,9,2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiDebug 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiDebug \- Set the debug level for CGI programming .SH SYNOPSYS .nf .B #include .sp .BI "void cgiDebug (int " level ", int " where ); .fi .SH DESCRIPTION This routine controls debugging for the cgi library. At the moment only level 0 (default, no debugging) and 1 (debugging enabled) are supported. The second argument .I where specifies if debug output should be written to stdout using HTML (1), to stderr as plain text (0, default), or to syslog (2). This shoud be the first one called from the CGI library. .SH "RETURN VALUE" .BR cgiDebug () does not return a value. .SH "AUTHOR" This CGi library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiInit (3), .BR cgiHeader (3), .BR cgiGetValue (3). cgilib-0.6/cgiEscape.30000644000076500001440000000327510776115516013471 0ustar joeyusers.\" cgiDebug - Set the debug level for CGI programming .\" Copyright (c) 2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiDebug 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiEscape \- HTML escape certain characters in a string .SH SYNOPSYS .nf .B #include .sp .BI "char *cgiEscape (char *" string ); .fi .SH DESCRIPTION This function returns a pointer to a sanitised string. It converts .BR < ", " & " and " > into HTML entities so that the result can be displayed without any danger of cross-site scripting in a browser. The result may be passed to .BR free (3) after use. This routine is meant to be called before any user provided strings are returned to the browser. .SH "RETURN VALUE" .BR cgiEscape () returns a pointer to the sanitised string or NULL in case of error. .SH "AUTHOR" This CGi library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR free (3). cgilib-0.6/cgiFree.30000644000076500001440000000326710776115516013153 0ustar joeyusers.\" cgiFree - Frees the data structures .\" Copyright (c) 1999,2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiFree 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiFree \- Frees CGI data structure .SH SYNOPSYS .nf .B #include .sp .BI "void cgiFree (s_cgi *" parms ); .fi .SH DESCRIPTION This routine frees memory allocated by CGI parsing routines. Temporary files from CGI file upload are also removed. This is the opposite of .BR cgiInit (3). After this routine was called, no variables, cookies, files and no additional header lines are available. The result type is also reset to "text/html". .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiInit (3), .BR cgiDebug (3), .BR cgiHeader (3), .BR cgiSetHeader (3), .BR cgiSetType (3), .BR cgiGetValue (3), .BR cgiGetVariables (3) .BR cgiGetCookie (3), .BR cgiGetCookies (3). cgilib-0.6/cgiFreeList.30000644000076500001440000000274010776115516014002 0ustar joeyusers.\" cgiFreeList - Frees memory used by a list .\" Copyright (c) 1999,2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiFreeList 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiFreeList \- Frees memory used by a list .SH SYNOPSYS .nf .B #include .sp .BI "void cgiFreeList (char **" list ); .fi .SH DESCRIPTION A zero terminated list of characters is returned by the routines .BR cgiGetVariables (3), .BR cgiGetCookies (3), and .BR cgiGetFiles (3). This routine frees memory allocated by these routines. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiGetVariables (3), .BR cgiGetFiles (3), .BR cgiGetCookies (3). cgilib-0.6/cgiGetCookie.30000644000076500001440000000443510776115516014141 0ustar joeyusers.\" cgiGetCookie - Return a cookie .\" Copyright (c) 1998,9,2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiGetCookie 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiGetCookie \- Return a cookie .SH SYNOPSYS .nf .B #include .sp .BI "s_cookie *cgiGetCookie (s_cgi *" parms ", const char *" name ); .fi .SH DESCRIPTION This routine returns a pointer to a s_cookie structure that contains all values for the cookie as referenced by .IR name . The pointer must not be freed. The .I s_cookie structure is declared as follows: .RS .nf typedef struct cookie_s { char *version, *name, *value, *path, *domain; } s_cookie; .fi .RE Memory allocated by this data structure is automatically freed by the final call to .BR cgiFree (3). To set a cookie in your program you'll need to manually add additional header lines. Please take a look at .IR cgitest.c . Basically, you'll need to add the following code: .nf cgiSetHeader ("Set-Cookie", "Version=1; name=value; Path=/"); cgiHeader(); .fi Please read the included file .I cookies.txt as well. .SH "RETURN VALUE" On success a pointer to a s_cookie structure is returned. If no cookie was set or no cookie with a given name exists NULL is returned. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiGetValue (3), .BR cgiGetVariables (3), .BR cgiGetCookies (3), .BR cgiDebug (3), .BR cgiHeader (3), .BR cgiInit (3), .BR cgiFree (3). cgilib-0.6/cgiGetCookies.30000644000076500001440000000343010776115516014316 0ustar joeyusers.\" cgiGetCookies - Returns a list of cookies .\" Copyright (c) 1999,2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiGetCookies 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiGetCookies \- Returns a list of cookies .SH SYNOPSYS .nf .B #include .sp .BI "char **cgiGetCookies (s_cgi *" parms ); .fi .SH DESCRIPTION This routine returns a NULL terminated array of names of cookies that were transmitted by the client. To get the value of a cookie you'll have to use .BR cgiGetCookie (3). To free the memory allocated for the array, use .BR cgiFreeList (3). .SH "RETURN VALUE" On success a NULL terminated array of strings is returned. The last element is set to NULL. If an error occurred or if no cookies were available NULL is returned. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiGetValue (3), .BR cgiGetVariables (3), .BR cgiGetCookie (3), .BR cgiFreeList (3), .BR cgiDebug (3), .BR cgiHeader (3), .BR cgiInit (3). cgilib-0.6/cgiGetFile.30000644000076500001440000000372010776115516013603 0ustar joeyusers.\" cgiGetValue - Return the value of a CGI variable .\" Copyright (c) 2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiGetValue 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiGetFile \- Return information of a CGI file variable .SH SYNOPSYS .nf .B #include .sp .BI "s_file *cgiGetFile (s_cgi *" parms ", const char *" name ); .fi .SH DESCRIPTION This routine returns a pointer to a datastructure associated with the value of a CGI file variable. The pointer must not be freed. The .I s_file structure is declared as follows: .RS .nf typedef struct file_s { char *name, *type, *filename, *tmpfile; } s_file; .fi .RE Memory allocated by this data structure is automatically freed by the final call to .BR cgiFree (3). .SH "RETURN VALUE" On success a pointer to the .I s_file datastructure is returned. If the variable wasn't transmitted or the through CGI or if the filename was empty NULL is returned. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiGetFiles (3), .BR cgiGetVariables (3), .BR cgiGetCookies (3), .BR cgiDebug (3), .BR cgiInit (3), .BR cgiFree (3). cgilib-0.6/cgiGetFiles.30000644000076500001440000000340310776115516013764 0ustar joeyusers.\" cgiGetFiles - Returns a list of CGI file variables .\" Copyright (c) 2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiGetFiles 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiGetFiles \- Returns a list of CGI file variables .SH SYNOPSYS .nf .B #include .sp .BI "char **cgiGetFiles (s_cgi *" parms ); .fi .SH DESCRIPTION This routine returns a NULL terminated array of names of CGI file variables that are available. Details are provided with the .BR cgiGetFile (3) function. To free the memory allocated for the array use .BR cgiFreeList (3). .SH "RETURN VALUE" On success a NULL terminated array of strings is returned. The last element is set to NULL. If an error occurred or if no files variables are available NULL is returned. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiGetFile (3), .BR cgiGetCookies (3), .BR cgiGetVariables (3), .BR cgiFreeList (3), .BR cgiDebug (3), .BR cgiInit (3). cgilib-0.6/cgiGetValue.30000644000076500001440000000371310776115516014002 0ustar joeyusers.\" cgiGetValue - Return the value of a CGI variable .\" Copyright (c) 1998,9,2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiGetValue 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiGetValue \- Return the value of a CGI variable .SH SYNOPSYS .nf .B #include .sp .BI "char *cgiGetValue (s_cgi *" parms ", const char *" name ); .fi .SH DESCRIPTION This routine returns a pointer to the value of a cgi variable. Encoded characters (%nn) are already decoded. One must not free the pointer. If .B multiple fields are used (i.e. a variable that may contain several values) the value returned contains all these values concatenated together with a newline character as separator. Memory allocated by the returned value is automatically freed by the final call to .BR cgiFree (3). .SH "RETURN VALUE" On success a pointer to a string is returned. If the variable wasn't transmitted through CGI or was empty NULL is returned. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiGetVariables (3), .BR cgiGetCookie (3), .BR cgiGetCookies (3), .BR cgiDebug (3), .BR cgiHeader (3), .BR cgiInit (3), .BR cgiFree (3). cgilib-0.6/cgiGetVariables.30000644000076500001440000000341410776115516014634 0ustar joeyusers.\" cgiGetVariables - Returns a list of CGI variables .\" Copyright (c) 1999,2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiGetVariables 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiGetVariables \- Returns a list of CGI variables .SH SYNOPSYS .nf .B #include .sp .BI "char **cgiGetVariables (s_cgi *" parms ); .fi .SH DESCRIPTION This routine returns a NULL terminated array of CGI variables that are available. To get the value of a variable you'll have to use .BR cgiGetValue (3). To free the memory allocated use .BR cgiFreeList (3). .SH "RETURN VALUE" On success a NULL terminated array of strings is returned. The last element is set to NULL. If an error occurred or if no variables are available NULL is returned. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiGetValue (3), .BR cgiGetCookie (3), .BR cgiGetCookies (3), .BR cgiFreeList (3), .BR cgiDebug (3), .BR cgiHeader (3), .BR cgiInit (3). cgilib-0.6/cgiHeader.30000644000076500001440000000325510776115516013457 0ustar joeyusers.\" cgiHeader - Print the HTTP header .\" Copyright (c) 1998,9,2008 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiHeader 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiHeader \- Print the HTTP header .SH SYNOPSYS .nf .B #include .sp .B void cgiHeader (); .fi .SH DESCRIPTION This routine just prints out the Contents-type: line to make the web server happy. By default the CGI library assumes that you return regular HTML code and uses "text/html" as type. If your CGI program returns something else, you may set the type with .BR cgiSetType (3). You may also add additional header lines by using .BR cgiSetHeader (3). .SH "RETURN VALUE" This routine does not return a value. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiDebug (3), .BR cgiSetHeader (3), .BR cgiSetType (3), .BR cgiInit (3), .BR cgiGetValue (3). cgilib-0.6/cgiInit.30000644000076500001440000000507010776115517013170 0ustar joeyusers.\" cgiInit - Initializes CGI library .\" Copyright (c) 1998,9,2007,8 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiInit 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiInit \- Initializes CGI library .SH SYNOPSYS .nf .B #include .sp .B s_cgi *cgiInit (); .fi .SH DESCRIPTION This routine initializes the CGI routines. Mainly it reads in and decodes CGI data for later processing. If the program is not called via CGI interface the user is prompted to type in CGI variable bindings via stdin - just like CGI.pm does. There is no limit in the number of variable bindings. If HTTP Cookies are used (through the variable HTTP_COOKIE) this routine will parse them as well and assign them to internal variables. There is no limit in the number of cookies. To set a cookie you'll have to use the .BR cgiSetHeader (3) routine. Some clients allow 4kB of Cookies while others only allow short ones. Some clients may also allow only 200 Cookies for a certain domain/path pair. This routine normally is the first or second that is called from this library. Only .BR cgiDebug () may be called before. If debugging is enabled this routine produces some additional output. The library supports both the regular GET and POST methods as well as multipart/form-data. Memory allocated by this library is freed with the .BR cgiFree (3) function. .SH "RETURN VALUE" On success a set of CGI variable bindings is returned that is needed for later processing. If an error occurs NULL is returned. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiDebug (3), .BR cgiSetType (3), .BR cgiSetHeader (3), .BR cgiHeader (3), .BR cgiGetValue (3), .BR cgiGetVariables (3) .BR cgiGetCookie (3), .BR cgiGetCookies (3), .BR cgiFree (3), .BR cgi (5). cgilib-0.6/cgiRedirect.30000644000076500001440000000276710776115517014040 0ustar joeyusers.\" cgiRedirect - Redirect the browser somewhere else .\" Copyright (c) 1998,9,2008 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiRedirect 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiRedirect \- Redirect the browser somewhere else .SH SYNOPSYS .nf .B #include .sp .BI "void cgiRedirect (char *" url ); .fi .SH DESCRIPTION The .B cgiRedirect routine redirects the browser to another .IR url . This mechanism may be useful to redirect invalid requests to some static pages describing the policy. .SH "RETURN VALUE" .BR cgiRedirect () does not return a value. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiInit (3), .BR cgiHeader (3). cgilib-0.6/cgiSetHeader.30000644000076500001440000000462110776115517014132 0ustar joeyusers.\" cgiSetHeader - Specify an additional HTTP header .\" Copyright (c) 1999,2008 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiSetHeader 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiSetHeader \- Specify an additional HTTP header .SH SYNOPSYS .nf .B #include .sp .BI "int cgiSetHeader (char *" name ", char *" value ); .fi .SH DESCRIPTION With the .B cgiSetHeader routine you may specify additional HTTP header lines such as Expires: or Pragma:. The order of header lines withing the HTTP header has no significance. You may call this routine multiple times to set multiple headers. They will be printed in the same order as specified, however, after the Content-type: header. This routine will only add the header to the internal stack, not print it. They will be printed by .BR cgiHeader (3). This routine does some syntax checking before accepting a new header. The .I name of a header must not contain any newline, space or colon, otherwise it will be truncated. The .I value must not contain any newline, otherwise it will be truncated as well. To set a cookie in your program you'll need to manually add additional header lines. Please take a look at .IR cgitest.c . Basically, you'll need to add the following code: .nf cgiSetHeader ("Set-Cookie", "Version=1; name=value; Path=/"); cgiHeader(); .fi Please read the included file .I cookies.txt as well. .SH "RETURN VALUE" On success 1 is returned, otherwise 0. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiHeader (3), .BR cgiGetCookies (3), .BR cgiGetCookie (3), .BR cgiInit (3). cgilib-0.6/cgiSetType.30000644000076500001440000000272210776115517013663 0ustar joeyusers.\" cgiSetType - Specify the result type .\" Copyright (c) 1999,2008 by Martin Schulze .\" .\" 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. .\" .TH cgiSetType 3 "6 April 2008" "CGI Library" "Programmer's Manual" .SH NAME cgiSetType \- Specify the result type .SH SYNOPSYS .nf .B #include .sp .BI "int cgiSetType (char *" type ); .fi .SH DESCRIPTION This routine sets the result type to .IR type . This routine may be used optional. Default is "text/html". It will be used by .BR cgiHeader (3). .SH "RETURN VALUE" On success 1 is returned, otherwise 0. .SH "AUTHOR" This CGI library is written by Martin Schulze . If you have additions or improvements please get in touch with him. .SH "SEE ALSO" .BR cgiHeader (3), .BR cgiSetHeader (3), .BR cgiInit (3). cgilib-0.6/cgitest.c0000644000076500001440000001735110776115517013331 0ustar joeyusers/* cgitest.c - Testprogram for cgi.o Copyright (c) 1998,9,2007,8 by Martin Schulze 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /* * Compile with: cc -o cgitest cgitest.c -lcgi */ #include #include #include #include #define HTML_CSS "\n" #define HTML_HEAD "\ncgilib forms\n\n" #define HTML_FOOT "
\ Infodrom Oldenburg    Project \ cgilib\n
" #define HTML_FORMCONTENT "\ \n\ \n\ \n\ \n" #define HTML_SUBMIT "
Input
Select
Radio yes \ no
Text
\n" s_cgi *cgi; void print_form() { printf ("

cgilib Test-Forms

\n"); printf ("
"); printf ("GET, display selected
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("POST, display selected
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("POST, multipart, display selected
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("GET, display everything
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("POST, display everything
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("POST, multipart, display everything
\n"); printf ("
\n"); printf (HTML_FORMCONTENT); printf ("File"); printf (HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("Misc\n"); printf ("
Redirect\n"); printf ("
List Everything\n"); printf ("
Set Cookie\n"); printf ("
"); } void eval_cgi() { printf ("

Results

\n\n"); printf ("
"); printf ("string: %s

\n", cgiGetValue(cgi, "string")); printf ("radio: %s

\n", cgiGetValue(cgi, "radio")); printf ("text: %s

\n", cgiGetValue(cgi, "text")); printf ("select: %s

\n", cgiGetValue(cgi, "select")); printf ("

"); } void listall (char **env) { char **vars; char *val; char *tmp; s_cookie *cookie; s_file *file; int i; printf ("

Environment Variables

\n
\n");
  for (i=0; env[i]; i++) {
      tmp = cgiEscape (env[i]);
      if (tmp) {
	  printf ("%s\n", tmp);
	  free (tmp);
      }
  }
  
  printf ("
\n

CGI Variables

\n
\n");

  vars = cgiGetVariables (cgi);
  if (vars) {
      for (i=0; vars[i] != NULL; i++) {
	  val = cgiGetValue (cgi, vars[i]);
	  if (val) {
	      tmp = cgiEscape (val);
	      if (tmp) {
		  printf ("%s=%s\n", vars[i], tmp);
		  free (tmp);
	      } else
		  printf ("%s=...\n", vars[i]);
	  } else
	      printf ("%s=\n", vars[i]);
      }
      cgiFreeList (vars);
  } else
      printf ("No variables transmitted.\n");

  printf ("
\n

Cookies

\n
\n");

  vars = cgiGetCookies (cgi);
  if (vars) {
      for (i=0; vars[i] != NULL; i++) {
	  cookie = cgiGetCookie (cgi, vars[i]);
	  if (cookie) {
	      tmp = cgiEscape (cookie->value);
	      if (tmp) {
		  printf ("%s=%s\n", vars[i], tmp);
		  free (tmp);
	      } else
		  printf ("%s=...\n", vars[i]);
	  } else
	      printf ("%s=\n", vars[i]);
      }
      cgiFreeList (vars);
  } else
      printf ("No cookies transmitted.\n");

  printf ("
\n

Files

\n
\n");

  vars = cgiGetFiles (cgi);
  if (vars) {
      for (i=0; vars[i] != NULL; i++) {
	  file = cgiGetFile (cgi, vars[i]);
	  if (file) {
	      tmp = cgiEscape (file->filename);
	      printf ("%s=%s (%s", vars[i], tmp, file->tmpfile);
	      free (tmp);
	      if (file->type) {
		  tmp = cgiEscape (file->type);
		  printf (", %s)\n", tmp);
		  free (tmp);
	      } else
		  printf (")\n");
	  }
      }
      cgiFreeList (vars);
  } else
      printf ("No files transmitted.\n");

  printf ("
\n"); } int main (int argc, char **argv, char **env) { char *path_info = NULL; cgiDebug(0, 0); cgi = cgiInit(); path_info = getenv("PATH_INFO"); if (path_info) { if (!strcmp(path_info, "/redirect")) { cgiRedirect("http://www.infodrom.org/"); exit (0); } else if (!strcmp(path_info, "/setcookie")) { cgiSetHeader ("Set-Cookie", "Version=1; Library=cgilib; Path=/"); cgiHeader(); printf (HTML_CSS HTML_HEAD); printf ("

Cookie Library set

\n"); printf ("



Test

\n"); printf ("



Redirect

\n"); printf ("



List Everything

\n"); } else if (!strcmp(path_info, "/listall")) { cgiHeader(); printf (HTML_CSS HTML_HEAD); listall (env); } else { cgiHeader(); printf (HTML_CSS HTML_HEAD); if (strlen (path_info)) printf ("path_info: %s
\n", path_info); if (!strcmp(path_info, "/insertdata")) { eval_cgi(); } else print_form(); } } else { cgiHeader(); printf (HTML_CSS HTML_HEAD); print_form(); } printf (HTML_FOOT); cgiFree (cgi); return 0; } /* * Local variables: * c-indent-level: 4 * c-basic-offset: 4 * tab-width: 8 * End: */ cgilib-0.6/cookies.c0000644000076500001440000001101710776115517013314 0ustar joeyusers/* cookies.c - Cookie support for CGI library Copyright (C) 1999,2007,8 by Martin Schulze 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include #include #include "aux.h" s_cookie **cgiReadCookies() { char *http_cookie; char *curpos, *n0, *n1, *v0, *v1, *cp; s_cookie **res, *pivot = NULL; int count; int len; if ((curpos = http_cookie = getenv ("HTTP_COOKIE")) == NULL) return NULL; count = 0; if ((res = (s_cookie **)malloc (sizeof (s_cookie *))) == NULL) return NULL; res[0] = NULL; while (*curpos) { for (n0=curpos; *n0 && *n0==' '; n0++); for (n1=n0; *n1 && *n1!=' ' && *n1!='=' && *n1!=';' && *n1!=',';n1++); for (v0=n1; *v0 && (*v0==' ' || *v0=='=' || *v0==' ');v0++); if (*v0 == '"') { v1=++v0; for (;*v1 && *v1!='"';v1++); } else { v1=v0; for (;*v1 && *v1!=',' && *v1!=';';v1++); } if (n0 != n1) { if (*n0 == '$') { if (strncasecmp (n0, "$version", 8) && strncasecmp (n0, "$path", 5) && strncasecmp (n0, "$domain", 7)) { curpos = v1+1; continue; } } else if (pivot && pivot->name) { count++; if ((res = (s_cookie **)realloc (res, sizeof (s_cookie *) * (count+1))) == NULL) return NULL; res[count-1] = pivot; res[count] = pivot = NULL; } if (!pivot) { if ((pivot = (s_cookie *)malloc (sizeof (s_cookie))) == NULL) return res; memset (pivot, 0, sizeof (s_cookie)); if (res && res[0] && res[0]->version) pivot->version = res[0]->version; /* Warning: *Never* free() res[n!=0]->version */ } if (*n0 == '$') { n0++; len = sizeof (char) * (v1-v0); if ((cp = (char *)malloc (len)) == NULL) return res; memset (cp, 0, len); strncpy (cp, v0, v1-v0); if (!strncasecmp (n0, "version", 7)) pivot->version = cp; else if (!strncasecmp (n0, "path", 4)) pivot->path = cp; else if (!strncasecmp (n0, "domain", 6)) pivot->domain = cp; else free (cp); } else { len = sizeof (char) * (n1-n0+1); if ((pivot->name = (char *)malloc (len)) == NULL) return res; memset (pivot->name, 0, len); strncpy (pivot->name, n0, n1-n0); if (*v0 == '"') v0++; len = sizeof (char) * (v1-v0+1); if ((pivot->value = (char *)malloc (len)) == NULL) return res; memset (pivot->value, 0, len); strncpy (pivot->value, v0, v1-v0); } } else { curpos = n0+1; } curpos = v1; if (*curpos) curpos++; } count++; if ((res = (s_cookie **)realloc (res, sizeof (s_cookie *) * (count+1))) == NULL) return NULL; res[count-1] = pivot; res[count] = NULL; return res; } s_cookie *cgiGetCookie (s_cgi *parms, const char *name) { int i; if (!parms || !parms->cookies) return NULL; for (i=0;parms->cookies[i]; i++) if (parms->cookies[i]->name && parms->cookies[i]->value && !strcmp(name,parms->cookies[i]->name)) { cgiDebugOutput (1, "%s found as %s\n", name, parms->cookies[i]->value); return parms->cookies[i]; } cgiDebugOutput (1, "%s not found\n", name); return NULL; } char **cgiGetCookies (s_cgi *parms) { int i, k; char **res = NULL; int len; if (!parms || !parms->cookies) return NULL; for (i=k=0;parms->cookies[i];i++) if (parms->cookies[i]->name && parms->cookies[i]->value) k++; len = sizeof (char *) * ++k; if ((res = (char **)malloc (len)) == NULL) return NULL; memset (res, 0, len); for (i=k=0;parms->cookies[i]; i++) { if (parms->cookies[i]->name && parms->cookies[i]->value) { len = strlen (parms->cookies[i]->name) +1; if ((res[i] = (char *)malloc (len)) == NULL) return NULL; memset (res[i], 0, len); strcpy (res[i], parms->cookies[i]->name); } } return res; } /* * Local variables: * c-indent-level: 4 * c-basic-offset: 4 * tab-width: 8 * End: */ cgilib-0.6/cookies.txt0000644000076500001440000000626710776143002013712 0ustar joeyusersInternet Cookies ================ This gives a short overview about cookies in general and their implementation in the CGI library in detail. The use of cookies is described in two partially different ways. Netscape has released their standard and an RFC (2109) has been filed from Bell Labs. Both documents are found through "More Information". Server Side ----------- The server initiates a session by sending a cookie to the client. The server may transmit one or more cookies by using the additional HTTP header "Set-Cookie" like Set-Cookie: foo=bar; Version=1; Path=/foo; Domain=.foo.org This cookie would only be available within any host that matches *.foo.org. According to RFC 2109 * may not contain a dot, according to Netscape's documentation it may. The client would only send the cookie back to the server if the URL is within /foo on that server. When more than one cookie are to be transmitted they are separated with a comma. Attributes are separated with a semicolon. If the current URL doesn't match the Path attribute or the hostname doesn't match the Domain attribute, the client will ignore the cookie. Client Side ----------- When the client requests a web page, it checks if the location is within the scope of one or more cookies. If so it sends them, ordered by the most matching one, like: Cookie: $Version=1; foo=bar; $path=/; bar=foo; $path=/foo Attributes belong to the most left name=value pair. Attributes specified before such a pair are valid for all subsequent cookies. Syntax ------ Cookie ::=

":" *( ";" )
::= "Cookie" | "Set-Cookie" ::= [ "=" ] ::= ::= | ::= sequence of non-special, non-white space characters Attribut names () are case-insensitive. Spaces may be used between tokens and within attribute values. Some attributes don't require a value. The Set-Cookie header uses these attributes: ::= NAME "=" VALUE *( ";" ) # required ::= "Comment" "=" value # optional | "Domain" "=" value # optional | "Max-Age" "=" value # optional | "Path" "=" value # optional | "Secure" # optional | "Version" "=" 1*DIGIT # required Apart from the cookie itself as NAME "=" VALUE the Cookie header as sent by the client uses only $Version, $Path and $Domain which are both optional. The value of the Version attribute must be the value from the Version attribute, if any, of the corresponding Set-Cookie response header. ::= *( ( ";" | "," ) ) ::= NAME "=" VALUE [";" ] [";" ] ::= "$Path" "=" value ::= "$Domain" "=" value Note: For backward compatibility, the separator in the Cookie header is semi-colon (;) everywhere. A server should also accept comma (,) as the separator between cookie-values for future compatibility. cgilib-0.6/jumpto.c0000644000076500001440000000337210776115517013203 0ustar joeyusers/* jumpto.c - Jump to a given URL Copyright (c) 1998,2008 Martin Schulze 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /* * Compile with: cc -o jumpto jumpto.c -lcgi * * To include this in your web pages you'll need something * like this: * *
*
* * * */ #include #include #include s_cgi *cgiArg; void main() { char *url; char *server_url = NULL; cgiDebug(0,0); cgiArg = cgiInit (); server_url = getenv("SERVER_URL"); if ((url = cgiGetValue(cgiArg, "url")) == NULL) { if (server_url) cgiRedirect(server_url); else cgiRedirect("/"); } else cgiRedirect(url); } /* * Local variables: * c-indent-level: 4 * c-basic-offset: 4 * tab-width: 8 * End: */ cgilib-0.6/readme0000644000076500001440000000572210776116670012703 0ustar joeyusersUsing this Library ------------------ To use this library you need to include the cgi.h include file with the following expression into your C programs: #include Additionally you'll have to add the library libcgi.a to the linker, either by modifying LDFLAGS in your Makefile or by adding `-lcgi' to the appropriate commandline. Test Suite ---------- This library comes with a test suite both to demonstrate its features and to help you understand how it works. The respective program is called cgitest.c. You can use it as reference implementation for this CGI library. It can also be used to debug the library by increasing its debug level. Therefore it should be included as source in your distribution. If you want to test your forms you can use the following action.
The /listall target will display all environment variables, all CGI variables, all HTTP Cookies and refer to all files that were transmitted. This way you can test your own implementations quite easy. Further Documentation --------------------- This section will refer to a number of URLs that document how this library and used protocols work The CGI Library http://www.infodrom.org/projects/cgilib/ HTTP Return Codes http://www.w3.org/Protocols/HTTP/HTRESP.html HTTP Headers http://www.w3.org/Protocols/HTTP/Object_Headers.html Development ----------- Development of this library takes place in a public CVS repository. This offers a way to do versioning and provide a history for all files and changes. At any time it is possible to restore an older version, partially or in total. Additionally, lean patches can be applied and reverted. Log in: cvs -d :pserver:anonymous@cvs.infodrom.org:/var/cvs/infodrom login [Hit return] Check out CVS HEAD: cvs -d :pserver:anonymous@cvs.infodrom.org:/var/cvs/infodrom co cgilib Mailing Lists ------------- A mailing list dedicated to discussions about this library has been created. It is meant as a forum for users and developers of the lightweight CGI library and applications that make use of it. The lists address is . To subscribe to this list, . send a mail to infodrom-cgilib-request@@lists.infodrom.org with the word "subscribe" as subject, or . send a mail to majordomo@@lists.infodrom.org with the body of "subscribe infodrom-cgilib". The list is publicly archived at . A second mailing list exists as infodrom-cgilib-cvs on the same host. Only CVS messages and differences are distributed there. Whenever new code is added to the CGI library, »cvs« generates a mail from these changes which will be sent to this particular list. Discussions continue to take place on the first list. Feedback -------- If you have additions, questions or improvements please don't hesitate to contact either the mailing list or me personally. Infodrom Oldenburg Martin Schulze joey@infodrom.org