Paste number 134012: openssl egg and read-byte

Index of paste annotations: 1

Paste number 134012: openssl egg and read-byte
Pasted by: sethalves
When:7 years, 8 months ago
Share:Tweet this! | http://paste.lisp.org/+2VEK
Channel:None
Paste contents:
Raw Source | XML | Display As
#!/usr/local/bin/csi -script

(require-extension tcp)
(require-extension openssl)

;; utf8 encoding of cent symbol is ef bf a0

(let-values (((insock outsock) (ssl-connect "headache.hungry.com" 443)))
;; (let-values (((insock outsock) (tcp-connect "headache.hungry.com" 80)))
  (write-string
   (string-append "GET /~seth/junk/utf8-test0.txt HTTP/1.1\r\n"
                  "Host: headache.hungry.com\r\n\r\n") #f outsock)
  (let loop ((result '())
             (x 0))
    (cond ((= x 3)
           (display
            (map
             (lambda (n) (number->string n 16))
             (reverse result)))
           (newline)
           (display (apply string (map integer->char (reverse result))))
           (newline))
          (else
           (let ((c (read-byte insock)))
             (loop (cons c result)
                   (if (= c 120) (+ x 1) x)))))))

Annotations for this paste:

Annotation number 1: more openssl and read-byte
Pasted by: sethalves
When:7 years, 8 months ago
Share:Tweet this! | http://paste.lisp.org/+2VEK/1
Paste contents:
Raw Source | Display As
#!/usr/local/bin/csi -script

(require-extension srfi-18)
(require-extension openssl)

(define (show-result result)
  (display
   (map
    (lambda (n) (number->string n 16))
    (reverse result)))
  (newline)
  (display (apply string (map integer->char (reverse result))))
  (newline))

(define (client)
  (make-thread
   (lambda ()
     (let-values (((in-client out-client) (ssl-connect "localhost" 33333)))
       (for-each
        (lambda (c) (write-byte c out-client))
        (list #x61 #x62 #x63 #xef #xbf #xa0 #x31 #x32 #x33 #x0a))))))

(let ((t (client))
      (listener (ssl-listen 33333)))
  (ssl-load-certificate-chain! listener "certificate-chain.pem")
  (ssl-load-private-key! listener "private-key.pem")
  (thread-start! t)
  (let-values (((in-server out-server) (ssl-accept listener)))
    (let loop ((result '()))
      (let ((c (read-byte in-server)))
        (cond ((= c 10) (show-result result))
              (else
               (loop (cons c result))))))))

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.