Changeset fb0ede0 in tol


Ignore:
Timestamp:
May 24, 2011, 3:28:37 PM (15 years ago)
Author:
Jorge Pérez <josp.jorge@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
2ae8ad2
Parents:
3f32a91
Message:
  • puede resolver #1166
  • fixes #927
  • el loop de 1 a 100 del ticket #1207 no falla, al menos en linux, habria que reescribirlo asi:
    Set For(1, 100, Set (Real i) {
      WriteLn(""<<i);
      Set CurlApi::GetUrl(
      [[ Text url = "http://packages.tol-project.org/OfficialTolArchiveNetwork/"
        "repository.php?tol_package_version=1.1&tol_version=v2.0.1%20b.4&"
        "action=ping&key=658184943" ]]
      )});
    

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@3575 be4846ae-f051-483a-831f-b64e7d08527f

Location:
tol/stdlib/general/system/CurlApi
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tol/stdlib/general/system/CurlApi/CurlFunctions.tol

    r3f32a91 rfb0ede0  
    1717};
    1818
     19Text _.autodoc.member.GetUrl =
     20  "Download an URL.\n"
     21  "Arguments:\n"
     22  "Args is a NameBlock containing the named arguments for this function, which are:\n"
     23  "  - Text url : the target url, this is a required argument\n"
     24  "  - Text file : the destination file, this is an optional argument, if this argument is not given then the url contents is returned in the result of the funnction.\n"
     25  "  - Real timeout : set the maximum time in seconds that you allow the transfer operation to take. Do note that normally, name lookups may take a considerable time and that limiting the operation to less than a few minutes risks aborting perfectly normal operations. This is an optional argument, it defaults to 0 which means no timeout.\n"
     26  "  - Real connectiontimeout : set the maximum time in seconds that you allow the connection to the server to take. This only limits the connection phase, once it has connected, this option is of no more use. This is an optional argument, it defaults to 0 which means no timeout.\n"
     27  "  - Real verbose : when True display a lot of verbose information about its operations. Very useful for protocol debugging and understanding.\n"
     28  "Result:\n"
     29  "The result is a Set containing information about the transfer. If the transfer is successful it returns a Set with the following fields:\n"
     30  "  - Real connectTime : the time, in seconds, it took from the start until the connect to the remote host (or proxy) was completed.\n"
     31  "  - Real fileTime: the remote time of the retrieved document (in number of seconds since 1 jan 1970 in the GMT/UTC time zone). If you get -1, it can be because of many reasons (unknown, the server hides it or the server doesn't support the command that tells document time etc) and the time of the document is unknown.\n"
     32  "  - Real totalTime : the total transaction time, in seconds, for the transfer, including name resolving, TCP connect etc.\n"
     33  "  - Real sizeDownload : the total amount of bytes that were downloaded.\n"
     34  "  - Real speedDownload : the average download speed, measured in bytes/second, for the complete download.\n"
     35  "  - Text nameData : the name of the field containing the downloaded data. It could take two values, \"file\" if the download was requested to be saved into a file or \"bodyData\" if no file destination was specified in the invocation.\n"
     36  "  - Text file : the file where the downloaded data where saved.\n"
     37  "  - Text bodyData : the downloaded data if no destination file was specified.\n"
     38  "If the transfer failed the result is a Set with the following fields:\n"
     39  "  - Real failStatus : the error code.\n"
     40  "  - Text explain : a string with a description of the error.\n"
     41  "Examples:\n
     42  // Ex. 1: request an http url and return the result in a variable.
     43  Set result1 = CurlApi::GetUrl( [[ Text url = \"http://packages.tol-project.org/OfficialTolArchiveNetwork/repository.php?tol_package_version=1.1&tol_version=v2.0.1%20b.4&action=ping&key=658184943\", Real verbose=1, Real connecttimeout=3 ]] );
     44  If( result1::failStatus, {
     45    WriteLn( \"CurlApi::GetUrl error: \"<<result1::details )
     46    }, {
     47    WriteLn( \"CurlApi::GetUrl ok, the result is: \"<<result1::bodyData )
     48  } );
     49
     50  // Ex. 2: request an http url and download the result into a file
     51  Set result2 = CurlApi::GetUrl( [[ Text url = \"http://packages.tol-project.org/OfficialTolArchiveNetwork/repository.php?action=download&format=attachment&package=BysMcmc.4.4\", Text file=\"/tmp/BysMcmc.4.4.zip\", Real verbose=1, Real connecttimeout=3 ]] );
     52  If( result2::failStatus, {
     53    WriteLn( \"CurlApi::GetUrl error: \"<<result2::details )
     54    }, {
     55    WriteLn( \"CurlApi::GetUrl ok, the result was downloaded to: \"<<result2::file )
     56  } );
     57
     58  // Ex. 3: request an ftp url and download the result into a file
     59  Set result3 = CurlApi::GetUrl( [[ Text url = \"ftp://ftp.rediris.es//pub/OpenBSD/README\", Text file=\"/tmp/README\", Real verbose=1, Real connecttimeout=30 ]] );
     60  "
     61  ;
    1962Set GetUrl( NameBlock args )
    2063{
    … …  
    2366 
    2467  Text url = args::url;
    25   Text file = args::file;
     68  //Text file = getOptArg::file;
     69  Text file = getOptArg( args, "file", "" );
     70  Real verbose = getOptArg( args, "verbose", 0 );
     71  Real connecttimeout = getOptArg( args, "connecttimeout", 5 );
     72  // un paquete no debe tardar mas de 60 segundos en bajar
     73  Real timeout = getOptArg( args, "timeout", 60 );
    2674  // otros argumentos opcionales seran tratados mas adelante
    27   Real verbose = getOptArg( args, "verbose", 0 );
    28   Set cmd = [[ "::TolCurl::getUrl", url, file ]];
     75  Set cmd = [[ "::TolCurl::getUrl", url,
     76               "-file", file, "-verbose", If( verbose, "1", "0" ),
     77               "-connecttimeout", FormatReal(connecttimeout, "%.0f"),
     78               "-timeout", FormatReal(timeout, "%.0f") ]];
    2979  Set tclResult = Tcl_EvalEx( cmd );
    3080  Real status = tclResult["status"];
    3181  Set If( status, {
    32       Set curlResult = {
     82      Set curlResult = { 
    3383        Eval( tclResult["result"] )
    3484      }
    … …  
    3888    } )
    3989};
     90
     91// Set result = CurlApi::GetUrl( [[ Text url = "http://packages.tol-project.org/OfficialTolArchiveNetwork/repository.php?tol_package_version=1.1&tol_version=v2.0.1%20b.4&action=ping&key=658184943", Real verbose=1, Real connecttimeout=3 ]] );
  • tol/stdlib/general/system/CurlApi/TolCurl.tcl

    r3f32a91 rfb0ede0  
    22
    33namespace eval ::TolCurl {
     4  # infoType specifies what kind of information it is (0 text, 1
     5  # incoming header, 2 outgoing header, 3 incoming data, 4 outgoing
     6  # data, 5 incoming SSL data, 6 outgoing SSL data).
     7  array set infoTypeConf {
     8    0 "**"
     9    0,PRINT 1
     10    0,DESCRIPTION "text"
     11    1 <-
     12    1,PRINT 1
     13    1,DESCRIPTION "incoming header"
     14    2 ->
     15    2,PRINT 1
     16    2,DESCRIPTION "outgoing header"
     17    3 <*
     18    3,PRINT 0
     19    3,DESCRIPTION "incoming data"
     20    4 *>
     21    4,PRINT 0
     22    4,DESCRIPTION "outgoing data"
     23    5 <*
     24    5,PRINT 0
     25    5,DESCRIPTION "incoming SSL data"
     26    6 *>
     27    6,PRINT 0
     28    6,DESCRIPTION "outgoing SSL data"
     29  }
    430}
    531
    6 proc ::TolCurl::getUrl0 { url receiveFile args } {
     32proc ::TolCurl::upload { sendFile url args } {
    733  set curlHandle [ ::curl::init ]
    834 
    9   $curlHandle configure -url               $url \
    10                          -verbose           0 \
     35  if { ![ file exists $sendFile ] || ![ file readable $sendFile ] } {
     36    set failStatus -2
     37    set explain "file $sendFile does not exists or is not readable"
     38    return [ list failStatus $failStatus explain $explain ]
     39  }
     40
     41  set fileSize [ file size $sendFile ]
     42
     43  $curlHandle configure  -url               $url \
     44                         -verbose           1 \
    1145                         -errorbuffer       errorBuffer \
    12                          -file              $receiveFile \
    13                          -failonerror       1 \
    14                          -followlocation    1 \
    15                          -filetime          1
     46                         -upload            1 \
     47                         -infile            $sendFile \
     48                         -infilesize        $fileSize \
     49                         -failonerror       1
     50  if { [ llength $args ] } {
     51    eval $curlHandle configure $args
     52  }
    1653
    17                          #-userpwd           $userid:$password \
    18                          #-proxy             $proxyHost \
    19                          #-proxyport         $proxyport \
    20                          #-proxytype         http \
    21                          # -verbose           1 \
     54  set r 0
     55  if { [ catch { $curlHandle perform } r ] } {   
     56    $curlHandle cleanup
     57    #puts "'$r -- $errorBuffer'"
     58    #return -code error "$r $errorBuffer"
     59    if { [ string is integer $r ] } {
     60      set status $r
     61    } else {
     62      set status -1
     63      set errorBuffer $r
     64    }
     65    #return -code error $errorBuffer
     66    return [ list failStatus $status explain $errorBuffer ]
     67  }
     68 
     69  set totalTime    [ $curlHandle getinfo totaltime     ]
     70  set connectTime  [ $curlHandle getinfo connecttime   ]
    2271
     72  set details \
     73      [ list failStatus $r totalTime $totalTime connectTime $connectTime ]
     74 
     75  $curlHandle cleanup
     76 
     77  return $details
     78}
     79
     80#::TolCurl::upload TolCurl.tcl scp://packages.localbayes.es/var/www/packages/TolCurl.tcl -userpwd "toldevel:toldevelbys"
     81
     82
     83proc ::TolCurl::getUrl0 { url args } {
     84  set curlHandle [ ::curl::init ]
     85 
     86  array set options {
     87    -file ""
     88    -verbose 0
     89    -timeout 0
     90    -connecttimeout 5
     91    -debugproc ""
     92  }
     93
     94  array set options $args
     95
     96  set bodyData ""
     97  if { $options(-file) eq "" } {
     98    set optionDest "-bodyvar"
     99    set nameDest "bodyData"
     100    set valueDest bodyData
     101  } else {
     102    set optionDest "-file"
     103    set nameDest "file"
     104    set valueDest $options(-file)
     105  }
     106  $curlHandle configure \
     107      -url               $url \
     108      -verbose           $options(-verbose) \
     109      -errorbuffer       errorBuffer \
     110      $optionDest        $valueDest \
     111      -failonerror       1 \
     112      -followlocation    1 \
     113      -filetime          1 \
     114      -connecttimeout    $options(-connecttimeout) \
     115      -timeout           $options(-timeout) \
     116      -debugproc         $options(-debugproc)
     117 
     118  #-userpwd           $userid:$password \
     119  #-proxy             $proxyHost \
     120  #-proxyport         $proxyport \
     121  #-proxytype         http \
     122     
    23123  set r 0
    24124  if { [ catch { $curlHandle perform } r ] } {   
    … …  
    40140  set connectTime  [ $curlHandle getinfo connecttime   ]
    41141  set sizeDownload [ $curlHandle getinfo sizedownload  ]
    42   set speedDownoad [ $curlHandle getinfo speeddownload ]
     142  set speedDownload [ $curlHandle getinfo speeddownload ]
    43143 
    44144  #puts " totalTime    = $totalTime    "
    … …  
    47147  #puts " speedDownoad = $speedDownoad "
    48148 
    49   set details [ list failStatus $r fileTime $fileTime totalTime $totalTime connectTime $connectTime sizeDownload $sizeDownload speedDownload $speedDownoad ]
     149  set details [ list failStatus $r fileTime $fileTime totalTime $totalTime \
     150                    connectTime $connectTime sizeDownload $sizeDownload \
     151                    speedDownload $speedDownload nameData $nameDest \
     152                    $nameDest [expr { $nameDest eq "file" ? 
     153                                      $options(-file) : $bodyData } ] ]
    50154 
    51155  $curlHandle cleanup
    … …  
    54158}
    55159
    56 proc ::TolCurl::getUrl { url receiveFile args } {
    57   set details [ eval getUrl0 [list $url] [list $receiveFile] $args ]
     160#::TolCurl::getUrl0 "http://packages.tol-project.org/OfficialTolArchiveNetwork/repository.php?tol_package_version=1.1&tol_version=v2.0.1%20b.4&action=ping&key=658184943"
     161
     162# infoType specifies what kind of information it is (0 text, 1
     163# incoming header, 2 outgoing header, 3 incoming data, 4 outgoing
     164# data, 5 incoming SSL data, 6 outgoing SSL data).
     165proc ::TolCurl::debugProc { infoType data } {
     166  variable infoTypeConf
     167
     168 
     169  if { $infoTypeConf($infoType,PRINT) } {
     170    set _data "[string trim $data]\n"
     171  } else {
     172    set _data "$infoTypeConf($infoType,DESCRIPTION)\n"
     173  }
     174  Tol_HciWriter "$infoTypeConf($infoType) $_data"
     175}
     176
     177proc ::TolCurl::getUrl { url args } {
     178  set details [ eval getUrl0 [list $url] $args -debugproc ::TolCurl::debugProc ]
    58179  set textItems [list]
    59180  foreach {key value} $details {
    60     set item [ expr { $key eq "explain" ?
    61                       "Text $key=\"$value\"" : "Real $key=$value"} ]
     181    set item [ expr { [string is double $value] ?
     182                      "Real $key=$value" : "Text $key=\"$value\""} ]
    62183    lappend textItems $item
    63184  }
    … …  
    66187}
    67188
    68 #puts [TolCurl::getUrl "http://packages.tol-project.org/tol_pkg/v2_0_1/GuiTools.oza" "/tmp1/GuiTools.oza"]
    69 #puts [TolCurl::getUrl "http://packages.tol-project.org/tol_pkg/v2_0_1" "/tmp/v2_0_1"]
     189#puts [TolCurl::getUrl "http://packages.tol-project.org/tol_pkg/v2_0_1/GuiTools.oza" -file "/tmp1/GuiTools.oza"]
     190#puts [TolCurl::getUrl "http://packages.tol-project.org/tol_pkg/v2_0_1" -file "/tmp/v2_0_1"]
Note: See TracChangeset for help on using the changeset viewer.