ynh_local_curl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/bash
  2. # local copy of ynh_local_curl() to test some improvement
  3. # https://github.com/YunoHost/yunohost/pull/1857
  4. # https://github.com/YunoHost/issues/issues/2396
  5. # https://codeberg.org/flohmarkt/flohmarkt_ynh/issues/51
  6. ynh_local_curl() {
  7. # Curl abstraction to help with POST requests to local pages (such as installation forms)
  8. #
  9. # usage: ynh_local_curl [--option [-other_option […]]] "page" "key1=value1" "key2=value2" ...
  10. # | arg: -l --line_match: check answer against an extended regex
  11. # | arg: -m --method: request method to use: POST (default), PUT, GET, DELETE
  12. # | arg: -H --header: add a header to the request (can be used multiple times)
  13. # | arg: -d --data: data to be PUT or POSTed. Can be used multiple times.
  14. # | arg: -s --seperator: seperator used to concatenate POST/PUT --date or key=value ('none'=no seperator)
  15. # | arg: (default for POST: '&', default for PUT: ' ')
  16. # | arg: -u --user: login username (requires --password)
  17. # | arg: -p --password: login password
  18. # | arg: -n --no_sleep: don't sleep 2 seconds (background: https://github.com/YunoHost/yunohost/pull/547)
  19. # | arg: page - either the PAGE part in 'https://$domain/$path/PAGE' or an URI
  20. # | arg: key1=value1 - (Optional, POST only) legacy version of '--data' as positional parameter
  21. # | arg: key2=value2 - (Optional, POST only) Another POST key and corresponding value
  22. # | arg: ... - (Optional, POST only) More POST keys and values
  23. #
  24. # example: ynh_local_curl "/install.php?installButton" "foo=$var1" "bar=$var2"
  25. # → will open a POST request to "https://$domain/$path/install.php?installButton" posting "foo=$var1" and "bar=$var2"
  26. # example: ynh_local_curl -m POST --header "Accept: application/json" \
  27. # -H "Content-Type: application/json" \
  28. # --data "{\"members\":{\"names\": [\"${app}\"],\"roles\": [\"editor\"]}}" -l '"ok":true' \
  29. # "http://localhost:5984/"
  30. # → will open a POST request to "http://localhost:5984/" adding headers with "Accept: application/json"
  31. # and "Content-Type: application/json" sending the data from the "--data" argument. ynh_local_curl will
  32. # return with an error if the servers response does not match the extended regex '"ok":true'.
  33. #
  34. # For multiple calls, cookies are persisted between each call for the same app.
  35. #
  36. # `$domain` and `$path_url` need to be defined externally if the first form for the 'page' argument is used.
  37. #
  38. # The return code of this function will vary depending of the use of --line_match:
  39. #
  40. # If --line_match has been used the return code will be the one of the grep checking line_match
  41. # against the output of curl. The output of curl will not be returned.
  42. #
  43. # If --line_match has not been provided the return code will be the one of the curl command and
  44. # the output of curl will be echoed.
  45. #
  46. # Requires YunoHost version 2.6.4 or higher.
  47. # Declare an array to define the options of this helper.a
  48. local -A supported_methods=( [PUT]=1 [POST]=1 [GET]=1 [DELETE]=1 )
  49. local legacy_args=Ld
  50. local -A args_array=( [l]=line_match= [m]=method= [H]=header= [n]=no_sleep [L]=location= [d]=data= [u]=user= [p]=password= [s]=seperator= )
  51. local line_match
  52. local method
  53. local -a header
  54. local no_sleep
  55. local location
  56. local user
  57. local password
  58. local seperator
  59. local -a data
  60. local -a curl_opt_args # optional arguments to `curl`
  61. # Manage arguments with getopts
  62. ynh_handle_getopts_args "$@"
  63. # make sure method is a supported one
  64. if ! [[ -v supported_methods[$method] ]]; then
  65. ynh_die --message="method $method not supported by ynh_local_curl"
  66. fi
  67. # Define url of page to curl
  68. # $location contains either an URL or just a page
  69. local full_page_url
  70. if [[ "$location" =~ ^https?:// ]]; then
  71. # if $location starts with an http-protocol use value as a complete URL
  72. full_page_url="$location"
  73. elif [ "${path_url}" == "/" ]; then
  74. # if $path_url points to the webserver root just append $location to localhost URL
  75. full_page_url="https://localhost$(ynh_normalize_url_path $location)"
  76. else
  77. # else append $path_url and $location to localhost URL
  78. full_page_url="https://localhost${path_url}$(ynh_normalize_url_path $location)"
  79. fi
  80. flohmarkt_print_debug "full_page_url='$full_page_url'"
  81. # Concatenate data
  82. # POST: all elements of array $data in one string seperated by '&'
  83. # PUT: all elements of $data concatenated in one string
  84. # GET: no data
  85. # DELETE: no data
  86. # if not defined by --seperator set default
  87. if [[ -v seperator ]] && [[ "$seperator" == 'none' ]]; then
  88. seperator=''
  89. elif ! [[ -v seperator ]] && [[ "$method" == 'PUT' ]]; then
  90. seperator=''
  91. elif ! [[ -v seperator ]]; then
  92. seperator='&'
  93. fi
  94. join_by() { local IFS="$1"; shift; echo "$*"; }
  95. local P_DATA=$( join_by "$seperator" ${data[@]} )
  96. if [[ "$P_DATA" != '' ]]; then curl_opt_args+=('--data'); curl_opt_args+=("$P_DATA"); fi
  97. # prepend every element in header array with " -H "
  98. local seq=0
  99. while [[ -v header ]] && [[ $seq -lt ${#header[@]} ]]; do
  100. curl_opt_args+=('-H')
  101. curl_opt_args+=("${header[$seq]}")
  102. seq=$(( $seq + 1 ))
  103. done
  104. # build --user for curl
  105. if [[ -n "$user" ]] && [[ -n "$password" ]]; then
  106. curl_opt_args+=('--user' "$user:$password")
  107. elif [[ -n "$user" ]] && [[ -z "$password" ]]; then
  108. ynh_die --message="user provided via '-u/--user' needs password specified via '-p/--password'"
  109. fi
  110. flohmarkt_print_debug "long string curl_opt_args='${curl_opt_args[@]}'"
  111. seq=0
  112. while [[ $seq -lt ${#curl_opt_args[@]} ]]; do
  113. flohmarkt_print_debug " opt[$seq]='${curl_opt_args[$seq]}'"
  114. seq=$(( $seq + 1 ))
  115. done
  116. # https://github.com/YunoHost/yunohost/pull/547
  117. # Wait untils nginx has fully reloaded (avoid curl fail with http2) unless disabled
  118. if ! [[ -v no_sleep ]]; then
  119. sleep 2
  120. fi
  121. local app=${app:-testing}
  122. local cookiefile=/tmp/ynh-$app-cookie.txt
  123. touch $cookiefile
  124. chown root $cookiefile
  125. chmod 700 $cookiefile
  126. # Temporarily enable visitors if needed...
  127. # TODO maybe there's a way to do this using --user and --password instead?
  128. # would improve security
  129. if ! [[ "$app" == "testing" ]]; then
  130. local visitors_enabled=$(ynh_permission_has_user "main" "visitors" && echo yes || echo no)
  131. if [[ $visitors_enabled == "no" ]]; then
  132. ynh_permission_update --permission "main" --add "visitors"
  133. fi
  134. fi
  135. flohmarkt_print_debug executing \'\
  136. curl --silent --show-error --insecure --location --resolve "$domain:443:127.0.0.1" \
  137. --header "Host: $domain" --cookie-jar $cookiefile --cookie $cookiefile \
  138. "${curl_opt_args[@]}" "$full_page_url"\'
  139. # Curl the URL
  140. local curl_result=$( curl --request "$method" --silent --show-error --insecure --location \
  141. --header "Host: $domain" --cookie-jar $cookiefile --cookie $cookiefile \
  142. --resolve "$domain:443:127.0.0.1" "${curl_opt_args[@]}" "$full_page_url" )
  143. local curl_error=$?
  144. flohmarkt_print_debug "curl_result='$curl_result' ($curl_error)"
  145. # check result agains --line_match if provided
  146. if [[ -v line_match ]] && [[ -n $line_match ]]; then
  147. printf '%s' "$curl_result" | grep "$line_match" > /dev/null
  148. # will return the error code of the above grep
  149. curl_error=$?
  150. else
  151. # no --line_match, return curls error code and output
  152. echo $curl_result
  153. fi
  154. # re-enable security
  155. if [[ -v visitor_enabled ]] && [[ $visitors_enabled == "no" ]]; then
  156. ynh_permission_update --permission "main" --remove "visitors"
  157. fi
  158. return $curl_error
  159. }