Subversion Repositories Kolibri OS

Rev

Rev 5726 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5725 serge 1
#include 
2
#include 
3
#include 
4
#include 
5
#include 
6
#include 
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
                tail  = count+offset-BUFFSIZE;
104
                count = BUFFSIZE - offset;
105
                if(count)
106
                {
107
                    memcpy(buf+offset, http->content_ptr, count);
108
                    offset = 0;
109
                };
110
111
                write(fd, buf, BUFFSIZE);
112
 
113
                if(tail)
114
 
115
                    memcpy(buf, http->content_ptr+count, tail);
116
                    offset = tail;
117
                }
118
            }
119
            received = http->content_received;
120
        }
121
        else break;
122
123
    }while( (http->flags & FLAG_GOT_ALL_DATA) == 0);
124
 
125
    if(offset)
126
 
127
        write(fd, buf, offset);
128
    }
129
130
//    ftruncate(fd, received);
131
 
132
133
    if(http->content_ptr)
134
 
135
    http_free(http);
136
137
    user_free(buf);
138
 
139
    return received;
140
 
141
err_get:
142
 
143
    return received;
144
}
145
146
147
 
148
 
149
    int   count;
150
151
    if(http_init())
152
 
153
154
    count = http_load_file("/tmp0/1/packages.xml", "http://ftp.kolibrios.org/users/Serge/new/OS/packages.xml");
155
 
156
    if(count)
157
 
158
        collection_t *collection;
159
        pkg_group_t *gr;
160
161
        collection = load_collection_file("/tmp0/1/packages.xml");
162
 
163
        list_for_each_entry(gr, &collection->groups, list)
164
 
165
            package_t   *pkg;
166
167
            list_for_each_entry(pkg, &gr->packages, list)
168
 
169
                printf("package %s-%s\n", pkg->name, pkg->version);
170
            }
171
        };
172
     }
173
174
    return 0;
175
 
176
err_init:
177
 
178
    return -1;
179
}
180