/*** Beginheader */ #ifndef __AIT_WEB_LIB #define __AIT_WEB_LIB /*** Endheader */ /* START LIBRARY DESCRIPTION ********************************************* AIT_WEB.LIB Copyright (c) 2004, Adaptive Internet Technologies DESCRIPTION: AIT Web Interface Utilities SUPPORT LIBS: REVISION HISTORY: 1.1 1/27/04 Original Issue END DESCRIPTION **********************************************************/ //--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--// /*** BeginHeader WEB_Form, WEB_GetVarAddr, WEB_ParsePost, WEB_ScanVars */ int WEB_Form(HttpState* state); void *WEB_GetVarAddr(char * Name); int WEB_ParsePost(HttpState *State); int WEB_ScanVars(char* tag, char* buffer, int type, void* dest); /*** EndHeader */ /* * Generic CGI function */ int WEB_Form(HttpState* state) { auto int i; if(state->length) { /* buffer to write out */ if(state->offset < state->length) { state->offset += sock_fastwrite(&state->s, state->buffer + (int)state->offset, (int)state->length - (int)state->offset); } else { state->offset = 0; state->length = 0; } } else { switch(state->substate) { case 0: /* parse the POST information */ if(WEB_ParsePost(state)) { state->substate++; } break; case 1: default: state->substate = 0; return 1; } } return 0; } // ----------------------------------------------- void *WEB_GetVarAddr(char * Name) { int i; // Scan the received POST for any server variable for (i = 0; i < SSPEC_MAXSPEC; i++) { if (server_spec[i].type == SSPEC_ROOTVAR) { if (strcmp(server_spec[i].name, Name) == 0) return ( (void *) server_spec[i].addr ); } } return ( (void *) NULL ); } // ----------------------------------------------- int WEB_ParsePost(HttpState *State) { auto int retval; auto int i; // auto ServerSpec * pServerSpec ; // state->s is the socket structure, and state->p is pointer // into the HTTP state buffer (initially pointing to the beginning // of the buffer). Note that state->p was set up in the submit // CGI function. Also note that we read up to the content_length, // or HTTP_MAXBUFFER, whichever is smaller. Larger POSTs will be // truncated. retval = sock_aread(&State->s, State->p, (State->content_length < HTTP_MAXBUFFER-1)? (int)State->content_length:HTTP_MAXBUFFER-1); if (retval < 0) { // Error--just bail out return 1; } // Using the subsubstate to keep track of how much data we have received State->subsubstate += retval; if (State->subsubstate >= State->content_length) { // NULL-terminate the content buffer State->buffer[(int)State->content_length] = '\0'; // Scan the received POST for any server variable for (i = 0; i < SSPEC_MAXSPEC; i++) { if (server_spec[i].type == SSPEC_ROOTVAR) { WEB_ScanVars(server_spec[i].name, State->buffer, server_spec[i].vartype, server_spec[i].addr ); } } // Finished processing--returning 1 indicates that we are done return 1; } // Processing not finished--return 0 so that we can be called again return 0; } /* START FUNCTION DESCRIPTION ******************************************** http_float SYNTAX: int WEB_ScanVars(char* tag, char* buffer, void* dest); KEYWORDS: tcpip, http, post DESCRIPTION: The WEB_ScanVars function allows you to scan a buffer with a POST response for the key/value pairs. Values are saved in the Web Server variable table This function is reentrant. PARAMETER1: Buffer holding the tag name. PARAMETER2: Buffer to read data from. PARAMETER3: Variable Type PARAMETER4: Address of variable to write to RETURN VALUE: zero if successful, non-zero otherwise. END DESCRIPTION **********************************************************/ int WEB_ScanVars(char* tag, char* buffer, int type, void* dest) { #define VALUE_LEN 32 auto int x, state, offset, length; auto char Value[VALUE_LEN]; state=offset=x=0; length=strlen(buffer); while(offset