/*
|
/*
|
* form.c -- Form processing (in-memory CGI) for the GoAhead Web server
|
* form.c -- Form processing (in-memory CGI) for the GoAhead Web server
|
*
|
*
|
* Copyright (c) Go Ahead Software Inc., 1995-1999. All Rights Reserved.
|
* Copyright (c) Go Ahead Software Inc., 1995-1999. All Rights Reserved.
|
*
|
*
|
* See the file "license.txt" for usage and redistribution license requirements
|
* See the file "license.txt" for usage and redistribution license requirements
|
*/
|
*/
|
|
|
/********************************** Description *******************************/
|
/********************************** Description *******************************/
|
|
|
/*
|
/*
|
* This module implements the /goform handler. It emulates CGI processing
|
* This module implements the /goform handler. It emulates CGI processing
|
* but performs this in-process and not as an external process. This enables
|
* but performs this in-process and not as an external process. This enables
|
* a very high performance implementation with easy parsing and decoding
|
* a very high performance implementation with easy parsing and decoding
|
* of query strings and posted data.
|
* of query strings and posted data.
|
*/
|
*/
|
|
|
/*********************************** Includes *********************************/
|
/*********************************** Includes *********************************/
|
|
|
#include "wsIntrn.h"
|
#include "wsIntrn.h"
|
|
|
/************************************ Locals **********************************/
|
/************************************ Locals **********************************/
|
|
|
static sym_fd_t formSymtab = -1; /* Symbol table for form handlers */
|
static sym_fd_t formSymtab = -1; /* Symbol table for form handlers */
|
|
|
/************************************* Code ***********************************/
|
/************************************* Code ***********************************/
|
/*
|
/*
|
* Process a form request. Returns 1 always to indicate it handled the URL
|
* Process a form request. Returns 1 always to indicate it handled the URL
|
*/
|
*/
|
|
|
int websFormHandler(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg,
|
int websFormHandler(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg,
|
char_t *url, char_t *path, char_t* query)
|
char_t *url, char_t *path, char_t* query)
|
{
|
{
|
sym_t *sp;
|
sym_t *sp;
|
char_t formBuf[FNAMESIZE];
|
char_t formBuf[FNAMESIZE];
|
char_t *cp, *formName;
|
char_t *cp, *formName;
|
int (*fn)(void *sock, char_t* path, char_t *args);
|
int (*fn)(void *sock, char_t* path, char_t *args);
|
|
|
a_assert(websValid(wp));
|
a_assert(websValid(wp));
|
a_assert(url && *url);
|
a_assert(url && *url);
|
a_assert(path && *path == '/');
|
a_assert(path && *path == '/');
|
|
|
websStats.formHits++;
|
websStats.formHits++;
|
|
|
/*
|
/*
|
* Extract the form name
|
* Extract the form name
|
*/
|
*/
|
gstrncpy(formBuf, path, TSZ(formBuf));
|
gstrncpy(formBuf, path, TSZ(formBuf));
|
if ((formName = gstrchr(&formBuf[1], '/')) == NULL) {
|
if ((formName = gstrchr(&formBuf[1], '/')) == NULL) {
|
websError(wp, 200, T("Missing form name"));
|
websError(wp, 200, T("Missing form name"));
|
return 1;
|
return 1;
|
}
|
}
|
formName++;
|
formName++;
|
if ((cp = gstrchr(formName, '/')) != NULL) {
|
if ((cp = gstrchr(formName, '/')) != NULL) {
|
*cp = '\0';
|
*cp = '\0';
|
}
|
}
|
|
|
/*
|
/*
|
* Lookup the C form function first and then try tcl (no javascript support
|
* Lookup the C form function first and then try tcl (no javascript support
|
* yet).
|
* yet).
|
*/
|
*/
|
sp = symLookup(formSymtab, formName);
|
sp = symLookup(formSymtab, formName);
|
if (sp == NULL) {
|
if (sp == NULL) {
|
websError(wp, 200, T("Form %s is not defined"), formName);
|
websError(wp, 200, T("Form %s is not defined"), formName);
|
} else {
|
} else {
|
fn = (int (*)(void*, char_t*, char_t*)) sp->content.value.integer;
|
fn = (int (*)(void*, char_t*, char_t*)) sp->content.value.integer;
|
a_assert(fn);
|
a_assert(fn);
|
if (fn) {
|
if (fn) {
|
/*
|
/*
|
* For good practice, forms must call websDone()
|
* For good practice, forms must call websDone()
|
*/
|
*/
|
(*fn)((void*) wp, formName, query);
|
(*fn)((void*) wp, formName, query);
|
if (websValid(wp)) {
|
if (websValid(wp)) {
|
websError(wp, 200, T("Form didn't call websDone"));
|
websError(wp, 200, T("Form didn't call websDone"));
|
}
|
}
|
}
|
}
|
}
|
}
|
return 1;
|
return 1;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Define a form function in the "form" map space.
|
* Define a form function in the "form" map space.
|
*/
|
*/
|
|
|
int websFormDefine(char_t *name, void (*fn)(webs_t wp, char_t *path,
|
int websFormDefine(char_t *name, void (*fn)(webs_t wp, char_t *path,
|
char_t *query))
|
char_t *query))
|
{
|
{
|
static int once = 0;
|
static int once = 0;
|
|
|
a_assert(name && *name);
|
a_assert(name && *name);
|
a_assert(fn);
|
a_assert(fn);
|
|
|
if (fn == NULL) {
|
if (fn == NULL) {
|
return -1;
|
return -1;
|
}
|
}
|
|
|
if (once++ == 0) {
|
if (once++ == 0) {
|
websFormOpen();
|
websFormOpen();
|
}
|
}
|
symEnter(formSymtab, name, valueInteger((int) fn), (int) NULL);
|
symEnter(formSymtab, name, valueInteger((int) fn), (int) NULL);
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Open the symbol table for forms.
|
* Open the symbol table for forms.
|
*/
|
*/
|
|
|
void websFormOpen()
|
void websFormOpen()
|
{
|
{
|
formSymtab = symOpen(64);
|
formSymtab = symOpen(64);
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Close the symbol table for forms.
|
* Close the symbol table for forms.
|
*/
|
*/
|
|
|
void websFormClose()
|
void websFormClose()
|
{
|
{
|
if (formSymtab != -1) {
|
if (formSymtab != -1) {
|
symClose(formSymtab, NULL);
|
symClose(formSymtab, NULL);
|
}
|
}
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Write a webs header. This is a convenience routine to write a common
|
* Write a webs header. This is a convenience routine to write a common
|
* header for a form back to the browser.
|
* header for a form back to the browser.
|
*/
|
*/
|
|
|
void websHeader(webs_t wp)
|
void websHeader(webs_t wp)
|
{
|
{
|
a_assert(websValid(wp));
|
a_assert(websValid(wp));
|
|
|
websWrite(wp, T("HTTP/1.0 200 OK\n"));
|
websWrite(wp, T("HTTP/1.0 200 OK\n"));
|
|
|
/*
|
/*
|
* By license terms the following line of code must not be modified
|
* By license terms the following line of code must not be modified
|
*/
|
*/
|
websWrite(wp, T("Server: GoAhead-Webs\r\n"));
|
websWrite(wp, T("Server: GoAhead-Webs\r\n"));
|
|
|
websWrite(wp, T("Pragma: no-cache\n"));
|
websWrite(wp, T("Pragma: no-cache\n"));
|
websWrite(wp, T("Cache-control: no-cache\n"));
|
websWrite(wp, T("Cache-control: no-cache\n"));
|
websWrite(wp, T("Content-Type: text/html\n"));
|
websWrite(wp, T("Content-Type: text/html\n"));
|
websWrite(wp, T("\n"));
|
websWrite(wp, T("\n"));
|
websWrite(wp, T("<html>\n"));
|
websWrite(wp, T("<html>\n"));
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Write a webs footer
|
* Write a webs footer
|
*/
|
*/
|
|
|
void websFooter(webs_t wp)
|
void websFooter(webs_t wp)
|
{
|
{
|
a_assert(websValid(wp));
|
a_assert(websValid(wp));
|
|
|
websWrite(wp, T("</html>\n"));
|
websWrite(wp, T("</html>\n"));
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
|
|