lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include "wsIntrn.h" |
| 2 | |
| 3 | static char_t *websDefaultPage; /* Default page name */ |
| 4 | static char_t *websDefaultDir; /* Default Web page directory */ |
| 5 | |
| 6 | static void websCgiDownLoadWriteEvent(webs_t wp); |
| 7 | static void websDefaultWriteEvent(webs_t wp); |
| 8 | |
| 9 | static int web_check_url(char *buf, char *nv_name, int is_ipv6) |
| 10 | { |
| 11 | if(buf && nv_name) { |
| 12 | char url[40] = {0}; |
| 13 | char full_url[50] = {0}; |
| 14 | sc_cfg_get(nv_name, url, sizeof(url)); |
| 15 | if (websSSLIsOpen()){ |
| 16 | if(is_ipv6) |
| 17 | snprintf(full_url, sizeof(full_url),"https://[%s]",url); |
| 18 | else |
| 19 | snprintf(full_url, sizeof(full_url),"https://%s",url); |
| 20 | } else { |
| 21 | if(is_ipv6) |
| 22 | snprintf(full_url, sizeof(full_url),"http://[%s]",url); |
| 23 | else |
| 24 | snprintf(full_url, sizeof(full_url),"http://%s",url); |
| 25 | } |
| 26 | if(strlen(buf) > strlen(full_url) && strstr(buf,full_url)) { |
| 27 | return 1; |
| 28 | } |
| 29 | } |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | int websDefaultHandler(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg, |
| 34 | char_t *url, char_t *path, char_t *query) |
| 35 | { |
| 36 | websStatType sbuf; |
| 37 | char_t *lpath, *tmp, *date; |
| 38 | int bytes, flags, nchars; |
| 39 | char wifi_png__path[256] = {0}; |
| 40 | char *wifi_ptr = NULL; |
| 41 | char wifi_root_dir[20] = {0}; |
| 42 | a_assert(websValid(wp)); |
| 43 | a_assert(url && *url); |
| 44 | a_assert(path); |
| 45 | a_assert(query); |
| 46 | #ifdef WEBINSPECT_FIX |
| 47 | if (strstr(query,"txtPwd")) { |
| 48 | websDone(wp, 0); |
| 49 | return 1; |
| 50 | } |
| 51 | #ifdef WEBS_SECURITY |
| 52 | if (strstr(query,"_method")) { |
| 53 | printf("websDH: qry=%s\n",query); |
| 54 | websError(wp, 405, T("")); |
| 55 | return 1; |
| 56 | } |
| 57 | #endif |
| 58 | #endif |
| 59 | |
| 60 | flags = websGetRequestFlags(wp); |
| 61 | |
| 62 | if (websValidateUrl(wp, path) < 0) |
| 63 | { |
| 64 | websError(wp, 500, T("Invalid URL")); |
| 65 | return 1; |
| 66 | } |
| 67 | lpath = websGetRequestLpath(wp); |
| 68 | |
| 69 | //qrcode_ssid_wifikey.png qrcode_multi_ssid_wifikey.png |
| 70 | if((strstr(lpath,"pic/qrcode_ssid_wifikey.png")!=NULL)||(strstr(lpath,"pic/qrcode_multi_ssid_wifikey.png")!=NULL)) |
| 71 | { |
| 72 | sc_cfg_get("wifi_root_dir",wifi_root_dir,sizeof(wifi_root_dir)); |
| 73 | wifi_ptr=strstr(lpath,"pic/qrcode_"); |
| 74 | wifi_ptr+=4; |
| 75 | //printf("[wifi_png]wifi_ptr:%s\n",wifi_ptr); |
| 76 | snprintf(wifi_png__path,sizeof(wifi_png__path),"%s/wifi/%s",wifi_root_dir,wifi_ptr); |
| 77 | //printf("[wifi_png]wifi_png_path:%s\n",wifi_png__path); |
| 78 | lpath=wifi_png__path; |
| 79 | } |
| 80 | |
| 81 | if(strstr(lpath,"web/messages")) |
| 82 | { |
| 83 | //lpath="/var/log/webshow_messages"; |
| 84 | snprintf(wifi_png__path,sizeof(wifi_png__path),"%s","/var/log/webshow_messages"); |
| 85 | lpath=wifi_png__path;// kw OVERWRITE_CONST_CHAR |
| 86 | } |
| 87 | if(strstr(lpath,"/webshow_messages")) |
| 88 | { |
| 89 | //lpath="/etc_ro/web/webshow_messages"; |
| 90 | snprintf(wifi_png__path,sizeof(wifi_png__path),"%s","/etc_ro/web/webshow_messages"); |
| 91 | lpath=wifi_png__path;// kw OVERWRITE_CONST_CHAR |
| 92 | } |
| 93 | nchars = gstrlen(lpath) - 1; |
| 94 | if (lpath[nchars] == '/' || lpath[nchars] == '\\') { |
| 95 | lpath[nchars] = '\0'; |
| 96 | } |
| 97 | if(lpath != websGetRequestLpath(wp)) |
| 98 | { |
| 99 | websSetRequestLpath(wp,lpath); |
| 100 | lpath = websGetRequestLpath(wp); |
| 101 | } |
| 102 | |
| 103 | if (websPageIsDirectory(lpath)) { |
| 104 | nchars = gstrlen(path); |
| 105 | if (path[nchars-1] == '/' || path[nchars-1] == '\\') { |
| 106 | path[--nchars] = '\0'; |
| 107 | } |
| 108 | nchars += gstrlen(websDefaultPage) + 2; |
| 109 | fmtAlloc(&tmp, nchars, T("%s/%s"), path, websDefaultPage); |
| 110 | websRedirect(wp, tmp); |
| 111 | bfreeSafe(B_L, tmp); |
| 112 | return 1; |
| 113 | } |
| 114 | #ifdef WEBS_SECURITY |
| 115 | if (strstr(path,websDefaultPage)){ |
| 116 | if (!(wp->flags & WEBS_SECURE) && websSSLIsOpen()){ |
| 117 | printf("[goahead]no https Redirect\n"); |
| 118 | websRedirect(wp, zte_web_get_login_page(wp)); |
| 119 | return 1; |
| 120 | } |
| 121 | } else { |
| 122 | #if 0 |
| 123 | if(wp->cookie == NULL) { |
| 124 | printf("[goahead]no cookie Redirect\n"); |
| 125 | websRedirect(wp, zte_web_get_login_page(wp)); |
| 126 | return 1; |
| 127 | } |
| 128 | #endif |
| 129 | if(wp->referer) |
| 130 | { |
| 131 | if(web_check_url(wp->referer, "LocalDomain", 0) == 0 |
| 132 | && web_check_url(wp->referer, "lan_ipaddr", 0) == 0 |
| 133 | && web_check_url(wp->referer, "lan_ipv6addr", 1) == 0) { |
| 134 | printf("[goahead]referer=%s Redirect\n",wp->referer); |
| 135 | websRedirect(wp, zte_web_get_login_page(wp)); |
| 136 | return 1; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | #endif |
| 141 | /* |
| 142 | * Open the document. Stat for later use. |
| 143 | */ |
| 144 | |
| 145 | if (websPageOpen(wp, lpath, path, SOCKET_RDONLY | SOCKET_BINARY, |
| 146 | 0666) < 0) |
| 147 | { |
| 148 | /* |
| 149 | * <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html> |
| 150 | */ |
| 151 | |
| 152 | websError(wp, 404, T("Cannot open URL")); |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | if (websPageStat(wp, lpath, path, &sbuf) < 0) { |
| 157 | websError(wp, 400, T("Cannot stat page for URL")); |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | websStats.localHits++; |
| 162 | #ifdef WEBS_IF_MODIFIED_SUPPORT |
| 163 | if (flags & WEBS_IF_MODIFIED && !(flags & WEBS_ASP)) { |
| 164 | if (sbuf.mtime <= wp->since) { |
| 165 | websWrite(wp, T("HTTP/1.0 304 Use local copy\r\n")); |
| 166 | |
| 167 | websWrite(wp, T("Server: %s\r\n"), WEBS_NAME); |
| 168 | |
| 169 | if (flags & WEBS_KEEP_ALIVE) { |
| 170 | websWrite(wp, T("Connection: keep-alive\r\n")); |
| 171 | } |
| 172 | websWrite(wp, T("\r\n")); |
| 173 | websSetRequestFlags(wp, flags |= WEBS_HEADER_DONE); |
| 174 | websDone(wp, 304); |
| 175 | return 1; |
| 176 | } |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | if ((date = websGetDateString(NULL)) != NULL) { |
| 181 | websWrite(wp, T("HTTP/1.0 200 OK\r\nDate: %s\r\n"), date); |
| 182 | |
| 183 | websWrite(wp, T("Server: %s\r\n"), WEBS_NAME); |
| 184 | bfree(B_L, date); |
| 185 | } |
| 186 | flags |= WEBS_HEADER_DONE; |
| 187 | |
| 188 | if (flags & WEBS_ASP) { |
| 189 | bytes = 0; |
| 190 | #ifndef WEBINSPECT_FIX |
| 191 | websWrite(wp, T("Pragma: no-cache\r\nCache-Control: no-cache\r\n")); |
| 192 | #endif |
| 193 | } else { |
| 194 | if ((date = websGetDateString(&sbuf)) != NULL) { |
| 195 | websWrite(wp, T("Last-modified: %s\r\n"), date); |
| 196 | bfree(B_L, date); |
| 197 | } |
| 198 | bytes = sbuf.size; |
| 199 | } |
| 200 | #ifdef WEBINSPECT_FIX |
| 201 | websWrite(wp, T("X-Frame-Options: SAMEORIGIN\r\nPragma: no-cache\r\nCache-Control: no-cache\r\n")); |
| 202 | #endif |
| 203 | #ifdef WEBS_SECURITY |
| 204 | websWrite(wp, T("Expires: 0\n")); |
| 205 | if (strstr(path,websDefaultPage)){ |
| 206 | char id[33] = {0}; |
| 207 | int ret = web_make_salt_base64(id, sizeof(id)); |
| 208 | if(ret > 0) |
| 209 | { |
| 210 | if (websSSLIsOpen()) |
| 211 | websWrite(wp, T("Set-Cookie: id=%s; secure; HttpOnly; SameSite=Lax;\r\n"),id); |
| 212 | else |
| 213 | websWrite(wp, T("Set-Cookie: id=%s; HttpOnly; SameSite=Lax;\r\n"),id); |
| 214 | } |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | if (bytes) { |
| 219 | #ifdef WEBINSPECT_FIX |
| 220 | if (strstr(path,"/tmpl/")) |
| 221 | websWrite(wp, T("Content-length: %d\r\n"), bytes+86); |
| 222 | else |
| 223 | #endif |
| 224 | websWrite(wp, T("Content-length: %d\r\n"), bytes); |
| 225 | websSetRequestBytes(wp, bytes); |
| 226 | } |
| 227 | websWrite(wp, T("Content-type: %s\r\n"), websGetRequestType(wp)); |
| 228 | |
| 229 | if ((flags & WEBS_KEEP_ALIVE) && !(flags & WEBS_ASP)) { |
| 230 | websWrite(wp, T("Connection: keep-alive\r\n")); |
| 231 | } |
| 232 | websWrite(wp, T("\r\n")); |
| 233 | |
| 234 | if (flags & WEBS_HEAD_REQUEST) { |
| 235 | websDone(wp, 200); |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | #ifdef WEB_ASP |
| 240 | if (flags & WEBS_ASP) { |
| 241 | if (websAspRequest(wp, lpath) < 0) { |
| 242 | return 1; |
| 243 | } |
| 244 | websDone(wp, 200); |
| 245 | return 1; |
| 246 | } |
| 247 | #endif |
| 248 | #ifdef WEBINSPECT_FIX |
| 249 | if (strstr(path,"/tmpl/") && bytes) { |
| 250 | websWrite(wp, T("<script type=\"text/javascript\">if(self!=top){top.location = self.location;}</script>\r\n")); |
| 251 | } |
| 252 | #endif |
| 253 | #ifdef WEBS_SSL_SUPPORT |
| 254 | if (wp->flags & WEBS_SECURE) { |
| 255 | websDefaultWriteEvent(wp); |
| 256 | } else { |
| 257 | websSetRequestSocketHandler(wp, SOCKET_WRITABLE, websDefaultWriteEvent); |
| 258 | } |
| 259 | #else |
| 260 | |
| 261 | websSetRequestSocketHandler(wp, SOCKET_WRITABLE, websDefaultWriteEvent); |
| 262 | #endif |
| 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | //#ifdef FEATURE_ZTE_WEB_TCARD |
| 267 | //added by guo shoupeng 10124224 for http share 20120110 start |
| 268 | int websCgiDownLoadHandler(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg, |
| 269 | char_t *url, char_t *path, char_t *query) |
| 270 | { |
| 271 | //urlPrefix /mmc2 |
| 272 | //url /mmc2/test.file?......... url = ALL |
| 273 | //path /mmc2/test.file |
| 274 | //query ...... |
| 275 | if (zte_check_downloading_file()) |
| 276 | { |
| 277 | printf("[httpshare]call websCgiDownLoadHandler:system is downloading!.\n"); |
| 278 | websError(wp, 404, T("System is downloading file,please try later!")); |
| 279 | return 1; |
| 280 | } |
| 281 | websStatType sbuf; |
| 282 | char_t *lpath, *tmp, *date; |
| 283 | int bytes, flags, nchars; |
| 284 | |
| 285 | a_assert(websValid(wp)); |
| 286 | a_assert(url && *url); |
| 287 | a_assert(path); |
| 288 | a_assert(query); |
| 289 | char_t mmc2_path[4096+1] = {0}; |
| 290 | |
| 291 | flags = websGetRequestFlags(wp); |
| 292 | |
| 293 | if (websValidateUrl(wp, path) < 0) |
| 294 | { |
| 295 | websError(wp, 500, T("Invalid URL")); |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | lpath = websGetRequestLpath(wp); |
| 300 | |
| 301 | nchars = gstrlen(lpath) - 1; |
| 302 | if (lpath[nchars] == '/' || lpath[nchars] == '\\') { |
| 303 | lpath[nchars] = '\0'; |
| 304 | } |
| 305 | |
| 306 | if (websPageIsDirectory(lpath)) { |
| 307 | nchars = gstrlen(path); |
| 308 | if (path[nchars-1] == '/' || path[nchars-1] == '\\') { |
| 309 | path[--nchars] = '\0'; |
| 310 | } |
| 311 | nchars += gstrlen(websDefaultPage) + 2; |
| 312 | fmtAlloc(&tmp, nchars, T("%s/%s"), path, websDefaultPage); |
| 313 | websRedirect(wp, tmp); |
| 314 | bfreeSafe(B_L, tmp); |
| 315 | return 1; |
| 316 | } |
| 317 | //added for download file start |
| 318 | |
| 319 | if(strstr(lpath,"/mmc2")) |
| 320 | { |
| 321 | snprintf(mmc2_path,4096+1,"/etc_rw/config%s",path); |
| 322 | lpath = mmc2_path; |
| 323 | path = mmc2_path; |
| 324 | |
| 325 | printf("[httpshare]insert download file->%s\n",mmc2_path); |
| 326 | zte_insert_download_file(mmc2_path); |
| 327 | websSetRequestLpath(wp,lpath); |
| 328 | } |
| 329 | |
| 330 | //added for download file end |
| 331 | |
| 332 | if (websPageOpen(wp, lpath, path, SOCKET_RDONLY | SOCKET_BINARY, |
| 333 | 0666) < 0) |
| 334 | { |
| 335 | printf("[httpshare]file is too big , can't open!\n"); |
| 336 | if(strstr(mmc2_path,"/mmc2") != NULL) |
| 337 | { |
| 338 | zte_del_download_file(); |
| 339 | printf("[httpshare]websPageClose:del file->%s form sql download.\n",mmc2_path); |
| 340 | } |
| 341 | websError(wp, 404, T("Cannot open URL,File Error!")); |
| 342 | return 1; |
| 343 | } |
| 344 | |
| 345 | if (websPageStat(wp, lpath, path, &sbuf) < 0) { |
| 346 | |
| 347 | if(strstr(mmc2_path,"/mmc2") != NULL) |
| 348 | { |
| 349 | zte_del_download_file(); |
| 350 | printf("[httpshare]websPageClose:del file->%s form sql download.\n",mmc2_path); |
| 351 | } |
| 352 | websError(wp, 400, T("Cannot stat page for URL")); |
| 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | websStats.localHits++; |
| 357 | #ifdef WEBS_IF_MODIFIED_SUPPORT |
| 358 | if (flags & WEBS_IF_MODIFIED && !(flags & WEBS_ASP)) { |
| 359 | if (sbuf.mtime <= wp->since) { |
| 360 | websWrite(wp, T("HTTP/1.0 304 Use local copy\r\n")); |
| 361 | |
| 362 | websWrite(wp, T("Server: %s\r\n"), WEBS_NAME); |
| 363 | |
| 364 | if (flags & WEBS_KEEP_ALIVE) { |
| 365 | websWrite(wp, T("Connection: keep-alive\r\n")); |
| 366 | } |
| 367 | websWrite(wp, T("\r\n")); |
| 368 | websSetRequestFlags(wp, flags |= WEBS_HEADER_DONE); |
| 369 | websDone(wp, 304); |
| 370 | return 1; |
| 371 | } |
| 372 | } |
| 373 | #endif |
| 374 | |
| 375 | if ((date = websGetDateString(NULL)) != NULL) { |
| 376 | websWrite(wp, T("HTTP/1.0 200 OK\r\nDate: %s\r\n"), date); |
| 377 | |
| 378 | websWrite(wp, T("Server: %s\r\n"), WEBS_NAME); |
| 379 | bfree(B_L, date); |
| 380 | } |
| 381 | flags |= WEBS_HEADER_DONE; |
| 382 | |
| 383 | #if 0 |
| 384 | if (flags & WEBS_ASP) { |
| 385 | bytes = 0; |
| 386 | websWrite(wp, T("Pragma: no-cache\r\nCache-Control: no-cache\r\n")); |
| 387 | |
| 388 | } else { |
| 389 | #endif |
| 390 | if ((date = websGetDateString(&sbuf)) != NULL) { |
| 391 | websWrite(wp, T("Last-modified: %s\r\n"), date); |
| 392 | bfree(B_L, date); |
| 393 | } |
| 394 | bytes = sbuf.size; |
| 395 | #if 0 |
| 396 | } |
| 397 | #endif |
| 398 | #if 0 |
| 399 | if (bytes) { |
| 400 | #endif |
| 401 | websWrite(wp, T("Content-length: %d\r\n"), bytes); |
| 402 | websSetRequestBytes(wp, bytes); |
| 403 | #if 0 |
| 404 | } |
| 405 | #endif |
| 406 | #if 0 |
| 407 | websWrite(wp, T("Content-type: %s\r\n"), websGetRequestType(wp)); |
| 408 | #else |
| 409 | char name[256] = {0}; |
| 410 | int k = 0; |
| 411 | int i = 0; |
| 412 | for(i = 0; i < gstrlen(lpath); i++){ |
| 413 | if((lpath[i] == '/') || (lpath[i] == '\\')){ |
| 414 | memset(name, 0, sizeof(name)); |
| 415 | k = 0; |
| 416 | continue; |
| 417 | }else{ |
| 418 | name[k] = lpath[i]; |
| 419 | k++; |
| 420 | } |
| 421 | } |
| 422 | name[k] = '\0'; |
| 423 | |
| 424 | |
| 425 | websWrite(wp, T("Content-type: application/octet-stream\r\n")); |
| 426 | websWrite(wp, T("Content-Disposition: attachment; filename\r\n")); |
| 427 | #endif |
| 428 | |
| 429 | #if 0 |
| 430 | if ((flags & WEBS_KEEP_ALIVE) |
| 431 | #if 0 |
| 432 | && !(flags & WEBS_ASP) |
| 433 | #endif |
| 434 | ) { |
| 435 | websWrite(wp, T("Connection: keep-alive\r\n")); |
| 436 | } |
| 437 | #endif |
| 438 | websWrite(wp, T("\r\n")); |
| 439 | |
| 440 | /* |
| 441 | * All done if the browser did a HEAD request |
| 442 | */ |
| 443 | /*if (flags & WEBS_HEAD_REQUEST) { |
| 444 | |
| 445 | printf("Head request:websDone\n"); |
| 446 | websDone(wp, 200); |
| 447 | return 1; |
| 448 | }*/ |
| 449 | |
| 450 | /* |
| 451 | * Evaluate ASP requests |
| 452 | */ |
| 453 | #if 0 |
| 454 | if (flags & WEBS_ASP) { |
| 455 | if (websAspRequest(wp, lpath) < 0) { |
| 456 | return 1; |
| 457 | } |
| 458 | websDone(wp, 200); |
| 459 | return 1; |
| 460 | } |
| 461 | #endif |
| 462 | |
| 463 | #ifdef WEBS_SSL_SUPPORT |
| 464 | if (wp->flags & WEBS_SECURE) { |
| 465 | websDefaultWriteEvent(wp); |
| 466 | } else { |
| 467 | websSetRequestSocketHandler(wp, SOCKET_WRITABLE, websCgiDownLoadWriteEvent); |
| 468 | } |
| 469 | #else |
| 470 | /* |
| 471 | * For normal web documents, return the data via background write |
| 472 | */ |
| 473 | websSetRequestSocketHandler(wp, SOCKET_WRITABLE, websCgiDownLoadWriteEvent); |
| 474 | #endif |
| 475 | return 1; |
| 476 | } |
| 477 | //added by guo shoupeng 10124224 for http share 20120110 end |
| 478 | //#endif |
| 479 | |
| 480 | #ifdef WIN32 |
| 481 | |
| 482 | static int badPath(char_t* path, char_t* badPath, int badLen) |
| 483 | { |
| 484 | int retval = 0; |
| 485 | int len = gstrlen(path); |
| 486 | int i = 0; |
| 487 | |
| 488 | if (len <= badLen +1) |
| 489 | { |
| 490 | for (i = 0; i < badLen; ++i) |
| 491 | { |
| 492 | if (badPath[i] != gtolower(path[i])) |
| 493 | { |
| 494 | return 0; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | retval = 1; |
| 499 | if (badLen + 1 == len) |
| 500 | { |
| 501 | if (gisalnum(path[len-1])) |
| 502 | { |
| 503 | retval = 0; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | return retval; |
| 509 | } |
| 510 | |
| 511 | |
| 512 | static int isBadWindowsPath(char_t** parts, int partCount) |
| 513 | { |
| 514 | OSVERSIONINFO version; |
| 515 | int i; |
| 516 | version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
| 517 | if (GetVersionEx(&version)) |
| 518 | { |
| 519 | if (VER_PLATFORM_WIN32_NT != version.dwPlatformId) |
| 520 | { |
| 521 | for (i = 0; i < partCount; ++i) |
| 522 | { |
| 523 | if ( |
| 524 | (badPath(parts[i], T("con"), 3)) || |
| 525 | (badPath(parts[i], T("nul"), 3)) || |
| 526 | (badPath(parts[i], T("aux"), 3)) || |
| 527 | (badPath(parts[i], T("clock$"), 6)) || |
| 528 | (badPath(parts[i], T("config$"), 7)) ) |
| 529 | { |
| 530 | return 1; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | #endif |
| 539 | |
| 540 | int websValidateUrl(webs_t wp, char_t *path) |
| 541 | { |
| 542 | #define kMaxUrlParts 64 |
| 543 | char_t *parts[kMaxUrlParts]; /* Array of ptr's to URL parts */ |
| 544 | char_t *token, *dir, *lpath; |
| 545 | int i, len, npart; |
| 546 | |
| 547 | a_assert(websValid(wp)); |
| 548 | a_assert(path); |
| 549 | |
| 550 | dir = websGetRequestDir(wp); |
| 551 | if (/*dir == NULL ||*/ *dir == '\0') { // kw 3 |
| 552 | return -1; |
| 553 | } |
| 554 | |
| 555 | path = bstrdup(B_L, path); |
| 556 | websDecodeUrl(path, path, gstrlen(path)); |
| 557 | |
| 558 | len = npart = 0; |
| 559 | parts[0] = NULL; |
| 560 | |
| 561 | token = gstrchr(path, '\\'); |
| 562 | while (token != NULL) |
| 563 | { |
| 564 | *token = '/'; |
| 565 | token = gstrchr(token, '\\'); |
| 566 | } |
| 567 | |
| 568 | token = gstrtok(path, T("/")); |
| 569 | |
| 570 | while (token != NULL) |
| 571 | { |
| 572 | if (npart >= kMaxUrlParts) |
| 573 | { |
| 574 | bfree(B_L, path); |
| 575 | return -1; |
| 576 | } |
| 577 | if (gstrcmp(token, T("..")) == 0) |
| 578 | { |
| 579 | if (npart > 0) |
| 580 | { |
| 581 | npart--; |
| 582 | } |
| 583 | |
| 584 | } |
| 585 | else if (gstrcmp(token, T(".")) != 0) |
| 586 | { |
| 587 | parts[npart] = token; |
| 588 | len += gstrlen(token) + 1; |
| 589 | npart++; |
| 590 | } |
| 591 | token = gstrtok(NULL, T("/")); |
| 592 | } |
| 593 | |
| 594 | #ifdef WIN32 |
| 595 | if (isBadWindowsPath(parts, npart)) |
| 596 | { |
| 597 | bfree(B_L, path); |
| 598 | return -1; |
| 599 | } |
| 600 | |
| 601 | #endif |
| 602 | |
| 603 | |
| 604 | if (npart || (gstrcmp(path, T("/")) == 0) || (path[0] == '\0')) |
| 605 | { |
| 606 | lpath = balloc(B_L, (gstrlen(dir) + 1 + len + 1) * sizeof(char_t)); |
| 607 | if(lpath == NULL){ |
| 608 | bfree(B_L, path); |
| 609 | return -1; |
| 610 | } |
| 611 | gstrcpy(lpath, dir); |
| 612 | |
| 613 | for (i = 0; i < npart; i++) |
| 614 | { |
| 615 | gstrcat(lpath, T("/")); |
| 616 | gstrcat(lpath, parts[i]); |
| 617 | } |
| 618 | websSetRequestLpath(wp, lpath); |
| 619 | bfree(B_L, path); |
| 620 | bfree(B_L, lpath); |
| 621 | } |
| 622 | else |
| 623 | { |
| 624 | bfree(B_L, path); |
| 625 | return -1; |
| 626 | } |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | //#ifdef FEATURE_ZTE_WEB_TCARD |
| 631 | //added by guo shoupeng 10124224 for http share 20120110 start |
| 632 | int write_bytes = 0; |
| 633 | static void websCgiDownLoadWriteEvent(webs_t wp) |
| 634 | { |
| 635 | //printf("websCgiDownLoadWriteEvent start\n"); |
| 636 | int len = -1; |
| 637 | int wrote, flags, bytes, written; |
| 638 | char *buf; |
| 639 | |
| 640 | extern int errno; |
| 641 | |
| 642 | a_assert(websValid(wp)); |
| 643 | |
| 644 | flags = websGetRequestFlags(wp); |
| 645 | |
| 646 | websSetTimeMark(wp); |
| 647 | |
| 648 | wrote = bytes = 0; |
| 649 | written = websGetRequestWritten(wp); |
| 650 | static unsigned int timer = 0; |
| 651 | |
| 652 | #if 0 |
| 653 | if ( !(flags & WEBS_ASP)) { |
| 654 | #endif |
| 655 | bytes = websGetRequestBytes(wp); |
| 656 | |
| 657 | if ((buf = balloc(B_L, 16*PAGE_READ_BUFSIZE)) == NULL) { |
| 658 | websError(wp, 200, T("Can't get memory")); |
| 659 | return; //cov |
| 660 | } else { |
| 661 | while ((len = websPageReadData(wp, buf, 16*PAGE_READ_BUFSIZE)) > 0) { |
| 662 | if ((wrote = websWriteDataNonBlock(wp, buf, len)) < 0) { |
| 663 | break; |
| 664 | } |
| 665 | write_bytes += wrote; |
| 666 | written += wrote; |
| 667 | if (wrote != len) { |
| 668 | websPageSeek(wp, - (len - wrote)); |
| 669 | break; |
| 670 | } |
| 671 | if(write_bytes > DOWNLOAD_INTERVAL) |
| 672 | { |
| 673 | write_bytes = 0; |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | if( timer ==0) |
| 678 | { |
| 679 | websSetLoginTimemark(wp); |
| 680 | printf("[httpshare]download reset login state~\n"); |
| 681 | } |
| 682 | |
| 683 | timer++; |
| 684 | timer=timer-(timer>>11<<11); //timer%2^11 |
| 685 | } |
| 686 | |
| 687 | //EOF, done |
| 688 | if (len == 0) { |
| 689 | a_assert(written >= bytes); |
| 690 | written = bytes; |
| 691 | } |
| 692 | memset(buf, 0, 16*PAGE_READ_BUFSIZE);//kw |
| 693 | bfree(B_L, buf); |
| 694 | } |
| 695 | #if 0 |
| 696 | } |
| 697 | #endif |
| 698 | |
| 699 | if(len < 0) |
| 700 | { |
| 701 | printf("[zyl-download-len-error]len->%d, errno->%d\n",len,errno); |
| 702 | } |
| 703 | websSetRequestWritten(wp, written); |
| 704 | if (wrote < 0 || written >= bytes|| len < 0) { |
| 705 | //if (wrote < 0 || written >= bytes) { |
| 706 | websDone(wp, 200); |
| 707 | } |
| 708 | } |
| 709 | //added by guo shoupeng 10124224 for http share 20120110 end |
| 710 | //#endif |
| 711 | |
| 712 | static void websDefaultWriteEvent(webs_t wp) |
| 713 | { |
| 714 | int len, wrote, flags, bytes, written; |
| 715 | char *buf; |
| 716 | |
| 717 | a_assert(websValid(wp)); |
| 718 | |
| 719 | flags = websGetRequestFlags(wp); |
| 720 | |
| 721 | websSetTimeMark(wp); |
| 722 | |
| 723 | wrote = bytes = 0; |
| 724 | written = websGetRequestWritten(wp); |
| 725 | |
| 726 | if ( !(flags & WEBS_ASP)) { |
| 727 | bytes = websGetRequestBytes(wp); |
| 728 | |
| 729 | if ((buf = balloc(B_L, PAGE_READ_BUFSIZE)) == NULL) { |
| 730 | websError(wp, 200, T("Can't get memory")); |
| 731 | return; //cov |
| 732 | } else { |
| 733 | while ((len = websPageReadData(wp, buf, PAGE_READ_BUFSIZE)) > 0) { |
| 734 | if ((wrote = websWriteDataNonBlock(wp, buf, len)) < 0) { |
| 735 | break; |
| 736 | } |
| 737 | #ifdef _USE_WEBUI_ZIP |
| 738 | if (wrote != len) |
| 739 | { |
| 740 | int tmplen; |
| 741 | int leftlen = (len - wrote); |
| 742 | while(leftlen > 0) |
| 743 | { |
| 744 | if((get_sys_uptime() - wp->timestamp) > (WEBS_TIMEOUT/1000)) |
| 745 | break; |
| 746 | tmplen = websWriteDataNonBlock(wp, buf+wrote, leftlen); |
| 747 | if(tmplen >= 0) |
| 748 | { |
| 749 | //printf("%s write=%d left=%d\n",wp->path,tmplen, leftlen); |
| 750 | wrote +=tmplen; |
| 751 | leftlen -=tmplen; |
| 752 | usleep(1000); |
| 753 | } |
| 754 | else |
| 755 | { |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | } |
| 761 | #endif |
| 762 | written += wrote; |
| 763 | if (wrote != len) { |
| 764 | websPageSeek(wp, - (len - wrote)); |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (len == 0) { |
| 770 | a_assert(written >= bytes); |
| 771 | written = bytes; |
| 772 | |
| 773 | } |
| 774 | memset(buf, 0, PAGE_READ_BUFSIZE);//kw |
| 775 | bfree(B_L, buf); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | websSetRequestWritten(wp, written); |
| 780 | if (wrote < 0 || written >= bytes) { |
| 781 | websDone(wp, 200); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | void websDefaultClose() |
| 786 | { |
| 787 | if (websDefaultPage) { |
| 788 | bfree(B_L, websDefaultPage); |
| 789 | websDefaultPage = NULL; |
| 790 | } |
| 791 | if (websDefaultDir) { |
| 792 | bfree(B_L, websDefaultDir); |
| 793 | websDefaultDir = NULL; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | char_t *websGetDefaultPage() |
| 798 | { |
| 799 | return websDefaultPage; |
| 800 | } |
| 801 | |
| 802 | char_t *websGetDefaultDir() |
| 803 | { |
| 804 | return websDefaultDir; |
| 805 | } |
| 806 | |
| 807 | void websSetDefaultPage(char_t *page) |
| 808 | { |
| 809 | a_assert(page && *page); |
| 810 | |
| 811 | if (websDefaultPage) { |
| 812 | bfree(B_L, websDefaultPage); |
| 813 | } |
| 814 | websDefaultPage = bstrdup(B_L, page); |
| 815 | } |
| 816 | |
| 817 | void websSetDefaultDir(char_t *dir) |
| 818 | { |
| 819 | a_assert(dir && *dir); |
| 820 | if (websDefaultDir) { |
| 821 | bfree(B_L, websDefaultDir); |
| 822 | } |
| 823 | websDefaultDir = bstrdup(B_L, dir); |
| 824 | } |
| 825 | |
| 826 | |