Diff
Not logged in

Differences From Artifact [cbf79b96a5]:

To Artifact [7f985511d4]:


81
82
83
84
85
86
87










88






89
90
91
92
93
94
95
  if {[info exists pkgr_path]} then {
    catch {
      ::Eagle::exportAndImportPackageCommands ::Eagle \
          [list addToPath appendArgs getDictionaryValue \
          isEagle isWindows readFile writeFile] false false
    }











    unset -nocomplain pkgr_path






  }

  #
  # NOTE: This procedure is used to provide a TIP #194 compatible [apply]
  #       command to the native Tcl 8.4 interpreter.  Eagle and native Tcl
  #       8.5 (or higher) have this command built-in.  The lambdaExpr
  #       argument must be a list with two or three elements.  The first







>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  if {[info exists pkgr_path]} then {
    catch {
      ::Eagle::exportAndImportPackageCommands ::Eagle \
          [list addToPath appendArgs getDictionaryValue \
          isEagle isWindows readFile writeFile] false false
    }

    #
    # NOTE: Manually load package containing some common procedures that
    #       are needed by this package.
    #
    source [file join $pkgr_path common.tcl]

    #
    # NOTE: Unset the "pkgr_path" variable as it will no longer be needed
    #       after this point.
    #
    unset -nocomplain pkgr_path

    #
    # NOTE: Attempt to import the procedures exposed by the common tools
    #       package.
    #
    namespace import ::Eagle::Tools::Common::getFileViaHttp
  }

  #
  # NOTE: This procedure is used to provide a TIP #194 compatible [apply]
  #       command to the native Tcl 8.4 interpreter.  Eagle and native Tcl
  #       8.5 (or higher) have this command built-in.  The lambdaExpr
  #       argument must be a list with two or three elements.  The first
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113

  #
  # NOTE: This procedure sets up the default values for all configuration
  #       parameters used by the package repository client.  There are no
  #       arguments.
  #
  proc setupPackageUnknownVars {} {
    #
    # NOTE: Should the HTTP request processor attempt to force the use of
    #       HTTPS for URIs that were originally HTTP?  This setting is only
    #       applicable to native Tcl.
    #
    variable forceSecureUri; # DEFAULT: true

    if {![info exists forceSecureUri]} then {
      set forceSecureUri true
    }

    #
    # NOTE: Is this HTTP request processor allowed to use plain HTTP if/when
    #       the "tls" package is not available?  This should only be changed
    #       if the "tls" package cannot be easily installed for use with the
    #       native Tcl interpreter in use.  It should be noted here that the
    #       official package repository server reserves the right to refuse
    #       plain HTTP connections, which means that changing this setting
    #       may be totally pointless.
    #
    variable allowInsecureUri; # DEFAULT: false

    if {![info exists allowInsecureUri]} then {
      set allowInsecureUri false
    }

    #
    # NOTE: Emit diagnostic messages when the [::http::geturl] procedure is
    #       about to be called?
    #
    variable verboseGetUrl; # DEFAULT: false

    if {![info exists verboseGetUrl]} then {
      set verboseGetUrl false
    }

    #
    # NOTE: Is this HTTP request processor allowed to use plain HTTP if/when
    #       the server responds with an HTTP redirect location to an original
    #       URI that was HTTPS?  Otherwise, a script error will result.
    #
    variable allowInsecureRedirect; # DEFAULT: false

    if {![info exists allowInsecureRedirect]} then {
      set allowInsecureRedirect false
    }

    #
    # NOTE: What is the default set of API keys if none were set explicitly?
    #       This list is subject to change at any time -AND- may be empty or
    #       may contain non-working API keys, please do not rely on it.
    #
    variable autoApiKeys; # DEFAULT: 0000000000000000000000000000000000000000








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







2069
2070
2071
2072
2073
2074
2075















































2076
2077
2078
2079
2080
2081
2082

  #
  # NOTE: This procedure sets up the default values for all configuration
  #       parameters used by the package repository client.  There are no
  #       arguments.
  #
  proc setupPackageUnknownVars {} {















































    #
    # NOTE: What is the default set of API keys if none were set explicitly?
    #       This list is subject to change at any time -AND- may be empty or
    #       may contain non-working API keys, please do not rely on it.
    #
    variable autoApiKeys; # DEFAULT: 0000000000000000000000000000000000000000

2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
    #
    # NOTE: Attempt to load the requested package using the metadata
    #       extracted in the previous step.
    #
    processLookupMetadata metadata
  }

  if {![isEagle]} then {
    ###########################################################################
    ############################# BEGIN Tcl ONLY ##############################
    ###########################################################################

    #
    # NOTE: This procedure was stolen from the "getEagle.tcl" script.  It is
    #       designed to emit a message to the console.  The channel argument
    #       is the channel where the message should be written.  The string
    #       argument is the content of the message to emit.
    #
    proc pageOut { channel string } {
      catch {
        puts -nonewline $channel $string; flush $channel
      }
    }

    #
    # NOTE: This procedure was stolen from the "getEagle.tcl" script.  It is
    #       designed to emit a message to the HTTP client log.  The string
    #       argument is the content of the message to emit.
    #
    proc pageLog { string } {
      catch {
        tclLog [appendArgs [pid] " : " [clock seconds] " : http : " $string]
      }
    }

    #
    # NOTE: This procedure was stolen from the "getEagle.tcl" script.  It is
    #       designed to emit a progress indicator while an HTTP request is
    #       being processed.  The channel argument is the Tcl channel where
    #       the progress indicator should be emitted.  The type argument is
    #       the single-character progress indicator.  The milliseconds
    #       argument is the number of milliseconds to wait until the next
    #       periodic progress indicator should be emitted.  This procedure
    #       reschedules its own execution.
    #
    proc pageProgress { channel type milliseconds } {
      #
      # NOTE: This variable is used to keep track of the currently scheduled
      #       (i.e. pending) [after] event.
      #
      variable afterForPageProgress

      #
      # NOTE: Show that something is happening...
      #
      pageOut $channel $type

      #
      # NOTE: Make sure that we are scheduled to run again, if requested.
      #
      if {$milliseconds > 0} then {
        set afterForPageProgress [after $milliseconds \
            [namespace code [list pageProgress $channel $type \
            $milliseconds]]]
      } else {
        unset -nocomplain afterForPageProgress
      }
    }

    #
    # NOTE: This procedure was stolen from the "getEagle.tcl" script.  It is
    #       designed to process a single HTTP request, including any HTTP
    #       3XX redirects (up to the specified limit), and return the raw
    #       HTTP response data.  It may raise any number of script errors.
    #
    # <public>
    proc getFileViaHttp { uri redirectLimit channel quiet args } {
      #
      # NOTE: This global variable is used to check the running version of
      #       Tcl.
      #
      global tcl_version

      #
      # NOTE: This variable is used to determine if plain HTTP URIs should be
      #       converted to HTTPS, if the "tls" package is available.
      #
      variable forceSecureUri

      #
      # NOTE: This variable is used to determine if plain HTTP is allowed if
      #       the "tls" package is not available.
      #
      variable allowInsecureUri

      #
      # NOTE: This variable is used to determine if a diagnostic message is
      #       emitted when [::http::geturl] is about to be called.
      #
      variable verboseGetUrl

      #
      # NOTE: This variable is used to determine if plain HTTP is allowed if
      #       an HTTP redirect response contains an HTTP URI and the original
      #       URI was HTTPS.
      #
      variable allowInsecureRedirect

      #
      # NOTE: This variable is used to keep track of the currently scheduled
      #       (i.e. pending) [after] event.
      #
      variable afterForPageProgress

      #
      # NOTE: This procedure requires the modern version of the HTTP package,
      #       which is typically included with the Tcl core distribution.
      #
      package require http 2.0

      #
      # NOTE: Tcl 8.6 added support for IPv6; however, on some machines this
      #       support can cause sockets to hang for a long time.  Therefore,
      #       for now, always force the use of IPv4.
      #
      if {[info exists tcl_version] && $tcl_version >= 8.6} then {
        namespace eval ::tcl::unsupported {}
        set ::tcl::unsupported::socketAF inet
      }

      #
      # NOTE: If the 'tls' package is available, always attempt to use HTTPS;
      #       otherwise, only attempt to use HTTP if explicitly allowed.
      #
      if {[catch {package require tls}] == 0} then {
        ::http::register https 443 [list ::tls::socket -tls1 true]

        if {$forceSecureUri} then {
          if {[string range $uri 0 6] eq "http://"} then {
            set uri [appendArgs https:// [string range $uri 7 end]]
          }
        }
      } else {
        if {$allowInsecureUri} then {
          if {[string range $uri 0 7] eq "https://"} then {
            set uri [appendArgs http:// [string range $uri 8 end]]
          }
        }
      }

      #
      # NOTE: Unless the caller forbids it, display progress messages during
      #       the download.
      #
      if {!$quiet} then {
        pageProgress $channel . 250
      }

      #
      # NOTE: All downloads are handled synchronously, which is not ideal;
      #       however, it is simple.  Keep going as long as there are less
      #       than X redirects.
      #
      set redirectCount 0

      while {1} {
        #
        # NOTE: Issue the HTTP request now, grabbing the resulting token.
        #
        if {$verboseGetUrl} then {
          #
          # NOTE: Emit important diagnostic information related to this
          #       HTTP request here.  This may be enhanced in the future.
          #
          pageLog [appendArgs \
              "attempting to download URL \"" $uri \"...]
        }

        set token [eval ::http::geturl [list $uri] $args]

        #
        # NOTE: Grab the HTTP response code and data now as they are needed
        #       in almost all cases.
        #
        set code [::http::ncode $token]; set data [::http::data $token]

        #
        # NOTE: Check the HTTP response code, in order to follow any HTTP
        #       redirect responses.
        #
        switch -glob -- $code {
          100 -
          101 -
          102 {
            ::http::cleanup $token; error [appendArgs \
                "unsupported informational HTTP response status code " \
                $code ", data: " $data]
          }
          200 -
          201 -
          202 -
          203 -
          204 -
          205 -
          206 -
          207 -
          208 -
          226 {
            #
            # NOTE: Ok, the HTTP response is actual data of some kind (which
            #       may be empty).
            #
            ::http::cleanup $token; break
          }
          301 -
          302 -
          303 -
          307 -
          308 {
            #
            # NOTE: Unless the caller forbids it, display progress messages
            #       when an HTTP redirect is returned.
            #
            if {!$quiet} then {
              pageProgress $channel > 0
            }

            #
            # NOTE: We hit another HTTP redirect.  Stop if there are more
            #       than X.
            #
            incr redirectCount

            #
            # TODO: Maybe make this limit more configurable?
            #
            if {$redirectCount > $redirectLimit} then {
              #
              # NOTE: Just "give up" and raise a script error.
              #
              ::http::cleanup $token; error [appendArgs \
                  "redirection limit of " $redirectLimit " exceeded"]
            }

            #
            # NOTE: Grab the metadata associated with this HTTP response.
            #
            array set meta [::http::meta $token]

            #
            # NOTE: Is there actually a new URI (location) to use?
            #
            if {[info exist meta(Location)]} then {
              #
              # NOTE: Ok, grab it now.  Later, at the top of the loop,
              #       it will be used in the subsequent HTTP request.
              #
              set location $meta(Location); unset meta

              #
              # NOTE: For security, by default, do NOT follow an HTTP
              #       redirect if it attempts to redirect from HTTPS
              #       to HTTP.
              #
              if {!$allowInsecureRedirect && \
                  [string range $uri 0 7] eq "https://" && \
                  [string range $location 0 7] ne "https://"} then {
                #
                # NOTE: Just "give up" and raise a script error.
                #
                ::http::cleanup $token; error [appendArgs \
                    "refused insecure redirect from \"" $uri "\" to \"" \
                    $location \"]
              }

              #
              # NOTE: Replace the original URI with the new one, for
              #       use in the next HTTP request.
              #
              set uri $location

              #
              # NOTE: Cleanup the current HTTP token now beause a new
              #       one will be created for the next request.
              #
              ::http::cleanup $token
            } else {
              #
              # NOTE: Just "give up" and raise a script error.
              #
              ::http::cleanup $token; error [appendArgs \
                  "redirect from \"" $uri "\" missing location, code " \
                  $code ", data: " $data]
            }
          }
          300 -
          304 -
          305 -
          306 {
            ::http::cleanup $token; error [appendArgs \
                "unsupported redirection HTTP response status code " $code \
                ", data: " $data]
          }
          4?? {
            ::http::cleanup $token; error [appendArgs \
                "client error HTTP response status code " $code ", data: " \
                $data]
          }
          5?? {
            ::http::cleanup $token; error [appendArgs \
                "server error HTTP response status code " $code ", data: " \
                $data]
          }
          default {
            ::http::cleanup $token; error [appendArgs \
                "unrecognized HTTP response status code " $code ", data: " \
                $data]
          }
        }
      }

      #
      # NOTE: If there is a currently scheduled [after] event, cancel it.
      #
      if {[info exists afterForPageProgress]} then {
        catch {after cancel $afterForPageProgress}
        unset -nocomplain afterForPageProgress
      }

      #
      # NOTE: If progress messages were emitted, start a fresh line.
      #
      if {!$quiet} then {
        pageOut $channel [appendArgs " " $uri \n]
      }

      return $data
    }

    ###########################################################################
    ############################## END Tcl ONLY ###############################
    ###########################################################################
  }

  #
  # NOTE: This package requires that support for namespaces, which is an
  #       optional feature of Eagle, must be enabled.
  #
  if {[isEagle] && ![namespace enable]} then {
    error "namespaces must be enabled for this package"
  }







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







2358
2359
2360
2361
2362
2363
2364

















































































































































































































































































































































2365
2366
2367
2368
2369
2370
2371
    #
    # NOTE: Attempt to load the requested package using the metadata
    #       extracted in the previous step.
    #
    processLookupMetadata metadata
  }


















































































































































































































































































































































  #
  # NOTE: This package requires that support for namespaces, which is an
  #       optional feature of Eagle, must be enabled.
  #
  if {[isEagle] && ![namespace enable]} then {
    error "namespaces must be enabled for this package"
  }