@@ -262,6 +262,11 @@ simple_get_prop()
262262 grep -m 1 -F -e " ${1:? } =" " ${2:? } " | cut -d ' =' -f 2
263263}
264264
265+ get_base_url ()
266+ {
267+ printf ' %s\n' " ${1:? } " | cut -d ' /' -f ' -3' -s
268+ }
269+
265270get_domain_from_url ()
266271{
267272 printf ' %s\n' " ${1:? } " | cut -d ' /' -f ' 3' -s
@@ -272,11 +277,6 @@ get_second_level_domain_from_url()
272277 printf ' %s\n' " ${1:? } " | cut -d ' /' -f ' 3' -s | rev | cut -d ' .' -f ' -2' -s | rev
273278}
274279
275- get_base_url ()
276- {
277- echo " ${1:? } " | cut -d ' /' -f ' 1,2,3' || return " ${?} "
278- }
279-
280280clear_dl_temp_dir ()
281281{
282282 rm -f -r " ${MAIN_DIR:? } /cache/temp"
@@ -291,10 +291,10 @@ _parse_and_store_cookie()
291291{
292292 local IFS _line_no _cookie_file _elem _psc_cookie_name _psc_cookie_val
293293
294- if test ! -e " ${MAIN_DIR:? } /cache/temp/cookies" ; then mkdir -p " ${MAIN_DIR:? } /cache/temp/cookies" || return " ${?} " ; fi
294+ test -d " ${MAIN_DIR:? } /cache/temp/cookies" || mkdir -p " ${MAIN_DIR:? } /cache/temp/cookies" || return " ${?} "
295295
296296 if test " ${DL_DEBUG:? } " = ' true' ; then
297- printf ' %s\n' " Set-Cookie: ${2:? } " >> " ${MAIN_DIR:? } /cache/temp/cookies/${1:? } .dat.debug"
297+ printf ' %s\n' " Set-Cookie: ${2:? } " 1 >> " ${MAIN_DIR:? } /cache/temp/cookies/${1:? } .dat.debug"
298298 fi
299299
300300 _cookie_file=" ${MAIN_DIR:? } /cache/temp/cookies/${1:? } .dat"
@@ -326,27 +326,31 @@ _parse_and_store_all_cookies()
326326 done
327327
328328 if test " ${DL_DEBUG:? } " = ' true' ; then
329- if test -e " ${MAIN_DIR:? } /cache/temp/cookies" ; then printf ' \n' >> " ${MAIN_DIR:? } /cache/temp/cookies/${1:? } .dat.debug" ; fi
329+ if test -d " ${MAIN_DIR:? } /cache/temp/cookies" ; then printf ' \n' 1 >> " ${MAIN_DIR:? } /cache/temp/cookies/${1:? } .dat.debug" ; fi
330330 fi
331331}
332332
333333_load_cookies ()
334334{
335335 local _domain _cookie_file
336336
337- _domain=" $( get_domain_from_url " ${1:? } " ) " || return " ${?} "
337+ _domain=" $( get_domain_from_url " ${1:? } " ) " || return 1
338338 _cookie_file=" ${MAIN_DIR:? } /cache/temp/cookies/${_domain:? } .dat"
339339
340- if test ! -e " ${_cookie_file:? } " ; then
341- _domain=" $( get_second_level_domain_from_url " ${1:? } " ) " || return " ${?} "
340+ if test -f " ${_cookie_file:? } " ; then
341+ : # OK
342+ else
343+ _domain=" $( get_second_level_domain_from_url " ${1:? } " ) " || return 2
342344 _cookie_file=" ${MAIN_DIR:? } /cache/temp/cookies/${_domain:? } .dat"
343- if test ! -e " ${_cookie_file:? } " ; then return 0; fi
345+ test -f " ${_cookie_file:? } " || return 0
344346 fi
345347
346348 while IFS=' =' read -r name val; do
347- if test -z " ${name?} " ; then continue ; fi
349+ test -n " ${name?} " || continue
348350 printf ' %s; ' " ${name:? } =${val?} "
349- done 0< " ${_cookie_file:? } " || return " ${?} "
351+ done 0< " ${_cookie_file:? } " || return 3
352+
353+ return 0
350354}
351355
352356verify_sha1 ()
@@ -380,6 +384,23 @@ _dl_validate_exit_code()
380384 return " ${2:? } "
381385}
382386
387+ _dl_validate_status_code_from_header_file ()
388+ {
389+ local _status_code
390+
391+ _status_code=" $( head -n 1 -- " ${1:? } " | grep -o -e ' HTTP/[0-9].*' | cut -d ' ' -f ' 2' -s) " || _status_code=0
392+ test " ${DL_DEBUG:? } " ! = ' true' || ui_debug " Status code: ${_status_code?} "
393+
394+ case " ${_status_code?} " in
395+ 2* ) return 0 ;; # Usually 200 => OK
396+ 3* ) return 3 ;; # Usually 302 => Redirect
397+ 404) return 4 ;; # 404 => Not Found
398+ * ) ;;
399+ esac
400+
401+ return 1 # Unknown
402+ }
403+
383404_parse_webpage_and_get_url ()
384405{
385406 local _url _referrer _search_pattern
@@ -390,75 +411,54 @@ _parse_webpage_and_get_url()
390411 _url=" ${1:? } "
391412 _referrer=" ${2?} "
392413 _search_pattern=" ${3:? } "
393- PREVIOUS_URL=" ${_url:? } "
394414
395- _domain=" $( get_domain_from_url " ${_url:? } " ) " || return " ${?} "
396- if _cookies=" $( _load_cookies " ${_url:? } " ) " ; then _cookies=" ${_cookies% ; } " ; else return " ${?} " ; fi
415+ _domain=" $( get_domain_from_url " ${_url:? } " ) " || return 9
397416 _parsed_code=' '
398417 _parsed_url=' '
399418 _status=0
400419
401- set -- -U " ${DL_UA:? } " --header " ${DL_ACCEPT_HEADER:? } " --header " ${DL_ACCEPT_LANG_HEADER:? } " || return " ${?} "
402- if test -n " ${_referrer?} " ; then
403- set -- " ${@ } " --header " Referer: ${_referrer:? } " || return " ${?} "
404- fi
405- if test -n " ${_cookies?} " ; then
406- set -- " ${@ } " --header " Cookie: ${_cookies:? } " || return " ${?} "
407- fi
420+ if _cookies=" $( _load_cookies " ${_url:? } " ) " ; then _cookies=" ${_cookies% " ; " } " ; else return " ${?} " ; fi
421+
422+ set -- -U " ${DL_UA:? } " --header " ${DL_ACCEPT_HEADER:? } " --header " ${DL_ACCEPT_LANG_HEADER:? } " || return 10
423+ test -z " ${_referrer?} " || set -- " ${@ } " --header " Referer: ${_referrer:? } " || return 11
424+ test -z " ${_cookies?} " || set -- " ${@ } " --header " Cookie: ${_cookies:? } " || return 12
425+ set -- -S " ${@ } " || return 13
426+
427+ _headers_file=" ${MAIN_DIR:? } /cache/temp/headers/${_domain:? } .dat"
428+ test -d " ${MAIN_DIR:? } /cache/temp/headers" || mkdir -p " ${MAIN_DIR:? } /cache/temp/headers" || return 14
408429
409430 if test " ${DL_DEBUG:? } " = ' true' ; then
410- ui_debug ' '
411- ui_debug " URL: ${_url?} "
412- ui_debug " User-Agent: ${DL_UA?} "
413- ui_debug " ${DL_ACCEPT_HEADER?} "
414- ui_debug " ${DL_ACCEPT_LANG_HEADER?} "
415- ui_debug " Referer: ${_referrer?} "
416- if test -n " ${_cookies?} " ; then ui_debug " Cookie: ${_cookies?} " ; fi
417- ui_debug ' '
431+ dl_debug " ${_url:? } " " GET" " ${@ } "
418432 fi
419433
420- _headers_file=" ${MAIN_DIR:? } /cache/temp/headers/${_domain:? } .dat"
421- test -d " ${MAIN_DIR:? } /cache/temp/headers" || mkdir -p " ${MAIN_DIR:? } /cache/temp/headers" || return 10
434+ _parsed_code=" $( " ${WGET_CMD:? } " -q -O ' -' " ${@ } " -- " ${_url:? } " 2> " ${_headers_file:? } " ) " || _status=" ${?} "
435+ test " ${DL_DEBUG:? } " ! = ' true' || cat 1>&2 " ${_headers_file:? } "
436+ _dl_validate_status_code_from_header_file " ${_headers_file:? } " || return 15
437+ _dl_validate_exit_code ' wget' " ${_status:? } " || return 16
438+ test -n " ${_parsed_code?} " || return 17
422439
440+ # shellcheck disable=SC3040 # Ignore: In POSIX sh, set option pipefail is undefined
423441 {
424- _parsed_code=" $( " ${WGET_CMD:? } " -q -S -O ' -' " ${@ } " -- " ${_url:? } " 2> " ${_headers_file:? } " ) " || _status=" ${?} "
425- _status_code=" $( head -n 1 -- " ${_headers_file:? } " | grep -o -e ' HTTP/[0-9].*' | cut -d ' ' -f ' 2' -s) " || _status_code=0
426- if test " ${DL_DEBUG:? } " = ' true' ; then ui_debug " Status code: ${_status_code?} " ; fi
427- _dl_validate_exit_code ' Wget' " ${_status:? } " || return 11
428- test -s " ${_headers_file:? } " || return 12
429- test -n " ${_parsed_code?} " || return 13
430-
431- # shellcheck disable=SC3040 # Ignore: In POSIX sh, set option pipefail is undefined
432- {
433- # IMPORTANT: We have to avoid "printf: write error: Broken pipe" when a string is piped to "grep -q" or "grep -m 1"
434- test " ${USING_PIPEFAIL:- false} " = ' false' || set +o pipefail
435- _parsed_code=" $( printf 2> /dev/null ' %s\n' " ${_parsed_code?} " | grep -o -m 1 -e " ${_search_pattern:? } " ) " || _status=" ${?} "
436- test " ${USING_PIPEFAIL:- false} " = ' false' || set -o pipefail
437- }
438- _dl_validate_exit_code ' Grep' " ${_status:? } " || return 14
439- test -n " ${_parsed_code?} " || return 15
440-
441- _parsed_url=" $( printf ' %s\n' " ${_parsed_code:? } " | grep -o -e ' href=".*' | cut -d ' "' -f ' 2' -s | sed -e ' s|&|\&|g' ) " || _status=" ${?} "
442- if test " ${DL_DEBUG:? } " = ' true' ; then
443- ui_debug " Parsed url: ${_parsed_url?} "
444- ui_debug " Status: ${_status?} "
445- fi
446-
447- if test " ${_status:? } " -ne 0 || test -z " ${_parsed_url?} " ; then
448- if test " ${DL_DEBUG:? } " = ' true' ; then
449- ui_error_msg " Webpage parsing failed, error code => ${_status?} "
450- fi
451- return 16
452- fi
442+ # IMPORTANT: We have to avoid "printf: write error: Broken pipe" when a string is piped to "grep -q" or "grep -m 1"
443+ test " ${USING_PIPEFAIL:- false} " = ' false' || set +o pipefail
444+ _parsed_code=" $( printf 2> /dev/null ' %s\n' " ${_parsed_code:? } " | grep -o -m 1 -e " ${_search_pattern:? } " ) " || _status=" ${?} "
445+ test " ${USING_PIPEFAIL:- false} " = ' false' || set -o pipefail
453446 }
447+ _dl_validate_exit_code ' grep' " ${_status:? } " || return 18
448+ test -n " ${_parsed_code?} " || return 19
449+
450+ _parsed_url=" $( printf ' %s\n' " ${_parsed_code:? } " | grep -o -e ' href=".*' | cut -d ' "' -f ' 2' -s | sed -e ' s|&|\&|g' ) " || _status=" ${?} "
451+ _dl_validate_exit_code ' final parsing' " ${_status:? } " || return 20
452+ test " ${DL_DEBUG:? } " = ' false' || ui_debug " Parsed url: ${_parsed_url?} "
453+ test -n " ${_parsed_url?} " || return 21
454454
455455 _parse_and_store_all_cookies " ${_domain:? } " 0< " ${_headers_file:? } " || {
456456 ui_error_msg " Header parsing failed, error code => ${?} "
457- return 17
457+ return 22
458458 }
459- rm -f " ${_headers_file:? } " || return 18
459+ rm -f " ${_headers_file:? } " || return 23
460460
461- printf ' %s\n' " ${_parsed_url?} "
461+ printf ' %s\n' " ${_parsed_url: ? } "
462462}
463463
464464dl_debug ()
@@ -624,7 +624,7 @@ send_web_request_and_output_headers()
624624
625625parse_headers_and_get_status_code ()
626626{
627- printf ' %s\n' " ${1?} " | head -n 1 | grep -o -e ' HTTP/.*' | cut -d ' ' -f ' 2' -s
627+ printf ' %s\n' " ${1?} " | head -n 1 | grep -o -e ' HTTP/[0-9] .*' | cut -d ' ' -f ' 2' -s
628628}
629629
630630parse_headers_and_get_location_url ()
@@ -682,7 +682,6 @@ _direct_download()
682682 _authorization=" ${6-} " # Optional
683683 _accept=" ${7-} " # Optional
684684 if test -n " ${_origin?} " ; then _is_ajax=' true' ; fi
685- PREVIOUS_URL=" ${_url:? } "
686685
687686 if test " ${_is_ajax:? } " = ' true' || test " ${_accept?} " = ' all' ; then
688687 set -- -U " ${DL_UA:? } " --header " ${DL_ACCEPT_ALL_HEADER:? } " --header " ${DL_ACCEPT_LANG_HEADER:? } " || return " ${?} "
@@ -717,58 +716,68 @@ report_failure_one()
717716 readonly DL_TYPE_1_FAILED=' true'
718717
719718 # printf '%s - ' "Failed at '${2}' with ret. code ${1:?}"
720- if test -n " ${3: - } " ; then printf ' %s\n ' " ${3:? } " ; fi
719+ test -z " ${3-} " || ui_debug " ${3:? } "
721720
722721 return " ${1:? } "
723722}
724723
725- dl_type_zero ()
724+ _init_dl ()
726725{
727- local _url _output
726+ _CURRENT_URL=' '
727+ _PREVIOUS_URL=' '
728+ clear_dl_temp_dir
729+ }
728730
729- clear_previous_url
731+ _set_url ()
732+ {
733+ _PREVIOUS_URL=" ${_CURRENT_URL?} "
734+ _CURRENT_URL=" ${1:? } "
735+ }
730736
731- _url=" ${1:? } "
732- _output=" ${2:? } "
737+ _deinit_dl ()
738+ {
739+ unset _CURRENT_URL
740+ unset _PREVIOUS_URL
741+ clear_dl_temp_dir
742+ }
743+
744+ dl_type_zero ()
745+ {
746+ _init_dl
733747
734- _direct_download " ${_url:? } " " ${_output:? } " ' GET' ||
748+ _set_url " ${1:? } "
749+ _direct_download " ${_CURRENT_URL:? } " " ${2:? } " ' GET' " ${_PREVIOUS_URL?} " ||
735750 report_failure 0 " ${?} " ' dl' || return " ${?} "
751+
752+ _deinit_dl
736753}
737754
738755dl_type_one ()
739756{
740757 if test " ${DL_TYPE_1_FAILED:- false} " ! = ' false' ; then return 128; fi
741- local _url _base_url _referrer _result
742-
743- clear_previous_url
758+ local _base_url _result
744759
745- _base_url=" $( get_base_url " ${2:? } " ) " || report_failure_one " ${?} " || return " ${?} "
760+ _init_dl
761+ _base_url=" $( get_base_url " ${1:? } " ) " || report_failure_one " ${?} " || return " ${?} "
746762
747- {
748- _referrer=" ${2:? } "
749- _url=" ${1:? } "
750- }
751- _result=" $( _parse_webpage_and_get_url " ${_url:? } " " ${_referrer:? } " ' downloadButton[^"]*"\s*href="[^"]*"' ) " || {
752- report_failure_one " ${?} " ' get link 1' " ${_result:- } " || return " ${?} "
763+ _set_url " ${1:? } "
764+ _result=" $( _parse_webpage_and_get_url " ${_CURRENT_URL:? } " " ${_PREVIOUS_URL?} " ' downloadButton[^"]*"\s*href="[^"]*"' ) " || {
765+ report_failure_one " ${?} " ' get link 1' " ${_result?} " || return " ${?} "
753766 }
754767
755768 sleep 0.2
756- {
757- _referrer=" ${_url:? } "
758- _url=" ${_base_url:? }${_result:? } "
759- }
760- _result=" $( _parse_webpage_and_get_url " ${_url:? } " " ${_referrer:? } " ' Your\sdownload\swill\sstart.*href="[^"]*"' ) " || {
761- report_failure_one " ${?} " ' get link 2' " ${_result:- } " || return " ${?} "
769+ _set_url " ${_base_url:? }${_result:? } "
770+ _result=" $( _parse_webpage_and_get_url " ${_CURRENT_URL:? } " " ${_PREVIOUS_URL?} " ' Your\sdownload\swill\sstart.*href="[^"]*"' ) " || {
771+ report_failure_one " ${?} " ' get link 2' " ${_result?} " || return " ${?} "
762772 }
763773
764774 sleep 0.3
765- {
766- _referrer=" ${_url:? } "
767- _url=" ${_base_url:? }${_result:? } "
768- }
769- _direct_download " ${_url:? } " " ${3:? } " ' GET' " ${_referrer:? } " || {
775+ _set_url " ${_base_url:? }${_result:? } "
776+ _direct_download " ${_CURRENT_URL:? } " " ${2:? } " ' GET' " ${_PREVIOUS_URL?} " || {
770777 report_failure_one " ${?} " ' dl' || return " ${?} "
771778 }
779+
780+ _deinit_dl
772781}
773782
774783dl_type_two ()
@@ -904,7 +913,7 @@ dl_file()
904913 ;;
905914 * \. ' apk' ' mirror' ' .com' )
906915 printf ' \n %s: ' ' DL type 1'
907- dl_type_one " ${_url:? } " " ${DL_PROT :? }${_domain :? } / " " ${ BUILD_CACHE_DIR:? } /${1:? } /${2:? } " || _status=" ${?} "
916+ dl_type_one " ${_url:? } " " ${BUILD_CACHE_DIR:? } /${1:? } /${2:? } " || _status=" ${?} "
908917 ;;
909918 ????* )
910919 printf ' \n %s: ' ' DL type 0'
0 commit comments