Fetching an HTTPS URL
PLT Scheme's url.ss library in the net collection does not directly support retrieval of URLs via HTTPS, i.e. using SSL.
PLT's unit system can be used to create a version of the url module which does support HTTPS URLs. Place the following code in a file called e.g.
ssl-url.scm:
#lang scheme
(require scheme/unit
net/url-sig net/url-unit
net/tcp-sig net/tcp-unit
net/ssl-tcp-unit)
(define-values/invoke-unit
(compound-unit/infer (import) (export url^) (link tcp@ url@))
(import) (export url^))
(define ssl-tcp@ (make-ssl-tcp@ #f #f #f #f #f #f #f))
(define-values/invoke-unit
(compound-unit (import) (export URL)
(link [((TCP : tcp^)) ssl-tcp@]
[((URL : url^)) url@ TCP]))
(import) (export (prefix ssl: url^)))
(provide (all-defined-out))
You can then require this module using
(require "ssl-url.scm"), and use the standard url API described in
WebFetchingURL to retrieve HTTPS URLs.
This works by essentially creating a clone of PLT's
url.ss module, and linking it with
ssl-tcp-unit.ss instead of
tcp-unit.ss. The above code is simply a copy of
url.ss with the relevant details changed.
Note that a module which uses the above module can only fetch SSL URLs. Regular HTTP URLs can be fetched by requiring the original
url.ss. A module which needs to fetch both types of URL can do so by applying a prefix to the names of one of the modules, for example:
(require (prefix ssl: "ssl-url.scm"))
...and then use procedure names like
ssl:get-pure-port to retrieve SSL URLs.
Also note that this still supports
http:// URLs only. You will need
to translate
https://foo.com/... to
http://foo.com:443/... (in
case it doesn't have a port).
This code was based on an
example provided by Noel Welsh.
--
AntonVanStraaten - 29 Nov 2005
Ergo, Anton is a hero.
--
NoelWelsh - 01 Dec 2005
Updated for PLT v370.
--
DaveGurnell - 31 May 2007
I needed to change "unitsig.ss" to "unit.ss" to work in v371. I'd change the code but how to do that is not clear. :-P
--
PeteHopkins - 04 Sep 2007
I've edited the code. Some source is stored separately in the
Scm web with the intent of being able to easily produce a collection of source from the Cookbook. Not that producing such a collection has actually been done, but it could be. Easily. :)
--
AntonVanStraaten - 05 Sep 2007
Here's an example of its use:
#lang scheme
(require "ssl-url.ss")
(ssl:call/input-url
(string->url "https://www.amazon.com/")
ssl:get-impure-port
(lambda (ip)
(fprintf (current-error-port) "Look out, nellie!!~%")
(copy-port ip (current-output-port))))
--
EricHanchrow - 17 Dec 2008