lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include "wsIntrn.h" |
| 2 | #include "../interface5.0/zte_web_interface.h" |
| 3 | //#include "../interface5.0/zte_rest_comm_interface.h" |
| 4 | |
| 5 | |
| 6 | |
| 7 | static websUrlHandlerType *websUrlHandler; |
| 8 | static int websUrlHandlerMax; |
| 9 | static int urlHandlerOpenCount = 0; |
| 10 | |
| 11 | static int websUrlHandlerSort(const void *p1, const void *p2); |
| 12 | static char_t *websCondenseMultipleChars(char_t *strToCondense, char_t cCondense); |
| 13 | |
| 14 | static int websPublishHandler(webs_t wp, char_t *urlPrefix, char_t *webDir, |
| 15 | int sid, char_t *url, char_t *path, char_t *query); |
| 16 | |
| 17 | |
| 18 | |
| 19 | static int websUrlHandlerSort(const void *p1, const void *p2) |
| 20 | { |
| 21 | websUrlHandlerType *s1, *s2; |
| 22 | int rc; |
| 23 | |
| 24 | a_assert(p1); |
| 25 | a_assert(p2); |
| 26 | |
| 27 | s1 = (websUrlHandlerType*) p1; |
| 28 | s2 = (websUrlHandlerType*) p2; |
| 29 | |
| 30 | if ((s1->flags & WEBS_HANDLER_FIRST) || (s2->flags & WEBS_HANDLER_LAST)) { |
| 31 | return -1; |
| 32 | } |
| 33 | |
| 34 | if ((s2->flags & WEBS_HANDLER_FIRST) || (s1->flags & WEBS_HANDLER_LAST)) { |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | if ((rc = gstrcmp(s1->urlPrefix, s2->urlPrefix)) == 0) { |
| 39 | if (s1->len < s2->len) { |
| 40 | return 1; |
| 41 | } else if (s1->len > s2->len) { |
| 42 | return -1; |
| 43 | } |
| 44 | } |
| 45 | return -rc; |
| 46 | } |
| 47 | |
| 48 | int websUrlHandlerOpen() |
| 49 | { |
| 50 | if (++urlHandlerOpenCount == 1) { |
| 51 | #ifdef WEB_ASP |
| 52 | websAspOpen(); |
| 53 | #endif |
| 54 | websUrlHandler = NULL; |
| 55 | websUrlHandlerMax = 0; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int websUrlHandlerDefine(char_t *urlPrefix, char_t *webDir, int arg, |
| 61 | int (*handler)(webs_t wp, char_t *urlPrefix, char_t *webdir, int arg, |
| 62 | char_t *url, char_t *path, char_t *query), int flags) |
| 63 | { |
| 64 | websUrlHandlerType *sp; |
| 65 | int len; |
| 66 | |
| 67 | a_assert(urlPrefix); |
| 68 | a_assert(handler); |
| 69 | |
| 70 | len = (websUrlHandlerMax + 1) * sizeof(websUrlHandlerType); |
| 71 | if ((websUrlHandler = brealloc(B_L, websUrlHandler, len)) == NULL) { |
| 72 | return -1; |
| 73 | } |
| 74 | sp = &websUrlHandler[websUrlHandlerMax++]; |
| 75 | memset(sp, 0, sizeof(websUrlHandlerType)); |
| 76 | |
| 77 | sp->urlPrefix = bstrdup(B_L, urlPrefix); |
| 78 | if(sp->urlPrefix) |
| 79 | sp->len = gstrlen(sp->urlPrefix); |
| 80 | if (webDir) { |
| 81 | sp->webDir = bstrdup(B_L, webDir); |
| 82 | } else { |
| 83 | sp->webDir = bstrdup(B_L, T("")); |
| 84 | } |
| 85 | sp->handler = handler; |
| 86 | sp->arg = arg; |
| 87 | sp->flags = flags; |
| 88 | |
| 89 | qsort(websUrlHandler, websUrlHandlerMax, sizeof(websUrlHandlerType), |
| 90 | websUrlHandlerSort); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | void websUrlHandlerClose() |
| 95 | { |
| 96 | websUrlHandlerType *sp; |
| 97 | |
| 98 | if (--urlHandlerOpenCount <= 0) { |
| 99 | #ifdef WEB_ASP |
| 100 | websAspClose(); |
| 101 | #endif |
| 102 | for (sp = websUrlHandler; sp < &websUrlHandler[websUrlHandlerMax]; |
| 103 | sp++) { |
| 104 | bfree(B_L, sp->urlPrefix); |
| 105 | if (sp->webDir) { |
| 106 | bfree(B_L, sp->webDir); |
| 107 | } |
| 108 | } |
| 109 | bfree(B_L, websUrlHandler); |
| 110 | websUrlHandlerMax = 0; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | int websPublish(char_t *urlPrefix, char_t *path) |
| 115 | { |
| 116 | return websUrlHandlerDefine(urlPrefix, path, 0, websPublishHandler, 0); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | int websUrlHandlerDelete(int (*handler)(webs_t wp, char_t *urlPrefix, |
| 121 | char_t *webDir, int arg, char_t *url, char_t *path, char_t *query)) |
| 122 | { |
| 123 | websUrlHandlerType *sp; |
| 124 | int index; |
| 125 | |
| 126 | for (index = 0; index < websUrlHandlerMax; index++) { |
| 127 | sp = &websUrlHandler[index]; |
| 128 | if (sp->handler == handler) { |
| 129 | sp->handler = NULL; |
| 130 | return 0; |
| 131 | } |
| 132 | } |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | char_t *websGetPublishDir(char_t *path, char_t **urlPrefix) |
| 137 | { |
| 138 | websUrlHandlerType *sp; |
| 139 | int index; |
| 140 | |
| 141 | for (index = 0; index < websUrlHandlerMax; index++) { |
| 142 | sp = &websUrlHandler[index]; |
| 143 | if (sp->urlPrefix[0] == '\0') { |
| 144 | continue; |
| 145 | } |
| 146 | if (sp->handler && gstrncmp(sp->urlPrefix, path, sp->len) == 0) { |
| 147 | if (urlPrefix) { |
| 148 | *urlPrefix = sp->urlPrefix; |
| 149 | } |
| 150 | return sp->webDir; |
| 151 | } |
| 152 | } |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | static int websPublishHandler(webs_t wp, char_t *urlPrefix, char_t *webDir, |
| 158 | int sid, char_t *url, char_t *path, char_t *query) |
| 159 | { |
| 160 | int len; |
| 161 | |
| 162 | a_assert(websValid(wp)); |
| 163 | a_assert(path); |
| 164 | |
| 165 | len = gstrlen(urlPrefix) + 1; |
| 166 | websSetRequestPath(wp, webDir, &path[len]); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | #ifdef OBSOLETE_CODE |
| 171 | static int websTidyUrl(webs_t wp) |
| 172 | { |
| 173 | char_t *parts[64]; |
| 174 | char_t *token, *url, *tidyurl; |
| 175 | int i, len, npart; |
| 176 | |
| 177 | a_assert(websValid(wp)); |
| 178 | |
| 179 | |
| 180 | url = bstrdup(B_L, wp->url); |
| 181 | websDecodeUrl(url, url, gstrlen(url)); |
| 182 | |
| 183 | len = npart = 0; |
| 184 | parts[0] = NULL; |
| 185 | token = gstrtok(url, T("/")); |
| 186 | |
| 187 | while (token != NULL) { |
| 188 | if (gstrcmp(token, T("..")) == 0) { |
| 189 | if (npart > 0) { |
| 190 | npart--; |
| 191 | } |
| 192 | |
| 193 | } else if (gstrcmp(token, T(".")) != 0) { |
| 194 | parts[npart] = token; |
| 195 | len += gstrlen(token) + 1; |
| 196 | npart++; |
| 197 | } |
| 198 | token = gstrtok(NULL, T("/")); |
| 199 | } |
| 200 | |
| 201 | if (npart || (gstrcmp(url, T("/")) == 0) || (url[0] == '\0')) { |
| 202 | tidyurl = balloc(B_L, (len + 2) * sizeof(char_t)); |
| 203 | *tidyurl = '\0'; |
| 204 | |
| 205 | for (i = 0; i < npart; i++) { |
| 206 | gstrcat(tidyurl, T("/")); |
| 207 | gstrcat(tidyurl, parts[i]); |
| 208 | } |
| 209 | |
| 210 | bfree(B_L, url); |
| 211 | |
| 212 | bfree(B_L, wp->url); |
| 213 | wp->url = tidyurl; |
| 214 | return 0; |
| 215 | } else { |
| 216 | bfree(B_L, url); |
| 217 | return -1; |
| 218 | } |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | int websUrlHandlerRequest(webs_t wp) |
| 223 | { |
| 224 | websUrlHandlerType *sp; |
| 225 | int i, first; |
| 226 | int m=0; |
| 227 | int n=0; |
| 228 | int m_count=0; |
| 229 | int n_count=0; |
| 230 | |
| 231 | char zte_user_login_flag[10] = {0}; |
| 232 | char zte_web_global_flag[10] = {0}; // 1 means needn't LOGIN, 0 means need LOGIN |
| 233 | char_t *ip_address = NULL; |
| 234 | char dataCard[32] = {0}; |
| 235 | char userIpaddr[40] = {0}; |
| 236 | a_assert(websValid(wp)); |
| 237 | if(websValid(wp)== 0){ |
| 238 | softap_assert("websUrlHandlerRequest 1"); |
| 239 | } |
| 240 | |
| 241 | socketDeleteHandler(wp->sid); |
| 242 | wp->state = WEBS_PROCESSING; |
| 243 | websStats.handlerHits++; |
| 244 | |
| 245 | websSetRequestPath(wp, websGetDefaultDir(), NULL); |
| 246 | |
| 247 | |
| 248 | ip_address = websGetRequestIpaddr(wp); |
| 249 | #if 0 // kw 3 |
| 250 | if (NULL == ip_address) |
| 251 | { |
| 252 | slog(MISC_PRINT,SLOG_ERR,"ip_address is null."); |
| 253 | websDone(wp, 200); |
| 254 | return 0; |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | sc_cfg_get(NV_DATA_CARD,dataCard,sizeof(dataCard)); |
| 259 | sc_cfg_get(NV_USER_IP_ADDR,userIpaddr,sizeof(userIpaddr)); |
| 260 | if (0 == strcmp(dataCard,"1")) |
| 261 | { |
| 262 | strcpy(zte_user_login_flag,"ok"); |
| 263 | } |
| 264 | else if(0 == strcmp(userIpaddr,ip_address)) |
| 265 | { |
| 266 | sc_cfg_get("loginfo",zte_user_login_flag,sizeof(zte_user_login_flag)); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | strcpy(zte_user_login_flag,""); |
| 271 | } |
| 272 | if(strstr(wp->url, "messages")) |
| 273 | { |
| 274 | if(strcmp("ok", zte_user_login_flag) != 0) |
| 275 | { |
| 276 | slog(MISC_PRINT,SLOG_DEBUG,"websUrlHandlerRequest -> GET is not allowed: %s", wp->url); |
| 277 | |
| 278 | //zte_webs_feedback_top(wp, zte_web_get_login_page(wp)); /*×ÔÊÊÓ¦ÖÕ¶Ëä¯ÀÀÆ÷*/ |
| 279 | //websDone(wp, 200); |
| 280 | websRedirect(wp, zte_web_get_login_page(wp)); |
| 281 | return 0; |
| 282 | } |
| 283 | // allow pass |
| 284 | } |
| 285 | |
| 286 | if (strstr(wp->url, "default_parameter") ||strstr(wp->url, "version_parameter") ||strstr(wp->url, "custom_parameter") ||strstr(wp->url, "zteconfig/config")) |
| 287 | { |
| 288 | slog(MISC_PRINT,SLOG_DEBUG,"websUrlHandlerRequest -> GET is not allowed: %s", wp->url); |
| 289 | websError(wp, 404, T("Bad state")); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | //zte_nv_read("loginfo", zte_user_login_flag, sizeof(zte_user_login_flag)); |
| 294 | |
| 295 | if (strcmp("ok", zte_user_login_flag) != 0) // not login |
| 296 | { |
| 297 | // allow pass |
| 298 | |
| 299 | } |
| 300 | |
| 301 | #ifdef FEATURE_ZTE_WEB_TCARD |
| 302 | //added by guo shoupeng 10124224 for http share 20111001 start |
| 303 | zte_httpShare_urlHandler(wp,wp->url); |
| 304 | //added by guo shoupeng 10124224 for http share 20111001 start |
| 305 | #endif //def FEATURE_ZTE_WEB_TCARD |
| 306 | |
| 307 | /* |
| 308 | * Eliminate security hole |
| 309 | */ |
| 310 | websCondenseMultipleChars(wp->path, '/'); |
| 311 | websCondenseMultipleChars(wp->url, '/'); |
| 312 | |
| 313 | first = 1; |
| 314 | for (i = 0; i < websUrlHandlerMax; i++) |
| 315 | { |
| 316 | sp = &websUrlHandler[i]; |
| 317 | if (sp->handler && gstrncmp(sp->urlPrefix, wp->path, sp->len) == 0) |
| 318 | { |
| 319 | if (first) |
| 320 | { |
| 321 | websSetEnv(wp); |
| 322 | first = 0; |
| 323 | } |
| 324 | |
| 325 | if(websValid(wp)== 0){ |
| 326 | softap_assert("websUrlHandlerRequest 2"); |
| 327 | } |
| 328 | |
| 329 | if ((*sp->handler)(wp, sp->urlPrefix, sp->webDir, sp->arg, |
| 330 | wp->url, wp->path, wp->query)) |
| 331 | { |
| 332 | return 1; |
| 333 | } |
| 334 | if (!websValid(wp)) |
| 335 | { |
| 336 | trace(0, T("webs: handler %s called websDone, but didn't return 1\n"), sp->urlPrefix); |
| 337 | return 1; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (i >= websUrlHandlerMax) |
| 343 | { |
| 344 | websError(wp, 200, T("No handler for this URL")); |
| 345 | } |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static char_t *websCondenseMultipleChars(char_t *strToCondense, char_t cCondense) |
| 350 | { |
| 351 | if (strToCondense != NULL) { |
| 352 | char_t *pStr, *pScan; |
| 353 | |
| 354 | pStr = pScan = strToCondense; |
| 355 | |
| 356 | while (*pScan && *pStr) { |
| 357 | |
| 358 | while ((*pScan == cCondense) && (*(pScan + 1) == cCondense)) { |
| 359 | pScan++; |
| 360 | } |
| 361 | |
| 362 | if (pStr != pScan) { |
| 363 | *pStr = *pScan; |
| 364 | } |
| 365 | |
| 366 | pScan++; |
| 367 | pStr++; |
| 368 | } |
| 369 | |
| 370 | if (pStr != pScan) { |
| 371 | *pStr = 0; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return strToCondense; |
| 376 | } |
| 377 | |