Go to the documentation of this file.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
00067
00068
00069 #include <sys/heap.h>
00070 #include <sys/confnet.h>
00071 #include <sys/version.h>
00072
00073 #include <stdlib.h>
00074 #include <string.h>
00075 #include <io.h>
00076 #include <fcntl.h>
00077 #include <memdebug.h>
00078
00079 #include <pro/httpd.h>
00080 #include <pro/asp.h>
00081 #include <arpa/inet.h>
00082
00083 extern CONFNET confnet;
00084
00085
00086
00087
00088
00089
00090
00091
00092 #define MAX_BUFFER_SIZE 256
00093
00094
00095
00096
00097
00098 #define MAX_ASP_FUNC_SIZE 64
00099
00100 enum {
00101 ASP_STATE_IDLE = 0,
00102 ASP_STATE_START,
00103 ASP_STATE_COPY_FUNC
00104 };
00105
00106
00107
00108
00109 static int (*asp_callback)(char *, FILE *) = NULL;
00110
00111
00112
00113
00114
00115
00116
00117 static void ProcessAspFunction(char *pASPFunction, FILE * stream)
00118 {
00119 if (strstr(pASPFunction, "nut_version") != NULL) {
00120 fprintf(stream, "%s", NutVersionString());
00121 return;
00122 }
00123
00124 if (strstr(pASPFunction, "nut_mac_addr") != NULL) {
00125 fprintf(stream, "%02X:%02X:%02X:%02X:%02X:%02X",
00126 confnet.cdn_mac[0], confnet.cdn_mac[1], confnet.cdn_mac[2],
00127 confnet.cdn_mac[3], confnet.cdn_mac[4], confnet.cdn_mac[5]);
00128 return;
00129 }
00130
00131 if (strstr(pASPFunction, "nut_ip_addr") != NULL) {
00132 fputs(inet_ntoa(confnet.cdn_ip_addr), stream);
00133 return;
00134 }
00135
00136 if (strstr(pASPFunction, "nut_ip_mask") != NULL) {
00137 fputs(inet_ntoa(confnet.cdn_ip_mask), stream);
00138 return;
00139 }
00140
00141 if (strstr(pASPFunction, "nut_gateway") != NULL) {
00142 fputs(inet_ntoa(confnet.cdn_gateway), stream);
00143 return;
00144 }
00145
00146
00147
00148
00149 if (asp_callback != NULL) {
00150 asp_callback(pASPFunction, stream);
00151 }
00152 }
00153
00154
00155
00156
00157
00158
00159
00160 void NutHttpProcessAsp(FILE * stream, int fd, int file_len, char* http_root, REQUEST *req)
00161 {
00162 int n;
00163 char *pReadBuffer = NULL;
00164 char *pWriteBuffer = NULL;
00165 char *pASPFunction = NULL;
00166 char Data;
00167 int Size;
00168 long lFileLen;
00169 unsigned char bASPState = ASP_STATE_IDLE;
00170 int ReadCount;
00171 int WriteCount;
00172 unsigned char bASPFuncLen;
00173
00174 lFileLen = _filelength(fd);
00175
00176 Size = MAX_BUFFER_SIZE;
00177 WriteCount = 0;
00178 bASPFuncLen = 0;
00179 pASPFunction = malloc(MAX_ASP_FUNC_SIZE);
00180 pReadBuffer = malloc(Size);
00181
00182
00183
00184
00185 pWriteBuffer = malloc(Size + 1);
00186
00187 if ((pReadBuffer != NULL) && (pWriteBuffer != NULL) && (pASPFunction != NULL)) {
00188
00189 while (lFileLen) {
00190 if (lFileLen < MAX_BUFFER_SIZE) {
00191 Size = (int) lFileLen;
00192 }
00193
00194 n = _read(fd, pReadBuffer, Size);
00195
00196 for (ReadCount = 0; ReadCount < n; ReadCount++) {
00197
00198 Data = pReadBuffer[ReadCount];
00199
00200 switch (bASPState) {
00201
00202 case ASP_STATE_IDLE:
00203 if (Data == '<') {
00204 bASPState = ASP_STATE_START;
00205 }
00206 pWriteBuffer[WriteCount] = Data;
00207 WriteCount++;
00208 break;
00209
00210
00211 case ASP_STATE_START:
00212 if (Data == '%') {
00213 bASPState = ASP_STATE_COPY_FUNC;
00214 if (WriteCount) {
00215 WriteCount--;
00216 }
00217
00218
00219
00220
00221 if (WriteCount) {
00222 fwrite(pWriteBuffer, 1, WriteCount, stream);
00223 WriteCount = 0;
00224 }
00225 } else {
00226 bASPState = ASP_STATE_IDLE;
00227 pWriteBuffer[WriteCount] = Data;
00228 WriteCount++;
00229 }
00230 break;
00231
00232
00233 case ASP_STATE_COPY_FUNC:
00234 if (Data == '>') {
00235 bASPState = ASP_STATE_IDLE;
00236 pASPFunction[bASPFuncLen] = 0;
00237
00238 ProcessAspFunction(pASPFunction, stream);
00239 bASPFuncLen = 0;
00240 } else {
00241
00242
00243
00244 if (Data == '%') {
00245 Data = 0;
00246 }
00247
00248 pASPFunction[bASPFuncLen] = Data;
00249 bASPFuncLen++;
00250 if (bASPFuncLen >= MAX_ASP_FUNC_SIZE) {
00251
00252
00253
00254 bASPFuncLen = 0;
00255 }
00256 }
00257 break;
00258
00259 }
00260 }
00261
00262
00263
00264
00265
00266 if (WriteCount) {
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 if (pWriteBuffer[WriteCount - 1] == '<') {
00277 WriteCount--;
00278 fwrite(pWriteBuffer, 1, WriteCount, stream);
00279
00280
00281
00282
00283 pWriteBuffer[0] = '<';
00284 WriteCount = 1;
00285 } else {
00286 fwrite(pWriteBuffer, 1, WriteCount, stream);
00287 WriteCount = 0;
00288 }
00289 }
00290
00291 lFileLen -= (long) n;
00292 }
00293 }
00294
00295 if (pReadBuffer != NULL) {
00296 free(pReadBuffer);
00297 }
00298 if (pWriteBuffer != NULL) {
00299 free(pWriteBuffer);
00300 }
00301 if (pASPFunction != NULL) {
00302 free(pASPFunction);
00303 }
00304 }
00305
00306
00307
00308
00309
00310
00311 int NutRegisterAspCallback(int (*func) (char *, FILE *))
00312 {
00313 register int result = 0;
00314
00315 if (asp_callback == NULL) {
00316 asp_callback = func;
00317 } else {
00318 result = -1;
00319 }
00320
00321 return (result);
00322 }
00323
00332 void NutRegisterAsp(void)
00333 {
00334 NutSetMimeHandler(".asp", NutHttpProcessAsp);
00335 }
00336