Subversion Repositories Kolibri OS

Rev

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

  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__ = 2
  18. __DEBUG__       = 1
  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, FLAG_KEEPALIVE or FLAG_BLOCK, 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.         invoke HTTP_receive, [httpstruct]
  67.  
  68.         cmp eax, 0
  69.         je .http_transfer_done
  70.  
  71.  
  72.         mov ebp, [httpstruct]
  73.         DEBUGF 1, "---- http flags = 0x%x.\n",     [ebp + http_msg.flags]
  74.         test [ebp + http_msg.flags], 0xffff0000
  75.         jnz .http_error
  76.  
  77.         mov ebp, [ebp + http_msg.content_received]
  78.         cmp ebp, [write_to_file.current_offset]
  79.         jle .http_receive_loop
  80.         ;; Only proceed if we have more data in HTTP buffer than we have written to file.
  81.  
  82.         ;; Process data we got (write it to the file)
  83.         mov ebp, [httpstruct]
  84.         mov ecx, [ebp + http_msg.content_length]
  85.         mov edx, [ebp + http_msg.content_received]
  86.  
  87.         DEBUGF 1, "---- Current file write offset : %u (http got : %u / %u)\n", [write_to_file.current_offset], edx, ecx
  88.         sub edx, [write_to_file.current_offset]
  89.         mov [write_to_file.bufsize], edx
  90.  
  91.         mov ecx, [ebp + http_msg.content_ptr]
  92.         add ecx, [write_to_file.current_offset]
  93.         mov [write_to_file.bufptr], ecx
  94.  
  95.         DEBUGF 1, "---- ecx + offset = 0x%x\n", ecx
  96.         DEBUGF 1, "---- Writing to file %u bytes at 0x%x to %s\n", [write_to_file.bufsize], [write_to_file.bufptr], current_filename
  97.         mcall 70, write_to_file
  98.         cmp eax, 0
  99.         jne .file_error
  100.  
  101.         DEBUGF 1, "---- Wrote to file %u bytes.\n", ebx
  102.         add [write_to_file.current_offset], ebx
  103.         DEBUGF 1, "---- File offset updated to : %u\n", [write_to_file.current_offset]
  104.  
  105.         jmp .http_receive_loop
  106.  
  107.     .file_error:
  108.     DEBUGF 1, "file_erroR with eax = %u!", eax
  109.         mcall -1
  110.  
  111.     .http_error:
  112.     DEBUGF 1, "http_erroR!"
  113.         mcall -1
  114.  
  115.     .http_transfer_done:
  116.         ;; Write any remaining bytes from the http buffer into the file
  117.          DEBUGF 1, "---- http flags = 0x%x.\n",     [httpstruct + http_msg.flags]
  118.         DEBUGF 1, "Got %u bytes in total\n", [httpstruct + http_msg.content_length]
  119.  
  120.         mov ebp, [httpstruct]
  121.         mov edx, [ebp + http_msg.content_length]
  122.  
  123.         sub edx, [write_to_file.current_offset]
  124.         mov [write_to_file.bufsize], edx
  125.  
  126.         mov ecx, [ebp + http_msg.content_ptr]
  127.         add ecx, [write_to_file.current_offset]
  128.         mov [write_to_file.bufptr], ecx
  129.  
  130.         DEBUGF 1, "---- Final ecx + offset = 0x%x\n", ecx
  131.         DEBUGF 1, "-- Writing to file %u bytes at 0x%x to %s\n", [write_to_file.bufsize], [write_to_file.bufptr], current_filename
  132.  
  133.         mcall 70, write_to_file
  134.         cmp eax, 0
  135.         jne .file_error
  136.  
  137.         DEBUGF 1, "-- Wrote to file %u bytes.\n", ebx
  138.         add [write_to_file.current_offset], ebx
  139.         DEBUGF 1, "-- File offset updated to : %u\n", [write_to_file.current_offset]
  140.         mov ebp, [httpstruct]
  141.         mov edx, [ebp + http_msg.content_length]
  142.         cmp [write_to_file.current_offset], edx
  143.         jne .http_transfer_done
  144.  
  145.     invoke HTTP_free, [httpstruct]
  146.  
  147.     popa
  148.     ret
  149. endp
  150.  
  151. proc make_new_folder newfolder
  152.     pusha
  153.  
  154.     mov eax, [newfolder]
  155.     mov [create_new_folder.foldername], eax
  156.     mcall 70, create_new_folder
  157.     test eax, eax
  158.     jz .success
  159.  
  160.     DEBUGF 1, "Failed to create folder: %s\n", [newfolder]
  161.     mcall -1
  162.  
  163. .success:
  164.     popa
  165.     ret
  166. endp
  167.  
  168. START:
  169.     mcall   68, 11                  ; init heap
  170.  
  171. ; load libraries
  172.     stdcall dll.Load, @IMPORT
  173.     test    eax, eax
  174.     jnz     .all_files_done_error
  175.  
  176.     DEBUGF 2, "-------------------------\n"
  177.     DEBUGF 2, "NETSURF INSTALLER.\n"
  178.  
  179.     stdcall make_new_folder, dirname_res
  180.     stdcall make_new_folder, dirname_res_pointers
  181.     stdcall make_new_folder, dirname_res_throbber
  182.     stdcall make_new_folder, dirname_res_icons
  183.  
  184.  
  185. .get_next_file:
  186.     mov        edi, current_url
  187.     mov        esi, url
  188.  
  189.     @@:
  190.         movsb
  191.         cmp byte[esi], 0
  192.         jne @b
  193.     ;;  Loaded the base URL into current URL
  194.  
  195.     ;; Move onto the subsequent file.
  196.     mov esi, [filelistoffset]
  197.     cmp byte[esi], 0
  198.     je  .all_files_done
  199.  
  200.     @@:
  201.         movsb
  202.         cmp byte[esi], 0
  203.         jne @b
  204.     movsb
  205.  
  206.     ;; DEBUGF 1, "-- Current URL with filename is : %s\n", current_url
  207.  
  208. ; Create name of file we will download to
  209.     mov esi, download_file_path
  210.     mov edi, current_filename
  211.  
  212.     @@:
  213.         movsb
  214.         cmp byte[esi], 0
  215.         jne @b
  216.  
  217.     mov esi, [filelistoffset]
  218.     @@:
  219.         movsb
  220.         cmp byte[esi], 0
  221.         jne @b
  222.     movsb
  223.     mov [filelistoffset], esi
  224.  
  225.     ;; current_filename is now set to the name of the file
  226.     ;; current_url is now set to the name of the file we will get after download
  227.     DEBUGF 2, "Fetching : %s", current_filename
  228.     stdcall get_file_over_http, current_url, current_filename
  229.     DEBUGF 2, "...DONE!\n"
  230.     jmp .get_next_file
  231.  
  232. .all_files_done:
  233.     DEBUGF 2, "-------------------------\n"
  234.     DEBUGF 2, "NETSURF INSTALLED. Enjoy!\n"
  235.     DEBUGF 2, "-------------------------\n"
  236.     mcall -1
  237.     ;; Inform user that all files are done
  238.  
  239. .all_files_done_error:
  240.     DEBUGF 1, "FATAL ERROR: FAILED.\n", eax
  241.     mcall -1
  242.  
  243. ;---------------------------------------------------------------------
  244. ; Data area
  245. ;-----------------------------------------------------------------------------
  246. align   4
  247. @IMPORT:
  248.  
  249. library lib_http,       'http.obj'
  250. import  lib_http, \
  251.         HTTP_get,       'get', \
  252.         HTTP_receive,   'receive', \
  253.         HTTP_free,      'free'
  254.  
  255. include_debug_strings
  256.  
  257. download_file_path db '/tmp0/1/', 0
  258. dirname_res db '/tmp0/1/res', 0
  259. dirname_res_pointers db '/tmp0/1/res/pointers', 0
  260. dirname_res_throbber db '/tmp0/1/res/throbber', 0
  261. dirname_res_icons    db '/tmp0/1/res/icons', 0
  262.  
  263. url              db 'www.ashmew2.me/',0
  264.  
  265. filelist db 'netsurf-kolibrios', 0
  266.          db 'netsurf-kolibrios.map', 0
  267.          db 'res/adblock.css', 0
  268.          db 'res/quirks.css', 0
  269.          db 'res/Messages', 0
  270.          db 'res/licence.html', 0
  271.          db 'res/default.css', 0
  272.          db 'res/netsurf.png', 0
  273.          db 'res/sans.ttf', 0
  274.          db 'res/welcome.html', 0
  275.          db 'res/internal.css', 0
  276.          db 'res/maps.html', 0
  277.          db 'res/favicon.png', 0
  278.          db 'res/credits.html', 0
  279.          db 'res/throbber/throbber8.png', 0
  280.          db 'res/throbber/throbber3.png', 0
  281.          db 'res/throbber/throbber4.png', 0
  282.          db 'res/throbber/throbber0.png', 0
  283.          db 'res/throbber/throbber6.png', 0
  284.          db 'res/throbber/throbber2.png', 0
  285.          db 'res/throbber/throbber1.png', 0
  286.          db 'res/throbber/throbber7.png', 0
  287.          db 'res/throbber/throbber5.png', 0
  288.          db 'res/pointers/point.png', 0
  289.          db 'res/pointers/no_drop.png', 0
  290.          db 'res/pointers/wait.png', 0
  291.          db 'res/pointers/up-down.png', 0
  292.          db 'res/pointers/help.png', 0
  293.          db 'res/pointers/ru-ld.png', 0
  294.          db 'res/pointers/menu.png', 0
  295.          db 'res/pointers/not_allowed.png', 0
  296.          db 'res/pointers/cross.png', 0
  297.          db 'res/pointers/default.png', 0
  298.          db 'res/pointers/caret.png', 0
  299.          db 'res/pointers/left-right.png', 0
  300.          db 'res/pointers/lu-rd.png', 0
  301.          db 'res/pointers/progress.png', 0
  302.          db 'res/pointers/move.png', 0
  303.          db 'res/icons/back.png', 0
  304.          db 'res/icons/back_g.png', 0
  305.          db 'res/icons/scrollr.png', 0
  306.          db 'res/icons/osk.png', 0
  307.          db 'res/icons/forward_g.png', 0
  308.          db 'res/icons/scrolll.png', 0
  309.          db 'res/icons/history.png', 0
  310.          db 'res/icons/forward.png', 0
  311.          db 'res/icons/home_g.png', 0
  312.          db 'res/icons/history_g.png', 0
  313.          db 'res/icons/reload_g.png', 0
  314.          db 'res/icons/scrollu.png', 0
  315.          db 'res/icons/stop.png', 0
  316.          db 'res/icons/scrolld.png', 0
  317.          db 'res/icons/stop_g.png', 0
  318.          db 'res/icons/home.png', 0
  319.          db 'res/icons/reload.png', 0
  320.          db 0
  321.  
  322. filelistoffset   dd filelist
  323. httpstruct       dd 0
  324.  
  325. create_new_file    dd 2, 0, 0, 0, 0
  326.     db 0
  327.     dd current_filename
  328.  
  329. create_new_folder dd 9, 0, 0, 0, 0
  330.                   db 0
  331.  .foldername dd 0
  332.  
  333. write_to_file    dd 3
  334.  .current_offset dd 0, 0
  335.  .bufsize        dd 0
  336.  .bufptr         dd 0
  337.                  db 0
  338.                  dd current_filename
  339.  
  340. socketdata       rb 4096
  341. current_url      rb URLMAXLEN
  342. current_filename rb FILENAMEMAXLEN
  343. I_END:
  344.