Subversion Repositories Kolibri OS

Rev

Rev 7863 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7863 Rev 8291
Line 4... Line 4...
4
//                                                   //
4
//                                                   //
5
//===================================================//
5
//===================================================//
Line 6... Line 6...
6
 
6
 
7
struct _http
7
struct _http
8
{
8
{
9
    dword url;
9
	dword cur_url;
10
    dword transfer;
10
    dword transfer;
11
    dword content_length;
11
    dword content_length;
12
    dword content_received;
12
    dword content_received;
13
    dword status_code;
13
    dword status_code;
14
    dword receive_result;
14
    dword receive_result;
15
    dword content_pointer;
15
    dword content_pointer;
16
    char redirect_url[4096*3];
16
    char redirect_url[4096*3];
Line 17... Line 17...
17
    char finaladress[4096*3];
17
    char content_type[64];
18
 
18
 
19
    dword get();
19
    dword get();
20
    void free();
20
    void hfree();
-
 
21
    void receive();
21
    void receive();
22
    bool handle_redirect();
Line 22... Line 23...
22
    bool handle_redirect();
23
    dword check_content_type();
23
};
24
};
24
 
25
 
25
dword _http::get(dword _url)
26
dword _http::get(dword _url)
-
 
27
{
26
{
28
	cur_url = _url;
27
    url = _url;
29
    http_get stdcall (_url, 0, 0, #accept_language);
28
    http_get stdcall (url, 0, 0, #accept_language);
30
    content_type[0] = '\0';
Line 29... Line 31...
29
    transfer = EAX;
31
    transfer = EAX;
30
    return transfer;
32
    return transfer;
31
}
33
}
32
 
34
 
33
void _http::free()
35
void _http::hfree()
Line 51... Line 53...
51
 
53
 
Line 52... Line 54...
52
 }
54
 }
53
 
55
 
54
:bool _http::handle_redirect()
-
 
55
{
56
:bool _http::handle_redirect()
56
	dword redirect;
57
{
57
    http_find_header_field stdcall (transfer, "location\0");
58
    http_find_header_field stdcall (transfer, "location\0");
58
    if (EAX!=0) {
59
    if (EAX!=0) {
59
        ESI = EAX;
60
        ESI = EAX;
60
        EDI = #redirect_url;
61
        EDI = #redirect_url;
61
        do {
62
        do {
62
            $lodsb;
63
            $lodsb;
63
            $stosb;
64
            $stosb;
64
        } while (AL != 0) && (AL != 13) && (AL != 10);
65
        } while (AL != 0) && (AL != 13) && (AL != 10);
65
        DSBYTE[EDI-1]='\0';
66
        DSBYTE[EDI-1]='\0';
66
        get_absolute_url(#finaladress, url, #redirect_url);
67
        get_absolute_url(#redirect_url, cur_url);
67
        strcpy(url, #finaladress);
68
        hfree();
68
        return true;
69
        return #redirect_url;
-
 
70
    }
-
 
71
    return NULL;
-
 
72
}
-
 
73
 
-
 
74
:dword _http::check_content_type()
-
 
75
{
-
 
76
	if (content_type[0]) return NULL;
-
 
77
    http_find_header_field stdcall (transfer, "content-type\0");
-
 
78
    if (EAX!=0) {
-
 
79
        ESI = EAX;
-
 
80
        EDI = #content_type;
-
 
81
        do {
-
 
82
            $lodsb;
-
 
83
            $stosb;
-
 
84
        } while (AL != 0) && (AL != 13) && (AL != 10);
-
 
85
        DSBYTE[EDI-1]='\0';
-
 
86
        debugln(#content_type);
-
 
87
        return #content_type;
69
    }
88
    }
Line 70... Line 89...
70
    return false;
89
    return NULL;
71
}
90
}
72
 
91
 
Line 81... Line 100...
81
	STATE_NOT_STARTED, 
100
	STATE_NOT_STARTED, 
82
	STATE_IN_PROGRESS, 
101
	STATE_IN_PROGRESS, 
83
	STATE_COMPLETED 
102
	STATE_COMPLETED 
84
};
103
};
Line 85... Line 104...
85
 
104
 
86
struct DOWNLOADER {
-
 
87
	_http httpd;
105
struct DOWNLOADER : _http {
88
	dword bufpointer, bufsize, url;
106
	dword bufpointer, bufsize, url;
89
	int state;
107
	int state;
90
	dword Start();
108
	dword Start();
91
	void Stop();
109
	void Stop();
Line 94... Line 112...
94
 
112
 
95
dword DOWNLOADER::Start(dword _url)
113
dword DOWNLOADER::Start(dword _url)
96
{
114
{
97
	url = _url;
115
	url = _url;
98
	state = STATE_IN_PROGRESS;
116
	state = STATE_IN_PROGRESS;
99
	httpd.get(url);
117
	get(_url);
100
	if (!httpd.transfer) Stop();
118
	if (!transfer) Stop();
101
	return httpd.transfer;
119
	return transfer;
Line 102... Line 120...
102
}
120
}
103
 
121
 
104
void DOWNLOADER::Stop()
122
void DOWNLOADER::Stop()
105
{
123
{
106
	state = STATE_NOT_STARTED;
124
	state = STATE_NOT_STARTED;
107
	if (httpd.transfer!=0)
125
	if (transfer!=0)
108
	{
126
	{
109
		EAX = httpd.transfer;
127
		EAX = transfer;
110
		EAX = EAX.http_msg.content_ptr;		// get pointer to data
128
		EAX = EAX.http_msg.content_ptr;		// get pointer to data
111
		$push EAX							// save it on the stack
129
		$push EAX							// save it on the stack
112
		http_free stdcall (httpd.transfer);	// abort connection
130
		http_free stdcall (transfer);	// abort connection
113
		$pop  EAX							
131
		$pop  EAX							
114
		free(EAX);						// free data
132
		free(EAX);						// free data
115
		httpd.transfer=0;
133
		transfer=0;
116
		bufsize = 0;
134
		bufsize = 0;
117
		bufpointer = free(bufpointer);
135
		bufpointer = free(bufpointer);
118
	}
136
	}
Line 119... Line 137...
119
	httpd.content_received = httpd.content_length = 0;
137
	content_received = content_length = 0;
120
}
138
}
121
 
139
 
122
bool DOWNLOADER::MonitorProgress() 
140
bool DOWNLOADER::MonitorProgress() 
123
{
141
{
124
	if (httpd.transfer <= 0) return false;
142
	if (transfer <= 0) return false;
125
	httpd.receive();
143
	receive();
126
	if (!httpd.content_length) httpd.content_length = httpd.content_received * 20;
144
	if (!content_length) content_length = content_received * 20;
127
 
145
 
128
	if (httpd.receive_result == 0) {
146
	if (receive_result == 0) {
129
		if (httpd.status_code >= 300) && (httpd.status_code < 400)
-
 
130
		{
-
 
131
			httpd.handle_redirect();
147
		if (status_code >= 300) && (status_code < 400)
132
			strcpy(url, httpd.url);
148
		{
133
			get_absolute_url(#httpd.finaladress, url, #httpd.redirect_url);
-
 
134
			Stop();
149
			url = handle_redirect();
135
			Start(#httpd.finaladress);
150
			Stop();
136
			url = #httpd.finaladress;
151
			Start(url);
137
			return false;
152
			return false;
138
		}
153
		}
139
		state = STATE_COMPLETED;
154
		state = STATE_COMPLETED;
140
		bufpointer = httpd.content_pointer;
155
		bufpointer = content_pointer;
141
		bufsize = httpd.content_received;
156
		bufsize = content_received;
142
		httpd.free();
157
		hfree();
Line 179... Line 194...
179
	if(!strncmp(_in,"WebView:",8)) return true;
194
	if(!strncmp(_in,"WebView:",8)) return true;
180
	if(check_is_the_adress_local(_in)) return true;
195
	if(check_is_the_adress_local(_in)) return true;
181
	return false;
196
	return false;
182
}
197
}
Line 183... Line -...
183
 
-
 
184
:void get_absolute_url(dword _rez, _base, _new)
-
 
185
{
-
 
186
	int i;
-
 
187
	//case: ./valera.html
-
 
188
	if (!strncmp(_new,"./", 2)) _new+=2;
-
 
189
	//case: http://site.name
-
 
190
	if (check_is_the_url_absolute(_new)) {
-
 
191
		strcpy(_rez, _new);
-
 
192
		goto _GET_ABSOLUTE_URL_END;
-
 
193
	}
-
 
194
	//case: /valera.html
-
 
195
	if (ESBYTE[_new] == '/') //remove everything after site domain name
-
 
196
	{
-
 
197
		strcpy(_rez, _base);
-
 
198
		i = strchr(_rez+8,'/');
-
 
199
		if (i<1) i=strlen(_rez)+_rez;
-
 
200
		strcpy(i, _new);
-
 
201
		goto _GET_ABSOLUTE_URL_END;
-
 
202
	}	
-
 
203
	//case: ../../valera.html
-
 
204
	strcpy(_rez, _base);
-
 
205
	ESBYTE[ strrchr(_rez,'/') + _rez -1 ] = '\0'; //remove name
-
 
206
	while (!strncmp(_new,"../",3))
-
 
207
	{
-
 
208
		_new += 3;
-
 
209
		ESBYTE[ strrchr(_rez,'/') + _rez -1 ] = '\0';	
-
 
210
	}
-
 
211
	//case: valera.html
-
 
212
	chrcat(_rez, '/');
-
 
213
	strcat(_rez, _new); 
-
 
214
	_GET_ABSOLUTE_URL_END:
-
 
215
	while (i=strstr(_rez, "&")) strcpy(i+1, i+5);	
-
 
216
}
-
 
217
 
-
 
218
 
198
 
219
:dword GetAbsoluteURL(dword new_URL, base_URL)
199
:dword get_absolute_url(dword new_URL, base_URL)
220
{
200
{
221
	int i;
201
	int i;
222
	dword orig_URL = new_URL;
202
	dword orig_URL = new_URL;
223
	char newurl[URL_SIZE+1];
203
	char newurl[URL_SIZE+1];