changeset 658:33580ee2579e

utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 19 Feb 2021 21:02:53 +0200
parents f46c152183a2
children eddf0ff17fbd
files utillib.tcl
diffstat 1 files changed, 70 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/utillib.tcl	Fri Feb 19 19:30:53 2021 +0200
+++ b/utillib.tcl	Fri Feb 19 21:02:53 2021 +0200
@@ -23,14 +23,6 @@
 set utl_html_ent_list [split [encoding convertfrom "utf-8" $utl_html_ent_str] "|"]
 
 
-# Convert given string, containing HTML/XML style entities into a normal
-# UTF-8 Unicode string, using the above entity->character mapping
-proc utl_convert_html_ent {udata} {
-  global utl_html_ent_list
-  return [string map -nocase $utl_html_ent_list [string map $utl_html_ent_list $udata]]
-}
-
-
 # Split given string "str" into a list of sub-strings of maximum length
 # "maxlen", by attempting to split at "words", if possible.
 proc utl_str_split {str maxlen} {
@@ -155,6 +147,76 @@
 
 
 ###
+### HTML / HTTP related
+###
+# Convert given string, containing HTML/XML style entities into a normal
+# UTF-8 Unicode string, using the above entity->character mapping
+proc utl_convert_html_ent {udata} {
+  global utl_html_ent_list
+  return [string map -nocase $utl_html_ent_list [string map $utl_html_ent_list $udata]]
+}
+
+
+proc utl_http_clear_request { urlStatus urlSCode urlCode urlData urlMeta } {
+
+  ### Clear the request data
+  upvar $urlStatus ustatus
+  upvar $urlSCode uscode
+  upvar $urlCode ucode
+  upvar $urlData udata
+  upvar $urlMeta umeta
+
+  unset ustatus
+  unset uscode
+  unset ucode
+  unset udata
+  array unset umeta
+}
+
+
+proc utl_http_do_request { urlHeaders urlStr urlStatus urlSCode urlCode urlData urlMeta } {
+
+  upvar $urlStatus ustatus
+  upvar $urlSCode uscode
+  upvar $urlCode ucode
+  upvar $urlData udata
+  upvar $urlMeta umeta
+
+  #set urlHeaders {}
+  #lappend urlHeaders "Accept-Encoding" "identity"
+  #lappend urlHeaders "Connection" "keep-alive"
+
+  ### Perform request
+  if {[catch {set utoken [::http::geturl $urlStr -timeout 6000 -binary 1 -headers $urlHeaders]} uerror]} {
+    set uscode $uerror
+    return -1
+  }
+
+  ### Check status
+  set ustatus [::http::status $utoken]
+  set uscode [::http::code $utoken]
+  set ucode [::http::ncode $utoken]
+
+  if {$ustatus != "ok"} {
+    return -2
+  }
+
+  ### Get data
+  set udata [::http::data $utoken]
+  array set umeta [::http::meta $utoken]
+  ::http::cleanup $utoken
+
+  ### Sanitize the metadata KEYS
+  foreach {ukey uvalue} [array get umeta] {
+    set ukey [string tolower $ukey]
+    set umeta($ukey) $uvalue
+  }
+
+  return 0
+}
+
+
+###
 ### SQL database handling
 ###
 proc utl_sql_init {ndb_handle db_type db_name db_host db_port db_user db_pass} {