Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | added scgi lib |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
c02c5fc05484414607465924afb95059 |
User & Date: | aldo 2016-12-11 14:28:46 |
Context
2016-12-11
| ||
14:36 | minor scgi fixes check-in: c1a0ba8bf0 user: aldo tags: trunk | |
14:28 | added scgi lib check-in: c02c5fc054 user: aldo tags: trunk | |
13:57 | added missing license headers check-in: 03e5fe6318 user: aldo tags: trunk | |
Changes
Added scgi.sls.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
;; ;; Copyright 2016 Aldo Nicolas Bruno ;; ;; Licensed under the Apache License, Version 2.0 (the "License"); ;; you may not use this file except in compliance with the License. ;; You may obtain a copy of the License at ;; ;; http://www.apache.org/licenses/LICENSE-2.0 ;; ;; Unless required by applicable law or agreed to in writing, software ;; distributed under the License is distributed on an "AS IS" BASIS, ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ;; See the License for the specific language governing permissions and ;; limitations under the License. (library (scgi) (export scgi-request-handler handle-scgi-connection run-scgi) (import (chezscheme) (socket) (netstring) (only (srfi s1 lists) list-index take drop) (only (posix) fork wait-for-pid wait-flag)) (define (header-get-token l) (let ([i (list-index zero? l)]) (values (take l i) (drop l (+ i 1))))) (define (list-u8->string l) (utf8->string (apply bytevector l))) (define (read-headers sock) (let ([r (read-netstring sock)]) (let loop ([l (bytevector->u8-list r)] [headers '()]) (if (null? l) (reverse headers) (let-values ([(tok1 rest1) (header-get-token l)]) (let-values ([(tok2 rest2) (header-get-token rest1)]) (loop rest2 (cons (cons (string->symbol (list-u8->string tok1)) (list-u8->string tok2)) headers)))))))) (define (scgi-headers->bytevector l) (apply bytevector (fold-right (lambda (x acc) (let ([name (car x)] [value (cdr x)]) (append (bytevector->u8-list (string->utf8 name)) '(0) (bytevector->u8-list (string->utf8 value)) '(0) acc))) '() l ))) (define scgi-request-handler (make-parameter (lambda (sock headers content) (printf "scgi: headers: ~a~n" headers) (printf "scgi: contents: ~a~n" content) (put-bytevector sock (string->utf8 "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><center><h1><big>WELCOME TO THUNDERCHEZ!</big></h1></center></body></html>"))))) (define (handle-scgi-connection sock) (define h (read-headers sock)) (assert (string=? "1" (cdr (assq 'SCGI h)))) (let* ([len (string->number (cdr (assq 'CONTENT_LENGTH h)))] [content (get-bytevector-n sock len)]) (assert (= (bytevector-length content) len)) ((scgi-request-handler) sock h content))) (define (run-scgi addr port) (define sock (socket 'inet 'stream '() 0)) (define nchildren 0) (define max-children 10) (define waitpid (foreign-procedure "waitpid" (int void* int) int)) (dynamic-wind (lambda () (bind/inet sock addr port) (listen sock 1000)) (lambda () (do () (#f) ;(printf "nchildren ~d~n" nchildren) (printf "scgi: waiting for connection...~n") (let ([cli #f]) (dynamic-wind (lambda () (set! cli (accept sock))) (lambda () (printf "accepted connection~n") (if (> nchildren max-children) (sleep (make-time 'time-duration 0 1))) (printf "scgi: forking..~n") (let ([pid (fork)]) (if (= pid 0) (guard (e [else (display "scgi: handler error: ") (display-condition e) (newline)]) (handle-scgi-connection cli) (exit)) (set! nchildren (+ 1 nchildren))))) (lambda () (close-port cli)))) (do () ((not (> (waitpid 0 0 (wait-flag 'nohang)) 0))) (set! nchildren (- nchildren 1))))) (lambda () (close-port sock)))) );;library scgi #| ;SERVER EXAMPLE: (import (scgi)) (run-scgi "localhost" 8088) ;; it will use the default scgi-request-handler ;CLIENT EXAMPLE: (import (netstring)) (import (socket)) (define sock (socket 'inet 'stream '() 0)) (connect/inet sock "localhost" 8086) (define h (scgi-headers->bytevector '(("CONTENT_LENGTH" . "10") ("SCGI" . "1") ("REQUEST_METHOD" . "GET") ("REQUEST_URI" . "/chez")))) (write-netstring sock h) (put-bytevector sock (bytevector 1 2 3 4 5 6 7 8 9 0)) (flush-output-port sock) (close-port sock) ;; or just configure nginx with something like this: ;; location /chez { ;; include scgi_params; ;; scgi_pass localhost:8088; ;; scgi_param SCRIPT_NAME "/chez"; ;; } ;; and point your browser to http://localhost/8088 |# |