ynh_local_curl 7.2 KB

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