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 #include <string.h>
00066 #if !defined(__IMAGECRAFT__)
00067 #include <alloca.h>
00068 #endif
00069 #include <sys/heap.h>
00070
00071 #include <pro/httpd.h>
00072
00077
00078 CGIFUNCTION *volatile cgiFunctionList = 0;
00079 char *cgiBinPath = NULL;
00080
00088 void NutRegisterCgiBinPath(char *path)
00089 {
00090 if (cgiBinPath) {
00091 NutHeapFree(cgiBinPath);
00092 }
00093
00094 cgiBinPath = NutHeapAlloc(strlen(path) + 1);
00095 strcpy(cgiBinPath, path);
00096 }
00097
00108 int NutCgiCheckRequest(FILE * stream, REQUEST * req)
00109 {
00110 #ifdef __GNUC__
00111
00112
00113
00114
00115 char * cgi_bin = "cgi-bin/";
00116 char * tmp;
00117 char * save_ptr = NULL;
00118
00119 if (cgiBinPath != NULL) {
00120 cgi_bin = cgiBinPath;
00121 }
00122 tmp = alloca(strlen(cgi_bin) + 1);
00123 strcpy(tmp, cgi_bin);
00124
00125 tmp = strtok_r(tmp, ";", &save_ptr);
00126
00127 do {
00128 if ((tmp != NULL) && (strncasecmp(req->req_url, tmp, strlen(tmp)) == 0)) {
00129 NutCgiProcessRequest(stream, req, strlen(tmp));
00130 return 1;
00131 }
00132 tmp = strtok_r(NULL, ";", &save_ptr);
00133 } while (tmp != NULL);
00134 #else
00135
00136
00137
00138
00139
00140 size_t len;
00141 CONST char *cp;
00142 CONST char *cgi_bin = cgiBinPath ? cgiBinPath : "cgi-bin/";
00143
00144 while (*cgi_bin) {
00145
00146 while (*cgi_bin == ';')
00147 cgi_bin++;
00148
00149 for (len = 0, cp = cgi_bin; *cp && *cp != ';'; len++, cp++);
00150 if (len) {
00151
00152 if (strncasecmp(req->req_url, cgi_bin, len) == 0) {
00153 NutCgiProcessRequest(stream, req, len);
00154 return 1;
00155 }
00156
00157 cgi_bin += len;
00158 }
00159 }
00160 #endif
00161 return 0;
00162 }
00163
00173 int NutRegisterCgi(char *name, int (*func) (FILE *, REQUEST *))
00174 {
00175 int unique_name = 1;
00176 CGIFUNCTION *cgi;
00177
00178 cgi = cgiFunctionList;
00179 while (cgi != NULL) {
00180 if (strcmp(name, cgi->cgi_name) == 0) {
00181 unique_name = 0;
00182 break;
00183 }
00184 cgi = cgi->cgi_next;
00185 }
00186
00187 if ((!unique_name) || ((cgi = NutHeapAlloc(sizeof(CGIFUNCTION))) == 0)) {
00188 return -1;
00189 }
00190 cgi->cgi_next = cgiFunctionList;
00191 cgi->cgi_name = NutHeapAlloc(strlen(name)+1);
00192 strcpy(cgi->cgi_name, name);
00193 cgi->cgi_func = func;
00194 cgiFunctionList = cgi;
00195
00196 return 0;
00197 }
00198
00209 void NutCgiProcessRequest(FILE * stream, REQUEST * req, int name_pos)
00210 {
00211 CGIFUNCTION *cgi;
00212
00213 if (req->req_method != METHOD_GET && req->req_method != METHOD_POST) {
00214 NutHttpSendError(stream, req, 501);
00215 return;
00216 }
00217
00218 for (cgi = cgiFunctionList; cgi; cgi = cgi->cgi_next) {
00219 if (strcasecmp(cgi->cgi_name, req->req_url + name_pos) == 0)
00220 break;
00221 }
00222 if (cgi == 0)
00223 NutHttpSendError(stream, req, 404);
00224 else if ((*cgi->cgi_func) (stream, req))
00225 NutHttpSendError(stream, req, 500);
00226 return;
00227 }
00228