Check-in [4c5a36a864]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:added more useful scgi example
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4c5a36a86418a867031489801499f8c5666c930b
User & Date: aldo 2016-12-11 15:18:30
Context
2016-12-11
16:11
use textual port for scgi response check-in: 9c4ec6ad9d user: aldo tags: trunk
15:18
added more useful scgi example check-in: 4c5a36a864 user: aldo tags: trunk
14:40
minor scgi fixes check-in: 0d8bb0bf16 user: aldo tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to scgi.sls.

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
    (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 "scgi: 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) 
	(socket)
	(scgi))

(define sock (socket 'inet 'stream '() 0))







<



<
<
>
>
|
|
|
<
|
|
<
|
|
<
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
|
|
|
|
<
<
|









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
    (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 nchildren 0)
    (define max-children 10)
    (define waitpid (foreign-procedure "waitpid" (int void* int) int))


    (call-with-port
     (socket 'inet 'stream '() 0)
     (lambda (sock)	
       (bind/inet sock addr port)
       (listen sock 1000)

       (do ()
	   (#f)

	 (printf "scgi: waiting for connection...~n")
	 (let ([cli #f])

	   (call-with-port
	    (accept sock)
	    (lambda (clifd)
	      (printf "scgi: 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 clifd)
			   (exit))
		    (set! nchildren (+ 1 nchildren)))))))


	 (do ()
	     ((not (> (waitpid 0 0 (wait-flag 'nohang)) 0)))
	   (set! nchildren (- nchildren 1)))))))
     


     );;library scgi



#|

;SERVER EXAMPLE:
(import (scgi))
(run-scgi "localhost" 8088)
;; it will use the default scgi-request-handler

;CUSTOM HANDLER:

(import (chezscheme)
	(scgi)
	(sxml)
	(sxml to-html))
(parameterize
 ([scgi-request-handler
   (lambda (sock headers content)
     (let ([xml (with-output-to-string
		  (lambda ()
		    (SXML->HTML '(html (h1 "WELCOME TO THE WEB!")))))])
       (put-bytevector sock (string->utf8 "Status: 200 OK\r\n"))
       (put-bytevector sock (string->utf8 "Content-Type: text/html\r\n"))
       (put-bytevector sock (string->utf8 "\r\n"))
       (put-bytevector sock (string->utf8 xml))))])
       
 (run-scgi "localhost" 8088))
		


;CLIENT EXAMPLE:
(import (netstring) 
	(socket)
	(scgi))

(define sock (socket 'inet 'stream '() 0))