Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
6903 ashmew2 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2017. All rights reserved.         ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  netsurf-installer - Set up Netsurf Browser on KolibriOS        ;;
7
;;               Author: ashmew2.                                  ;;
8
;;                                                                 ;;
9
;;  Inspired from downloader.asm by hidnplayr@kolibrios.org        ;;
10
;;            GENERAL PUBLIC LICENSE                               ;;
11
;;             Version 2, June 1991                                ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
 
15
URLMAXLEN       = 65535
16
FILENAMEMAXLEN  = 1024
17
__DEBUG_LEVEL__ = 1
18
__DEBUG__       = 0
19
 
20
format binary as ""
21
use32
22
        org     0x0
23
        db      'MENUET01'      ; header
24
        dd      0x01            ; header version
25
        dd      START           ; entry point
26
        dd      I_END          ; image size
27
        dd      I_END+0x1000    ; required memory
28
        dd      I_END+0x1000    ; esp
29
        dd      0x0             ; I_Path
30
        dd      0x0             ; I_Path
31
 
32
include '../../macros.inc'
33
include '../../proc32.inc'
34
include '../../dll.inc'
35
include '../../debug-fdo.inc'
36
include '../../develop/libraries/http/http.inc'
37
 
38
virtual at 0
39
        http_msg http_msg
40
end virtual
41
 
42
;; Parameters
43
;; HTTP URL to download
44
;; Target filename
45
proc get_file_over_http targeturl, targetfilename
46
    pusha
47
	xor eax, eax
48
    mov [write_to_file.current_offset], eax
49
    mov [write_to_file.bufsize], eax
50
    mov [write_to_file.bufptr], eax
51
 
52
    DEBUGF 1, "---- HTTP : Getting %s\n", [targeturl]
53
    invoke  HTTP_get, [targeturl], 0, 0, 0
54
    cmp     eax, 0
55
    je .http_error
56
	mov [httpstruct], eax
57
 
58
    ;; No HTTP errors, create a new file for the download.
59
	DEBUGF 1, "---- Creating new file : %s\n", [targetfilename]
60
	mcall 70, create_new_file
61
    cmp eax, 0
62
    jne .file_error
63
 
64
    .http_receive_loop:
65
		DEBUGF 1, "---- Receiving over http.\n"
66
     	DEBUGF 1, "---- Invoking http receive.\n"
67
        invoke HTTP_receive, [httpstruct]
68
        cmp eax, 0
69
        je .http_transfer_done
70
 
71
	    mov ebp, [httpstruct]
72
		DEBUGF 1, "---- http flags = 0x%x.\n", 	[ebp + http_msg.flags]
73
	    test [ebp + http_msg.flags], 0xffff0000
74
        jnz .http_error
75
 
76
	    mov ebp, [ebp + http_msg.content_received]
77
	    cmp ebp, [write_to_file.current_offset]
78
        jle .http_receive_loop
79
	    ;; Only proceed if we have more data in HTTP buffer than we have written to file.
80
 
81
        ;; Process data we got (write it to the file)
82
	    mov ebp, [httpstruct]
83
	    mov ecx, [ebp + http_msg.content_length]
84
        mov edx, [ebp + http_msg.content_received]
85
 
86
	    DEBUGF 1, "---- Current file write offset : %u (http got : %u / %u)\n", [write_to_file.current_offset], edx, ecx
87
        sub edx, [write_to_file.current_offset]
88
        mov [write_to_file.bufsize], edx
89
 
90
        mov ecx, [ebp + http_msg.content_ptr]
91
        add ecx, [write_to_file.current_offset]
92
        mov [write_to_file.bufptr], ecx
93
 
94
	    DEBUGF 1, "---- ecx + offset = 0x%x\n", ecx
95
	    DEBUGF 1, "---- Writing to file %u bytes at 0x%x to %s\n", [write_to_file.bufsize], [write_to_file.bufptr], current_filename
96
        mcall 70, write_to_file
97
        cmp eax, 0
98
        jne .file_error
99
 
100
	    DEBUGF 1, "---- Wrote to file %u bytes.\n", ebx
101
        add [write_to_file.current_offset], ebx
102
	    DEBUGF 1, "---- File offset updated to : %u\n", [write_to_file.current_offset]
103
 
104
        jmp .http_receive_loop
105
 
106
    .file_error:
107
	DEBUGF 1, "file_erroR with eax = %u!", eax
108
        mcall -1
109
 
110
    .http_error:
111
	DEBUGF 1, "http_erroR!"
112
        mcall -1
113
 
114
    .http_transfer_done:
115
	    ;; Write any remaining bytes from the http buffer into the file
116
     	DEBUGF 1, "---- http flags = 0x%x.\n", 	[httpstruct + http_msg.flags]
117
        DEBUGF 1, "Got %u bytes in total\n", [httpstruct + http_msg.content_length]
118
 
119
	    mov ebp, [httpstruct]
120
        mov edx, [ebp + http_msg.content_length]
121
 
122
        sub edx, [write_to_file.current_offset]
123
        mov [write_to_file.bufsize], edx
124
 
125
        mov ecx, [ebp + http_msg.content_ptr]
126
        add ecx, [write_to_file.current_offset]
127
        mov [write_to_file.bufptr], ecx
128
 
129
	    DEBUGF 1, "---- Final ecx + offset = 0x%x\n", ecx
130
	    DEBUGF 1, "-- Writing to file %u bytes at 0x%x to %s\n", [write_to_file.bufsize], [write_to_file.bufptr], current_filename
131
 
132
        mcall 70, write_to_file
133
        cmp eax, 0
134
        jne .file_error
135
 
136
	    DEBUGF 1, "-- Wrote to file %u bytes.\n", ebx
137
        add [write_to_file.current_offset], ebx
138
	    DEBUGF 1, "-- File offset updated to : %u\n", [write_to_file.current_offset]
139
	    mov ebp, [httpstruct]
140
        mov edx, [ebp + http_msg.content_length]
141
        cmp [write_to_file.current_offset], edx
142
        jne .http_transfer_done
143
 
144
	invoke HTTP_free, [httpstruct]
145
 
146
	popa
147
    ret
148
endp
149
 
150
proc make_new_folder newfolder
151
	pusha
152
 
153
	mov eax, [newfolder]
154
    mov [create_new_folder.foldername], eax
155
	mcall 70, create_new_folder
156
    test eax, eax
157
    jz .success
158
 
159
	DEBUGF 1, "Failed to create folder: %s\n", [newfolder]
160
    mcall -1
161
 
162
.success:
163
    popa
164
    ret
165
endp
166
 
167
START:
168
    mcall   68, 11                  ; init heap so we can allocate memory dynamically
169
 
170
; load libraries
171
    stdcall dll.Load, @IMPORT
172
    test    eax, eax
173
    jnz     .all_files_done_error
174
 
175
	stdcall make_new_folder, dirname_res
176
	stdcall make_new_folder, dirname_res_pointers
177
	stdcall make_new_folder, dirname_res_throbber
178
	stdcall make_new_folder, dirname_res_icons
179
 
180
.get_next_file:
181
    mov        edi, current_url
182
    mov        esi, url
183
 
184
    @@:
185
	    movsb
186
        cmp byte[esi], 0
187
        jne @b
188
	;;  Loaded the base URL into current URL
189
 
190
    ;; Move onto the subsequent file.
191
    mov esi, [filelistoffset]
192
    cmp byte[esi], 0
193
    je  .all_files_done
194
 
195
    @@:
196
	    movsb
197
        cmp byte[esi], 0
198
        jne @b
199
    movsb
200
 
201
	;; DEBUGF 1, "-- Current URL with filename is : %s\n", current_url
202
 
203
; Create name of file we will download to
204
	mov esi, download_file_path
205
    mov edi, current_filename
206
 
207
    @@:
208
        movsb
209
        cmp byte[esi], 0
210
        jne @b
211
 
212
    mov esi, [filelistoffset]
213
    @@:
214
        movsb
215
        cmp byte[esi], 0
216
        jne @b
217
    movsb
218
    mov [filelistoffset], esi
219
 
220
    ;; current_filename is now set to the name of the file
221
    ;; current_url is now set to the name of the file we will get after download
222
	stdcall get_file_over_http, current_url, current_filename
223
	jmp .get_next_file
224
 
225
.all_files_done:
226
	DEBUGF 1, "All FILES DONE!\n"
227
    ;; Inform user that all files are done
228
	;; print 'fuck off'
229
 
230
.all_files_done_error:
231
	DEBUGF 1, "FUCKED BIG TIME with eax = %u!\n", eax
232
    mcall -1
233
 
234
;---------------------------------------------------------------------
235
; Data area
236
;-----------------------------------------------------------------------------
237
align   4
238
@IMPORT:
239
 
240
library lib_http,       'http.obj'
241
import  lib_http, \
242
        HTTP_get,       'get', \
243
        HTTP_receive,   'receive', \
244
        HTTP_free,      'free'
245
 
246
include_debug_strings
247
 
248
download_file_path db '/tmp0/1/', 0
249
dirname_res db '/tmp0/1/res', 0
250
dirname_res_pointers db '/tmp0/1/res/pointers', 0
251
dirname_res_throbber db '/tmp0/1/res/throbber', 0
252
dirname_res_icons    db '/tmp0/1/res/icons', 0
253
 
254
url              db 'www.ashmew2.me/',0
255
filelist db 'res/adblock.css', 0
256
         db 'res/quirks.css', 0
257
         db 'res/Messages', 0
258
         db 'res/licence.html', 0
259
         db 'res/default.css', 0
260
         db 'res/netsurf.png', 0
261
         db 'res/sans.ttf', 0
262
         db 'res/welcome.html', 0
263
         db 'res/internal.css', 0
264
         db 'res/maps.html', 0
265
         db 'res/favicon.png', 0
266
         db 'res/credits.html', 0
267
         db 'res/throbber/throbber8.png', 0
268
         db 'res/throbber/throbber3.png', 0
269
         db 'res/throbber/throbber4.png', 0
270
         db 'res/throbber/throbber0.png', 0
271
         db 'res/throbber/throbber6.png', 0
272
         db 'res/throbber/throbber2.png', 0
273
         db 'res/throbber/throbber1.png', 0
274
         db 'res/throbber/throbber7.png', 0
275
         db 'res/throbber/throbber5.png', 0
276
         db 'res/pointers/point.png', 0
277
         db 'res/pointers/no_drop.png', 0
278
         db 'res/pointers/wait.png', 0
279
         db 'res/pointers/up-down.png', 0
280
         db 'res/pointers/help.png', 0
281
         db 'res/pointers/ru-ld.png', 0
282
         db 'res/pointers/menu.png', 0
283
         db 'res/pointers/not_allowed.png', 0
284
         db 'res/pointers/cross.png', 0
285
         db 'res/pointers/default.png', 0
286
         db 'res/pointers/caret.png', 0
287
         db 'res/pointers/left-right.png', 0
288
         db 'res/pointers/lu-rd.png', 0
289
         db 'res/pointers/progress.png', 0
290
         db 'res/pointers/move.png', 0
291
         db 'res/icons/back.png', 0
292
         db 'res/icons/back_g.png', 0
293
         db 'res/icons/scrollr.png', 0
294
         db 'res/icons/osk.png', 0
295
         db 'res/icons/forward_g.png', 0
296
         db 'res/icons/scrolll.png', 0
297
         db 'res/icons/history.png', 0
298
         db 'res/icons/forward.png', 0
299
         db 'res/icons/home_g.png', 0
300
         db 'res/icons/history_g.png', 0
301
         db 'res/icons/reload_g.png', 0
302
         db 'res/icons/scrollu.png', 0
303
         db 'res/icons/stop.png', 0
304
         db 'res/icons/scrolld.png', 0
305
         db 'res/icons/stop_g.png', 0
306
         db 'res/icons/home.png', 0
307
         db 'res/icons/reload.png', 0
308
         db 'nskolibrios', 0
309
         db 0
310
 
311
current_url      rb URLMAXLEN
312
filelistoffset   dd filelist
313
current_filename rb FILENAMEMAXLEN
314
httpstruct       dd 0
315
 
316
create_new_file	dd 2, 0, 0, 0, 0
317
    db 0
318
    dd current_filename
319
 
320
create_new_folder dd 9, 0, 0, 0, 0
321
                  db 0
322
 .foldername dd 0
323
 
324
write_to_file    dd 3
325
 .current_offset dd 0, 0
326
 .bufsize        dd 0
327
 .bufptr         dd 0
328
                 db 0
329
                 dd current_filename
330
 
331
I_END: