ynh_local_curl 7.1 KB

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