00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 #include <stdio.h>
00138 #include <stdlib.h>
00139 #include <memory.h>
00140 #include <ctype.h>
00141 #include <fcntl.h>
00142 #include <string.h>
00143 #include <ctype.h>
00144 #ifdef WIN32
00145 #include <io.h>
00146 #include <httpext.h>
00147 #endif
00148
00149 #include "cgi.h"
00150
00151 #define _getenv(X) (pCO->pfEnv ? pCO->pfEnv(pCO->pEmbed,(X),0) : getenv(X))
00152
00153 #if (!defined(_WIN32) && !defined(__MACOS__))
00154 static int stricmp(char *a, char *b){
00155 char ca,cb;
00156
00157 while( 1 ){
00158 ca = *a++;
00159 cb = *b++;
00160 ca = isupper(ca) ? tolower(ca) : ca;
00161 cb = isupper(cb) ? tolower(cb) : cb;
00162 if( ca == (char)0 && cb == (char)0 )return 0;
00163 if( ca != cb )return ca-cb;
00164 }
00165 }
00166 #endif
00167
00168
00169 #define CR '\r'
00170 #define LF '\n'
00171
00172 #define BUFFER_INCREASE 1024
00173 #define BUFFER_MAX 10240
00174
00175 #define CONTENT_MAX 0xA00000
00176 #define FILE_MAX 0xA00000
00177
00178 #define ISAPI_BUFFER 0xA000
00179
00180 #define DEBUGFP (pCO->pDebugInfo->fpDebugInput)
00181
00182 #define GETCHAR() (pCO->CharacterInput(pCO))
00183 #define ALLOC(x) (pCO->maf((x),pCO->pSegment))
00184 #define FREE(x) (pCO->mrf((x),pCO->pSegment))
00185
00186 static char x2c(char *what){
00187 register char digit;
00188 #define TOHEX(x) ((x) >= 'A' ? (((x) & 0xdf) - 'A')+10 : ((x) - '0'))
00189
00190 digit = TOHEX(*what);
00191 digit *= 16;
00192 what++;
00193 digit += TOHEX(*what);
00194 return digit;
00195 #undef TOHEX
00196 }
00197
00198 static void unescape(char *s, long *len){
00199 char *p;
00200 long rest;
00201
00202 p = s;
00203 rest = *len;
00204 while( rest ){
00205 if( *p == '+' )*p = ' ';
00206 p++;
00207 rest --;
00208 }
00209
00210 p = s;
00211 rest = *len;
00212
00213 while( rest ){
00214 *p = *s;
00215 if( *p == '%' ){
00216 s++;rest--;
00217 *p = x2c(s);
00218 s++;
00219 rest--;
00220 (*len) -= 2;
00221 }
00222 p++;
00223 s++;
00224 rest--;
00225 }
00226 }
00227
00228 static void *my_maf(long size, void *p){
00229 return malloc(size);
00230 }
00231 static void my_mrf(void *q, void*p){
00232 free(q);
00233 }
00234 #ifdef WIN32
00235 static int IsapiGetChar(pCgiObject pCO){
00236 unsigned char ch;
00237
00238
00239 if( pCO->lContentLength == 0 )return EOF;
00240 if( pCO->cbAvailable ){
00241 pCO->cbAvailable--;
00242 ch = *(pCO->pszNextChar);
00243 pCO->lContentLength --;
00244 pCO->pszNextChar ++;
00245 return (unsigned int)ch;
00246 }
00247
00248 if( pCO->pszLocalBuffer == NULL ){
00249 if( pCO->dwIsapiBufferSize > pCO->lContentLength )
00250 pCO->dwIsapiBufferSize = pCO->lContentLength;
00251 pCO->pszLocalBuffer = ALLOC(pCO->dwIsapiBufferSize);
00252 if( pCO->pszLocalBuffer == NULL )return EOF;
00253 }
00254 pCO->pszNextChar = pCO->pszLocalBuffer;
00255 pCO->cbAvailable = pCO->dwIsapiBufferSize;
00256 if( pCO->lpECB->ReadClient(pCO->lpECB->ConnID,pCO->pszLocalBuffer,&(pCO->cbAvailable)) && pCO->cbAvailable ){
00257 ch = *(pCO->pszNextChar);
00258 pCO->lContentLength --;
00259 pCO->cbAvailable--;
00260 pCO->pszNextChar ++;
00261 return (unsigned int)ch;
00262 }
00263
00264 return EOF;
00265 }
00266 #endif
00267 static int CgiGetChar(pCgiObject pCO){
00268 int ch;
00269 #ifdef WIN32
00270 if( ! pCO->pfStdIn ){
00271 setmode(fileno(stdin), O_BINARY);
00272 _fmode = O_BINARY;
00273 }
00274 #endif
00275 if( pCO->lContentLength ){
00276 pCO->lContentLength --;
00277 if( pCO->pfStdIn )
00278 ch = pCO->pfStdIn(pCO->pEmbed);
00279 else
00280 ch = getchar();
00281
00282 return ch;
00283 }
00284 return EOF;
00285 }
00286 static void CgiPutChar(pCgiObject pCO, int ch){
00287 if( pCO->pfStdOut )
00288 pCO->pfStdOut(ch,pCO->pEmbed);
00289 else
00290 putc(ch,stdout);
00291 }
00292 static int DebugGetChar(pCgiObject pCO){
00293 int ch;
00294 #ifdef WIN32
00295 setmode(fileno(DEBUGFP), O_BINARY);
00296 _fmode = O_BINARY;
00297 #endif
00298 ch = getc(DEBUGFP);
00299
00300 return ch;
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 void cgi_InitCgi(pCgiObject pCO
00399 ){
00400
00401
00402 pCO->maf = my_maf;
00403 pCO->mrf = my_mrf;
00404 pCO->pSegment = NULL;
00405
00406 pCO->fInterface = CGI_INTERFACE_CGI;
00407
00408 pCO->pszBoundary = NULL;
00409 pCO->cbBoundary = 0;
00410 pCO->pszBuffer = NULL;
00411 pCO->cbBuffer = 0;
00412
00413 pCO->cbFill = 0;
00414 pCO->lBufferPosition = 0;
00415
00416 pCO->CharacterInput = CgiGetChar;
00417 pCO->pInputParameter = NULL;
00418 pCO->lContentLength = 0;
00419
00420 pCO->lBufferIncrease = BUFFER_INCREASE;
00421 pCO->lBufferMax = BUFFER_MAX;
00422 pCO->pGetParameters = NULL;
00423 pCO->pPostParameters = NULL;
00424 pCO->lContentMax = CONTENT_MAX;
00425 pCO->lFileMax = FILE_MAX;
00426 #ifdef WIN32
00427 pCO->dwIsapiBufferSize = ISAPI_BUFFER;
00428 #endif
00429
00430 pCO->fMethods = CGI_METHOD_GET | CGI_METHOD_POST | CGI_METHOD_UPL;
00431
00432 pCO->pszDebugFile = NULL;
00433
00434 pCO->pfStdIn = NULL;
00435 pCO->pfStdOut = NULL;
00436 pCO->pfEnv = NULL;
00437 pCO->pEmbed = NULL;
00438 }
00439
00440 #ifdef WIN32
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456 void cgi_InitIsapi(pCgiObject pCO,
00457 LPEXTENSION_CONTROL_BLOCK lpECB
00458 ){
00459
00460
00461 cgi_InitCgi(pCO);
00462
00463 pCO->lpECB = lpECB;
00464 pCO->CharacterInput = IsapiGetChar;
00465 pCO->pszNextChar = lpECB->lpbData;
00466 pCO->cbAvailable = lpECB->cbAvailable;
00467 pCO->fInterface = CGI_INTERFACE_ISAPI;
00468 pCO->pszLocalBuffer = NULL;
00469
00470 pCO->pfStdIn = NULL;
00471 pCO->pfStdOut = NULL;
00472 pCO->pfEnv = NULL;
00473 pCO->pEmbed = NULL;
00474 }
00475 #else
00476 void cgi_InitIsapi(pCgiObject pCO,
00477 LPEXTENSION_CONTROL_BLOCK lpECB
00478 ){
00479
00480 }
00481 #endif
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 long cgi_ReadHttpRequest(pCgiObject pCO
00498 ){
00499
00500
00501 char LocalBuffer[1024],*s,*r;
00502 long w;
00503
00504 switch( pCO->fInterface ){
00505 case CGI_INTERFACE_ISAPI:
00506
00507
00508
00509
00510
00511 pCO->pDebugInfo = ALLOC(sizeof(DebugStore));
00512 if( pCO->pDebugInfo == NULL )return CGI_ERROR_MEMORY_LOW;
00513 memset(pCO->pDebugInfo,0,sizeof(DebugStore));
00514 case CGI_INTERFACE_CGI:
00515 case CGI_INTERFACE_DEBUG:
00516 RetryWithDebugMode:
00517 if( NULL != (s = cgi_RequestMethod(pCO)) ){
00518 w = 0;
00519 if( !strcmp(s,"GET") )w = CGI_METHOD_GET;
00520 else
00521 if( !strcmp(s,"HEAD") )w = CGI_METHOD_HEAD;
00522 if( w ){
00523 if( ! (pCO->fMethods&w) )return CGI_ERROR_METHOD_NOTALL;
00524 return cgi_GetGetParameters(pCO);
00525 }
00526 }
00527 if( s && !strcmp(s,"POST") ){
00528 if( ! (pCO->fMethods&CGI_METHOD_POST) )return CGI_ERROR_METHOD_NOTALL;
00529 r=cgi_ContentLength(pCO);
00530 pCO->lContentLength = r ? atol( r ) : 0 ;
00531 if( pCO->lContentLength > pCO->lContentMax )return CGI_ERROR_BIG_CONTENT;
00532 if( (r=cgi_ContentType(pCO)) && ! memcmp(r,"multipart/form-data",19) ){
00533 if( ! (pCO->fMethods&CGI_METHOD_UPL) )return CGI_ERROR_METHOD_NOTALL;
00534 return cgi_GetMultipartParameters(pCO);
00535 }
00536 else return cgi_GetPostParameters(pCO);
00537 }
00538
00539 if( pCO->fInterface == CGI_INTERFACE_DEBUG )return CGI_ERROR_INVALID_METHOD;
00540
00541 pCO->fInterface = CGI_INTERFACE_DEBUG;
00542 #define READL(x) if( fgets(LocalBuffer,1024,DEBUGFP) ){\
00543 LocalBuffer[w=strlen(LocalBuffer)-2] = (char)0; \
00544 pCO->pDebugInfo->x = ALLOC(w);\
00545 if( pCO->pDebugInfo->x == NULL )return CGI_ERROR_MEMORY_LOW;\
00546 strcpy(pCO->pDebugInfo->x,LocalBuffer);\
00547 }else pCO->pDebugInfo->x = "";
00548
00549 pCO->pDebugInfo = ALLOC(sizeof(DebugStore));
00550 if( pCO->pDebugInfo == NULL )return CGI_ERROR_MEMORY_LOW;
00551 if( pCO->pszDebugFile == NULL )return CGI_ERROR_NO_DEBUG_FILE;
00552 DEBUGFP = fopen(pCO->pszDebugFile,"rb");
00553 if( DEBUGFP == NULL )return CGI_ERROR_NO_DEBUG_FILE;
00554
00555 READL(ServerSoftware);
00556 READL(ServerName);
00557 READL(GatewayInterface);
00558 READL(ServerProtocol);
00559 READL(ServerPort);
00560 READL(RequestMethod);
00561 READL(PathInfo);
00562 READL(PathTranslated);
00563 READL(ScriptName);
00564 READL(QueryString);
00565 READL(RemoteHost);
00566 READL(RemoteAddress);
00567 READL(AuthType);
00568 READL(RemoteUser);
00569 READL(RemoteIdent);
00570 READL(ContentType);
00571 READL(ContentLength);
00572 READL(UserAgent);
00573 READL(Cookie);
00574
00575 pCO->CharacterInput = DebugGetChar;
00576 goto RetryWithDebugMode;
00577 case CGI_INTERFACE_NSAPI:
00578 return CGI_ERROR_NOTIMP;
00579 case CGI_INTERFACE_FCGI:
00580 return CGI_ERROR_NOTIMP;
00581 }
00582 return CGI_ERROR_INVALID_METHOD;
00583 }
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598 char *cgi_PostParam(pCgiObject pCO,
00599 char *pszParam
00600 ){
00601
00602
00603
00604
00605 pSymbolList p;
00606
00607 p = pCO->pPostParameters;
00608 while( p ){
00609 if( !strcmp(p->symbol,pszParam) )return p->value ? p->value : "";
00610 p = p->next;
00611 }
00612
00613 return NULL;
00614 }
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629 char *cgi_GetParam(pCgiObject pCO,
00630 char *pszParam
00631 ){
00632
00633
00634
00635
00636 pSymbolList p;
00637
00638 p = pCO->pGetParameters;
00639 while( p ){
00640 if( !strcmp(p->symbol,pszParam) )return p->value ? p->value : "";
00641 p = p->next;
00642 }
00643
00644 return NULL;
00645 }
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665 char *cgi_PostParamEx(pCgiObject pCO,
00666 char *pszParam,
00667 pSymbolList *p
00668 ){
00669
00670
00671
00672 if( *p == NULL )
00673 *p = pCO->pPostParameters;
00674 else
00675 *p = (*p)->next;
00676 while( *p ){
00677 if( !pszParam || !strcmp((*p)->symbol,pszParam) )
00678 return (*p)->value ? (*p)->value : "";
00679
00680 *p = (*p)->next;
00681 }
00682
00683 return NULL;
00684 }
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704 char *cgi_GetParamEx(pCgiObject pCO,
00705 char *pszParam,
00706 pSymbolList *p
00707 ){
00708
00709
00710
00711 if( *p == NULL )
00712 *p = pCO->pGetParameters;
00713 else
00714 *p = (*p)->next;
00715 while( *p ){
00716 if( !pszParam || !strcmp((*p)->symbol,pszParam) )
00717 return (*p)->value ? (*p)->value : "";
00718
00719 *p = (*p)->next;
00720 }
00721
00722 return NULL;
00723 }
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737 FILE *cgi_FILEp(pCgiObject pCO,
00738 char *pszParam
00739 ){
00740
00741
00742 pSymbolList p;
00743
00744 p = pCO->pPostParameters;
00745 while( p ){
00746 if( !strcmp(p->symbol,pszParam) )return p->fp;
00747 p = p->next;
00748 }
00749 return NULL;
00750 }
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767 char *cgi_OriginalFileName(pCgiObject pCO,
00768 char *pszParam
00769 ){
00770
00771
00772 pSymbolList p;
00773
00774 p = pCO->pPostParameters;
00775 while( p ){
00776 if( !strcmp(p->symbol,pszParam) )return p->file;
00777 p = p->next;
00778 }
00779
00780 return NULL;
00781 }
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798 long cgi_FileLength(pCgiObject pCO,
00799 char *pszParam
00800 ){
00801
00802
00803 pSymbolList p;
00804
00805 p = pCO->pPostParameters;
00806 while( p ){
00807 if( !strcmp(p->symbol,pszParam) )return p->len;
00808 p = p->next;
00809 }
00810
00811 return 0;
00812 }
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829 pSymbolList cgi_PartHeader(pCgiObject pCO,
00830 char *pszParam
00831 ){
00832
00833
00834 pSymbolList p;
00835
00836 p = pCO->pPostParameters;
00837 while( p ){
00838 if( !strcmp(p->symbol,pszParam) )return p->pHeaders;
00839 p = p->next;
00840 }
00841
00842 return NULL;
00843 }
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 char *cgi_Header(pCgiObject pCO,
00864 char *symbol,
00865 pSymbolList pHeader
00866 ){
00867
00868
00869 while( pHeader ){
00870 if( ! stricmp(pHeader->symbol,symbol) )return pHeader->value;
00871 pHeader = pHeader->next;
00872 }
00873 return NULL;
00874 }
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914 #define ISAPIVAR(X,Y) \
00915 if( pCO->pDebugInfo->X )return pCO->pDebugInfo->X;\
00916 { DWORD cbBuffer;\
00917 cbBuffer = 0;\
00918 pCO->lpECB->GetServerVariable(pCO->lpECB->ConnID,Y,NULL,&cbBuffer);\
00919 if( cbBuffer == 0 )return NULL;\
00920 pCO->pDebugInfo->X = ALLOC(cbBuffer);\
00921 if( pCO->pDebugInfo->X == NULL )return NULL;\
00922 pCO->lpECB->GetServerVariable(pCO->lpECB->ConnID,Y,pCO->pDebugInfo->X,&cbBuffer);\
00923 return pCO->pDebugInfo->X;}
00924
00925
00926 char *cgi_Referer(pCgiObject pCO
00927 ){
00928 switch( pCO->fInterface ){
00929 case CGI_INTERFACE_CGI:
00930 return _getenv("HTTP_REFERER");
00931 #ifdef WIN32
00932 case CGI_INTERFACE_ISAPI:
00933 ISAPIVAR(Cookie,"HTTP_REFERER")
00934 #endif
00935 case CGI_INTERFACE_NSAPI:
00936 return NULL;
00937 case CGI_INTERFACE_FCGI:
00938 return NULL;
00939 case CGI_INTERFACE_DEBUG:
00940 return NULL;
00941 }
00942 return NULL;
00943 }
00944
00945
00946 char *cgi_Cookie(pCgiObject pCO
00947 ){
00948 switch( pCO->fInterface ){
00949 case CGI_INTERFACE_CGI:
00950 return _getenv("HTTP_COOKIE");
00951 #ifdef WIN32
00952 case CGI_INTERFACE_ISAPI:
00953 ISAPIVAR(Cookie,"HTTP_COOKIE")
00954 #endif
00955 case CGI_INTERFACE_NSAPI:
00956 return NULL;
00957 case CGI_INTERFACE_FCGI:
00958 return NULL;
00959 case CGI_INTERFACE_DEBUG:
00960 return pCO->pDebugInfo->Cookie;
00961 }
00962 return NULL;
00963 }
00964
00965
00966 char *cgi_ServerSoftware(pCgiObject pCO
00967 ){
00968
00969 switch( pCO->fInterface ){
00970 case CGI_INTERFACE_CGI:
00971 return _getenv("SERVER_SOFTWARE");
00972 #ifdef WIN32
00973 case CGI_INTERFACE_ISAPI:
00974 ISAPIVAR(ServerSoftware,"SERVER_SOFTWARE")
00975 #endif
00976 case CGI_INTERFACE_NSAPI:
00977 return NULL;
00978 case CGI_INTERFACE_FCGI:
00979 return NULL;
00980 case CGI_INTERFACE_DEBUG:
00981 return pCO->pDebugInfo->ServerSoftware;
00982 }
00983 return NULL;
00984 }
00985
00986
00987 char *cgi_ServerName(pCgiObject pCO
00988 ){
00989
00990 switch( pCO->fInterface ){
00991 case CGI_INTERFACE_CGI:
00992 return _getenv("SERVER_NAME");
00993 #ifdef WIN32
00994 case CGI_INTERFACE_ISAPI:
00995 ISAPIVAR(ServerName,"SERVER_NAME")
00996 #endif
00997 case CGI_INTERFACE_NSAPI:
00998 return NULL;
00999 case CGI_INTERFACE_FCGI:
01000 return NULL;
01001 case CGI_INTERFACE_DEBUG:
01002 return pCO->pDebugInfo->ServerName;
01003 }
01004 return NULL;
01005 }
01006
01007
01008 char *cgi_GatewayInterface(pCgiObject pCO
01009 ){
01010
01011 switch( pCO->fInterface ){
01012 case CGI_INTERFACE_CGI:
01013 return _getenv("GATEWAY_INTERFACE");
01014 #ifdef WIN32
01015 case CGI_INTERFACE_ISAPI:
01016 return "ISAPI";
01017 #endif
01018 case CGI_INTERFACE_NSAPI:
01019 return NULL;
01020 case CGI_INTERFACE_FCGI:
01021 return NULL;
01022 case CGI_INTERFACE_DEBUG:
01023 return pCO->pDebugInfo->GatewayInterface;
01024 }
01025 return NULL;
01026 }
01027
01028
01029 char *cgi_ServerProtocol(pCgiObject pCO
01030 ){
01031
01032 switch( pCO->fInterface ){
01033 case CGI_INTERFACE_CGI:
01034 return _getenv("SERVER_PROTOCOL");
01035 #ifdef WIN32
01036 case CGI_INTERFACE_ISAPI:
01037 ISAPIVAR(ServerProtocol,"SERVER_PROTOCOL")
01038 #endif
01039 case CGI_INTERFACE_NSAPI:
01040 return NULL;
01041 case CGI_INTERFACE_FCGI:
01042 return NULL;
01043 case CGI_INTERFACE_DEBUG:
01044 return pCO->pDebugInfo->ServerProtocol;
01045 }
01046 return NULL;
01047 }
01048
01049
01050 char *cgi_ServerPort(pCgiObject pCO
01051 ){
01052
01053 switch( pCO->fInterface ){
01054 case CGI_INTERFACE_CGI:
01055 return _getenv("SERVER_PORT");
01056 #ifdef WIN32
01057 case CGI_INTERFACE_ISAPI:
01058 ISAPIVAR(ServerPort,"SERVER_PORT")
01059 #endif
01060 case CGI_INTERFACE_NSAPI:
01061 return NULL;
01062 case CGI_INTERFACE_FCGI:
01063 return NULL;
01064 case CGI_INTERFACE_DEBUG:
01065 return pCO->pDebugInfo->ServerPort;
01066 }
01067 return NULL;
01068 }
01069
01070
01071 char *cgi_RequestMethod(pCgiObject pCO
01072 ){
01073
01074 switch( pCO->fInterface ){
01075 case CGI_INTERFACE_CGI:
01076 return _getenv("REQUEST_METHOD");
01077 #ifdef WIN32
01078 case CGI_INTERFACE_ISAPI:
01079 ISAPIVAR(RequestMethod,"REQUEST_METHOD");
01080 #endif
01081 case CGI_INTERFACE_NSAPI:
01082 return NULL;
01083 case CGI_INTERFACE_FCGI:
01084 return NULL;
01085 case CGI_INTERFACE_DEBUG:
01086 return pCO->pDebugInfo->RequestMethod;
01087 }
01088 return NULL;
01089 }
01090
01091
01092 char *cgi_PathInfo(pCgiObject pCO
01093 ){
01094
01095 switch( pCO->fInterface ){
01096 case CGI_INTERFACE_CGI:
01097 return _getenv("PATH_INFO");
01098 #ifdef WIN32
01099 case CGI_INTERFACE_ISAPI:
01100 ISAPIVAR(PathInfo,"PATH_INFO")
01101 #endif
01102 case CGI_INTERFACE_NSAPI:
01103 return NULL;
01104 case CGI_INTERFACE_FCGI:
01105 return NULL;
01106 case CGI_INTERFACE_DEBUG:
01107 return pCO->pDebugInfo->PathInfo;
01108 }
01109 return NULL;
01110 }
01111
01112
01113 char *cgi_PathTranslated(pCgiObject pCO
01114 ){
01115
01116 switch( pCO->fInterface ){
01117 case CGI_INTERFACE_CGI:
01118 return _getenv("PATH_TRANSLATED");
01119 #ifdef WIN32
01120 case CGI_INTERFACE_ISAPI:
01121 ISAPIVAR(PathTranslated,"PATH_TRANSLATED")
01122 #endif
01123 case CGI_INTERFACE_NSAPI:
01124 return NULL;
01125 case CGI_INTERFACE_FCGI:
01126 return NULL;
01127 case CGI_INTERFACE_DEBUG:
01128 return pCO->pDebugInfo->PathTranslated;
01129 }
01130 return NULL;
01131 }
01132
01133
01134 char *cgi_ScriptName(pCgiObject pCO
01135 ){
01136
01137 switch( pCO->fInterface ){
01138 case CGI_INTERFACE_CGI:
01139 return _getenv("SCRIPT_NAME");
01140 #ifdef WIN32
01141 case CGI_INTERFACE_ISAPI:
01142 ISAPIVAR(ScriptName,"SCRIPT_NAME")
01143 #endif
01144 case CGI_INTERFACE_NSAPI:
01145 return NULL;
01146 case CGI_INTERFACE_FCGI:
01147 return NULL;
01148 case CGI_INTERFACE_DEBUG:
01149 return pCO->pDebugInfo->ScriptName;
01150 }
01151 return NULL;
01152 }
01153
01154
01155 char *cgi_QueryString(pCgiObject pCO
01156 ){
01157
01158 switch( pCO->fInterface ){
01159 case CGI_INTERFACE_CGI:
01160 return _getenv("QUERY_STRING");
01161 #ifdef WIN32
01162 case CGI_INTERFACE_ISAPI:
01163 ISAPIVAR(QueryString,"QUERY_STRING")
01164 #endif
01165 case CGI_INTERFACE_NSAPI:
01166 return NULL;
01167 case CGI_INTERFACE_FCGI:
01168 return NULL;
01169 case CGI_INTERFACE_DEBUG:
01170 return pCO->pDebugInfo->QueryString;
01171 }
01172 return NULL;
01173 }
01174
01175
01176 char *cgi_RemoteHost(pCgiObject pCO
01177 ){
01178
01179 switch( pCO->fInterface ){
01180 case CGI_INTERFACE_CGI:
01181 return _getenv("REMOTE_HOST");
01182 #ifdef WIN32
01183 case CGI_INTERFACE_ISAPI:
01184 ISAPIVAR(RemoteHost,"REMOTE_HOST")
01185 #endif
01186 case CGI_INTERFACE_NSAPI:
01187 return NULL;
01188 case CGI_INTERFACE_FCGI:
01189 return NULL;
01190 case CGI_INTERFACE_DEBUG:
01191 return pCO->pDebugInfo->RemoteHost;
01192 }
01193 return NULL;
01194 }
01195
01196
01197 char *cgi_RemoteAddress(pCgiObject pCO
01198 ){
01199
01200 switch( pCO->fInterface ){
01201 case CGI_INTERFACE_CGI:
01202 return _getenv("REMOTE_ADDR");
01203 #ifdef WIN32
01204 case CGI_INTERFACE_ISAPI:
01205 ISAPIVAR(RemoteAddress,"REMOTE_ADDR")
01206 #endif
01207 case CGI_INTERFACE_NSAPI:
01208 return NULL;
01209 case CGI_INTERFACE_FCGI:
01210 return NULL;
01211 case CGI_INTERFACE_DEBUG:
01212 return pCO->pDebugInfo->RemoteAddress;
01213 }
01214 return NULL;
01215 }
01216
01217
01218 char *cgi_AuthType(pCgiObject pCO
01219 ){
01220
01221 switch( pCO->fInterface ){
01222 case CGI_INTERFACE_CGI:
01223 return _getenv("AUTH_TYPE");
01224 #ifdef WIN32
01225 case CGI_INTERFACE_ISAPI:
01226 ISAPIVAR(AuthType,"AUTH_TYPE")
01227 #endif
01228 case CGI_INTERFACE_NSAPI:
01229 return NULL;
01230 case CGI_INTERFACE_FCGI:
01231 return NULL;
01232 case CGI_INTERFACE_DEBUG:
01233 return pCO->pDebugInfo->AuthType;
01234 }
01235 return NULL;
01236 }
01237
01238
01239 char *cgi_RemoteUser(pCgiObject pCO
01240 ){
01241
01242 switch( pCO->fInterface ){
01243 case CGI_INTERFACE_CGI:
01244 return _getenv("REMOTE_USER");
01245 #ifdef WIN32
01246 case CGI_INTERFACE_ISAPI:
01247 ISAPIVAR(RemoteUser,"REMOTE_USER")
01248 #endif
01249 case CGI_INTERFACE_NSAPI:
01250 return NULL;
01251 case CGI_INTERFACE_FCGI:
01252 return NULL;
01253 case CGI_INTERFACE_DEBUG:
01254 return pCO->pDebugInfo->RemoteUser;
01255 }
01256 return NULL;
01257 }
01258
01259
01260 char *cgi_RemoteIdent(pCgiObject pCO
01261 ){
01262
01263 switch( pCO->fInterface ){
01264 case CGI_INTERFACE_CGI:
01265 return _getenv("REMOTE_IDENT");
01266 #ifdef WIN32
01267 case CGI_INTERFACE_ISAPI:
01268 return NULL;
01269 #endif
01270 case CGI_INTERFACE_NSAPI:
01271 return NULL;
01272 case CGI_INTERFACE_FCGI:
01273 return NULL;
01274 case CGI_INTERFACE_DEBUG:
01275 return pCO->pDebugInfo->RemoteIdent;
01276 }
01277 return NULL;
01278 }
01279
01280
01281 char *cgi_ContentType(pCgiObject pCO
01282 ){
01283
01284 switch( pCO->fInterface ){
01285 case CGI_INTERFACE_CGI:
01286 return _getenv("CONTENT_TYPE");
01287 #ifdef WIN32
01288 case CGI_INTERFACE_ISAPI:
01289 ISAPIVAR(ContentType,"CONTENT_TYPE")
01290 #endif
01291 case CGI_INTERFACE_NSAPI:
01292 return NULL;
01293 case CGI_INTERFACE_FCGI:
01294 return NULL;
01295 case CGI_INTERFACE_DEBUG:
01296 return pCO->pDebugInfo->ContentType;
01297 }
01298 return NULL;
01299 }
01300
01301
01302 char *cgi_ContentLength(pCgiObject pCO
01303 ){
01304
01305 switch( pCO->fInterface ){
01306 case CGI_INTERFACE_CGI:
01307 return _getenv("CONTENT_LENGTH");
01308 #ifdef WIN32
01309 case CGI_INTERFACE_ISAPI:
01310 ISAPIVAR(ContentLength,"CONTENT_LENGTH")
01311 #endif
01312 case CGI_INTERFACE_NSAPI:
01313 return NULL;
01314 case CGI_INTERFACE_FCGI:
01315 return NULL;
01316 case CGI_INTERFACE_DEBUG:
01317 return pCO->pDebugInfo->ContentLength;
01318 }
01319 return NULL;
01320 }
01321
01322
01323 char *cgi_UserAgent(pCgiObject pCO
01324 ){
01325
01326 switch( pCO->fInterface ){
01327 case CGI_INTERFACE_CGI:
01328 return _getenv("HTTP_USER_AGENT");
01329 #ifdef WIN32
01330 case CGI_INTERFACE_ISAPI:
01331 return NULL;
01332 #endif
01333 case CGI_INTERFACE_NSAPI:
01334 return NULL;
01335 case CGI_INTERFACE_FCGI:
01336 return NULL;
01337 case CGI_INTERFACE_DEBUG:
01338 return pCO->pDebugInfo->UserAgent;
01339 }
01340 return NULL;
01341 }
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380 int cgi_ResizeBuffer(pCgiObject pCO,
01381 unsigned long lNewSize
01382 ){
01383
01384
01385 char *s;
01386
01387 if( lNewSize <= pCO->cbBuffer )return 1;
01388
01389 s = (char *)pCO->pszBuffer;
01390 pCO->pszBuffer = ALLOC(lNewSize);
01391 if( pCO->pszBuffer == NULL ){
01392 pCO->pszBuffer = (unsigned char *)s;
01393 return 0;
01394 }
01395 if( s )
01396 memcpy(pCO->pszBuffer,s,pCO->cbFill);
01397 pCO->cbBuffer = lNewSize;
01398 if( s )
01399 FREE(s);
01400 return 1;
01401 }
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415 long cgi_FillBuffer(pCgiObject pCO
01416 ){
01417
01418
01419 int ch;
01420 long lchcount;
01421
01422 lchcount = 0;
01423 while( pCO->cbFill < pCO->cbBuffer ){
01424 ch = GETCHAR();
01425 if( ch == EOF )return lchcount;
01426 lchcount ++;
01427 pCO->pszBuffer[pCO->cbFill++] = ch;
01428 }
01429 return lchcount;
01430 }
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447 void cgi_ShiftBuffer(pCgiObject pCO,
01448 unsigned long nch
01449 ){
01450
01451
01452 unsigned long i,j;
01453
01454 if( nch == 0 )return;
01455 for( i=0, j=nch ; j < pCO->cbFill ; i++, j++ )
01456 pCO->pszBuffer[i] = pCO->pszBuffer[j];
01457 if( pCO->cbFill > nch )
01458 pCO->cbFill -= nch;
01459 else
01460 pCO->cbFill = 0;
01461 if( pCO->lBufferPosition > nch )
01462 pCO->lBufferPosition -= nch;
01463 else
01464 pCO->lBufferPosition = 0;
01465 return;
01466 }
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479 void cgi_NormalizeBuffer(pCgiObject pCO
01480 ){
01481
01482
01483
01484 if( pCO->lBufferPosition == 0 )return;
01485 cgi_ShiftBuffer(pCO,pCO->lBufferPosition);
01486 }
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504 long cgi_SkipAfterBoundary(pCgiObject pCO
01505 ){
01506
01507
01508 unsigned long i;
01509
01510
01511
01512 if( !cgi_ResizeBuffer(pCO,pCO->cbBoundary+4) )return CGI_ERROR_MEMORY_LOW;
01513
01514 do{
01515 for( i=0 ; i+1 < pCO->cbFill ; i++ ){
01516 if( pCO->pszBuffer[i] == '-' && pCO->pszBuffer[i+1] == '-'){
01517 cgi_ShiftBuffer(pCO,i+2);
01518 cgi_FillBuffer(pCO);
01519
01520
01521
01522 if( pCO->cbFill < pCO->cbBoundary )return CGI_ERROR_EOF;
01523
01524
01525 if( memcmp(pCO->pszBuffer,pCO->pszBoundary,pCO->cbBoundary) )
01526 continue;
01527 else{
01528
01529 if( (pCO->cbBoundary < pCO->cbFill && pCO->pszBuffer[pCO->cbBoundary] == '-') &&
01530 (pCO->cbBoundary+1 < pCO->cbFill && pCO->pszBuffer[pCO->cbBoundary+1] == '-' ) ){
01531
01532 return CGI_ERROR_EOF;
01533 }
01534 if( (pCO->cbBoundary < pCO->cbFill && pCO->pszBuffer[pCO->cbBoundary] != CR) ||
01535 (pCO->cbBoundary+1 < pCO->cbFill && pCO->pszBuffer[pCO->cbBoundary+1] != LF) ){
01536
01537 continue;
01538 }
01539 if( !(pCO->cbBoundary < pCO->cbFill) )return CGI_ERROR_EOF;
01540 cgi_ShiftBuffer(pCO,pCO->cbBoundary+2);
01541 cgi_FillBuffer(pCO);
01542 if( pCO->cbFill == 0 )return CGI_ERROR_EOF;
01543 pCO->lBufferPosition = 0;
01544 return 0;
01545 }
01546 }
01547 }
01548
01549
01550 cgi_EmptyBuffer(pCO);
01551
01552 if( ! cgi_FillBuffer(pCO) )return CGI_ERROR_EOF;
01553 }while(1);
01554 }
01555
01556
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572 int cgi_GetNextByte(pCgiObject pCO
01573 ){
01574
01575
01576
01577
01578
01579
01580 if( !cgi_ResizeBuffer(pCO,pCO->cbBoundary+6) )return EOF;
01581
01582
01583 if( pCO->cbFill == 0 && cgi_FillBuffer(pCO) == 0 )return EOF;
01584
01585
01586
01587 if( pCO->lBufferPosition + 4 > pCO->cbFill )cgi_NormalizeBuffer(pCO);
01588 if( pCO->cbFill == 0 && cgi_FillBuffer(pCO) == 0 )return EOF;
01589 if( pCO->cbFill < 4 )cgi_FillBuffer(pCO);
01590
01591
01592
01593 if( pCO->pszBuffer[pCO->lBufferPosition] == CR &&
01594 pCO->lBufferPosition+1 < pCO->cbFill &&
01595 pCO->pszBuffer[pCO->lBufferPosition+1] == LF &&
01596 pCO->lBufferPosition+2 < pCO->cbFill &&
01597 pCO->pszBuffer[pCO->lBufferPosition+2] == '-' &&
01598 pCO->lBufferPosition+3 < pCO->cbFill &&
01599 pCO->pszBuffer[pCO->lBufferPosition+3] == '-' ){
01600
01601 cgi_NormalizeBuffer(pCO);
01602
01603
01604
01605 if( pCO->cbFill >= pCO->cbBoundary+4 &&
01606 !memcmp(pCO->pszBuffer+4,pCO->pszBoundary,pCO->cbBoundary) &&
01607 (
01608 (pCO->pszBuffer[pCO->cbBoundary+4] == CR && pCO->pszBuffer[pCO->cbBoundary+5] == LF )
01609 ||
01610 (pCO->pszBuffer[pCO->cbBoundary+4] == '-' && pCO->pszBuffer[pCO->cbBoundary+5] == '-' )
01611 )
01612 )return EOF;
01613 }
01614 return pCO->pszBuffer[pCO->lBufferPosition++];
01615 }
01616
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631 int cgi_GetNextChar(pCgiObject pCO
01632 ){
01633
01634
01635
01636 if( pCO->cbBuffer == 0 && !cgi_ResizeBuffer(pCO,pCO->lBufferIncrease) )return EOF;
01637
01638
01639 if( pCO->cbFill == 0 && cgi_FillBuffer(pCO) == 0 )return EOF;
01640
01641 if( pCO->lBufferPosition >= pCO->cbFill ){
01642 cgi_EmptyBuffer(pCO);
01643 if( cgi_FillBuffer(pCO) == 0 )return EOF;
01644 }
01645 return pCO->pszBuffer[pCO->lBufferPosition++];
01646 }
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664
01665
01666
01667
01668 long cgi_ReadHeader(pCgiObject pCO,
01669 pSymbolList *pHeader
01670 ){
01671
01672
01673 unsigned long i,j,k,w;
01674 char *pszHeader,*pszValue;
01675 char *pszHeaderBuffer;
01676
01677 cgi_NormalizeBuffer(pCO);
01678
01679
01680 i = 0;
01681 do{
01682 if( i+4 >= pCO->cbFill ){
01683 if( pCO->cbFill == pCO->cbBuffer )
01684 if( pCO->cbBuffer+pCO->lBufferIncrease > pCO->lBufferMax )return CGI_ERROR_BUFFER_OVERFLOW;
01685 if( !cgi_ResizeBuffer(pCO,pCO->cbBuffer+pCO->lBufferIncrease) )return CGI_ERROR_MEMORY_LOW;
01686 if( cgi_FillBuffer(pCO) == 0 )return CGI_ERROR_ILLF_MULTI1;
01687 }
01688 if( pCO->pszBuffer[i+0] == CR &&
01689 pCO->pszBuffer[i+1] == LF &&
01690 pCO->pszBuffer[i+2] == CR &&
01691 pCO->pszBuffer[i+3] == LF )break;
01692 i++;
01693 }while(1);
01694
01695
01696 for( j = 0 ; j < i ; j++ ){
01697 if( pCO->pszBuffer[j+0] == CR &&
01698 pCO->pszBuffer[j+1] == LF &&
01699 isspace(pCO->pszBuffer[j+2]) ){
01700 pCO->pszBuffer[j] = ' ';
01701
01702 for( k=j+3 ; k < i && isspace(pCO->pszBuffer[k]) ; k++ )continue;
01703
01704 for( w=j+1 ; k < pCO->cbFill ; w++, k++ )
01705 pCO->pszBuffer[w] = pCO->pszBuffer[k];
01706
01707 pCO->cbFill -= k-w;
01708 i -= k-w;
01709 }
01710 }
01711
01712 pszHeaderBuffer = ALLOC(sizeof(char)*i+1);
01713 if( pszHeaderBuffer == NULL )return CGI_ERROR_MEMORY_LOW;
01714 memcpy(pszHeaderBuffer,pCO->pszBuffer,i+1);
01715 cgi_ShiftBuffer(pCO,i+4);
01716
01717 k = 0;
01718 while( k<=i ){
01719 pszHeader = pszHeaderBuffer+k;
01720 while( k<=i && pszHeaderBuffer[k] != ':' )k++;
01721 if( !(k<=i) )return CGI_ERROR_ILLF_MULTI2;
01722 pszHeaderBuffer[k] = (char)0;
01723 k++;
01724 while( k<=i && isspace(pszHeaderBuffer[k]) )k++;
01725 if( !(k<=i) )return CGI_ERROR_ILLF_MULTI3;
01726 pszValue = pszHeaderBuffer+k;
01727 while( k<=i && pszHeaderBuffer[k] != CR )k++;
01728 if( !(k<=i) )return CGI_ERROR_ILLF_MULTI4;
01729 pszHeaderBuffer[k] = (char)0;
01730 *pHeader = ALLOC(sizeof(SymbolList));
01731 if( *pHeader == NULL )return CGI_ERROR_ILLF_MULTI5;
01732 (*pHeader)->symbol = pszHeader;
01733 (*pHeader)->value = pszValue;
01734 (*pHeader)->fp = NULL;
01735 (*pHeader)->file = NULL;
01736 (*pHeader)->next = NULL;
01737 pHeader = &((*pHeader)->next);
01738 if( !(k<=i) )return 0;
01739 k++;
01740 while( k<=i && (pszHeaderBuffer[k] == CR || pszHeaderBuffer[k] == LF) )k++;
01741 }
01742 return 0;
01743 }
01744
01745
01746
01747
01748
01749
01750
01751
01752
01753
01754
01755
01756
01757
01758 int cgi_ResizeThisBuffer(pCgiObject pCO,
01759 char **ppszBuffer,
01760 long *plOldSize,
01761 long lNewSize
01762 ){
01763
01764
01765 char *s;
01766
01767 if( lNewSize <= *plOldSize )return 1;
01768
01769 s = *ppszBuffer;
01770 *ppszBuffer = ALLOC(lNewSize);
01771 if( *ppszBuffer == NULL ){
01772 *ppszBuffer = s;
01773 return 0;
01774 }
01775 memcpy(*ppszBuffer,s,*plOldSize);
01776 *plOldSize = lNewSize;
01777 if( s )
01778 FREE(s);
01779 return 1;
01780 }
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792
01793
01794
01795
01796 void cgi_FillSymbolAndFile(pCgiObject pCO,
01797 char *pszContentDisposition,
01798 pSymbolList pHeader
01799 ){
01800
01801
01802 char *s,*r;
01803 long Len;
01804
01805 if( pszContentDisposition == NULL )return;
01806
01807 s = pszContentDisposition;
01808 while( *s && memcmp(s,"name=",5) )s++;
01809 if( !*s )goto HandleFileName;
01810 s += 5;
01811 while( *s && isspace(*s) )s++;
01812 if( *s == '\"' ){
01813 s++;
01814 r = s;
01815 while( *r && *r != '\"' )r++;
01816 }else{
01817 r = s;
01818 while( *r && *r != ';' )r++;
01819 }
01820 Len = r-s;
01821 pHeader->symbol = ALLOC(Len+1);
01822 if( pHeader->symbol == NULL )return;
01823 memcpy(pHeader->symbol,s,Len);
01824 pHeader->symbol[Len] = (char)0;
01825
01826 HandleFileName:;
01827 s = pszContentDisposition;
01828 while( *s && memcmp(s,"filename=",9) )s++;
01829 if( !*s )return;
01830 s += 9;
01831 while( *s && isspace(*s) )s++;
01832 if( *s == '\"' ){
01833 s++;
01834 r = s;
01835 while( *r && *r != '\"' )r++;
01836 }else{
01837 r = s;
01838 while( *r && *r != ';' )r++;
01839 }
01840 Len = r-s;
01841 pHeader->file = ALLOC(Len+1);
01842 if( pHeader->file == NULL )return;
01843 memcpy(pHeader->file,s,Len);
01844 pHeader->file[Len] = (char)0;
01845 }
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864 long cgi_GetMultipartParameters(pCgiObject pCO
01865 ){
01866
01867
01868 char *s,*pszContentDisposition;
01869 unsigned long i;
01870 int ch,lErrorCode;
01871 pSymbolList pThisHeader;
01872 pSymbolList *pParamp;
01873 char *pszBuffer;
01874 unsigned long cbBuffer;
01875
01876
01877 cgi_GetGetParameters(pCO);
01878
01879 s = cgi_ContentType(pCO) + 19;
01880 while( *s && memcmp(s,"boundary=",9) )s++;
01881 if( *s ){
01882 pCO->pszBoundary = s+9;
01883 pCO->cbBoundary = strlen(pCO->pszBoundary);
01884 if( (lErrorCode=cgi_SkipAfterBoundary(pCO)) == CGI_ERROR_EOF )return CGI_ERROR_ILLF_MULTI6;
01885 if( lErrorCode == CGI_ERROR_MEMORY_LOW )return CGI_ERROR_MEMORY_LOW;
01886 }else{
01887
01888
01889
01890 cgi_ResizeBuffer(pCO,pCO->lBufferIncrease);
01891 i = 0;
01892 do{
01893 if( ! cgi_FillBuffer(pCO) )
01894
01895 return CGI_ERROR_ILLF_MULTI7;
01896
01897 while( i >= pCO->cbFill-1 ){
01898
01899 if( pCO->cbBuffer+pCO->lBufferIncrease > pCO->lBufferMax )return CGI_ERROR_BUFFER_OVERFLOW;
01900 if( !cgi_ResizeBuffer(pCO,pCO->cbBuffer+pCO->lBufferIncrease) )return CGI_ERROR_MEMORY_LOW;
01901 }
01902 while( i < pCO->cbFill-1 ){
01903 if( pCO->pszBuffer[i] == CR && pCO->pszBuffer[i+1] == LF ){
01904 pCO->pszBoundary = ALLOC(i-2);
01905 if( pCO->pszBoundary == NULL )return CGI_ERROR_MEMORY_LOW;
01906 pCO->cbBoundary = i-2;
01907 memcpy(pCO->pszBoundary,pCO->pszBuffer+2,i);
01908 cgi_ShiftBuffer(pCO,i+2);
01909 goto OUTER_BREAK;
01910 }
01911 i++;
01912 }
01913 }while(1);
01914 OUTER_BREAK:;
01915 }
01916
01917 cgi_ResizeBuffer(pCO,pCO->lBufferIncrease);
01918 pszBuffer = NULL;
01919 cbBuffer = 0L;
01920 pParamp = &(pCO->pPostParameters);
01921 while( 1 ){
01922 pThisHeader = NULL;
01923 if( lErrorCode = cgi_ReadHeader(pCO,&pThisHeader) )return lErrorCode;
01924 *pParamp = ALLOC(sizeof(SymbolList));
01925 if( *pParamp == NULL )return CGI_ERROR_MEMORY_LOW;
01926 (*pParamp)->symbol = NULL;
01927 (*pParamp)->file = NULL;
01928 (*pParamp)->value = NULL;
01929 (*pParamp)->fp = NULL;
01930 (*pParamp)->file = NULL;
01931 (*pParamp)->next = NULL;
01932 (*pParamp)->pHeaders = pThisHeader;
01933
01934 pszContentDisposition = cgi_Header(pCO,"Content-Disposition",(*pParamp)->pHeaders);
01935
01936 cgi_FillSymbolAndFile(pCO,pszContentDisposition,*pParamp);
01937 if( (*pParamp)->file ){
01938 (*pParamp)->fp = tmpfile();
01939 if( (*pParamp)->fp == NULL )return CGI_ERROR_MEMORY_LOW;
01940 i = 0;
01941 while( (ch=cgi_GetNextByte(pCO)) != EOF ){
01942 putc(ch,(*pParamp)->fp);
01943 if( ++i > pCO->lFileMax )return CGI_ERROR_FILEMAX;
01944 }
01945 (*pParamp)->len = i;
01946 fseek((*pParamp)->fp,0L,SEEK_SET);
01947 }else{
01948 cgi_ResizeThisBuffer(pCO,&pszBuffer,&cbBuffer,pCO->lBufferIncrease);
01949 i = 0;
01950 while( (ch=cgi_GetNextByte(pCO)) != EOF ){
01951 while( i >= cbBuffer-1 ){
01952 if( pCO->lBufferIncrease + cbBuffer > pCO->lBufferMax )return CGI_ERROR_BUFFER_OVERFLOW;
01953 cgi_ResizeThisBuffer(pCO,&pszBuffer,&cbBuffer,cbBuffer+pCO->lBufferIncrease);
01954 }
01955 pszBuffer[i++] = ch;
01956 }
01957 pszBuffer[i] = (char)0;
01958 (*pParamp)->value = ALLOC(i+1);
01959 if( (*pParamp)->value == NULL )return CGI_ERROR_MEMORY_LOW;
01960 memcpy((*pParamp)->value,pszBuffer,i+1);
01961 }
01962 pParamp = &((*pParamp)->next);
01963 if( (lErrorCode = cgi_SkipAfterBoundary(pCO)) == CGI_ERROR_EOF ){
01964 FREE(pszBuffer);
01965 return 0;
01966 }
01967
01968 if( lErrorCode == CGI_ERROR_MEMORY_LOW )return CGI_ERROR_MEMORY_LOW;
01969 }
01970 }
01971
01972
01973
01974
01975
01976
01977
01978
01979 long cgi_GetGetParameters(pCgiObject pCO
01980 ){
01981
01982
01983 char *s,*Symbol,*Value;
01984 long Len;
01985 pSymbolList *pParamp;
01986
01987 s = cgi_QueryString(pCO);
01988 pParamp = &(pCO->pGetParameters);
01989 while( s && *s ){
01990 Symbol = s;
01991 while( *s && *s != '=' && *s != '&' )s++;
01992 Len = s - Symbol;
01993 *pParamp = ALLOC(sizeof(SymbolList));
01994 if( *pParamp == NULL )return CGI_ERROR_MEMORY_LOW;
01995 (*pParamp)->fp = NULL;
01996 (*pParamp)->file = NULL;
01997 (*pParamp)->value = NULL;
01998 (*pParamp)->next = NULL;
01999 (*pParamp)->pHeaders = NULL;
02000 (*pParamp)->symbol = ALLOC(Len+1);
02001 if( (*pParamp)->symbol == NULL )return CGI_ERROR_MEMORY_LOW;
02002 memcpy((*pParamp)->symbol,Symbol,Len);
02003 (*pParamp)->symbol[Len] = (char)0;
02004 Len++;
02005 unescape((*pParamp)->symbol,&Len);
02006 if( !*s )break;
02007 if( *s == '=' )s++;
02008 Value = s;
02009 while( *s && *s != '&' )s++;
02010 Len = s - Value;
02011 (*pParamp)->value = ALLOC(Len+1);
02012 if( (*pParamp)->value == NULL )return CGI_ERROR_MEMORY_LOW;
02013 memcpy((*pParamp)->value,Value,Len);
02014 (*pParamp)->value[Len] = (char)0;
02015 Len++;
02016 unescape((*pParamp)->value,&Len);
02017 pParamp = &((*pParamp)->next);
02018 if( *s )s++;
02019 }
02020 return 0;
02021 }
02022
02023
02024
02025
02026
02027
02028
02029
02030 long cgi_GetPostParameters(pCgiObject pCO
02031 ){
02032
02033
02034 char *s,*Symbol,*Value;
02035 long Len;
02036 pSymbolList *pParamp;
02037
02038
02039 cgi_GetGetParameters(pCO);
02040
02041 if( ! cgi_ResizeBuffer(pCO,pCO->lBufferIncrease) )return CGI_ERROR_MEMORY_LOW;
02042
02043 while( cgi_FillBuffer(pCO) ){
02044 if( pCO->cbBuffer+pCO->lBufferIncrease > pCO->lBufferMax )return CGI_ERROR_BUFFER_OVERFLOW;
02045 if( !cgi_ResizeBuffer(pCO,pCO->cbBuffer+pCO->lBufferIncrease) )return CGI_ERROR_MEMORY_LOW;
02046 }
02047 s = (char *)pCO->pszBuffer;
02048
02049
02050 s[ pCO->cbFill ] = (char)0;
02051
02052 pParamp = &(pCO->pPostParameters);
02053 while( *s ){
02054 Symbol = s;
02055 while( *s && *s != '=' && *s != '&' )s++;
02056 Len = s - Symbol;
02057 *pParamp = ALLOC(sizeof(SymbolList));
02058 if( *pParamp == NULL )return CGI_ERROR_MEMORY_LOW;
02059 (*pParamp)->fp = NULL;
02060 (*pParamp)->file = NULL;
02061 (*pParamp)->value = NULL;
02062 (*pParamp)->next = NULL;
02063 (*pParamp)->pHeaders = NULL;
02064 (*pParamp)->symbol = ALLOC(Len+1);
02065 if( (*pParamp)->symbol == NULL )return CGI_ERROR_MEMORY_LOW;
02066 memcpy((*pParamp)->symbol,Symbol,Len);
02067 (*pParamp)->symbol[Len] = (char)0;
02068 Len++;
02069 unescape((*pParamp)->symbol,&Len);
02070 if( !*s )break;
02071 if( *s == '=' )s++;
02072 Value = s;
02073 while( *s && *s != '&' )s++;
02074 Len = s - Value;
02075 (*pParamp)->value = ALLOC(Len+1);
02076 if( (*pParamp)->value == NULL )return CGI_ERROR_MEMORY_LOW;
02077 memcpy((*pParamp)->value,Value,Len);
02078 (*pParamp)->value[Len] = (char)0;
02079 Len++;
02080 unescape((*pParamp)->value,&Len);
02081 pParamp = &((*pParamp)->next);
02082 if( *s )s++;
02083 }
02084 return 0;
02085 }