Subversion Repositories Kolibri OS

Rev

Rev 5726 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/stat.h>
  4. #include <sys/unistd.h>
  5. #include <fcntl.h>
  6. #include <kos32sys.h>
  7. #include "collection.h"
  8. #include "http.h"
  9.  
  10. #define BUFFSIZE  (64*1024)
  11.  
  12. int http_load_mem(char *buf, const char *url)
  13. {
  14.     http_t *http;
  15.     int offset = 0;
  16.     int count;
  17.  
  18. //    asm volatile("int3");
  19.     http = http_get(url, NULL,FLAG_STREAM|FLAG_REUSE_BUFFER, NULL);
  20.     if(http == NULL)
  21.         goto err_get;
  22.  
  23.     do
  24.     {
  25. //        delay(100);
  26.         if(http_receive_with_retry(http, 500)==0)
  27.         {
  28.             if(http->flags & 0xffff0000)
  29.                 goto err_http;
  30.  
  31.             count = http->content_received - offset;
  32.             if(count == 0)
  33.                 continue;
  34.             memcpy(buf+offset, http->content_ptr, count);
  35.             offset = http->content_received;
  36.         }
  37.         else goto err_http;
  38.  
  39.     }while( (http->flags & FLAG_GOT_ALL_DATA) == 0);
  40.  
  41.     if(http->content_ptr)
  42.         user_free(http->content_ptr);
  43.     http_free(http);
  44.  
  45.     return offset;
  46.  
  47. err_http:
  48.     if(http->content_ptr)
  49.         user_free(http->content_ptr);
  50.     http_free(http);
  51.  
  52.     printf("HTTP receive failed\n");
  53.     return offset;
  54.  
  55. err_get:
  56.     printf("HTTP GET failed\n");
  57.     return offset;
  58.  
  59. }
  60.  
  61. int http_load_file(const char *path, const char *url)
  62. {
  63.     http_t *http;
  64.     int     received = 0;
  65.     int     offset = 0;
  66.     int     tail;
  67.     char   *buf;
  68.     int     fd;
  69.     int     i;
  70.  
  71.     buf = user_alloc(BUFFSIZE);
  72.     for(i = 0; i < 16; i++)
  73.         buf[i*4096] = 0;
  74.  
  75.     fd = open(path, O_CREAT|O_WRONLY);
  76.     if(fd == -1)
  77.     {
  78.         user_free(buf);
  79.         return 0;
  80.     };
  81.  
  82.     http = http_get(url, NULL,FLAG_STREAM|FLAG_REUSE_BUFFER, NULL);
  83.     if(http == NULL)
  84.         goto err_get;
  85.  
  86.     do
  87.     {
  88.         if(http_receive_with_retry(http, 500) == 0)
  89.         {
  90.             int count;
  91.  
  92.             if(http->flags & 0xffff0000)
  93.                 break;
  94.  
  95.             count = http->content_received - received;
  96.             if(count+offset <= BUFFSIZE)
  97.             {
  98.                 memcpy(buf+offset, http->content_ptr, count);
  99.                 offset+= count;
  100.             }    
  101.  
  102.  
  103.             else
  104.             {
  105.                 tail  = count+offset-BUFFSIZE;
  106.                 count = BUFFSIZE - offset;
  107.                 if(count)
  108.                 {
  109.                     memcpy(buf+offset, http->content_ptr, count);
  110.                     offset = 0;
  111.                 };
  112.  
  113.                 write(fd, buf, BUFFSIZE);
  114.  
  115.                 if(tail)
  116.                 {
  117.                     memcpy(buf, http->content_ptr+count, tail);
  118.                     offset = tail;
  119.                 }
  120.             }
  121.             received = http->content_received;
  122.         }
  123.         else break;
  124.  
  125.     }while( (http->flags & FLAG_GOT_ALL_DATA) == 0);
  126.  
  127.     if(offset)
  128.     {
  129.         write(fd, buf, offset);
  130.     }
  131.  
  132. //    ftruncate(fd, received);
  133.     close(fd);
  134.  
  135.     if(http->content_ptr)
  136.         user_free(http->content_ptr);
  137.     http_free(http);
  138.  
  139.     user_free(buf);
  140.  
  141.     return received;
  142.  
  143. err_get:
  144.     printf("HTTP GET failed\n");
  145.     return received;
  146. }
  147.  
  148.  
  149. int main(int argc, char *argv[])
  150. {
  151.     int   count;
  152.  
  153.     if(http_init())
  154.         goto err_init;
  155.  
  156.     count = http_load_file("/tmp0/1/packages.xml", "http://ftp.kolibrios.org/users/Serge/new/OS/packages.xml");
  157.  
  158.     if(count)
  159.     {
  160.         collection_t *collection;
  161.         pkg_group_t *gr;
  162.  
  163.         collection = load_collection_file("/tmp0/1/packages.xml");
  164.  
  165.         list_for_each_entry(gr, &collection->groups, list)
  166.         {
  167.             package_t   *pkg;
  168.  
  169.             list_for_each_entry(pkg, &gr->packages, list)
  170.             {
  171.                 printf("package %s-%s\n", pkg->name, pkg->version);
  172.             }
  173.         };
  174.      }
  175.  
  176.     return 0;
  177.  
  178. err_init:
  179.     printf("HTTP library initialization failed\n");
  180.     return -1;
  181. }
  182.