From 712e69d8302bf01e3b9677f71ad8763e3a99f1ec Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Thu, 6 Apr 2017 15:33:56 +0200 Subject: [PATCH] New helper ynh_local_curl (#288) * New helper ynh_curl_abstract Just an abstraction around curl command. To prevent forgot arguments and simplify this difficult command. * [enh] Switch args of ynh_curl_abstract * [enh] Rename ynh_curl_abstract into ynh_local_curl * Splitting the various key=value into several arguments * Comment to clarify bash command * Adding comment about $domain and $path_url --- data/helpers.d/utils | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/data/helpers.d/utils b/data/helpers.d/utils index 165a394d3..6bc1e39d1 100644 --- a/data/helpers.d/utils +++ b/data/helpers.d/utils @@ -24,3 +24,31 @@ ynh_get_plain_key() { fi done } + +# Curl abstraction to help with POST requests to local pages (such as installation forms) +# +# $domain and $path_url should be defined externally (and correspond to the domain.tld and the /path (of the app?)) +# +# example: ynh_local_curl "/install.php?installButton" "foo=$var1" "bar=$var2" +# +# usage: ynh_local_curl "page_uri" "key1=value1" "key2=value2" ... +# | arg: page_uri - Path (relative to $path_url) of the page where POST data will be sent +# | arg: key1=value1 - (Optionnal) POST key and corresponding value +# | arg: key2=value2 - (Optionnal) Another POST key and corresponding value +# | arg: ... - (Optionnal) More POST keys and values +ynh_local_curl () { + # Define url of page to curl + full_page_url=https://localhost$path_url$1 + + # Concatenate all other arguments with '&' to prepare POST data + POST_data="" + for arg in "${@:2}" + do + POST_data="${POST_data}${arg}&" + done + # (Remove the last character, which is an unecessary '&') + POST_data=${POST_data::-1} + + # Curl the URL + curl -kL -H "Host: $domain" --resolve $domain:443:127.0.0.1 --data "$POST_data" "$full_page_url" 2>&1 +}