Subversion Repositories Kolibri OS

Rev

Rev 6058 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. enum {
  3.         STATE_NOT_STARTED,
  4.         STATE_IN_PROGRESS,
  5.         STATE_COMPLETED
  6. };
  7.  
  8. struct DOWNLOADER {
  9.         unsigned data_downloaded_size, data_full_size;
  10.         dword bufpointer, bufsize, url;
  11.         byte state;
  12.         dword http_transfer;
  13.         int http_status_code;
  14.         dword Start();
  15.         void Stop();
  16.         void Completed();
  17.         int MonitorProgress();
  18. } downloader;
  19.  
  20. dword DOWNLOADER::Start(dword _url)
  21. {
  22.         url = _url;
  23.         state = STATE_IN_PROGRESS;
  24.         http_get stdcall (url, 0, 0, #accept_language);
  25.         http_transfer = EAX;
  26.         return http_transfer;
  27. }
  28.  
  29. void DOWNLOADER::Stop()
  30. {
  31.         state = STATE_NOT_STARTED;
  32.         if (http_transfer!=0)
  33.         {
  34.                 EAX = http_transfer;
  35.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  36.                 $push EAX                                                       // save it on the stack
  37.                 http_free stdcall (http_transfer);      // abort connection
  38.                 $pop  EAX                                                      
  39.                 mem_Free(EAX);                                          // free data
  40.                 http_transfer=0;
  41.                 bufsize = 0;
  42.                 bufpointer = mem_Free(bufpointer);
  43.         }
  44.         data_downloaded_size = data_full_size = 0;
  45. }
  46.  
  47. void DOWNLOADER::Completed()
  48. {
  49.         state = STATE_COMPLETED;
  50.         ESI = http_transfer;
  51.         bufpointer = ESI.http_msg.content_ptr;
  52.         bufsize = ESI.http_msg.content_received;
  53.         http_free stdcall (http_transfer);
  54.         http_transfer=0;
  55. }
  56.  
  57. int DOWNLOADER::MonitorProgress()
  58. {
  59.         dword receive_result;
  60.         char redirect_url[4096*3], finaladress[4096*3];
  61.         if (http_transfer <= 0) return false;
  62.         http_receive stdcall (http_transfer);
  63.         receive_result = EAX;
  64.         EDI = http_transfer;
  65.         http_status_code = EDI.http_msg.status;
  66.         data_full_size = EDI.http_msg.content_length;
  67.         data_downloaded_size = EDI.http_msg.content_received;
  68.         if (!data_full_size) data_full_size = data_downloaded_size * 6 + 1;
  69.  
  70.         if (receive_result == 0) {
  71.                 if (http_status_code >= 300) && (http_status_code < 400)
  72.                 {
  73.                         http_find_header_field stdcall (http_transfer, "location\0");
  74.                         if (EAX!=0) {
  75.                                 ESI = EAX;
  76.                                 EDI = #redirect_url;
  77.                                 do {
  78.                                         $lodsb;
  79.                                         $stosb;
  80.                                 } while (AL != 0) && (AL != 13) && (AL != 10));
  81.                                 DSBYTE[EDI-1]='\0';
  82.                         }
  83.                         get_absolute_url(#finaladress, url, #redirect_url);
  84.                         Stop();
  85.                         Start(#finaladress);
  86.                         url = #finaladress;
  87.                         return false;
  88.                 }
  89.                 Completed();
  90.         }
  91.         return true;
  92. }
  93.  
  94.  
  95. /*=====================================
  96. ==                                   ==
  97. ==         CHECK URL TYPE            ==
  98. ==                                   ==
  99. =====================================*/
  100.  
  101.  
  102. int check_is_the_adress_local(dword _in)
  103. {
  104.         if (ESBYTE[_in]!='/') return false;
  105.         _in++;
  106.         if(!strncmp(_in,"rd/",3)) return true;
  107.         if(!strncmp(_in,"fd/",3)) return true;
  108.         if(!strncmp(_in,"hd",2)) return true;
  109.         if(!strncmp(_in,"bd",2)) return true;
  110.         if(!strncmp(_in,"cd",2)) return true;
  111.         if(!strncmp(_in,"sys/",4)) return true;
  112.         if(!strncmp(_in,"tmp/",4)) return true;
  113.         if(!strncmp(_in,"usbhd",5)) return true;
  114.         if(!strncmp(_in,"kolibrios",10)) return true;
  115.         return false;
  116. }
  117.  
  118. int check_is_the_url_absolute(dword _in)
  119. {
  120.         if(!strncmp(_in,"ftp:",4)) return true;
  121.         if(!strncmp(_in,"http:",5)) return true;
  122.         if(!strncmp(_in,"https:",6)) return true;
  123.         if(!strncmp(_in,"mailto:",7)) return true;
  124.         if(check_is_the_adress_local(_in)) return true;
  125.         return false;
  126. }
  127.  
  128. void get_absolute_url(dword _rez, _base, _new)
  129. {
  130.         int i;
  131.         debug("_base:");debugln(_base);
  132.         debug("_new:");debugln(_new);
  133.         //case: ./valera.html
  134.         if (!strncmp(_new,"./", 2)) _new+=2;
  135.         //case: http://site.name
  136.         if (check_is_the_url_absolute(_new)) {
  137.                 strcpy(_rez, _new);
  138.                 goto _GET_ABSOLUTE_URL_END;
  139.         }
  140.         //case: /valera.html
  141.         if (ESBYTE[_new] == '/') //remove everything after site domain name
  142.         {
  143.                 strcpy(_rez, _base);
  144.                 i = strchr(_rez+8,'/');
  145.                 if (i<1) i=strlen(_rez)+_rez;
  146.                 strcpy(i, _new);
  147.                 goto _GET_ABSOLUTE_URL_END;
  148.         }      
  149.         //case: ../../valera.html
  150.         strcpy(_rez, _base);
  151.         ESBYTE[ strrchr(_rez,'/') + _rez -1 ] = '\0'; //remove name
  152.         while (!strncmp(_new,"../",3))
  153.         {
  154.                 _new += 3;
  155.                 ESBYTE[ strrchr(_rez,'/') + _rez -1 ] = '\0';  
  156.         }
  157.         //case: valera.html
  158.         chrcat(_rez, '/');
  159.         strcat(_rez, _new);
  160.         _GET_ABSOLUTE_URL_END:
  161.         while (i=strstr(_rez, "&amp;")) strcpy(i+1, i+5);      
  162.         debug("_rez:");debugln(_rez);
  163. }
  164.