#!/usr/bin/env scheme-script
;; -*- mode: scheme; coding: utf-8 -*- !#
;; Copyright © 2009, 2010 Göran Weinholt <goran@weinholt.se>
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
#!r6rs

(import (prefix (weinholt crypto x509) x509:)
        (weinholt net tls)
        (weinholt net tls simple)
        (rnrs))

(define (print . x) (for-each display x) (newline))

(let-values (((in out conn)
              (tls-connect (cadr (command-line))
                           (caddr (command-line)))))

  (print "Server certificate validation: "
         (x509:validate-certificate-path (tls-conn-remote-certs conn)
                                         (cadr (command-line))))

  (print "Sending GET request.")
  
  (put-bytevector out (string->utf8
                       (string-append "GET / HTTP/1.1\r\n"
                                      "Host: " (cadr (command-line)) ":" (caddr (command-line)) "\r\n"
                                      "Connection: close\r\n"
                                      "\r\n\r\n")))
  (flush-output-port out)
  (let lp ()
    (unless (port-eof? in)
      (display (utf8->string (get-bytevector-n in 128)))
      (lp)))
  (close-port in)
  (close-port out))
